Skip to content

Commit

Permalink
feat: custom HOrizontalRule
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 2, 2023
1 parent f62e63e commit 0af6d96
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/matters-editor",
"version": "0.2.0-alpha.50",
"version": "0.2.0-alpha.51",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
63 changes: 63 additions & 0 deletions src/editors/extensions/horizontalRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { mergeAttributes, Node, nodeInputRule } from '@tiptap/core'
import { TextSelection } from '@tiptap/pm/state'

export interface HorizontalRuleOptions {
HTMLAttributes: Record<string, any>
}

declare module '@tiptap/core' {
interface Commands<ReturnType> {
horizontalRule: {
/**
* Add a horizontal rule
*/
setHorizontalRule: () => ReturnType
}
}
}

export const HorizontalRule = Node.create<HorizontalRuleOptions>({
name: 'horizontalRule',

addOptions() {
return {
HTMLAttributes: {},
}
},

group: 'block',

parseHTML() {
return [{ tag: 'hr' }]
},

renderHTML({ HTMLAttributes }) {
return ['hr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]
},

addCommands() {
return {
setHorizontalRule:
() =>
({ chain }) => {
return chain()
.insertContent([
{ type: this.name },
{
type: 'paragraph',
},
])
.run()
},
}
},

addInputRules() {
return [
nodeInputRule({
find: /^(?:---|-|___\s|\*\*\*\s)$/,
type: this.type,
}),
]
},
})
3 changes: 2 additions & 1 deletion src/editors/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Gapcursor from '@tiptap/extension-gapcursor'
import HardBreak from '@tiptap/extension-hard-break'
import Heading from '@tiptap/extension-heading'
import History from '@tiptap/extension-history'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
import ListItem from '@tiptap/extension-list-item'
import OrderedList from '@tiptap/extension-ordered-list'
import Paragraph from '@tiptap/extension-paragraph'
Expand All @@ -21,11 +20,13 @@ import { FigureImage } from './figureImage'
import { Link } from './link'
import { Mention, MentionSuggestion } from './mention'
import { Bold } from './bold'
import { HorizontalRule } from './horizontalRule'

export * from './figureAudio'
export * from './figureEmbed'
export * from './figureImage'
export * from './link'
export * from './horizontalRule'
export * from './mention'
export * from './bold'

Expand Down

0 comments on commit 0af6d96

Please sign in to comment.