Skip to content
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 @@ -47,8 +47,35 @@
({ commands, editor }) => {
if (!className) return false;
const types = type ? [type] : this.options.types;
const existing = types.map((type) => editor.getAttributes(type)?.class as string).filter((x) => x);
return existing.length ? commands.unsetClassName(type) : commands.setClassName(className, type);

const toggleClasses = className.split(/\s+/).filter((c) => c);
if (toggleClasses.length === 0) {
return true;
}

return types
.map((t) => {
const existingClass = (editor.getAttributes(t)?.class as string) ?? '';
const classes = existingClass.split(/\s+/).filter((c) => c);
const hasAllToggleClasses = toggleClasses.every((c) => classes.includes(c));

let newClasses: Array<string>;
if (hasAllToggleClasses) {
// All toggle classes present: remove them
newClasses = classes.filter((c) => !toggleClasses.includes(c));
} else {
// Not all toggle classes present: add missing ones
const toAdd = toggleClasses.filter((c) => !classes.includes(c));
newClasses = [...classes, ...toAdd];
}

if (newClasses.length === 0) {
// No classes left, remove the attribute entirely
return commands.resetAttributes(t, 'class');
}
return commands.updateAttributes(t, { class: newClasses.join(' ') });
})
.every((response) => response);

Check warning on line 78 in src/Umbraco.Web.UI.Client/src/packages/tiptap/extensions/html-attr-class/html-attr-class.tiptap-extension.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Complex Method

addCommands has a cyclomatic complexity of 10, 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.
},
unsetClassName:
(type) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export const UmbLink = Link.extend({

addCommands() {
return {
// TODO: [v17] Remove the `@ts-expect-error` once Tiptap has resolved the TypeScript definitions. [LK:2025-10-01]
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
ensureUmbLink: (attributes) => {
// TODO: [v17] Remove the `@ts-expect-error` once Tiptap has resolved the TypeScript definitions. [LK:2025-10-01]
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return ({ editor, chain }) => {
if (editor.isActive(this.name)) {
return true;
}
return chain().setMark(this.name, attributes).setMeta('preventAutolink', true).run();
};
},
// TODO: [v17] Remove the `@ts-expect-error` once Tiptap has resolved the TypeScript definitions. [LK:2025-10-01]
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Expand All @@ -59,6 +73,14 @@ export const UmbLink = Link.extend({
declare module '@tiptap/core' {
interface Commands<ReturnType> {
umbLink: {
ensureUmbLink: (attributes: {
type: string;
href: string;
'data-anchor'?: string | null;
target?: string | null;
title?: string | null;
}) => ReturnType;

setUmbLink: (attributes: {
type: string;
href: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ChainedCommands, Editor } from '../../externals.js';

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

Expand All @@ -25,6 +25,7 @@ export default class UmbTiptapToolbarStyleMenuApi extends UmbTiptapToolbarElemen
h5: this.#headingCommand(5),
h6: this.#headingCommand(6),
p: { type: 'paragraph', command: (chain) => chain.setParagraph() },
a: { type: 'umbLink', command: (chain) => chain.ensureUmbLink({ type: 'external', href: '#' }) },
blockquote: { type: 'blockquote', command: (chain) => chain.toggleBlockquote() },
code: { type: 'code', command: (chain) => chain.toggleCode() },
codeBlock: { type: 'codeBlock', command: (chain) => chain.toggleCodeBlock() },
Expand Down
Loading