Skip to content

Tiptap RTE: Toolbar menu active highlighting #19532

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

Merged
merged 6 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -9,6 +9,7 @@
element?: HTMLElement;
separatorAfter?: boolean;
style?: string;
isActive?: () => boolean | undefined;
execute?: () => void;
};

Expand All @@ -21,8 +22,12 @@
return this.shadowRoot?.querySelector(`#${popoverId}`) as UUIPopoverContainerElement;
}

#onMouseEnter(item: UmbCascadingMenuItem, popoverId: string) {
if (!item.items?.length) return;
#isMenuActive(items?: UmbCascadingMenuItem[]): boolean {
return !!items?.some((item) => item.isActive?.() || this.#isMenuActive(item.items));
}

#onMouseEnter(item: UmbCascadingMenuItem, popoverId?: string) {
if (!item.items?.length || !popoverId) return;

const popover = this.#getPopoverById(popoverId);
if (!popover) return;
Expand All @@ -33,7 +38,9 @@
popover.showPopover();
}

#onMouseLeave(item: UmbCascadingMenuItem, popoverId: string) {
#onMouseLeave(item: UmbCascadingMenuItem, popoverId?: string) {
if (!popoverId) return;

const popover = this.#getPopoverById(popoverId);
if (!popover) return;

Expand All @@ -43,12 +50,16 @@
popover.hidePopover();
}

#onClick(item: UmbCascadingMenuItem, popoverId: string) {
#onClick(item: UmbCascadingMenuItem, popoverId?: string) {
item.execute?.();

setTimeout(() => {
this.#onMouseLeave(item, popoverId);
}, 100);
if (!popoverId) {
setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.hidePopover();
}, 100);
}
}

override render() {
Expand All @@ -64,38 +75,46 @@
}

#renderItem(item: UmbCascadingMenuItem, index: number) {
const popoverId = `item-${index}`;
const popoverId = item.items ? `menu-${index}` : undefined;

const element = item.element;
if (element) {
if (element && popoverId) {
element.setAttribute('popovertarget', popoverId);
}

const label = this.localize.string(item.label);
const isActive = item.isActive?.() || this.#isMenuActive(item.items) || false;

return html`
<div
@mouseenter=${() => this.#onMouseEnter(item, popoverId)}
@mouseleave=${() => this.#onMouseLeave(item, popoverId)}>
${when(
element,
() => element,
() => html`
<uui-menu-item
class=${item.separatorAfter ? 'separator' : ''}
label=${label}
popovertarget=${popoverId}
popovertarget=${ifDefined(popoverId)}
select-mode="highlight"
?selected=${isActive}
@click-label=${() => this.#onClick(item, popoverId)}>
${when(item.icon, (icon) => html`<uui-icon slot="icon" name=${icon}></uui-icon>`)}
<div slot="label" class="menu-item">
<span style=${ifDefined(item.style)}>${label}</span>
${when(item.items, () => html`<uui-symbol-expand></uui-symbol-expand>`)}
</div>
</uui-menu-item>
`,
)}
<umb-cascading-menu-popover id=${popoverId} placement="right-start" .items=${item.items}>
</umb-cascading-menu-popover>
${when(
popoverId,
(popoverId) => html`
<umb-cascading-menu-popover id=${popoverId} placement="right-start" .items=${item.items}>
</umb-cascading-menu-popover>
`,
)}

Check warning on line 117 in src/Umbraco.Web.UI.Client/src/packages/tiptap/components/cascading-menu-popover/cascading-menu-popover.element.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

UmbCascadingMenuPopoverElement.renderItem has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
</div>
`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@
import type { MetaTiptapToolbarStyleMenuItem } from '../../extensions/types.js';
import type { ChainedCommands, Editor } from '@umbraco-cms/backoffice/external/tiptap';

type UmbTiptapToolbarStyleMenuCommandType = {
type: string;
command: (chain: ChainedCommands) => ChainedCommands;
isActive?: (editor?: Editor) => boolean | undefined;
};

export default class UmbTiptapToolbarStyleMenuApi extends UmbTiptapToolbarElementApiBase {
#commands: Record<string, { type: string; command: (chain: ChainedCommands) => ChainedCommands }> = {
h1: { type: 'heading', command: (chain) => chain.toggleHeading({ level: 1 }) },
h2: { type: 'heading', command: (chain) => chain.toggleHeading({ level: 2 }) },
h3: { type: 'heading', command: (chain) => chain.toggleHeading({ level: 3 }) },
h4: { type: 'heading', command: (chain) => chain.toggleHeading({ level: 4 }) },
h5: { type: 'heading', command: (chain) => chain.toggleHeading({ level: 5 }) },
h6: { type: 'heading', command: (chain) => chain.toggleHeading({ level: 6 }) },
#headingCommand(level: 1 | 2 | 3 | 4 | 5 | 6): UmbTiptapToolbarStyleMenuCommandType {
return {
type: 'heading',
command: (chain) => chain.toggleHeading({ level }),
isActive: (editor) => editor?.isActive('heading', { level }),
};
}

#commands: Record<string, UmbTiptapToolbarStyleMenuCommandType> = {
h1: this.#headingCommand(1),
h2: this.#headingCommand(2),
h3: this.#headingCommand(3),
h4: this.#headingCommand(4),
h5: this.#headingCommand(5),
h6: this.#headingCommand(6),
p: { type: 'paragraph', command: (chain) => chain.setParagraph() },
blockquote: { type: 'blockquote', command: (chain) => chain.toggleBlockquote() },
code: { type: 'code', command: (chain) => chain.toggleCode() },
Expand All @@ -24,6 +38,20 @@
ul: { type: 'bulletList', command: (chain) => chain.toggleBulletList() },
};

override isActive(editor?: Editor, item?: MetaTiptapToolbarStyleMenuItem) {
if (!editor || !item?.data) return false;

const { tag, id, class: className } = item.data;
const ext = tag ? this.#commands[tag] : null;
const attrs = editor?.getAttributes(ext?.type ?? 'paragraph');

const tagMatch = !tag ? true : ext ? (ext.isActive?.(editor) ?? editor?.isActive(ext.type) ?? false) : false;
const idMatch = !id ? true : attrs.id === id;
const classMatch = !className ? true : attrs.class?.includes(className) === true;

return tagMatch && idMatch && classMatch;
}

Check warning on line 53 in src/Umbraco.Web.UI.Client/src/packages/tiptap/components/toolbar/style-menu.tiptap-toolbar-api.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

UmbTiptapToolbarStyleMenuApi.isActive has a cyclomatic complexity of 16, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

override execute(editor?: Editor, item?: MetaTiptapToolbarStyleMenuItem) {
if (!editor || !item?.data) return;
const { tag, id, class: className } = item.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,18 @@
style: item.appearance?.style ?? item.style,
separatorAfter: item.separatorAfter,
element,
isActive: () => this.api?.isActive(this.editor, item),

Check warning on line 102 in src/Umbraco.Web.UI.Client/src/packages/tiptap/components/toolbar/tiptap-toolbar-menu.element.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

UmbTiptapToolbarMenuElement.getMenuItem has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
execute: () => this.api?.execute(this.editor, item),
};
}

#isMenuActive(items?: UmbCascadingMenuItem[]): boolean {
return !!items?.some((item) => item.isActive?.() || this.#isMenuActive(item.items));
}

readonly #onEditorUpdate = () => {
if (this.api && this.editor && this.manifest) {
this.isActive = this.api.isActive(this.editor);
this.isActive = this.api.isActive(this.editor) || this.#isMenuActive(this.#menu) || false;
}
};

Expand All @@ -117,8 +122,8 @@
() => html`
<uui-button
compact
look=${this.isActive ? 'outline' : 'default'}
label=${ifDefined(label)}
look=${this.isActive ? 'outline' : 'default'}
title=${label}
popovertarget="popover-menu">
${when(
Expand All @@ -130,7 +135,11 @@
</uui-button>
`,
() => html`
<uui-button compact label=${ifDefined(label)} popovertarget="popover-menu">
<uui-button
compact
label=${ifDefined(label)}
look=${this.isActive ? 'outline' : 'default'}
popovertarget="popover-menu">
<span>${label}</span>
<uui-symbol-expand slot="extra" open></uui-symbol-expand>
</uui-button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class UmbTiptapTableToolbarMenuElement extends UmbTiptapToolbarMenuElemen
`,
)}
${this.renderMenu()}
<uui-popover-container id="popover-insert">
<uui-popover-container id="popover-insert" style="box-shadow: var(--uui-shadow-depth-3);">
<umb-tiptap-table-insert .editor=${this.editor}></umb-tiptap-table-insert>
</uui-popover-container>
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class UmbTiptapToolbarTableExtensionApi extends UmbTiptapToolbarElementAp
tableProperties: (editor) => this.#tableProperties(editor),
};

override isActive(editor?: Editor, item?: unknown) {
if (!item) return super.isActive(editor);
return false;
}

async #tableProperties(editor?: Editor) {
if (!editor || !editor.isActive('table')) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import type { MetaTiptapToolbarMenuItem } from '../types.js';
import type { Editor } from '@umbraco-cms/backoffice/external/tiptap';

export default class UmbTiptapToolbarFontFamilyExtensionApi extends UmbTiptapToolbarElementApiBase {
override isActive(editor?: Editor, item?: MetaTiptapToolbarMenuItem) {
const styles = editor?.getAttributes('span')?.style;
return styles?.includes(`font-family: ${item?.data};`) === true;
}

override execute(editor?: Editor, item?: MetaTiptapToolbarMenuItem) {
if (!item?.data) return;
editor?.chain().focus().toggleSpanStyle(`font-family: ${item.data};`).run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import type { MetaTiptapToolbarMenuItem } from '../types.js';
import type { Editor } from '@umbraco-cms/backoffice/external/tiptap';

export default class UmbTiptapToolbarFontFamilyExtensionApi extends UmbTiptapToolbarElementApiBase {
override isActive(editor?: Editor, item?: MetaTiptapToolbarMenuItem) {
const styles = editor?.getAttributes('span')?.style;
return styles?.includes(`font-size: ${item?.data};`) === true;
}

override execute(editor?: Editor, item?: MetaTiptapToolbarMenuItem) {
if (!item?.data) return;
editor?.chain().focus().toggleSpanStyle(`font-size: ${item.data};`).run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface UmbTiptapToolbarElementApi extends UmbApi, UmbTiptapExtensionAr
/**
* Checks if the toolbar element is active.
*/
isActive(editor?: Editor): boolean;
isActive(editor?: Editor, ...args: Array<unknown>): boolean;

/**
* Checks if the toolbar element is disabled.
Expand Down
Loading