tiptap/packages/extension-paragraph/src/paragraph.ts

53 lines
917 B
TypeScript
Raw Normal View History

2020-11-25 16:50:54 +08:00
import { Command, Node, mergeAttributes } from '@tiptap/core'
2020-11-13 23:44:22 +08:00
export interface ParagraphOptions {
HTMLAttributes: {
[key: string]: any
},
}
2020-03-06 03:30:58 +08:00
2021-02-10 16:59:35 +08:00
declare module '@tiptap/core' {
interface Commands {
2021-02-11 01:05:02 +08:00
/**
* Toggle a paragraph
*/
2021-02-10 16:59:35 +08:00
setParagraph: () => Command,
}
}
2020-12-08 04:32:50 +08:00
export const Paragraph = Node.create({
2020-10-21 21:17:05 +08:00
name: 'paragraph',
2020-11-14 16:23:47 +08:00
defaultOptions: <ParagraphOptions>{
HTMLAttributes: {},
},
2020-11-13 23:44:22 +08:00
2020-10-21 21:17:05 +08:00
group: 'block',
content: 'inline*',
2020-10-12 16:32:54 +08:00
2020-10-10 04:59:25 +08:00
parseHTML() {
return [
{ tag: 'p' },
]
2020-10-21 21:17:05 +08:00
},
2020-10-10 04:59:25 +08:00
2020-11-13 23:07:20 +08:00
renderHTML({ HTMLAttributes }) {
2020-11-25 16:50:54 +08:00
return ['p', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
2020-10-21 21:17:05 +08:00
},
2020-10-22 15:42:28 +08:00
2020-10-22 17:14:44 +08:00
addCommands() {
2020-10-22 15:42:28 +08:00
return {
2021-02-10 16:59:35 +08:00
setParagraph: () => ({ commands }) => {
return commands.toggleNode('paragraph', 'paragraph')
2020-10-22 15:42:28 +08:00
},
}
},
2020-10-22 17:14:44 +08:00
addKeyboardShortcuts() {
2020-10-22 15:42:28 +08:00
return {
2020-11-18 19:29:24 +08:00
'Mod-Alt-0': () => this.editor.commands.setParagraph(),
2020-10-22 15:42:28 +08:00
}
},
2020-10-21 21:17:05 +08:00
})