2020-09-22 15:08:08 +08:00
|
|
|
import { Command, Node } from '@tiptap/core'
|
2020-09-10 21:10:22 +08:00
|
|
|
import { textblockTypeInputRule } from 'prosemirror-inputrules'
|
|
|
|
|
2020-09-22 15:08:08 +08:00
|
|
|
export type CodeBlockCommand = () => Command
|
|
|
|
|
2020-09-10 21:10:22 +08:00
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
|
|
interface Editor {
|
2020-09-22 15:08:08 +08:00
|
|
|
codeBlock: CodeBlockCommand,
|
2020-09-10 21:10:22 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 14:53:59 +08:00
|
|
|
|
2020-09-09 05:44:45 +08:00
|
|
|
export default new Node()
|
2020-09-22 15:08:08 +08:00
|
|
|
.name('code_block')
|
2020-09-09 05:44:45 +08:00
|
|
|
.schema(() => ({
|
|
|
|
content: 'text*',
|
|
|
|
marks: '',
|
|
|
|
group: 'block',
|
|
|
|
code: true,
|
|
|
|
defining: true,
|
|
|
|
draggable: false,
|
|
|
|
parseDOM: [
|
|
|
|
{ tag: 'pre', preserveWhitespace: 'full' },
|
|
|
|
],
|
|
|
|
toDOM: () => ['pre', ['code', 0]],
|
|
|
|
}))
|
2020-09-22 15:08:08 +08:00
|
|
|
.commands(({ name }) => ({
|
|
|
|
codeBlock: attrs => ({ commands }) => {
|
|
|
|
return commands.toggleNode(name, 'paragraph', attrs)
|
|
|
|
},
|
|
|
|
}))
|
2020-09-10 21:10:22 +08:00
|
|
|
.keys(({ editor }) => ({
|
|
|
|
'Shift-Ctrl-\\': () => editor.codeBlock()
|
|
|
|
}))
|
|
|
|
.inputRules(({ type }) => [
|
|
|
|
textblockTypeInputRule(/^```$/, type),
|
|
|
|
])
|
2020-09-09 05:44:45 +08:00
|
|
|
.create()
|