tiptap/packages/extension-code/index.ts

69 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-04-02 20:34:07 +08:00
import { Mark, markInputRule, markPasteRule, CommandSpec } from '@tiptap/core'
2020-04-02 19:26:56 +08:00
import { MarkSpec } from 'prosemirror-model'
import VerEx from 'verbal-expressions'
declare module '@tiptap/core/src/Editor' {
interface Editor {
code(): Editor,
}
}
export default class Code extends Mark {
name = 'code'
schema(): MarkSpec {
return {
excludes: '_',
parseDOM: [
{ tag: 'code' },
],
toDOM: () => ['code', 0],
}
}
2020-04-02 20:34:07 +08:00
commands(): CommandSpec {
return {
2020-04-22 20:06:15 +08:00
code: next => () => {
this.editor.toggleMark(this.name)
2020-04-02 20:34:07 +08:00
next()
},
}
}
2020-04-02 19:26:56 +08:00
keys() {
2020-04-13 18:27:29 +08:00
return {
'Mod-`': () => this.editor.code()
}
2020-04-02 19:26:56 +08:00
}
inputRules() {
2020-04-13 18:27:29 +08:00
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
.endOfLine()
return markInputRule(regex, this.type)
2020-04-02 19:26:56 +08:00
}
pasteRules() {
2020-04-13 18:27:29 +08:00
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
return markPasteRule(regex, this.type)
2020-04-02 19:26:56 +08:00
}
}