Skip to content

Commit

Permalink
Better maintain user selection
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Feb 16, 2024
1 parent b84f011 commit eb00bbc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion package/src/extensions/search/widget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InputSelection, BasicExtension } from "../.."
import { isChrome, isMac, createTemplate, preventDefault } from "../../core"
import { isChrome, isMac, createTemplate, preventDefault, setSelection } from "../../core"
import { regexEscape, getLines, getModifierCode } from "../../utils"
import { addTextareaListener } from "../../utils/local"
import { createReplaceAPI } from "./replace"
Expand Down Expand Up @@ -43,6 +43,7 @@ export const searchWidget = (): SearchWidget => {
searchSelection: [number, number] | undefined,
isOpen: boolean,
currentSelection: InputSelection,
prevUserSelection: InputSelection,
prevMargin: number,
selectNext = false,
marginTop: number
Expand All @@ -54,6 +55,7 @@ export const searchWidget = (): SearchWidget => {
replaceAPI = createReplaceAPI(editor)

const startSearch = (selectMatch?: boolean) => {
if (selectMatch) setSelection(prevUserSelection)
const error = replaceAPI.search(
findInput.value,
matchCase,
Expand All @@ -63,6 +65,7 @@ export const searchWidget = (): SearchWidget => {
),
index = error ? -1 : selectNext ? replaceAPI.next() : replaceAPI.closest()

setSelection()
current.data = <any>index + 1
total.data = <any>replaceAPI.matches.length
findContainer.classList.toggle("pce-error", !!error)
Expand Down Expand Up @@ -91,6 +94,10 @@ export const searchWidget = (): SearchWidget => {
}
}

const selectionChange = (selection: InputSelection) => {
if (editor.focused) prevUserSelection = selection
}

const beforeinput = () => {
if (searchSelection) currentSelection = selection()
}
Expand All @@ -117,6 +124,8 @@ export const searchWidget = (): SearchWidget => {
isOpen = true
if (marginTop == null) prevMargin = marginTop = getStyleValue(wrapper, "marginTop")
editor.addListener("update", input)
editor.addListener("selectionChange", selectionChange)
prevUserSelection = selection()
addTextareaListener(editor, "beforeinput", beforeinput)
container.style.display = "flex"
updateMargin()
Expand All @@ -131,6 +140,7 @@ export const searchWidget = (): SearchWidget => {
isOpen = false
replaceAPI.stopSearch()
editor.removeListener("update", input)
editor.removeListener("selectionChange", selectionChange)
textarea.removeEventListener("beforeinput", beforeinput)
container.style.display = "none"
updateMargin()
Expand Down

0 comments on commit eb00bbc

Please sign in to comment.