From c82c30b5301fc32fb482b3a28d9e85ca7423c72c Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 16 May 2024 11:32:45 +0500 Subject: [PATCH] editor: fix search replace --- .../search-replace/search-replace.ts | 40 ++++++++++++++----- packages/editor/styles/styles.css | 5 ++- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/packages/editor/src/extensions/search-replace/search-replace.ts b/packages/editor/src/extensions/search-replace/search-replace.ts index 08108d9b3..44c63a4bc 100644 --- a/packages/editor/src/extensions/search-replace/search-replace.ts +++ b/packages/editor/src/extensions/search-replace/search-replace.ts @@ -19,7 +19,13 @@ along with this program. If not, see . import { Extension } from "@tiptap/core"; import { Decoration, DecorationSet } from "prosemirror-view"; -import { EditorState, Plugin, PluginKey, Transaction } from "prosemirror-state"; +import { + EditorState, + Plugin, + PluginKey, + Transaction, + TextSelection +} from "prosemirror-state"; import { SearchSettings } from "../../toolbar/stores/search-store"; type DispatchFn = (tr: Transaction) => void; @@ -274,44 +280,58 @@ export const SearchReplace = Extension.create({ }, moveToNextResult: () => - ({ state, dispatch, commands }) => { + ({ state, dispatch }) => { const { selectedIndex, results } = this.storage; if (!results || results.length <= 0) return false; let nextIndex = selectedIndex + 1; if (isNaN(nextIndex) || nextIndex >= results.length) nextIndex = 0; + const { tr } = state; const { from, to } = results[nextIndex]; - commands.setTextSelection({ from, to }); + tr.setSelection( + TextSelection.create( + tr.doc, + tr.mapping.map(from), + tr.mapping.map(to) + ) + ); scrollIntoView(); this.storage.selectedIndex = nextIndex; - state.tr.setMeta("selectedIndex", nextIndex); + tr.setMeta("selectedIndex", nextIndex); if (dispatch) updateView(state, dispatch); return true; }, moveToPreviousResult: () => - ({ state, dispatch, commands }) => { + ({ state, dispatch }) => { const { selectedIndex, results } = this.storage; if (!results || results.length <= 0) return false; let prevIndex = selectedIndex - 1; if (isNaN(prevIndex) || prevIndex < 0) prevIndex = results.length - 1; + const { tr } = state; const { from, to } = results[prevIndex]; - commands.setTextSelection({ from, to }); + tr.setSelection( + TextSelection.create( + tr.doc, + tr.mapping.map(from), + tr.mapping.map(to) + ) + ); scrollIntoView(); this.storage.selectedIndex = prevIndex; - state.tr.setMeta("selectedIndex", prevIndex); + tr.setMeta("selectedIndex", prevIndex); if (dispatch) updateView(state, dispatch); return true; }, replace: (term) => - ({ commands, tr, dispatch }) => { + ({ chain, tr, dispatch }) => { const { selectedIndex, results } = this.storage; if (!dispatch || !results || results.length <= 0) return false; @@ -320,9 +340,7 @@ export const SearchReplace = Extension.create({ const { from, to } = results[index]; tr.insertText(term, from, to); - dispatch(tr); - commands.moveToNextResult(); - return true; + return chain().moveToNextResult().run(); }, replaceAll: (term) => diff --git a/packages/editor/styles/styles.css b/packages/editor/styles/styles.css index d7bd810c6..2b1d78420 100644 --- a/packages/editor/styles/styles.css +++ b/packages/editor/styles/styles.css @@ -282,9 +282,10 @@ img.ProseMirror-separator { background-color: var(--paragraph, var(--nn_primary_paragraph)) !important; } -.search-result.selected { +.search-result.selected, +.search-result.selected::selection { background-color: var(--accent-secondary) !important; - color: var(--accentForeground-secondary); + color: var(--accentForeground-secondary) !important; } .search-result {