2020-10-02 21:28:00 +08:00
|
|
|
import {
|
2020-10-28 05:14:40 +08:00
|
|
|
Command, createMark, markInputRule, markPasteRule,
|
2020-10-02 21:28:00 +08:00
|
|
|
} from '@tiptap/core'
|
|
|
|
|
2020-10-28 04:21:20 +08:00
|
|
|
export interface HighlightOptions {
|
|
|
|
color: string,
|
2020-10-02 21:28:00 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 05:14:40 +08:00
|
|
|
export const inputRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))$/gm
|
|
|
|
export const pasteRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))/gm
|
|
|
|
|
2020-10-28 04:21:20 +08:00
|
|
|
const Highlight = createMark({
|
|
|
|
name: 'highlight',
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2020-10-03 03:39:16 +08:00
|
|
|
color: {
|
|
|
|
default: null,
|
2020-10-28 04:21:20 +08:00
|
|
|
parseHTML: element => {
|
2020-10-03 03:39:16 +08:00
|
|
|
return {
|
2020-10-28 05:14:40 +08:00
|
|
|
color:
|
|
|
|
element.getAttribute('data-color')
|
|
|
|
|| element.style.backgroundColor,
|
2020-10-28 04:21:20 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
renderHTML: attributes => {
|
|
|
|
if (!attributes.color) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-10-28 05:14:40 +08:00
|
|
|
'data-color': attributes.color,
|
2020-10-28 04:21:20 +08:00
|
|
|
style: `background-color: ${attributes.color}`,
|
2020-10-03 03:39:16 +08:00
|
|
|
}
|
|
|
|
},
|
2020-10-02 21:28:00 +08:00
|
|
|
},
|
2020-10-28 04:21:20 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: 'mark',
|
|
|
|
},
|
2020-10-28 05:14:40 +08:00
|
|
|
{
|
|
|
|
style: 'background-color',
|
|
|
|
},
|
2020-10-28 04:21:20 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
renderHTML({ attributes }) {
|
|
|
|
return ['mark', attributes, 0]
|
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
highlight: (attrs?: HighlightOptions): Command => ({ commands }) => {
|
2020-10-28 05:22:34 +08:00
|
|
|
return commands.toggleMark('highlight', attrs)
|
2020-10-28 04:21:20 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
|
|
|
'Mod-e': () => this.editor.highlight(),
|
|
|
|
}
|
|
|
|
},
|
2020-10-28 05:14:40 +08:00
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return [
|
|
|
|
markInputRule(inputRegex, this.type),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addPasteRules() {
|
|
|
|
return [
|
|
|
|
markPasteRule(inputRegex, this.type),
|
|
|
|
]
|
|
|
|
},
|
2020-10-28 04:21:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
export default Highlight
|
|
|
|
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
|
|
interface AllExtensions {
|
|
|
|
Highlight: typeof Highlight,
|
|
|
|
}
|
|
|
|
}
|