mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-23 08:47:50 +08:00
45 lines
888 B
TypeScript
45 lines
888 B
TypeScript
import {
|
|
Command, Mark,
|
|
} from '@tiptap/core'
|
|
|
|
export type HighlightCommand = () => Command
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
interface Commands {
|
|
highlight: HighlightCommand,
|
|
}
|
|
}
|
|
|
|
export default new Mark()
|
|
.name('highlight')
|
|
.schema(() => ({
|
|
attrs: {
|
|
color: {
|
|
default: null,
|
|
},
|
|
},
|
|
parseDOM: [
|
|
{
|
|
tag: 'mark',
|
|
getAttrs: node => {
|
|
return {
|
|
color: (node as HTMLElement).style.backgroundColor,
|
|
}
|
|
},
|
|
},
|
|
],
|
|
toDOM: node => ['mark', {
|
|
...node.attrs,
|
|
style: node.attrs.color && `background-color: ${node.attrs.color};`,
|
|
}, 0],
|
|
}))
|
|
.commands(({ name }) => ({
|
|
highlight: attrs => ({ commands }) => {
|
|
return commands.toggleMark(name, attrs)
|
|
},
|
|
}))
|
|
.keys(({ editor }) => ({
|
|
'Mod-e': () => editor.highlight(),
|
|
}))
|
|
.create()
|