Skip to content

Commit

Permalink
fix: replace the whole node in nodeInputRule (#4341)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Gryman <ngryman@gmail.com>
  • Loading branch information
bdbch and ngryman committed Aug 18, 2023
1 parent c09d9e9 commit ffeefe2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
46 changes: 6 additions & 40 deletions packages/core/src/inputRules/nodeInputRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NodeType } from '@tiptap/pm/model'
import { NodeSelection, TextSelection } from '@tiptap/pm/state'

import { InputRule, InputRuleFinder } from '../InputRule.js'
import { ExtendedRegExpMatchArray } from '../types.js'
Expand All @@ -20,18 +19,6 @@ export function nodeInputRule(config: {
*/
type: NodeType

/**
* Should the input rule replace the node or append to it
* If true, the node will be replaced
*/
blockReplace?: boolean

/**
* Insert empty paragraph after inserting the node
* Only works if blockReplace is true
*/
addExtraNewline?: boolean

/**
* A function that returns the attributes for the node
* can also be an object of attributes
Expand All @@ -47,13 +34,11 @@ export function nodeInputRule(config: {
handler: ({ state, range, match }) => {
const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
const { tr } = state
const start = config.blockReplace ? range.from - 1 : range.from
const start = range.from
let end = range.to

const newNode = config.type.create(attributes)

const { $to } = tr.selection

if (match[1]) {
const offset = match[0].lastIndexOf(match[1])
let matchStart = start + offset
Expand All @@ -72,32 +57,13 @@ export function nodeInputRule(config: {
// insert node from input rule
tr.replaceWith(matchStart, end, newNode)
} else if (match[0]) {
tr.replaceWith(start, end, newNode)
tr.insert(start - 1, config.type.create(attributes)).delete(
tr.mapping.map(start),
tr.mapping.map(end),
)
}

if (config.blockReplace && config.addExtraNewline) {
if ($to.nodeAfter) {
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 - 1))
}
} else {
// add node after horizontal rule if it’s the end of the document
const defaultNode = $to.parent.type.contentMatch.defaultType?.create() || state.doc.type.contentMatch.defaultType?.create()

if (defaultNode) {
const newPos = start + newNode.nodeSize

tr.insert(newPos, defaultNode)
tr.setSelection(TextSelection.create(tr.doc, newPos))
}
}

tr.scrollIntoView()
}
tr.scrollIntoView()
},
})
}
2 changes: 0 additions & 2 deletions packages/extension-horizontal-rule/src/horizontal-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
nodeInputRule({
find: /^(?:---|—-|___\s|\*\*\*\s)$/,
type: this.type,
blockReplace: true,
addExtraNewline: true,
}),
]
},
Expand Down

0 comments on commit ffeefe2

Please sign in to comment.