-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
278 lines (243 loc) · 8.78 KB
/
Copy pathscript.js
File metadata and controls
278 lines (243 loc) · 8.78 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
let currentData = null;
let currentFileName = 'edited.json';
document.addEventListener('DOMContentLoaded', () => {
const fileInput = document.getElementById('file-input');
const saveBtn = document.getElementById('save-btn');
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) return;
currentFileName = file.name;
const reader = new FileReader();
reader.onload = (e) => {
try {
currentData = JSON.parse(e.target.result);
saveBtn.disabled = false;
renderEditor();
} catch (error) {
alert('Invalid JSON file.');
console.error(error);
}
};
reader.readAsText(file);
});
saveBtn.addEventListener('click', () => {
if (!currentData) return;
const jsonString = JSON.stringify(currentData, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = currentFileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
});
function renderEditor() {
const editorContainer = document.getElementById('editor-container');
editorContainer.innerHTML = '';
if (currentData === null) {
editorContainer.innerHTML = '<p class="placeholder">Please open a JSON file to begin.</p>';
return;
}
// Pass a dummy object as parent of root to allow root replacement if needed
const rootContainer = { root: currentData };
const tree = createNode(currentData, 'root', rootContainer, 'root');
editorContainer.appendChild(tree);
// Update global data in case root was changed
currentData = rootContainer.root;
}
function getType(value) {
if (value === null) return 'null';
if (Array.isArray(value)) return 'array';
return typeof value;
}
function createNode(value, key, parent, parentKey) {
const nodeDiv = document.createElement('div');
nodeDiv.className = 'tree-node';
const currentType = getType(value);
const isObject = currentType === 'object';
const isArray = currentType === 'array';
// Key/Label rendering
const headerDiv = document.createElement('div');
headerDiv.className = 'node-header';
if (isObject || isArray) {
const toggleBtn = document.createElement('span');
toggleBtn.className = 'toggle-btn expanded';
toggleBtn.textContent = '▼';
toggleBtn.onclick = (e) => toggleNode(e.target, childrenContainer);
headerDiv.appendChild(toggleBtn);
} else {
const spacer = document.createElement('span');
spacer.className = 'spacer';
headerDiv.appendChild(spacer);
}
const keyInput = document.createElement('input');
keyInput.type = 'text';
keyInput.className = 'node-key-input';
keyInput.value = key;
// Disable key editing for root or array indices
if (key === 'root' || Array.isArray(parent)) {
keyInput.disabled = true;
keyInput.className += ' disabled-key';
if (Array.isArray(parent)) {
keyInput.value = `[${key}]`;
}
} else {
keyInput.onchange = (e) => {
const newKey = e.target.value;
if (newKey && newKey !== key) {
if (parent.hasOwnProperty(newKey)) {
alert('Key already exists!');
e.target.value = key; // Revert
} else {
parent[newKey] = parent[key];
delete parent[key];
renderEditor(); // Re-render to reflect key change and reorder
}
} else if (!newKey) {
e.target.value = key; // Revert if empty
}
};
}
headerDiv.appendChild(keyInput);
// Type Selector
const typeSelect = document.createElement('select');
typeSelect.className = 'node-type-select';
const types = ['string', 'number', 'boolean', 'object', 'array', 'null'];
types.forEach(t => {
const option = document.createElement('option');
option.value = t;
option.textContent = t;
if (t === currentType) option.selected = true;
typeSelect.appendChild(option);
});
typeSelect.onchange = (e) => {
const newType = e.target.value;
let newValue;
switch (newType) {
case 'string': newValue = ""; break;
case 'number': newValue = 0; break;
case 'boolean': newValue = false; break;
case 'object': newValue = {}; break;
case 'array': newValue = []; break;
case 'null': newValue = null; break;
}
parent[key] = newValue;
renderEditor();
};
headerDiv.appendChild(typeSelect);
if (isObject || isArray) {
const typeSpan = document.createElement('span');
typeSpan.className = 'node-type';
typeSpan.textContent = isArray ? `[${value.length}]` : `{${Object.keys(value).length}}`;
headerDiv.appendChild(typeSpan);
} else {
const separator = document.createElement('span');
separator.textContent = ': ';
headerDiv.appendChild(separator);
const valueInput = document.createElement('input');
valueInput.className = 'node-value';
if (currentType === 'boolean') {
valueInput.type = 'checkbox';
valueInput.checked = value;
valueInput.onchange = (e) => updateValue(parent, key, e.target.checked);
} else if (currentType !== 'null') {
valueInput.type = 'text';
valueInput.value = value;
valueInput.onchange = (e) => {
let val = e.target.value;
if (currentType === 'number') {
if (!isNaN(val) && val.trim() !== '') {
val = Number(val);
} else {
val = 0; // fallback if invalid number
}
}
updateValue(parent, key, val);
};
}
if (currentType === 'null') {
valueInput.type = 'text';
valueInput.value = 'null';
valueInput.disabled = true;
}
headerDiv.appendChild(valueInput);
}
// Add / Delete Buttons
const controlsDiv = document.createElement('div');
controlsDiv.className = 'node-controls';
if (isObject || isArray) {
const addBtn = document.createElement('button');
addBtn.textContent = '+';
addBtn.className = 'icon-btn add-btn';
addBtn.title = 'Add Item';
addBtn.onclick = () => {
if (isArray) {
value.push("");
} else {
let newKey = "newKey";
let counter = 1;
while (value.hasOwnProperty(newKey)) {
newKey = `newKey${counter++}`;
}
value[newKey] = "";
}
renderEditor();
};
controlsDiv.appendChild(addBtn);
}
if (key !== 'root') {
const delBtn = document.createElement('button');
delBtn.innerHTML = '×';
delBtn.className = 'icon-btn del-btn';
delBtn.title = 'Delete Item';
delBtn.onclick = () => {
if (Array.isArray(parent)) {
parent.splice(key, 1);
} else {
delete parent[key];
}
renderEditor();
};
controlsDiv.appendChild(delBtn);
}
headerDiv.appendChild(controlsDiv);
nodeDiv.appendChild(headerDiv);
// Children rendering
let childrenContainer = null;
if (isObject || isArray) {
childrenContainer = document.createElement('div');
childrenContainer.className = 'node-children';
if (isArray) {
value.forEach((item, idx) => {
childrenContainer.appendChild(createNode(item, idx, value, idx));
});
} else {
Object.keys(value).forEach((k) => {
childrenContainer.appendChild(createNode(value[k], k, value, k));
});
}
nodeDiv.appendChild(childrenContainer);
}
return nodeDiv;
}
function toggleNode(btn, container) {
if (container) {
if (container.style.display === 'none') {
container.style.display = 'block';
btn.textContent = '▼';
btn.classList.add('expanded');
} else {
container.style.display = 'none';
btn.textContent = '▶';
btn.classList.remove('expanded');
}
}
}
function updateValue(parent, key, newValue) {
if (parent && key !== null) {
parent[key] = newValue;
}
}