Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich text editor | Enable input rules for specific formatting options #1549

Merged
merged 21 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f5d1b11
Disabled input rules for bold italics in Tiptap
vivinkrishna-ni Sep 19, 2023
7cd2817
Change files
vivinkrishna-ni Sep 19, 2023
18ccf8b
Fix lint error
vivinkrishna-ni Sep 19, 2023
7ad6b4d
Updated test cases for formatting options
vivinkrishna-ni Sep 19, 2023
0712320
Updated change file description
vivinkrishna-ni Sep 19, 2023
67f7022
Updated the test cases
vivinkrishna-ni Sep 20, 2023
72fd6ec
Merge branch 'main' into users/vivin/disable-input-rules-for-marks
vivinkrishna-ni Sep 20, 2023
773194e
Updated test case name
vivinkrishna-ni Sep 20, 2023
c626147
Merge branch 'users/vivin/disable-input-rules-for-marks' of https://g…
vivinkrishna-ni Sep 20, 2023
66fe9c3
Merge branch 'main' into users/vivin/disable-input-rules-for-marks
vivinkrishna-ni Sep 20, 2023
d14793c
Resolve PR comments
vivinkrishna-ni Sep 20, 2023
841fe66
Merge branch 'users/vivin/disable-input-rules-for-marks' of https://g…
vivinkrishna-ni Sep 20, 2023
66fb153
Merge branch 'main' into users/vivin/disable-input-rules-for-marks
vivinkrishna-ni Sep 20, 2023
17a03eb
Fix lint error
vivinkrishna-ni Sep 20, 2023
8538bf4
Merge branch 'users/vivin/disable-input-rules-for-marks' of https://g…
vivinkrishna-ni Sep 20, 2023
5d6e444
Updated the comment in the code
vivinkrishna-ni Sep 20, 2023
3ee4a3a
Updated the paste event pageobject method to work in firefox
vivinkrishna-ni Sep 20, 2023
db70735
Merge branch 'users/vivin/disable-input-rules-for-marks' of https://g…
vivinkrishna-ni Sep 20, 2023
f95b2e3
Updated the test cases to parameterizeNamedList
vivinkrishna-ni Sep 21, 2023
8f8453b
Fix lint error
vivinkrishna-ni Sep 21, 2023
b59be28
Merge branch 'main' into users/vivin/disable-input-rules-for-marks
vivinkrishna-ni Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Avoid converting markdown syntax characters like star (*) and underscore(_) to format the editor content automatically while typing",
"packageName": "@ni/nimble-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions packages/nimble-components/src/rich-text/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ export class RichTextEditor extends FoundationElement implements ErrorPattern {
*/
return new Editor({
element: this.editor,
// The editor will detect markdown syntax for an input only for these items
// https://tiptap.dev/api/editor#enable-input-rules
enableInputRules: [BulletList, OrderedList],
vivinkrishna-ni marked this conversation as resolved.
Show resolved Hide resolved
vivinkrishna-ni marked this conversation as resolved.
Show resolved Hide resolved
// The editor will not detect markdown syntax when pasting content in any supported items
// Lists do not have any default paste rules, they have only input rules, so disabled paste rules
// https://tiptap.dev/api/editor#enable-paste-rules
enablePasteRules: false,
extensions: [
Document,
Paragraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ export class RichTextEditorPageObject {
toggleButton.control.dispatchEvent(event);
}

public pasteToEditor(text: string): void {
const editor = this.getTiptapEditor();
const pasteEvent = new ClipboardEvent('paste', {
clipboardData: new DataTransfer()
});
pasteEvent.clipboardData?.setData('text/plain', text);
editor!.dispatchEvent(pasteEvent);
}

public async setEditorTextContent(value: string): Promise<void> {
const lastElement = this.getEditorLastChildElement();
const textNode = document.createTextNode(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { html } from '@microsoft/fast-element';
import { richTextEditorTag, RichTextEditor } from '..';
import { type Fixture, fixture } from '../../../utilities/tests/fixture';
import { getSpecTypeByNamedList } from '../../../utilities/tests/parameterized';
import {
getSpecTypeByNamedList,
parameterizeNamedList
} from '../../../utilities/tests/parameterized';
import { RichTextEditorPageObject } from '../testing/rich-text-editor.pageobject';
import { wackyStrings } from '../../../utilities/tests/wacky-strings';
import type { Button } from '../../../button';
Expand Down Expand Up @@ -390,6 +393,125 @@ describe('RichTextEditor', () => {
]);
});

describe('should render as a plain text for bold and italics input rule entered into the editor', () => {
const markdownInput = [
{ name: 'bold(**)', input: '**bold**' },
{ name: 'bold(__)', input: '__bold__' },
{ name: 'italics(*)', input: '*italics*' },
{ name: 'italics(_)', input: '_italics_' }
] as const;
parameterizeNamedList(markdownInput, (spec, name, value) => {
spec(`for ${name} markdown input to the editor`, async () => {
await pageObject.setEditorTextContent(value.input);

expect(pageObject.getEditorTagNames()).toEqual(['P']);
expect(pageObject.getEditorLeafContents()).toEqual([
value.input
]);
});
});
});

describe('should render as lists when its input rule is entered into the editor', () => {
const markdownInput = [
{ name: 'bullet list', input: '*', tagName: 'UL' },
{ name: 'bullet list', input: '+', tagName: 'UL' },
{ name: 'bullet list', input: '-', tagName: 'UL' },
{ name: 'numbered list', input: '1.', tagName: 'OL' },
{ name: 'numbered list', input: '5.', tagName: 'OL' }
] as const;
parameterizeNamedList(markdownInput, (spec, name, value) => {
spec(`for ${name} markdown input to the editor`, async () => {
await pageObject.setEditorTextContent(value.input);
await pageObject.pressEnterKeyInEditor();
await pageObject.setEditorTextContent(value.name);

expect(
pageObject.getEditorTagNamesWithClosingTags()
).toEqual([
value.tagName,
'LI',
'P',
'/P',
'/LI',
`/${value.tagName}`
]);
expect(pageObject.getEditorLeafContents()).toEqual([
value.name
]);
});
});
});

describe('should render as a plain text for all supported markdown strings are pasted into the editor', () => {
const markdownInput = [
{ name: 'bold(**)', input: '**bold**' },
{ name: 'bold(__)', input: '__bold__' },
{ name: 'italics(*)', input: '*italics*' },
{ name: 'italics(_)', input: '_italics_' },
{ name: 'bullet list(*)', input: '* ' },
{ name: 'bullet list(+)', input: '+ ' },
{ name: 'bullet list(-)', input: '- ' },
{ name: 'numbered list(1.)', input: '1. ' },
{ name: 'numbered list(5.)', input: '5. ' },
{ name: 'autolink(<https>)', input: '<https://nimble.ni.dev>' },
{ name: 'autolink(<http>)', input: '<http://nimble.ni.dev>' },
{ name: 'autolink(<ftp>)', input: '<ftp://example>' },
{ name: 'hard break', input: 'hard\\nbreak' },
{ name: 'blockquote', input: '> blockquote' },
{ name: 'code', input: '`code`' },
{ name: 'fence', input: '```fence```' },
{ name: 'strikethrough', input: '~~strikethrough~~' },
{ name: 'heading 1', input: '# heading 1' },
{ name: 'heading 2', input: '## heading 2' },
{ name: 'heading 3', input: '### heading 3' },
{ name: 'hyperlink', input: '[link](url)' },
{
name: 'reference',
input: '[ref][link] [link]:url'
},
{ name: 'image', input: '![Text](Image)' },
{ name: 'horizontal rule(-)', input: '---' },
{ name: 'horizontal rule(*)', input: '***' },
{ name: 'horizontal rule(_)', input: '___' },
{ name: 'Infinity', input: '-Infinity' },
{ name: 'entity', input: '&nbsp;' },
{
name: 'symbols',
input: '(c) (C) (r) (R) (tm) (TM) (p) (P) +-'
},
{ name: 'html string(p)', input: '<div><p>text</p></div>' },
{ name: 'html string(b)', input: '<b>not bold</b>' },
{ name: 'html string(em)', input: '<em>not italic</em>' },
{
name: 'html string(ol)',
input: '<ol><li>not list</li><li>not list</li></ol>'
},
{
name: 'html string(ul)',
input: '<ul><li>not list</li><li>not list</li></ul>'
},
{
name: 'html string(a)',
input: '<a href="https://nimble.ni.dev/">https://nimble.ni.dev/</a>'
},
{
name: 'html string(script)',
input: '<script>alert("not alert")</script>'
}
] as const;
parameterizeNamedList(markdownInput, (spec, name, value) => {
spec(`for ${name} markdown syntax to the editor`, () => {
pageObject.pasteToEditor(value.input);

expect(pageObject.getEditorTagNames()).toEqual(['P']);
expect(pageObject.getEditorLeafContents()).toEqual([
value.input
]);
});
});
});

it('should have br tag name when pressing shift + Enter with numbered list content', async () => {
await pageObject.setEditorTextContent('numbered list1');
await pageObject.clickFooterButton(ToolbarButton.numberedList);
Expand Down
Loading