revert: "fix: remove empty text style handling (#5905)" (#5906)
Some checks failed
build / lint (20) (push) Has been cancelled
build / test (20, map[name:Demos/Examples spec:./demos/src/Examples/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Experiments spec:./demos/src/Experiments/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Extensions spec:./demos/src/Extensions/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/GuideContent spec:./demos/src/GuideContent/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/GuideGettingStarted spec:./demos/src/GuideGettingStarted/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Marks spec:./demos/src/Marks/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Demos/Nodes spec:./demos/src/Nodes/**/*.spec.{js,ts}]) (push) Has been cancelled
build / test (20, map[name:Integration spec:./tests/cypress/integration/**/*.spec.{js,ts}]) (push) Has been cancelled
Publish / Release (20) (push) Has been cancelled
build / build (20) (push) Has been cancelled

This reverts commit df40b7f1a4.
This commit is contained in:
Nick Perez 2024-12-02 13:59:46 +01:00 committed by GitHub
parent df40b7f1a4
commit 3c05bf2f0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 38 deletions

View File

@ -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

View File

@ -72,15 +72,9 @@ export const FontFamily = Extension.create<FontFamilyOptions>({
.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()
},

View File

@ -1,4 +1,5 @@
import {
getMarkAttributes,
Mark,
mergeAttributes,
} from '@tiptap/core'
@ -63,32 +64,15 @@ export const TextStyle = Mark.create<TextStyleOptions>({
addCommands() {
return {
removeEmptyTextStyle: () => ({ tr }) => {
removeEmptyTextStyle: () => ({ state, commands }) => {
const attributes = getMarkAttributes(state, this.type)
const hasStyles = Object.entries(attributes).some(([, value]) => !!value)
const { selection } = tr
// 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) {
if (hasStyles) {
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)
},
}
},