2020-10-02 21:28:00 +08:00
|
|
|
import {
|
2020-11-16 18:19:43 +08:00
|
|
|
Mark,
|
2020-11-06 20:42:40 +08:00
|
|
|
markInputRule,
|
|
|
|
markPasteRule,
|
2020-11-25 16:50:54 +08:00
|
|
|
mergeAttributes,
|
2020-10-02 21:28:00 +08:00
|
|
|
} from '@tiptap/core'
|
|
|
|
|
2020-11-13 23:44:22 +08:00
|
|
|
export interface HighlightOptions {
|
2020-12-04 06:32:11 +08:00
|
|
|
multicolor: boolean,
|
2021-04-21 15:43:31 +08:00
|
|
|
HTMLAttributes: Record<string, any>,
|
2020-11-13 23:44:22 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-06-05 03:56:29 +08:00
|
|
|
interface Commands<ReturnType> {
|
2021-02-16 18:27:58 +08:00
|
|
|
highlight: {
|
|
|
|
/**
|
|
|
|
* Set a highlight mark
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
setHighlight: (attributes?: { color: string }) => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
/**
|
|
|
|
* Toggle a highlight mark
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
toggleHighlight: (attributes?: { color: string }) => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
/**
|
|
|
|
* Unset a highlight mark
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
unsetHighlight: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:04:06 +08:00
|
|
|
export const inputRegex = /(?:^|\s)((?:==)((?:[^~=]+))(?:==))$/
|
|
|
|
export const pasteRegex = /(?:^|\s)((?:==)((?:[^~=]+))(?:==))/g
|
2020-10-28 05:14:40 +08:00
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const Highlight = Mark.create<HighlightOptions>({
|
2020-10-28 04:21:20 +08:00
|
|
|
name: 'highlight',
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
multicolor: false,
|
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
2020-11-13 23:44:22 +08:00
|
|
|
},
|
|
|
|
|
2020-10-28 04:21:20 +08:00
|
|
|
addAttributes() {
|
2020-12-04 06:32:11 +08:00
|
|
|
if (!this.options.multicolor) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
2020-10-28 04:21:20 +08:00
|
|
|
return {
|
2020-10-03 03:39:16 +08:00
|
|
|
color: {
|
|
|
|
default: null,
|
2021-09-09 05:53:44 +08:00
|
|
|
parseHTML: element => 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-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2020-11-25 16:50:54 +08:00
|
|
|
return ['mark', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
2020-10-28 04:21:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
setHighlight: attributes => ({ commands }) => {
|
2021-12-02 21:56:57 +08:00
|
|
|
return commands.setMark(this.name, attributes)
|
2020-11-18 04:59:04 +08:00
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
toggleHighlight: attributes => ({ commands }) => {
|
2021-12-02 21:56:57 +08:00
|
|
|
return commands.toggleMark(this.name, attributes)
|
2020-10-28 04:21:20 +08:00
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
unsetHighlight: () => ({ commands }) => {
|
2021-12-02 21:56:57 +08:00
|
|
|
return commands.unsetMark(this.name)
|
2020-11-18 04:59:04 +08:00
|
|
|
},
|
2020-10-28 04:21:20 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
2020-11-19 08:16:10 +08:00
|
|
|
'Mod-Shift-h': () => this.editor.commands.toggleHighlight(),
|
2020-10-28 04:21:20 +08:00
|
|
|
}
|
|
|
|
},
|
2020-10-28 05:14:40 +08:00
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return [
|
2021-10-08 21:02:09 +08:00
|
|
|
markInputRule({
|
|
|
|
find: inputRegex,
|
|
|
|
type: this.type,
|
|
|
|
}),
|
2020-10-28 05:14:40 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addPasteRules() {
|
|
|
|
return [
|
2021-10-08 21:02:09 +08:00
|
|
|
markPasteRule({
|
|
|
|
find: pasteRegex,
|
|
|
|
type: this.type,
|
|
|
|
}),
|
2020-10-28 05:14:40 +08:00
|
|
|
]
|
|
|
|
},
|
2020-10-28 04:21:20 +08:00
|
|
|
})
|