tiptap/packages/extension-strike/src/strike.ts

104 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-09-24 06:29:05 +08:00
import {
Mark,
2020-11-13 23:44:22 +08:00
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 StrikeOptions {
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
strike: {
/**
* Set a strike mark
*/
2021-06-05 03:56:29 +08:00
setStrike: () => ReturnType,
2021-02-16 18:27:58 +08:00
/**
* Toggle a strike mark
*/
2021-06-05 03:56:29 +08:00
toggleStrike: () => ReturnType,
2021-02-16 18:27:58 +08:00
/**
* Unset a strike mark
*/
2021-06-05 03:56:29 +08:00
unsetStrike: () => ReturnType,
2021-02-16 18:27:58 +08:00
}
2021-02-10 16:59:35 +08:00
}
}
export const inputRegex = /(?:^|\s)((?:~~)((?:[^~]+))(?:~~))$/
export const pasteRegex = /(?:^|\s)((?:~~)((?:[^~]+))(?:~~))/g
2020-09-10 17:20:40 +08:00
2021-02-11 01:25:08 +08:00
export const Strike = Mark.create<StrikeOptions>({
2020-10-22 18:34:49 +08:00
name: 'strike',
addOptions() {
return {
HTMLAttributes: {},
}
2020-11-13 23:44:22 +08:00
},
2020-10-22 18:34:49 +08:00
parseHTML() {
return [
2020-09-10 17:20:40 +08:00
{
tag: 's',
},
{
tag: 'del',
},
{
tag: 'strike',
},
{
style: 'text-decoration',
consuming: false,
getAttrs: style => ((style as string).includes('line-through') ? {} : false),
2020-09-10 17:20:40 +08:00
},
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 ['s', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
2020-10-22 18:34:49 +08:00
},
addCommands() {
return {
2021-02-10 16:59:35 +08:00
setStrike: () => ({ commands }) => {
2021-12-02 21:56:57 +08:00
return commands.setMark(this.name)
2020-11-18 04:50:40 +08:00
},
2021-02-10 16:59:35 +08:00
toggleStrike: () => ({ commands }) => {
2021-12-02 21:56:57 +08:00
return commands.toggleMark(this.name)
2020-10-22 18:34:49 +08:00
},
2021-02-10 16:59:35 +08:00
unsetStrike: () => ({ commands }) => {
2021-12-02 21:56:57 +08:00
return commands.unsetMark(this.name)
2020-11-18 04:50:40 +08:00
},
2020-10-22 18:34:49 +08:00
}
},
addKeyboardShortcuts() {
return {
2020-11-19 08:16:10 +08:00
'Mod-Shift-x': () => this.editor.commands.toggleStrike(),
2020-10-22 18:34:49 +08:00
}
},
addInputRules() {
return [
markInputRule({
find: inputRegex,
type: this.type,
}),
2020-10-22 18:34:49 +08:00
]
},
addPasteRules() {
return [
markPasteRule({
find: pasteRegex,
type: this.type,
}),
2020-10-22 18:34:49 +08:00
]
},
})