2020-09-24 06:29:05 +08:00
|
|
|
import {
|
2020-11-18 18:10:37 +08:00
|
|
|
Mark,
|
|
|
|
markInputRule,
|
|
|
|
markPasteRule,
|
2020-11-25 16:50:54 +08:00
|
|
|
mergeAttributes,
|
2020-09-24 06:29:05 +08:00
|
|
|
} from '@tiptap/core'
|
2020-09-22 05:17:30 +08:00
|
|
|
|
2020-11-13 23:44:22 +08:00
|
|
|
export interface BoldOptions {
|
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
|
|
|
bold: {
|
|
|
|
/**
|
|
|
|
* Set a bold mark
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
setBold: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
/**
|
|
|
|
* Toggle a bold mark
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
toggleBold: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
/**
|
|
|
|
* Unset a bold mark
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
unsetBold: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 03:27:37 +08:00
|
|
|
export const starInputRegex = /(?:^|\s)((?:\*\*)((?:[^*]+))(?:\*\*))$/
|
|
|
|
export const starPasteRegex = /(?:^|\s)((?:\*\*)((?:[^*]+))(?:\*\*))/g
|
|
|
|
export const underscoreInputRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))$/
|
|
|
|
export const underscorePasteRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))/g
|
2020-09-10 00:47:47 +08:00
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const Bold = Mark.create<BoldOptions>({
|
2020-10-22 18:34:49 +08:00
|
|
|
name: 'bold',
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
2020-11-13 23:44:22 +08:00
|
|
|
},
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
parseHTML() {
|
|
|
|
return [
|
2020-09-09 05:44:45 +08:00
|
|
|
{
|
|
|
|
tag: 'strong',
|
2020-04-02 20:34:07 +08:00
|
|
|
},
|
2020-09-09 05:44:45 +08:00
|
|
|
{
|
|
|
|
tag: 'b',
|
|
|
|
getAttrs: node => (node as HTMLElement).style.fontWeight !== 'normal' && null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
style: 'font-weight',
|
|
|
|
getAttrs: value => /^(bold(er)?|[5-9]\d{2,})$/.test(value as string) && null,
|
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2020-11-25 16:50:54 +08:00
|
|
|
return ['strong', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
setBold: () => ({ commands }) => {
|
2020-11-19 00:36:00 +08:00
|
|
|
return commands.setMark('bold')
|
2020-11-18 04:47:39 +08:00
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
toggleBold: () => ({ commands }) => {
|
2020-10-22 18:34:49 +08:00
|
|
|
return commands.toggleMark('bold')
|
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
unsetBold: () => ({ commands }) => {
|
2020-11-19 00:38:16 +08:00
|
|
|
return commands.unsetMark('bold')
|
2020-11-18 04:47:39 +08:00
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
2020-11-18 04:47:39 +08:00
|
|
|
'Mod-b': () => this.editor.commands.toggleBold(),
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return [
|
2021-10-08 21:02:09 +08:00
|
|
|
markInputRule({
|
|
|
|
find: starInputRegex,
|
|
|
|
type: this.type,
|
|
|
|
}),
|
|
|
|
markInputRule({
|
|
|
|
find: underscoreInputRegex,
|
|
|
|
type: this.type,
|
|
|
|
}),
|
2020-10-22 18:34:49 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addPasteRules() {
|
|
|
|
return [
|
2021-10-08 21:02:09 +08:00
|
|
|
markPasteRule({
|
|
|
|
find: starPasteRegex,
|
|
|
|
type: this.type,
|
|
|
|
}),
|
|
|
|
markPasteRule({
|
|
|
|
find: underscorePasteRegex,
|
|
|
|
type: this.type,
|
|
|
|
}),
|
2020-10-22 18:34:49 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|