File size: 2,762 Bytes
ac3107a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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;
}