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
14 changes: 11 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 All @@ -23,6 +23,7 @@ import Text from '@tiptap/extension-text';
import { template } from './template';
import { styles } from './styles';
import type { ToggleButton } from '../toggle-button';
import { TipTapNodeNames } from './types';

declare global {
interface HTMLElementTagNameMap {
Expand Down Expand Up @@ -332,10 +333,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 === TipTapNodeNames.bulletList;
this.numberedListButton.checked = parentList !== undefined
&& parentList.node.type.name === TipTapNodeNames.numberedList;
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
}

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
12 changes: 12 additions & 0 deletions packages/nimble-components/src/rich-text-editor/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
* TipTap node types.
* @public
*/

saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
export const TipTapNodeNames = {
saikrishnan-ni marked this conversation as resolved.
Show resolved Hide resolved
bulletList: 'bulletList',
numberedList: 'orderedList'
} as const;

export type TipTapNodeNames =
(typeof TipTapNodeNames)[keyof typeof TipTapNodeNames];