Skip to content

Commit

Permalink
fix: update the logic of the removeEmptyTextStyle command from text-s…
Browse files Browse the repository at this point in the history
…yle extension (#5909)

Co-authored-by: Alex Casillas <[email protected]>
  • Loading branch information
alexvcasillas and Alex Casillas authored Dec 11, 2024
1 parent b08c94c commit d735cf3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-students-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@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
34 changes: 26 additions & 8 deletions packages/extension-text-style/src/text-style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
getMarkAttributes,
Mark,
mergeAttributes,
} from '@tiptap/core'
Expand Down Expand Up @@ -89,15 +88,34 @@ export const TextStyle = Mark.create<TextStyleOptions>({

addCommands() {
return {
removeEmptyTextStyle: () => ({ state, commands }) => {
const attributes = getMarkAttributes(state, this.type)
const hasStyles = Object.entries(attributes).some(([, value]) => !!value)
removeEmptyTextStyle: () => ({ tr }) => {

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) {
return true
}

if (hasStyles) {
return true
}
// Check if the node has no inline style attributes.
// Filter out non-`textStyle` marks.
if (
!node.marks.filter(mark => mark.type === this.type).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 commands.unsetMark(this.name)
return true
},
}
},
Expand Down

0 comments on commit d735cf3

Please sign in to comment.