reifyui 0.18.5 → 0.19.0
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/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/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,178 @@
|
|
|
1
|
+
// Types for reifyui/tree. Hand-written (the source is JSX), kept beside the source.
|
|
2
|
+
import type { ComponentType, CSSProperties } from 'react';
|
|
3
|
+
|
|
4
|
+
export type TreeKind = 'file' | 'dir';
|
|
5
|
+
|
|
6
|
+
/** One file or folder. A file's folders are implied by its path; a 'dir' entry adds a folder
|
|
7
|
+
* that may be empty. */
|
|
8
|
+
export interface TreeEntry {
|
|
9
|
+
/** forward slashes, no leading slash: 'tasks/refund' */
|
|
10
|
+
path: string;
|
|
11
|
+
kind: TreeKind;
|
|
12
|
+
/** a short quiet word after the name ("edited", "2 KB") */
|
|
13
|
+
note?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface TreeNode {
|
|
17
|
+
path: string;
|
|
18
|
+
name: string;
|
|
19
|
+
kind: TreeKind;
|
|
20
|
+
/** 1 at the top */
|
|
21
|
+
depth: number;
|
|
22
|
+
children: TreeNode[];
|
|
23
|
+
/** files inside a folder, all the way down (0 for a file) */
|
|
24
|
+
files: number;
|
|
25
|
+
note?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FileTreeModel {
|
|
29
|
+
/** the top ('' path) */
|
|
30
|
+
root: TreeNode;
|
|
31
|
+
/** the top level, sorted folders first then by name */
|
|
32
|
+
nodes: TreeNode[];
|
|
33
|
+
byPath: Map<string, TreeNode>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TreeRow {
|
|
37
|
+
path: string;
|
|
38
|
+
name: string;
|
|
39
|
+
kind: TreeKind;
|
|
40
|
+
depth: number;
|
|
41
|
+
parent: string;
|
|
42
|
+
open: boolean;
|
|
43
|
+
hasChildren: boolean;
|
|
44
|
+
files: number;
|
|
45
|
+
note?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type TreeKeyAction =
|
|
49
|
+
| { type: 'focus' | 'open' | 'close' | 'rename' | 'delete' | 'menu'; path: string }
|
|
50
|
+
| { type: 'activate'; path: string; kind: TreeKind }
|
|
51
|
+
| { type: 'none' };
|
|
52
|
+
|
|
53
|
+
/** Every word FileTree shows. A function takes the values named in its parameters. */
|
|
54
|
+
export interface TreeLabels {
|
|
55
|
+
/** the tree's accessible name when there is no title */
|
|
56
|
+
tree: string;
|
|
57
|
+
newFile: string;
|
|
58
|
+
newFolder: string;
|
|
59
|
+
upload: string;
|
|
60
|
+
collapseAll: string;
|
|
61
|
+
rename: string;
|
|
62
|
+
delete: string;
|
|
63
|
+
/** the button that puts a delete question away */
|
|
64
|
+
keep: string;
|
|
65
|
+
/** the key named beside Rename and Delete in the menu */
|
|
66
|
+
renameKey: string;
|
|
67
|
+
deleteKey: string;
|
|
68
|
+
/** shown when there are no entries */
|
|
69
|
+
empty: string;
|
|
70
|
+
/** the tag on a folder with nothing in it */
|
|
71
|
+
emptyFolder: string;
|
|
72
|
+
loading: string;
|
|
73
|
+
saving: string;
|
|
74
|
+
/** shown when a handler throws */
|
|
75
|
+
failed: string;
|
|
76
|
+
newFileInput: string;
|
|
77
|
+
newFolderInput: string;
|
|
78
|
+
renameInput: string | ((name: string) => string);
|
|
79
|
+
confirmFile: string | ((name: string) => string);
|
|
80
|
+
confirmFolder: string | ((name: string, files: number) => string);
|
|
81
|
+
/** the menu's accessible name */
|
|
82
|
+
menu: string | ((name: string) => string);
|
|
83
|
+
nameEmpty: string | ((kind: TreeKind) => string);
|
|
84
|
+
nameDots: string | ((kind: TreeKind) => string);
|
|
85
|
+
nameSlash: string | ((kind: TreeKind) => string);
|
|
86
|
+
nameInvisible: string | ((kind: TreeKind) => string);
|
|
87
|
+
nameTaken: string | ((name: string, kind: TreeKind) => string);
|
|
88
|
+
}
|
|
89
|
+
export const DEFAULT_TREE_LABELS: Readonly<TreeLabels>;
|
|
90
|
+
|
|
91
|
+
/** A handler's answer: nothing when it went through, a message to refuse (shown where the person
|
|
92
|
+
* acted). A thrown error or a rejected Promise shows `labels.failed`. */
|
|
93
|
+
export type TreeAnswer = void | string | Promise<void | string>;
|
|
94
|
+
|
|
95
|
+
export type TreeHeaderAction = 'newFile' | 'newFolder' | 'upload' | 'collapseAll';
|
|
96
|
+
|
|
97
|
+
export interface TreeMenuItem {
|
|
98
|
+
id: string;
|
|
99
|
+
label: string;
|
|
100
|
+
onSelect: (path: string, kind: TreeKind) => void;
|
|
101
|
+
/** shown only where this answers true (e.g. files only) */
|
|
102
|
+
when?: (path: string, kind: TreeKind) => boolean;
|
|
103
|
+
disabled?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface FileTreeProps {
|
|
107
|
+
entries: TreeEntry[];
|
|
108
|
+
/** the open file (or folder): drawn selected, and its folders open when it changes */
|
|
109
|
+
selected?: string | null;
|
|
110
|
+
/** a row was clicked, or Enter / Space was pressed on it */
|
|
111
|
+
onSelect?: (path: string, kind: TreeKind) => void;
|
|
112
|
+
|
|
113
|
+
/** open folders, controlled; pair with onOpenChange */
|
|
114
|
+
open?: string[];
|
|
115
|
+
/** uncontrolled: true (default) opens every folder, a list opens those */
|
|
116
|
+
defaultOpen?: boolean | string[];
|
|
117
|
+
onOpenChange?: (open: string[]) => void;
|
|
118
|
+
|
|
119
|
+
/** the header's quiet label (shown uppercase) and the tree's accessible name */
|
|
120
|
+
title?: string;
|
|
121
|
+
/** the accessible name when it should differ from the title */
|
|
122
|
+
label?: string;
|
|
123
|
+
/** which header actions to show, beside the title. Default: all whose handler is given
|
|
124
|
+
* (Collapse all always). */
|
|
125
|
+
actions?: TreeHeaderAction[];
|
|
126
|
+
labels?: Partial<TreeLabels>;
|
|
127
|
+
|
|
128
|
+
/** a name typed for New file / New folder, in the folder last used (or the top) */
|
|
129
|
+
onCreate?: (e: { path: string; kind: TreeKind }) => TreeAnswer;
|
|
130
|
+
/** F2, the row's pencil, or the menu */
|
|
131
|
+
onRename?: (e: { from: string; to: string; kind: TreeKind }) => TreeAnswer;
|
|
132
|
+
/** after the in-row question is answered Delete */
|
|
133
|
+
onDelete?: (e: { path: string; kind: TreeKind }) => TreeAnswer;
|
|
134
|
+
/** a row dropped on a folder ('' is the top); never into itself or a folder inside it */
|
|
135
|
+
onMove?: (e: { from: string; toDir: string; kind: TreeKind }) => TreeAnswer;
|
|
136
|
+
/** the header's Upload: open your own file picker for `dirPath` ('' is the top) */
|
|
137
|
+
onUpload?: (dirPath: string) => void;
|
|
138
|
+
/** your own rule, asked after the defaults ('' . .. a slash, a taken name) pass; '' allows it */
|
|
139
|
+
validateName?: (name: string, dirPath: string, kind: TreeKind) => string;
|
|
140
|
+
/** more choices in a row's menu, between Rename and Delete */
|
|
141
|
+
menuItems?: TreeMenuItem[];
|
|
142
|
+
|
|
143
|
+
/** no header actions but Collapse all, no menu, no row actions, no dragging */
|
|
144
|
+
readOnly?: boolean;
|
|
145
|
+
/** a load or a save in flight: changes wait, and an empty tree shows loading rows */
|
|
146
|
+
busy?: boolean;
|
|
147
|
+
/** rows with a save of their own in flight */
|
|
148
|
+
pending?: string[];
|
|
149
|
+
/** a message under a row, by path */
|
|
150
|
+
rowErrors?: Record<string, string>;
|
|
151
|
+
/** a message under the header, for the whole tree */
|
|
152
|
+
error?: string;
|
|
153
|
+
|
|
154
|
+
className?: string;
|
|
155
|
+
style?: CSSProperties;
|
|
156
|
+
}
|
|
157
|
+
export const FileTree: ComponentType<FileTreeProps>;
|
|
158
|
+
|
|
159
|
+
export function buildFileTree(entries: TreeEntry[]): FileTreeModel;
|
|
160
|
+
export function visibleRows(nodes: TreeNode[], isOpen: Set<string> | ((path: string) => boolean)): TreeRow[];
|
|
161
|
+
/** What a key does on the focused row (WAI-ARIA tree pattern); null when the key is not the tree's. */
|
|
162
|
+
export function treeKeyAction(rows: TreeRow[], active: string | null, ev: string | { key: string; shiftKey?: boolean; ctrlKey?: boolean; metaKey?: boolean; altKey?: boolean },
|
|
163
|
+
opts?: { editable?: boolean }): TreeKeyAction | null;
|
|
164
|
+
/** Why a name can't be used in `dir`, or ''. */
|
|
165
|
+
export function nameProblem(name: string, opts?: {
|
|
166
|
+
kind?: TreeKind; dir?: string; siblings?: Iterable<string>; labels?: Partial<TreeLabels>;
|
|
167
|
+
validate?: (name: string, dirPath: string, kind: TreeKind) => string;
|
|
168
|
+
}): string;
|
|
169
|
+
/** Whether `from` may move into `toDir`: not into itself or below it, not where it is, not onto a taken name. */
|
|
170
|
+
export function canMove(tree: FileTreeModel, from: string, toDir: string): boolean;
|
|
171
|
+
export function folderPaths(tree: FileTreeModel): string[];
|
|
172
|
+
export function parentOf(path: string): string;
|
|
173
|
+
export function baseName(path: string): string;
|
|
174
|
+
export function joinPath(dir: string, name: string): string;
|
|
175
|
+
export function isWithin(path: string, dir: string): boolean;
|
|
176
|
+
export function movedPath(path: string, from: string, to: string): string;
|
|
177
|
+
export function normalizePath(raw: string): string;
|
|
178
|
+
export function splitExt(name: string): [string, string];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// reifyui/tree: a folder tree of files the way an editor's Explorer shows one.
|
|
2
|
+
//
|
|
3
|
+
// the view FileTree (styles: reifyui/styles/tree.css)
|
|
4
|
+
// the model buildFileTree, visibleRows, treeKeyAction, nameProblem, canMove, folderPaths,
|
|
5
|
+
// parentOf, baseName, joinPath, isWithin, movedPath, normalizePath, splitExt,
|
|
6
|
+
// DEFAULT_TREE_LABELS
|
|
7
|
+
//
|
|
8
|
+
// A separate entry so a host that renders a Button never pulls in a tree. The model has no React
|
|
9
|
+
// and no DOM: a host can check a name or a move with the same rules the tree uses.
|
|
10
|
+
export { FileTree } from './FileTree.jsx';
|
|
11
|
+
export {
|
|
12
|
+
buildFileTree, visibleRows, treeKeyAction, nameProblem, canMove, folderPaths,
|
|
13
|
+
parentOf, baseName, joinPath, isWithin, movedPath, normalizePath, splitExt, DEFAULT_TREE_LABELS,
|
|
14
|
+
} from './model.js';
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// reifyui/tree — the model behind FileTree, with no React and no DOM, so every rule the tree
|
|
2
|
+
// follows (how paths become folders, which rows are visible, what a key does, which names and
|
|
3
|
+
// moves are refused) is a plain function a test can call and a host can reuse.
|
|
4
|
+
|
|
5
|
+
/** The folder a path sits in ('' is the top). */
|
|
6
|
+
export const parentOf = (p) => { const i = p.lastIndexOf('/'); return i < 0 ? '' : p.slice(0, i); };
|
|
7
|
+
/** A path's last part. */
|
|
8
|
+
export const baseName = (p) => p.slice(p.lastIndexOf('/') + 1);
|
|
9
|
+
/** A name inside a folder ('' is the top). */
|
|
10
|
+
export const joinPath = (dir, name) => (dir ? `${dir}/${name}` : name);
|
|
11
|
+
/** True when `p` is `dir` or anything inside it. */
|
|
12
|
+
export const isWithin = (p, dir) => p === dir || p.startsWith(dir + '/');
|
|
13
|
+
|
|
14
|
+
/** A path as the tree keys it: forward slashes, nothing doubled, none at either end. */
|
|
15
|
+
export function normalizePath(raw) {
|
|
16
|
+
return String(raw ?? '').replace(/\\/g, '/').replace(/\/{2,}/g, '/').replace(/^\/+|\/+$/g, '');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A name cut for display: the extension kept whole when the rest is cut ("very-long-na….md").
|
|
20
|
+
* A leading dot (".env") is part of the name, not an extension. */
|
|
21
|
+
export function splitExt(name) {
|
|
22
|
+
const i = name.lastIndexOf('.');
|
|
23
|
+
return i <= 0 ? [name, ''] : [name.slice(0, i), name.slice(i)];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const byName = (a, b) => (a.kind !== b.kind ? (a.kind === 'dir' ? -1 : 1)
|
|
27
|
+
: a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' }) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The tree drawn from a flat list of entries.
|
|
31
|
+
* entries [{ path: 'tasks/refund', kind: 'file' | 'dir', note? }]
|
|
32
|
+
* A file's folders are implied by its path; a 'dir' entry adds a folder that may be empty. Each
|
|
33
|
+
* level is sorted folders first, then by name (numbers in names compare as numbers). A folder's
|
|
34
|
+
* `files` counts every file inside it, all the way down. When one path is given both as a file and
|
|
35
|
+
* as a folder (or a file sits "inside" a file), the folder wins and the file entry is left out.
|
|
36
|
+
*/
|
|
37
|
+
export function buildFileTree(entries) {
|
|
38
|
+
const root = { path: '', name: '', kind: 'dir', depth: 0, children: [], files: 0 };
|
|
39
|
+
const byPath = new Map([['', root]]);
|
|
40
|
+
const ensureDir = (path) => {
|
|
41
|
+
const hit = byPath.get(path);
|
|
42
|
+
if (hit) {
|
|
43
|
+
if (hit.kind === 'dir') return hit;
|
|
44
|
+
// a file at a path something else sits inside: the folder takes its place
|
|
45
|
+
hit.kind = 'dir'; hit.children = []; hit.files = 0; delete hit.note;
|
|
46
|
+
return hit;
|
|
47
|
+
}
|
|
48
|
+
const parent = ensureDir(parentOf(path));
|
|
49
|
+
const node = { path, name: baseName(path), kind: 'dir', depth: parent.depth + 1, children: [], files: 0 };
|
|
50
|
+
parent.children.push(node);
|
|
51
|
+
byPath.set(path, node);
|
|
52
|
+
return node;
|
|
53
|
+
};
|
|
54
|
+
for (const e of entries || []) {
|
|
55
|
+
const path = normalizePath(e && e.path);
|
|
56
|
+
if (!path) continue;
|
|
57
|
+
if (e.kind === 'dir') { const d = ensureDir(path); if (e.note) d.note = String(e.note); continue; }
|
|
58
|
+
if (byPath.has(path)) continue; // a folder (or the same file) already holds this path
|
|
59
|
+
const parent = ensureDir(parentOf(path));
|
|
60
|
+
const node = { path, name: baseName(path), kind: 'file', depth: parent.depth + 1, children: [], files: 0 };
|
|
61
|
+
if (e.note) node.note = String(e.note);
|
|
62
|
+
parent.children.push(node);
|
|
63
|
+
byPath.set(path, node);
|
|
64
|
+
}
|
|
65
|
+
const finish = (n) => {
|
|
66
|
+
n.children.sort(byName);
|
|
67
|
+
n.files = 0;
|
|
68
|
+
for (const c of n.children) { if (c.kind === 'dir') { finish(c); n.files += c.files; } else n.files += 1; }
|
|
69
|
+
};
|
|
70
|
+
finish(root);
|
|
71
|
+
return { root, nodes: root.children, byPath };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Every folder path in the tree. */
|
|
75
|
+
export function folderPaths(tree) {
|
|
76
|
+
const out = [];
|
|
77
|
+
for (const [p, n] of tree.byPath) if (p && n.kind === 'dir') out.push(p);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The rows a person can see, top to bottom: every node whose folders are all open.
|
|
83
|
+
* Each row: { path, name, kind, depth (1 at the top), parent, open, hasChildren, files, note }.
|
|
84
|
+
*/
|
|
85
|
+
export function visibleRows(nodes, isOpen) {
|
|
86
|
+
const open = typeof isOpen === 'function' ? isOpen : (p) => !!(isOpen && isOpen.has && isOpen.has(p));
|
|
87
|
+
const rows = [];
|
|
88
|
+
const walk = (list) => {
|
|
89
|
+
for (const n of list) {
|
|
90
|
+
const o = n.kind === 'dir' && open(n.path);
|
|
91
|
+
rows.push({ path: n.path, name: n.name, kind: n.kind, depth: n.depth, parent: parentOf(n.path), open: o,
|
|
92
|
+
hasChildren: n.children.length > 0, files: n.files, note: n.note });
|
|
93
|
+
if (o) walk(n.children);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
walk(nodes || []);
|
|
97
|
+
return rows;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* What a key does on the focused row, per the WAI-ARIA tree pattern. Pure: it reads the visible
|
|
102
|
+
* rows and returns an action for the component to apply, or null when the key is not the tree's.
|
|
103
|
+
* { type: 'focus', path } move focus to a row
|
|
104
|
+
* { type: 'open', path } open a closed folder (focus stays)
|
|
105
|
+
* { type: 'close', path } close an open folder (focus stays)
|
|
106
|
+
* { type: 'activate', path, kind } Enter / Space: select it, and open or close a folder
|
|
107
|
+
* { type: 'rename', path } / { type: 'delete', path } / { type: 'menu', path }
|
|
108
|
+
* `opts.editable` false drops rename, delete and the menu.
|
|
109
|
+
*/
|
|
110
|
+
export function treeKeyAction(rows, active, ev, opts = {}) {
|
|
111
|
+
const key = typeof ev === 'string' ? ev : ev.key;
|
|
112
|
+
const shift = typeof ev === 'object' && !!ev.shiftKey;
|
|
113
|
+
const mod = typeof ev === 'object' && !!(ev.ctrlKey || ev.metaKey || ev.altKey);
|
|
114
|
+
if (!rows.length) return null;
|
|
115
|
+
let i = rows.findIndex((r) => r.path === active);
|
|
116
|
+
if (i < 0) i = 0;
|
|
117
|
+
const row = rows[i];
|
|
118
|
+
const editable = opts.editable !== false;
|
|
119
|
+
if (mod && key !== 'F10') return null;
|
|
120
|
+
switch (key) {
|
|
121
|
+
case 'ArrowDown': return i < rows.length - 1 ? { type: 'focus', path: rows[i + 1].path } : { type: 'focus', path: row.path };
|
|
122
|
+
case 'ArrowUp': return i > 0 ? { type: 'focus', path: rows[i - 1].path } : { type: 'focus', path: row.path };
|
|
123
|
+
case 'Home': return { type: 'focus', path: rows[0].path };
|
|
124
|
+
case 'End': return { type: 'focus', path: rows[rows.length - 1].path };
|
|
125
|
+
case 'ArrowRight':
|
|
126
|
+
if (row.kind !== 'dir') return { type: 'none' };
|
|
127
|
+
if (!row.open) return { type: 'open', path: row.path };
|
|
128
|
+
return row.hasChildren && rows[i + 1] ? { type: 'focus', path: rows[i + 1].path } : { type: 'none' };
|
|
129
|
+
case 'ArrowLeft':
|
|
130
|
+
if (row.kind === 'dir' && row.open) return { type: 'close', path: row.path };
|
|
131
|
+
return row.parent ? { type: 'focus', path: row.parent } : { type: 'none' };
|
|
132
|
+
case 'Enter': case ' ': case 'Spacebar':
|
|
133
|
+
return { type: 'activate', path: row.path, kind: row.kind };
|
|
134
|
+
case 'F2': return editable ? { type: 'rename', path: row.path } : null;
|
|
135
|
+
case 'Delete': case 'Backspace': return editable ? { type: 'delete', path: row.path } : null;
|
|
136
|
+
case 'ContextMenu': return editable ? { type: 'menu', path: row.path } : null;
|
|
137
|
+
case 'F10': return shift && editable ? { type: 'menu', path: row.path } : null;
|
|
138
|
+
default: return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const invisible = (s) => Array.from(s).some((c) => { const n = c.charCodeAt(0); return n < 32 || n === 127; });
|
|
143
|
+
const say = (v, ...a) => (typeof v === 'function' ? v(...a) : v);
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Why `name` can't be used for a new or renamed entry in `dir`, or '' when it can.
|
|
147
|
+
* kind 'file' | 'dir'
|
|
148
|
+
* siblings the names already in `dir` (the entry being renamed left out)
|
|
149
|
+
* labels the words (DEFAULT_TREE_LABELS' name* keys)
|
|
150
|
+
* validate the host's own rule, (name, dir, kind) => message | '' — asked only once the
|
|
151
|
+
* defaults pass, so it never sees '', '.', '..' or a name with a slash
|
|
152
|
+
*/
|
|
153
|
+
export function nameProblem(name, { kind = 'file', dir = '', siblings = [], labels = {}, validate } = {}) {
|
|
154
|
+
const L = { ...NAME_LABELS, ...labels };
|
|
155
|
+
const n = String(name ?? '').trim();
|
|
156
|
+
if (!n) return say(L.nameEmpty, kind);
|
|
157
|
+
if (n === '.' || n === '..') return say(L.nameDots, kind);
|
|
158
|
+
if (/[\\/]/.test(n)) return say(L.nameSlash, kind);
|
|
159
|
+
if (invisible(n)) return say(L.nameInvisible, kind);
|
|
160
|
+
const lower = n.toLowerCase();
|
|
161
|
+
const sib = Array.from(siblings || []);
|
|
162
|
+
if (sib.some((s) => s === n)) return say(L.nameTaken, n, kind);
|
|
163
|
+
// two names that differ only in case read as one on most computers a file is later saved to
|
|
164
|
+
if (sib.some((s) => s.toLowerCase() === lower)) return say(L.nameTaken, sib.find((s) => s.toLowerCase() === lower), kind);
|
|
165
|
+
if (typeof validate === 'function') return validate(n, dir, kind) || '';
|
|
166
|
+
return '';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Whether `from` (a file or folder) may move into `toDir` ('' is the top): never into itself or a
|
|
171
|
+
* folder inside it, never where it already is, never onto a name already taken there, and only
|
|
172
|
+
* into a folder that exists. `tree` is buildFileTree's result.
|
|
173
|
+
*/
|
|
174
|
+
export function canMove(tree, from, toDir) {
|
|
175
|
+
const node = tree.byPath.get(from);
|
|
176
|
+
if (!node || !from) return false;
|
|
177
|
+
const dest = tree.byPath.get(toDir);
|
|
178
|
+
if (!dest || dest.kind !== 'dir') return false;
|
|
179
|
+
if (parentOf(from) === toDir) return false;
|
|
180
|
+
if (node.kind === 'dir' && isWithin(toDir, from)) return false;
|
|
181
|
+
return !dest.children.some((c) => c.name.toLowerCase() === node.name.toLowerCase());
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Rewrite a path when `from` moved to `to` (a folder's insides move with it). */
|
|
185
|
+
export const movedPath = (p, from, to) => (isWithin(p, from) ? to + p.slice(from.length) : p);
|
|
186
|
+
|
|
187
|
+
const NAME_LABELS = {
|
|
188
|
+
nameEmpty: (kind) => (kind === 'dir' ? 'Type a folder name.' : 'Type a file name.'),
|
|
189
|
+
nameDots: 'Choose a name other than . or ..',
|
|
190
|
+
nameSlash: 'Use a name without a slash.',
|
|
191
|
+
nameInvisible: 'Use visible characters only.',
|
|
192
|
+
nameTaken: (name) => `${name} is already here.`,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/** Every word FileTree shows, each overridable through `labels`. A function takes the values
|
|
196
|
+
* named in its parameters. */
|
|
197
|
+
export const DEFAULT_TREE_LABELS = Object.freeze({
|
|
198
|
+
tree: 'Files',
|
|
199
|
+
newFile: 'New file',
|
|
200
|
+
newFolder: 'New folder',
|
|
201
|
+
upload: 'Upload files',
|
|
202
|
+
collapseAll: 'Collapse all',
|
|
203
|
+
rename: 'Rename',
|
|
204
|
+
delete: 'Delete',
|
|
205
|
+
keep: 'Keep',
|
|
206
|
+
renameKey: 'F2',
|
|
207
|
+
deleteKey: '⌫',
|
|
208
|
+
empty: 'No files yet.',
|
|
209
|
+
emptyFolder: 'empty',
|
|
210
|
+
loading: 'Loading',
|
|
211
|
+
saving: 'Saving',
|
|
212
|
+
failed: 'That didn’t work. Try again.',
|
|
213
|
+
newFileInput: 'Name of the new file',
|
|
214
|
+
newFolderInput: 'Name of the new folder',
|
|
215
|
+
renameInput: (name) => `New name for ${name}`,
|
|
216
|
+
confirmFile: (name) => `Delete ${name}?`,
|
|
217
|
+
confirmFolder: (name, files) => (files ? `Delete ${name} and the ${files === 1 ? 'file' : `${files} files`} in it?` : `Delete ${name}?`),
|
|
218
|
+
menu: (name) => name,
|
|
219
|
+
...NAME_LABELS,
|
|
220
|
+
});
|
package/styles/tree.css
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/* reifyui/tree: FileTree, a folder tree of files the way an editor's Explorer shows one.
|
|
2
|
+
|
|
3
|
+
The look is the HarnessRouter console's skill-editor tree: 30px rows, 14px icons, 14px more
|
|
4
|
+
indent per level, names in the monospace face, the open file on the surface colour with a
|
|
5
|
+
hairline, row actions at the right of a row on hover, and a quiet uppercase header label with
|
|
6
|
+
small icon buttons beside it.
|
|
7
|
+
|
|
8
|
+
Token contract. Every colour reads --rui-tree-* first, then the package-wide --uic-* set
|
|
9
|
+
(themes/light.css, themes/dark.css or a host's own), then a light fallback:
|
|
10
|
+
--rui-tree-bg the tree pane --uic-surface-2
|
|
11
|
+
--rui-tree-surface the selected row, a name being typed --uic-surface
|
|
12
|
+
--rui-tree-ink / --rui-tree-mute / --rui-tree-faint text, strongest to quietest
|
|
13
|
+
--rui-tree-line hairlines --uic-line
|
|
14
|
+
--rui-tree-brand focus, a drop target, the folder in use --uic-brand
|
|
15
|
+
--rui-tree-hover a row under the pointer --uic-hover-wash
|
|
16
|
+
--rui-tree-fail / --rui-tree-fail-bg a refused name, a delete question
|
|
17
|
+
--rui-tree-mono names --uic-mono
|
|
18
|
+
Sizes a host may tune: --rui-tree-row-h (30px), --rui-tree-indent (14px), --rui-tree-font-size
|
|
19
|
+
(12.5px). The tree fills its box; give the box its size. */
|
|
20
|
+
|
|
21
|
+
:where(.rui-tree, .rui-tree-menu) {
|
|
22
|
+
--_pane: var(--rui-tree-bg, var(--uic-surface-2, #F7F8FA));
|
|
23
|
+
--_surface: var(--rui-tree-surface, var(--uic-surface, #FFFFFF));
|
|
24
|
+
--_ink: var(--rui-tree-ink, var(--uic-ink, #111317));
|
|
25
|
+
--_mute: var(--rui-tree-mute, var(--uic-mute, #5B5D66));
|
|
26
|
+
--_faint: var(--rui-tree-faint, var(--uic-mute-2, #8E9199));
|
|
27
|
+
--_line: var(--rui-tree-line, var(--uic-line, #DDDFE6));
|
|
28
|
+
--_brand: var(--rui-tree-brand, var(--uic-brand, #285AFF));
|
|
29
|
+
--_brand-faint: var(--rui-tree-brand-faint, var(--uic-brand-faint, #EFF3FF));
|
|
30
|
+
--_ring: var(--uic-focus-ring, rgba(40, 90, 255, .14));
|
|
31
|
+
--_hover: var(--rui-tree-hover, var(--uic-hover-wash, rgba(0, 0, 0, .05)));
|
|
32
|
+
--_fail: var(--rui-tree-fail, var(--uic-fail-ink, #B91C1C));
|
|
33
|
+
--_fail-bg: var(--rui-tree-fail-bg, var(--uic-fail-bg, #FEF2F2));
|
|
34
|
+
--_mono: var(--rui-tree-mono, var(--uic-mono, ui-monospace, SFMono-Regular, Menlo, monospace));
|
|
35
|
+
--_skeleton: var(--uic-skeleton, rgba(17, 24, 39, .06));
|
|
36
|
+
--_shadow: var(--uic-shadow-2, 0 8px 24px -12px rgba(17, 24, 39, .12), 0 2px 6px -2px rgba(17, 24, 39, .06));
|
|
37
|
+
--_r-menu: var(--uic-r-card, 12px);
|
|
38
|
+
--_r-row: var(--uic-r-input, 8px);
|
|
39
|
+
--_z: var(--uic-z-overlay, 9000);
|
|
40
|
+
--_fs-md: var(--uic-font-md, 14px);
|
|
41
|
+
--_row: var(--rui-tree-row-h, 30px);
|
|
42
|
+
--_indent: var(--rui-tree-indent, 14px);
|
|
43
|
+
--_fs: var(--rui-tree-font-size, 12.5px);
|
|
44
|
+
/* where a row's text starts past its chevron: the row's own 4px, the 14px chevron, a 4px gap */
|
|
45
|
+
--_lead: 22px;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.rui-tree-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
|
|
49
|
+
|
|
50
|
+
/* ── the pane ────────────────────────────────────────────────────────────────────────────────── */
|
|
51
|
+
.rui-tree {
|
|
52
|
+
display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; box-sizing: border-box;
|
|
53
|
+
background: var(--_pane); color: var(--_mute); font-size: var(--_fs);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* the header: the title and, beside it, the tree's own actions */
|
|
57
|
+
.rui-tree-head { flex: none; display: flex; align-items: center; gap: 2px; min-width: 0; padding: 8px 8px 6px 12px; }
|
|
58
|
+
.rui-tree-title {
|
|
59
|
+
flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
|
60
|
+
font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--_mute);
|
|
61
|
+
}
|
|
62
|
+
.rui-tree-tools { flex: none; display: flex; align-items: center; gap: 2px; }
|
|
63
|
+
.rui-tree-tool {
|
|
64
|
+
display: grid; place-items: center; width: 24px; height: 24px; padding: 0; border: 0; border-radius: 6px;
|
|
65
|
+
background: transparent; color: var(--_faint); cursor: pointer;
|
|
66
|
+
}
|
|
67
|
+
.rui-tree-tool:hover { background: var(--_hover); color: var(--_ink); }
|
|
68
|
+
.rui-tree-tool:focus-visible { outline: 2px solid var(--_brand); outline-offset: -2px; color: var(--_ink); }
|
|
69
|
+
.rui-tree-tool:disabled { opacity: .45; cursor: default; background: transparent; }
|
|
70
|
+
|
|
71
|
+
.rui-tree-body {
|
|
72
|
+
flex: 1; min-height: 0; overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain;
|
|
73
|
+
padding: 0 6px 6px; box-sizing: border-box;
|
|
74
|
+
}
|
|
75
|
+
/* the top of the tree as a drop target */
|
|
76
|
+
.rui-tree-body.is-drop { background: var(--_brand-faint); box-shadow: inset 0 0 0 1px var(--_brand); }
|
|
77
|
+
|
|
78
|
+
.rui-tree-list, .rui-tree-group { list-style: none; margin: 0; padding: 0; }
|
|
79
|
+
.rui-tree-item { position: relative; outline: none; border-radius: var(--_r-row); }
|
|
80
|
+
/* a folder a dragged row would land in: the folder and everything in it, its outline drawn over
|
|
81
|
+
the rows inside it (the selected one paints its own surface) */
|
|
82
|
+
.rui-tree-item.is-drop { background: var(--_brand-faint); }
|
|
83
|
+
.rui-tree-item.is-drop::after {
|
|
84
|
+
content: ""; position: absolute; inset: 0; z-index: 1; border-radius: inherit; pointer-events: none;
|
|
85
|
+
box-shadow: inset 0 0 0 1px var(--_brand);
|
|
86
|
+
}
|
|
87
|
+
.rui-tree-item.is-drop > .rui-tree-row { color: var(--_brand); }
|
|
88
|
+
.rui-tree-row.is-dragging { opacity: .5; }
|
|
89
|
+
|
|
90
|
+
/* ── a row ───────────────────────────────────────────────────────────────────────────────────── */
|
|
91
|
+
.rui-tree-row {
|
|
92
|
+
position: relative; display: flex; align-items: center; gap: 4px; min-width: 0; min-height: var(--_row);
|
|
93
|
+
padding: 0 2px 0 calc(4px + var(--rui-tree-d, 0) * var(--_indent)); box-sizing: border-box;
|
|
94
|
+
border-radius: var(--_r-row); line-height: 1.4; color: var(--_mute); cursor: default; user-select: none;
|
|
95
|
+
-webkit-user-select: none;
|
|
96
|
+
}
|
|
97
|
+
.rui-tree-row:hover { background: var(--_hover); }
|
|
98
|
+
.rui-tree-row.is-menu { background: var(--_hover); }
|
|
99
|
+
.rui-tree-row.is-selected { background: var(--_surface); box-shadow: inset 0 0 0 1px var(--_line); color: var(--_ink); }
|
|
100
|
+
.rui-tree-row.is-target, .rui-tree-row.is-target .rui-tree-icon { color: var(--_brand); }
|
|
101
|
+
.rui-tree-item:focus-visible > .rui-tree-row { outline: 2px solid var(--_brand); outline-offset: -2px; }
|
|
102
|
+
|
|
103
|
+
.rui-tree-twisty { flex: none; width: 14px; height: 14px; display: grid; place-items: center; color: var(--_faint); }
|
|
104
|
+
.rui-tree-twisty svg { transition: transform .15s; }
|
|
105
|
+
.rui-tree-twisty.is-open svg { transform: rotate(90deg); }
|
|
106
|
+
.rui-tree-icon { flex: none; display: grid; place-items: center; color: var(--_faint); }
|
|
107
|
+
|
|
108
|
+
/* a long name is cut at the end, its extension kept whole; the full path is the row's title */
|
|
109
|
+
.rui-tree-name { flex: 0 1 auto; min-width: 0; display: flex; overflow: hidden; white-space: nowrap; font-family: var(--_mono); font-size: var(--_fs); }
|
|
110
|
+
.rui-tree-base { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
111
|
+
.rui-tree-ext { flex: none; }
|
|
112
|
+
.rui-tree-note, .rui-tree-tag { flex: none; margin-left: 6px; font-size: 11px; color: var(--_faint); white-space: nowrap; }
|
|
113
|
+
|
|
114
|
+
/* the row's actions: at its right, shown on hover, on the selected row and on the focused row.
|
|
115
|
+
Out of the layout until then, so a narrow tree gives an idle row its whole name. */
|
|
116
|
+
.rui-tree-acts { flex: none; display: none; margin-left: auto; padding-left: 4px; }
|
|
117
|
+
.rui-tree-row:hover .rui-tree-acts, .rui-tree-row.is-selected .rui-tree-acts,
|
|
118
|
+
.rui-tree-item:focus-visible > .rui-tree-row .rui-tree-acts { display: flex; }
|
|
119
|
+
.rui-tree-ib {
|
|
120
|
+
display: grid; place-items: center; width: 26px; height: 26px; padding: 0; border: 0; border-radius: 6px;
|
|
121
|
+
background: transparent; color: var(--_faint); cursor: pointer;
|
|
122
|
+
}
|
|
123
|
+
.rui-tree-ib:hover { background: var(--_hover); color: var(--_ink); }
|
|
124
|
+
.rui-tree-ib:disabled { opacity: .45; cursor: default; }
|
|
125
|
+
|
|
126
|
+
/* a row with a save of its own in flight */
|
|
127
|
+
.rui-tree-row.is-pending .rui-tree-name { opacity: .6; }
|
|
128
|
+
.rui-tree-spin {
|
|
129
|
+
flex: none; width: 12px; height: 12px; margin: 0 8px 0 auto; box-sizing: border-box; border-radius: 50%;
|
|
130
|
+
border: 1.5px solid var(--_line); border-top-color: var(--_brand); animation: rui-tree-spin .8s linear infinite;
|
|
131
|
+
}
|
|
132
|
+
@keyframes rui-tree-spin { to { transform: rotate(360deg); } }
|
|
133
|
+
|
|
134
|
+
/* ── a name being typed (New file, New folder, Rename) ───────────────────────────────────────── */
|
|
135
|
+
.rui-tree-input {
|
|
136
|
+
flex: 1; min-width: 0; width: auto; margin: 2px 4px 2px 0; padding: 3px 7px; box-sizing: border-box;
|
|
137
|
+
border: 1px solid var(--_brand); border-radius: 6px; background: var(--_surface); color: var(--_ink);
|
|
138
|
+
font-family: var(--_mono); font-size: var(--_fs); line-height: 1.4; outline: none; box-shadow: 0 0 0 3px var(--_ring);
|
|
139
|
+
user-select: text; -webkit-user-select: text;
|
|
140
|
+
}
|
|
141
|
+
.rui-tree-input[aria-invalid="true"] { border-color: var(--_fail); box-shadow: none; }
|
|
142
|
+
.rui-tree-input[readonly] { opacity: .7; }
|
|
143
|
+
|
|
144
|
+
/* ── messages and the delete question, under their row ───────────────────────────────────────── */
|
|
145
|
+
.rui-tree-msg {
|
|
146
|
+
margin: 2px 6px 6px calc(var(--_lead) + var(--rui-tree-d, 0) * var(--_indent));
|
|
147
|
+
font-size: 12px; line-height: 1.4; color: var(--_fail); overflow-wrap: anywhere;
|
|
148
|
+
}
|
|
149
|
+
.rui-tree-msg.is-tree { margin: 0 12px 6px; }
|
|
150
|
+
.rui-tree-ask {
|
|
151
|
+
display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px;
|
|
152
|
+
margin: 2px 0 6px calc(var(--_lead) + var(--rui-tree-d, 0) * var(--_indent)); padding: 8px 10px;
|
|
153
|
+
border-radius: var(--_r-row); background: var(--_fail-bg); font-size: 12.5px; line-height: 1.4; color: var(--_ink);
|
|
154
|
+
overflow-wrap: anywhere;
|
|
155
|
+
}
|
|
156
|
+
.rui-tree-ask-q { min-width: 0; }
|
|
157
|
+
.rui-tree-ask-b { display: flex; gap: 14px; margin-left: auto; }
|
|
158
|
+
.rui-tree-link { padding: 2px 0; border: 0; background: none; font: inherit; font-weight: 600; color: var(--_brand); cursor: pointer; }
|
|
159
|
+
.rui-tree-link.is-danger { color: var(--_fail); }
|
|
160
|
+
.rui-tree-link:focus-visible { outline: 2px solid var(--_brand); outline-offset: 2px; border-radius: 4px; }
|
|
161
|
+
.rui-tree-link:disabled { opacity: .5; cursor: default; }
|
|
162
|
+
|
|
163
|
+
.rui-tree-empty { margin: 8px 6px; font-size: 13px; line-height: 1.4; color: var(--_faint); }
|
|
164
|
+
.rui-tree-skel { display: block; padding-top: 2px; }
|
|
165
|
+
.rui-tree-skel-line { display: block; height: 11px; margin: 9px 0 9px var(--_lead); border-radius: 4px; background: var(--_skeleton); animation: rui-tree-pulse 1.5s ease-in-out infinite; }
|
|
166
|
+
@keyframes rui-tree-pulse { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }
|
|
167
|
+
|
|
168
|
+
/* ── the row's menu (in document.body; its colours come with it from the tree) ───────────────── */
|
|
169
|
+
.rui-tree-menu {
|
|
170
|
+
position: fixed; z-index: var(--_z); box-sizing: border-box; min-width: 220px; max-width: calc(100vw - 16px);
|
|
171
|
+
padding: 6px; background: var(--_surface); border: 1px solid var(--_line); border-radius: var(--_r-menu);
|
|
172
|
+
box-shadow: var(--_shadow); color: var(--_ink);
|
|
173
|
+
}
|
|
174
|
+
.rui-tree-menu-item {
|
|
175
|
+
display: flex; align-items: center; gap: 10px; width: 100%; min-height: 34px; padding: 6px 10px; box-sizing: border-box;
|
|
176
|
+
border: 0; border-radius: var(--_r-row); background: transparent; color: inherit;
|
|
177
|
+
font: inherit; font-size: var(--_fs-md); font-weight: 500; line-height: 1.3; text-align: left; cursor: pointer;
|
|
178
|
+
}
|
|
179
|
+
/* opened by the pointer, the first item takes focus without looking chosen; from the keyboard it shows */
|
|
180
|
+
.rui-tree-menu-item:focus { outline: none; }
|
|
181
|
+
.rui-tree-menu-item:hover, .rui-tree-menu-item:focus-visible { background: var(--_hover); }
|
|
182
|
+
.rui-tree-menu-item:disabled { opacity: .5; cursor: default; background: transparent; }
|
|
183
|
+
.rui-tree-menu-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
184
|
+
.rui-tree-menu-hint { flex: none; padding-left: 10px; font: inherit; font-size: 12px; font-weight: 400; color: var(--_faint); }
|
|
185
|
+
.rui-tree-menu-sep { height: 1px; margin: 4px 6px; background: var(--_line); }
|
|
186
|
+
|
|
187
|
+
/* ── touch: actions always shown, rows and buttons a finger wide, a field that does not zoom ─── */
|
|
188
|
+
@media (hover: none) { .rui-tree-acts { display: flex; } }
|
|
189
|
+
@media (pointer: coarse) {
|
|
190
|
+
.rui-tree-row { min-height: 38px; }
|
|
191
|
+
.rui-tree-tool { width: 34px; height: 34px; }
|
|
192
|
+
.rui-tree-ib { width: 34px; height: 34px; }
|
|
193
|
+
.rui-tree-input { font-size: 16px; }
|
|
194
|
+
.rui-tree-menu-item { min-height: 40px; }
|
|
195
|
+
}
|
|
196
|
+
@media (prefers-reduced-motion: reduce) {
|
|
197
|
+
.rui-tree-twisty svg { transition: none; }
|
|
198
|
+
.rui-tree-spin, .rui-tree-skel-line { animation: none; }
|
|
199
|
+
}
|