forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathEditorFolder.js
169 lines (146 loc) · 4.73 KB
/
EditorFolder.js
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
import './Common.css';
import './EditorEntry.css';
import { useEffect, useMemo, useRef, useState } from 'react';
import { sendMessageFor } from 'php-cgi-wasm/msg-bus';
import EditorFile from './EditorFile';
import fileIcon from './nomo-dark/file.svg';
import folderOpen from './nomo-dark/folder.open.svg';
import folderClose from './nomo-dark/folder.close.svg';
import loader from './tail-spin.svg';
const sendMessage = sendMessageFor((`${window.location.origin}${process.env.PUBLIC_URL}/cgi-worker.mjs`))
const pathStates = new Map();
export default function EditorFolder({path = '/', name = ''}) {
const [dirs, setDirs] = useState([]);
const [showContext, setShowContext] = useState(false);
const [showNewFile, setShowNewFile] = useState(false);
const [showNewFolder, setShowNewFolder] = useState(false);
const [files, setFiles] = useState([]);
const [loading, setLoading] = useState(false);
const box = useRef(null);
const query = useMemo(() => new URLSearchParams(window.location.search), []);
const startPath = query.has('path') ? query.get('path') : '/';
const startOpened = pathStates.has(path)
? pathStates.get(path)
: (path === startPath.substr(0, path.length));
const [expanded, setExpanded] = useState(startOpened);
const onContext = event => {
event.preventDefault();
setExpanded(true);
setShowNewFolder(false);
setShowNewFile(false);
setShowContext(true);
}
const onBlur = event => setTimeout(() => setShowContext(false), 160);
const openFile = path => {
window.dispatchEvent(new CustomEvent('editor-open-file', {detail: path}));
query.set('path', path);
window.history.replaceState({}, null, window.location.pathname + '?' + query);
};
const newFileKeyUp = async event => {
if(event.key === 'Enter')
{
if(event.target.value)
{
const newName = path + '/' + event.target.value;
await sendMessage('writeFile', [newName, new TextEncoder().encode('')]);
openFile(newName);
loadFiles();
}
setShowNewFile(false);
event.target.value = '';
}
if(event.key === 'Escape')
{
setShowNewFile(false);
event.target.value = '';
}
};
const newFolderKeyUp = async event => {
if(event.key === 'Enter')
{
if(event.target.value)
{
const newName = path + '/' + event.target.value;
sendMessage('mkdir', [newName]);
loadFiles();
}
setShowNewFolder(false);
event.target.value = '';
}
if(event.key === 'Escape')
{
setShowNewFolder(false);
event.target.value = '';
}
};
const loadFiles = () => {
setLoading(true);
sendMessage('readdir', [path]).then(async entries => {
entries = entries.filter(f => f !== '.' && f !== '..');
const types = await Promise.all(entries.map(async f =>
(await sendMessage('analyzePath', [path + (path[path.length - 1] !== '/' ? '/' : '') + f]))
.object.isFolder
));
const dirs = entries.filter((_,k) => types[k]);
const files = entries.filter((_,k) => !types[k]);
setDirs(dirs);
setFiles(files);
setLoading(false);
});
};
useEffect(() => {
if(startPath === path)
{
loadFiles();
box.current.focus();
}
}, []);
useEffect(() => {
loadFiles();
}, []);
const toggleExpanded = event => {
event.stopPropagation();
setExpanded(!expanded);
pathStates.set(path, !expanded);
};
return (
<div className = "editor-entry editor-folder">
<p onClick = { toggleExpanded } onContextMenu={onContext} onBlur = {onBlur} tabIndex="0" ref = {box}>
<img className = "file icon" src = {
!loading
? (expanded ? folderOpen : folderClose)
: loader
} alt = "" />
{name}
</p>
{showContext && <span className = "contents only-focus">
<p className = "context" onClick = {() => setShowNewFile(true)}>
<img className = "file icon" src = {fileIcon} alt = "" />
Create New File...
</p>
<p className = "context" onClick = {() => setShowNewFolder(true)}>
<img className = "file icon" src = {folderClose} alt = "" />
Create New Folder...
</p>
</span>}
{showNewFile && <p className = "context">
<img className = "file icon" src = {fileIcon} alt = "" />
<input placeholder='filename' onKeyUp = {newFileKeyUp} autoFocus={true} />
</p>}
{showNewFolder && <p className = "context">
<img className = "file icon" src = {folderClose} alt = "" />
<input placeholder='filename' onKeyUp = {newFolderKeyUp} autoFocus={true} />
</p>}
{expanded && dirs.map(dir =>
<div key = {dir}>
<EditorFolder name = {dir} path = {path + (path[path.length - 1] !== '/' ? '/' : '') + dir} />
</div>
)}
{expanded && files.map(file =>
<div key = {file}>
<EditorFile name = {file} path = {path + (path[path.length - 1] !== '/' ? '/' : '') + file} />
</div>
)}
</div>
);
}