about summary refs log tree commit diff stats
path: root/html/file-system/index.html
blob: 89e05b8ee6ab1ab8673eeb7eda83bfd2de946e3d (plain) (blame)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Folders in Folders in Folders</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: beige;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        #navigation {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            margin: 0.625em 0;
        }
        #navigation button {
            margin: 0.3125em;
            padding: 0.625em 0.9375em;
            font-size: 1em;
            flex-grow: 1;
            min-width: 7.5em;
        }
        #currentPath {
            font-weight: bold;
            margin: 0.625em 0;
        }
        ul {
            list-style-type: none;
            width: 100%;
            max-width: 37.5em;
        }
        li {
            margin: 0.3125em 0;
            cursor: pointer;
        }
        .folder {
            display: inline-block;
            padding: 0.3125em 0;
            cursor: pointer;
            font-size: 1.125em;
            color: black;
        }
        #editor {
            margin-top: 1.25em;
            width: 100%;
            max-width: 37.5em;
        }
        #editor textarea {
            width: 100%;
            height: 9.375em;
            font-size: 1em;
            padding: 0.625em;
            box-sizing: border-box;
            resize: none;
        }
        h1 {
            text-align: center;
            font-size: 1.5em;
            margin-top: 0.625em;
        }
        @media (max-width: 37.5em) {
            h1 {
                font-size: 1.25em;
            }
            #navigation button {
                font-size: 0.875em;
                padding: 0.5em 0.625em;
            }
            .folder {
                font-size: 1em;
            }
            #editor textarea {
                height: 7.5em;
                font-size: 0.875em;
            }
        }
    </style>
</head>
<body>

<div id="navigation">
    <button onclick="goToRoot()">Go to Root</button>
    <button onclick="goUp()">Go Up One Level</button>
    <button onclick="createFolderPrompt()">Create Folder</button>
    <button onclick="copyItem()">Copy</button>
    <button onclick="pasteItem()">Paste</button>
    <button onclick="deleteItem()">Delete</button>
</div>

<h1 id="currentPath">Current Folder: /</h1>

<ul id="fileSystemTree"></ul>

<div id="editor">
    <h2>Folder Content Editor</h2>
    <textarea id="folderContent" placeholder="Select a folder to edit its contents"></textarea>
</div>

<script>
const initialState = () => ({
    root: { type: 'folder', children: {}, content: '' }
});

const loadFileSystem = () => JSON.parse(localStorage.getItem('fileSystem')) || initialState();

const state = {
    fileSystem: loadFileSystem(),
    currentFolderPath: 'root',
    copiedFolderData: null, // Pastebin for the copied folder
    copiedItemPath: null
};

const clone = (obj) => JSON.parse(JSON.stringify(obj));

const updateFileSystem = (newFileSystem) => {
    state.fileSystem = newFileSystem;
    saveFileSystem(newFileSystem);
};

const getFolder = (path, fileSystem = state.fileSystem) => {
    const parts = path.split('/').filter(part => part !== 'root');
    return parts.reduce((current, part) => {
        if (!current || !current.children[part]) {
            return null;
        }
        return current.children[part];
    }, fileSystem.root);
};

const updateFolder = (fileSystem, path, updateFn) => {
    const rootCopy = clone(fileSystem);
    const folderToUpdate = getFolder(path, rootCopy);
    if (!folderToUpdate) {
        alert(`Folder not found for path: ${path}`);
        return null;
    }

    updateFn(folderToUpdate);
    return rootCopy;
};

const addFolder = (path, name, folderData, fileSystem) => {
    return updateFolder(fileSystem, path, (folder) => {
        if (folder.children[name]) {
            alert(`Folder with name "${name}" already exists.`);
            return;
        }
        folder.children[name] = folderData || { type: 'folder', children: {}, content: '' };
    });
};

const deleteFolder = (path, fileSystem) => {
    const [parentPath, folderName] = path.split(/\/([^\/]+)$/);
    return updateFolder(fileSystem, parentPath, (folder) => {
        delete folder.children[folderName];
    });
};

const saveFileSystem = (fileSystem) => {
    localStorage.setItem('fileSystem', JSON.stringify(fileSystem));
};

const goToRoot = () => {
    state.currentFolderPath = 'root';
    render();
};

const goUp = () => {
    const parts = state.currentFolderPath.split('/');
    if (parts.length > 1) {
        parts.pop();
        state.currentFolderPath = parts.join('/');
        render();
    }
};

const createFolder = (path, name) => {
    const updatedFileSystem = addFolder(path, name, null, state.fileSystem);
    if (updatedFileSystem) {
        updateFileSystem(updatedFileSystem);
        render();
    } else {
        console.error('Error creating folder', path, name);
        alert('Error creating folder');
    }
};

const deleteItem = () => {
    if (state.currentFolderPath === 'root') return alert("Can't delete the root!");

    const folderToDelete = getFolder(state.currentFolderPath);
    if (!folderToDelete) return alert("Folder not found!");

    const hasNestedFolders = Object.keys(folderToDelete.children).length > 0;

    if (hasNestedFolders) {
        const confirmDelete = confirm(`The folder contains nested folders. Do you really wanna delete them too?`);
        if (!confirmDelete) {
            return;
        }
    }

    const updatedFileSystem = deleteFolder(state.currentFolderPath, state.fileSystem);
    if (updatedFileSystem) {
        const parts = state.currentFolderPath.split('/');
        parts.pop();
        state.currentFolderPath = parts.join('/') || 'root';
        updateFileSystem(updatedFileSystem);
        render();
    }
};

const copyItem = () => {
    const folder = getFolder(state.currentFolderPath);
    if (folder) {
        state.copiedFolderData = clone(folder); // Store a deep copy of the folder
        state.copiedItemPath = state.currentFolderPath; // Store the path so that we can remove later
        // console.log('Folder copied:', state.copiedFolderData);
    }
};

const pasteItem = () => {
    if (!state.copiedItemPath || !state.copiedFolderData) return alert('No item to paste');

    const [oldPath, folderName] = state.copiedItemPath.split(/\/([^\/]+)$/);
    const updatedFileSystem = addFolder(state.currentFolderPath, folderName, state.copiedFolderData, state.fileSystem);

    if (updatedFileSystem) {
        updateFileSystem(deleteFolder(oldPath, updatedFileSystem)); // Remove original
        state.copiedFolderData = null; // Clear the copied data
        state.copiedItemPath = null;
        render();
    } else {
        alert('Error pasting item');
    }
};

const saveFolderContent = () => {
    const updatedFileSystem = updateFolder(state.fileSystem, state.currentFolderPath, (folder) => {
        folder.content = document.getElementById('folderContent').value;
    });
    if (updatedFileSystem) {
        updateFileSystem(updatedFileSystem);
    }
};

document.getElementById('folderContent').addEventListener('input', () => {
    saveFolderContent();
});

const renderFolderTree = (folder, path = 'root') => {
    const entries = Object.entries(folder.children);
    return entries.length ? entries.map(([name, item]) => `
        <li>
            <span class="folder" onclick="selectFolder('${path}/${name}')">${name}</span>
            <ul>${renderFolderTree(item, `${path}/${name}`)}</ul>
        </li>
    `).join('') : '';
};

const selectFolder = (path) => {
    const folder = getFolder(path);
    if (folder) {
        state.currentFolderPath = path;
        document.getElementById('folderContent').value = folder.content || '';
        render();
    } else {
        console.error('Folder not found', path);
        alert('Folder not found');
    }
};

const render = () => {
    if (!state.fileSystem.root) {
        console.error('File system is not initialized correctly', state.fileSystem);
        alert('File system is not initialized correctly');
        return;
    }
    document.getElementById('currentPath').textContent = state.currentFolderPath.replace('root', '') || '/';
    document.getElementById('fileSystemTree').innerHTML = renderFolderTree(state.fileSystem.root);
};

const createFolderPrompt = () => {
    const name = prompt('Enter folder name:');
    if (name) createFolder(state.currentFolderPath, name);
};

render();
</script>

</body>
</html>