diff --git a/src/utils/helpers.js b/src/utils/helpers.js index cbc8f912d..359b0d58f 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -1,6 +1,5 @@ import fsOperation from "fileSystem"; import alert from "dialogs/alert"; -import escapeStringRegexp from "escape-string-regexp"; import adRewards from "lib/adRewards"; import config from "lib/config"; import fileIcons from "lib/fileIcons"; @@ -286,17 +285,25 @@ export default { updateUriOfAllActiveFiles(oldUrl, newUrl) { const files = editorManager.files; const { url } = Url.parse(oldUrl); + const base = url.endsWith("/") ? url.slice(0, -1) : url; for (let file of files) { if (!file.uri) continue; const fileUrl = Url.parse(file.uri).url; - if (new RegExp("^" + escapeStringRegexp(url)).test(fileUrl)) { - if (newUrl) { - file.uri = Url.join(newUrl, file.filename); - } else { + if (!newUrl) { + // A SAF storage root is a bare tree URI; its documents are `::`. + if ( + Url.isSameOrDescendant(fileUrl, base) || + fileUrl.startsWith(`${base}::`) + ) { file.uri = null; } + continue; } + if (!Url.isSameOrDescendant(fileUrl, base)) continue; + // Keep the file's path relative to the renamed/moved folder. + const relative = fileUrl.slice(base.length + 1); + file.uri = relative ? Url.join(newUrl, relative) : newUrl; } editorManager.onupdate("file-delete"); diff --git a/tests/unit/updateUriOfAllActiveFiles.test.js b/tests/unit/updateUriOfAllActiveFiles.test.js new file mode 100644 index 000000000..5b24f46c8 --- /dev/null +++ b/tests/unit/updateUriOfAllActiveFiles.test.js @@ -0,0 +1,99 @@ +import { afterEach, expect, test, vi } from "vitest"; + +vi.mock("fileSystem", () => ({ default: vi.fn() })); +vi.mock("dialogs/alert", () => ({ default: vi.fn() })); +vi.mock("lib/adRewards", () => ({ default: {} })); +vi.mock("lib/config", () => ({ default: {} })); +vi.mock("lib/fileIcons", () => ({ default: {} })); +vi.mock("lib/startAd", () => ({ + interstitialAd: {}, + requestBannerForPage: vi.fn(), +})); + +import helpers from "utils/helpers"; + +afterEach(() => vi.unstubAllGlobals()); + +function openTabs(...uris) { + const files = uris.map((uri) => ({ + uri, + filename: uri.slice(uri.lastIndexOf("/") + 1), + })); + vi.stubGlobal("editorManager", { + files, + onupdate: vi.fn(), + emit: vi.fn(), + }); + return files; +} + +const uris = (files) => files.map((file) => file.uri); + +test("renaming a folder keeps the sub-path of nested open files", () => { + const files = openTabs( + "file:///sdcard/proj/src/index.js", + "file:///sdcard/proj/src/components/Button.js", + "file:///sdcard/proj/src/a/b/c.js", + ); + helpers.updateUriOfAllActiveFiles( + "file:///sdcard/proj/src/", + "file:///sdcard/proj/lib/", + ); + expect(uris(files)).toEqual([ + "file:///sdcard/proj/lib/index.js", + "file:///sdcard/proj/lib/components/Button.js", + "file:///sdcard/proj/lib/a/b/c.js", + ]); +}); + +test("renaming a folder leaves sibling folders that share its name prefix alone", () => { + const files = openTabs( + "file:///sdcard/proj/src/index.js", + "file:///sdcard/proj/src-old/legacy.js", + "file:///sdcard/proj/src2/main.js", + ); + helpers.updateUriOfAllActiveFiles( + "file:///sdcard/proj/src", + "file:///sdcard/proj/lib", + ); + expect(uris(files)).toEqual([ + "file:///sdcard/proj/lib/index.js", + "file:///sdcard/proj/src-old/legacy.js", + "file:///sdcard/proj/src2/main.js", + ]); +}); + +test("deleting a folder only detaches files inside it", () => { + const files = openTabs( + "file:///sdcard/proj/src/components/Button.js", + "file:///sdcard/proj/src-old/legacy.js", + ); + helpers.updateUriOfAllActiveFiles("file:///sdcard/proj/src", null); + expect(uris(files)).toEqual([null, "file:///sdcard/proj/src-old/legacy.js"]); +}); + +test("renaming an open file points its tab at the new name", () => { + const files = openTabs( + "file:///sdcard/proj/old.js", + "file:///sdcard/proj/old.json", + ); + helpers.updateUriOfAllActiveFiles( + "file:///sdcard/proj/old.js", + "file:///sdcard/proj/new.js", + ); + expect(uris(files)).toEqual([ + "file:///sdcard/proj/new.js", + "file:///sdcard/proj/old.json", + ]); +}); + +test("removing a SAF storage detaches the tabs opened from it", () => { + const tree = + "content://com.android.externalstorage.documents/tree/primary%3ATesthtml"; + const files = openTabs( + `${tree}::primary:Testhtml/Styles/index.html`, + `${tree}2::primary:Testhtml2/index.html`, + ); + helpers.updateUriOfAllActiveFiles(tree, null); + expect(uris(files)).toEqual([null, `${tree}2::primary:Testhtml2/index.html`]); +});