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 | Fix for Editor's blinking caret disappearing issue in Firefox browser #1455

Merged
merged 16 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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 @@
{
aagash-ni marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch",
"comment": "Fix for Firefox caret disappearing issue",
"packageName": "@ni/nimble-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
aagash-ni marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions packages/nimble-components/src/rich-text-editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class RichTextEditor extends FoundationElement {
*/
public boldButtonClick(): void {
this.tiptapEditor.chain().focus().toggleBold().run();
this.forceFocusEditor();
}

/**
Expand All @@ -110,6 +111,7 @@ export class RichTextEditor extends FoundationElement {
public boldButtonKeyDown(event: KeyboardEvent): boolean {
if (this.keyActivatesButton(event)) {
this.tiptapEditor.chain().focus().toggleBold().run();
this.forceFocusEditor();
return false;
}
return true;
Expand All @@ -121,6 +123,7 @@ export class RichTextEditor extends FoundationElement {
*/
public italicsButtonClick(): void {
this.tiptapEditor.chain().focus().toggleItalic().run();
this.forceFocusEditor();
}

/**
Expand All @@ -130,6 +133,7 @@ export class RichTextEditor extends FoundationElement {
public italicsButtonKeyDown(event: KeyboardEvent): boolean {
if (this.keyActivatesButton(event)) {
this.tiptapEditor.chain().focus().toggleItalic().run();
this.forceFocusEditor();
return false;
}
return true;
Expand All @@ -141,6 +145,7 @@ export class RichTextEditor extends FoundationElement {
*/
public bulletListButtonClick(): void {
this.tiptapEditor.chain().focus().toggleBulletList().run();
this.forceFocusEditor();
}

/**
Expand All @@ -150,6 +155,7 @@ export class RichTextEditor extends FoundationElement {
public bulletListButtonKeyDown(event: KeyboardEvent): boolean {
if (this.keyActivatesButton(event)) {
this.tiptapEditor.chain().focus().toggleBulletList().run();
this.forceFocusEditor();
return false;
}
return true;
Expand All @@ -161,6 +167,7 @@ export class RichTextEditor extends FoundationElement {
*/
public numberedListButtonClick(): void {
this.tiptapEditor.chain().focus().toggleOrderedList().run();
this.forceFocusEditor();
}

/**
Expand All @@ -170,6 +177,7 @@ export class RichTextEditor extends FoundationElement {
public numberedListButtonKeyDown(event: KeyboardEvent): boolean {
if (this.keyActivatesButton(event)) {
this.tiptapEditor.chain().focus().toggleOrderedList().run();
this.forceFocusEditor();
return false;
}
return true;
Expand Down Expand Up @@ -347,6 +355,15 @@ export class RichTextEditor extends FoundationElement {
return false;
}
}

// In Firefox browser, once the editor gets focused, the blinking caret will be visible until we click format buttons (Bold, Italic ...) in the Firefox browser (changing focus).
// But once any of the toolbar button is clicked, editor internally has its focus but the blinking caret disappears.
// As a workaround, manually triggering blur and setting focus on editor makes the blinking caret to re-appear.
// Mozilla issue https://bugzilla.mozilla.org/show_bug.cgi?id=1496769 tracks removal of this workaround.
private forceFocusEditor(): void {
this.tiptapEditor.commands.blur();
this.tiptapEditor.commands.focus();
}
}

const nimbleRichTextEditor = RichTextEditor.compose({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ export class RichTextEditorPageObject {
.map(el => el.textContent || '');
}

private getEditorSection(): Element | null | undefined {
return this.richTextEditorElement.shadowRoot?.querySelector('.editor');
public isRichTextEditorActiveElement(): boolean {
return (
document.activeElement === this.richTextEditorElement
&& document.activeElement?.shadowRoot?.activeElement
=== this.getTiptapEditor()
);
}

private getTiptapEditor(): Element | null | undefined {
Expand All @@ -174,4 +178,8 @@ export class RichTextEditorPageObject {
);
return buttons[index];
}

private getEditorSection(): Element | null | undefined {
return this.richTextEditorElement.shadowRoot?.querySelector('.editor');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ describe('RichTextEditor', () => {
expect(okButtonSpy).toHaveBeenCalledTimes(1);
});

it('Should return editor as active element after clicking formatting button', async () => {
await pageObject.setEditorTextContent('Sample Text');
await pageObject.clickFooterButton(ToolbarButton.bulletList);
expect(pageObject.isRichTextEditorActiveElement()).toBeTrue();
});

const formattingButtons: {
name: string,
toolbarButtonIndex: ToolbarButton,
Expand Down