playground: fix deleting directory error
This commit is contained in:
		
							parent
							
								
									b6f1ee835a
								
							
						
					
					
						commit
						532e371e7e
					
				|  | @ -176,7 +176,11 @@ export default function Playground({ lang }: { lang: PlaygroundLang }) { | ||||||
|                     marginTop: "0.5cm", |                     marginTop: "0.5cm", | ||||||
|                 }} |                 }} | ||||||
|             > |             > | ||||||
|                 <ErrorDisplay lang={lang.errorDisplay} error={errorMsg} setError={setErrorMsg} /> |                 <ErrorDisplay | ||||||
|  |                     lang={lang.errorDisplay} | ||||||
|  |                     error={errorMsg} | ||||||
|  |                     setError={setErrorMsg} | ||||||
|  |                 /> | ||||||
|                 <Header |                 <Header | ||||||
|                     lang={lang.header} |                     lang={lang.header} | ||||||
|                     onSave={onSave} |                     onSave={onSave} | ||||||
|  | @ -389,10 +393,11 @@ function jsonReplacer(_key: any, value: any): any { | ||||||
| 
 | 
 | ||||||
| function deleteFile(monaco: Monaco, updater: Updater<Directory>, name: string) { | function deleteFile(monaco: Monaco, updater: Updater<Directory>, name: string) { | ||||||
|     const uri = monaco.Uri.parse(name); |     const uri = monaco.Uri.parse(name); | ||||||
|     console.log(uri); |  | ||||||
|     const model = monaco.editor.getModel(uri); |     const model = monaco.editor.getModel(uri); | ||||||
|     if (model) { |     if (model) { | ||||||
|         model.dispose(); |         model.dispose(); | ||||||
|  |     } else { | ||||||
|  |         console.error("Model not found: ", name); | ||||||
|     } |     } | ||||||
|     updater((dir) => { |     updater((dir) => { | ||||||
|         let current = dir; |         let current = dir; | ||||||
|  | @ -407,7 +412,6 @@ function deleteFile(monaco: Monaco, updater: Updater<Directory>, name: string) { | ||||||
|             } |             } | ||||||
|             current = current.dirs[part]; |             current = current.dirs[part]; | ||||||
|         } |         } | ||||||
|         console.log("file", current); |  | ||||||
|         if (current.files) { |         if (current.files) { | ||||||
|             delete current.files[last]; |             delete current.files[last]; | ||||||
|         } |         } | ||||||
|  | @ -415,42 +419,38 @@ function deleteFile(monaco: Monaco, updater: Updater<Directory>, name: string) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function deleteDir(monaco: Monaco, updater: Updater<Directory>, path: string) { | function deleteDir(monaco: Monaco, updater: Updater<Directory>, path: string) { | ||||||
|     const parts = path.split("/").filter((s) => s !== ""); |     const parts = path.split("/"); | ||||||
|     const last = parts.pop()!; |     const firstCheck = parts.at(0); | ||||||
| 
 |     if (firstCheck != undefined && firstCheck == "") { | ||||||
|     let current = getFiles(monaco); |         parts.splice(0, 1); | ||||||
|     for (const part of parts) { |     } | ||||||
|         if (!current.dirs) { |     const lastCheck = parts.at(-1); | ||||||
|             current.dirs = {}; |     if (lastCheck != undefined && lastCheck == "") { | ||||||
|         } |         parts.pop(); | ||||||
|         if (!current.dirs[part]) { |  | ||||||
|             current.dirs[part] = {}; |  | ||||||
|         } |  | ||||||
|         current = current.dirs[part]; |  | ||||||
|     } |     } | ||||||
|     console.log(current); |  | ||||||
| 
 | 
 | ||||||
|     if (current.dirs) { |     let current = getFiles(monaco) as Directory | null; | ||||||
|         for (const [name, _] of Object.entries(current.dirs ?? {})) { |     for (let part of parts) { | ||||||
|             deleteDir(monaco, updater, path + name + "/"); |         if (current?.dirs) { | ||||||
|  |             current = current.dirs[part]; | ||||||
|         } |         } | ||||||
|         for (const [name, _] of Object.entries(current.files ?? {})) { |     } | ||||||
|             deleteFile(monaco, updater, path + name); |     if (current) { | ||||||
|         } |         destroyModels(monaco, path, current); | ||||||
| 
 |  | ||||||
|         delete current.dirs[last]; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     updater((dir) => { |     updater((dir) => { | ||||||
|  |         const last = parts.pop(); | ||||||
|  |         if (!last) return; | ||||||
|         let current = dir; |         let current = dir; | ||||||
|  | 
 | ||||||
|         for (const part of parts) { |         for (const part of parts) { | ||||||
|             if (!current.dirs) { |             if (current.dirs) { | ||||||
|                 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) { |         if (current.dirs) { | ||||||
|  | @ -459,6 +459,31 @@ function deleteDir(monaco: Monaco, updater: Updater<Directory>, 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( | function renameFile( | ||||||
|     monaco: Monaco, |     monaco: Monaco, | ||||||
|     updater: Updater<Directory>, |     updater: Updater<Directory>, | ||||||
|  | @ -471,4 +496,3 @@ function renameFile( | ||||||
|         loadFile(monaco, updater, file, newName); |         loadFile(monaco, updater, file, newName); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue