tiptap/packages/extension-highlight/index.ts

45 lines
878 B
TypeScript
Raw Normal View History

2020-10-02 21:28:00 +08:00
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(() => ({
2020-10-03 03:39:16 +08:00
attrs: {
color: {
default: null,
},
},
2020-10-02 21:28:00 +08:00
parseDOM: [
{
tag: 'mark',
2020-10-03 03:39:16 +08:00
getAttrs: node => {
return {
color: (node as HTMLElement).style.backgroundColor,
}
},
2020-10-02 21:28:00 +08:00
},
],
2020-10-03 03:39:16 +08:00
toDOM: node => ['mark', {
...node.attrs,
style: node.attrs.color && `background-color: ${node.attrs.color};`,
}, 0],
2020-10-02 21:28:00 +08:00
}))
.commands(({ name }) => ({
highlight: () => ({ commands }) => {
return commands.toggleMark(name)
},
}))
.keys(({ editor }) => ({
'Mod-e': () => editor.highlight(),
}))
.create()