Files
tiptap/packages/extension-text-style/src/text-style.ts
Dominik 8c6751f0c6 add precommit hook for linting and automatic eslint fixes + update eslint packages (#2862)
* chore: add precommit hook for eslint fixes, fix linting issues
* chore: add eslint import sort plugin
2022-06-08 14:10:25 +02:00

68 lines
1.3 KiB
TypeScript

import {
getMarkAttributes,
Mark,
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',
addOptions() {
return {
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(this.name)
},
}
},
})