mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-26 18:57:50 +08:00
fbf442269d
I built some extensions based on the textStyle extension but whenever I reset a style/attribute, the whole textStyle mark gets removed when there are still other styles defined in the mark.
66 lines
1.3 KiB
TypeScript
66 lines
1.3 KiB
TypeScript
import {
|
|
Mark,
|
|
getMarkAttributes,
|
|
mergeAttributes,
|
|
} from '@tiptap/core'
|
|
|
|
export interface TextStyleOptions {
|
|
HTMLAttributes: Record<string, any>,
|
|
}
|
|
|
|
declare module '@tiptap/core' {
|
|
interface Commands<ReturnType> {
|
|
textStyle: {
|
|
/**
|
|
* Remove spans without inline style attributes.
|
|
*/
|
|
removeEmptyTextStyle: () => ReturnType,
|
|
}
|
|
}
|
|
}
|
|
|
|
export const TextStyle = Mark.create<TextStyleOptions>({
|
|
name: 'textStyle',
|
|
|
|
defaultOptions: {
|
|
HTMLAttributes: {},
|
|
},
|
|
|
|
parseHTML() {
|
|
return [
|
|
{
|
|
tag: 'span',
|
|
getAttrs: element => {
|
|
const hasStyles = (element as HTMLElement).hasAttribute('style')
|
|
|
|
if (!hasStyles) {
|
|
return false
|
|
}
|
|
|
|
return {}
|
|
},
|
|
},
|
|
]
|
|
},
|
|
|
|
renderHTML({ HTMLAttributes }) {
|
|
return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
removeEmptyTextStyle: () => ({ state, commands }) => {
|
|
const attributes = getMarkAttributes(state, this.type)
|
|
const hasStyles = Object.entries(attributes).some(([, value]) => !!value)
|
|
|
|
if (hasStyles) {
|
|
return true
|
|
}
|
|
|
|
return commands.unsetMark('textStyle')
|
|
},
|
|
}
|
|
},
|
|
|
|
})
|