improve text style extension
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { createMark } from '@tiptap/core'
|
||||
import { Command, createMark, getMarkAttrs } from '@tiptap/core'
|
||||
|
||||
const TextStyle = createMark({
|
||||
name: 'textStyle',
|
||||
@@ -7,6 +7,15 @@ const TextStyle = createMark({
|
||||
return [
|
||||
{
|
||||
tag: 'span',
|
||||
getAttrs: element => {
|
||||
const hasStyles = (element as HTMLElement).hasAttribute('style')
|
||||
|
||||
if (!hasStyles) {
|
||||
return false
|
||||
}
|
||||
|
||||
return {}
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
@@ -14,6 +23,22 @@ const TextStyle = createMark({
|
||||
renderHTML({ attributes }) {
|
||||
return ['span', attributes, 0]
|
||||
},
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
removeEmptyTextStyle: (): Command => ({ state, commands }) => {
|
||||
const attributes = getMarkAttrs(state, this.type)
|
||||
const hasStyles = Object.entries(attributes).every(([, value]) => !!value)
|
||||
|
||||
if (hasStyles) {
|
||||
return true
|
||||
}
|
||||
|
||||
return commands.removeMark('textStyle')
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
})
|
||||
|
||||
export default TextStyle
|
||||
|
||||
Reference in New Issue
Block a user