Skip to content

Commit

Permalink
Fix cases around newlines and whitespace (#1619)
Browse files Browse the repository at this point in the history
* Fix cases around newlines and whitespace

* Add changeset
  • Loading branch information
nemanja-tosic authored Jun 23, 2022
1 parent 465d5d9 commit 303a818
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .changeset/stupid-tomatoes-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@udecode/plate-mention': patch
'@udecode/plate-test-utils': patch
---

Merge lines, strip whitespaces
9 changes: 1 addition & 8 deletions packages/nodes/code-block/src/insertDataCodeBlock.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
import { createPlateUIEditor } from '@udecode/plate';
import { PlateEditor } from '@udecode/plate-core';
import { createParagraphPlugin } from '@udecode/plate-paragraph';
import { jsx } from '@udecode/plate-test-utils';
import { createDataTransfer, jsx } from '@udecode/plate-test-utils';
import { createCodeBlockPlugin } from './createCodeBlockPlugin';

jsx;

const createDataTransfer = (dataMap: Map<string, any> = new Map()) => {
return ({
getData: (key: string) => dataMap.get(key) ?? '',
setData: (key: string, value: string) => dataMap.set(key, value),
} as unknown) as DataTransfer;
};

const editorTest = (input: any, data: DataTransfer, expected: any) => {
const plugins = [createParagraphPlugin(), createCodeBlockPlugin()];

Expand Down
126 changes: 102 additions & 24 deletions packages/nodes/mention/src/withMention.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
ComboboxState,
} from '@udecode/plate-combobox';
import { moveSelection, PlateEditor, select, Value } from '@udecode/plate-core';
import { createDataTransfer, jsx } from '@udecode/plate-test-utils';
import {
createDataTransfer,
DataTransferDataMap,
jsx,
} from '@udecode/plate-test-utils';
import { Range } from 'slate';
import { createEditorWithMentions } from './__tests__/createEditorWithMentions';
import { getMentionOnSelectItem } from './getMentionOnSelectItem';
Expand Down Expand Up @@ -482,36 +486,110 @@ describe('withMention', () => {
});

describe('paste', () => {
it('should paste the clipboard contents into mention as text', () => {
const editor = createEditorWithMentionInput(
<hp>
<cursor />
</hp>
);
const testPaste: (
data: DataTransferDataMap,
input: JSX.Element,
expected: JSX.Element
) => void = (data, input, expected) => {
const editor = createEditorWithMentionInput(input);

const fragment = createDataTransfer(
new Map([
[
'text/html',
'<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body>hello</body></html>',
],
['text/plain', 'hello'],
])
);
editor.insertData(createDataTransfer(data));

editor.insertData(fragment);
expect(editor.children).toEqual([expected]);
};

expect(editor.children).toEqual([
const testPasteBasic: (
data: DataTransferDataMap,
expected: string
) => void = (data, expected) => {
testPaste(
data,
<hp>
<cursor />
</hp>,
<hp>
<htext />
<hmentioninput trigger={trigger}>hello</hmentioninput>
<hmentioninput trigger={trigger}>{expected}</hmentioninput>
<htext />
</hp>,
]);
</hp>
);
};

type PasteTestCase = {
data: DataTransferDataMap;
expected: string;
};

const basePasteTestSuite = ({
simple,
whitespace,
newLine,
newLineAndWhitespace,
}: {
simple: PasteTestCase;
whitespace: PasteTestCase;
newLine: PasteTestCase;
newLineAndWhitespace: PasteTestCase;
}): void => {
it('should paste the clipboard contents into mention as text', () =>
testPasteBasic(simple.data, simple.expected));

it('should merge lines', () =>
testPasteBasic(newLine.data, newLine.expected));

it('should trim the text', () =>
testPasteBasic(whitespace.data, whitespace.expected));

it('should trim every line before merging', () =>
testPasteBasic(
newLineAndWhitespace.data,
newLineAndWhitespace.expected
));
};

describe('html', () => {
basePasteTestSuite({
simple: {
data: new Map([['text/html', '<html><body>hello</body></html>']]),
expected: 'hello',
},
whitespace: {
data: new Map([['text/html', '<html><body> hello </body></html>']]),
expected: 'hello',
},
newLine: {
data: new Map([
['text/html', '<html><body>hello<br>world</body></html>'],
]),
expected: 'helloworld',
},
newLineAndWhitespace: {
data: new Map([
['text/html', '<html><body> hello <br> world </body></html>'],
]),
expected: 'helloworld',
},
});
});

expect(editor.selection).toEqual<Range>({
anchor: { path: [0, 1, 0], offset: 5 },
focus: { path: [0, 1, 0], offset: 5 },
describe('plain text', () => {
basePasteTestSuite({
simple: {
data: new Map([['text/plain', 'hello']]),
expected: 'hello',
},
whitespace: {
data: new Map([['text/plain', ' hello ']]),
expected: 'hello',
},
newLine: {
data: new Map([['text/plain', 'hello\r\nworld\n!\r!']]),
expected: 'helloworld!!',
},
newLineAndWhitespace: {
data: new Map([['text/plain', ' hello \r\n world \n ! \r ! ']]),
expected: 'helloworld!!',
},
});
});
});
Expand Down
26 changes: 25 additions & 1 deletion packages/nodes/mention/src/withMention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ export const withMention = <
insertText: _insertText,
deleteBackward,
insertFragment: _insertFragment,
insertTextData,
} = editor;

const stripNewLineAndTrim: (text: string) => string = (text) => {
return text
.split(/\r\n|\r|\n/)
.map((line) => line.trim())
.join('');
};

editor.insertFragment = (fragment) => {
const inMentionInput = findMentionInput(editor) !== undefined;
if (!inMentionInput) {
Expand All @@ -52,10 +60,26 @@ export const withMention = <

return insertText(
editor,
fragment.map((node) => getNodeString(node)).join('')
fragment.map((node) => stripNewLineAndTrim(getNodeString(node))).join('')
);
};

editor.insertTextData = (data) => {
const inMentionInput = findMentionInput(editor) !== undefined;
if (!inMentionInput) {
return insertTextData(data);
}

const text = data.getData('text/plain');
if (!text) {
return false;
}

editor.insertText(stripNewLineAndTrim(text));

return true;
};

editor.deleteBackward = (unit) => {
const currentMentionInput = findMentionInput(editor);
if (currentMentionInput && getNodeString(currentMentionInput[0]) === '') {
Expand Down
10 changes: 7 additions & 3 deletions packages/test-utils/src/createDataTransfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const createDataTransfer = (dataMap: Map<string, any> = new Map()) =>
export type DataTransferDataType = 'text/plain' | 'text/html' | string;

export type DataTransferDataMap = Map<DataTransferDataType, unknown>;

export const createDataTransfer = (dataMap: DataTransferDataMap = new Map()) =>
(({
getData: (key: string) => dataMap.get(key) ?? '',
setData: (key: string, value: string) => dataMap.set(key, value),
getData: (type: string) => dataMap.get(type) ?? '',
setData: (type: string, value: string) => dataMap.set(type, value),
} as unknown) as DataTransfer);

1 comment on commit 303a818

@vercel
Copy link

@vercel vercel bot commented on 303a818 Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

plate – ./

plate-git-main-udecode.vercel.app
plate-udecode.vercel.app
plate.udecode.io
www.plate.udecode.io

Please sign in to comment.