@x-tech-laboratories/x-libs 0.4.0 → 0.6.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 +63 -13
- package/dist/dates.d.ts +9 -0
- package/dist/dates.js +60 -0
- package/dist/durations.d.ts +8 -1
- package/dist/durations.js +9 -5
- package/dist/electron/bridge.d.ts +30 -0
- package/dist/electron/bridge.js +32 -0
- package/dist/electron/window.d.ts +85 -0
- package/dist/electron/window.js +85 -0
- package/dist/web/menus.d.ts +4 -0
- package/dist/web/menus.js +28 -1
- package/dist/web/{preact → react}/badges.d.ts +9 -9
- package/dist/web/{preact → react}/badges.js +6 -6
- package/dist/web/{preact → react}/controls.d.ts +7 -7
- package/dist/web/{preact → react}/controls.js +7 -6
- package/dist/web/{preact → react}/dialog.d.ts +10 -10
- package/dist/web/{preact → react}/dialog.js +14 -9
- package/dist/web/{preact → react}/layout.d.ts +33 -33
- package/dist/web/react/layout.js +71 -0
- package/dist/web/react/menus.d.ts +31 -0
- package/dist/web/react/menus.js +159 -0
- package/dist/web/react/shell.d.ts +37 -0
- package/dist/web/react/shell.js +81 -0
- package/dist/web/react/shortcuts.d.ts +23 -0
- package/dist/web/react/shortcuts.js +95 -0
- package/dist/web/{preact → react}/split.d.ts +4 -4
- package/dist/web/{preact → react}/split.js +9 -10
- package/dist/web/{preact → react}/states.d.ts +7 -7
- package/dist/web/{preact → react}/states.js +6 -5
- package/dist/web/react/table.d.ts +46 -0
- package/dist/web/react/table.js +210 -0
- package/dist/web/{preact → react}/time.d.ts +1 -1
- package/dist/web/{preact → react}/time.js +1 -1
- package/dist/web/{preact → react}/toasts.d.ts +3 -1
- package/dist/web/react/toasts.js +41 -0
- package/dist/web/react/tree.d.ts +23 -0
- package/dist/web/react/tree.js +168 -0
- package/dist/web/tables.d.ts +18 -1
- package/dist/web/tables.js +70 -8
- package/dist/web/theme.d.ts +6 -1
- package/dist/web/theme.js +31 -21
- package/dist/web/tree.d.ts +35 -0
- package/dist/web/tree.js +107 -0
- package/package.json +15 -4
- package/dist/web/preact/layout.js +0 -71
- package/dist/web/preact/menus.d.ts +0 -19
- package/dist/web/preact/menus.js +0 -70
- package/dist/web/preact/shell.d.ts +0 -27
- package/dist/web/preact/shell.js +0 -63
- package/dist/web/preact/table.d.ts +0 -23
- package/dist/web/preact/table.js +0 -20
- package/dist/web/preact/toasts.js +0 -34
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// A tree of rows: fold and unfold, walk it with the arrow keys, and reorder it by dragging or with Alt and the arrow keys.
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
import { by_position, can_drop as is_outside_subtree, key_drop, move_for_drop, nest, placement_at, } from "../tree.js";
|
|
5
|
+
const KEY_MOVES = { ArrowUp: "up", ArrowDown: "down", ArrowRight: "indent", ArrowLeft: "outdent" };
|
|
6
|
+
// The rows a person can see, top to bottom, skipping those inside folded rows.
|
|
7
|
+
function visible_nodes(nodes, collapsed) {
|
|
8
|
+
return nodes.flatMap((node) => [node, ...(collapsed.has(node.item.id) ? [] : visible_nodes(node.children, collapsed))]);
|
|
9
|
+
}
|
|
10
|
+
export function Tree({ items, label, render_item, render_extra, name_of, collapsed, on_collapsed_change, selected_id = null, on_select, on_move, can_drop, allow_inside = true, new_item = null, on_context_menu, }) {
|
|
11
|
+
const [focused_id, set_focused_id] = useState(null);
|
|
12
|
+
const [dragged_id, set_dragged_id] = useState(null);
|
|
13
|
+
const [drop_target, set_drop_target] = useState(null);
|
|
14
|
+
const row_elements = useRef(new Map());
|
|
15
|
+
const moves_focus = useRef(false);
|
|
16
|
+
const roots = nest([...items].sort(by_position), items);
|
|
17
|
+
const shown = visible_nodes(roots, collapsed);
|
|
18
|
+
const tab_stop = [focused_id, selected_id].find((id) => id !== null && shown.some((node) => node.item.id === id)) ?? shown[0]?.item.id;
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!moves_focus.current || focused_id === null)
|
|
21
|
+
return;
|
|
22
|
+
moves_focus.current = false;
|
|
23
|
+
row_elements.current.get(focused_id)?.focus();
|
|
24
|
+
}, [focused_id, items]);
|
|
25
|
+
// ── Folding ────────────────────────────────────────────────────────────────
|
|
26
|
+
function set_collapsed(id, is_collapsed) {
|
|
27
|
+
if (collapsed.has(id) === is_collapsed)
|
|
28
|
+
return;
|
|
29
|
+
const next = new Set(collapsed);
|
|
30
|
+
if (is_collapsed)
|
|
31
|
+
next.add(id);
|
|
32
|
+
else
|
|
33
|
+
next.delete(id);
|
|
34
|
+
on_collapsed_change(next);
|
|
35
|
+
}
|
|
36
|
+
// ── Moving ─────────────────────────────────────────────────────────────────
|
|
37
|
+
function is_allowed(dragged, target, placement) {
|
|
38
|
+
if (placement === "inside" && !allow_inside)
|
|
39
|
+
return false;
|
|
40
|
+
return is_outside_subtree(dragged.id, target.id, items) && (can_drop?.(dragged, target, placement) ?? true);
|
|
41
|
+
}
|
|
42
|
+
// A row dropped into a folded one unfolds it, so the moved row stays in sight.
|
|
43
|
+
function move(item, target, placement) {
|
|
44
|
+
if (placement === "inside")
|
|
45
|
+
set_collapsed(target.id, false);
|
|
46
|
+
on_move(item, move_for_drop(item, target, placement, items));
|
|
47
|
+
}
|
|
48
|
+
function placement_over(event, target) {
|
|
49
|
+
const dragged = items.find((item) => item.id === dragged_id);
|
|
50
|
+
if (!dragged)
|
|
51
|
+
return null;
|
|
52
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
53
|
+
const placement = placement_at(box.height > 0 ? (event.clientY - box.top) / box.height : 0.5, allow_inside);
|
|
54
|
+
return is_allowed(dragged, target, placement) ? placement : null;
|
|
55
|
+
}
|
|
56
|
+
function drag_over(event, target) {
|
|
57
|
+
const placement = placement_over(event, target);
|
|
58
|
+
if (!placement)
|
|
59
|
+
return;
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
if (event.dataTransfer)
|
|
62
|
+
event.dataTransfer.dropEffect = "move";
|
|
63
|
+
if (drop_target?.id !== target.id || drop_target.placement !== placement)
|
|
64
|
+
set_drop_target({ id: target.id, placement });
|
|
65
|
+
}
|
|
66
|
+
function drop(event, target) {
|
|
67
|
+
const placement = placement_over(event, target);
|
|
68
|
+
const dragged = items.find((item) => item.id === dragged_id);
|
|
69
|
+
end_drag();
|
|
70
|
+
if (!placement || !dragged)
|
|
71
|
+
return;
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
move(dragged, target, placement);
|
|
74
|
+
}
|
|
75
|
+
function end_drag() {
|
|
76
|
+
set_dragged_id(null);
|
|
77
|
+
set_drop_target(null);
|
|
78
|
+
}
|
|
79
|
+
// ── Keyboard ───────────────────────────────────────────────────────────────
|
|
80
|
+
function focus_row(id) {
|
|
81
|
+
if (id === undefined)
|
|
82
|
+
return;
|
|
83
|
+
moves_focus.current = true;
|
|
84
|
+
set_focused_id(id);
|
|
85
|
+
}
|
|
86
|
+
function key_down(event) {
|
|
87
|
+
const row = event.target.closest("[role='treeitem']");
|
|
88
|
+
if (row !== event.target)
|
|
89
|
+
return;
|
|
90
|
+
const index = shown.findIndex((node) => node.item.id === row.dataset.id);
|
|
91
|
+
const node = shown[index];
|
|
92
|
+
if (!node)
|
|
93
|
+
return;
|
|
94
|
+
const { item } = node;
|
|
95
|
+
const is_open = node.children.length > 0 && !collapsed.has(item.id);
|
|
96
|
+
if (event.altKey && on_move && KEY_MOVES[event.key]) {
|
|
97
|
+
const key_target = key_drop(item, KEY_MOVES[event.key], items);
|
|
98
|
+
if (key_target && is_allowed(item, key_target.target, key_target.placement)) {
|
|
99
|
+
move(item, key_target.target, key_target.placement);
|
|
100
|
+
focus_row(item.id);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (event.altKey || event.ctrlKey || event.metaKey) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
else if (event.key === "ArrowDown")
|
|
107
|
+
focus_row(shown[index + 1]?.item.id);
|
|
108
|
+
else if (event.key === "ArrowUp")
|
|
109
|
+
focus_row(shown[index - 1]?.item.id);
|
|
110
|
+
else if (event.key === "Home")
|
|
111
|
+
focus_row(shown[0]?.item.id);
|
|
112
|
+
else if (event.key === "End")
|
|
113
|
+
focus_row(shown.at(-1)?.item.id);
|
|
114
|
+
else if (event.key === "ArrowRight") {
|
|
115
|
+
if (is_open)
|
|
116
|
+
focus_row(node.children[0].item.id);
|
|
117
|
+
else if (node.children.length > 0)
|
|
118
|
+
set_collapsed(item.id, false);
|
|
119
|
+
else
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
else if (event.key === "ArrowLeft") {
|
|
123
|
+
if (is_open)
|
|
124
|
+
set_collapsed(item.id, true);
|
|
125
|
+
else if (item.parent_id !== null && shown.some((each) => each.item.id === item.parent_id))
|
|
126
|
+
focus_row(item.parent_id);
|
|
127
|
+
else
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else if ((event.key === "Enter" || event.key === " ") && on_select)
|
|
131
|
+
on_select(item);
|
|
132
|
+
else
|
|
133
|
+
return;
|
|
134
|
+
event.preventDefault();
|
|
135
|
+
}
|
|
136
|
+
// ── Rows ───────────────────────────────────────────────────────────────────
|
|
137
|
+
function new_item_row(parent_id) {
|
|
138
|
+
if (!new_item || new_item.parent_id !== parent_id)
|
|
139
|
+
return null;
|
|
140
|
+
return _jsx("li", { role: "none", className: "tree-new", children: new_item.form });
|
|
141
|
+
}
|
|
142
|
+
function branch(node, depth) {
|
|
143
|
+
const { item, children } = node;
|
|
144
|
+
const has_children = children.length > 0;
|
|
145
|
+
const is_collapsed = collapsed.has(item.id);
|
|
146
|
+
const name = name_of?.(item) ?? "row";
|
|
147
|
+
const new_child = new_item_row(item.id);
|
|
148
|
+
const row_class = [
|
|
149
|
+
"tree-row",
|
|
150
|
+
dragged_id === item.id && "dragging",
|
|
151
|
+
drop_target?.id === item.id && `drop-${drop_target.placement}`,
|
|
152
|
+
].filter(Boolean).join(" ");
|
|
153
|
+
return (_jsxs("li", { ref: (element) => { if (element)
|
|
154
|
+
row_elements.current.set(item.id, element);
|
|
155
|
+
else
|
|
156
|
+
row_elements.current.delete(item.id); }, role: "treeitem", className: "tree-item", "data-id": item.id, "aria-level": depth + 1, "aria-expanded": has_children ? !is_collapsed : undefined, "aria-selected": on_select ? selected_id === item.id : undefined, tabIndex: tab_stop === item.id ? 0 : -1, onFocus: (event) => { if (event.target === event.currentTarget)
|
|
157
|
+
set_focused_id(item.id); }, children: [_jsxs("div", { className: row_class, draggable: on_move ? true : undefined, onDragStart: (event) => {
|
|
158
|
+
event.dataTransfer?.setData("text/plain", item.id);
|
|
159
|
+
if (event.dataTransfer)
|
|
160
|
+
event.dataTransfer.effectAllowed = "move";
|
|
161
|
+
set_dragged_id(item.id);
|
|
162
|
+
}, onDragEnd: end_drag, onDragOver: (event) => drag_over(event, item), onDragLeave: () => { if (drop_target?.id === item.id)
|
|
163
|
+
set_drop_target(null); }, onDrop: (event) => drop(event, item), onClick: () => { focus_row(item.id); on_select?.(item); }, onContextMenu: on_context_menu && ((event) => on_context_menu(event, item)), children: [has_children
|
|
164
|
+
? (_jsx("button", { type: "button", className: "tree-toggle", tabIndex: -1, "aria-label": is_collapsed ? `Expand ${name}` : `Collapse ${name}`, onClick: (event) => { event.stopPropagation(); set_collapsed(item.id, !is_collapsed); }, children: _jsx("span", { "aria-hidden": "true", children: is_collapsed ? "▸" : "▾" }) }))
|
|
165
|
+
: _jsx("span", { className: "tree-toggle", "aria-hidden": "true" }), _jsx("span", { className: "tree-label", children: render_item(item) }), render_extra && _jsx("span", { className: "tree-extra", children: render_extra(item) })] }), ((has_children && !is_collapsed) || new_child) && (_jsxs("ul", { role: "group", className: "tree-children", children: [!is_collapsed && children.map((child) => branch(child, depth + 1)), new_child] }))] }, item.id));
|
|
166
|
+
}
|
|
167
|
+
return (_jsxs("ul", { role: "tree", className: "tree", "aria-label": label, onKeyDown: key_down, children: [roots.map((node) => branch(node, 0)), new_item_row(null)] }));
|
|
168
|
+
}
|
package/dist/web/tables.d.ts
CHANGED
|
@@ -4,7 +4,24 @@ export interface Sorting {
|
|
|
4
4
|
}
|
|
5
5
|
export interface SortableColumn<Row> {
|
|
6
6
|
label: string;
|
|
7
|
+
id?: string;
|
|
7
8
|
sort?: (row: Row) => string | number | null;
|
|
8
9
|
}
|
|
9
|
-
export
|
|
10
|
+
export type SortValue = string | number | null | undefined;
|
|
11
|
+
export declare function column_id(column: {
|
|
12
|
+
id?: string;
|
|
13
|
+
label: string;
|
|
14
|
+
}): string;
|
|
15
|
+
export declare function sorting_list(sorting: Sorting | readonly Sorting[] | undefined): Sorting[];
|
|
16
|
+
export declare function compare_values(first: SortValue, second: SortValue, descending?: boolean): number;
|
|
17
|
+
export declare function sorted_rows<Row>(columns: SortableColumn<Row>[], rows: Row[], sorting: Sorting | readonly Sorting[] | undefined): Row[];
|
|
18
|
+
export interface TreeRow<Row> {
|
|
19
|
+
row: Row;
|
|
20
|
+
children: TreeRow<Row>[];
|
|
21
|
+
}
|
|
22
|
+
export declare function sorted_tree<Row>(columns: SortableColumn<Row>[], rows: Row[], sorting: Sorting | readonly Sorting[] | undefined, get_children: (row: Row) => Row[] | undefined): TreeRow<Row>[];
|
|
23
|
+
export declare function sort_after_click(sorting: readonly Sorting[], key: string, adding: boolean): Sorting[];
|
|
10
24
|
export declare function sorting_from(query: URLSearchParams, fallback: Sorting): Sorting;
|
|
25
|
+
export declare function ordered_columns(saved_order: readonly string[], all_columns: readonly string[]): string[];
|
|
26
|
+
export declare function move_column(order: readonly string[], column: string, target: string, after: boolean): string[];
|
|
27
|
+
export declare function clamp_width(width: number, min?: number, max?: number): number;
|
package/dist/web/tables.js
CHANGED
|
@@ -1,18 +1,80 @@
|
|
|
1
|
-
// The order a sorted table shows its rows in,
|
|
2
|
-
|
|
1
|
+
// The order a sorted table shows its rows in, the sorting a click or a view's query asks for, and the order of its columns.
|
|
2
|
+
export function column_id(column) {
|
|
3
|
+
return column.id ?? column.label;
|
|
4
|
+
}
|
|
5
|
+
// One sorting or several as a list, the first deciding first.
|
|
6
|
+
export function sorting_list(sorting) {
|
|
7
|
+
if (!sorting)
|
|
8
|
+
return [];
|
|
9
|
+
return Array.isArray(sorting) ? [...sorting] : [sorting];
|
|
10
|
+
}
|
|
11
|
+
const is_empty = (value) => value === null || value === undefined || value === "";
|
|
12
|
+
// Numbers by size and text by its words and numbers, with an empty value last whichever way the column sorts.
|
|
13
|
+
export function compare_values(first, second, descending = false) {
|
|
14
|
+
if (is_empty(first) || is_empty(second))
|
|
15
|
+
return Number(is_empty(first)) - Number(is_empty(second));
|
|
16
|
+
const order = typeof first === "number" && typeof second === "number"
|
|
17
|
+
? first - second
|
|
18
|
+
: String(first).localeCompare(String(second), undefined, { numeric: true, sensitivity: "base" });
|
|
19
|
+
return descending ? -order : order;
|
|
20
|
+
}
|
|
21
|
+
// The rows in the order the sortings ask for, the later ones breaking ties, and rows missing a value last.
|
|
3
22
|
export function sorted_rows(columns, rows, sorting) {
|
|
4
|
-
const
|
|
5
|
-
|
|
23
|
+
const keys = sorting_list(sorting).flatMap((key) => {
|
|
24
|
+
const column = columns.find((candidate) => column_id(candidate) === key.key && candidate.sort);
|
|
25
|
+
return column?.sort ? [{ value: column.sort, descending: key.descending }] : [];
|
|
26
|
+
});
|
|
27
|
+
if (keys.length === 0)
|
|
6
28
|
return rows;
|
|
7
|
-
const value = column.sort;
|
|
8
29
|
return [...rows].sort((left, right) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
30
|
+
for (const key of keys) {
|
|
31
|
+
const order = compare_values(key.value(left), key.value(right), key.descending);
|
|
32
|
+
if (order !== 0)
|
|
33
|
+
return order;
|
|
34
|
+
}
|
|
35
|
+
return 0;
|
|
12
36
|
});
|
|
13
37
|
}
|
|
38
|
+
// Sorts each level of a tree on its own, so children stay under their parents.
|
|
39
|
+
export function sorted_tree(columns, rows, sorting, get_children) {
|
|
40
|
+
return sorted_rows(columns, rows, sorting).map((row) => ({ row, children: sorted_tree(columns, get_children(row) ?? [], sorting, get_children) }));
|
|
41
|
+
}
|
|
42
|
+
// A plain click sorts by that column alone; a Shift+click adds it to the sort or flips it; a third click takes it out.
|
|
43
|
+
export function sort_after_click(sorting, key, adding) {
|
|
44
|
+
const current = sorting.find((each) => each.key === key);
|
|
45
|
+
const next = !current ? { key, descending: false } : current.descending ? null : { key, descending: true };
|
|
46
|
+
if (!adding) {
|
|
47
|
+
if (current && sorting.length === 1)
|
|
48
|
+
return next ? [next] : [];
|
|
49
|
+
return [{ key, descending: false }];
|
|
50
|
+
}
|
|
51
|
+
if (!current)
|
|
52
|
+
return [...sorting, next];
|
|
53
|
+
return next ? sorting.map((each) => (each.key === key ? next : each)) : sorting.filter((each) => each.key !== key);
|
|
54
|
+
}
|
|
14
55
|
// The sorting a view's query asks for, such as ?sort=Name&dir=desc.
|
|
15
56
|
export function sorting_from(query, fallback) {
|
|
16
57
|
const key = query.get("sort");
|
|
17
58
|
return key ? { key, descending: query.get("dir") === "desc" } : fallback;
|
|
18
59
|
}
|
|
60
|
+
// ── Columns ──────────────────────────────────────────────────────────────────
|
|
61
|
+
// Every column in its saved order, leaving out ones gone since and placing a new one just after the column before it.
|
|
62
|
+
export function ordered_columns(saved_order, all_columns) {
|
|
63
|
+
const ordered = saved_order.filter((column, index) => all_columns.includes(column) && saved_order.indexOf(column) === index);
|
|
64
|
+
all_columns.forEach((column, index) => {
|
|
65
|
+
if (ordered.includes(column))
|
|
66
|
+
return;
|
|
67
|
+
ordered.splice(index === 0 ? 0 : ordered.indexOf(all_columns[index - 1]) + 1, 0, column);
|
|
68
|
+
});
|
|
69
|
+
return ordered;
|
|
70
|
+
}
|
|
71
|
+
// Moves a column to just before or after another one.
|
|
72
|
+
export function move_column(order, column, target, after) {
|
|
73
|
+
const rest = order.filter((each) => each !== column);
|
|
74
|
+
const index = rest.indexOf(target) + (after ? 1 : 0);
|
|
75
|
+
return [...rest.slice(0, index), column, ...rest.slice(index)];
|
|
76
|
+
}
|
|
77
|
+
// A width held between the column's narrowest and widest.
|
|
78
|
+
export function clamp_width(width, min = 48, max = 960) {
|
|
79
|
+
return Math.round(Math.min(max, Math.max(min, width)));
|
|
80
|
+
}
|
package/dist/web/theme.d.ts
CHANGED
|
@@ -5,5 +5,10 @@ export interface ThemeChoice {
|
|
|
5
5
|
on_change(listener: (theme: Theme) => void): () => void;
|
|
6
6
|
apply(theme?: Theme): void;
|
|
7
7
|
}
|
|
8
|
-
export
|
|
8
|
+
export interface ThemeStorage {
|
|
9
|
+
read(): Theme | undefined;
|
|
10
|
+
write(theme: Theme): void;
|
|
11
|
+
}
|
|
12
|
+
export declare function local_theme_storage(storage_key: string): ThemeStorage;
|
|
13
|
+
export declare function theme_choice(where: string | ThemeStorage): ThemeChoice;
|
|
9
14
|
export declare function next_theme(theme: Theme): Theme;
|
package/dist/web/theme.js
CHANGED
|
@@ -1,16 +1,34 @@
|
|
|
1
|
-
// The light, dark or system theme, kept in this browser and set as data-theme on the page.
|
|
2
|
-
// The choice kept under the storage key, such as "x-nix-theme".
|
|
3
|
-
export function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
// The light, dark or system theme, kept in this browser or the app's own settings, and set as data-theme on the page.
|
|
2
|
+
// The choice kept in this browser under the storage key, such as "x-nix-theme".
|
|
3
|
+
export function local_theme_storage(storage_key) {
|
|
4
|
+
return {
|
|
5
|
+
read: () => {
|
|
6
|
+
try {
|
|
7
|
+
const saved = localStorage.getItem(storage_key);
|
|
8
|
+
return saved === "light" || saved === "dark" ? saved : undefined;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
write: (theme) => {
|
|
15
|
+
try {
|
|
16
|
+
if (theme === "system")
|
|
17
|
+
localStorage.removeItem(storage_key);
|
|
18
|
+
else
|
|
19
|
+
localStorage.setItem(storage_key, theme);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// Without storage the choice lasts until the page is closed.
|
|
23
|
+
}
|
|
24
|
+
},
|
|
13
25
|
};
|
|
26
|
+
}
|
|
27
|
+
// The choice kept under a storage key in this browser, or in the storage the app hands over.
|
|
28
|
+
export function theme_choice(where) {
|
|
29
|
+
const storage = typeof where === "string" ? local_theme_storage(where) : where;
|
|
30
|
+
const listeners = new Set();
|
|
31
|
+
const get = () => storage.read() ?? "system";
|
|
14
32
|
const apply = (theme = get()) => {
|
|
15
33
|
if (theme === "system")
|
|
16
34
|
delete document.documentElement.dataset.theme;
|
|
@@ -20,15 +38,7 @@ export function theme_choice(storage_key) {
|
|
|
20
38
|
listener(theme);
|
|
21
39
|
};
|
|
22
40
|
const set = (theme) => {
|
|
23
|
-
|
|
24
|
-
if (theme === "system")
|
|
25
|
-
localStorage.removeItem(storage_key);
|
|
26
|
-
else
|
|
27
|
-
localStorage.setItem(storage_key, theme);
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
// Without storage the choice lasts until the page is closed.
|
|
31
|
-
}
|
|
41
|
+
storage.write(theme);
|
|
32
42
|
apply(theme);
|
|
33
43
|
};
|
|
34
44
|
return { get, set, apply, on_change: (listener) => {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface TreeItem {
|
|
2
|
+
id: string;
|
|
3
|
+
parent_id: string | null;
|
|
4
|
+
position: number;
|
|
5
|
+
}
|
|
6
|
+
export interface TreeNode<Item> {
|
|
7
|
+
item: Item;
|
|
8
|
+
children: TreeNode<Item>[];
|
|
9
|
+
}
|
|
10
|
+
export type SidePlacement = "before" | "after";
|
|
11
|
+
export type DropPlacement = SidePlacement | "inside";
|
|
12
|
+
export interface TreeMove {
|
|
13
|
+
parent_id: string | null;
|
|
14
|
+
position: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function by_position<Item extends {
|
|
17
|
+
position: number;
|
|
18
|
+
}>(first: Item, second: Item): number;
|
|
19
|
+
export declare function children_of<Item extends TreeItem>(parent_id: string | null, items: Item[]): Item[];
|
|
20
|
+
export declare function subtree_ids(item_id: string, items: TreeItem[]): Set<string>;
|
|
21
|
+
export declare function nest<Item extends TreeItem>(shown: Item[], all?: Item[]): TreeNode<Item>[];
|
|
22
|
+
export declare function placement_at(fraction_down: number, allow_inside: boolean): DropPlacement;
|
|
23
|
+
export declare function can_drop(dragged_id: string, target_id: string, items: TreeItem[]): boolean;
|
|
24
|
+
export declare function position_for_drop<Item extends {
|
|
25
|
+
id: string;
|
|
26
|
+
position: number;
|
|
27
|
+
}>(dragged: Item, target: Item, placement: SidePlacement, items: Item[]): number;
|
|
28
|
+
export declare function move_for_drop<Item extends TreeItem>(dragged: Item, target: Item, placement: DropPlacement, items: Item[]): TreeMove;
|
|
29
|
+
export declare function moves_for_drop<Item extends TreeItem>(dragged: Item[], target: Item, placement: DropPlacement, items: Item[]): TreeMove[];
|
|
30
|
+
export type KeyMove = "up" | "down" | "indent" | "outdent";
|
|
31
|
+
export declare function key_drop<Item extends TreeItem>(item: Item, direction: KeyMove, items: Item[]): {
|
|
32
|
+
target: Item;
|
|
33
|
+
placement: DropPlacement;
|
|
34
|
+
} | null;
|
|
35
|
+
export declare function move_for_key<Item extends TreeItem>(item: Item, direction: KeyMove, items: Item[]): TreeMove | null;
|
package/dist/web/tree.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Trees of rows that name their parent and their place among siblings, and where a dragged or keyed row lands.
|
|
2
|
+
// ── Shape ────────────────────────────────────────────────────────────────────
|
|
3
|
+
export function by_position(first, second) {
|
|
4
|
+
return first.position - second.position;
|
|
5
|
+
}
|
|
6
|
+
export function children_of(parent_id, items) {
|
|
7
|
+
return items.filter((item) => item.parent_id === parent_id);
|
|
8
|
+
}
|
|
9
|
+
// The item itself plus everything below it, at any depth.
|
|
10
|
+
export function subtree_ids(item_id, items) {
|
|
11
|
+
const ids = new Set([item_id]);
|
|
12
|
+
for (const id of ids) {
|
|
13
|
+
for (const child of children_of(id, items))
|
|
14
|
+
ids.add(child.id);
|
|
15
|
+
}
|
|
16
|
+
return ids;
|
|
17
|
+
}
|
|
18
|
+
// Each shown item sits under its nearest ancestor that is also shown, or at the top when none is, in the order given.
|
|
19
|
+
export function nest(shown, all = shown) {
|
|
20
|
+
const item_by_id = new Map(all.map((item) => [item.id, item]));
|
|
21
|
+
const node_by_id = new Map(shown.map((item) => [item.id, { item, children: [] }]));
|
|
22
|
+
const top_nodes = [];
|
|
23
|
+
for (const node of node_by_id.values()) {
|
|
24
|
+
const seen = new Set([node.item.id]);
|
|
25
|
+
let ancestor_id = node.item.parent_id;
|
|
26
|
+
while (ancestor_id && !node_by_id.has(ancestor_id) && !seen.has(ancestor_id)) {
|
|
27
|
+
seen.add(ancestor_id);
|
|
28
|
+
ancestor_id = item_by_id.get(ancestor_id)?.parent_id ?? null;
|
|
29
|
+
}
|
|
30
|
+
if (ancestor_id && node_by_id.has(ancestor_id) && !seen.has(ancestor_id))
|
|
31
|
+
node_by_id.get(ancestor_id).children.push(node);
|
|
32
|
+
else
|
|
33
|
+
top_nodes.push(node);
|
|
34
|
+
}
|
|
35
|
+
return top_nodes;
|
|
36
|
+
}
|
|
37
|
+
// ── Dropping ─────────────────────────────────────────────────────────────────
|
|
38
|
+
// The top and bottom quarters of a row drop beside it and the middle drops into it, or halves when nothing drops inside.
|
|
39
|
+
export function placement_at(fraction_down, allow_inside) {
|
|
40
|
+
if (!allow_inside)
|
|
41
|
+
return fraction_down < 0.5 ? "before" : "after";
|
|
42
|
+
if (fraction_down < 0.25)
|
|
43
|
+
return "before";
|
|
44
|
+
if (fraction_down > 0.75)
|
|
45
|
+
return "after";
|
|
46
|
+
return "inside";
|
|
47
|
+
}
|
|
48
|
+
// A row cannot drop onto itself or anything below it.
|
|
49
|
+
export function can_drop(dragged_id, target_id, items) {
|
|
50
|
+
return !subtree_ids(dragged_id, items).has(target_id);
|
|
51
|
+
}
|
|
52
|
+
// Picks a position between the neighbours at the drop point, so only the moved item changes.
|
|
53
|
+
function position_beside(siblings, target, placement) {
|
|
54
|
+
const sorted = [...siblings].sort(by_position);
|
|
55
|
+
const index = sorted.findIndex((sibling) => sibling.id === target.id);
|
|
56
|
+
const neighbour = sorted[placement === "before" ? index - 1 : index + 1];
|
|
57
|
+
if (!neighbour)
|
|
58
|
+
return placement === "before" ? target.position - 1 : target.position + 1;
|
|
59
|
+
return (target.position + neighbour.position) / 2;
|
|
60
|
+
}
|
|
61
|
+
// For a flat list, where items only move before or after each other.
|
|
62
|
+
export function position_for_drop(dragged, target, placement, items) {
|
|
63
|
+
return position_beside(items.filter((item) => item.id !== dragged.id), target, placement);
|
|
64
|
+
}
|
|
65
|
+
// Inside drops after the target's last child; before and after drop among the target's siblings.
|
|
66
|
+
export function move_for_drop(dragged, target, placement, items) {
|
|
67
|
+
const others = items.filter((item) => item.id !== dragged.id);
|
|
68
|
+
if (placement === "inside") {
|
|
69
|
+
const last_child_position = Math.max(target.position, ...children_of(target.id, others).map((item) => item.position));
|
|
70
|
+
return { parent_id: target.id, position: last_child_position + 1 };
|
|
71
|
+
}
|
|
72
|
+
return { parent_id: target.parent_id, position: position_beside(children_of(target.parent_id, others), target, placement) };
|
|
73
|
+
}
|
|
74
|
+
// Moves several items together, keeping their order: inside the target after its last child, or in a run between it and its neighbour.
|
|
75
|
+
export function moves_for_drop(dragged, target, placement, items) {
|
|
76
|
+
const dragged_ids = new Set(dragged.map((item) => item.id));
|
|
77
|
+
const others = items.filter((item) => !dragged_ids.has(item.id));
|
|
78
|
+
if (placement === "inside") {
|
|
79
|
+
const last_child_position = Math.max(target.position, ...children_of(target.id, others).map((item) => item.position));
|
|
80
|
+
return dragged.map((_, index) => ({ parent_id: target.id, position: last_child_position + index + 1 }));
|
|
81
|
+
}
|
|
82
|
+
const siblings = children_of(target.parent_id, others).sort(by_position);
|
|
83
|
+
const index = siblings.findIndex((sibling) => sibling.id === target.id);
|
|
84
|
+
const neighbour = siblings[placement === "before" ? index - 1 : index + 1];
|
|
85
|
+
const low = placement === "before" ? (neighbour?.position ?? target.position - 1) : target.position;
|
|
86
|
+
const high = placement === "before" ? target.position : (neighbour?.position ?? target.position + 1);
|
|
87
|
+
return dragged.map((_, index) => ({ parent_id: target.parent_id, position: low + ((high - low) * (index + 1)) / (dragged.length + 1) }));
|
|
88
|
+
}
|
|
89
|
+
// Up and down pass the sibling beside it, indent goes last inside the sibling above, outdent goes after its parent; null when it cannot go that way.
|
|
90
|
+
export function key_drop(item, direction, items) {
|
|
91
|
+
const siblings = children_of(item.parent_id, items).sort(by_position);
|
|
92
|
+
const index = siblings.findIndex((sibling) => sibling.id === item.id);
|
|
93
|
+
const above = siblings[index - 1];
|
|
94
|
+
const below = siblings[index + 1];
|
|
95
|
+
const parent = items.find((each) => each.id === item.parent_id);
|
|
96
|
+
if (direction === "up")
|
|
97
|
+
return above ? { target: above, placement: "before" } : null;
|
|
98
|
+
if (direction === "down")
|
|
99
|
+
return below ? { target: below, placement: "after" } : null;
|
|
100
|
+
if (direction === "indent")
|
|
101
|
+
return above ? { target: above, placement: "inside" } : null;
|
|
102
|
+
return parent ? { target: parent, placement: "after" } : null;
|
|
103
|
+
}
|
|
104
|
+
export function move_for_key(item, direction, items) {
|
|
105
|
+
const drop = key_drop(item, direction, items);
|
|
106
|
+
return drop && move_for_drop(item, drop.target, drop.placement, items);
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-libs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Code the X-apps share: agents and their long runs, passwords, durations, and the web page's helpers and widgets",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@fastify/websocket": "^11.3.5",
|
|
31
31
|
"fastify": "^5.12.7",
|
|
32
|
-
"
|
|
32
|
+
"react": "^19",
|
|
33
|
+
"react-dom": "^19"
|
|
33
34
|
},
|
|
34
35
|
"peerDependenciesMeta": {
|
|
35
36
|
"@fastify/websocket": {
|
|
@@ -38,22 +39,32 @@
|
|
|
38
39
|
"fastify": {
|
|
39
40
|
"optional": true
|
|
40
41
|
},
|
|
41
|
-
"
|
|
42
|
+
"react": {
|
|
43
|
+
"optional": true
|
|
44
|
+
},
|
|
45
|
+
"react-dom": {
|
|
42
46
|
"optional": true
|
|
43
47
|
}
|
|
44
48
|
},
|
|
45
49
|
"devDependencies": {
|
|
46
50
|
"@fastify/websocket": "^11.3.5",
|
|
51
|
+
"@testing-library/dom": "^10.4.2",
|
|
52
|
+
"@testing-library/react": "^16.3.3",
|
|
47
53
|
"@types/node": "^24.19.2",
|
|
54
|
+
"@types/react": "^19.3.0",
|
|
55
|
+
"@types/react-dom": "^19.3.0",
|
|
48
56
|
"@types/sshpk": "^1.17.5",
|
|
49
57
|
"fastify": "^5.12.7",
|
|
58
|
+
"jsdom": "^31.0.0",
|
|
50
59
|
"oxlint": "^1.90.0",
|
|
51
|
-
"
|
|
60
|
+
"react": "^19.3.1",
|
|
61
|
+
"react-dom": "^19.3.1",
|
|
52
62
|
"typescript": "^7.0.2",
|
|
53
63
|
"vitest": "^5.0.5"
|
|
54
64
|
},
|
|
55
65
|
"dependencies": {
|
|
56
66
|
"@inquirer/password": "^5.2.5",
|
|
67
|
+
"@tanstack/react-table": "^8.21.3",
|
|
57
68
|
"parse-duration": "^2.1.11",
|
|
58
69
|
"pretty-bytes": "^7.2.0",
|
|
59
70
|
"sshpk": "^1.18.0"
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
-
// The pieces a page is laid out with: panels, facts, tiles, cards, tabs, breadcrumbs, toolbars, steppers and settings rows.
|
|
3
|
-
import { Fragment } from "preact";
|
|
4
|
-
// ── Panels ───────────────────────────────────────────────────────────────────
|
|
5
|
-
export function Section({ title, tools, children }) {
|
|
6
|
-
return (_jsxs("section", { class: "panel", children: [_jsxs("div", { class: "panel-head", children: [_jsx("h2", { children: title }), tools && _jsx("div", { class: "panel-tools", children: tools })] }), children] }));
|
|
7
|
-
}
|
|
8
|
-
// Labelled values in a grid of tiles, or as rows of label and value for long values such as paths.
|
|
9
|
-
export function Facts({ rows, layout = "grid" }) {
|
|
10
|
-
return (_jsx("dl", { class: layout === "rows" ? "facts facts-rows" : "facts", children: rows.filter((row) => Boolean(row)).map(([label, value, class_name]) => (_jsxs("div", { children: [_jsx("dt", { children: label }), _jsx("dd", { class: class_name, children: value ?? "—" })] }, label))) }));
|
|
11
|
-
}
|
|
12
|
-
// A stack of panels or blocks spaced by its gap, tighter when tight.
|
|
13
|
-
export function Stack({ tight = false, children }) {
|
|
14
|
-
return _jsx("div", { class: tight ? "stack tight" : "stack", children: children });
|
|
15
|
-
}
|
|
16
|
-
// One headline figure and its label.
|
|
17
|
-
export function Tile({ label, value, tone, class_name, href, value_first = false }) {
|
|
18
|
-
const tile_class = ["tile", tone && tone !== "none" && `tile-${tone}`, class_name].filter(Boolean).join(" ");
|
|
19
|
-
const label_part = _jsx("div", { class: "tile-label", children: label });
|
|
20
|
-
const value_part = _jsx("div", { class: "tile-value", children: value });
|
|
21
|
-
const children = value_first ? _jsxs(_Fragment, { children: [value_part, label_part] }) : _jsxs(_Fragment, { children: [label_part, value_part] });
|
|
22
|
-
return href ? _jsx("a", { class: tile_class, href: href, children: children }) : _jsx("div", { class: tile_class, children: children });
|
|
23
|
-
}
|
|
24
|
-
export function Tiles({ children }) {
|
|
25
|
-
return _jsx("div", { class: "tiles", children: children });
|
|
26
|
-
}
|
|
27
|
-
// A titled card for one thing among many, laid out in a grid of Cards.
|
|
28
|
-
export function Card({ title, subtitle, badge, class_name, href, on_click, children }) {
|
|
29
|
-
const card_class = class_name ? `card ${class_name}` : "card";
|
|
30
|
-
const body = (_jsxs(_Fragment, { children: [_jsxs("div", { class: "card-head", children: [_jsx("strong", { children: title }), badge] }), subtitle && _jsx("div", { class: "muted card-host", children: subtitle }), children] }));
|
|
31
|
-
if (href)
|
|
32
|
-
return _jsx("a", { class: card_class, href: href, children: body });
|
|
33
|
-
if (on_click)
|
|
34
|
-
return _jsx("button", { type: "button", class: card_class, onClick: on_click, children: body });
|
|
35
|
-
return _jsx("div", { class: card_class, children: body });
|
|
36
|
-
}
|
|
37
|
-
export function Cards({ children }) {
|
|
38
|
-
return _jsx("div", { class: "grid", children: children });
|
|
39
|
-
}
|
|
40
|
-
export function Tabs({ items, active, label = "Sections", on_select }) {
|
|
41
|
-
return (_jsx("nav", { class: "tabs", "aria-label": label, children: items.map((item) => {
|
|
42
|
-
const tab_class = item.id === active ? "tab active" : "tab";
|
|
43
|
-
const current = item.id === active ? "page" : undefined;
|
|
44
|
-
return item.href
|
|
45
|
-
? _jsx("a", { class: tab_class, href: item.href, "aria-current": current, children: item.label }, item.id)
|
|
46
|
-
: _jsx("button", { type: "button", class: tab_class, "aria-current": current, onClick: () => on_select?.(item.id), children: item.label }, item.id);
|
|
47
|
-
}) }));
|
|
48
|
-
}
|
|
49
|
-
export function Crumbs({ items }) {
|
|
50
|
-
return (_jsx("nav", { class: "crumbs", "aria-label": "Breadcrumb", children: items.map(([label, href], index) => (_jsxs(Fragment, { children: [index > 0 && _jsx("span", { class: "muted", "aria-hidden": "true", children: " / " }), href ? _jsx("a", { href: href, children: label }) : _jsx("span", { "aria-current": "page", children: label })] }, index))) }));
|
|
51
|
-
}
|
|
52
|
-
// ── Toolbars ─────────────────────────────────────────────────────────────────
|
|
53
|
-
export function Toolbar({ children }) {
|
|
54
|
-
return _jsx("div", { class: "toolbar", children: children });
|
|
55
|
-
}
|
|
56
|
-
// Steps to the item before or after this one, each a link or a dead button when there is none, with where this one stands between.
|
|
57
|
-
export function Stepper({ label, previous, position, next }) {
|
|
58
|
-
const step = (target) => target.href === null
|
|
59
|
-
? _jsx("button", { disabled: true, children: target.text })
|
|
60
|
-
: _jsx("a", { class: "button", href: target.href, children: target.text });
|
|
61
|
-
return _jsxs("div", { class: "stepper", role: "group", "aria-label": label, children: [step(previous), _jsx("span", { class: "muted", children: position }), step(next)] });
|
|
62
|
-
}
|
|
63
|
-
// ── Settings ─────────────────────────────────────────────────────────────────
|
|
64
|
-
export function SettingsGroup({ heading, hint, children }) {
|
|
65
|
-
return (_jsxs("section", { class: "settings-group", children: [_jsx("h3", { class: "settings-heading", children: heading }), hint && _jsx("p", { class: "setting-hint", children: hint }), children] }));
|
|
66
|
-
}
|
|
67
|
-
// A setting's name and hint on one side and its control on the other; as a label, clicking anywhere works its one control.
|
|
68
|
-
export function SettingRow({ label, hint, as_label = false, children }) {
|
|
69
|
-
const text = (_jsxs("span", { class: "setting-text", children: [_jsx("span", { class: "setting-label", children: label }), hint && _jsx("span", { class: "setting-hint", children: hint })] }));
|
|
70
|
-
return as_label ? _jsxs("label", { class: "setting-row", children: [text, children] }) : _jsxs("div", { class: "setting-row", children: [text, children] });
|
|
71
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type ComponentChildren } from "preact";
|
|
2
|
-
import { type MenuItem } from "../menus.ts";
|
|
3
|
-
export type { MenuItem } from "../menus.ts";
|
|
4
|
-
interface ContextMenuProps {
|
|
5
|
-
x: number;
|
|
6
|
-
y: number;
|
|
7
|
-
items: MenuItem[];
|
|
8
|
-
on_close: () => void;
|
|
9
|
-
}
|
|
10
|
-
export declare function ContextMenu({ x, y, items, on_close }: ContextMenuProps): import("preact").JSX.Element;
|
|
11
|
-
interface PopoverProps {
|
|
12
|
-
anchor?: HTMLElement | null;
|
|
13
|
-
x?: number;
|
|
14
|
-
y?: number;
|
|
15
|
-
label: string;
|
|
16
|
-
on_close: () => void;
|
|
17
|
-
children: ComponentChildren;
|
|
18
|
-
}
|
|
19
|
-
export declare function Popover({ anchor, x, y, label, on_close, children }: PopoverProps): import("preact").JSX.Element;
|