mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 19:59:02 +08:00
add horizontal rule extension
This commit is contained in:
parent
cf3bcf3093
commit
667d735a12
@ -22,7 +22,7 @@ You don’t 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/) |
|
||||
|
@ -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'
|
||||
|
||||
|
15
packages/core/src/inputRules/nodeInputRule.ts
Normal file
15
packages/core/src/inputRules/nodeInputRule.ts
Normal 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
|
||||
})
|
||||
}
|
28
packages/extension-horizontal-rule/index.ts
Normal file
28
packages/extension-horizontal-rule/index.ts
Normal 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()
|
18
packages/extension-horizontal-rule/package.json
Normal file
18
packages/extension-horizontal-rule/package.json
Normal 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"
|
||||
}
|
||||
}
|
@ -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(),
|
||||
]
|
||||
}
|
@ -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(),
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user