2020-11-25 16:50:54 +08:00
|
|
|
import {
|
|
|
|
Command,
|
|
|
|
Mark,
|
|
|
|
getMarkAttributes,
|
|
|
|
mergeAttributes,
|
|
|
|
} from '@tiptap/core'
|
|
|
|
|
|
|
|
export interface TextStyleOptions {
|
|
|
|
HTMLAttributes: {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
|
|
|
}
|
2020-11-06 21:46:59 +08:00
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands {
|
2021-02-11 01:05:02 +08:00
|
|
|
/**
|
|
|
|
* Remove spans without inline style attributes.
|
|
|
|
*/
|
2021-02-10 16:59:35 +08:00
|
|
|
removeEmptyTextStyle: () => Command,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 04:32:50 +08:00
|
|
|
export const TextStyle = Mark.create({
|
2020-11-06 21:46:59 +08:00
|
|
|
name: 'textStyle',
|
|
|
|
|
2020-11-25 16:50:54 +08:00
|
|
|
defaultOptions: <TextStyleOptions>{
|
|
|
|
HTMLAttributes: {},
|
|
|
|
},
|
|
|
|
|
2020-11-06 21:46:59 +08:00
|
|
|
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 }) {
|
2020-11-25 16:50:54 +08:00
|
|
|
return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
2020-11-06 21:46:59 +08:00
|
|
|
},
|
2020-11-06 23:21:19 +08:00
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
removeEmptyTextStyle: () => ({ state, commands }) => {
|
2020-11-18 04:10:08 +08:00
|
|
|
const attributes = getMarkAttributes(state, this.type)
|
2020-11-06 23:21:19 +08:00
|
|
|
const hasStyles = Object.entries(attributes).every(([, value]) => !!value)
|
|
|
|
|
|
|
|
if (hasStyles) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:38:16 +08:00
|
|
|
return commands.unsetMark('textStyle')
|
2020-11-06 23:21:19 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-06 21:46:59 +08:00
|
|
|
})
|