From ead35e18197d0fa3fa369d2340e58645fe9ed9d7 Mon Sep 17 00:00:00 2001 From: ihipi Date: Mon, 3 Jan 2022 09:47:19 +0100 Subject: [PATCH] Added Regex group replace on SearchMenu (#2205) Co-authored-by: albert --- src/muya/lib/contentState/searchCtrl.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/muya/lib/contentState/searchCtrl.js b/src/muya/lib/contentState/searchCtrl.js index b35977b8..d95b8d4d 100644 --- a/src/muya/lib/contentState/searchCtrl.js +++ b/src/muya/lib/contentState/searchCtrl.js @@ -34,11 +34,23 @@ const matchString = (text, value, options) => { } const searchCtrl = ContentState => { + ContentState.prototype.buildRegexValue = function (match, value) { + const groups = value.match(/\$(\d)*/g) + if (groups) { + for (const regexGroup of groups) { + const groupIndex = regexGroup[1] - 1 + if (groupIndex < match.subMatches.length) { + value = value.replace(regexGroup, match.subMatches[groupIndex]) + } + } + } + return value + } + ContentState.prototype.replaceOne = function (match, value) { const { start, end, key } = match const block = this.getBlock(key) const { text } = block - block.text = text.substring(0, start) + value + text.substring(end) } @@ -48,6 +60,9 @@ const searchCtrl = ContentState => { const searchOptions = Object.assign({}, defaultSearchOption, opt) const { matches, value, index } = this.searchMatches if (matches.length) { + if (opt.isRegexp) { + replaceValue = this.buildRegexValue(matches[index], replaceValue) + } if (isSingle) { this.replaceOne(matches[index], replaceValue) } else { @@ -108,7 +123,8 @@ const searchCtrl = ContentState => { return { key, start: m.index, - end: m.index + m.match.length + end: m.index + m.match.length, + subMatches: m.subMatches } })) }