tiptap/packages/extension-horizontal-rule/src/horizontal-rule.ts

62 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-11-25 16:50:54 +08:00
import {
Command,
Node,
nodeInputRule,
mergeAttributes,
} from '@tiptap/core'
2020-10-22 18:34:49 +08:00
2020-11-13 23:44:22 +08:00
export interface HorizontalRuleOptions {
HTMLAttributes: {
[key: string]: any
},
}
const HorizontalRule = Node.create({
2020-10-22 18:34:49 +08:00
name: 'horizontalRule',
2020-11-13 23:44:22 +08:00
defaultOptions: <HorizontalRuleOptions>{
HTMLAttributes: {},
},
2020-10-22 18:34:49 +08:00
group: 'block',
parseHTML() {
return [
{ tag: 'hr' },
]
},
2020-11-13 23:07:20 +08:00
renderHTML({ HTMLAttributes }) {
2020-11-25 16:50:54 +08:00
return ['hr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]
2020-10-22 18:34:49 +08:00
},
addCommands() {
return {
2020-11-13 22:08:30 +08:00
/**
* Add a horizontal rule
*/
2020-11-18 19:26:13 +08:00
setHorizontalRule: (): Command => ({ tr, dispatch }) => {
if (dispatch) {
tr.replaceSelectionWith(this.type.create())
}
2020-10-22 18:34:49 +08:00
return true
},
}
},
addInputRules() {
return [
nodeInputRule(/^(?:---|___\s|\*\*\*\s)$/, this.type),
]
},
})
2020-10-23 04:40:40 +08:00
export default HorizontalRule
declare module '@tiptap/core' {
interface AllExtensions {
HorizontalRule: typeof HorizontalRule,
2020-10-23 04:40:40 +08:00
}
}