reifyui 0.18.5 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -0
- package/dist/index.cjs +128 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +122 -8
- package/dist/index.js.map +1 -1
- package/dist/tree.cjs +1159 -0
- package/dist/tree.cjs.map +1 -0
- package/dist/tree.d.ts +178 -0
- package/dist/tree.js +1129 -0
- package/dist/tree.js.map +1 -0
- package/package.json +9 -1
- package/src/auth/AuthForm.jsx +2 -0
- package/src/auth/client.js +34 -4
- package/src/auth/personCheck.js +98 -0
- package/src/index.d.ts +7 -1
- package/src/index.js +2 -1
- package/src/tree/FileTree.jsx +668 -0
- package/src/tree/index.d.ts +178 -0
- package/src/tree/index.js +14 -0
- package/src/tree/model.js +220 -0
- package/styles/tree.css +199 -0
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
// FileTree — a folder tree of files the way an editor's Explorer shows one: folders first, a
|
|
2
|
+
// chevron to open each, the open file marked, and the tree's own actions (new file, new folder,
|
|
3
|
+
// upload, collapse all) in its header beside its title. Names are typed in place (New file, New
|
|
4
|
+
// folder, Rename), a delete asks in the row, a row drags into another folder, and a right click,
|
|
5
|
+
// Shift+F10 or the menu key opens the row's menu. The keyboard follows the WAI-ARIA tree pattern.
|
|
6
|
+
//
|
|
7
|
+
// The tree owns no files: it draws `entries` and reports what the person did (onCreate, onRename,
|
|
8
|
+
// onDelete, onMove, onUpload); the host changes its list and passes it back. A handler may return
|
|
9
|
+
// a message (or a Promise of one) to refuse, and that message is shown where the person acted.
|
|
10
|
+
//
|
|
11
|
+
// Look and behaviour follow the HarnessRouter console's skill-editor tree (30px rows, 14px icons,
|
|
12
|
+
// 14px per level, the open file on the surface colour with a hairline, row actions at the right on
|
|
13
|
+
// hover). Styles: reifyui/styles/tree.css.
|
|
14
|
+
import React, { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
15
|
+
import { createPortal } from 'react-dom';
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_TREE_LABELS, baseName, buildFileTree, canMove, folderPaths, isWithin, joinPath, movedPath,
|
|
18
|
+
nameProblem, parentOf, splitExt, treeKeyAction, visibleRows,
|
|
19
|
+
} from './model.js';
|
|
20
|
+
|
|
21
|
+
const useIsoLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
22
|
+
const DRAG_TYPE = 'application/x-reifyui-tree-path';
|
|
23
|
+
const OPEN_ON_HOVER_MS = 600; // a closed folder held under a drag opens after this
|
|
24
|
+
const FOCUS_WAIT_MS = 4000; // how long a focus waits for its row to arrive from the host
|
|
25
|
+
const HEADER_ACTIONS = ['newFile', 'newFolder', 'upload', 'collapseAll'];
|
|
26
|
+
// the resolved colours a menu carries with it into document.body, where the host's theme class
|
|
27
|
+
// (set on some ancestor of the tree) no longer reaches
|
|
28
|
+
const MENU_VARS = ['--_surface', '--_ink', '--_mute', '--_faint', '--_line', '--_brand', '--_hover', '--_fail', '--_shadow',
|
|
29
|
+
'--_r-menu', '--_r-row', '--_z', '--_fs-md'];
|
|
30
|
+
|
|
31
|
+
const say = (v, ...a) => (typeof v === 'function' ? v(...a) : v);
|
|
32
|
+
|
|
33
|
+
function Svg({ s = 14, children }) {
|
|
34
|
+
return (
|
|
35
|
+
<svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"
|
|
36
|
+
strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" focusable="false">{children}</svg>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const I = {
|
|
40
|
+
file: <Svg><path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z" /><path d="M14 3v5h5" /></Svg>,
|
|
41
|
+
folder: <Svg><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" /></Svg>,
|
|
42
|
+
chevron: <Svg s={12}><path d="M9 6l6 6-6 6" /></Svg>,
|
|
43
|
+
newFile: <Svg><path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z" /><path d="M14 3v5h5" /><path d="M12 11v6M9 14h6" /></Svg>,
|
|
44
|
+
newFolder: <Svg><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" /><path d="M12 10.5v5M9.5 13h5" /></Svg>,
|
|
45
|
+
upload: <Svg><path d="M12 16V4M7 9l5-5 5 5" /><path d="M4 16v3a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-3" /></Svg>,
|
|
46
|
+
collapseAll: <Svg><path d="M7 10l5-5 5 5M7 19l5-5 5 5" /></Svg>,
|
|
47
|
+
pencil: <Svg><path d="M12 20h9" /><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z" /></Svg>,
|
|
48
|
+
trash: <Svg><path d="M4 7h16M9 7V4.5h6V7M18 7l-1 13H7L6 7M10 11v5M14 11v5" /></Svg>,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** A handler's answer as a message: '' when it went through, the host's words when it refused,
|
|
52
|
+
* the generic failure when it threw. */
|
|
53
|
+
async function answer(fn, failed) {
|
|
54
|
+
try { const r = await fn(); return typeof r === 'string' ? r : ''; }
|
|
55
|
+
catch { return failed; }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A name cut at the end, its extension kept whole. */
|
|
59
|
+
function Name({ name }) {
|
|
60
|
+
const [base, ext] = splitExt(name);
|
|
61
|
+
return <span className="rui-tree-name"><span className="rui-tree-base">{base}</span>{ext ? <span className="rui-tree-ext">{ext}</span> : null}</span>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The row's menu: fixed at the pointer (or under the row from the keyboard), kept inside the
|
|
65
|
+
* window, closed by Escape, a press outside, a scroll, or a choice. */
|
|
66
|
+
function TreeMenu({ menu, items, label, tokensFrom, onClose, menuRef }) {
|
|
67
|
+
const [pos, setPos] = useState(null);
|
|
68
|
+
const btns = useRef([]);
|
|
69
|
+
const style = useMemo(() => {
|
|
70
|
+
const s = {};
|
|
71
|
+
const src = tokensFrom.current;
|
|
72
|
+
if (src && typeof getComputedStyle === 'function') {
|
|
73
|
+
const cs = getComputedStyle(src);
|
|
74
|
+
for (const v of MENU_VARS) { const val = cs.getPropertyValue(v); if (val) s[v] = val.trim(); }
|
|
75
|
+
s.fontFamily = cs.fontFamily;
|
|
76
|
+
}
|
|
77
|
+
return s;
|
|
78
|
+
}, [tokensFrom, menu]);
|
|
79
|
+
|
|
80
|
+
useIsoLayoutEffect(() => {
|
|
81
|
+
const el = menuRef.current;
|
|
82
|
+
if (!el) return;
|
|
83
|
+
const r = el.getBoundingClientRect();
|
|
84
|
+
const vw = window.innerWidth, vh = window.innerHeight, m = 8;
|
|
85
|
+
const left = Math.max(m, Math.min(menu.x, vw - r.width - m));
|
|
86
|
+
const top = menu.y + r.height > vh - m ? Math.max(m, menu.y - r.height) : menu.y;
|
|
87
|
+
setPos({ left, top });
|
|
88
|
+
}, [menu]);
|
|
89
|
+
useEffect(() => { if (pos) btns.current.find((b) => b && !b.disabled)?.focus(); }, [pos]);
|
|
90
|
+
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
const down = (e) => { if (!menuRef.current?.contains(e.target)) onClose(false); };
|
|
93
|
+
const scroll = (e) => { if (!menuRef.current?.contains(e.target)) onClose(false); };
|
|
94
|
+
const resize = () => onClose(false);
|
|
95
|
+
document.addEventListener('pointerdown', down, true);
|
|
96
|
+
window.addEventListener('scroll', scroll, true);
|
|
97
|
+
window.addEventListener('resize', resize);
|
|
98
|
+
return () => {
|
|
99
|
+
document.removeEventListener('pointerdown', down, true);
|
|
100
|
+
window.removeEventListener('scroll', scroll, true);
|
|
101
|
+
window.removeEventListener('resize', resize);
|
|
102
|
+
};
|
|
103
|
+
}, [onClose, menuRef]);
|
|
104
|
+
|
|
105
|
+
const onKeyDown = (e) => {
|
|
106
|
+
const list = btns.current.filter((b) => b && !b.disabled);
|
|
107
|
+
const at = list.indexOf(document.activeElement);
|
|
108
|
+
const go = (i) => { e.preventDefault(); list[(i + list.length) % list.length]?.focus(); };
|
|
109
|
+
if (e.key === 'ArrowDown') go(at + 1);
|
|
110
|
+
else if (e.key === 'ArrowUp') go(at < 0 ? list.length - 1 : at - 1);
|
|
111
|
+
else if (e.key === 'Home') go(0);
|
|
112
|
+
else if (e.key === 'End') go(list.length - 1);
|
|
113
|
+
else if (e.key === 'Tab' || e.key === 'Escape') { e.preventDefault(); onClose(true); }
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
btns.current = [];
|
|
117
|
+
let k = 0;
|
|
118
|
+
return createPortal(
|
|
119
|
+
<div ref={menuRef} className="rui-tree-menu" role="menu" aria-label={label} onKeyDown={onKeyDown}
|
|
120
|
+
onContextMenu={(e) => e.preventDefault()}
|
|
121
|
+
style={{ ...style, left: pos ? pos.left : menu.x, top: pos ? pos.top : menu.y, visibility: pos ? 'visible' : 'hidden' }}>
|
|
122
|
+
{items.map((it, i) => (it.sep ? <div key={`s${i}`} className="rui-tree-menu-sep" role="separator" /> : (
|
|
123
|
+
<button key={it.id} type="button" role="menuitem" tabIndex={-1} ref={(el) => { btns.current[k++] = el; }}
|
|
124
|
+
className={`rui-tree-menu-item${it.danger ? ' is-danger' : ''}`} disabled={it.disabled}
|
|
125
|
+
aria-keyshortcuts={it.keys || undefined}
|
|
126
|
+
onClick={() => { onClose(false); it.run(); }}>
|
|
127
|
+
<span className="rui-tree-menu-label">{it.label}</span>
|
|
128
|
+
{it.hint ? <kbd className="rui-tree-menu-hint" aria-hidden="true">{it.hint}</kbd> : null}
|
|
129
|
+
</button>
|
|
130
|
+
)))}
|
|
131
|
+
</div>,
|
|
132
|
+
document.body,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function FileTree(props) {
|
|
137
|
+
const {
|
|
138
|
+
entries, selected = null, onSelect,
|
|
139
|
+
open: openProp, defaultOpen = true, onOpenChange,
|
|
140
|
+
title, label, actions, labels: labelsProp,
|
|
141
|
+
onCreate, onRename, onDelete, onMove, onUpload, validateName, menuItems,
|
|
142
|
+
readOnly = false, busy = false, pending, rowErrors, error,
|
|
143
|
+
className, style,
|
|
144
|
+
} = props;
|
|
145
|
+
const L = useMemo(() => ({ ...DEFAULT_TREE_LABELS, ...(labelsProp || {}) }), [labelsProp]);
|
|
146
|
+
const uid = useId().replace(/:/g, '');
|
|
147
|
+
const rootRef = useRef(null);
|
|
148
|
+
const menuRef = useRef(null);
|
|
149
|
+
const inputRef = useRef(null);
|
|
150
|
+
const askRef = useRef(null);
|
|
151
|
+
|
|
152
|
+
const tree = useMemo(() => buildFileTree(entries), [entries]);
|
|
153
|
+
const editable = !readOnly;
|
|
154
|
+
|
|
155
|
+
// ── open folders: controlled (open + onOpenChange) or kept here ────────────────────────────────
|
|
156
|
+
// Kept here as "everything open except" (defaultOpen true: folders that arrive later start open)
|
|
157
|
+
// or "only these open" (a list, or after Collapse all).
|
|
158
|
+
const controlled = Array.isArray(openProp);
|
|
159
|
+
const [kept, setKept] = useState(() => (defaultOpen === true
|
|
160
|
+
? { all: true, set: new Set() } : { all: false, set: new Set(Array.isArray(defaultOpen) ? defaultOpen : []) }));
|
|
161
|
+
// the latest open state, read by changes made one after another in the same event (a move both
|
|
162
|
+
// carries a folder's open state to its new path and opens the folder it landed in)
|
|
163
|
+
const keptRef = useRef(kept);
|
|
164
|
+
const ctlRef = useRef(openProp);
|
|
165
|
+
if (controlled) ctlRef.current = openProp;
|
|
166
|
+
const openSet = useMemo(() => (controlled ? new Set(openProp) : null), [controlled, openProp]);
|
|
167
|
+
const isOpen = useCallback((p) => (controlled ? openSet.has(p) : kept.all ? !kept.set.has(p) : kept.set.has(p)),
|
|
168
|
+
[controlled, openSet, kept]);
|
|
169
|
+
const readOpen = () => (controlled ? { all: false, set: new Set(ctlRef.current || []) } : keptRef.current);
|
|
170
|
+
const writeOpen = (next) => {
|
|
171
|
+
if (controlled) { const list = Array.from(next.set); ctlRef.current = list; onOpenChange?.(list); return; }
|
|
172
|
+
keptRef.current = next;
|
|
173
|
+
setKept(next);
|
|
174
|
+
onOpenChange?.(next.all ? folderPaths(tree).filter((p) => !next.set.has(p)) : Array.from(next.set));
|
|
175
|
+
};
|
|
176
|
+
const setOpen = (paths, value) => {
|
|
177
|
+
const cur = readOpen();
|
|
178
|
+
const set = new Set(cur.set);
|
|
179
|
+
let changed = false;
|
|
180
|
+
for (const p of paths) {
|
|
181
|
+
if (!p) continue;
|
|
182
|
+
const inSet = cur.all ? !value : value;
|
|
183
|
+
if (set.has(p) !== inSet) { if (inSet) set.add(p); else set.delete(p); changed = true; }
|
|
184
|
+
}
|
|
185
|
+
if (changed) writeOpen({ all: cur.all, set });
|
|
186
|
+
};
|
|
187
|
+
const remapOpen = (from, to) => {
|
|
188
|
+
const cur = readOpen();
|
|
189
|
+
if (!Array.from(cur.set).some((p) => isWithin(p, from))) return;
|
|
190
|
+
writeOpen({ all: cur.all, set: new Set(Array.from(cur.set).map((p) => movedPath(p, from, to))) });
|
|
191
|
+
};
|
|
192
|
+
const collapseAll = () => {
|
|
193
|
+
writeOpen({ all: false, set: new Set() });
|
|
194
|
+
if (active) setActive(active.split('/')[0]);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const rows = useMemo(() => visibleRows(tree.nodes, isOpen), [tree, isOpen]);
|
|
198
|
+
|
|
199
|
+
// ── focus: one row holds the tab stop; `active` is the last row the person was on ──────────────
|
|
200
|
+
// null: not yet on any row; '': the top (an empty part of the tree was clicked).
|
|
201
|
+
const [active, setActive] = useState(null);
|
|
202
|
+
const pendingFocus = useRef(null);
|
|
203
|
+
const focusSoon = (path) => { pendingFocus.current = { path, at: Date.now() }; setActive(path); };
|
|
204
|
+
const tabPath = useMemo(() => {
|
|
205
|
+
const has = (p) => rows.some((r) => r.path === p);
|
|
206
|
+
for (let p = active; p; p = parentOf(p)) if (has(p)) return p;
|
|
207
|
+
if (selected && has(selected)) return selected;
|
|
208
|
+
return rows[0]?.path ?? null;
|
|
209
|
+
}, [rows, active, selected]);
|
|
210
|
+
const tabPathRef = useRef(tabPath);
|
|
211
|
+
tabPathRef.current = tabPath;
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
const want = pendingFocus.current;
|
|
214
|
+
if (!want) return;
|
|
215
|
+
if (Date.now() - want.at > FOCUS_WAIT_MS) { pendingFocus.current = null; return; }
|
|
216
|
+
const root = rootRef.current;
|
|
217
|
+
if (!root) return;
|
|
218
|
+
const here = document.activeElement;
|
|
219
|
+
if (here && here !== document.body && !root.contains(here)) { pendingFocus.current = null; return; }
|
|
220
|
+
const el = Array.from(root.querySelectorAll('[role="treeitem"][data-path]')).find((x) => x.dataset.path === want.path);
|
|
221
|
+
if (el) { pendingFocus.current = null; el.focus(); }
|
|
222
|
+
});
|
|
223
|
+
// the selected file is never hidden in a closed folder: its folders open when it changes
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
if (!selected) return;
|
|
226
|
+
const up = [];
|
|
227
|
+
for (let p = parentOf(selected); p; p = parentOf(p)) if (tree.byPath.get(p)?.kind === 'dir' && !isOpen(p)) up.push(p);
|
|
228
|
+
if (up.length) setOpen(up, true);
|
|
229
|
+
// when the selection changes, not when a folder is closed around it
|
|
230
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
231
|
+
}, [selected]);
|
|
232
|
+
|
|
233
|
+
// ── transient states: a name being typed, a delete being asked, a menu ─────────────────────────
|
|
234
|
+
const [edit, setEditState] = useState(null); // { mode: 'create', kind, dir, value, err, busy } | { mode: 'rename', path, kind, ... }
|
|
235
|
+
const editRef = useRef(null);
|
|
236
|
+
const setEdit = (x) => { editRef.current = x; setEditState(x); };
|
|
237
|
+
const returnTo = useRef(null);
|
|
238
|
+
const [ask, setAsk] = useState(null); // { path, kind, files, busy }
|
|
239
|
+
const [menu, setMenu] = useState(null); // { path, kind, x, y }
|
|
240
|
+
const [notes, setNotes] = useState({}); // a refused delete or move, under its row
|
|
241
|
+
const note = (path, msg) => setNotes((n) => { const x = { ...n }; if (msg) x[path] = msg; else delete x[path]; return x; });
|
|
242
|
+
|
|
243
|
+
const nodeKind = (p) => tree.byPath.get(p)?.kind;
|
|
244
|
+
// the nearest folder that still exists (the host may have removed the one last used)
|
|
245
|
+
const existingDir = (d) => { let x = d || ''; while (x && nodeKind(x) !== 'dir') x = parentOf(x); return x; };
|
|
246
|
+
const targetDir = () => {
|
|
247
|
+
const from = active !== null ? active : selected;
|
|
248
|
+
if (!from) return '';
|
|
249
|
+
return existingDir(nodeKind(from) === 'dir' ? from : parentOf(from));
|
|
250
|
+
};
|
|
251
|
+
const childNames = (dir) => (tree.byPath.get(dir)?.children || []).map((c) => c.name);
|
|
252
|
+
const hold = busy;
|
|
253
|
+
|
|
254
|
+
// a name the host is still answering for stays until it answers
|
|
255
|
+
function dropEdit() { if (!editRef.current?.busy) setEdit(null); }
|
|
256
|
+
function closeAll() { dropEdit(); setAsk(null); setMenu(null); }
|
|
257
|
+
|
|
258
|
+
function startCreate(kind, dir) {
|
|
259
|
+
if (!editable || !onCreate || hold || editRef.current?.busy) return;
|
|
260
|
+
setAsk(null); setMenu(null);
|
|
261
|
+
returnTo.current = typeof document !== 'undefined' ? document.activeElement : null;
|
|
262
|
+
dir = existingDir(dir);
|
|
263
|
+
if (dir) setOpen([...ancestorsOf(dir), dir], true);
|
|
264
|
+
setEdit({ mode: 'create', kind, dir, value: '', err: '', busy: false });
|
|
265
|
+
}
|
|
266
|
+
function startRename(path) {
|
|
267
|
+
if (!editable || !onRename || hold || editRef.current?.busy || !tree.byPath.has(path)) return;
|
|
268
|
+
setAsk(null); setMenu(null);
|
|
269
|
+
setEdit({ mode: 'rename', path, kind: nodeKind(path), value: baseName(path), err: '', busy: false });
|
|
270
|
+
setActive(path);
|
|
271
|
+
}
|
|
272
|
+
function askDelete(path) {
|
|
273
|
+
if (!editable || !onDelete || hold) return;
|
|
274
|
+
const n = tree.byPath.get(path);
|
|
275
|
+
if (!n) return;
|
|
276
|
+
dropEdit(); setMenu(null);
|
|
277
|
+
setAsk({ path, kind: n.kind, files: n.files, busy: false });
|
|
278
|
+
setActive(path);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function commitEdit(fromBlur) {
|
|
282
|
+
const ed = editRef.current;
|
|
283
|
+
if (!ed || ed.busy) return;
|
|
284
|
+
const value = ed.value.trim();
|
|
285
|
+
const same = ed.mode === 'rename' && value === baseName(ed.path);
|
|
286
|
+
if (same || (fromBlur && !value)) {
|
|
287
|
+
setEdit(null);
|
|
288
|
+
if (!fromBlur) { if (ed.mode === 'rename') focusSoon(ed.path); else restoreFocus(); }
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const dir = ed.mode === 'create' ? ed.dir : parentOf(ed.path);
|
|
292
|
+
const siblings = childNames(dir).filter((n) => ed.mode !== 'rename' || n !== baseName(ed.path));
|
|
293
|
+
const err = nameProblem(value, { kind: ed.kind, dir, siblings, labels: L, validate: validateName });
|
|
294
|
+
if (err) { setEdit({ ...ed, err }); return; }
|
|
295
|
+
const to = joinPath(dir, value);
|
|
296
|
+
const going = { ...ed, err: '', busy: true };
|
|
297
|
+
setEdit(going);
|
|
298
|
+
const refused = await answer(() => (ed.mode === 'create'
|
|
299
|
+
? onCreate({ path: to, kind: ed.kind }) : onRename({ from: ed.path, to, kind: ed.kind })), L.failed);
|
|
300
|
+
if (editRef.current !== going) return; // put away while the host was answering
|
|
301
|
+
if (refused) { setEdit({ ...going, busy: false, err: refused }); return; }
|
|
302
|
+
setEdit(null);
|
|
303
|
+
if (ed.mode === 'rename' && ed.kind === 'dir') remapOpen(ed.path, to);
|
|
304
|
+
if (ed.mode === 'create' && ed.kind === 'dir') setOpen([to], true);
|
|
305
|
+
if (fromBlur) setActive(to); else focusSoon(to);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async function confirmDelete() {
|
|
309
|
+
const a = ask;
|
|
310
|
+
if (!a || a.busy) return;
|
|
311
|
+
const i = rows.findIndex((r) => r.path === a.path);
|
|
312
|
+
const after = rows.slice(i + 1).find((r) => !isWithin(r.path, a.path));
|
|
313
|
+
const next = after && parentOf(after.path) === parentOf(a.path) ? after.path : (rows[i - 1]?.path ?? after?.path ?? null);
|
|
314
|
+
const going = { ...a, busy: true };
|
|
315
|
+
setAsk(going);
|
|
316
|
+
const refused = await answer(() => onDelete({ path: a.path, kind: a.kind }), L.failed);
|
|
317
|
+
setAsk((cur) => (cur && cur.path === a.path ? null : cur));
|
|
318
|
+
if (refused) { note(a.path, refused); focusSoon(a.path); return; }
|
|
319
|
+
note(a.path, '');
|
|
320
|
+
if (next !== null) focusSoon(next); else setActive(null);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async function doMove(from, kind, toDir) {
|
|
324
|
+
const to = joinPath(toDir, baseName(from));
|
|
325
|
+
note(from, '');
|
|
326
|
+
const refused = await answer(() => onMove({ from, toDir, kind }), L.failed);
|
|
327
|
+
if (refused) { note(from, refused); return; }
|
|
328
|
+
if (kind === 'dir') remapOpen(from, to);
|
|
329
|
+
if (toDir) setOpen([...ancestorsOf(toDir), toDir], true);
|
|
330
|
+
setActive(to);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function restoreFocus() {
|
|
334
|
+
const el = returnTo.current;
|
|
335
|
+
returnTo.current = null;
|
|
336
|
+
if (el && el.isConnected && rootRef.current?.contains(el)) el.focus();
|
|
337
|
+
else if (tabPath) focusSoon(tabPath);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Escape puts away the innermost thing, and only that: a dialog the tree sits in stays open
|
|
341
|
+
// (taken on the way down, before a host's own Escape listener hears it).
|
|
342
|
+
useEffect(() => {
|
|
343
|
+
if (!edit && !ask && !menu) return undefined;
|
|
344
|
+
const onKey = (e) => {
|
|
345
|
+
if (e.key !== 'Escape') return;
|
|
346
|
+
const t = e.target;
|
|
347
|
+
if (!(rootRef.current?.contains(t) || menuRef.current?.contains(t))) return;
|
|
348
|
+
e.stopPropagation(); e.preventDefault();
|
|
349
|
+
if (menu) { setMenu(null); focusSoon(menu.path || tabPath); return; }
|
|
350
|
+
if (editRef.current) {
|
|
351
|
+
const ed = editRef.current;
|
|
352
|
+
if (ed.busy) return;
|
|
353
|
+
setEdit(null);
|
|
354
|
+
if (ed.mode === 'rename') focusSoon(ed.path); else restoreFocus();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (ask) { setAsk(null); focusSoon(ask.path); }
|
|
358
|
+
};
|
|
359
|
+
window.addEventListener('keydown', onKey, true);
|
|
360
|
+
return () => window.removeEventListener('keydown', onKey, true);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
// a name being typed takes focus as it opens, the name selected without its extension
|
|
364
|
+
useEffect(() => {
|
|
365
|
+
const el = inputRef.current;
|
|
366
|
+
if (!el || !edit) return;
|
|
367
|
+
el.focus();
|
|
368
|
+
const v = el.value;
|
|
369
|
+
const dot = edit.mode === 'rename' && edit.kind === 'file' ? splitExt(v)[0].length : v.length;
|
|
370
|
+
el.setSelectionRange(edit.mode === 'rename' ? 0 : v.length, dot);
|
|
371
|
+
// when one opens, not on every keystroke
|
|
372
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
373
|
+
}, [edit?.mode, edit?.path, edit?.dir, edit?.kind]);
|
|
374
|
+
// a delete being asked: its Delete button takes focus, as the console's in-row confirms do
|
|
375
|
+
useEffect(() => { if (ask && !ask.busy) askRef.current?.focus(); }, [ask?.path]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
376
|
+
|
|
377
|
+
// ── the menu ───────────────────────────────────────────────────────────────────────────────────
|
|
378
|
+
const itemsFor = (path, kind) => {
|
|
379
|
+
const at = kind === 'dir' ? path : parentOf(path);
|
|
380
|
+
const groups = [[], [], []];
|
|
381
|
+
if (onCreate) {
|
|
382
|
+
groups[0].push({ id: 'new-file', label: L.newFile, run: () => startCreate('file', at) });
|
|
383
|
+
groups[0].push({ id: 'new-folder', label: L.newFolder, run: () => startCreate('dir', at) });
|
|
384
|
+
}
|
|
385
|
+
if (path) {
|
|
386
|
+
if (onRename) groups[1].push({ id: 'rename', label: L.rename, hint: L.renameKey, keys: 'F2', run: () => startRename(path) });
|
|
387
|
+
for (const m of menuItems || []) {
|
|
388
|
+
if (m.when && !m.when(path, kind)) continue;
|
|
389
|
+
groups[1].push({ id: `x-${m.id}`, label: m.label, disabled: !!m.disabled, run: () => { focusSoon(path); m.onSelect(path, kind); } });
|
|
390
|
+
}
|
|
391
|
+
if (onDelete) groups[2].push({ id: 'delete', label: L.delete, hint: L.deleteKey, keys: 'Delete', danger: true, run: () => askDelete(path) });
|
|
392
|
+
}
|
|
393
|
+
const out = [];
|
|
394
|
+
for (const g of groups) { if (!g.length) continue; if (out.length) out.push({ sep: true }); out.push(...g); }
|
|
395
|
+
return out;
|
|
396
|
+
};
|
|
397
|
+
function openMenu(path, kind, x, y) {
|
|
398
|
+
if (!editable || hold) return false;
|
|
399
|
+
if (!itemsFor(path, kind).length) return false;
|
|
400
|
+
dropEdit(); setAsk(null);
|
|
401
|
+
setMenu({ path, kind, x, y });
|
|
402
|
+
if (path) setActive(path);
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
const menuNow = useRef(null);
|
|
406
|
+
menuNow.current = menu;
|
|
407
|
+
const closeMenu = useCallback((refocus) => {
|
|
408
|
+
const m = menuNow.current;
|
|
409
|
+
if (!m) return;
|
|
410
|
+
if (refocus) pendingFocus.current = { path: m.path || tabPathRef.current, at: Date.now() };
|
|
411
|
+
setMenu(null);
|
|
412
|
+
}, []);
|
|
413
|
+
function menuFromKeyboard(path) {
|
|
414
|
+
const el = rowEl(path);
|
|
415
|
+
const r = el ? el.getBoundingClientRect() : { left: 0, bottom: 0 };
|
|
416
|
+
openMenu(path, nodeKind(path), Math.round(r.left + 24), Math.round(r.bottom));
|
|
417
|
+
}
|
|
418
|
+
const rowEl = (path) => Array.from(rootRef.current?.querySelectorAll('[data-row]') || []).find((x) => x.dataset.row === path);
|
|
419
|
+
|
|
420
|
+
// ── keyboard on the tree ──────────────────────────────────────────────────────────────────────
|
|
421
|
+
function onTreeKeyDown(e) {
|
|
422
|
+
const t = e.target;
|
|
423
|
+
if (t.closest('input, button, .rui-tree-ask')) return;
|
|
424
|
+
const item = t.closest('[role="treeitem"]');
|
|
425
|
+
if (!item || item.dataset.path === undefined) return;
|
|
426
|
+
const act = treeKeyAction(rows, item.dataset.path, e, { editable });
|
|
427
|
+
if (!act) return;
|
|
428
|
+
e.preventDefault();
|
|
429
|
+
switch (act.type) {
|
|
430
|
+
case 'focus': focusSoon(act.path); break;
|
|
431
|
+
case 'open': setOpen([act.path], true); break;
|
|
432
|
+
case 'close': setOpen([act.path], false); break;
|
|
433
|
+
case 'activate':
|
|
434
|
+
closeAll();
|
|
435
|
+
onSelect?.(act.path, act.kind);
|
|
436
|
+
if (act.kind === 'dir') setOpen([act.path], !isOpen(act.path));
|
|
437
|
+
break;
|
|
438
|
+
case 'rename': startRename(act.path); break;
|
|
439
|
+
case 'delete': askDelete(act.path); break;
|
|
440
|
+
case 'menu': menuFromKeyboard(act.path); break;
|
|
441
|
+
default: break;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function onRowClick(r) {
|
|
445
|
+
if (editRef.current?.mode === 'rename' && editRef.current.path === r.path) return;
|
|
446
|
+
dropEdit(); setAsk(null);
|
|
447
|
+
setActive(r.path);
|
|
448
|
+
onSelect?.(r.path, r.kind);
|
|
449
|
+
if (r.kind === 'dir') setOpen([r.path], !isOpen(r.path));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// ── drag a row into a folder ──────────────────────────────────────────────────────────────────
|
|
453
|
+
const dragRef = useRef(null);
|
|
454
|
+
const [dragging, setDragging] = useState(null);
|
|
455
|
+
const [dropDir, setDropDirState] = useState(null);
|
|
456
|
+
const dropRef = useRef(null);
|
|
457
|
+
const hoverTimer = useRef(null);
|
|
458
|
+
const setDropDir = (d) => {
|
|
459
|
+
if (dropRef.current === d) return;
|
|
460
|
+
dropRef.current = d;
|
|
461
|
+
setDropDirState(d);
|
|
462
|
+
clearTimeout(hoverTimer.current);
|
|
463
|
+
if (d && !isOpen(d)) hoverTimer.current = setTimeout(() => { if (dropRef.current === d) setOpen([d], true); }, OPEN_ON_HOVER_MS);
|
|
464
|
+
};
|
|
465
|
+
useEffect(() => () => clearTimeout(hoverTimer.current), []);
|
|
466
|
+
const canDrag = editable && !!onMove && !hold;
|
|
467
|
+
const overDir = (e, dir) => {
|
|
468
|
+
const d = dragRef.current;
|
|
469
|
+
if (!d) return;
|
|
470
|
+
e.stopPropagation();
|
|
471
|
+
if (canMove(tree, d.path, dir)) { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; setDropDir(dir); }
|
|
472
|
+
else { e.dataTransfer.dropEffect = 'none'; setDropDir(null); }
|
|
473
|
+
};
|
|
474
|
+
const dropOn = (e, dir) => {
|
|
475
|
+
const d = dragRef.current;
|
|
476
|
+
if (!d) return;
|
|
477
|
+
e.preventDefault(); e.stopPropagation();
|
|
478
|
+
dragRef.current = null; setDragging(null); setDropDir(null);
|
|
479
|
+
if (canMove(tree, d.path, dir)) void doMove(d.path, d.kind, dir);
|
|
480
|
+
};
|
|
481
|
+
const endDrag = () => { dragRef.current = null; setDragging(null); setDropDir(null); };
|
|
482
|
+
|
|
483
|
+
// ── what the header offers ────────────────────────────────────────────────────────────────────
|
|
484
|
+
const wanted = Array.isArray(actions) ? actions : HEADER_ACTIONS;
|
|
485
|
+
const offered = wanted.filter((a) => (a === 'collapseAll' ? true
|
|
486
|
+
: !editable ? false : a === 'upload' ? !!onUpload : (a === 'newFile' || a === 'newFolder') ? !!onCreate : false));
|
|
487
|
+
const headerAction = {
|
|
488
|
+
newFile: { label: L.newFile, icon: I.newFile, run: () => startCreate('file', targetDir()), mutates: true },
|
|
489
|
+
newFolder: { label: L.newFolder, icon: I.newFolder, run: () => startCreate('dir', targetDir()), mutates: true },
|
|
490
|
+
upload: { label: L.upload, icon: I.upload, run: () => onUpload(targetDir()), mutates: true },
|
|
491
|
+
collapseAll: { label: L.collapseAll, icon: I.collapseAll, run: collapseAll, mutates: false },
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// ── rendering ─────────────────────────────────────────────────────────────────────────────────
|
|
495
|
+
const pendingSet = useMemo(() => new Set(pending || []), [pending]);
|
|
496
|
+
const errs = rowErrors || {};
|
|
497
|
+
const depthStyle = (depth) => ({ '--rui-tree-d': depth });
|
|
498
|
+
let msgN = 0;
|
|
499
|
+
|
|
500
|
+
const entryRow = (dir, depth) => {
|
|
501
|
+
const ed = edit;
|
|
502
|
+
const errId = `${uid}-new-err`;
|
|
503
|
+
return (
|
|
504
|
+
<li key="__rui-tree-new" role="treeitem" aria-level={depth} aria-selected={false} className="rui-tree-item is-new"
|
|
505
|
+
aria-label={ed.kind === 'dir' ? L.newFolderInput : L.newFileInput}>
|
|
506
|
+
<div className="rui-tree-row is-editing" style={depthStyle(depth - 1)}>
|
|
507
|
+
<span className="rui-tree-twisty" aria-hidden="true" />
|
|
508
|
+
<span className="rui-tree-icon" aria-hidden="true">{ed.kind === 'dir' ? I.folder : I.file}</span>
|
|
509
|
+
{nameInput(ed, ed.kind === 'dir' ? L.newFolderInput : L.newFileInput, errId)}
|
|
510
|
+
</div>
|
|
511
|
+
{ed.err ? <p className="rui-tree-msg is-error" id={errId} role="alert" style={depthStyle(depth - 1)}>{ed.err}</p> : null}
|
|
512
|
+
</li>
|
|
513
|
+
);
|
|
514
|
+
};
|
|
515
|
+
function nameInput(ed, aria, errId) {
|
|
516
|
+
return (
|
|
517
|
+
<input ref={inputRef} className="rui-tree-input" value={ed.value} aria-label={aria} autoComplete="off" spellCheck={false}
|
|
518
|
+
readOnly={ed.busy} aria-busy={ed.busy || undefined} aria-invalid={!!ed.err} aria-describedby={ed.err ? errId : undefined}
|
|
519
|
+
onClick={(e) => e.stopPropagation()} onDoubleClick={(e) => e.stopPropagation()}
|
|
520
|
+
onChange={(e) => setEdit({ ...ed, value: e.target.value, err: '' })}
|
|
521
|
+
onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); void commitEdit(false); } }}
|
|
522
|
+
onBlur={() => { if (editRef.current) void commitEdit(true); }} />
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function renderNode(n) {
|
|
527
|
+
const r = { path: n.path, kind: n.kind, depth: n.depth };
|
|
528
|
+
const isDir = n.kind === 'dir';
|
|
529
|
+
const open = isDir && isOpen(n.path);
|
|
530
|
+
const renaming = edit && edit.mode === 'rename' && edit.path === n.path;
|
|
531
|
+
const asking = ask && ask.path === n.path;
|
|
532
|
+
const sel = selected === n.path;
|
|
533
|
+
const busyRow = pendingSet.has(n.path);
|
|
534
|
+
const msg = errs[n.path] || notes[n.path] || '';
|
|
535
|
+
const msgId = msg ? `${uid}-m${msgN++}` : undefined;
|
|
536
|
+
const errId = `${uid}-ren-err`;
|
|
537
|
+
const creatingHere = edit && edit.mode === 'create' && isDir && edit.dir === n.path;
|
|
538
|
+
const kids = open ? n.children : [];
|
|
539
|
+
const cls = ['rui-tree-row', sel ? 'is-selected' : '', isDir && active === n.path ? 'is-target' : '',
|
|
540
|
+
menu && menu.path === n.path ? 'is-menu' : '', busyRow ? 'is-pending' : '', renaming ? 'is-editing' : '',
|
|
541
|
+
dragging === n.path ? 'is-dragging' : ''].filter(Boolean).join(' ');
|
|
542
|
+
const showActs = editable && !renaming && !busyRow && (onRename || onDelete);
|
|
543
|
+
return (
|
|
544
|
+
<li key={n.path} role="treeitem" data-path={n.path} aria-level={n.depth} aria-expanded={isDir ? open : undefined}
|
|
545
|
+
aria-selected={sel} aria-busy={busyRow || undefined} aria-describedby={msgId}
|
|
546
|
+
tabIndex={tabPath === n.path ? 0 : -1}
|
|
547
|
+
className={`rui-tree-item${dropDir === n.path ? ' is-drop' : ''}`}>
|
|
548
|
+
<div className={cls} data-row={n.path} style={depthStyle(n.depth - 1)} title={n.path}
|
|
549
|
+
draggable={canDrag && !renaming ? true : undefined}
|
|
550
|
+
onClick={() => onRowClick(r)}
|
|
551
|
+
onContextMenu={(e) => { if (openMenu(n.path, n.kind, e.clientX, e.clientY)) e.preventDefault(); e.stopPropagation(); }}
|
|
552
|
+
onDragStart={canDrag ? (e) => {
|
|
553
|
+
closeAll();
|
|
554
|
+
dragRef.current = { path: n.path, kind: n.kind };
|
|
555
|
+
setDragging(n.path);
|
|
556
|
+
e.dataTransfer.effectAllowed = 'move';
|
|
557
|
+
try { e.dataTransfer.setData(DRAG_TYPE, n.path); e.dataTransfer.setData('text/plain', n.path); } catch { /* old engines */ }
|
|
558
|
+
} : undefined}
|
|
559
|
+
onDragEnd={canDrag ? endDrag : undefined}
|
|
560
|
+
onDragEnter={(e) => overDir(e, isDir ? n.path : parentOf(n.path))}
|
|
561
|
+
onDragOver={(e) => overDir(e, isDir ? n.path : parentOf(n.path))}
|
|
562
|
+
onDrop={(e) => dropOn(e, isDir ? n.path : parentOf(n.path))}>
|
|
563
|
+
<span className={`rui-tree-twisty${open ? ' is-open' : ''}`} aria-hidden="true">{isDir ? I.chevron : null}</span>
|
|
564
|
+
<span className="rui-tree-icon" aria-hidden="true">{isDir ? I.folder : I.file}</span>
|
|
565
|
+
{renaming ? nameInput(edit, say(L.renameInput, n.name), errId) : <Name name={n.name} />}
|
|
566
|
+
{!renaming && n.note ? <span className="rui-tree-note">{n.note}</span> : null}
|
|
567
|
+
{!renaming && isDir && !n.children.length ? <span className="rui-tree-tag">{L.emptyFolder}</span> : null}
|
|
568
|
+
{busyRow ? <span className="rui-tree-spin" role="status"><span className="rui-tree-sr">{L.saving}</span></span> : null}
|
|
569
|
+
{showActs ? (
|
|
570
|
+
<span className="rui-tree-acts" aria-hidden="true">
|
|
571
|
+
{onRename ? (
|
|
572
|
+
<button type="button" className="rui-tree-ib" tabIndex={-1} title={L.rename} disabled={hold}
|
|
573
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
574
|
+
onClick={(e) => { e.stopPropagation(); startRename(n.path); }}>{I.pencil}</button>
|
|
575
|
+
) : null}
|
|
576
|
+
{onDelete ? (
|
|
577
|
+
<button type="button" className="rui-tree-ib" tabIndex={-1} title={L.delete} disabled={hold}
|
|
578
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
579
|
+
onClick={(e) => { e.stopPropagation(); askDelete(n.path); }}>{I.trash}</button>
|
|
580
|
+
) : null}
|
|
581
|
+
</span>
|
|
582
|
+
) : null}
|
|
583
|
+
</div>
|
|
584
|
+
{renaming && edit.err ? <p className="rui-tree-msg is-error" id={errId} role="alert" style={depthStyle(n.depth - 1)}>{edit.err}</p> : null}
|
|
585
|
+
{msg ? <p className="rui-tree-msg is-error" id={msgId} style={depthStyle(n.depth - 1)}>{msg}</p> : null}
|
|
586
|
+
{asking ? (
|
|
587
|
+
<div className="rui-tree-ask" role="group" aria-label={isDir ? say(L.confirmFolder, n.name, n.files) : say(L.confirmFile, n.name)}
|
|
588
|
+
style={depthStyle(n.depth - 1)}>
|
|
589
|
+
<span className="rui-tree-ask-q">{isDir ? say(L.confirmFolder, n.name, n.files) : say(L.confirmFile, n.name)}</span>
|
|
590
|
+
<span className="rui-tree-ask-b">
|
|
591
|
+
<button type="button" className="rui-tree-link" disabled={ask.busy} onClick={() => { setAsk(null); focusSoon(n.path); }}>{L.keep}</button>
|
|
592
|
+
<button type="button" className="rui-tree-link is-danger" ref={askRef} disabled={ask.busy} onClick={() => void confirmDelete()}>{L.delete}</button>
|
|
593
|
+
</span>
|
|
594
|
+
</div>
|
|
595
|
+
) : null}
|
|
596
|
+
{(open && kids.length) || creatingHere ? (
|
|
597
|
+
<ul role="group" className="rui-tree-group">
|
|
598
|
+
{kids.map(renderNode)}
|
|
599
|
+
{creatingHere ? entryRow(n.path, n.depth + 1) : null}
|
|
600
|
+
</ul>
|
|
601
|
+
) : null}
|
|
602
|
+
</li>
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const empty = !tree.nodes.length;
|
|
607
|
+
const creatingAtTop = edit && edit.mode === 'create' && edit.dir === '';
|
|
608
|
+
const treeLabel = label || title || L.tree;
|
|
609
|
+
const rootClass = ['rui-tree', readOnly ? 'is-readonly' : '', className || ''].filter(Boolean).join(' ');
|
|
610
|
+
|
|
611
|
+
return (
|
|
612
|
+
<div ref={rootRef} className={rootClass} style={style}>
|
|
613
|
+
{title || offered.length ? (
|
|
614
|
+
<div className="rui-tree-head">
|
|
615
|
+
{title ? <span className="rui-tree-title">{title}</span> : <span className="rui-tree-title" />}
|
|
616
|
+
{offered.length ? (
|
|
617
|
+
<span className="rui-tree-tools">
|
|
618
|
+
{offered.map((a) => (
|
|
619
|
+
<button key={a} type="button" className="rui-tree-tool" aria-label={headerAction[a].label} title={headerAction[a].label}
|
|
620
|
+
disabled={headerAction[a].mutates && hold} onClick={headerAction[a].run}>{headerAction[a].icon}</button>
|
|
621
|
+
))}
|
|
622
|
+
</span>
|
|
623
|
+
) : null}
|
|
624
|
+
</div>
|
|
625
|
+
) : null}
|
|
626
|
+
{error ? <p className="rui-tree-msg is-error is-tree" role="alert">{error}</p> : null}
|
|
627
|
+
<div className={`rui-tree-body${dropDir === '' ? ' is-drop' : ''}`}
|
|
628
|
+
onClick={(e) => { if (e.target === e.currentTarget || e.target.classList?.contains('rui-tree-list')) { dropEdit(); setAsk(null); setActive(''); } }}
|
|
629
|
+
onContextMenu={(e) => { if (e.target.closest('[data-row]')) return; if (openMenu('', 'dir', e.clientX, e.clientY)) e.preventDefault(); }}
|
|
630
|
+
onDragEnter={(e) => overDir(e, '')} onDragOver={(e) => overDir(e, '')} onDrop={(e) => dropOn(e, '')}
|
|
631
|
+
onDragLeave={(e) => {
|
|
632
|
+
// Chrome often gives a drag's leave no relatedTarget, even between two rows: where the
|
|
633
|
+
// pointer is says whether it left the tree
|
|
634
|
+
const b = e.currentTarget.getBoundingClientRect();
|
|
635
|
+
const out = e.relatedTarget ? !e.currentTarget.contains(e.relatedTarget)
|
|
636
|
+
: e.clientX < b.left || e.clientX >= b.right || e.clientY < b.top || e.clientY >= b.bottom;
|
|
637
|
+
if (out) setDropDir(null);
|
|
638
|
+
}}>
|
|
639
|
+
{busy && empty ? (
|
|
640
|
+
<div className="rui-tree-skel" role="status">
|
|
641
|
+
<span className="rui-tree-sr">{L.loading}</span>
|
|
642
|
+
{[64, 48, 56].map((w, i) => <span key={i} className="rui-tree-skel-line" style={{ width: `${w}%` }} aria-hidden="true" />)}
|
|
643
|
+
</div>
|
|
644
|
+
) : null}
|
|
645
|
+
{!busy && empty && !creatingAtTop ? <p className="rui-tree-empty">{L.empty}</p> : null}
|
|
646
|
+
{!empty || creatingAtTop ? (
|
|
647
|
+
<ul role="tree" className="rui-tree-list" aria-label={treeLabel} aria-busy={busy || undefined}
|
|
648
|
+
onKeyDown={onTreeKeyDown}
|
|
649
|
+
onFocus={(e) => { const p = e.target.getAttribute?.('data-path'); if (e.target.getAttribute?.('role') === 'treeitem' && p !== null && p !== active) setActive(p); }}>
|
|
650
|
+
{tree.nodes.map(renderNode)}
|
|
651
|
+
{creatingAtTop ? entryRow('', 1) : null}
|
|
652
|
+
</ul>
|
|
653
|
+
) : null}
|
|
654
|
+
</div>
|
|
655
|
+
{menu && typeof document !== 'undefined' ? (
|
|
656
|
+
<TreeMenu menu={menu} items={itemsFor(menu.path, menu.kind)} label={say(L.menu, menu.path ? baseName(menu.path) : treeLabel)}
|
|
657
|
+
tokensFrom={rootRef} menuRef={menuRef} onClose={closeMenu} />
|
|
658
|
+
) : null}
|
|
659
|
+
</div>
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/** Every folder above `dir`, nearest last. */
|
|
664
|
+
function ancestorsOf(dir) {
|
|
665
|
+
const out = [];
|
|
666
|
+
for (let p = parentOf(dir); p; p = parentOf(p)) out.unshift(p);
|
|
667
|
+
return out;
|
|
668
|
+
}
|