@classic-homes/files-svelte 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 +49 -1
- package/dist/lib/FileBranch.svelte +278 -0
- package/dist/lib/FileBranch.svelte.d.ts +39 -0
- package/dist/lib/FileBrowser.svelte +414 -83
- package/dist/lib/FileBrowser.svelte.d.ts +21 -2
- package/dist/lib/FileList.svelte +103 -62
- package/dist/lib/FileList.svelte.d.ts +7 -0
- package/dist/lib/FilePreview.svelte +110 -11
- package/dist/lib/FilePreview.svelte.d.ts +14 -1
- package/dist/lib/FileTree.svelte +13 -18
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +1 -1
- package/dist/lib/tree-rows.d.ts +18 -0
- package/dist/lib/tree-rows.js +26 -0
- package/dist/lib/utils.d.ts +18 -2
- package/dist/lib/utils.js +56 -3
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row styling shared by the folder tree (FileTree) and the open document's
|
|
3
|
+
* branch (FileBranch), so the left pane looks the same with or without a file
|
|
4
|
+
* open.
|
|
5
|
+
*/
|
|
6
|
+
import { cn } from './utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* The tree itself. Positioned, so absolutely positioned sr-only text in its rows
|
|
9
|
+
* scrolls with the pane instead of stretching the page past the scroll container.
|
|
10
|
+
*/
|
|
11
|
+
export const TREE_CLASS = 'relative select-none py-1 text-sm';
|
|
12
|
+
/** Left padding of a treeitem at `level` (1 = a drive). */
|
|
13
|
+
export const treeIndent = (level) => `${(level - 1) * 0.875 + 0.25}rem`;
|
|
14
|
+
/** Left padding of a note row (Loading…, empty) under items at `level`. */
|
|
15
|
+
export const noteIndent = (level) => `${level * 0.875 + 0.5}rem`;
|
|
16
|
+
export const TREE_NOTE_CLASS = 'py-1 text-xs text-muted-foreground';
|
|
17
|
+
/** A treeitem: `current` is the highlighted row (open folder, or open file). */
|
|
18
|
+
export function treeRowClass(opts) {
|
|
19
|
+
return cn('flex items-center gap-1 rounded-sm py-1 pr-2 outline-none', 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring', opts.current ? 'bg-chart-1/15 font-medium text-foreground' : 'hover:bg-muted/60', opts.disabled ? 'cursor-not-allowed text-muted-foreground' : 'cursor-pointer',
|
|
20
|
+
// Hidden entries: muted and italic, not faded (opacity fails contrast).
|
|
21
|
+
opts.hidden && 'italic text-muted-foreground');
|
|
22
|
+
}
|
|
23
|
+
/** The chevron slot before the icon; `leaf` keeps its space but hides it. */
|
|
24
|
+
export function treeChevronClass(leaf) {
|
|
25
|
+
return cn('flex h-5 w-5 shrink-0 items-center justify-center rounded text-muted-foreground hover:bg-muted', leaf && 'invisible');
|
|
26
|
+
}
|
package/dist/lib/utils.d.ts
CHANGED
|
@@ -2,8 +2,11 @@ import { type ClassValue } from 'clsx';
|
|
|
2
2
|
import { type FileEntry, type FilesSource } from '@classic-homes/files-core';
|
|
3
3
|
export declare function cn(...inputs: ClassValue[]): string;
|
|
4
4
|
/**
|
|
5
|
-
* Download an entry
|
|
6
|
-
* the
|
|
5
|
+
* Download an entry and save it under its name. Pass an already-loaded Blob
|
|
6
|
+
* to skip the request. Otherwise a source with `downloadUrl()` (CHAPI) hands
|
|
7
|
+
* the browser a short-lived link, so the file streams to disk instead of
|
|
8
|
+
* sitting in memory; without it, or if the link can't be had, the bytes come
|
|
9
|
+
* through `read({ download: true })`, which sends auth.
|
|
7
10
|
*/
|
|
8
11
|
export declare function downloadEntry(source: FilesSource, rootId: string, entry: FileEntry, options?: {
|
|
9
12
|
blob?: Blob;
|
|
@@ -11,3 +14,16 @@ export declare function downloadEntry(source: FilesSource, rootId: string, entry
|
|
|
11
14
|
}): Promise<void>;
|
|
12
15
|
/** True when a key event comes from a place that types text. */
|
|
13
16
|
export declare function isTextInput(target: EventTarget | null): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Split a file name for middle truncation: the head shrinks (ellipsis), the
|
|
19
|
+
* tail always shows. The tail is the extension (from the last dot, unless the
|
|
20
|
+
* dot leads the name or the extension is over 10 characters) plus the 6
|
|
21
|
+
* characters before it, never splitting a character (grapheme). The split moves
|
|
22
|
+
* left off whitespace, so neither part starts or ends with a space and the
|
|
23
|
+
* name renders exactly as one span would. `null` for names of 16 characters or
|
|
24
|
+
* fewer, or when no head would be left.
|
|
25
|
+
*/
|
|
26
|
+
export declare function splitNameTail(name: string): {
|
|
27
|
+
head: string;
|
|
28
|
+
tail: string;
|
|
29
|
+
} | null;
|
package/dist/lib/utils.js
CHANGED
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import { clsx } from 'clsx';
|
|
2
2
|
import { twMerge } from 'tailwind-merge';
|
|
3
|
-
import { saveBlob } from '@classic-homes/files-core';
|
|
3
|
+
import { isAbortError, isFilesError, openDownloadLink, saveBlob, } from '@classic-homes/files-core';
|
|
4
4
|
export function cn(...inputs) {
|
|
5
5
|
return twMerge(clsx(inputs));
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* Download an entry
|
|
9
|
-
* the
|
|
8
|
+
* Download an entry and save it under its name. Pass an already-loaded Blob
|
|
9
|
+
* to skip the request. Otherwise a source with `downloadUrl()` (CHAPI) hands
|
|
10
|
+
* the browser a short-lived link, so the file streams to disk instead of
|
|
11
|
+
* sitting in memory; without it, or if the link can't be had, the bytes come
|
|
12
|
+
* through `read({ download: true })`, which sends auth.
|
|
10
13
|
*/
|
|
11
14
|
export async function downloadEntry(source, rootId, entry, options = {}) {
|
|
15
|
+
if (!options.blob && source.downloadUrl) {
|
|
16
|
+
try {
|
|
17
|
+
const link = await source.downloadUrl(rootId, entry.path, options.signal);
|
|
18
|
+
openDownloadLink(link.url);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
// A signed-out session or an abort is final; anything else (an older
|
|
23
|
+
// server without links, a hiccup) falls back to reading the bytes.
|
|
24
|
+
if (isAbortError(err) || (isFilesError(err) && err.code === 'unauthorized'))
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
12
28
|
const blob = options.blob ??
|
|
13
29
|
(await source.read(rootId, entry.path, { download: true, signal: options.signal }));
|
|
14
30
|
saveBlob(blob, entry.name);
|
|
@@ -26,3 +42,40 @@ export function isTextInput(target) {
|
|
|
26
42
|
}
|
|
27
43
|
return target.getAttribute('role') === 'combobox' || target.getAttribute('role') === 'textbox';
|
|
28
44
|
}
|
|
45
|
+
/** Names this short are never split: there's little to gain. */
|
|
46
|
+
const SPLIT_MIN_LENGTH = 17;
|
|
47
|
+
/** Longest extension (after the dot) kept whole in the tail. */
|
|
48
|
+
const SPLIT_MAX_EXTENSION = 10;
|
|
49
|
+
/** Characters of the stem kept before the extension. */
|
|
50
|
+
const SPLIT_STEM_KEEP = 6;
|
|
51
|
+
function graphemes(text) {
|
|
52
|
+
const Segmenter = Intl.Segmenter;
|
|
53
|
+
if (Segmenter) {
|
|
54
|
+
return Array.from(new Segmenter(undefined, { granularity: 'grapheme' }).segment(text), (s) => s.segment);
|
|
55
|
+
}
|
|
56
|
+
return Array.from(text); // code points: never splits a surrogate pair
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Split a file name for middle truncation: the head shrinks (ellipsis), the
|
|
60
|
+
* tail always shows. The tail is the extension (from the last dot, unless the
|
|
61
|
+
* dot leads the name or the extension is over 10 characters) plus the 6
|
|
62
|
+
* characters before it, never splitting a character (grapheme). The split moves
|
|
63
|
+
* left off whitespace, so neither part starts or ends with a space and the
|
|
64
|
+
* name renders exactly as one span would. `null` for names of 16 characters or
|
|
65
|
+
* fewer, or when no head would be left.
|
|
66
|
+
*/
|
|
67
|
+
export function splitNameTail(name) {
|
|
68
|
+
if (name.length < SPLIT_MIN_LENGTH)
|
|
69
|
+
return null;
|
|
70
|
+
const dot = name.lastIndexOf('.');
|
|
71
|
+
const extLength = dot > 0 && name.length - dot - 1 <= SPLIT_MAX_EXTENSION ? name.length - dot : 0;
|
|
72
|
+
const parts = graphemes(name.slice(0, name.length - extLength));
|
|
73
|
+
let at = Math.max(0, parts.length - SPLIT_STEM_KEEP);
|
|
74
|
+
const isSpace = (s) => s !== undefined && /^\s+$/u.test(s);
|
|
75
|
+
while (at > 0 && (isSpace(parts[at - 1]) || isSpace(parts[at])))
|
|
76
|
+
at--;
|
|
77
|
+
if (at === 0)
|
|
78
|
+
return null;
|
|
79
|
+
const head = parts.slice(0, at).join('');
|
|
80
|
+
return { head, tail: name.slice(head.length) };
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classic-homes/files-svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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",
|