@classic-homes/files-svelte 0.6.1 → 0.6.2

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 CHANGED
@@ -66,6 +66,11 @@ folder, or containing `/`) clears the binding once that is certain:
66
66
  `onerror` instead. In pane mode `onnavigate` also fires when a file opens, changes
67
67
  or closes, with `file` set; `onfilechange` reports it in both modes.
68
68
 
69
+ A link without `rootId` opens on the first drive the caller can read and keeps its
70
+ `path` and `file`: the `onnavigate` for that pick carries the pending `file` (pane
71
+ mode), so a URL rebuilt from it keeps the document. An unknown `rootId` also falls
72
+ back to the first drive, but at its top with no file open.
73
+
69
74
  ```svelte
70
75
  <FileBrowser
71
76
  {source}
@@ -18,7 +18,7 @@
18
18
  TREE_CLASS,
19
19
  TREE_NOTE_CLASS,
20
20
  } from './tree-rows.js';
21
- import { cn, splitNameTail } from './utils.js';
21
+ import { cn, nameClipped, splitNameTail } from './utils.js';
22
22
 
23
23
  interface Props {
24
24
  /** Label of the drive (level 1). */
@@ -184,6 +184,71 @@
184
184
  if (current !== undefined) el(current)?.scrollIntoView({ block: 'nearest' });
185
185
  });
186
186
 
187
+ /*
188
+ * Middle truncation: the "…" shows only while a name's head is clipped
189
+ * (`data-clipped` on its `[data-name-split]`). One ResizeObserver serves the
190
+ * whole branch: it watches each head (the room it has) and its text (the
191
+ * width it wants), and a frame later measures every name that changed in one
192
+ * pass, reads before writes. Writing in the observer callback would resize
193
+ * observed elements mid-delivery and raise "ResizeObserver loop" errors.
194
+ */
195
+ let clipObserver: ResizeObserver | undefined;
196
+ const clipPending = new Set<HTMLElement>();
197
+ let clipFrame = 0;
198
+
199
+ function measureClips() {
200
+ clipFrame = 0;
201
+ const results: [HTMLElement, boolean][] = [];
202
+ for (const split of clipPending) {
203
+ if (!split.isConnected) continue;
204
+ const head = split.querySelector<HTMLElement>('[data-name-head]');
205
+ const text = split.querySelector<HTMLElement>('[data-name-text]');
206
+ const dots = split.querySelector<HTMLElement>('[data-name-ellipsis]');
207
+ if (!head || !text) continue;
208
+ const shown = split.hasAttribute('data-clipped');
209
+ results.push([
210
+ split,
211
+ nameClipped(
212
+ text.getBoundingClientRect().width,
213
+ head.getBoundingClientRect().width,
214
+ shown && dots ? dots.getBoundingClientRect().width : 0
215
+ ),
216
+ ]);
217
+ }
218
+ clipPending.clear();
219
+ for (const [split, clipped] of results) split.toggleAttribute('data-clipped', clipped);
220
+ }
221
+
222
+ function watchClip(split: HTMLElement) {
223
+ if (typeof ResizeObserver === 'undefined') return; // no layout (SSR, jsdom): plain text
224
+ clipObserver ??= new ResizeObserver((records) => {
225
+ for (const record of records) {
226
+ const owner = (record.target as HTMLElement).closest<HTMLElement>('[data-name-split]');
227
+ if (owner) clipPending.add(owner);
228
+ }
229
+ if (clipPending.size && !clipFrame) clipFrame = requestAnimationFrame(measureClips);
230
+ });
231
+ const targets = [
232
+ split.querySelector<HTMLElement>('[data-name-head]'),
233
+ split.querySelector<HTMLElement>('[data-name-text]'),
234
+ ].filter((t): t is HTMLElement => !!t);
235
+ for (const t of targets) clipObserver.observe(t);
236
+ return {
237
+ destroy() {
238
+ for (const t of targets) clipObserver?.unobserve(t);
239
+ clipPending.delete(split);
240
+ },
241
+ };
242
+ }
243
+
244
+ $effect(() => () => {
245
+ clipObserver?.disconnect();
246
+ clipObserver = undefined;
247
+ if (clipFrame) cancelAnimationFrame(clipFrame);
248
+ clipFrame = 0;
249
+ clipPending.clear();
250
+ });
251
+
187
252
  /** "Load more" clicked: move to the first new row once it lands (the button may go). */
188
253
  let focusFrom = $state<number>();
189
254
  function loadMoreClicked() {
@@ -242,12 +307,14 @@
242
307
  <FileIcon name={row.entry.name} type={row.entry.type} size={16} />
243
308
  {/if}
244
309
  {#if split}
245
- <!-- Middle truncation: the head takes the ellipsis, the tail (…-FHN.xlsx) stays.
246
- Screen readers get the name once, whole. -->
247
- <span class="flex min-w-0" aria-hidden="true" data-name-split>
248
- <span class="min-w-0 truncate">{split.head}</span><span
249
- class="shrink-0 whitespace-nowrap">{split.tail}</span
250
- >
310
+ <!-- Middle truncation: the head clips, our own "…" (only while it is clipped)
311
+ joins it tight to the tail (…-FHN.xlsx), which stays. A CSS ellipsis would
312
+ snap to whole glyphs and leave a gap. Screen readers get the name once, whole. -->
313
+ <span class="group flex min-w-0" aria-hidden="true" data-name-split use:watchClip>
314
+ <span class="min-w-0 overflow-hidden whitespace-nowrap" data-name-head
315
+ ><span class="inline-block w-max" data-name-text>{split.head}</span></span
316
+ ><span class="hidden shrink-0 group-data-[clipped]:inline" data-name-ellipsis>…</span
317
+ ><span class="shrink-0 whitespace-nowrap" data-name-tail>{split.tail}</span>
251
318
  </span>
252
319
  <span class="sr-only">{row.name}</span>
253
320
  {:else}
@@ -44,7 +44,11 @@
44
44
  interface Props {
45
45
  /** Where the data comes from: `createChapiFilesSource()`, `createMemoryFilesSource()` or your own. */
46
46
  source: FilesSource;
47
- /** Current root. Bindable; defaults to the first root the caller can read. */
47
+ /**
48
+ * Current root. Bindable; defaults to the first root the caller can read,
49
+ * keeping `path` and `file` (a deep link without a drive). An unknown root
50
+ * also falls back to the first one, but starts at its top with no file open.
51
+ */
48
52
  rootId?: string;
49
53
  /** Current folder, root-relative (`''` = the root). Bindable. */
50
54
  path?: string;
@@ -265,10 +269,25 @@
265
269
  const current = untrack(() => rootId);
266
270
  if (list.length && !list.some((r) => r.id === current)) {
267
271
  // Unknown or missing root: start on the first one the caller can read.
268
- rootId = list[0].id;
269
- if (current !== undefined) path = '';
270
- onrootchange?.(list[0].id);
271
- onnavigate?.({ rootId: list[0].id, path: untrack(() => currentPath) });
272
+ const first = list[0].id;
273
+ if (current !== undefined) {
274
+ // An unknown drive: its folder (and so its file) means nothing here.
275
+ path = '';
276
+ if (untrack(() => file) !== undefined) {
277
+ file = undefined;
278
+ onfilechange?.(undefined);
279
+ }
280
+ }
281
+ rootId = first;
282
+ onrootchange?.(first);
283
+ // No drive given is not a drive change: a deep link's `path` and
284
+ // `file` stay, and the reported location keeps the pending file.
285
+ const pending = untrack(() => file);
286
+ onnavigate?.(
287
+ preview === 'pane' && pending !== undefined
288
+ ? { rootId: first, path: untrack(() => currentPath), file: pending }
289
+ : { rootId: first, path: untrack(() => currentPath) }
290
+ );
272
291
  }
273
292
  },
274
293
  (err) => {
@@ -467,7 +486,9 @@
467
486
 
468
487
  // `file` from outside (a deep link, Back/Forward): open or close to match. A
469
488
  // location change closes the open file; a new `file` then resolves in the new folder.
489
+ // Picking the first drive when none was given is not a change: nothing was open.
470
490
  let lastLocation: string | undefined;
491
+ let lastRootId: string | undefined;
471
492
  let resolveController: AbortController | undefined;
472
493
  $effect(() => {
473
494
  const want = file;
@@ -476,8 +497,10 @@
476
497
  const listed = listedKey;
477
498
  const key = locationKey(id, folder);
478
499
  untrack(() => {
479
- if (lastLocation !== undefined && key !== lastLocation) hidePreview();
500
+ if (lastLocation !== undefined && lastRootId !== undefined && key !== lastLocation)
501
+ hidePreview();
480
502
  lastLocation = key;
503
+ lastRootId = id;
481
504
  const openName = previewOpen ? previewEntry?.name : undefined;
482
505
  if (want === openName) return;
483
506
  resolveController?.abort();
@@ -714,7 +737,8 @@
714
737
  // The drive picker, only where no folder tree lists the drives.
715
738
  const showRootPicker = $derived(roots.length > 1 && !navigationPane);
716
739
  /** Phones: the header's secondary controls, behind one ⋯ button. */
717
- // The component's own item type: the package's exported `DropdownMenuItem` lacks `checked`.
740
+ // The component's own item type, not the exported `DropdownMenuItem`: that one lacks
741
+ // `checked` up to theme-svelte 1.50.1, and this package accepts any theme-svelte (`*`).
718
742
  const moreItems = $derived<ComponentProps<typeof DropdownMenu>['items']>([
719
743
  {
720
744
  label: 'Show hidden files',
@@ -5,7 +5,11 @@ import type { FileBrowserContext } from './types.js';
5
5
  interface Props {
6
6
  /** Where the data comes from: `createChapiFilesSource()`, `createMemoryFilesSource()` or your own. */
7
7
  source: FilesSource;
8
- /** Current root. Bindable; defaults to the first root the caller can read. */
8
+ /**
9
+ * Current root. Bindable; defaults to the first root the caller can read,
10
+ * keeping `path` and `file` (a deep link without a drive). An unknown root
11
+ * also falls back to the first one, but starts at its top with no file open.
12
+ */
9
13
  rootId?: string;
10
14
  /** Current folder, root-relative (`''` = the root). Bindable. */
11
15
  path?: string;
@@ -15,7 +15,15 @@ export declare function downloadEntry(source: FilesSource, rootId: string, entry
15
15
  /** True when a key event comes from a place that types text. */
16
16
  export declare function isTextInput(target: EventTarget | null): boolean;
17
17
  /**
18
- * Split a file name for middle truncation: the head shrinks (ellipsis), the
18
+ * Whether a middle-truncated name's head is clipped: its text (`textWidth`, the
19
+ * natural width) is wider than the room it has. While the "…" shows it takes
20
+ * `ellipsisWidth` of that room, so pass it then: the name only goes back to
21
+ * plain text once it fits without the "…" (no flip-flopping at the boundary).
22
+ * Half a pixel of slack absorbs subpixel layout.
23
+ */
24
+ export declare function nameClipped(textWidth: number, boxWidth: number, ellipsisWidth?: number): boolean;
25
+ /**
26
+ * Split a file name for middle truncation: the head shrinks (clipped, with a "…" after it), the
19
27
  * tail always shows. The tail is the extension (from the last dot, unless the
20
28
  * dot leads the name or the extension is over 10 characters) plus the 6
21
29
  * characters before it, never splitting a character (grapheme). The split moves
package/dist/lib/utils.js CHANGED
@@ -56,7 +56,17 @@ function graphemes(text) {
56
56
  return Array.from(text); // code points: never splits a surrogate pair
57
57
  }
58
58
  /**
59
- * Split a file name for middle truncation: the head shrinks (ellipsis), the
59
+ * Whether a middle-truncated name's head is clipped: its text (`textWidth`, the
60
+ * natural width) is wider than the room it has. While the "…" shows it takes
61
+ * `ellipsisWidth` of that room, so pass it then: the name only goes back to
62
+ * plain text once it fits without the "…" (no flip-flopping at the boundary).
63
+ * Half a pixel of slack absorbs subpixel layout.
64
+ */
65
+ export function nameClipped(textWidth, boxWidth, ellipsisWidth = 0) {
66
+ return textWidth > boxWidth + ellipsisWidth + 0.5;
67
+ }
68
+ /**
69
+ * Split a file name for middle truncation: the head shrinks (clipped, with a "…" after it), the
60
70
  * tail always shows. The tail is the extension (from the last dot, unless the
61
71
  * dot leads the name or the extension is over 10 characters) plus the 6
62
72
  * characters before it, never splitting a character (grapheme). The split moves
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/files-svelte",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "Svelte network-drive file browser for Classic Theme: FileBrowser plus FileList, FileBreadcrumbs, FileIcon, FilePreview and FileDetails, with previews through the document viewers",
5
5
  "type": "module",
6
6
  "svelte": "./dist/lib/index.js",