Skip to content

Commit

Permalink
revert: "fix: remove empty text style handling (#5905)" (#5906)
Browse files Browse the repository at this point in the history
This reverts commit df40b7f.
  • Loading branch information
nperez0111 authored Dec 2, 2024
1 parent df40b7f commit 3c05bf2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 38 deletions.
6 changes: 0 additions & 6 deletions .changeset/yellow-cats-teach.md

This file was deleted.

10 changes: 2 additions & 8 deletions packages/extension-font-family/src/font-family.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down
32 changes: 8 additions & 24 deletions packages/extension-text-style/src/text-style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getMarkAttributes,
Mark,
mergeAttributes,
} from '@tiptap/core'
Expand Down Expand Up @@ -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
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)
},
}
},
Expand Down

0 comments on commit 3c05bf2

Please sign in to comment.