2020-11-06 23:21:19 +08:00
|
|
|
import { Command, createMark, getMarkAttrs } from '@tiptap/core'
|
2020-11-06 21:46:59 +08:00
|
|
|
|
|
|
|
const TextStyle = createMark({
|
|
|
|
name: 'textStyle',
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: 'span',
|
2020-11-06 23:21:19 +08:00
|
|
|
getAttrs: element => {
|
|
|
|
const hasStyles = (element as HTMLElement).hasAttribute('style')
|
|
|
|
|
|
|
|
if (!hasStyles) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return {}
|
|
|
|
},
|
2020-11-06 21:46:59 +08:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
|
|
|
return ['span', HTMLAttributes, 0]
|
2020-11-06 21:46:59 +08:00
|
|
|
},
|
2020-11-06 23:21:19 +08:00
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2020-11-13 22:08:30 +08:00
|
|
|
/**
|
|
|
|
* Remove spans without inline style attributes.
|
|
|
|
*/
|
2020-11-06 23:21:19 +08:00
|
|
|
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')
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-06 21:46:59 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
export default TextStyle
|
|
|
|
|
2020-11-11 04:18:22 +08:00
|
|
|
declare module '@tiptap/core' {
|
2020-11-06 21:46:59 +08:00
|
|
|
interface AllExtensions {
|
|
|
|
TextStyle: typeof TextStyle,
|
|
|
|
}
|
|
|
|
}
|