tiptap/packages/extension-code/index.ts

70 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 { toggleMark } from 'prosemirror-commands'
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 {
code: (next, { view }) => {
2020-04-11 04:07:27 +08:00
toggleMark(this.type)(view.state, view.dispatch)
2020-04-02 20:34:07 +08:00
next()
},
}
}
2020-04-02 19:26:56 +08:00
keys() {
2020-04-13 06:16:12 +08:00
return 'Mod-`'
2020-04-02 19:26:56 +08:00
}
inputRules() {
2020-04-13 05:47:24 +08:00
return markInputRule(
VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
.endOfLine(),
this.type,
)
2020-04-02 19:26:56 +08:00
}
pasteRules() {
2020-04-13 05:47:24 +08:00
return markPasteRule(
VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture(),
this.type,
)
2020-04-02 19:26:56 +08:00
}
}