occulite-ui 0.0.0-stage → 0.2.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 (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +290 -2
  3. package/THIRD_PARTY_NOTICES.md +36 -0
  4. package/dist/clipboard.d.ts +26 -0
  5. package/dist/clipboard.js +70 -0
  6. package/dist/components/ContextMenu.svelte +158 -0
  7. package/dist/components/ContextMenu.svelte.d.ts +14 -0
  8. package/dist/components/DataTable.svelte +1990 -0
  9. package/dist/components/DataTable.svelte.d.ts +154 -0
  10. package/dist/components/Dialog.svelte +420 -0
  11. package/dist/components/Dialog.svelte.d.ts +55 -0
  12. package/dist/components/GithubLink.svelte +74 -0
  13. package/dist/components/GithubLink.svelte.d.ts +16 -0
  14. package/dist/components/Header.svelte +196 -0
  15. package/dist/components/Header.svelte.d.ts +42 -0
  16. package/dist/components/Icon.svelte +51 -0
  17. package/dist/components/Icon.svelte.d.ts +12 -0
  18. package/dist/components/LanguageSwitch.svelte +63 -0
  19. package/dist/components/LanguageSwitch.svelte.d.ts +17 -0
  20. package/dist/components/Loader.svelte +65 -0
  21. package/dist/components/Loader.svelte.d.ts +9 -0
  22. package/dist/components/MultiSelect.svelte +467 -0
  23. package/dist/components/MultiSelect.svelte.d.ts +28 -0
  24. package/dist/components/Notices.svelte +146 -0
  25. package/dist/components/Notices.svelte.d.ts +16 -0
  26. package/dist/components/PrimaryToolbarButton.svelte +189 -0
  27. package/dist/components/PrimaryToolbarButton.svelte.d.ts +34 -0
  28. package/dist/components/Tabs.svelte +118 -0
  29. package/dist/components/Tabs.svelte.d.ts +16 -0
  30. package/dist/components/ThemeSwitch.svelte +46 -0
  31. package/dist/components/ThemeSwitch.svelte.d.ts +11 -0
  32. package/dist/components/Toolbar.svelte +57 -0
  33. package/dist/components/Toolbar.svelte.d.ts +11 -0
  34. package/dist/components/ToolbarButton.svelte +126 -0
  35. package/dist/components/ToolbarButton.svelte.d.ts +22 -0
  36. package/dist/components/Tooltip.svelte +106 -0
  37. package/dist/components/Tooltip.svelte.d.ts +31 -0
  38. package/dist/components/TooltipBubble.svelte +65 -0
  39. package/dist/components/TooltipBubble.svelte.d.ts +18 -0
  40. package/dist/components/contextMenu.d.ts +19 -0
  41. package/dist/components/contextMenu.js +6 -0
  42. package/dist/components/dialogGeometry.d.ts +91 -0
  43. package/dist/components/dialogGeometry.js +140 -0
  44. package/dist/components/index.d.ts +22 -0
  45. package/dist/components/index.js +22 -0
  46. package/dist/components/multiSelect.d.ts +27 -0
  47. package/dist/components/multiSelect.js +26 -0
  48. package/dist/components/navigation.d.ts +34 -0
  49. package/dist/components/navigation.js +19 -0
  50. package/dist/components/tooltip.d.ts +29 -0
  51. package/dist/components/tooltip.js +27 -0
  52. package/dist/css/app.css +3 -0
  53. package/dist/css/base.css +84 -0
  54. package/dist/css/tokens.css +170 -0
  55. package/dist/dialog/index.d.ts +6 -0
  56. package/dist/dialog/index.js +6 -0
  57. package/dist/embedding/ShellEmbedding.svelte.d.ts +27 -0
  58. package/dist/embedding/ShellEmbedding.svelte.js +79 -0
  59. package/dist/embedding/constants.d.ts +23 -0
  60. package/dist/embedding/constants.js +54 -0
  61. package/dist/embedding/index.d.ts +2 -0
  62. package/dist/embedding/index.js +2 -0
  63. package/dist/externalLink.d.ts +15 -0
  64. package/dist/externalLink.js +20 -0
  65. package/dist/format.d.ts +6 -0
  66. package/dist/format.js +16 -0
  67. package/dist/i18n/i18n.svelte.d.ts +112 -0
  68. package/dist/i18n/i18n.svelte.js +211 -0
  69. package/dist/i18n/index.d.ts +3 -0
  70. package/dist/i18n/index.js +3 -0
  71. package/dist/i18n/messages.d.ts +9 -0
  72. package/dist/i18n/messages.js +58 -0
  73. package/dist/i18n/translator.d.ts +65 -0
  74. package/dist/i18n/translator.js +99 -0
  75. package/dist/icons/index.d.ts +17 -0
  76. package/dist/icons/index.js +11 -0
  77. package/dist/index.d.ts +16 -0
  78. package/dist/index.js +16 -0
  79. package/dist/notices/NoticesStore.svelte.d.ts +64 -0
  80. package/dist/notices/NoticesStore.svelte.js +127 -0
  81. package/dist/notices/index.d.ts +1 -0
  82. package/dist/notices/index.js +1 -0
  83. package/dist/storage.d.ts +11 -0
  84. package/dist/storage.js +27 -0
  85. package/dist/table/LocalColumnWidths.svelte.d.ts +32 -0
  86. package/dist/table/LocalColumnWidths.svelte.js +91 -0
  87. package/dist/table/columnMeasure.d.ts +24 -0
  88. package/dist/table/columnMeasure.js +82 -0
  89. package/dist/table/columnWidths.d.ts +60 -0
  90. package/dist/table/columnWidths.js +86 -0
  91. package/dist/table/dataTableContext.d.ts +26 -0
  92. package/dist/table/dataTableContext.js +16 -0
  93. package/dist/table/index.d.ts +6 -0
  94. package/dist/table/index.js +6 -0
  95. package/dist/table/metrics.d.ts +10 -0
  96. package/dist/table/metrics.js +10 -0
  97. package/dist/table/tableModel.d.ts +306 -0
  98. package/dist/table/tableModel.js +437 -0
  99. package/dist/theme/choices.d.ts +8 -0
  100. package/dist/theme/choices.js +6 -0
  101. package/dist/theme/index.d.ts +2 -0
  102. package/dist/theme/index.js +2 -0
  103. package/dist/theme/theme.svelte.d.ts +60 -0
  104. package/dist/theme/theme.svelte.js +108 -0
  105. package/package.json +137 -4
@@ -0,0 +1,64 @@
1
+ /** How loud a notice is: a receipt, something worth reading, or something that failed. */
2
+ export type NoticeLevel = 'info' | 'warn' | 'error';
3
+ export interface Notice {
4
+ readonly id: number;
5
+ readonly level: NoticeLevel;
6
+ readonly message: string;
7
+ /** What the notice is about (a connection, a service), shown beside the message. */
8
+ readonly source?: string;
9
+ /** Milliseconds since epoch. */
10
+ readonly timestamp: number;
11
+ }
12
+ /** How many toasts are on screen at once; the rest collapse into a counter. */
13
+ export declare const VISIBLE_NOTICES = 5;
14
+ /** An informational toast is gone after this long, unless it was dismissed first. */
15
+ export declare const INFO_TTL_MS = 5000;
16
+ /** A warning stays three times as long: it is worth reading, but not worth a click. */
17
+ export declare const WARN_TTL_MS = 15000;
18
+ export interface NoticesStoreOptions {
19
+ /** How many notices are kept at all; the oldest is dropped. */
20
+ readonly max?: number;
21
+ readonly now?: () => number;
22
+ /** Lifetime of an `info` toast in ms; `0` keeps it until it is dismissed. */
23
+ readonly infoTtlMs?: number;
24
+ /** Lifetime of a `warn` toast in ms; `0` keeps it until it is dismissed. */
25
+ readonly warnTtlMs?: number;
26
+ /**
27
+ * The hint a warning or an error calls for, or `undefined`. The store shows it once per
28
+ * session as a toast of its own, after the notice that called for it - a hint that every
29
+ * failure of the same kind would otherwise repeat.
30
+ */
31
+ readonly hintFor?: (message: string) => string | undefined;
32
+ }
33
+ /**
34
+ * Everything the user should see but must not be interrupted by: a dismissable toast, and nothing
35
+ * steals focus. A modal alert would close whatever else is open and could throw away a half-filled
36
+ * dialog.
37
+ *
38
+ * Without rules the toasts pile into a wall that covers the bottom of the window. Three rules keep
39
+ * the stack short, and they are deliberately different per level:
40
+ *
41
+ * - an `info` toast is a receipt ("this write went out"). It has been read by the time the next
42
+ * thing happens, so it expires on its own after {@link INFO_TTL_MS}.
43
+ * - a `warn` gets {@link WARN_TTL_MS} - long enough to notice, short enough not to accumulate.
44
+ * - an `error` never expires. Something failed; the user decides when that is dealt with.
45
+ *
46
+ * The last rule is why the on-screen cap ({@link VISIBLE_NOTICES}) is a *view* and not a drop: an
47
+ * error must not be able to vanish behind five status messages, so what leaves the screen when the
48
+ * sixth toast arrives stays in `items` and is counted. As the info toasts expire, the errors
49
+ * underneath surface again on their own.
50
+ */
51
+ export declare class NoticesStore {
52
+ #private;
53
+ items: Notice[];
54
+ constructor(options?: NoticesStoreOptions);
55
+ /** The newest {@link VISIBLE_NOTICES} - what the toast stack draws. */
56
+ get visible(): Notice[];
57
+ /** How many are held but not on screen; the "n more" counter. */
58
+ get hidden(): number;
59
+ /** Adds a notice and returns its id. `source` names what it is about. */
60
+ push(level: NoticeLevel, message: string, source?: string): number;
61
+ dismiss(id: number): void;
62
+ clear(): void;
63
+ dispose(): void;
64
+ }
@@ -0,0 +1,127 @@
1
+ /** How many toasts are on screen at once; the rest collapse into a counter. */
2
+ export const VISIBLE_NOTICES = 5;
3
+ /** An informational toast is gone after this long, unless it was dismissed first. */
4
+ export const INFO_TTL_MS = 5_000;
5
+ /** A warning stays three times as long: it is worth reading, but not worth a click. */
6
+ export const WARN_TTL_MS = 15_000;
7
+ /**
8
+ * Everything the user should see but must not be interrupted by: a dismissable toast, and nothing
9
+ * steals focus. A modal alert would close whatever else is open and could throw away a half-filled
10
+ * dialog.
11
+ *
12
+ * Without rules the toasts pile into a wall that covers the bottom of the window. Three rules keep
13
+ * the stack short, and they are deliberately different per level:
14
+ *
15
+ * - an `info` toast is a receipt ("this write went out"). It has been read by the time the next
16
+ * thing happens, so it expires on its own after {@link INFO_TTL_MS}.
17
+ * - a `warn` gets {@link WARN_TTL_MS} - long enough to notice, short enough not to accumulate.
18
+ * - an `error` never expires. Something failed; the user decides when that is dealt with.
19
+ *
20
+ * The last rule is why the on-screen cap ({@link VISIBLE_NOTICES}) is a *view* and not a drop: an
21
+ * error must not be able to vanish behind five status messages, so what leaves the screen when the
22
+ * sixth toast arrives stays in `items` and is counted. As the info toasts expire, the errors
23
+ * underneath surface again on their own.
24
+ */
25
+ export class NoticesStore {
26
+ items = $state([]);
27
+ #max;
28
+ #now;
29
+ #infoTtlMs;
30
+ #warnTtlMs;
31
+ #hintFor;
32
+ /** The hints already shown in this session. Not reactive: nothing renders from it. */
33
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity -- see above
34
+ #hintsShown = new Set();
35
+ // Bookkeeping for the expiry timers, deliberately not reactive: nothing renders from it, and a
36
+ // SvelteMap here would invalidate the toast stack on every timer that starts or is cleared.
37
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity -- see above
38
+ #timers = new Map();
39
+ #nextId = 1;
40
+ constructor(options = {}) {
41
+ this.#max = options.max ?? 20;
42
+ this.#now = options.now ?? (() => Date.now());
43
+ this.#infoTtlMs = options.infoTtlMs ?? INFO_TTL_MS;
44
+ this.#warnTtlMs = options.warnTtlMs ?? WARN_TTL_MS;
45
+ this.#hintFor = options.hintFor;
46
+ }
47
+ /** The newest {@link VISIBLE_NOTICES} - what the toast stack draws. */
48
+ get visible() {
49
+ return this.items.slice(Math.max(0, this.items.length - VISIBLE_NOTICES));
50
+ }
51
+ /** How many are held but not on screen; the "n more" counter. */
52
+ get hidden() {
53
+ return Math.max(0, this.items.length - VISIBLE_NOTICES);
54
+ }
55
+ /** Adds a notice and returns its id. `source` names what it is about. */
56
+ push(level, message, source) {
57
+ const id = this.#nextId;
58
+ this.#nextId += 1;
59
+ const notice = {
60
+ id,
61
+ level,
62
+ message,
63
+ timestamp: this.#now(),
64
+ ...(source === undefined ? {} : { source }),
65
+ };
66
+ const items = [...this.items, notice];
67
+ const dropped = items.length > this.#max ? items.slice(0, items.length - this.#max) : [];
68
+ for (const gone of dropped) {
69
+ this.#stopTimer(gone.id);
70
+ }
71
+ this.items = dropped.length > 0 ? items.slice(dropped.length) : items;
72
+ this.#startTimer(id, level);
73
+ if (level !== 'info') {
74
+ this.#hint(message);
75
+ }
76
+ return id;
77
+ }
78
+ /** The hint a notice calls for, once per session; it stays until it is dismissed. */
79
+ #hint(message) {
80
+ const hint = this.#hintFor?.(message);
81
+ if (hint === undefined || hint === '' || this.#hintsShown.has(hint)) {
82
+ return;
83
+ }
84
+ this.#hintsShown.add(hint);
85
+ this.push('error', hint);
86
+ }
87
+ dismiss(id) {
88
+ this.#stopTimer(id);
89
+ this.items = this.items.filter((notice) => notice.id !== id);
90
+ }
91
+ clear() {
92
+ for (const id of [...this.#timers.keys()]) {
93
+ this.#stopTimer(id);
94
+ }
95
+ this.items = [];
96
+ }
97
+ dispose() {
98
+ this.clear();
99
+ }
100
+ /** The lifetime of a level, `0` for "until dismissed". */
101
+ #ttl(level) {
102
+ if (level === 'info') {
103
+ return this.#infoTtlMs;
104
+ }
105
+ return level === 'warn' ? this.#warnTtlMs : 0;
106
+ }
107
+ #startTimer(id, level) {
108
+ const ttl = this.#ttl(level);
109
+ if (ttl <= 0) {
110
+ return;
111
+ }
112
+ const timer = setTimeout(() => {
113
+ this.#timers.delete(id);
114
+ this.items = this.items.filter((notice) => notice.id !== id);
115
+ }, ttl);
116
+ // A toast must never be the reason a Node process (a jsdom test run) stays alive.
117
+ timer.unref?.();
118
+ this.#timers.set(id, timer);
119
+ }
120
+ #stopTimer(id) {
121
+ const timer = this.#timers.get(id);
122
+ if (timer !== undefined) {
123
+ clearTimeout(timer);
124
+ this.#timers.delete(id);
125
+ }
126
+ }
127
+ }
@@ -0,0 +1 @@
1
+ export * from './NoticesStore.svelte.js';
@@ -0,0 +1 @@
1
+ export * from './NoticesStore.svelte.js';
@@ -0,0 +1,11 @@
1
+ /** The two `localStorage` methods the kit uses; a test passes a `Map`-backed stub. */
2
+ export interface StorageLike {
3
+ getItem(key: string): string | null;
4
+ setItem(key: string, value: string): void;
5
+ }
6
+ /** `localStorage`, or `undefined` where the browser refuses it (blocked site data throws on the accessor). */
7
+ export declare function defaultStorage(): StorageLike | undefined;
8
+ /** A stored value, or `undefined`; never throws. */
9
+ export declare function readStored(storage: StorageLike | undefined, key: string): string | undefined;
10
+ /** Stores a value; a full or refused storage loses it on the next reload, and nothing else. */
11
+ export declare function writeStored(storage: StorageLike | undefined, key: string, value: string): void;
@@ -0,0 +1,27 @@
1
+ /** `localStorage`, or `undefined` where the browser refuses it (blocked site data throws on the accessor). */
2
+ export function defaultStorage() {
3
+ try {
4
+ return typeof localStorage === 'undefined' ? undefined : localStorage;
5
+ }
6
+ catch {
7
+ return undefined;
8
+ }
9
+ }
10
+ /** A stored value, or `undefined`; never throws. */
11
+ export function readStored(storage, key) {
12
+ try {
13
+ return storage?.getItem(key) ?? undefined;
14
+ }
15
+ catch {
16
+ return undefined;
17
+ }
18
+ }
19
+ /** Stores a value; a full or refused storage loses it on the next reload, and nothing else. */
20
+ export function writeStored(storage, key, value) {
21
+ try {
22
+ storage?.setItem(key, value);
23
+ }
24
+ catch {
25
+ // see above
26
+ }
27
+ }
@@ -0,0 +1,32 @@
1
+ import type { StorageLike } from '../storage.js';
2
+ import { type ColumnWidths } from './columnWidths.js';
3
+ import type { ColumnWidthPersistence } from './dataTableContext.js';
4
+ export interface LocalColumnWidthsOptions {
5
+ /** The `localStorage` key all widths are kept under (`myapp.columnWidths`). */
6
+ readonly storageKey: string;
7
+ /** The storage; `undefined` keeps the widths for as long as the store lives. */
8
+ readonly storage: StorageLike | undefined;
9
+ /**
10
+ * The scope a width belongs to - a connection profile, a user. Read on every access, so a
11
+ * scope that changes later (a configuration that arrives) switches the widths. One scope,
12
+ * `default`, when absent.
13
+ */
14
+ readonly scope?: () => string;
15
+ }
16
+ /**
17
+ * The column widths a user gave the tables, per scope and per table, kept in `localStorage`.
18
+ *
19
+ * A column width is a property of the screen it was dragged on - the same app is opened from a
20
+ * phone and from a desktop, and one width for both would be wrong on one of them - so it lives in
21
+ * the browser, not on a server. Keyed by a scope (a connection profile, say), so a scope switched to
22
+ * another target starts from the designed widths and gets its own back when it is switched back.
23
+ */
24
+ export declare class LocalColumnWidths implements ColumnWidthPersistence {
25
+ #private;
26
+ constructor(options: LocalColumnWidthsOptions);
27
+ /** The widths of one table in the current scope; empty when the user has not resized any. */
28
+ widths(tableId: string): ColumnWidths;
29
+ set(tableId: string, key: string, width: number): void;
30
+ /** Back to the designed widths - every column of this table, in this scope only. */
31
+ reset(tableId: string): void;
32
+ }
@@ -0,0 +1,91 @@
1
+ import { clampColumnWidth, sanitizeColumnWidths } from './columnWidths.js';
2
+ const NO_WIDTHS = Object.freeze({});
3
+ function isRecord(value) {
4
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
5
+ }
6
+ /** Whatever is in the storage, reduced to what this store can use; nothing at all when it is garbage. */
7
+ function readStored(storage, storageKey) {
8
+ let parsed;
9
+ try {
10
+ const raw = storage?.getItem(storageKey);
11
+ parsed = raw == null ? undefined : JSON.parse(raw);
12
+ }
13
+ catch {
14
+ return {};
15
+ }
16
+ const stored = {};
17
+ if (!isRecord(parsed)) {
18
+ return stored;
19
+ }
20
+ for (const [profile, tables] of Object.entries(parsed)) {
21
+ if (!isRecord(tables)) {
22
+ continue;
23
+ }
24
+ const kept = {};
25
+ for (const [tableId, widths] of Object.entries(tables)) {
26
+ const clean = sanitizeColumnWidths(widths);
27
+ if (Object.keys(clean).length > 0) {
28
+ kept[tableId] = clean;
29
+ }
30
+ }
31
+ if (Object.keys(kept).length > 0) {
32
+ stored[profile] = kept;
33
+ }
34
+ }
35
+ return stored;
36
+ }
37
+ /**
38
+ * The column widths a user gave the tables, per scope and per table, kept in `localStorage`.
39
+ *
40
+ * A column width is a property of the screen it was dragged on - the same app is opened from a
41
+ * phone and from a desktop, and one width for both would be wrong on one of them - so it lives in
42
+ * the browser, not on a server. Keyed by a scope (a connection profile, say), so a scope switched to
43
+ * another target starts from the designed widths and gets its own back when it is switched back.
44
+ */
45
+ export class LocalColumnWidths {
46
+ #stored = $state.raw({});
47
+ #storage;
48
+ #storageKey;
49
+ #profile;
50
+ constructor(options) {
51
+ this.#storage = options.storage;
52
+ this.#storageKey = options.storageKey;
53
+ this.#profile = options.scope ?? (() => 'default');
54
+ this.#stored = readStored(options.storage, options.storageKey);
55
+ }
56
+ /** The widths of one table in the current scope; empty when the user has not resized any. */
57
+ widths(tableId) {
58
+ return this.#stored[this.#profile()]?.[tableId] ?? NO_WIDTHS;
59
+ }
60
+ set(tableId, key, width) {
61
+ const profile = this.#profile();
62
+ const tables = this.#stored[profile] ?? {};
63
+ const widths = tables[tableId] ?? {};
64
+ const clamped = clampColumnWidth(width);
65
+ if (widths[key] === clamped) {
66
+ return;
67
+ }
68
+ this.#stored = { ...this.#stored, [profile]: { ...tables, [tableId]: { ...widths, [key]: clamped } } };
69
+ this.#write();
70
+ }
71
+ /** Back to the designed widths - every column of this table, in this scope only. */
72
+ reset(tableId) {
73
+ const profile = this.#profile();
74
+ const tables = this.#stored[profile];
75
+ if (tables?.[tableId] === undefined) {
76
+ return;
77
+ }
78
+ const rest = Object.fromEntries(Object.entries(tables).filter(([id]) => id !== tableId));
79
+ const others = Object.fromEntries(Object.entries(this.#stored).filter(([name]) => name !== profile));
80
+ this.#stored = Object.keys(rest).length === 0 ? others : { ...others, [profile]: rest };
81
+ this.#write();
82
+ }
83
+ #write() {
84
+ try {
85
+ this.#storage?.setItem(this.#storageKey, JSON.stringify(this.#stored));
86
+ }
87
+ catch {
88
+ // A full or refused storage loses the width on the next reload, and nothing else.
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The two things the grid has to ask the browser about a cell: is its text cut
3
+ * off, and how wide would it be if it were not. Both only ever run for the cells at hand - the one
4
+ * under the pointer, the rendered window of one column - never for every row of a table.
5
+ */
6
+ /** Marks an element that must not count when a cell is measured - the resize handle. */
7
+ export declare const MEASURE_SKIP_ATTRIBUTE = "data-measure-skip";
8
+ /**
9
+ * Is the browser drawing an ellipsis? A cell clips (`overflow: hidden`) and does not wrap, so its
10
+ * content is wider than its box exactly when some of it is not shown.
11
+ */
12
+ export declare function isTruncated(element: HTMLElement): boolean;
13
+ /** The text of a cell as one line - what its tooltip says. */
14
+ export declare function fullText(element: HTMLElement): string;
15
+ /**
16
+ * The natural width of each element - its content on one line plus its own padding - in the order
17
+ * given.
18
+ *
19
+ * The elements are cloned into one hidden `max-content` box inside `host`, so that the clones keep
20
+ * the classes, the scoped styles and the inherited font of the originals while nothing clips them,
21
+ * then measured in one layout pass and removed again before anything is painted. The originals
22
+ * are not touched: the table does not reflow under the user's pointer.
23
+ */
24
+ export declare function measureNaturalWidths(host: HTMLElement, elements: readonly HTMLElement[]): number[];
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The two things the grid has to ask the browser about a cell: is its text cut
3
+ * off, and how wide would it be if it were not. Both only ever run for the cells at hand - the one
4
+ * under the pointer, the rendered window of one column - never for every row of a table.
5
+ */
6
+ /** Marks an element that must not count when a cell is measured - the resize handle. */
7
+ export const MEASURE_SKIP_ATTRIBUTE = 'data-measure-skip';
8
+ /**
9
+ * Is the browser drawing an ellipsis? A cell clips (`overflow: hidden`) and does not wrap, so its
10
+ * content is wider than its box exactly when some of it is not shown.
11
+ */
12
+ export function isTruncated(element) {
13
+ return element.scrollWidth > element.clientWidth;
14
+ }
15
+ /** The text of a cell as one line - what its tooltip says. */
16
+ export function fullText(element) {
17
+ return (element.innerText || element.textContent || '').replace(/\s+/g, ' ').trim();
18
+ }
19
+ /**
20
+ * The natural width of each element - its content on one line plus its own padding - in the order
21
+ * given.
22
+ *
23
+ * The elements are cloned into one hidden `max-content` box inside `host`, so that the clones keep
24
+ * the classes, the scoped styles and the inherited font of the originals while nothing clips them,
25
+ * then measured in one layout pass and removed again before anything is painted. The originals
26
+ * are not touched: the table does not reflow under the user's pointer.
27
+ */
28
+ export function measureNaturalWidths(host, elements) {
29
+ if (elements.length === 0) {
30
+ return [];
31
+ }
32
+ const box = document.createElement('div');
33
+ box.setAttribute('aria-hidden', 'true');
34
+ Object.assign(box.style, {
35
+ position: 'absolute',
36
+ left: '0',
37
+ top: '0',
38
+ width: 'max-content',
39
+ height: '0',
40
+ overflow: 'visible',
41
+ visibility: 'hidden',
42
+ pointerEvents: 'none',
43
+ });
44
+ const clones = elements.map((element) => {
45
+ // Read before any clone is in the document, so the styles are resolved once for all of them.
46
+ const style = getComputedStyle(element);
47
+ const clone = element.cloneNode(true);
48
+ for (const skipped of clone.querySelectorAll(`[${MEASURE_SKIP_ATTRIBUTE}]`)) {
49
+ skipped.remove();
50
+ }
51
+ // a clone must never be what a test - or an assistive technology - finds
52
+ for (const node of [clone, ...clone.querySelectorAll('[data-testid], [id]')]) {
53
+ node.removeAttribute('data-testid');
54
+ node.removeAttribute('id');
55
+ }
56
+ Object.assign(clone.style, {
57
+ display: 'block',
58
+ position: 'static',
59
+ width: 'max-content',
60
+ minWidth: '0',
61
+ maxWidth: 'none',
62
+ height: 'auto',
63
+ overflow: 'visible',
64
+ // The clone is measured outside the row it came from, so what it inherited
65
+ // there - the bold of a sub-grid's label row - is given to it directly.
66
+ fontFamily: style.fontFamily,
67
+ fontSize: style.fontSize,
68
+ fontStyle: style.fontStyle,
69
+ fontWeight: style.fontWeight,
70
+ letterSpacing: style.letterSpacing,
71
+ });
72
+ box.append(clone);
73
+ return clone;
74
+ });
75
+ host.append(box);
76
+ try {
77
+ return clones.map((clone) => clone.getBoundingClientRect().width);
78
+ }
79
+ finally {
80
+ box.remove();
81
+ }
82
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The widths a user gives the columns of a grid.
3
+ *
4
+ * "In den Spalten Räume / Gewerke / RX-Mode sind die Namen teilweise abgeschnitten. Es wäre schön,
5
+ * wenn die Spaltenbreite anpassbar wäre" - and a few minutes later, "das Problem besteht wohl in
6
+ * allen Spalten". So every text column of the shared `DataTable` has a drag handle on the right edge
7
+ * of its label, a double click on that handle fits the column to what it shows, and the result is
8
+ * kept per table.
9
+ *
10
+ * A width set here is a pixel width that replaces the column's proportional weight
11
+ * ({@link tableLayout}); every column the user has not touched keeps its weight and shares the rest.
12
+ * The arithmetic - the limits and what "fit" means for a set of measured cells - is here, without
13
+ * a DOM, so it is tested directly.
14
+ */
15
+ import type { DataTableColumn } from './tableModel.js';
16
+ /** Column key -> width in CSS pixels, for one table. */
17
+ export type ColumnWidths = Readonly<Record<string, number>>;
18
+ /**
19
+ * The narrowest a user can drag a column. The cell's padding is 6 px on each side and the handle
20
+ * sits in the right one, which leaves room for two or three characters and the ellipsis - enough to
21
+ * see that there is something in it, and the tooltip says what.
22
+ */
23
+ export declare const MIN_COLUMN_WIDTH = 40;
24
+ /** The widest a drag or a stored value may make one column. */
25
+ export declare const MAX_COLUMN_WIDTH = 1200;
26
+ /**
27
+ * The widest a double click makes a column. A FLAGS or a firmware cell with a long status would
28
+ * otherwise push every other column out of the window for one row's sake.
29
+ */
30
+ export declare const FIT_MAX_WIDTH = 600;
31
+ /** One arrow key press on a focused handle. */
32
+ export declare const RESIZE_KEY_STEP = 10;
33
+ /** Pixels, whole ones, between the limits; anything that is not a number is the minimum. */
34
+ export declare function clampColumnWidth(width: number, min?: number, max?: number): number;
35
+ /**
36
+ * What a double click on the handle makes of a column: as wide as the widest of the measured
37
+ * natural widths - the label and every rendered cell - rounded up so that a fractional pixel does
38
+ * not bring the ellipsis back, and kept between the minimum and {@link FIT_MAX_WIDTH}.
39
+ *
40
+ * `undefined` when nothing was measured: a table that is not on the screen has nothing to fit to,
41
+ * and a guess would be stored as if the user had chosen it.
42
+ */
43
+ export declare function fitColumnWidth(naturalWidths: readonly number[], min?: number, max?: number): number | undefined;
44
+ /**
45
+ * Picture and marker columns keep their pixels (`fixed`): they have nothing more to say when they
46
+ * are wider, and their size is computed from the picture. A column of buttons is resizable
47
+ * down to its own minimum instead ({@link minimumColumnWidth}).
48
+ */
49
+ export declare function isResizable<T>(column: DataTableColumn<T>): boolean;
50
+ /**
51
+ * The narrowest a user can make this column: {@link MIN_COLUMN_WIDTH}, or the column's own
52
+ * `minWidth` when it keeps it (`keepMinWidth`, the PARAMSETS buttons, which a narrower column
53
+ * put under the next cell).
54
+ */
55
+ export declare function minimumColumnWidth<T>(column: DataTableColumn<T>): number;
56
+ /**
57
+ * The widths of one table from untrusted JSON - `localStorage` is edited by hand, and by older and
58
+ * newer versions of this app. Keeps the finite numbers, clamped; drops everything else.
59
+ */
60
+ export declare function sanitizeColumnWidths(value: unknown): Record<string, number>;
@@ -0,0 +1,86 @@
1
+ /**
2
+ * The widths a user gives the columns of a grid.
3
+ *
4
+ * "In den Spalten Räume / Gewerke / RX-Mode sind die Namen teilweise abgeschnitten. Es wäre schön,
5
+ * wenn die Spaltenbreite anpassbar wäre" - and a few minutes later, "das Problem besteht wohl in
6
+ * allen Spalten". So every text column of the shared `DataTable` has a drag handle on the right edge
7
+ * of its label, a double click on that handle fits the column to what it shows, and the result is
8
+ * kept per table.
9
+ *
10
+ * A width set here is a pixel width that replaces the column's proportional weight
11
+ * ({@link tableLayout}); every column the user has not touched keeps its weight and shares the rest.
12
+ * The arithmetic - the limits and what "fit" means for a set of measured cells - is here, without
13
+ * a DOM, so it is tested directly.
14
+ */
15
+ /**
16
+ * The narrowest a user can drag a column. The cell's padding is 6 px on each side and the handle
17
+ * sits in the right one, which leaves room for two or three characters and the ellipsis - enough to
18
+ * see that there is something in it, and the tooltip says what.
19
+ */
20
+ export const MIN_COLUMN_WIDTH = 40;
21
+ /** The widest a drag or a stored value may make one column. */
22
+ export const MAX_COLUMN_WIDTH = 1200;
23
+ /**
24
+ * The widest a double click makes a column. A FLAGS or a firmware cell with a long status would
25
+ * otherwise push every other column out of the window for one row's sake.
26
+ */
27
+ export const FIT_MAX_WIDTH = 600;
28
+ /** One arrow key press on a focused handle. */
29
+ export const RESIZE_KEY_STEP = 10;
30
+ /** Pixels, whole ones, between the limits; anything that is not a number is the minimum. */
31
+ export function clampColumnWidth(width, min = MIN_COLUMN_WIDTH, max = MAX_COLUMN_WIDTH) {
32
+ if (!Number.isFinite(width)) {
33
+ return min;
34
+ }
35
+ return Math.round(Math.min(Math.max(width, min), Math.max(min, max)));
36
+ }
37
+ /**
38
+ * What a double click on the handle makes of a column: as wide as the widest of the measured
39
+ * natural widths - the label and every rendered cell - rounded up so that a fractional pixel does
40
+ * not bring the ellipsis back, and kept between the minimum and {@link FIT_MAX_WIDTH}.
41
+ *
42
+ * `undefined` when nothing was measured: a table that is not on the screen has nothing to fit to,
43
+ * and a guess would be stored as if the user had chosen it.
44
+ */
45
+ export function fitColumnWidth(naturalWidths, min = MIN_COLUMN_WIDTH, max = FIT_MAX_WIDTH) {
46
+ const measured = naturalWidths.filter((width) => Number.isFinite(width) && width > 0);
47
+ if (measured.length === 0) {
48
+ return undefined;
49
+ }
50
+ return clampColumnWidth(Math.ceil(Math.max(...measured)), min, max);
51
+ }
52
+ /**
53
+ * Picture and marker columns keep their pixels (`fixed`): they have nothing more to say when they
54
+ * are wider, and their size is computed from the picture. A column of buttons is resizable
55
+ * down to its own minimum instead ({@link minimumColumnWidth}).
56
+ */
57
+ export function isResizable(column) {
58
+ return column.fixed !== true && column.hidden !== true;
59
+ }
60
+ /**
61
+ * The narrowest a user can make this column: {@link MIN_COLUMN_WIDTH}, or the column's own
62
+ * `minWidth` when it keeps it (`keepMinWidth`, the PARAMSETS buttons, which a narrower column
63
+ * put under the next cell).
64
+ */
65
+ export function minimumColumnWidth(column) {
66
+ if (column.keepMinWidth !== true || column.minWidth === undefined) {
67
+ return MIN_COLUMN_WIDTH;
68
+ }
69
+ return Math.max(MIN_COLUMN_WIDTH, Math.round(column.minWidth));
70
+ }
71
+ /**
72
+ * The widths of one table from untrusted JSON - `localStorage` is edited by hand, and by older and
73
+ * newer versions of this app. Keeps the finite numbers, clamped; drops everything else.
74
+ */
75
+ export function sanitizeColumnWidths(value) {
76
+ const widths = {};
77
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
78
+ return widths;
79
+ }
80
+ for (const [key, width] of Object.entries(value)) {
81
+ if (typeof width === 'number' && Number.isFinite(width)) {
82
+ widths[key] = clampColumnWidth(width);
83
+ }
84
+ }
85
+ return widths;
86
+ }
@@ -0,0 +1,26 @@
1
+ import type { MessageParams } from '../i18n/translator.js';
2
+ import type { ColumnWidths } from './columnWidths.js';
3
+ /**
4
+ * What a `DataTable` takes from the app around it rather than from each page.
5
+ *
6
+ * Every table of an app has the same column handles and the same header menu, so the store the
7
+ * widths are kept in and the translation of the table's own chrome are set once, at the app's
8
+ * root, instead of being threaded through every page. A table rendered without it keeps its widths
9
+ * for as long as it lives and speaks the kit's catalogue (`occulite-ui/i18n`).
10
+ */
11
+ export interface ColumnWidthPersistence {
12
+ /** The widths of one table; reactive where the implementation is. */
13
+ widths(tableId: string): ColumnWidths;
14
+ set(tableId: string, key: string, width: number): void;
15
+ reset(tableId: string): void;
16
+ }
17
+ export interface DataTableEnvironment {
18
+ readonly columnWidths?: ColumnWidthPersistence | undefined;
19
+ readonly t?: ((key: string, params?: MessageParams) => string) | undefined;
20
+ }
21
+ /** The context key; exported for a component test's `render(..., {context})`. */
22
+ export declare const DATA_TABLE_KEY: unique symbol;
23
+ export declare function setDataTableEnvironment(environment: DataTableEnvironment): void;
24
+ export declare function getDataTableEnvironment(): DataTableEnvironment | undefined;
25
+ /** The key itself with its placeholders filled in. */
26
+ export declare function untranslated(key: string, params?: MessageParams): string;
@@ -0,0 +1,16 @@
1
+ import { getContext, hasContext, setContext } from 'svelte';
2
+ /** The context key; exported for a component test's `render(..., {context})`. */
3
+ export const DATA_TABLE_KEY = Symbol('occulite-ui.data-table');
4
+ export function setDataTableEnvironment(environment) {
5
+ setContext(DATA_TABLE_KEY, environment);
6
+ }
7
+ export function getDataTableEnvironment() {
8
+ return hasContext(DATA_TABLE_KEY) ? getContext(DATA_TABLE_KEY) : undefined;
9
+ }
10
+ /** The key itself with its placeholders filled in. */
11
+ export function untranslated(key, params = {}) {
12
+ return key.replace(/\{(\w+)\}/g, (match, name) => {
13
+ const value = params[name];
14
+ return value === undefined ? match : String(value);
15
+ });
16
+ }
@@ -0,0 +1,6 @@
1
+ export * from './tableModel.js';
2
+ export * from './columnWidths.js';
3
+ export * from './columnMeasure.js';
4
+ export * from './metrics.js';
5
+ export * from './dataTableContext.js';
6
+ export * from './LocalColumnWidths.svelte.js';