Repository navigation
feat(search): ask for confirmation before replace all #3051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import autosize from "autosize"; | |||||||||||||||||||||||||||||||||||
| import { getDocText } from "cm/editorUtils"; | ||||||||||||||||||||||||||||||||||||
| import Checkbox from "components/checkbox"; | ||||||||||||||||||||||||||||||||||||
| import Sidebar, { preventSlide } from "components/sidebar"; | ||||||||||||||||||||||||||||||||||||
| import confirm from "dialogs/confirm"; | ||||||||||||||||||||||||||||||||||||
| import escapeStringRegexp from "escape-string-regexp"; | ||||||||||||||||||||||||||||||||||||
| import Reactive from "html-tag-js/reactive"; | ||||||||||||||||||||||||||||||||||||
| import Ref from "html-tag-js/ref"; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -25,6 +26,8 @@ const workers = []; | |||||||||||||||||||||||||||||||||||
| const results = []; | ||||||||||||||||||||||||||||||||||||
| const filesSearched = []; | ||||||||||||||||||||||||||||||||||||
| const filesReplaced = []; | ||||||||||||||||||||||||||||||||||||
| /** Number of files the current Replace All was confirmed for. */ | ||||||||||||||||||||||||||||||||||||
| let filesToReplaceCount = 0; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const $container = Ref(); | ||||||||||||||||||||||||||||||||||||
| const $regExp = Ref(); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -370,7 +373,7 @@ async function onWorkerMessage(e) { | |||||||||||||||||||||||||||||||||||
| const { file, text } = data; | ||||||||||||||||||||||||||||||||||||
| filesReplaced.push(file); | ||||||||||||||||||||||||||||||||||||
| openFile(file.url, { | ||||||||||||||||||||||||||||||||||||
| render: filesSearched.length === filesReplaced.length, | ||||||||||||||||||||||||||||||||||||
| render: filesReplaced.length === filesToReplaceCount, | ||||||||||||||||||||||||||||||||||||
| text, | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -800,7 +803,7 @@ function sendNativeSearch( | |||||||||||||||||||||||||||||||||||
| case "replace-result": | ||||||||||||||||||||||||||||||||||||
| filesReplaced.push(event.file); | ||||||||||||||||||||||||||||||||||||
| openFile(event.file.url, { | ||||||||||||||||||||||||||||||||||||
| render: filesSearched.length === filesReplaced.length, | ||||||||||||||||||||||||||||||||||||
| render: filesReplaced.length === filesToReplaceCount, | ||||||||||||||||||||||||||||||||||||
| text: event.text, | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -929,9 +932,9 @@ function withTimeout(promise, ms) { | |||||||||||||||||||||||||||||||||||
| * Sends a message to the worker threads to perform the replacement. | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| async function replaceAll() { | ||||||||||||||||||||||||||||||||||||
| terminateWorker(); | ||||||||||||||||||||||||||||||||||||
| filesReplaced.length = 0; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Snapshot everything before the dialog: the search keeps running while it | ||||||||||||||||||||||||||||||||||||
| // is open, so only the files the user was shown may be replaced. | ||||||||||||||||||||||||||||||||||||
| const files = [...filesSearched]; | ||||||||||||||||||||||||||||||||||||
| const search = $search.value; | ||||||||||||||||||||||||||||||||||||
| const replace = $replace.value; | ||||||||||||||||||||||||||||||||||||
| const options = getOptions(); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -940,14 +943,24 @@ async function replaceAll() { | |||||||||||||||||||||||||||||||||||
| const regex = toRegex(search, options); | ||||||||||||||||||||||||||||||||||||
| if (!regex) return; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (files.length) { | ||||||||||||||||||||||||||||||||||||
| const confirmed = await confirm( | ||||||||||||||||||||||||||||||||||||
| strings.warning, | ||||||||||||||||||||||||||||||||||||
| strings["replace all confirm"] | ||||||||||||||||||||||||||||||||||||
| .replace("{matches}", resultOverview.matchesCount) | ||||||||||||||||||||||||||||||||||||
| .replace("{files}", files.length), | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| if (!confirmed) return; | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+946
to
+953
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor edge case: Bailing out if the search changed while the dialog was open would close this:
Suggested change
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| terminateWorker(); | ||||||||||||||||||||||||||||||||||||
| filesReplaced.length = 0; | ||||||||||||||||||||||||||||||||||||
| filesToReplaceCount = files.length; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| replacing = true; | ||||||||||||||||||||||||||||||||||||
| activeReplaceTasks = 0; | ||||||||||||||||||||||||||||||||||||
| const nativeFiles = filesSearched.filter((file) => | ||||||||||||||||||||||||||||||||||||
| supportsNativeSearch(file.url), | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| const workerFiles = filesSearched.filter( | ||||||||||||||||||||||||||||||||||||
| (file) => !supportsNativeSearch(file.url), | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| const nativeFiles = files.filter((file) => supportsNativeSearch(file.url)); | ||||||||||||||||||||||||||||||||||||
| const workerFiles = files.filter((file) => !supportsNativeSearch(file.url)); | ||||||||||||||||||||||||||||||||||||
| if (nativeFiles.length) { | ||||||||||||||||||||||||||||||||||||
| activeReplaceTasks += 1; | ||||||||||||||||||||||||||||||||||||
| sendNativeSearch("replace", nativeFiles, search, options, replace); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.