From ffeefe21ff3c1f951a5a4f9ae9697317ddd1c5ad Mon Sep 17 00:00:00 2001 From: bdbch <6538827+bdbch@users.noreply.github.com> Date: Fri, 18 Aug 2023 08:32:06 -0700 Subject: [PATCH] fix: replace the whole node in nodeInputRule (#4341) Co-authored-by: Nicolas Gryman --- packages/core/src/inputRules/nodeInputRule.ts | 46 +++---------------- .../src/horizontal-rule.ts | 2 - 2 files changed, 6 insertions(+), 42 deletions(-) diff --git a/packages/core/src/inputRules/nodeInputRule.ts b/packages/core/src/inputRules/nodeInputRule.ts index 24bf90e6d9..b60f739975 100644 --- a/packages/core/src/inputRules/nodeInputRule.ts +++ b/packages/core/src/inputRules/nodeInputRule.ts @@ -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' @@ -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 @@ -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 @@ -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() }, }) } diff --git a/packages/extension-horizontal-rule/src/horizontal-rule.ts b/packages/extension-horizontal-rule/src/horizontal-rule.ts index 8d81d804fa..a6b547aa28 100644 --- a/packages/extension-horizontal-rule/src/horizontal-rule.ts +++ b/packages/extension-horizontal-rule/src/horizontal-rule.ts @@ -91,8 +91,6 @@ export const HorizontalRule = Node.create({ nodeInputRule({ find: /^(?:---|—-|___\s|\*\*\*\s)$/, type: this.type, - blockReplace: true, - addExtraNewline: true, }), ] },