// fileStore.js const STORAGE_KEY = "devmate_ide_tree"; // Default starter tree const defaultTree = { type: "folder", name: "root", path: "", children: [ { type: "file", name: "main.py", path: "main.py", content: "# Python\nprint('Hello from IDE')", }, { type: "file", name: "script.js", path: "script.js", content: "console.log('Hello from JS');", }, ], }; // ============ HELPERS ============ export function loadTree() { try { const data = localStorage.getItem(STORAGE_KEY); return data ? JSON.parse(data) : defaultTree; } catch { return defaultTree; } } export function saveTree(tree) { localStorage.setItem(STORAGE_KEY, JSON.stringify(tree)); } export function getNodeByPath(node, path) { if (!path) return null; if (node.path === path) return node; if (!node.children) return null; for (let c of node.children) { const result = getNodeByPath(c, path); if (result) return result; } return null; } function buildPath(parent, name) { return parent.path ? `${parent.path}/${name}` : name; } // ============ ADD ============ export function addFile(tree, name) { const newTree = JSON.parse(JSON.stringify(tree)); newTree.children.push({ type: "file", name, path: name, content: `// ${name}`, }); return newTree; } export function addFolder(tree, name) { const newTree = JSON.parse(JSON.stringify(tree)); newTree.children.push({ type: "folder", name, path: name, children: [], }); return newTree; } // ============ DELETE ============ export function deleteNode(tree, path) { const clone = JSON.parse(JSON.stringify(tree)); clone.children = clone.children.filter((c) => c.path !== path); return clone; } // ============ RENAME ============ export function renameNode(tree, path, newName) { const clone = JSON.parse(JSON.stringify(tree)); function renameRec(node) { if (node.path === path) { node.name = newName; node.path = newName; } node.children?.forEach(renameRec); } renameRec(clone); return clone; } // ============ UPDATE CONTENT ============ export function updateFileContent(tree, path, content) { const clone = JSON.parse(JSON.stringify(tree)); function update(node) { if (node.path === path && node.type === "file") { node.content = content; } node.children?.forEach(update); } update(clone); return clone; } // ============ SEARCH ============ export function searchTree(node, term, result = []) { if (node.type === "file" && node.content?.includes(term)) { result.push({ file: node.name, path: node.path }); } node.children?.forEach((c) => searchTree(c, term, result)); return result; }