From 8ee0d67b83fea6a38d3abf02528d62ab482fe509 Mon Sep 17 00:00:00 2001 From: Nokola Date: Wed, 22 Sep 2021 03:59:25 -0700 Subject: [PATCH] fix: nodeInputRule() support for group match (#1574) * Fix: nodeInputRule() support for group match Fixes in nodeInputRule() - add support for "first group match, if any" similar to https://prosemirror.net/docs/ref/#inputrules - fix issue where rewriting includes extra unnecessary character from the match * Insert last typed in nodeInputRule group match --- packages/core/src/inputRules/nodeInputRule.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/core/src/inputRules/nodeInputRule.ts b/packages/core/src/inputRules/nodeInputRule.ts index b6b3158f..bbfa6485 100644 --- a/packages/core/src/inputRules/nodeInputRule.ts +++ b/packages/core/src/inputRules/nodeInputRule.ts @@ -8,8 +8,24 @@ export default function (regexp: RegExp, type: NodeType, getAttributes?: (match: : getAttributes const { tr } = state - if (match[0]) { - tr.replaceWith(start - 1, end, type.create(attributes)) + if (match[1]) { + const offset = match[0].lastIndexOf(match[1]) + let matchStart = start + offset + if (matchStart > end) { + matchStart = end + } + else { + end = matchStart + match[1].length + } + + // insert last typed character + const lastChar = match[0][match[0].length - 1] + tr.insertText(lastChar, start + match[0].length - 1) + + // insert node from input rule + tr.replaceWith(matchStart, end, type.create(attributes)) + } else if (match[0]) { + tr.replaceWith(start, end, type.create(attributes)) } return tr