improve text style extension

This commit is contained in:
Philipp Kühn
2020-11-06 16:21:19 +01:00
parent 944d5e7039
commit 1af7829924
3 changed files with 49 additions and 15 deletions

View File

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