tiptap/packages/extension-strike/index.ts

51 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-09-24 06:29:05 +08:00
import {
Command, Mark, markInputRule, markPasteRule,
} from '@tiptap/core'
2020-09-22 05:17:30 +08:00
type StrikeCommand = () => Command
2020-09-10 17:20:40 +08:00
declare module '@tiptap/core/src/Editor' {
2020-09-22 16:49:38 +08:00
interface Commands {
2020-09-22 05:17:30 +08:00
strike: StrikeCommand,
2020-09-10 17:20:40 +08:00
}
}
export const inputRegex = /(?:^|\s)((?:~~)((?:[^~]+))(?:~~))$/gm
export const pasteRegex = /(?:^|\s)((?:~~)((?:[^~]+))(?:~~))/gm
2020-09-10 17:20:40 +08:00
export default new Mark()
.name('strike')
.schema(() => ({
parseDOM: [
{
tag: 's',
},
{
tag: 'del',
},
{
tag: 'strike',
},
{
style: 'text-decoration',
2020-09-24 06:29:05 +08:00
getAttrs: node => (node === 'line-through' ? {} : false),
2020-09-10 17:20:40 +08:00
},
],
toDOM: () => ['s', 0],
}))
2020-09-22 05:17:30 +08:00
.commands(({ name }) => ({
strike: () => ({ commands }) => {
return commands.toggleMark(name)
2020-09-10 17:20:40 +08:00
},
}))
.keys(({ editor }) => ({
2020-09-24 06:29:05 +08:00
'Mod-d': () => editor.strike(),
2020-09-10 17:20:40 +08:00
}))
.inputRules(({ type }) => [
2020-09-24 06:29:05 +08:00
markInputRule(inputRegex, type),
2020-09-10 17:20:40 +08:00
])
.pasteRules(({ type }) => [
2020-09-24 06:29:05 +08:00
markPasteRule(inputRegex, type),
2020-09-10 17:20:40 +08:00
])
.create()