Skip to content

Commit

Permalink
Fixed multiple search widget bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Apr 4, 2024
1 parent 703b1ce commit 05a941d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
24 changes: 12 additions & 12 deletions package/src/extensions/search/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export interface ReplaceAPI extends SearchAPI {
*/
selectMatch(index: number, scrollPadding?: number): void
/**
* If a match is selected, it's replaced with the specified value.
* If not, the closest match will be selected and the index is returned.
* If a match is selected and it's different to the provided string, it's replaced,
* else the index of the next match is returned.
* If there's no selected match, the index of the closest match is returned.
*/
replace(value: string): number | undefined
replace(str: string): number | undefined
/**
* @param value Value
* @param selection Does nothing. Kept for backwards compatibility.
Expand All @@ -44,7 +45,7 @@ const createReplaceAPI = (editor: PrismEditor): ReplaceAPI => {
const caretPos = getSelection()[0]
const l = matches.length
for (let i = l; i; ) {
if (caretPos > matches[--i][1]) return i == l - 1 ? 0 : i + 1
if (caretPos >= matches[--i][1]) return (i + <any>(matches[i][0] < caretPos)) % l
}
return l ? 0 : -1
}
Expand Down Expand Up @@ -102,15 +103,14 @@ const createReplaceAPI = (editor: PrismEditor): ReplaceAPI => {
},
replace(str: string) {
if (matches[0]) {
const index = closest()
const [start, end] = matches[index]
const [caretStart, caretEnd] = getSelection()
let index = closest()
let [start, end] = matches[index]
let [caretStart, caretEnd] = getSelection()
let notSelected = start != caretStart || end != caretEnd

if (start != caretStart || end != caretEnd) {
this.selectMatch(index)
return index
}
insertText(editor, str)
if (notSelected) return index
if (editor.value.slice(start, end) == str) return (index = (index + 1) % matches.length)
return insertText(editor, str)!
}
},
replaceAll(str: string) {
Expand Down
13 changes: 9 additions & 4 deletions package/src/extensions/search/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ export const searchWidget = (): SearchWidget => {

const replace = () => {
selectNext = true
replaceAPI.replace(replaceInput.value)
const index = replaceAPI.replace(replaceInput.value)
if (index != null) {
current.data = <any>(index + 1)
replaceAPI.selectMatch(index, prevMargin)
}
selectNext = false
}

Expand Down Expand Up @@ -251,12 +255,13 @@ export const searchWidget = (): SearchWidget => {

container.addEventListener("click", e => {
const target = <HTMLElement>e.target
const remove = addListener(editor, "update", () => target.focus())
elementHandlerMap.get(<HTMLElement>target)?.()
if (target.matches(".pce-options>button")) {
toggleAttr(target, "aria-pressed")
startSearch(true)
}
if (e.isTrusted) target.focus()
remove()
})

findInput.oninput = () => isOpen && startSearch(true)
Expand All @@ -273,9 +278,9 @@ export const searchWidget = (): SearchWidget => {
}
} else if (keyCode == 13 && target.tagName == "INPUT") {
preventDefault(e)
if (!shortcut) isFind ? move(true) : replace()
if (!shortcut) isFind ? move(true) : replaceEl.click()
else if (shortcut == 8 && isFind) move()
else if (shortcut == (isMac ? 4 : 3) && !isFind) replaceAll()
else if (shortcut == (isMac ? 4 : 3) && !isFind) replaceAllEl.click()
target.focus()
} else if (!shortcut && keyCode == 27) close()
else keydown(e)
Expand Down

0 comments on commit 05a941d

Please sign in to comment.