From 3c05bf2f0b7ad2a5fed0d5f39185b28409c6eaa0 Mon Sep 17 00:00:00 2001 From: Nick Perez Date: Mon, 2 Dec 2024 13:59:46 +0100 Subject: [PATCH] revert: "fix: remove empty text style handling (#5905)" (#5906) This reverts commit df40b7f1a4dc5920f896c096a6778134e490c445. --- .changeset/yellow-cats-teach.md | 6 ---- .../extension-font-family/src/font-family.ts | 10 ++---- .../extension-text-style/src/text-style.ts | 32 +++++-------------- 3 files changed, 10 insertions(+), 38 deletions(-) delete mode 100644 .changeset/yellow-cats-teach.md diff --git a/.changeset/yellow-cats-teach.md b/.changeset/yellow-cats-teach.md deleted file mode 100644 index 8b7f50acf..000000000 --- a/.changeset/yellow-cats-teach.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@tiptap/extension-font-family": minor -"@tiptap/extension-text-style": minor ---- - -fix: #4311 - update the logic of removeEmptyTextStyle to manually handle the selection of all of the nodes within the selection to check for their marks independently to fix an issue where unsetting the font family on a selection would remove all applied text style marks from the selection as well diff --git a/packages/extension-font-family/src/font-family.ts b/packages/extension-font-family/src/font-family.ts index 208ce15af..ac57f521c 100644 --- a/packages/extension-font-family/src/font-family.ts +++ b/packages/extension-font-family/src/font-family.ts @@ -72,15 +72,9 @@ export const FontFamily = Extension.create({ .setMark('textStyle', { fontFamily }) .run() }, - unsetFontFamily: () => ({ chain, tr }) => { - const { selection } = tr - - // Retrieve all previous mark attributes applied to the current selection - const previousMarkAttributes = selection.$anchor.marks().map(mark => mark.attrs) - + unsetFontFamily: () => ({ chain }) => { return chain() - // Only remove the font family attribute from the previous mark attributes - .setMark('textStyle', { ...previousMarkAttributes, fontFamily: null }) + .setMark('textStyle', { fontFamily: null }) .removeEmptyTextStyle() .run() }, diff --git a/packages/extension-text-style/src/text-style.ts b/packages/extension-text-style/src/text-style.ts index 6c1bb7a1d..80c0da06b 100644 --- a/packages/extension-text-style/src/text-style.ts +++ b/packages/extension-text-style/src/text-style.ts @@ -1,4 +1,5 @@ import { + getMarkAttributes, Mark, mergeAttributes, } from '@tiptap/core' @@ -63,32 +64,15 @@ export const TextStyle = Mark.create({ addCommands() { return { - removeEmptyTextStyle: () => ({ tr }) => { + removeEmptyTextStyle: () => ({ state, commands }) => { + const attributes = getMarkAttributes(state, this.type) + const hasStyles = Object.entries(attributes).some(([, value]) => !!value) - const { selection } = tr + if (hasStyles) { + return true + } - // Gather all of the nodes within the selection range. - // We would need to go through each node individually - // to check if it has any inline style attributes. - // Otherwise, calling commands.unsetMark(this.name) - // removes everything from all the nodes - // within the selection range. - tr.doc.nodesBetween(selection.from, selection.to, (node, pos) => { - - // Check if it's a paragraph element, if so, skip this node as we apply - // the text style to inline text nodes only (span). - if (node.isTextblock) { - return true - } - - // Check if the node has no inline style attributes. - if (!node.marks.some(mark => Object.values(mark.attrs).some(value => !!value))) { - // Proceed with the removal of the `textStyle` mark for this node only - tr.removeMark(pos, pos + node.nodeSize, this.type) - } - }) - - return true + return commands.unsetMark(this.name) }, } },