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 nested list highlighting #1462

Merged
merged 11 commits into from
Sep 1, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "updates the list button to be higlighted only on last node type",
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
"packageName": "@ni/nimble-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 14 additions & 3 deletions packages/nimble-components/src/rich-text-editor/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { observable } from '@microsoft/fast-element';
import { DesignSystem, FoundationElement } from '@microsoft/fast-foundation';
import { keyEnter, keySpace } from '@microsoft/fast-web-utilities';
import { Editor } from '@tiptap/core';
import { Editor, findParentNode, isList } from '@tiptap/core';
import {
schema,
defaultMarkdownParser,
Expand Down Expand Up @@ -70,6 +70,10 @@ export class RichTextEditor extends FoundationElement {
private readonly markdownSerializer = this.initializeMarkdownSerializer();
private readonly domSerializer = DOMSerializer.fromSchema(schema);
private readonly xmlSerializer = new XMLSerializer();
private readonly tipTapNodeNames = {
bulletList: 'bulletList',
numberedList: 'orderedList'
} as const;
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved

public constructor() {
super();
Expand Down Expand Up @@ -332,10 +336,17 @@ export class RichTextEditor extends FoundationElement {
}

private updateEditorButtonsState(): void {
const { extensionManager, state } = this.tiptapEditor;
const { extensions } = extensionManager;
const { selection } = state;
const parentList = findParentNode(node => isList(node.type.name, extensions))(selection);

this.boldButton.checked = this.tiptapEditor.isActive('bold');
this.italicsButton.checked = this.tiptapEditor.isActive('italic');
this.bulletListButton.checked = this.tiptapEditor.isActive('bulletList');
this.numberedListButton.checked = this.tiptapEditor.isActive('orderedList');
this.bulletListButton.checked = parentList !== undefined
&& parentList.node.type.name === this.tipTapNodeNames.bulletList;
this.numberedListButton.checked = parentList !== undefined
&& parentList.node.type.name === this.tipTapNodeNames.numberedList;
}

private keyActivatesButton(event: KeyboardEvent): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ describe('RichTextEditor', () => {
]);
expect(
pageObject.getButtonCheckedState(ToolbarButton.numberedList)
).toBeTrue();
).toBeFalse();
expect(
pageObject.getButtonCheckedState(ToolbarButton.bulletList)
).toBeTrue();
Expand Down Expand Up @@ -504,7 +504,7 @@ describe('RichTextEditor', () => {
).toBeTrue();
expect(
pageObject.getButtonCheckedState(ToolbarButton.bulletList)
).toBeTrue();
).toBeFalse();
});

it('should have "ul" tag names for bullet lists when clicking "tab" to make it nested and "shift+Tab" to make it usual list', async () => {
Expand Down Expand Up @@ -541,6 +541,24 @@ describe('RichTextEditor', () => {
).toBeTrue();
});

it('should have only inner most nested list type button active', async () => {
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
await pageObject.setEditorTextContent('List');
await pageObject.clickFooterButton(ToolbarButton.bulletList);
expect(
pageObject.getButtonCheckedState(ToolbarButton.bulletList)
).toBeTrue();
await pageObject.pressEnterKeyInEditor();
await pageObject.pressTabKeyInEditor();
await pageObject.setEditorTextContent('Nested List');
await pageObject.clickFooterButton(ToolbarButton.numberedList);
expect(
pageObject.getButtonCheckedState(ToolbarButton.bulletList)
).toBeFalse();
expect(
pageObject.getButtonCheckedState(ToolbarButton.numberedList)
).toBeTrue();
});

it('should have "strong" and "em" tag name for both bold and italics button clicks', async () => {
await pageObject.clickFooterButton(ToolbarButton.bold);
await pageObject.clickFooterButton(ToolbarButton.italics);
Expand Down