@tiptap/extension-horizontal-rule
Advanced tools
Comparing version
import { Node, mergeAttributes, nodeInputRule } from '@tiptap/core'; | ||
import { TextSelection } from '@tiptap/pm/state'; | ||
import { TextSelection, NodeSelection } from '@tiptap/pm/state'; | ||
@@ -20,5 +20,12 @@ const HorizontalRule = Node.create({ | ||
return { | ||
setHorizontalRule: () => ({ chain }) => { | ||
return (chain() | ||
.insertContent({ type: this.name }) | ||
setHorizontalRule: () => ({ chain, state }) => { | ||
const { $to: $originTo } = state.selection; | ||
const currentChain = chain(); | ||
if ($originTo.parentOffset === 0) { | ||
currentChain.insertContentAt($originTo.pos - 2, { type: this.name }); | ||
} | ||
else { | ||
currentChain.insertContent({ type: this.name }); | ||
} | ||
return (currentChain | ||
// set cursor after horizontal rule | ||
@@ -31,3 +38,11 @@ .command(({ tr, dispatch }) => { | ||
if ($to.nodeAfter) { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos)); | ||
if ($to.nodeAfter.isTextblock) { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos + 1)); | ||
} | ||
else if ($to.nodeAfter.isBlock) { | ||
tr.setSelection(NodeSelection.create(tr.doc, $to.pos)); | ||
} | ||
else { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos)); | ||
} | ||
} | ||
@@ -39,3 +54,3 @@ else { | ||
tr.insert(posAfter, node); | ||
tr.setSelection(TextSelection.create(tr.doc, posAfter)); | ||
tr.setSelection(TextSelection.create(tr.doc, posAfter + 1)); | ||
} | ||
@@ -42,0 +57,0 @@ } |
@@ -23,5 +23,12 @@ (function (global, factory) { | ||
return { | ||
setHorizontalRule: () => ({ chain }) => { | ||
return (chain() | ||
.insertContent({ type: this.name }) | ||
setHorizontalRule: () => ({ chain, state: state$1 }) => { | ||
const { $to: $originTo } = state$1.selection; | ||
const currentChain = chain(); | ||
if ($originTo.parentOffset === 0) { | ||
currentChain.insertContentAt($originTo.pos - 2, { type: this.name }); | ||
} | ||
else { | ||
currentChain.insertContent({ type: this.name }); | ||
} | ||
return (currentChain | ||
// set cursor after horizontal rule | ||
@@ -34,3 +41,11 @@ .command(({ tr, dispatch }) => { | ||
if ($to.nodeAfter) { | ||
tr.setSelection(state.TextSelection.create(tr.doc, $to.pos)); | ||
if ($to.nodeAfter.isTextblock) { | ||
tr.setSelection(state.TextSelection.create(tr.doc, $to.pos + 1)); | ||
} | ||
else if ($to.nodeAfter.isBlock) { | ||
tr.setSelection(state.NodeSelection.create(tr.doc, $to.pos)); | ||
} | ||
else { | ||
tr.setSelection(state.TextSelection.create(tr.doc, $to.pos)); | ||
} | ||
} | ||
@@ -42,3 +57,3 @@ else { | ||
tr.insert(posAfter, node); | ||
tr.setSelection(state.TextSelection.create(tr.doc, posAfter)); | ||
tr.setSelection(state.TextSelection.create(tr.doc, posAfter + 1)); | ||
} | ||
@@ -45,0 +60,0 @@ } |
{ | ||
"name": "@tiptap/extension-horizontal-rule", | ||
"description": "horizontal rule extension for tiptap", | ||
"version": "2.1.0-rc.14", | ||
"version": "2.1.0", | ||
"homepage": "https://tiptap.dev", | ||
@@ -32,4 +32,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"@tiptap/core": "^2.1.0-rc.14", | ||
"@tiptap/pm": "^2.1.0-rc.14" | ||
"@tiptap/core": "^2.1.0", | ||
"@tiptap/pm": "^2.1.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependencies": { |
import { mergeAttributes, Node, nodeInputRule } from '@tiptap/core' | ||
import { TextSelection } from '@tiptap/pm/state' | ||
import { NodeSelection, TextSelection } from '@tiptap/pm/state' | ||
@@ -41,6 +41,15 @@ export interface HorizontalRuleOptions { | ||
setHorizontalRule: | ||
() => ({ chain }) => { | ||
() => ({ chain, state }) => { | ||
const { $to: $originTo } = state.selection | ||
const currentChain = chain() | ||
if ($originTo.parentOffset === 0) { | ||
currentChain.insertContentAt($originTo.pos - 2, { type: this.name }) | ||
} else { | ||
currentChain.insertContent({ type: this.name }) | ||
} | ||
return ( | ||
chain() | ||
.insertContent({ type: this.name }) | ||
currentChain | ||
// set cursor after horizontal rule | ||
@@ -53,3 +62,9 @@ .command(({ tr, dispatch }) => { | ||
if ($to.nodeAfter) { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos)) | ||
if ($to.nodeAfter.isTextblock) { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos + 1)) | ||
} else if ($to.nodeAfter.isBlock) { | ||
tr.setSelection(NodeSelection.create(tr.doc, $to.pos)) | ||
} else { | ||
tr.setSelection(TextSelection.create(tr.doc, $to.pos)) | ||
} | ||
} else { | ||
@@ -61,3 +76,3 @@ // add node after horizontal rule if it’s the end of the document | ||
tr.insert(posAfter, node) | ||
tr.setSelection(TextSelection.create(tr.doc, posAfter)) | ||
tr.setSelection(TextSelection.create(tr.doc, posAfter + 1)) | ||
} | ||
@@ -64,0 +79,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
30809
29.49%335
20.5%0
-100%