add horizontal rule extension

This commit is contained in:
Philipp Kühn 2020-09-10 12:28:14 +02:00
parent cf3bcf3093
commit 667d735a12
7 changed files with 67 additions and 1 deletions

View File

@ -22,7 +22,7 @@ You dont have to use it, but we prepared a `@tiptap/vue-starter-kit` which in
| [HardBreak](/api/extensions/hard-break) | Yes | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-hard-break/) |
| [Heading](/api/extensions/heading) | Yes | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-heading/) |
| [History](/api/extensions/history) | Yes | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-history/) |
| [HorizontalRule](/api/extensions/horizontal-rule) | | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-horizontal-rule/) |
| [HorizontalRule](/api/extensions/horizontal-rule) | Yes | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-horizontal-rule/) |
| [Italic](/api/extensions/italic) | Yes | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-italic/) |
| [Link](/api/extensions/link) | | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-link/) |
| [OrderedList](/api/extensions/ordered-list) | | [GitHub](https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-ordered-list/) |

View File

@ -8,6 +8,7 @@ export { default as Node } from './src/Node'
export { default as Mark } from './src/Mark'
export { Extensions } from './src/types'
export { default as nodeInputRule } from './src/inputRules/nodeInputRule'
export { default as markInputRule } from './src/inputRules/markInputRule'
export { default as markPasteRule } from './src/pasteRules/markPasteRule'

View File

@ -0,0 +1,15 @@
import { InputRule } from 'prosemirror-inputrules'
import { NodeType } from 'prosemirror-model'
export default function (regexp: RegExp, type: NodeType, getAttrs?: Function) {
return new InputRule(regexp, (state, match, start, end) => {
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
const { tr } = state
if (match[0]) {
tr.replaceWith(start - 1, end, type.create(attrs))
}
return tr
})
}

View File

@ -0,0 +1,28 @@
import { Node, nodeInputRule } from '@tiptap/core'
declare module '@tiptap/core/src/Editor' {
interface Editor {
horizontalRule(): Editor,
}
}
export default new Node()
.name('horizontalRule')
.schema(() => ({
group: 'block',
parseDOM: [{ tag: 'hr' }],
toDOM: () => ['hr'],
}))
.commands(({ editor, type }) => ({
horizontalRule: next => () => {
const { state, view } = editor
const { dispatch } = view
dispatch(state.tr.replaceSelectionWith(type.create()))
next()
},
}))
.inputRules(({ type }) => [
nodeInputRule(/^(?:---|___\s|\*\*\*\s)$/, type),
])
.create()

View File

@ -0,0 +1,18 @@
{
"name": "@tiptap/extension-horizontal-rule",
"version": "1.0.0",
"source": "index.ts",
"main": "dist/tiptap-extension-horizontal-rule.js",
"umd:main": "dist/tiptap-extension-horizontal-rule.umd.js",
"module": "dist/tiptap-extension-horizontal-rule.mjs",
"unpkg": "dist/tiptap-extension-horizontal-rule.js",
"jsdelivr": "dist/tiptap-extension-horizontal-rule.js",
"files": [
"src",
"dist"
],
"peerDependencies": {
"@tiptap/core": "2.x",
"prosemirror-commands": "^1.1.3"
}
}

View File

@ -11,6 +11,7 @@ import HardBreak from '@tiptap/extension-hard-break'
import Strike from '@tiptap/extension-strike'
import Underline from '@tiptap/extension-underline'
import Blockquote from '@tiptap/extension-blockquote'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
export default function defaultExtensions() {
return [
@ -27,5 +28,6 @@ export default function defaultExtensions() {
Strike(),
Underline(),
Blockquote(),
HorizontalRule(),
]
}

View File

@ -13,6 +13,7 @@ import HardBreak from '@tiptap/extension-hard-break'
import Strike from '@tiptap/extension-strike'
import Underline from '@tiptap/extension-underline'
import Blockquote from '@tiptap/extension-blockquote'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
export function defaultExtensions() {
return [
@ -29,5 +30,6 @@ export function defaultExtensions() {
Strike(),
Underline(),
Blockquote(),
HorizontalRule(),
]
}