refactor highligh extension to the new API
This commit is contained in:
@@ -1,44 +1,67 @@
|
|||||||
import {
|
import {
|
||||||
Command, Mark,
|
Command, createMark,
|
||||||
} from '@tiptap/core'
|
} from '@tiptap/core'
|
||||||
|
|
||||||
export type HighlightCommand = () => Command
|
export interface HighlightOptions {
|
||||||
|
color: string,
|
||||||
declare module '@tiptap/core/src/Editor' {
|
|
||||||
interface Commands {
|
|
||||||
highlight: HighlightCommand,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Mark()
|
const Highlight = createMark({
|
||||||
.name('highlight')
|
name: 'highlight',
|
||||||
.schema(() => ({
|
|
||||||
attrs: {
|
addAttributes() {
|
||||||
|
return {
|
||||||
color: {
|
color: {
|
||||||
default: null,
|
default: null,
|
||||||
},
|
parseHTML: element => {
|
||||||
},
|
|
||||||
parseDOM: [
|
|
||||||
{
|
|
||||||
tag: 'mark',
|
|
||||||
getAttrs: node => {
|
|
||||||
return {
|
return {
|
||||||
color: (node as HTMLElement).style.backgroundColor,
|
color: element.style.backgroundColor,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
renderHTML: attributes => {
|
||||||
|
if (!attributes.color) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
style: `background-color: ${attributes.color}`,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
}
|
||||||
toDOM: node => ['mark', {
|
},
|
||||||
...node.attrs,
|
|
||||||
style: node.attrs.color && `background-color: ${node.attrs.color};`,
|
parseHTML() {
|
||||||
}, 0],
|
return [
|
||||||
}))
|
{
|
||||||
.commands(({ name }) => ({
|
tag: 'mark',
|
||||||
highlight: attrs => ({ commands }) => {
|
},
|
||||||
return commands.toggleMark(name, attrs)
|
]
|
||||||
},
|
},
|
||||||
}))
|
|
||||||
.keys(({ editor }) => ({
|
renderHTML({ attributes }) {
|
||||||
'Mod-e': () => editor.highlight(),
|
return ['mark', attributes, 0]
|
||||||
}))
|
},
|
||||||
.create()
|
|
||||||
|
addCommands() {
|
||||||
|
return {
|
||||||
|
highlight: (attrs?: HighlightOptions): Command => ({ commands }) => {
|
||||||
|
return commands.toggleMark('highlight')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
addKeyboardShortcuts() {
|
||||||
|
return {
|
||||||
|
'Mod-e': () => this.editor.highlight(),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export default Highlight
|
||||||
|
|
||||||
|
declare module '@tiptap/core/src/Editor' {
|
||||||
|
interface AllExtensions {
|
||||||
|
Highlight: typeof Highlight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user