@widgetic/file-browser 0.5.24 → 0.5.25
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 +225 -0
- package/dist/components/FolderTree.svelte +4 -1
- package/dist/components/FolderTree.svelte.d.ts +2 -6
- package/dist/components/core/FileBrowser.svelte +494 -120
- package/dist/components/core/FileBrowser.svelte.d.ts +25 -1
- package/dist/components/core/FileUploader.svelte +114 -45
- package/dist/components/core/Header.svelte +12 -1
- package/dist/components/core/Header.svelte.d.ts +1 -0
- package/dist/components/core/RecentFiles.svelte +34 -26
- package/dist/components/core/UploadStatus.svelte +8 -5
- package/dist/components/core/UploadStatusList.svelte +13 -21
- package/dist/components/items/FileItem.svelte +19 -10
- package/dist/components/items/FolderItem.svelte +6 -3
- package/dist/components/modals/MoveDialog.svelte +43 -32
- package/dist/components/modals/NewFolderButton.svelte +5 -2
- package/dist/components/modals/SearchModal.svelte +16 -21
- package/dist/components/modals/SearchModal.svelte.d.ts +1 -1
- package/dist/components/modals/SelectedFilesModal.svelte +14 -9
- package/dist/components/modals/SelectedFilesModal.svelte.d.ts +1 -1
- package/dist/components/previews/AudioPlayer.svelte +5 -2
- package/dist/components/previews/ErrorPreview.svelte +7 -4
- package/dist/components/previews/ErrorPreview.svelte.d.ts +1 -1
- package/dist/components/previews/FilePreview.svelte +17 -7
- package/dist/components/previews/FontPreview.svelte +18 -9
- package/dist/components/previews/FontPreview.svelte.d.ts +1 -1
- package/dist/components/previews/TextPreview.svelte +12 -5
- package/dist/components/previews/VideoPlayer.svelte +8 -5
- package/dist/components/ui/ControlToolbar.svelte +45 -23
- package/dist/components/ui/ControlToolbar.svelte.d.ts +1 -1
- package/dist/components/ui/DragSelect.svelte +2 -2
- package/dist/components/ui/DropdownMenu.svelte +35 -14
- package/dist/components/ui/FileIcon.svelte +8 -5
- package/dist/components/ui/FileIcon.svelte.d.ts +1 -0
- package/dist/components/ui/FileMetadata.svelte +12 -15
- package/dist/components/ui/SearchResults.svelte +4 -1
- package/dist/i18n/context.d.ts +38 -0
- package/dist/i18n/context.js +70 -0
- package/dist/i18n/errors.d.ts +11 -0
- package/dist/i18n/errors.js +19 -0
- package/dist/i18n/index.d.ts +11 -0
- package/dist/i18n/index.js +18 -0
- package/dist/i18n/messages.d.ts +277 -0
- package/dist/i18n/messages.js +290 -0
- package/dist/i18n/text.d.ts +19 -0
- package/dist/i18n/text.js +35 -0
- package/dist/i18n/translator.d.ts +75 -0
- package/dist/i18n/translator.js +317 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/mediaLibrary/capabilities.d.ts +22 -0
- package/dist/mediaLibrary/capabilities.js +42 -0
- package/dist/mediaLibrary/context.d.ts +17 -0
- package/dist/mediaLibrary/context.js +16 -0
- package/dist/mediaLibrary/errors.d.ts +42 -0
- package/dist/mediaLibrary/errors.js +114 -0
- package/dist/mediaLibrary/index.d.ts +10 -0
- package/dist/mediaLibrary/index.js +9 -0
- package/dist/mediaLibrary/normalize.d.ts +39 -0
- package/dist/mediaLibrary/normalize.js +139 -0
- package/dist/mediaLibrary/paging.d.ts +37 -0
- package/dist/mediaLibrary/paging.js +62 -0
- package/dist/mediaLibrary/resolve.d.ts +39 -0
- package/dist/mediaLibrary/resolve.js +137 -0
- package/dist/mediaLibrary/treeOps.d.ts +24 -0
- package/dist/mediaLibrary/treeOps.js +67 -0
- package/dist/mediaLibrary/types.d.ts +126 -0
- package/dist/mediaLibrary/types.js +11 -0
- package/dist/mediaLibrary/urls.d.ts +30 -0
- package/dist/mediaLibrary/urls.js +59 -0
- package/dist/mediaLibrary/widgetic.d.ts +39 -0
- package/dist/mediaLibrary/widgetic.js +89 -0
- package/dist/stores/fileStore.d.ts +2 -2
- package/dist/stores/folderCountStore.js +2 -2
- package/dist/stores/navigationStore.d.ts +1 -1
- package/dist/stores/persistentSelectStore.d.ts +11 -1
- package/dist/stores/persistentSelectStore.js +26 -0
- package/dist/utils/dateUtils.d.ts +11 -2
- package/dist/utils/dateUtils.js +20 -15
- package/dist/utils/fileKinds.d.ts +44 -0
- package/dist/utils/fileKinds.js +96 -0
- package/dist/utils/fileNames.d.ts +7 -0
- package/dist/utils/fileNames.js +19 -0
- package/dist/utils/fileOperations.js +2 -11
- package/dist/utils/fileTypes.d.ts +11 -2
- package/dist/utils/fileTypes.js +26 -14
- package/dist/utils/fileUtils.d.ts +8 -1
- package/dist/utils/fileUtils.js +22 -7
- package/dist/utils/folderUtils.js +3 -2
- package/dist/utils/safeText.d.ts +29 -0
- package/dist/utils/safeText.js +66 -0
- package/dist/utils/userUploadsStorage.d.ts +4 -1
- package/dist/utils/userUploadsStorage.js +5 -2
- package/dist/utils/widgeticTusUpload.d.ts +10 -0
- package/dist/utils/widgeticTusUpload.js +61 -3
- package/dist/utils/widgeticUpload.d.ts +3 -0
- package/dist/utils/widgeticUpload.js +7 -2
- package/package.json +4 -2
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Translate } from './context';
|
|
2
|
+
/** A sentence cut around the words of it that are a link: what comes before, the link, what comes after. */
|
|
3
|
+
export interface SentenceWithLink {
|
|
4
|
+
before: string;
|
|
5
|
+
/** Empty when the sentence does not contain the link's words. */
|
|
6
|
+
link: string;
|
|
7
|
+
after: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Finds the words that act as a link inside a sentence, so the screen can underline just those. The two come from two
|
|
11
|
+
* messages (the sentence and the link), so a language can put the link anywhere in its sentence. A link that the
|
|
12
|
+
* sentence does not contain gives the sentence whole, with nothing underlined.
|
|
13
|
+
*/
|
|
14
|
+
export declare function splitAround(sentence: string, link: string): SentenceWithLink;
|
|
15
|
+
/**
|
|
16
|
+
* The word for a kind of file ("image", "document", "SVG") in the page's language, for a sentence like "Upload image
|
|
17
|
+
* files". A kind the catalog has no word for is shown as it came.
|
|
18
|
+
*/
|
|
19
|
+
export declare function kindWord(kind: string, translate: Translate): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the words that act as a link inside a sentence, so the screen can underline just those. The two come from two
|
|
3
|
+
* messages (the sentence and the link), so a language can put the link anywhere in its sentence. A link that the
|
|
4
|
+
* sentence does not contain gives the sentence whole, with nothing underlined.
|
|
5
|
+
*/
|
|
6
|
+
export function splitAround(sentence, link) {
|
|
7
|
+
const index = link === '' ? -1 : sentence.indexOf(link);
|
|
8
|
+
if (index < 0)
|
|
9
|
+
return { before: sentence, link: '', after: '' };
|
|
10
|
+
return { before: sentence.slice(0, index), link, after: sentence.slice(index + link.length) };
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The word for a kind of file ("image", "document", "SVG") in the page's language, for a sentence like "Upload image
|
|
14
|
+
* files". A kind the catalog has no word for is shown as it came.
|
|
15
|
+
*/
|
|
16
|
+
export function kindWord(kind, translate) {
|
|
17
|
+
switch (kind) {
|
|
18
|
+
case 'any':
|
|
19
|
+
return translate('fileKind.any');
|
|
20
|
+
case 'svg':
|
|
21
|
+
return translate('fileKind.svg');
|
|
22
|
+
case 'document':
|
|
23
|
+
return translate('fileKind.document');
|
|
24
|
+
case 'image':
|
|
25
|
+
return translate('fileKind.image');
|
|
26
|
+
case 'video':
|
|
27
|
+
return translate('fileKind.video');
|
|
28
|
+
case 'audio':
|
|
29
|
+
return translate('fileKind.audio');
|
|
30
|
+
case 'font':
|
|
31
|
+
return translate('fileKind.font');
|
|
32
|
+
default:
|
|
33
|
+
return kind;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The words of the File Browser, as a function.
|
|
3
|
+
*
|
|
4
|
+
* A message is a template in a flat catalog: `'upload.progress': '{done} of {total} uploaded'`. A page can replace any
|
|
5
|
+
* of them (the `messages` option); a key it leaves out keeps the English text, so a partly translated catalog never
|
|
6
|
+
* shows a hole.
|
|
7
|
+
*
|
|
8
|
+
* A template is plain text with two kinds of `{...}`:
|
|
9
|
+
*
|
|
10
|
+
* {name} the value called `name`; a number is written the way the locale
|
|
11
|
+
* writes numbers (1,234 or 1.234)
|
|
12
|
+
* {count, plural, =0 {none} one {# file} other {# files}}
|
|
13
|
+
* one branch, chosen by the locale's plural rules (`Intl.PluralRules`),
|
|
14
|
+
* so a language with more than two forms (Polish, Arabic) can say so.
|
|
15
|
+
* `=0` matches exactly 0; `other` is required; `#` is the number
|
|
16
|
+
*
|
|
17
|
+
* `\{`, `\}`, `\#` and `\\` write the character itself. An apostrophe is just an apostrophe: `Delete '{name}'?` works.
|
|
18
|
+
*
|
|
19
|
+
* Nothing here knows about Svelte or the DOM: the same code runs in a unit test.
|
|
20
|
+
*/
|
|
21
|
+
/** A value a template can show. */
|
|
22
|
+
export type MessageValue = string | number;
|
|
23
|
+
export type MessageValues = Readonly<Record<string, MessageValue>>;
|
|
24
|
+
/** Something wrong in what the page supplied. Reported once, never thrown: a broken message must not break the screen. */
|
|
25
|
+
export type MessageProblem = {
|
|
26
|
+
kind: 'unknown-message';
|
|
27
|
+
key: string;
|
|
28
|
+
suggestion?: string;
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'not-text';
|
|
31
|
+
key: string;
|
|
32
|
+
} | {
|
|
33
|
+
kind: 'broken-message';
|
|
34
|
+
key: string;
|
|
35
|
+
reason: string;
|
|
36
|
+
} | {
|
|
37
|
+
kind: 'unknown-value';
|
|
38
|
+
key: string;
|
|
39
|
+
name: string;
|
|
40
|
+
} | {
|
|
41
|
+
kind: 'unknown-locale';
|
|
42
|
+
locale: string;
|
|
43
|
+
};
|
|
44
|
+
/** One line a developer can read in the console about a {@link MessageProblem}. */
|
|
45
|
+
export declare function describeMessageProblem(problem: MessageProblem): string;
|
|
46
|
+
/** The shape the page supplies for `messages`: any of the catalog's keys, with text. */
|
|
47
|
+
export type MessageOverrides = Readonly<Record<string, unknown>>;
|
|
48
|
+
export interface TranslatorOptions<Key extends string> {
|
|
49
|
+
/** A BCP 47 tag ("de", "pt-BR"). It sets plural rules and number formats. The words come from `messages`. Default "en". */
|
|
50
|
+
locale?: string;
|
|
51
|
+
/** The built-in English messages. Every key the screens ask for is here. */
|
|
52
|
+
catalog: Readonly<Record<Key, string>>;
|
|
53
|
+
/** The page's own words: any of the catalog's keys. */
|
|
54
|
+
messages?: MessageOverrides | null;
|
|
55
|
+
/** Told about each problem once. The default is to say nothing. */
|
|
56
|
+
onProblem?: (problem: MessageProblem) => void;
|
|
57
|
+
}
|
|
58
|
+
export interface Translator<Key extends string> {
|
|
59
|
+
/** The message for `key`, with `values` put in. An unknown key comes back as the key itself. */
|
|
60
|
+
t: (key: Key, values?: MessageValues) => string;
|
|
61
|
+
/** The language tag in use, after checking it ("en" when the one given was not a tag). */
|
|
62
|
+
readonly locale: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Makes the function that turns keys into text. It never throws for what the page supplied: an unusable message is
|
|
66
|
+
* reported through `onProblem` (once) and the English one is used in its place.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createTranslator<Key extends string>(options: TranslatorOptions<Key>): Translator<Key>;
|
|
69
|
+
/**
|
|
70
|
+
* The names of the values a message asks for: each `{name}` in it, and the number a plural looks at, sorted. For a test
|
|
71
|
+
* or a tool that checks the code gives a message everything it needs. Throws when the template does not parse.
|
|
72
|
+
*/
|
|
73
|
+
export declare function valueNamesOfTemplate(template: string): string[];
|
|
74
|
+
/** Checks `messages` against a catalog without making a translator: what a test, or a build step, can call. */
|
|
75
|
+
export declare function findMessageProblems<Key extends string>(catalog: Readonly<Record<Key, string>>, messages: MessageOverrides): MessageProblem[];
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The words of the File Browser, as a function.
|
|
3
|
+
*
|
|
4
|
+
* A message is a template in a flat catalog: `'upload.progress': '{done} of {total} uploaded'`. A page can replace any
|
|
5
|
+
* of them (the `messages` option); a key it leaves out keeps the English text, so a partly translated catalog never
|
|
6
|
+
* shows a hole.
|
|
7
|
+
*
|
|
8
|
+
* A template is plain text with two kinds of `{...}`:
|
|
9
|
+
*
|
|
10
|
+
* {name} the value called `name`; a number is written the way the locale
|
|
11
|
+
* writes numbers (1,234 or 1.234)
|
|
12
|
+
* {count, plural, =0 {none} one {# file} other {# files}}
|
|
13
|
+
* one branch, chosen by the locale's plural rules (`Intl.PluralRules`),
|
|
14
|
+
* so a language with more than two forms (Polish, Arabic) can say so.
|
|
15
|
+
* `=0` matches exactly 0; `other` is required; `#` is the number
|
|
16
|
+
*
|
|
17
|
+
* `\{`, `\}`, `\#` and `\\` write the character itself. An apostrophe is just an apostrophe: `Delete '{name}'?` works.
|
|
18
|
+
*
|
|
19
|
+
* Nothing here knows about Svelte or the DOM: the same code runs in a unit test.
|
|
20
|
+
*/
|
|
21
|
+
/** One line a developer can read in the console about a {@link MessageProblem}. */
|
|
22
|
+
export function describeMessageProblem(problem) {
|
|
23
|
+
switch (problem.kind) {
|
|
24
|
+
case 'unknown-message':
|
|
25
|
+
return problem.suggestion
|
|
26
|
+
? `There is no message called "${problem.key}". Did you mean "${problem.suggestion}"? It is ignored.`
|
|
27
|
+
: `There is no message called "${problem.key}". It is ignored.`;
|
|
28
|
+
case 'not-text':
|
|
29
|
+
return `The message "${problem.key}" must be a string. The English text is used.`;
|
|
30
|
+
case 'broken-message':
|
|
31
|
+
return `The message "${problem.key}" cannot be used (${problem.reason}). The English text is used.`;
|
|
32
|
+
case 'unknown-value':
|
|
33
|
+
return `The message "${problem.key}" asks for {${problem.name}}, which this message is never given. It would show as written.`;
|
|
34
|
+
case 'unknown-locale':
|
|
35
|
+
return `"${problem.locale}" is not a language tag (like "de" or "pt-BR"). Numbers and dates use English.`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
class TemplateSyntaxError extends Error {
|
|
39
|
+
}
|
|
40
|
+
/** The names of the values a template uses, for checking a replacement against the English one. */
|
|
41
|
+
function valueNamesOf(parts, names = new Set()) {
|
|
42
|
+
for (const part of parts) {
|
|
43
|
+
if (part.kind === 'value')
|
|
44
|
+
names.add(part.name);
|
|
45
|
+
if (part.kind === 'plural') {
|
|
46
|
+
names.add(part.name);
|
|
47
|
+
for (const branch of part.branches.values())
|
|
48
|
+
valueNamesOf(branch, names);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return names;
|
|
52
|
+
}
|
|
53
|
+
const NAME_START = /[A-Za-z_]/;
|
|
54
|
+
const NAME_CHARACTER = /[A-Za-z0-9_]/;
|
|
55
|
+
/** Reads a template. Throws a {@link TemplateSyntaxError} that says what is wrong, for the console. */
|
|
56
|
+
function parseTemplate(source) {
|
|
57
|
+
let position = 0;
|
|
58
|
+
function skipSpaces() {
|
|
59
|
+
while (position < source.length && /\s/.test(source[position]))
|
|
60
|
+
position += 1;
|
|
61
|
+
}
|
|
62
|
+
function readName() {
|
|
63
|
+
skipSpaces();
|
|
64
|
+
const begin = position;
|
|
65
|
+
if (position >= source.length || !NAME_START.test(source[position])) {
|
|
66
|
+
throw new TemplateSyntaxError(`a value name was expected at character ${position + 1}`);
|
|
67
|
+
}
|
|
68
|
+
while (position < source.length && NAME_CHARACTER.test(source[position]))
|
|
69
|
+
position += 1;
|
|
70
|
+
const name = source.slice(begin, position);
|
|
71
|
+
skipSpaces();
|
|
72
|
+
return name;
|
|
73
|
+
}
|
|
74
|
+
/** Reads parts until the end of the text, or (inside a branch) until the `}` that ends it, which it does not consume. */
|
|
75
|
+
function readParts(insideBranch) {
|
|
76
|
+
const parts = [];
|
|
77
|
+
let text = '';
|
|
78
|
+
const flushText = () => {
|
|
79
|
+
if (text !== '')
|
|
80
|
+
parts.push({ kind: 'text', text });
|
|
81
|
+
text = '';
|
|
82
|
+
};
|
|
83
|
+
while (position < source.length) {
|
|
84
|
+
const character = source[position];
|
|
85
|
+
if (character === '\\') {
|
|
86
|
+
const escaped = source[position + 1];
|
|
87
|
+
if (escaped === undefined)
|
|
88
|
+
throw new TemplateSyntaxError('a backslash at the end');
|
|
89
|
+
text += escaped;
|
|
90
|
+
position += 2;
|
|
91
|
+
}
|
|
92
|
+
else if (character === '{') {
|
|
93
|
+
flushText();
|
|
94
|
+
position += 1;
|
|
95
|
+
parts.push(readArgument());
|
|
96
|
+
}
|
|
97
|
+
else if (character === '}') {
|
|
98
|
+
if (insideBranch)
|
|
99
|
+
break;
|
|
100
|
+
throw new TemplateSyntaxError(`a } at character ${position + 1} closes nothing (write \\} for the character)`);
|
|
101
|
+
}
|
|
102
|
+
else if (character === '#' && insideBranch) {
|
|
103
|
+
flushText();
|
|
104
|
+
parts.push({ kind: 'number' });
|
|
105
|
+
position += 1;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
text += character;
|
|
109
|
+
position += 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
flushText();
|
|
113
|
+
return parts;
|
|
114
|
+
}
|
|
115
|
+
/** After the `{`: `name}` or `name, plural, selector {…} … }`. */
|
|
116
|
+
function readArgument() {
|
|
117
|
+
const name = readName();
|
|
118
|
+
if (source[position] === '}') {
|
|
119
|
+
position += 1;
|
|
120
|
+
return { kind: 'value', name };
|
|
121
|
+
}
|
|
122
|
+
if (source[position] !== ',') {
|
|
123
|
+
throw new TemplateSyntaxError(`a } or a comma was expected after {${name} at character ${position + 1}`);
|
|
124
|
+
}
|
|
125
|
+
position += 1;
|
|
126
|
+
skipSpaces();
|
|
127
|
+
const typeBegin = position;
|
|
128
|
+
while (position < source.length && /[a-z]/.test(source[position]))
|
|
129
|
+
position += 1;
|
|
130
|
+
const type = source.slice(typeBegin, position);
|
|
131
|
+
if (type !== 'plural') {
|
|
132
|
+
throw new TemplateSyntaxError(`{${name}, ${type || '…'}} is not supported: only plural is`);
|
|
133
|
+
}
|
|
134
|
+
skipSpaces();
|
|
135
|
+
if (source[position] !== ',')
|
|
136
|
+
throw new TemplateSyntaxError(`a comma was expected after {${name}, plural`);
|
|
137
|
+
position += 1;
|
|
138
|
+
const branches = new Map();
|
|
139
|
+
for (;;) {
|
|
140
|
+
skipSpaces();
|
|
141
|
+
if (position >= source.length)
|
|
142
|
+
throw new TemplateSyntaxError(`{${name}, plural, … is never closed`);
|
|
143
|
+
if (source[position] === '}') {
|
|
144
|
+
position += 1;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const selectorBegin = position;
|
|
148
|
+
while (position < source.length && !/[\s{}]/.test(source[position]))
|
|
149
|
+
position += 1;
|
|
150
|
+
const selector = source.slice(selectorBegin, position);
|
|
151
|
+
if (selector === '' || !/^(=\d+|zero|one|two|few|many|other)$/.test(selector)) {
|
|
152
|
+
throw new TemplateSyntaxError(`"${selector}" is not a plural form (use =0, zero, one, two, few, many or other)`);
|
|
153
|
+
}
|
|
154
|
+
skipSpaces();
|
|
155
|
+
if (source[position] !== '{')
|
|
156
|
+
throw new TemplateSyntaxError(`a { was expected after "${selector}"`);
|
|
157
|
+
position += 1;
|
|
158
|
+
if (branches.has(selector))
|
|
159
|
+
throw new TemplateSyntaxError(`"${selector}" is given twice`);
|
|
160
|
+
const branch = readParts(true);
|
|
161
|
+
if (source[position] !== '}')
|
|
162
|
+
throw new TemplateSyntaxError(`the text of "${selector}" is never closed with }`);
|
|
163
|
+
position += 1;
|
|
164
|
+
branches.set(selector, branch);
|
|
165
|
+
}
|
|
166
|
+
if (!branches.has('other'))
|
|
167
|
+
throw new TemplateSyntaxError(`{${name}, plural, …} needs an "other" form`);
|
|
168
|
+
return { kind: 'plural', name, branches };
|
|
169
|
+
}
|
|
170
|
+
return readParts(false);
|
|
171
|
+
}
|
|
172
|
+
/** The catalog key nearest to `key` by spelling, when it is near enough to be a typo. */
|
|
173
|
+
function nearestKey(key, keys) {
|
|
174
|
+
let best;
|
|
175
|
+
// A key that is up to three edits away is taken for a typo.
|
|
176
|
+
let bestDistance = 4;
|
|
177
|
+
for (const candidate of keys) {
|
|
178
|
+
if (Math.abs(candidate.length - key.length) >= bestDistance)
|
|
179
|
+
continue;
|
|
180
|
+
let previous = Array.from({ length: candidate.length + 1 }, (_, index) => index);
|
|
181
|
+
for (let row = 1; row <= key.length; row += 1) {
|
|
182
|
+
const current = [row];
|
|
183
|
+
for (let column = 1; column <= candidate.length; column += 1) {
|
|
184
|
+
current[column] = Math.min(previous[column] + 1, current[column - 1] + 1, previous[column - 1] + (key[row - 1] === candidate[column - 1] ? 0 : 1));
|
|
185
|
+
}
|
|
186
|
+
previous = current;
|
|
187
|
+
}
|
|
188
|
+
const distance = previous[candidate.length];
|
|
189
|
+
if (distance < bestDistance) {
|
|
190
|
+
best = candidate;
|
|
191
|
+
bestDistance = distance;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return best;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Makes the function that turns keys into text. It never throws for what the page supplied: an unusable message is
|
|
198
|
+
* reported through `onProblem` (once) and the English one is used in its place.
|
|
199
|
+
*/
|
|
200
|
+
export function createTranslator(options) {
|
|
201
|
+
const { catalog, onProblem } = options;
|
|
202
|
+
const reported = new Set();
|
|
203
|
+
const report = (problem) => {
|
|
204
|
+
const signature = JSON.stringify(problem);
|
|
205
|
+
if (reported.has(signature))
|
|
206
|
+
return;
|
|
207
|
+
reported.add(signature);
|
|
208
|
+
onProblem?.(problem);
|
|
209
|
+
};
|
|
210
|
+
// The locale: a tag this browser has number and plural rules for, or English. A tag that Intl accepts but has no data
|
|
211
|
+
// for would silently use the visitor's own language, which would mix two languages on one screen.
|
|
212
|
+
let locale = 'en';
|
|
213
|
+
if (options.locale !== undefined && options.locale !== null && options.locale !== '') {
|
|
214
|
+
try {
|
|
215
|
+
const canonical = Intl.getCanonicalLocales(options.locale)[0];
|
|
216
|
+
if (canonical !== undefined &&
|
|
217
|
+
Intl.NumberFormat.supportedLocalesOf([canonical]).length > 0 &&
|
|
218
|
+
Intl.PluralRules.supportedLocalesOf([canonical]).length > 0) {
|
|
219
|
+
locale = canonical;
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
report({ kind: 'unknown-locale', locale: String(options.locale) });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
report({ kind: 'unknown-locale', locale: String(options.locale) });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const numberFormat = new Intl.NumberFormat(locale);
|
|
230
|
+
const pluralRules = new Intl.PluralRules(locale);
|
|
231
|
+
const catalogKeys = Object.keys(catalog);
|
|
232
|
+
const englishParts = new Map();
|
|
233
|
+
const englishPartsOf = (key) => {
|
|
234
|
+
let parts = englishParts.get(key);
|
|
235
|
+
if (parts === undefined && Object.prototype.hasOwnProperty.call(catalog, key)) {
|
|
236
|
+
// The built-in messages are ours; a syntax error in one is a bug the tests catch, so it is allowed to throw.
|
|
237
|
+
parts = parseTemplate(catalog[key]);
|
|
238
|
+
englishParts.set(key, parts);
|
|
239
|
+
}
|
|
240
|
+
return parts;
|
|
241
|
+
};
|
|
242
|
+
// The page's messages, checked once, here: a typo in a key, a value that is not text, a template that does not parse,
|
|
243
|
+
// or one that asks for a value the English message is never given.
|
|
244
|
+
const replacements = new Map();
|
|
245
|
+
const supplied = options.messages;
|
|
246
|
+
if (supplied !== undefined && supplied !== null && typeof supplied === 'object') {
|
|
247
|
+
for (const key of Object.keys(supplied)) {
|
|
248
|
+
if (!Object.prototype.hasOwnProperty.call(catalog, key)) {
|
|
249
|
+
report({ kind: 'unknown-message', key, suggestion: nearestKey(key, catalogKeys) });
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const text = supplied[key];
|
|
253
|
+
if (typeof text !== 'string') {
|
|
254
|
+
report({ kind: 'not-text', key });
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const parts = parseTemplate(text);
|
|
259
|
+
const allowedNames = valueNamesOf(englishPartsOf(key) ?? []);
|
|
260
|
+
for (const name of valueNamesOf(parts)) {
|
|
261
|
+
if (!allowedNames.has(name))
|
|
262
|
+
report({ kind: 'unknown-value', key, name });
|
|
263
|
+
}
|
|
264
|
+
replacements.set(key, parts);
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
if (!(error instanceof TemplateSyntaxError))
|
|
268
|
+
throw error;
|
|
269
|
+
report({ kind: 'broken-message', key, reason: error.message });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function write(parts, values, pluralNumber) {
|
|
274
|
+
let output = '';
|
|
275
|
+
for (const part of parts) {
|
|
276
|
+
if (part.kind === 'text') {
|
|
277
|
+
output += part.text;
|
|
278
|
+
}
|
|
279
|
+
else if (part.kind === 'number') {
|
|
280
|
+
output += pluralNumber === null ? '#' : numberFormat.format(pluralNumber);
|
|
281
|
+
}
|
|
282
|
+
else if (part.kind === 'value') {
|
|
283
|
+
const value = values[part.name];
|
|
284
|
+
output += value === undefined ? `{${part.name}}` : typeof value === 'number' ? numberFormat.format(value) : String(value);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const raw = values[part.name];
|
|
288
|
+
const count = typeof raw === 'number' ? raw : Number(raw);
|
|
289
|
+
const form = Number.isFinite(count) ? (part.branches.get(`=${count}`) ?? part.branches.get(pluralRules.select(count))) : undefined;
|
|
290
|
+
output += write(form ?? part.branches.get('other'), values, Number.isFinite(count) ? count : null);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return output;
|
|
294
|
+
}
|
|
295
|
+
const t = (key, values = {}) => {
|
|
296
|
+
const parts = replacements.get(key) ?? englishPartsOf(key);
|
|
297
|
+
if (parts === undefined) {
|
|
298
|
+
report({ kind: 'unknown-message', key, suggestion: nearestKey(key, catalogKeys) });
|
|
299
|
+
return key;
|
|
300
|
+
}
|
|
301
|
+
return write(parts, values, null);
|
|
302
|
+
};
|
|
303
|
+
return { t, locale };
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* The names of the values a message asks for: each `{name}` in it, and the number a plural looks at, sorted. For a test
|
|
307
|
+
* or a tool that checks the code gives a message everything it needs. Throws when the template does not parse.
|
|
308
|
+
*/
|
|
309
|
+
export function valueNamesOfTemplate(template) {
|
|
310
|
+
return [...valueNamesOf(parseTemplate(template))].sort();
|
|
311
|
+
}
|
|
312
|
+
/** Checks `messages` against a catalog without making a translator: what a test, or a build step, can call. */
|
|
313
|
+
export function findMessageProblems(catalog, messages) {
|
|
314
|
+
const problems = [];
|
|
315
|
+
createTranslator({ catalog, messages, onProblem: (problem) => problems.push(problem) });
|
|
316
|
+
return problems;
|
|
317
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,8 @@ export * from './components';
|
|
|
3
3
|
export * from './stores';
|
|
4
4
|
export { fileBrowserUploadConfig, setFileBrowserUploadConfig, getFileBrowserUploadConfig, type FileBrowserUploadConfig, } from './stores/uploadApiStore';
|
|
5
5
|
export * from './utils';
|
|
6
|
+
export type { MediaLibraryCapabilities, MediaLibraryDeleteRequest, MediaLibraryErrorCode, MediaLibraryFile, MediaLibraryListRequest, MediaLibraryPage, MediaLibraryProvider, MediaLibraryRenameRequest, MediaLibraryUploadRequest, WidgeticMediaLibraryOptions, } from './mediaLibrary';
|
|
7
|
+
export { MediaLibraryError, createWidgeticMediaLibrary } from './mediaLibrary';
|
|
8
|
+
export { FILE_BROWSER_MESSAGES, checkFileBrowserMessages, describeMessageProblem, type FileBrowserMessageKey, type MessageOverrides, type MessageProblem, } from './i18n';
|
|
6
9
|
export * from './types';
|
|
7
10
|
export type { FileSystemItem, FileSystemRoot, FolderItemWithCount } from './types';
|
package/dist/index.js
CHANGED
|
@@ -7,5 +7,9 @@ export * from './stores';
|
|
|
7
7
|
export { fileBrowserUploadConfig, setFileBrowserUploadConfig, getFileBrowserUploadConfig, } from './stores/uploadApiStore';
|
|
8
8
|
// Export all utilities
|
|
9
9
|
export * from './utils';
|
|
10
|
+
export { MediaLibraryError, createWidgeticMediaLibrary } from './mediaLibrary';
|
|
11
|
+
// Export the languages seam: the English catalog (every key a page can replace through `messages`), and the checker
|
|
12
|
+
// a page or a build step can run on its own messages.
|
|
13
|
+
export { FILE_BROWSER_MESSAGES, checkFileBrowserMessages, describeMessageProblem, } from './i18n';
|
|
10
14
|
// Export types
|
|
11
15
|
export * from './types';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { MediaLibraryCapabilities, MediaLibraryProvider } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* What the File Browser offers when it was not given a media library: everything it has always offered. The Widgetic
|
|
4
|
+
* site and the editor use it this way, so they behave exactly as before.
|
|
5
|
+
*/
|
|
6
|
+
export declare const LEGACY_CAPABILITIES: Readonly<MediaLibraryCapabilities>;
|
|
7
|
+
/**
|
|
8
|
+
* What the File Browser offers when the media library it was given cannot be used: nothing, so no button leads to
|
|
9
|
+
* something that cannot work. (The screen says what is wrong.)
|
|
10
|
+
*/
|
|
11
|
+
export declare const NO_CAPABILITIES: Readonly<MediaLibraryCapabilities>;
|
|
12
|
+
/**
|
|
13
|
+
* What the File Browser offers for a library it was given.
|
|
14
|
+
*
|
|
15
|
+
* A library can only take actions away. Rename, Delete and Upload need the library's matching function, so the
|
|
16
|
+
* File Browser never shows a button that has nothing behind it.
|
|
17
|
+
*
|
|
18
|
+
* A library is one flat list of files, so New folder and Move are never offered: a folder made in the File Browser
|
|
19
|
+
* would exist only until the File Browser is closed, which looks like a feature that works and then loses the
|
|
20
|
+
* visitor's work. (Folders need a library that has folders of its own; the contract does not have them yet.)
|
|
21
|
+
*/
|
|
22
|
+
export declare function capabilitiesOf(provider: Pick<MediaLibraryProvider, 'capabilities' | 'upload' | 'rename' | 'delete'>): MediaLibraryCapabilities;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the File Browser offers when it was not given a media library: everything it has always offered. The Widgetic
|
|
3
|
+
* site and the editor use it this way, so they behave exactly as before.
|
|
4
|
+
*/
|
|
5
|
+
export const LEGACY_CAPABILITIES = Object.freeze({
|
|
6
|
+
upload: true,
|
|
7
|
+
rename: true,
|
|
8
|
+
delete: true,
|
|
9
|
+
download: true,
|
|
10
|
+
folders: true
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* What the File Browser offers when the media library it was given cannot be used: nothing, so no button leads to
|
|
14
|
+
* something that cannot work. (The screen says what is wrong.)
|
|
15
|
+
*/
|
|
16
|
+
export const NO_CAPABILITIES = Object.freeze({
|
|
17
|
+
upload: false,
|
|
18
|
+
rename: false,
|
|
19
|
+
delete: false,
|
|
20
|
+
download: false,
|
|
21
|
+
folders: false
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* What the File Browser offers for a library it was given.
|
|
25
|
+
*
|
|
26
|
+
* A library can only take actions away. Rename, Delete and Upload need the library's matching function, so the
|
|
27
|
+
* File Browser never shows a button that has nothing behind it.
|
|
28
|
+
*
|
|
29
|
+
* A library is one flat list of files, so New folder and Move are never offered: a folder made in the File Browser
|
|
30
|
+
* would exist only until the File Browser is closed, which looks like a feature that works and then loses the
|
|
31
|
+
* visitor's work. (Folders need a library that has folders of its own; the contract does not have them yet.)
|
|
32
|
+
*/
|
|
33
|
+
export function capabilitiesOf(provider) {
|
|
34
|
+
const declared = provider.capabilities ?? {};
|
|
35
|
+
return {
|
|
36
|
+
upload: typeof provider.upload === 'function' && declared.upload !== false,
|
|
37
|
+
rename: typeof provider.rename === 'function' && declared.rename !== false,
|
|
38
|
+
delete: typeof provider.delete === 'function' && declared.delete !== false,
|
|
39
|
+
download: declared.download !== false,
|
|
40
|
+
folders: false
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ResolvedMediaLibrary } from './resolve';
|
|
2
|
+
import type { MediaLibraryCapabilities } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* What the File Browser's parts need to know about the library, shared with them through Svelte's component context.
|
|
5
|
+
*
|
|
6
|
+
* Context belongs to one File Browser instance, so two File Browsers on a page (with two libraries) never mix.
|
|
7
|
+
*/
|
|
8
|
+
export interface MediaLibraryContext {
|
|
9
|
+
/** The library the host supplied, or `null` when the File Browser works as it always has (legacy settings). */
|
|
10
|
+
library: ResolvedMediaLibrary | null;
|
|
11
|
+
/** Which actions to offer. Everything, when there is no library. */
|
|
12
|
+
capabilities: MediaLibraryCapabilities;
|
|
13
|
+
}
|
|
14
|
+
/** Called by the File Browser while it is created, so everything inside it can find the library. */
|
|
15
|
+
export declare function provideMediaLibraryContext(context: MediaLibraryContext): void;
|
|
16
|
+
/** Called by a part of the File Browser while it is created. Never throws; outside a File Browser it reads as legacy. */
|
|
17
|
+
export declare function useMediaLibraryContext(): MediaLibraryContext;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
import { LEGACY_CAPABILITIES } from './capabilities';
|
|
3
|
+
const MEDIA_LIBRARY_CONTEXT_KEY = Symbol('widgetic-file-browser-media-library');
|
|
4
|
+
/** What a part of the File Browser sees when no library was supplied: the way it has always worked. */
|
|
5
|
+
const LEGACY_CONTEXT = Object.freeze({
|
|
6
|
+
library: null,
|
|
7
|
+
capabilities: LEGACY_CAPABILITIES
|
|
8
|
+
});
|
|
9
|
+
/** Called by the File Browser while it is created, so everything inside it can find the library. */
|
|
10
|
+
export function provideMediaLibraryContext(context) {
|
|
11
|
+
setContext(MEDIA_LIBRARY_CONTEXT_KEY, context);
|
|
12
|
+
}
|
|
13
|
+
/** Called by a part of the File Browser while it is created. Never throws; outside a File Browser it reads as legacy. */
|
|
14
|
+
export function useMediaLibraryContext() {
|
|
15
|
+
return getContext(MEDIA_LIBRARY_CONTEXT_KEY) ?? LEGACY_CONTEXT;
|
|
16
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { FileBrowserMessageKey } from '../i18n/messages';
|
|
2
|
+
import type { MessageValues } from '../i18n/translator';
|
|
3
|
+
import type { MediaLibraryErrorCode } from './types';
|
|
4
|
+
/** A failed call to a media library, with a reason the File Browser can act on. */
|
|
5
|
+
export declare class MediaLibraryError extends Error {
|
|
6
|
+
readonly code: MediaLibraryErrorCode;
|
|
7
|
+
/** The HTTP status behind the failure, when there was one. */
|
|
8
|
+
readonly status: number | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The catalog message that says the same thing in the visitor's language. Set when `message` is the File Browser's
|
|
11
|
+
* own English text (not one the library wrote): the visitor then reads the translation, while the `error` event and
|
|
12
|
+
* the console keep the English `message`. Absent when the library supplied the text, which is shown as it is.
|
|
13
|
+
*/
|
|
14
|
+
readonly messageKey: FileBrowserMessageKey | undefined;
|
|
15
|
+
/** The values the message at `messageKey` asks for (a size limit, a count), when it asks for any. */
|
|
16
|
+
readonly messageValues: MessageValues | undefined;
|
|
17
|
+
constructor(code: MediaLibraryErrorCode, message: string, options?: {
|
|
18
|
+
status?: number;
|
|
19
|
+
cause?: unknown;
|
|
20
|
+
messageKey?: FileBrowserMessageKey;
|
|
21
|
+
messageValues?: MessageValues;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** The error code that fits an HTTP status. */
|
|
25
|
+
export declare function codeForHttpStatus(status: number): MediaLibraryErrorCode;
|
|
26
|
+
/** True when something was cancelled with an AbortController (fetch and friends throw an `AbortError`). */
|
|
27
|
+
export declare function isAbortError(error: unknown): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* True when a call ended because it was cancelled on purpose (the visitor pressed Cancel, or the File Browser was
|
|
30
|
+
* closed): either a raw `AbortError` or a {@link MediaLibraryError} coded `ABORTED`. Not a failure to show.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isCancellation(error: unknown): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Turns whatever a library's function threw (or rejected with) into a {@link MediaLibraryError}.
|
|
35
|
+
*
|
|
36
|
+
* A library is another party's code: it may throw an Error, a string, a fetch `Response`-like object or nothing at
|
|
37
|
+
* all. The visitor gets the library's own message when it has one, otherwise the File Browser's `fallbackMessage`
|
|
38
|
+
* (and, for the visitor's language, `fallbackKey`: the catalog message that says the same).
|
|
39
|
+
*/
|
|
40
|
+
export declare function toMediaLibraryError(error: unknown, fallbackMessage: string, fallbackKey?: FileBrowserMessageKey): MediaLibraryError;
|
|
41
|
+
/** True when the failure means the credentials were refused, so refreshing them and trying again may help. */
|
|
42
|
+
export declare function isUnauthorized(error: unknown): boolean;
|