tiptap/packages/extension-strike/index.ts

49 lines
1023 B
TypeScript
Raw Normal View History

2020-09-22 05:17:30 +08:00
import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
type StrikeCommand = () => Command
2020-09-10 17:20:40 +08:00
declare module '@tiptap/core/src/Editor' {
interface Editor {
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
export default new Mark()
.name('strike')
.schema(() => ({
parseDOM: [
{
tag: 's',
},
{
tag: 'del',
},
{
tag: 'strike',
},
{
style: 'text-decoration',
getAttrs: node => node === 'line-through' ? {} : false,
},
],
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 }) => ({
'Mod-d': () => editor.strike()
}))
.inputRules(({ type }) => [
markInputRule(inputRegex, type)
])
.pasteRules(({ type }) => [
markPasteRule(inputRegex, type)
])
.create()