@x-tech-laboratories/x-libs 0.6.0 → 0.8.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.
Files changed (65) hide show
  1. package/README.md +40 -11
  2. package/dist/backups.d.ts +31 -0
  3. package/dist/backups.js +43 -0
  4. package/dist/durations.d.ts +1 -0
  5. package/dist/durations.js +10 -0
  6. package/dist/electron/app.d.ts +13 -0
  7. package/dist/electron/app.js +27 -0
  8. package/dist/electron/bridge.d.ts +3 -1
  9. package/dist/electron/bridge.js +21 -0
  10. package/dist/electron/events.d.ts +21 -0
  11. package/dist/electron/events.js +25 -0
  12. package/dist/electron/fetch.d.ts +25 -0
  13. package/dist/electron/fetch.js +67 -0
  14. package/dist/electron/window.d.ts +11 -0
  15. package/dist/electron/window.js +49 -6
  16. package/dist/log.d.ts +30 -0
  17. package/dist/log.js +79 -0
  18. package/dist/server/backup_files.d.ts +3 -0
  19. package/dist/server/backup_files.js +25 -0
  20. package/dist/web/drag.d.ts +3 -0
  21. package/dist/web/drag.js +13 -0
  22. package/dist/web/format.d.ts +1 -0
  23. package/dist/web/format.js +5 -0
  24. package/dist/web/prefs.d.ts +12 -0
  25. package/dist/web/prefs.js +62 -0
  26. package/dist/web/react/controls.d.ts +39 -4
  27. package/dist/web/react/controls.js +51 -7
  28. package/dist/web/react/dialog.d.ts +12 -2
  29. package/dist/web/react/dialog.js +7 -3
  30. package/dist/web/react/layout.d.ts +26 -6
  31. package/dist/web/react/layout.js +15 -6
  32. package/dist/web/react/list_picker.d.ts +19 -0
  33. package/dist/web/react/list_picker.js +50 -0
  34. package/dist/web/react/media.d.ts +24 -0
  35. package/dist/web/react/media.js +18 -0
  36. package/dist/web/react/menu_button.d.ts +10 -0
  37. package/dist/web/react/menu_button.js +12 -0
  38. package/dist/web/react/menus.js +26 -2
  39. package/dist/web/react/prefs.d.ts +2 -0
  40. package/dist/web/react/prefs.js +11 -0
  41. package/dist/web/react/reveal.d.ts +10 -0
  42. package/dist/web/react/reveal.js +30 -0
  43. package/dist/web/react/shell.d.ts +8 -2
  44. package/dist/web/react/shell.js +17 -6
  45. package/dist/web/react/table.d.ts +31 -1
  46. package/dist/web/react/table.js +209 -24
  47. package/dist/web/react/time.d.ts +1 -0
  48. package/dist/web/react/time.js +11 -1
  49. package/dist/web/react/toasts.d.ts +12 -0
  50. package/dist/web/react/toasts.js +31 -0
  51. package/dist/web/react/tree.d.ts +7 -2
  52. package/dist/web/react/tree.js +22 -7
  53. package/dist/web/react/watch.d.ts +3 -0
  54. package/dist/web/react/watch.js +5 -0
  55. package/dist/web/selection.d.ts +25 -0
  56. package/dist/web/selection.js +106 -0
  57. package/dist/web/tables.d.ts +1 -0
  58. package/dist/web/tables.js +8 -0
  59. package/dist/web/tree.d.ts +8 -3
  60. package/dist/web/tree.js +31 -8
  61. package/dist/web/virtual.d.ts +7 -0
  62. package/dist/web/virtual.js +10 -0
  63. package/dist/web/watch.d.ts +6 -0
  64. package/dist/web/watch.js +17 -0
  65. package/package.json +1 -1
@@ -14,4 +14,16 @@ export interface ToastOptions {
14
14
  }
15
15
  export type ToastCloseReason = "timeout" | "dismissed" | "action" | "closed";
16
16
  export declare function toast(message: string, options?: ToastOptions): () => void;
17
+ export interface UndoableOptions {
18
+ commit: () => void;
19
+ undo: () => void;
20
+ duration_in_milliseconds?: number;
21
+ undo_label?: string;
22
+ }
23
+ export interface Undoable {
24
+ finish: () => void;
25
+ undo: () => void;
26
+ readonly settled: boolean;
27
+ }
28
+ export declare function undoable(message: string, { commit, undo, duration_in_milliseconds, undo_label }: UndoableOptions): Undoable;
17
29
  export declare function Toasts(): import("react").JSX.Element;
@@ -25,6 +25,37 @@ export function toast(message, options = {}) {
25
25
  setTimeout(() => close_toast(id, "timeout"), options.duration_in_milliseconds ?? 6000);
26
26
  return () => close_toast(id, "closed");
27
27
  }
28
+ // Shows the message with an Undo button, then either undoes or commits the action, exactly once.
29
+ export function undoable(message, { commit, undo, duration_in_milliseconds = 8000, undo_label = "Undo" }) {
30
+ let settled = false;
31
+ let undo_wanted = false;
32
+ const settle = (reason) => {
33
+ if (settled)
34
+ return;
35
+ settled = true;
36
+ if (reason === "action" || undo_wanted)
37
+ undo();
38
+ else
39
+ commit();
40
+ };
41
+ const close = toast(message, {
42
+ action: { text: undo_label, run: () => { } },
43
+ duration_in_milliseconds,
44
+ on_close: settle,
45
+ });
46
+ return {
47
+ finish: () => close(),
48
+ undo: () => {
49
+ if (settled)
50
+ return;
51
+ undo_wanted = true;
52
+ close();
53
+ },
54
+ get settled() {
55
+ return settled;
56
+ },
57
+ };
58
+ }
28
59
  // ── Host ─────────────────────────────────────────────────────────────────────
29
60
  // Where toasts stack; the Shell holds one, and a page without the Shell renders its own.
30
61
  export function Toasts() {
@@ -1,4 +1,4 @@
1
- import { type MouseEvent, type ReactNode } from "react";
1
+ import { type DragEvent, type MouseEvent, type ReactNode } from "react";
2
2
  import { type DropPlacement, type TreeItem, type TreeMove } from "../tree.ts";
3
3
  export type { DropPlacement, TreeItem, TreeMove } from "../tree.ts";
4
4
  interface TreeProps<Item extends TreeItem> {
@@ -10,7 +10,10 @@ interface TreeProps<Item extends TreeItem> {
10
10
  collapsed: ReadonlySet<string>;
11
11
  on_collapsed_change: (collapsed: Set<string>) => void;
12
12
  selected_id?: string | null;
13
+ selected_ids?: ReadonlySet<string>;
13
14
  on_select?: (item: Item) => void;
15
+ on_activate?: (item: Item) => void;
16
+ row_class?: (item: Item) => string | undefined;
14
17
  on_move?: (item: Item, move: TreeMove) => void;
15
18
  can_drop?: (dragged: Item, target: Item, placement: DropPlacement) => boolean;
16
19
  allow_inside?: boolean;
@@ -19,5 +22,7 @@ interface TreeProps<Item extends TreeItem> {
19
22
  form: ReactNode;
20
23
  } | null;
21
24
  on_context_menu?: (event: MouseEvent<HTMLElement>, item: Item) => void;
25
+ accepts_external?: (event: DragEvent<HTMLElement>, item: Item) => boolean;
26
+ on_external_drop?: (event: DragEvent<HTMLElement>, item: Item) => void;
22
27
  }
23
- export declare function Tree<Item extends TreeItem>({ items, label, render_item, render_extra, name_of, collapsed, on_collapsed_change, selected_id, on_select, on_move, can_drop, allow_inside, new_item, on_context_menu, }: TreeProps<Item>): import("react").JSX.Element;
28
+ export declare function Tree<Item extends TreeItem>({ items, label, render_item, render_extra, name_of, collapsed, on_collapsed_change, selected_id, selected_ids, on_select, on_activate, row_class: class_of, on_move, can_drop, allow_inside, new_item, on_context_menu, accepts_external, on_external_drop, }: TreeProps<Item>): import("react").JSX.Element;
@@ -7,7 +7,7 @@ const KEY_MOVES = { ArrowUp: "up", ArrowDown: "down", ArrowRight: "indent", Arro
7
7
  function visible_nodes(nodes, collapsed) {
8
8
  return nodes.flatMap((node) => [node, ...(collapsed.has(node.item.id) ? [] : visible_nodes(node.children, collapsed))]);
9
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, }) {
10
+ export function Tree({ items, label, render_item, render_extra, name_of, collapsed, on_collapsed_change, selected_id = null, selected_ids, on_select, on_activate, row_class: class_of, on_move, can_drop, allow_inside = true, new_item = null, on_context_menu, accepts_external, on_external_drop, }) {
11
11
  const [focused_id, set_focused_id] = useState(null);
12
12
  const [dragged_id, set_dragged_id] = useState(null);
13
13
  const [drop_target, set_drop_target] = useState(null);
@@ -15,7 +15,9 @@ export function Tree({ items, label, render_item, render_extra, name_of, collaps
15
15
  const moves_focus = useRef(false);
16
16
  const roots = nest([...items].sort(by_position), items);
17
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;
18
+ const is_selected = (id) => selected_id === id || Boolean(selected_ids?.has(id));
19
+ const first_selected = shown.find((node) => selected_ids?.has(node.item.id))?.item.id ?? null;
20
+ const tab_stop = [focused_id, selected_id, first_selected].find((id) => id !== null && shown.some((node) => node.item.id === id)) ?? shown[0]?.item.id;
19
21
  useEffect(() => {
20
22
  if (!moves_focus.current || focused_id === null)
21
23
  return;
@@ -53,17 +55,29 @@ export function Tree({ items, label, render_item, render_extra, name_of, collaps
53
55
  const placement = placement_at(box.height > 0 ? (event.clientY - box.top) / box.height : 0.5, allow_inside);
54
56
  return is_allowed(dragged, target, placement) ? placement : null;
55
57
  }
58
+ // A drag from outside the tree, such as a task, lands inside the row that accepts it.
59
+ function accepts_from_outside(event, target) {
60
+ return dragged_id === null && Boolean(accepts_external?.(event, target));
61
+ }
56
62
  function drag_over(event, target) {
57
- const placement = placement_over(event, target);
63
+ const external = accepts_from_outside(event, target);
64
+ const placement = external ? "inside" : placement_over(event, target);
58
65
  if (!placement)
59
66
  return;
60
67
  event.preventDefault();
61
- if (event.dataTransfer)
68
+ // An outside drag keeps the effect its source allowed, which may not include a move.
69
+ if (event.dataTransfer && !external)
62
70
  event.dataTransfer.dropEffect = "move";
63
71
  if (drop_target?.id !== target.id || drop_target.placement !== placement)
64
72
  set_drop_target({ id: target.id, placement });
65
73
  }
66
74
  function drop(event, target) {
75
+ if (accepts_from_outside(event, target)) {
76
+ event.preventDefault();
77
+ end_drag();
78
+ on_external_drop?.(event, target);
79
+ return;
80
+ }
67
81
  const placement = placement_over(event, target);
68
82
  const dragged = items.find((item) => item.id === dragged_id);
69
83
  end_drag();
@@ -147,22 +161,23 @@ export function Tree({ items, label, render_item, render_extra, name_of, collaps
147
161
  const new_child = new_item_row(item.id);
148
162
  const row_class = [
149
163
  "tree-row",
164
+ class_of?.(item),
150
165
  dragged_id === item.id && "dragging",
151
166
  drop_target?.id === item.id && `drop-${drop_target.placement}`,
152
167
  ].filter(Boolean).join(" ");
153
168
  return (_jsxs("li", { ref: (element) => { if (element)
154
169
  row_elements.current.set(item.id, element);
155
170
  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)
171
+ 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_ids ? is_selected(item.id) : undefined, tabIndex: tab_stop === item.id ? 0 : -1, onFocus: (event) => { if (event.target === event.currentTarget)
157
172
  set_focused_id(item.id); }, children: [_jsxs("div", { className: row_class, draggable: on_move ? true : undefined, onDragStart: (event) => {
158
173
  event.dataTransfer?.setData("text/plain", item.id);
159
174
  if (event.dataTransfer)
160
175
  event.dataTransfer.effectAllowed = "move";
161
176
  set_dragged_id(item.id);
162
177
  }, 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
178
+ set_drop_target(null); }, onDrop: (event) => drop(event, item), onClick: () => { focus_row(item.id); on_select?.(item); }, onDoubleClick: on_activate && (() => on_activate(item)), onContextMenu: on_context_menu && ((event) => on_context_menu(event, item)), children: [has_children
164
179
  ? (_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
180
  : _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
181
  }
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)] }));
182
+ return (_jsxs("ul", { role: "tree", className: "tree", "aria-label": label, "aria-multiselectable": selected_ids ? true : undefined, onKeyDown: key_down, children: [roots.map((node) => branch(node, 0)), new_item_row(null)] }));
168
183
  }
@@ -0,0 +1,3 @@
1
+ import type { Watch } from "../watch.ts";
2
+ export type { Watch } from "../watch.ts";
3
+ export declare function use_watch(watch: Watch): number;
@@ -0,0 +1,5 @@
1
+ // Redraws a component whenever a watch it reads is bumped.
2
+ import { useSyncExternalStore } from "react";
3
+ export function use_watch(watch) {
4
+ return useSyncExternalStore(watch.subscribe, watch.version);
5
+ }
@@ -0,0 +1,25 @@
1
+ export type SelectionIntent = "replace" | "toggle" | "range" | "range-add";
2
+ export declare function intent_for(modifiers: {
3
+ shift?: boolean;
4
+ ctrl?: boolean;
5
+ }): SelectionIntent;
6
+ export declare class Selection<Id = number> {
7
+ private readonly picked;
8
+ private anchor;
9
+ get size(): number;
10
+ has(id: Id): boolean;
11
+ ids(): Id[];
12
+ inspect(): {
13
+ size: number;
14
+ anchor: number | null;
15
+ };
16
+ clear(): void;
17
+ apply(ordered: readonly Id[], index: number, intent: SelectionIntent): void;
18
+ select_all(ordered: readonly Id[]): void;
19
+ invert(ordered: readonly Id[]): void;
20
+ retain(ordered: readonly Id[]): void;
21
+ }
22
+ export declare function toggled(picked: readonly string[], id: string): string[];
23
+ export declare function with_range(order: readonly string[], picked: readonly string[], anchor_id: string | null, id: string): string[];
24
+ export declare function still_shown(order: readonly string[], picked: readonly string[]): string[];
25
+ export declare function row_after_key(key: string, current: number, last: number, rows_per_page: number): number | null;
@@ -0,0 +1,106 @@
1
+ // Picking rows in a list: plain, Ctrl and Shift clicks, an anchor ranges measure from, and the row an arrow or page key moves to.
2
+ export function intent_for(modifiers) {
3
+ if (modifiers.shift)
4
+ return modifiers.ctrl ? "range-add" : "range";
5
+ return modifiers.ctrl ? "toggle" : "replace";
6
+ }
7
+ // ── Selection ────────────────────────────────────────────────────────────────
8
+ export class Selection {
9
+ picked = new Set();
10
+ anchor = null;
11
+ get size() {
12
+ return this.picked.size;
13
+ }
14
+ has(id) {
15
+ return this.picked.has(id);
16
+ }
17
+ ids() {
18
+ return [...this.picked];
19
+ }
20
+ // The picks and the anchor, for finding why a range came out wrong.
21
+ inspect() {
22
+ return { size: this.picked.size, anchor: this.anchor };
23
+ }
24
+ clear() {
25
+ this.picked.clear();
26
+ this.anchor = null;
27
+ }
28
+ // Applies a click on the row at index in the list as shown.
29
+ apply(ordered, index, intent) {
30
+ if (index < 0 || index >= ordered.length)
31
+ return;
32
+ const id = ordered[index];
33
+ if (intent === "replace") {
34
+ this.picked.clear();
35
+ this.picked.add(id);
36
+ this.anchor = index;
37
+ return;
38
+ }
39
+ if (intent === "toggle") {
40
+ if (this.picked.has(id))
41
+ this.picked.delete(id);
42
+ else
43
+ this.picked.add(id);
44
+ this.anchor = index;
45
+ return;
46
+ }
47
+ // The anchor stays put, so every extension measures from the same row.
48
+ const from = this.anchor ?? index;
49
+ const [low, high] = from <= index ? [from, index] : [index, from];
50
+ if (intent === "range")
51
+ this.picked.clear();
52
+ for (let row = low; row <= high; row += 1)
53
+ this.picked.add(ordered[row]);
54
+ }
55
+ select_all(ordered) {
56
+ for (const id of ordered)
57
+ this.picked.add(id);
58
+ }
59
+ invert(ordered) {
60
+ const next = ordered.filter((id) => !this.picked.has(id));
61
+ this.picked.clear();
62
+ for (const id of next)
63
+ this.picked.add(id);
64
+ }
65
+ // Drops picks no longer in the list, such as after a filter narrows it.
66
+ retain(ordered) {
67
+ const kept = new Set(ordered);
68
+ for (const id of this.picked)
69
+ if (!kept.has(id))
70
+ this.picked.delete(id);
71
+ }
72
+ }
73
+ // ── Ids ──────────────────────────────────────────────────────────────────────
74
+ // The same picking as plain lists of ids held in an app's state, the anchor kept by id so it survives the rows moving between clicks.
75
+ // Adds the id when missing and takes it out when there.
76
+ export function toggled(picked, id) {
77
+ return picked.includes(id) ? picked.filter((each) => each !== id) : [...picked, id];
78
+ }
79
+ // Every id between the anchor and the id in the shown order, both included, added to those picked; just the id when the anchor is not shown.
80
+ export function with_range(order, picked, anchor_id, id) {
81
+ const anchor_index = anchor_id === null ? -1 : order.indexOf(anchor_id);
82
+ const index = order.indexOf(id);
83
+ if (anchor_index === -1 || index === -1)
84
+ return [id];
85
+ const run = order.slice(Math.min(anchor_index, index), Math.max(anchor_index, index) + 1);
86
+ return [...picked.filter((each) => !run.includes(each)), ...run];
87
+ }
88
+ // Keeps only the picked ids still shown, in the shown order.
89
+ export function still_shown(order, picked) {
90
+ const picked_ids = new Set(picked);
91
+ return order.filter((id) => picked_ids.has(id));
92
+ }
93
+ // ── Keys ─────────────────────────────────────────────────────────────────────
94
+ // The row an arrow, page, Home or End key moves to, held inside the list, or null for any other key.
95
+ export function row_after_key(key, current, last, rows_per_page) {
96
+ const clamp = (row) => Math.min(last, Math.max(0, row));
97
+ switch (key) {
98
+ case "ArrowDown": return clamp(current + 1);
99
+ case "ArrowUp": return clamp(current - 1);
100
+ case "PageDown": return clamp(current + rows_per_page);
101
+ case "PageUp": return clamp(current - rows_per_page);
102
+ case "Home": return 0;
103
+ case "End": return Math.max(0, last);
104
+ default: return null;
105
+ }
106
+ }
@@ -24,4 +24,5 @@ export declare function sort_after_click(sorting: readonly Sorting[], key: strin
24
24
  export declare function sorting_from(query: URLSearchParams, fallback: Sorting): Sorting;
25
25
  export declare function ordered_columns(saved_order: readonly string[], all_columns: readonly string[]): string[];
26
26
  export declare function move_column(order: readonly string[], column: string, target: string, after: boolean): string[];
27
+ export declare function moved<Item>(items: readonly Item[], from: number, to: number): Item[];
27
28
  export declare function clamp_width(width: number, min?: number, max?: number): number;
@@ -74,6 +74,14 @@ export function move_column(order, column, target, after) {
74
74
  const index = rest.indexOf(target) + (after ? 1 : 0);
75
75
  return [...rest.slice(0, index), column, ...rest.slice(index)];
76
76
  }
77
+ // Moves the item at from to to, such as for a list's up and down buttons.
78
+ export function moved(items, from, to) {
79
+ if (from < 0 || from >= items.length)
80
+ return [...items];
81
+ const rest = items.filter((_, index) => index !== from);
82
+ rest.splice(Math.max(0, Math.min(to, rest.length)), 0, items[from]);
83
+ return rest;
84
+ }
77
85
  // A width held between the column's narrowest and widest.
78
86
  export function clamp_width(width, min = 48, max = 960) {
79
87
  return Math.round(Math.min(max, Math.max(min, width)));
@@ -13,20 +13,25 @@ export interface TreeMove {
13
13
  parent_id: string | null;
14
14
  position: number;
15
15
  }
16
+ export interface DropOptions {
17
+ by_list_order?: boolean;
18
+ }
16
19
  export declare function by_position<Item extends {
17
20
  position: number;
18
21
  }>(first: Item, second: Item): number;
19
22
  export declare function children_of<Item extends TreeItem>(parent_id: string | null, items: Item[]): Item[];
20
23
  export declare function subtree_ids(item_id: string, items: TreeItem[]): Set<string>;
21
24
  export declare function nest<Item extends TreeItem>(shown: Item[], all?: Item[]): TreeNode<Item>[];
25
+ export declare function in_tree_order<Item extends TreeItem>(items: Item[], reversed?: boolean): Item[];
26
+ export declare function path_names<Item extends TreeItem>(item: Item, items: Item[], name_of: (item: Item) => string): string[];
22
27
  export declare function placement_at(fraction_down: number, allow_inside: boolean): DropPlacement;
23
28
  export declare function can_drop(dragged_id: string, target_id: string, items: TreeItem[]): boolean;
24
29
  export declare function position_for_drop<Item extends {
25
30
  id: string;
26
31
  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[];
32
+ }>(dragged: Item, target: Item, placement: SidePlacement, items: Item[], options?: DropOptions): number;
33
+ export declare function move_for_drop<Item extends TreeItem>(dragged: Item, target: Item, placement: DropPlacement, items: Item[], options?: DropOptions): TreeMove;
34
+ export declare function moves_for_drop<Item extends TreeItem>(dragged: Item[], target: Item, placement: DropPlacement, items: Item[], options?: DropOptions): TreeMove[];
30
35
  export type KeyMove = "up" | "down" | "indent" | "outdent";
31
36
  export declare function key_drop<Item extends TreeItem>(item: Item, direction: KeyMove, items: Item[]): {
32
37
  target: Item;
package/dist/web/tree.js CHANGED
@@ -34,6 +34,27 @@ export function nest(shown, all = shown) {
34
34
  }
35
35
  return top_nodes;
36
36
  }
37
+ // Every item in tree order: each followed by the items below it, siblings by position, each set of siblings reversed when asked.
38
+ export function in_tree_order(items, reversed = false) {
39
+ const sorted = [...items].sort(by_position);
40
+ if (reversed)
41
+ sorted.reverse();
42
+ const walk = (nodes) => nodes.flatMap((node) => [node.item, ...walk(node.children)]);
43
+ const ordered = walk(nest(sorted, items));
44
+ const reached = new Set(ordered);
45
+ return [...ordered, ...sorted.filter((item) => !reached.has(item))];
46
+ }
47
+ // The names from the top of the tree down to the item, such as ["Work", "Clients"]; stops at a missing parent or a loop.
48
+ export function path_names(item, items, name_of) {
49
+ const item_by_id = new Map(items.map((each) => [each.id, each]));
50
+ const path = [item];
51
+ let parent = item.parent_id === null ? undefined : item_by_id.get(item.parent_id);
52
+ while (parent && !path.includes(parent)) {
53
+ path.unshift(parent);
54
+ parent = parent.parent_id === null ? undefined : item_by_id.get(parent.parent_id);
55
+ }
56
+ return path.map(name_of);
57
+ }
37
58
  // ── Dropping ─────────────────────────────────────────────────────────────────
38
59
  // The top and bottom quarters of a row drop beside it and the middle drops into it, or halves when nothing drops inside.
39
60
  export function placement_at(fraction_down, allow_inside) {
@@ -50,8 +71,8 @@ export function can_drop(dragged_id, target_id, items) {
50
71
  return !subtree_ids(dragged_id, items).has(target_id);
51
72
  }
52
73
  // 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);
74
+ function position_beside(siblings, target, placement, options = {}) {
75
+ const sorted = options.by_list_order ? siblings : [...siblings].sort(by_position);
55
76
  const index = sorted.findIndex((sibling) => sibling.id === target.id);
56
77
  const neighbour = sorted[placement === "before" ? index - 1 : index + 1];
57
78
  if (!neighbour)
@@ -59,27 +80,29 @@ function position_beside(siblings, target, placement) {
59
80
  return (target.position + neighbour.position) / 2;
60
81
  }
61
82
  // 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);
83
+ export function position_for_drop(dragged, target, placement, items, options = {}) {
84
+ return position_beside(items.filter((item) => item.id !== dragged.id), target, placement, options);
64
85
  }
65
86
  // 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) {
87
+ export function move_for_drop(dragged, target, placement, items, options = {}) {
67
88
  const others = items.filter((item) => item.id !== dragged.id);
68
89
  if (placement === "inside") {
69
90
  const last_child_position = Math.max(target.position, ...children_of(target.id, others).map((item) => item.position));
70
91
  return { parent_id: target.id, position: last_child_position + 1 };
71
92
  }
72
- return { parent_id: target.parent_id, position: position_beside(children_of(target.parent_id, others), target, placement) };
93
+ return { parent_id: target.parent_id, position: position_beside(children_of(target.parent_id, others), target, placement, options) };
73
94
  }
74
95
  // 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) {
96
+ export function moves_for_drop(dragged, target, placement, items, options = {}) {
76
97
  const dragged_ids = new Set(dragged.map((item) => item.id));
77
98
  const others = items.filter((item) => !dragged_ids.has(item.id));
78
99
  if (placement === "inside") {
79
100
  const last_child_position = Math.max(target.position, ...children_of(target.id, others).map((item) => item.position));
80
101
  return dragged.map((_, index) => ({ parent_id: target.id, position: last_child_position + index + 1 }));
81
102
  }
82
- const siblings = children_of(target.parent_id, others).sort(by_position);
103
+ const siblings = children_of(target.parent_id, others);
104
+ if (!options.by_list_order)
105
+ siblings.sort(by_position);
83
106
  const index = siblings.findIndex((sibling) => sibling.id === target.id);
84
107
  const neighbour = siblings[placement === "before" ? index - 1 : index + 1];
85
108
  const low = placement === "before" ? (neighbour?.position ?? target.position - 1) : target.position;
@@ -0,0 +1,7 @@
1
+ export interface VirtualWindow {
2
+ start: number;
3
+ end: number;
4
+ offset_px: number;
5
+ total_px: number;
6
+ }
7
+ export declare function virtual_window(total_rows: number, row_height: number, scroll_top: number, viewport_height: number, overscan?: number): VirtualWindow;
@@ -0,0 +1,10 @@
1
+ // Which rows of a long list a scrolled viewport needs, so only those are drawn.
2
+ export function virtual_window(total_rows, row_height, scroll_top, viewport_height, overscan = 8) {
3
+ if (row_height <= 0)
4
+ throw new Error("row_height must be positive");
5
+ const first_visible = Math.floor(Math.max(0, scroll_top) / row_height);
6
+ const visible_count = Math.ceil(viewport_height / row_height);
7
+ const start = Math.max(0, first_visible - overscan);
8
+ const end = Math.min(total_rows, first_visible + visible_count + overscan);
9
+ return { start, end, offset_px: start * row_height, total_px: total_rows * row_height };
10
+ }
@@ -0,0 +1,6 @@
1
+ export interface Watch {
2
+ version(): number;
3
+ bump(): void;
4
+ subscribe(listener: () => void): () => void;
5
+ }
6
+ export declare function create_watch(): Watch;
@@ -0,0 +1,17 @@
1
+ // A change counter a store bumps and a page watches, so only what reads the store redraws when it changes.
2
+ export function create_watch() {
3
+ let version = 0;
4
+ const listeners = new Set();
5
+ return {
6
+ version: () => version,
7
+ bump: () => {
8
+ version += 1;
9
+ for (const listener of listeners)
10
+ listener();
11
+ },
12
+ subscribe: (listener) => {
13
+ listeners.add(listener);
14
+ return () => void listeners.delete(listener);
15
+ },
16
+ };
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-tech-laboratories/x-libs",
3
- "version": "0.6.0",
3
+ "version": "0.8.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": {