diff --git a/src/components/Playground.tsx b/src/components/Playground.tsx
index bea6fa8..fa3b82c 100644
--- a/src/components/Playground.tsx
+++ b/src/components/Playground.tsx
@@ -176,7 +176,11 @@ export default function Playground({ lang }: { lang: PlaygroundLang }) {
marginTop: "0.5cm",
}}
>
-
+
, name: string) {
const uri = monaco.Uri.parse(name);
- console.log(uri);
const model = monaco.editor.getModel(uri);
if (model) {
model.dispose();
+ } else {
+ console.error("Model not found: ", name);
}
updater((dir) => {
let current = dir;
@@ -407,7 +412,6 @@ function deleteFile(monaco: Monaco, updater: Updater, name: string) {
}
current = current.dirs[part];
}
- console.log("file", current);
if (current.files) {
delete current.files[last];
}
@@ -415,42 +419,38 @@ function deleteFile(monaco: Monaco, updater: Updater, name: string) {
}
function deleteDir(monaco: Monaco, updater: Updater, path: string) {
- const parts = path.split("/").filter((s) => s !== "");
- const last = parts.pop()!;
-
- let current = getFiles(monaco);
- for (const part of parts) {
- if (!current.dirs) {
- current.dirs = {};
- }
- if (!current.dirs[part]) {
- current.dirs[part] = {};
- }
- current = current.dirs[part];
+ const parts = path.split("/");
+ const firstCheck = parts.at(0);
+ if (firstCheck != undefined && firstCheck == "") {
+ parts.splice(0, 1);
+ }
+ const lastCheck = parts.at(-1);
+ if (lastCheck != undefined && lastCheck == "") {
+ parts.pop();
}
- console.log(current);
- if (current.dirs) {
- for (const [name, _] of Object.entries(current.dirs ?? {})) {
- deleteDir(monaco, updater, path + name + "/");
+ let current = getFiles(monaco) as Directory | null;
+ for (let part of parts) {
+ if (current?.dirs) {
+ current = current.dirs[part];
}
- for (const [name, _] of Object.entries(current.files ?? {})) {
- deleteFile(monaco, updater, path + name);
- }
-
- delete current.dirs[last];
+ }
+ if (current) {
+ destroyModels(monaco, path, current);
}
updater((dir) => {
+ const last = parts.pop();
+ if (!last) return;
let current = dir;
+
for (const part of parts) {
- if (!current.dirs) {
- current.dirs = {};
+ if (current.dirs) {
+ current = current.dirs[part];
+ if (!current) return;
+ } else {
+ return;
}
- if (!current.dirs[part]) {
- current.dirs[part] = {};
- }
- current = current.dirs[part];
}
if (current.dirs) {
@@ -459,6 +459,31 @@ function deleteDir(monaco: Monaco, updater: Updater, path: string) {
});
}
+function destroyModels(monaco: Monaco, path: string, dir: Directory) {
+ if (!path.endsWith("/")) {
+ path += "/";
+ }
+ if (dir.files) {
+ for (const file of Object.keys(dir.files)) {
+ const filepath = path + file;
+ const uri = monaco.Uri.parse(filepath);
+ const model = monaco.editor.getModel(uri);
+ if (model) {
+ model.dispose();
+ } else {
+ console.error("Model not found: ", filepath);
+ }
+ }
+ }
+ if (dir.dirs) {
+ for (const dirname of Object.keys(dir.dirs)) {
+ const dirpath = path + dirname;
+ const subdir = dir.dirs[dirname];
+ destroyModels(monaco, dirpath, subdir);
+ }
+ }
+}
+
function renameFile(
monaco: Monaco,
updater: Updater,
@@ -471,4 +496,3 @@ function renameFile(
loadFile(monaco, updater, file, newName);
}
}
-