@gramlot/gramlot 0.3.41 → 0.4.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/assets/icons/applications.svg +4 -0
- package/assets/icons/archive.svg +4 -0
- package/assets/icons/attachment.svg +4 -0
- package/assets/icons/book.svg +4 -0
- package/assets/icons/bookmark.svg +4 -0
- package/assets/icons/calendar.svg +4 -0
- package/assets/icons/cancel.svg +4 -0
- package/assets/icons/chart.svg +4 -1
- package/assets/icons/chat.svg +4 -0
- package/assets/icons/clock.svg +4 -0
- package/assets/icons/code.svg +4 -0
- package/assets/icons/collapse.svg +4 -0
- package/assets/icons/columns.svg +4 -0
- package/assets/icons/confirm.svg +4 -0
- package/assets/icons/copy.svg +4 -0
- package/assets/icons/cut.svg +4 -0
- package/assets/icons/database.svg +4 -0
- package/assets/icons/delete.svg +4 -0
- package/assets/icons/detach.svg +4 -0
- package/assets/icons/document.svg +4 -0
- package/assets/icons/download.svg +4 -0
- package/assets/icons/duplicate.svg +4 -0
- package/assets/icons/edit.svg +4 -0
- package/assets/icons/expand.svg +4 -0
- package/assets/icons/export.svg +4 -0
- package/assets/icons/eye.svg +4 -0
- package/assets/icons/filter.svg +4 -0
- package/assets/icons/first.svg +4 -0
- package/assets/icons/folder.svg +4 -0
- package/assets/icons/group.svg +4 -0
- package/assets/icons/help.svg +4 -0
- package/assets/icons/home.svg +4 -0
- package/assets/icons/import.svg +4 -0
- package/assets/icons/info.svg +4 -0
- package/assets/icons/inspector.svg +4 -0
- package/assets/icons/last.svg +4 -0
- package/assets/icons/layers.svg +4 -0
- package/assets/icons/link.svg +4 -0
- package/assets/icons/list.svg +4 -0
- package/assets/icons/lock.svg +4 -0
- package/assets/icons/logout.svg +4 -0
- package/assets/icons/navigation.svg +4 -0
- package/assets/icons/new.svg +4 -0
- package/assets/icons/next.svg +4 -0
- package/assets/icons/open.svg +4 -0
- package/assets/icons/palette.svg +4 -0
- package/assets/icons/panel-bottom.svg +4 -0
- package/assets/icons/panel-left.svg +4 -0
- package/assets/icons/panel-right.svg +4 -0
- package/assets/icons/panel-top.svg +4 -0
- package/assets/icons/paste.svg +4 -0
- package/assets/icons/pause.svg +4 -0
- package/assets/icons/pin.svg +4 -0
- package/assets/icons/play.svg +4 -0
- package/assets/icons/previous.svg +4 -0
- package/assets/icons/print.svg +4 -0
- package/assets/icons/redo.svg +4 -0
- package/assets/icons/refresh.svg +4 -0
- package/assets/icons/save.svg +4 -0
- package/assets/icons/search.svg +4 -0
- package/assets/icons/send.svg +4 -0
- package/assets/icons/settings.svg +4 -0
- package/assets/icons/sort-ascending.svg +4 -0
- package/assets/icons/sort-descending.svg +4 -0
- package/assets/icons/stop.svg +4 -0
- package/assets/icons/table.svg +4 -0
- package/assets/icons/tree.svg +4 -0
- package/assets/icons/undo.svg +4 -0
- package/assets/icons/unlock.svg +4 -0
- package/assets/icons/upload.svg +4 -0
- package/assets/icons/user.svg +4 -0
- package/assets/icons/warning.svg +4 -0
- package/assets/icons/zoom-in.svg +4 -0
- package/assets/icons/zoom-out.svg +4 -0
- package/dist/components/charts/__init__.py +1 -1
- package/dist/components/charts/assets/index.js +1 -1
- package/dist/components/charts/assets/libraries/vega.js +11 -11
- package/dist/components/editors/__init__.py +1 -1
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/editors/assets/libraries/codemirror.js +30 -31
- package/dist/components/editors/assets/libraries/jodit.js +3 -1
- package/dist/components/editors/grammar.json +6 -1
- package/dist/components/grid/__init__.py +1 -1
- package/dist/components/grid/assets/index.js +1 -1
- package/dist/components/grid/commands.json +4 -6
- package/dist/components/grid/grammar.json +51 -0
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/inputs/grammar.json +4987 -216
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/layout/grammar.json +69 -4
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/components/tree/grammar.json +16 -0
- package/dist/components/typst/__init__.py +1 -1
- package/dist/components/typst/assets/collection.js +6 -6
- package/dist/components/typst/assets/index.js +1 -1
- package/dist/components/uploads/__init__.py +1 -1
- package/dist/components/uploads/assets/index.js +1 -1
- package/dist/components/uploads/grammar.json +66 -0
- package/dist/declared-collections.js +1 -1
- package/dist/gramlot.js +3961 -1249
- package/dist/gramlot.min.js +329 -131
- package/dist/libraries.json +1 -0
- package/dist/standard-collections.js +1 -1
- package/dist/standard-grammar.js +1 -1
- package/package.json +3 -1
- package/src/binding/ask-dialog.js +17 -3
- package/src/binding/providers.js +1 -1
- package/src/binding/runtime.js +6 -3
- package/src/builder/chart-icon.js +2 -0
- package/src/builder/gramlot-builder.d.ts +3 -1
- package/src/builder/gramlot-builder.js +26 -9
- package/src/builder/source.js +12 -3
- package/src/builder/xml-recipe.js +144 -0
- package/src/chat-file-types.js +13 -0
- package/src/chat.js +23 -25
- package/src/commands.d.ts +1 -1
- package/src/commands.js +1 -1
- package/src/components/border-container.js +64 -6
- package/src/components/buttons.js +26 -31
- package/src/components/chart-geographic-options.js +1 -1
- package/src/components/chart-pane.js +5 -5
- package/src/components/chart-spatial-options.js +5 -1
- package/src/components/check-box-text-editor.js +1 -0
- package/src/components/check-box-text.js +2 -0
- package/src/components/choice-editor.js +3 -2
- package/src/components/choice-field-styles.js +9 -1
- package/src/components/code-mirror-engine.js +18 -5
- package/src/components/code-mirror-gramlot.js +85 -0
- package/src/components/code-mirror.js +3 -2
- package/src/components/drawing.js +43 -14
- package/src/components/editor-options.js +1 -1
- package/src/components/graphical-ranges.js +177 -0
- package/src/components/grid-footer.js +6 -1
- package/src/components/grid-styles.js +11 -2
- package/src/components/grid-tools.js +15 -7
- package/src/components/grid.js +69 -8
- package/src/components/inputs.js +93 -23
- package/src/components/jodit-engine.js +3 -1
- package/src/components/menus.js +83 -25
- package/src/components/numeric-range.js +39 -0
- package/src/components/selected-container.js +109 -63
- package/src/components/tab-bar.js +371 -0
- package/src/components/toolbar-icon-style.js +22 -0
- package/src/components/toolbar-overflow.js +74 -12
- package/src/components/tree.js +12 -3
- package/src/components/typst-preview.js +3 -3
- package/src/components/upload-blocks.js +88 -0
- package/src/components/upload-engine.js +21 -31
- package/src/components/uploader.js +168 -47
- package/src/gramlot.js +6 -3
- package/src/handlers/dom-handler.d.ts +2 -0
- package/src/handlers/dom-handler.js +9 -0
- package/src/handlers/rpc-handler.d.ts +1 -1
- package/src/handlers/rpc-handler.js +2 -0
- package/src/handlers/source-handler.d.ts +5 -3
- package/src/handlers/source-handler.js +31 -13
- package/src/icons.js +84 -0
- package/src/index-page-runtime.js +9 -2
- package/src/index-page-styles.js +1 -1
- package/src/index-theme.js +210 -3
- package/src/index.d.ts +5 -1
- package/src/index.js +5 -1
- package/src/libraries.js +1 -1
- package/src/renderer/gramlot-renderer.js +4 -1
- package/src/server/chat-attachments.js +139 -0
- package/src/server/chat.d.ts +31 -0
- package/src/server/chat.js +157 -0
- package/src/server/database-selectors.d.ts +6 -0
- package/src/server/database-selectors.js +13 -0
- package/src/server/gramlot-file-server.js +6 -1
- package/src/server/gramlot-server.d.ts +19 -3
- package/src/server/gramlot-server.js +144 -18
- package/src/server/index-page.d.ts +10 -0
- package/src/server/index-page.js +73 -8
- package/src/server/index.d.ts +1 -1
- package/src/server/index.js +1 -1
- package/src/server/page-base.js +4 -0
- package/src/server/page-store.d.ts +21 -0
- package/src/server/page-store.js +219 -0
- package/src/server/page.d.ts +6 -1
- package/src/server/page.js +5 -1
- package/src/server/store-recipe.js +102 -0
- package/src/server/toolbar-icon-name.d.ts +76 -0
- package/src/server/upload.d.ts +2 -0
- package/src/server/upload.js +36 -18
- package/src/server/visual-editor.js +4 -2
- package/src/tree-store.d.ts +5 -1
- package/src/tree-store.js +11 -2
- package/src/view/choice-focus.js +17 -0
- package/src/view/html.js +21 -1
- package/src/view/menu.js +9 -3
- package/src/visual-editor.js +6 -2
- package/themes/gramlot-base/theme.css +53 -28
- package/themes/gramlot-compact/theme.css +2 -0
- package/themes/gramlot-index/icons/applications.svg +4 -0
- package/themes/gramlot-index/icons/archive.svg +4 -0
- package/themes/gramlot-index/icons/attachment.svg +4 -0
- package/themes/gramlot-index/icons/book.svg +4 -0
- package/themes/gramlot-index/icons/bookmark.svg +4 -0
- package/themes/gramlot-index/icons/calendar.svg +4 -0
- package/themes/gramlot-index/icons/cancel.svg +4 -0
- package/themes/gramlot-index/icons/chart.svg +4 -0
- package/themes/gramlot-index/icons/chat.svg +4 -0
- package/themes/gramlot-index/icons/clock.svg +4 -0
- package/themes/gramlot-index/icons/code.svg +4 -0
- package/themes/gramlot-index/icons/collapse.svg +4 -0
- package/themes/gramlot-index/icons/columns.svg +4 -0
- package/themes/gramlot-index/icons/confirm.svg +4 -0
- package/themes/gramlot-index/icons/copy.svg +4 -0
- package/themes/gramlot-index/icons/cut.svg +4 -0
- package/themes/gramlot-index/icons/database.svg +4 -0
- package/themes/gramlot-index/icons/delete.svg +4 -0
- package/themes/gramlot-index/icons/detach.svg +4 -0
- package/themes/gramlot-index/icons/document.svg +4 -0
- package/themes/gramlot-index/icons/download.svg +4 -0
- package/themes/gramlot-index/icons/duplicate.svg +4 -0
- package/themes/gramlot-index/icons/edit.svg +4 -0
- package/themes/gramlot-index/icons/expand.svg +4 -0
- package/themes/gramlot-index/icons/export.svg +4 -0
- package/themes/gramlot-index/icons/eye.svg +4 -0
- package/themes/gramlot-index/icons/filter.svg +4 -0
- package/themes/gramlot-index/icons/first.svg +4 -0
- package/themes/gramlot-index/icons/folder.svg +4 -0
- package/themes/gramlot-index/icons/group.svg +4 -0
- package/themes/gramlot-index/icons/help.svg +4 -0
- package/themes/gramlot-index/icons/home.svg +4 -0
- package/themes/gramlot-index/icons/import.svg +4 -0
- package/themes/gramlot-index/icons/info.svg +4 -0
- package/themes/gramlot-index/icons/inspector.svg +4 -0
- package/themes/gramlot-index/icons/last.svg +4 -0
- package/themes/gramlot-index/icons/layers.svg +4 -0
- package/themes/gramlot-index/icons/link.svg +4 -0
- package/themes/gramlot-index/icons/list.svg +4 -0
- package/themes/gramlot-index/icons/lock.svg +4 -0
- package/themes/gramlot-index/icons/logout.svg +4 -0
- package/themes/gramlot-index/icons/navigation.svg +4 -0
- package/themes/gramlot-index/icons/new.svg +4 -0
- package/themes/gramlot-index/icons/next.svg +4 -0
- package/themes/gramlot-index/icons/open.svg +4 -0
- package/themes/gramlot-index/icons/palette.svg +4 -0
- package/themes/gramlot-index/icons/panel-bottom.svg +4 -0
- package/themes/gramlot-index/icons/panel-left.svg +4 -0
- package/themes/gramlot-index/icons/panel-right.svg +4 -0
- package/themes/gramlot-index/icons/panel-top.svg +4 -0
- package/themes/gramlot-index/icons/paste.svg +4 -0
- package/themes/gramlot-index/icons/pause.svg +4 -0
- package/themes/gramlot-index/icons/pin.svg +4 -0
- package/themes/gramlot-index/icons/play.svg +4 -0
- package/themes/gramlot-index/icons/previous.svg +4 -0
- package/themes/gramlot-index/icons/print.svg +4 -0
- package/themes/gramlot-index/icons/redo.svg +4 -0
- package/themes/gramlot-index/icons/refresh.svg +4 -0
- package/themes/gramlot-index/icons/save.svg +4 -0
- package/themes/gramlot-index/icons/search.svg +4 -0
- package/themes/gramlot-index/icons/send.svg +4 -0
- package/themes/gramlot-index/icons/settings.svg +4 -0
- package/themes/gramlot-index/icons/sort-ascending.svg +4 -0
- package/themes/gramlot-index/icons/sort-descending.svg +4 -0
- package/themes/gramlot-index/icons/stop.svg +4 -0
- package/themes/gramlot-index/icons/table.svg +4 -0
- package/themes/gramlot-index/icons/tree.svg +4 -0
- package/themes/gramlot-index/icons/undo.svg +4 -0
- package/themes/gramlot-index/icons/unlock.svg +4 -0
- package/themes/gramlot-index/icons/upload.svg +4 -0
- package/themes/gramlot-index/icons/user.svg +4 -0
- package/themes/gramlot-index/icons/warning.svg +4 -0
- package/themes/gramlot-index/icons/zoom-in.svg +4 -0
- package/themes/gramlot-index/icons/zoom-out.svg +4 -0
- package/themes/gramlot-index/icons.json +76 -0
- package/themes/gramlot-index/theme.css +21 -0
- package/themes/joanna/theme.css +2 -0
- package/themes/mela/theme.css +2 -0
- package/themes/mimi/theme.css +2 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {fromTytx} from '@genrojs/tytx';
|
|
2
|
+
|
|
3
|
+
const blockSize = 256 * 1024;
|
|
4
|
+
|
|
5
|
+
function waitForRetry(signal, delay) {
|
|
6
|
+
signal?.throwIfAborted();
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const abort = () => { clearTimeout(timer); reject(signal.reason); };
|
|
9
|
+
const timer = setTimeout(() => {
|
|
10
|
+
signal?.removeEventListener('abort', abort);
|
|
11
|
+
resolve();
|
|
12
|
+
}, delay);
|
|
13
|
+
signal?.addEventListener('abort', abort, {once:true});
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function postBlock(window, url, headers, block, signal) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const xhr = new window.XMLHttpRequest();
|
|
20
|
+
xhr.open('POST', url);
|
|
21
|
+
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
|
|
22
|
+
for (const [name, value] of Object.entries(headers)) xhr.setRequestHeader(name, value);
|
|
23
|
+
const abort = () => xhr.abort();
|
|
24
|
+
const finish = fn => () => { signal?.removeEventListener('abort', abort); fn(); };
|
|
25
|
+
xhr.addEventListener('load', finish(() => {
|
|
26
|
+
try {
|
|
27
|
+
if (xhr.status !== 200) throw new Error(`Upload HTTP ${xhr.status}`);
|
|
28
|
+
resolve(fromTytx(xhr.responseText));
|
|
29
|
+
} catch (error) { reject(error); }
|
|
30
|
+
}));
|
|
31
|
+
xhr.addEventListener('error', finish(() => reject(new TypeError('Upload connection failed'))));
|
|
32
|
+
xhr.addEventListener('abort', finish(() => reject(new Error('Upload cancelled'))));
|
|
33
|
+
signal?.addEventListener('abort', abort, {once:true});
|
|
34
|
+
xhr.send(block);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function readBlock(window, blob) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const reader = new window.FileReader();
|
|
41
|
+
reader.onload = () => resolve(new Uint8Array(reader.result));
|
|
42
|
+
reader.onerror = () => reject(reader.error);
|
|
43
|
+
reader.readAsArrayBuffer(blob);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One checkpoint per complete block. A lost response is reconciled before another write. */
|
|
48
|
+
export async function sendUploadBlocks(page, file, grant, {window, offset = 0, signal, progress = () => {}}) {
|
|
49
|
+
const {pageId, rpc, uploadUrl} = page;
|
|
50
|
+
const {transferId, authorization} = grant;
|
|
51
|
+
let delay = 250;
|
|
52
|
+
for (;;) {
|
|
53
|
+
signal?.throwIfAborted();
|
|
54
|
+
const end = Math.min(offset + blockSize, file.size);
|
|
55
|
+
const block = file.slice(offset, end);
|
|
56
|
+
let outcome;
|
|
57
|
+
try {
|
|
58
|
+
outcome = uploadUrl ? await postBlock(window, uploadUrl, {
|
|
59
|
+
'X-Gramlot-Page-Id':pageId, 'X-Gramlot-Transfer-Id':transferId,
|
|
60
|
+
'X-Gramlot-Upload-Token':authorization, 'X-Gramlot-Upload-Offset':String(offset),
|
|
61
|
+
}, block, signal) : await rpc.transport.upload({pageId, transferId, authorization, offset, block:await readBlock(window, block)}, progress, signal);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
for (;;) {
|
|
64
|
+
signal?.throwIfAborted();
|
|
65
|
+
try {
|
|
66
|
+
outcome = await rpc.call('upload', 'status', {transferId}, {signal});
|
|
67
|
+
break;
|
|
68
|
+
} catch (statusError) {
|
|
69
|
+
if (statusError.name !== 'TypeError' && !page._pageStore?.reconnecting) throw statusError;
|
|
70
|
+
await waitForRetry(signal, delay);
|
|
71
|
+
delay = Math.min(delay * 2, 5000);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (outcome.state === 'pending' && outcome.offset === offset) {
|
|
75
|
+
if (!(error instanceof TypeError)) throw error;
|
|
76
|
+
await waitForRetry(signal, delay);
|
|
77
|
+
delay = Math.min(delay * 2, 5000);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (outcome.state === 'completed') progress(file.size);
|
|
82
|
+
if (outcome.state !== 'pending' || outcome.offset === undefined) return outcome;
|
|
83
|
+
if (outcome.offset !== end || end === offset || end === file.size) throw new Error('Invalid upload checkpoint');
|
|
84
|
+
offset = end;
|
|
85
|
+
progress(offset);
|
|
86
|
+
delay = 250;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** One browser queue, independent of its presentation. Files and grants stay private. */
|
|
2
2
|
import {Bag} from '@genrojs/bag';
|
|
3
|
-
import {
|
|
3
|
+
import {sendUploadBlocks} from './upload-blocks.js';
|
|
4
4
|
import {randomId} from '../random-id.js';
|
|
5
5
|
|
|
6
6
|
/** Read the optional shared context at preparation time, independently of file fields. */
|
|
@@ -53,7 +53,7 @@ export class UploadEngine {
|
|
|
53
53
|
if (!this.gramlot.capabilities.includes('upload')) {
|
|
54
54
|
this.#update(id, {status:'failed', error:'Upload is unavailable in this environment'}); return;
|
|
55
55
|
}
|
|
56
|
-
const state = {
|
|
56
|
+
const state = {controller:new AbortController(), transferId:null, cancelled:false};
|
|
57
57
|
this.#active.set(id, state);
|
|
58
58
|
this.#update(id, {status:'preparing', error:null});
|
|
59
59
|
try {
|
|
@@ -61,13 +61,13 @@ export class UploadEngine {
|
|
|
61
61
|
metadata:{name:file.name, size:file.size, mediaType:file.type || '',
|
|
62
62
|
fields:row.getItem('metadata').asDict(false, false, true), context:this.getContext()}});
|
|
63
63
|
state.transferId = grant.transferId;
|
|
64
|
-
this.#transfers.set(id, grant
|
|
64
|
+
this.#transfers.set(id, grant);
|
|
65
65
|
if (state.cancelled || this.#closed) {
|
|
66
66
|
const outcome = await this.gramlot.rpc.call('upload', 'cancel', {transferId:state.transferId});
|
|
67
67
|
await this.#apply(id, outcome, true); return;
|
|
68
68
|
}
|
|
69
69
|
this.#update(id, {status:'uploading'});
|
|
70
|
-
const result = await this
|
|
70
|
+
const result = await sendUploadBlocks(this.gramlot, file, grant, {window:this.window, signal:state.controller.signal, progress:loaded => this.#update(id, {loaded})});
|
|
71
71
|
await this.#apply(id, result, state.cancelled);
|
|
72
72
|
} catch (error) {
|
|
73
73
|
if (state.transferId) {
|
|
@@ -83,28 +83,6 @@ export class UploadEngine {
|
|
|
83
83
|
this.#active.delete(id);
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
#send(file, state, token, id) {
|
|
87
|
-
if (!this.gramlot.uploadUrl) {
|
|
88
|
-
return this.gramlot.rpc.transport.upload({pageId:this.gramlot.pageId, transferId:state.transferId,
|
|
89
|
-
authorization:token, file}, loaded => this.#update(id, {loaded}), state.controller.signal);
|
|
90
|
-
}
|
|
91
|
-
return new Promise((resolve, reject) => {
|
|
92
|
-
const request = state.xhr = new this.window.XMLHttpRequest();
|
|
93
|
-
request.open('POST', this.gramlot.uploadUrl, true);
|
|
94
|
-
request.setRequestHeader('Content-Type', 'application/octet-stream');
|
|
95
|
-
request.setRequestHeader('X-Gramlot-Page-Id', this.gramlot.pageId);
|
|
96
|
-
request.setRequestHeader('X-Gramlot-Transfer-Id', state.transferId);
|
|
97
|
-
request.setRequestHeader('X-Gramlot-Upload-Token', token);
|
|
98
|
-
request.upload.addEventListener('progress', event => this.#update(id, {loaded:event.loaded}));
|
|
99
|
-
request.addEventListener('load', () => {
|
|
100
|
-
if (request.status !== 200) { reject(new Error(`Upload HTTP ${request.status}`)); return; }
|
|
101
|
-
try { resolve(fromTytx(request.responseText)); } catch (error) { reject(error); }
|
|
102
|
-
}, {once:true});
|
|
103
|
-
request.addEventListener('error', () => reject(new Error('Upload connection failed')), {once:true});
|
|
104
|
-
request.addEventListener('abort', () => reject(new Error('Upload cancelled')), {once:true});
|
|
105
|
-
request.send(file);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
86
|
async #apply(id, outcome, cancelled = false) {
|
|
109
87
|
if (outcome.state === 'completed') {
|
|
110
88
|
this.#update(id, {status:'completed', result:outcome.result, error:null});
|
|
@@ -132,15 +110,28 @@ export class UploadEngine {
|
|
|
132
110
|
return true;
|
|
133
111
|
}
|
|
134
112
|
async reconcile(id) {
|
|
135
|
-
const
|
|
136
|
-
if (!
|
|
113
|
+
const grant = this.#transfers.get(id);
|
|
114
|
+
if (!grant || this.#closed || this.#active.has(id) || this.#checking.has(id)) return;
|
|
137
115
|
this.#checking.add(id);
|
|
116
|
+
const state = {transferId:grant.transferId, controller:new AbortController(), cancelled:false};
|
|
138
117
|
try {
|
|
139
|
-
|
|
118
|
+
this.#active.set(id, state);
|
|
119
|
+
let outcome = await this.gramlot.rpc.call('upload', 'status', {transferId:grant.transferId});
|
|
120
|
+
if (outcome.state === 'pending' && Number.isSafeInteger(outcome.offset)) {
|
|
121
|
+
outcome = await sendUploadBlocks(this.gramlot, this.#files.get(id), grant, {
|
|
122
|
+
window:this.window, offset:outcome.offset, signal:state.controller.signal, progress:loaded => this.#update(id, {loaded}),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
140
125
|
await this.#apply(id, outcome);
|
|
141
126
|
} catch (error) {
|
|
142
|
-
|
|
127
|
+
if (state.cancelled) {
|
|
128
|
+
try {
|
|
129
|
+
const outcome = await this.gramlot.rpc.call('upload', 'cancel', {transferId:grant.transferId});
|
|
130
|
+
await this.#apply(id, outcome, true);
|
|
131
|
+
} catch (recoveryError) { this.#update(id, {status:'uncertain', error:recoveryError.message}); }
|
|
132
|
+
} else this.#update(id, {status:'uncertain', error:error.message});
|
|
143
133
|
} finally {
|
|
134
|
+
this.#active.delete(id);
|
|
144
135
|
this.#checking.delete(id);
|
|
145
136
|
}
|
|
146
137
|
}
|
|
@@ -154,7 +145,6 @@ export class UploadEngine {
|
|
|
154
145
|
if (!state) return;
|
|
155
146
|
state.cancelled = true;
|
|
156
147
|
state.controller.abort();
|
|
157
|
-
state.xhr?.abort();
|
|
158
148
|
}
|
|
159
149
|
dispose() {
|
|
160
150
|
this.#closed = true;
|
|
@@ -2,15 +2,79 @@
|
|
|
2
2
|
import {Bag} from '@genrojs/bag';
|
|
3
3
|
import {defineComponent} from './registry.js';
|
|
4
4
|
import {Container} from './container.js';
|
|
5
|
+
import icons from '../../themes/gramlot-index/icons.json' with {type:'json'};
|
|
5
6
|
import {UploadEngine, readUploadContext} from './upload-engine.js';
|
|
6
7
|
|
|
8
|
+
// Built-in texts by language of the page locale; English when the language has none.
|
|
9
|
+
const TEXTS = {
|
|
10
|
+
en: {drop:['Drag a file here or', 'Drag files here or'], choose:['choose it from your computer', 'choose them from your computer'],
|
|
11
|
+
accepted:'Accepted', limit:'Per file', tooLarge:(name, limit) => `${name}: exceeds the ${limit} limit`,
|
|
12
|
+
summary:'Upload summary', filePreview:'File preview', upload:'Upload', remove:'Remove', preview:'Preview', cancel:'Cancel', retry:'Retry',
|
|
13
|
+
check:'Check status', one:'Select one file', notAccepted:name => `${name}: file type is not accepted`,
|
|
14
|
+
files:count => `${count} file${count === 1 ? '' : 's'}`, select:name => `Select ${name}`,
|
|
15
|
+
noPreview:'No preview available', previewFailed:message => `Preview failed: ${message}`,
|
|
16
|
+
status:{queued:'queued', preparing:'preparing', uploading:'uploading', completed:'completed', failed:'failed',
|
|
17
|
+
cancelled:'cancelled', uncertain:'uncertain'}},
|
|
18
|
+
it: {drop:['Trascina qui il file oppure', 'Trascina qui i file oppure'], choose:['sceglilo dal computer', 'sceglili dal computer'],
|
|
19
|
+
accepted:'Formati accettati', limit:'Limite per file', tooLarge:(name, limit) => `${name}: supera il limite di ${limit}`,
|
|
20
|
+
summary:'Riepilogo caricamenti', filePreview:'Anteprima file', upload:'Carica', remove:'Rimuovi', preview:'Anteprima', cancel:'Annulla', retry:'Riprova',
|
|
21
|
+
check:'Verifica', one:'Scegli un solo file', notAccepted:name => `${name}: formato non accettato`,
|
|
22
|
+
files:count => `${count} file`, select:name => `Scegli ${name}`,
|
|
23
|
+
noPreview:'Anteprima non disponibile', previewFailed:message => `Anteprima non riuscita: ${message}`,
|
|
24
|
+
status:{queued:'in coda', preparing:'in preparazione', uploading:'in caricamento', completed:'caricato',
|
|
25
|
+
failed:'non riuscito', cancelled:'annullato', uncertain:'da verificare'}},
|
|
26
|
+
};
|
|
27
|
+
const STYLE = `
|
|
28
|
+
:host{display:block}
|
|
29
|
+
button,input{font:inherit}
|
|
30
|
+
.drop{display:grid;justify-items:center;gap:.3rem;padding:1.4rem 1rem;text-align:center;cursor:pointer;
|
|
31
|
+
color:var(--gramlot-ink,inherit);background:var(--gramlot-surface-subtle,transparent);
|
|
32
|
+
border:1.5px dashed var(--gramlot-border-control,#8a8a8a);border-radius:var(--gramlot-radius,4px);
|
|
33
|
+
transition:border-color .12s,background-color .12s}
|
|
34
|
+
.drop:hover,.drop[data-over]{border-color:var(--gramlot-action,#1a5fb4)}
|
|
35
|
+
.drop[data-over]{background:color-mix(in srgb,var(--gramlot-action,#1a5fb4) 9%,transparent)}
|
|
36
|
+
.drop[aria-disabled=true]{opacity:.55;cursor:default}
|
|
37
|
+
.icon{inline-size:1.9rem;block-size:1.9rem;background:var(--gramlot-text-secondary,currentColor);mask:url("${icons.upload}") center / contain no-repeat}
|
|
38
|
+
.choose{padding:0;border:0;background:none;color:var(--gramlot-action,#1a5fb4);font-weight:600;cursor:pointer;text-decoration:underline;text-underline-offset:.15em}
|
|
39
|
+
button:focus-visible{outline:1px solid var(--gramlot-focus-ring,Highlight);outline-offset:-1px}
|
|
40
|
+
.hint{color:var(--gramlot-text-secondary,inherit);font-size:.875em}
|
|
41
|
+
.message:empty,.summary:empty,.preview:empty,.files:empty{display:none}
|
|
42
|
+
.message{color:var(--gramlot-error,#b13d48)}
|
|
43
|
+
.summary{color:var(--gramlot-text-secondary,inherit);font-size:.875em}
|
|
44
|
+
.files{display:grid;gap:.4rem;margin:.6rem 0 0;padding:0;list-style:none}
|
|
45
|
+
.files li{display:grid;grid-template-columns:minmax(0,1fr) auto;align-items:center;gap:.3rem .8rem;padding:.5rem .7rem;
|
|
46
|
+
background:var(--gramlot-surface,transparent);border:1px solid var(--gramlot-border,#d0d0d0);border-radius:var(--gramlot-radius,4px)}
|
|
47
|
+
.files li[data-selected]{border-color:var(--gramlot-action,#1a5fb4)}
|
|
48
|
+
.file{display:flex;flex-wrap:wrap;align-items:baseline;gap:.2rem .6rem;min-inline-size:0}
|
|
49
|
+
.name{padding:0;border:0;background:none;color:inherit;font-weight:600;cursor:pointer;text-align:start;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-inline-size:100%}
|
|
50
|
+
.meta{color:var(--gramlot-text-secondary,inherit);font-size:.875em}
|
|
51
|
+
.files li[data-state=failed] .meta,.error{color:var(--gramlot-error,#b13d48)}
|
|
52
|
+
.files li[data-state=completed] .meta{color:var(--gramlot-success,var(--gramlot-text-secondary,inherit))}
|
|
53
|
+
.actions{display:flex;gap:.4rem}
|
|
54
|
+
.actions button{padding:.2rem .6rem;border:0;border-radius:var(--gramlot-radius,4px);
|
|
55
|
+
background:var(--gramlot-button-background,ButtonFace);color:inherit;font-size:.875em;cursor:pointer}
|
|
56
|
+
.actions button:hover{background:var(--gramlot-button-hover,ButtonFace)}
|
|
57
|
+
progress{grid-column:1/-1;inline-size:100%;block-size:.3rem;accent-color:var(--gramlot-action,#1a5fb4)}
|
|
58
|
+
.error{grid-column:1/-1;font-size:.875em}
|
|
59
|
+
.start{justify-self:start}
|
|
60
|
+
.preview{margin-block-start:.5rem}`;
|
|
61
|
+
function sizeText(bytes, language) {
|
|
62
|
+
const units = [[1048576, 'MB'], [1024, 'kB']];
|
|
63
|
+
for (const [factor, unit] of units) {
|
|
64
|
+
if (bytes >= factor) return `${(bytes / factor).toLocaleString(language, {maximumFractionDigits:1})} ${unit}`;
|
|
65
|
+
}
|
|
66
|
+
return `${bytes} ${language === 'it' ? 'byte' : 'bytes'}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
7
69
|
export const Uploader = defineComponent({
|
|
8
70
|
name:'uploader', tag:'gramlot-uploader', base:Container, sourceContext:true,
|
|
9
71
|
parameters:{operation:{dtype:'T'}, storepath:{dtype:'T'}, contextPath:{dtype:'T',nullable:true,default:null}, accept:{dtype:'T', default:'', binding:'read'},
|
|
10
72
|
multiple:{dtype:'B', default:false, binding:'read'}, autoUpload:{dtype:'B', default:true, binding:'read'},
|
|
11
73
|
manualUpload:{dtype:'B', default:true, binding:'read'},
|
|
12
74
|
disabled:{dtype:'B', default:false, binding:'read'},
|
|
13
|
-
selectedPath:{dtype:'T', nullable:true, default:null, binding:'twoWay'}, onComplete:{dtype:'T', nullable:true, default:null}
|
|
75
|
+
selectedPath:{dtype:'T', nullable:true, default:null, binding:'twoWay'}, onComplete:{dtype:'T', nullable:true, default:null},
|
|
76
|
+
caption:{dtype:'T', nullable:true, default:null, binding:'read'},
|
|
77
|
+
maxBytes:{dtype:'L', nullable:true, default:null, binding:'read'}},
|
|
14
78
|
implementation: Base => class extends Base {
|
|
15
79
|
#context; #engine; #draining = false; #previewUrl = null; #previewId = null;
|
|
16
80
|
#requestedPath; #selectedPath = null;
|
|
@@ -19,25 +83,47 @@ export const Uploader = defineComponent({
|
|
|
19
83
|
this.control.replaceChildren();
|
|
20
84
|
const doc = this.ownerDocument;
|
|
21
85
|
const style = doc.createElement('style');
|
|
22
|
-
style.textContent =
|
|
86
|
+
style.textContent = STYLE;
|
|
23
87
|
this.drop = doc.createElement('div'); this.drop.className = 'drop';
|
|
24
|
-
const
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
88
|
+
const icon = doc.createElement('span'); icon.className = 'icon'; icon.setAttribute('aria-hidden', 'true');
|
|
89
|
+
this.lead = doc.createElement('div');
|
|
90
|
+
this.leadText = doc.createElement('span');
|
|
91
|
+
this.chooseButton = doc.createElement('button'); this.chooseButton.type = 'button'; this.chooseButton.className = 'choose';
|
|
92
|
+
this.lead.append(this.leadText, doc.createTextNode(' '), this.chooseButton);
|
|
93
|
+
this.hint = doc.createElement('div'); this.hint.className = 'hint';
|
|
94
|
+
this.input = doc.createElement('input'); this.input.type = 'file'; this.input.hidden = true;
|
|
95
|
+
this.drop.append(icon, this.lead, this.hint, this.input);
|
|
96
|
+
this.startButton = doc.createElement('button'); this.startButton.type = 'button'; this.startButton.className = 'start';
|
|
97
|
+
this.message = doc.createElement('div'); this.message.className = 'message'; this.message.setAttribute('role','status');
|
|
98
|
+
this.summary = doc.createElement('div'); this.summary.className = 'summary';
|
|
30
99
|
this.summary.setAttribute('aria-live', 'polite');
|
|
31
|
-
this.list = doc.createElement('ul');
|
|
32
|
-
this.preview = doc.createElement('div'); this.preview.
|
|
33
|
-
this.
|
|
100
|
+
this.list = doc.createElement('ul'); this.list.className = 'files';
|
|
101
|
+
this.preview = doc.createElement('div'); this.preview.className = 'preview';
|
|
102
|
+
this.control.append(this.drop, this.startButton, this.message, this.summary, this.list, this.preview);
|
|
34
103
|
this.shadowRoot.append(style);
|
|
104
|
+
this.chooseButton.addEventListener('click', event => { event.stopPropagation(); if (!this.parameters?.disabled) this.input.click(); });
|
|
105
|
+
this.drop.addEventListener('click', event => {
|
|
106
|
+
if (event.target !== this.input && !this.parameters?.disabled) this.input.click();
|
|
107
|
+
});
|
|
108
|
+
const over = event => {
|
|
109
|
+
if (!Array.from(event.dataTransfer.types).includes('Files')) return;
|
|
110
|
+
event.preventDefault(); event.stopPropagation();
|
|
111
|
+
event.dataTransfer.dropEffect = this.parameters.disabled ? 'none' : 'copy';
|
|
112
|
+
this.drop.toggleAttribute('data-over', !this.parameters.disabled);
|
|
113
|
+
};
|
|
114
|
+
this.drop.addEventListener('dragenter', over);
|
|
115
|
+
this.drop.addEventListener('dragover', over);
|
|
116
|
+
this.drop.addEventListener('dragleave', event => { if (!this.drop.contains(event.relatedTarget)) delete this.drop.dataset.over; });
|
|
35
117
|
this.input.addEventListener('change', () => { this.#add(this.input.files); this.input.value = ''; });
|
|
36
118
|
this.startButton.addEventListener('click', () => {
|
|
37
119
|
if (this.parameters.manualUpload && !this.parameters.autoUpload) void this.#startAll();
|
|
38
120
|
});
|
|
39
|
-
this.drop.addEventListener('
|
|
40
|
-
|
|
121
|
+
this.drop.addEventListener('drop', event => {
|
|
122
|
+
this.drop.removeAttribute('data-over');
|
|
123
|
+
if (!Array.from(event.dataTransfer.types).includes('Files')) return;
|
|
124
|
+
event.preventDefault(); event.stopPropagation();
|
|
125
|
+
this.#add(event.dataTransfer.files);
|
|
126
|
+
});
|
|
41
127
|
}
|
|
42
128
|
setSourceContext(context) { this.#context = context; }
|
|
43
129
|
/** The queue Bag, read at the storepath address at each use; the uploader never keeps it. */
|
|
@@ -60,6 +146,22 @@ export const Uploader = defineComponent({
|
|
|
60
146
|
this.input.disabled = Boolean(this.parameters.disabled);
|
|
61
147
|
this.startButton.disabled = Boolean(this.parameters.disabled);
|
|
62
148
|
this.startButton.hidden = Boolean(this.parameters.autoUpload || !this.parameters.manualUpload);
|
|
149
|
+
const texts = this.#texts(), many = this.parameters.multiple ? 1 : 0;
|
|
150
|
+
this.leadText.textContent = this.parameters.caption || texts.drop[many];
|
|
151
|
+
this.chooseButton.textContent = texts.choose[many];
|
|
152
|
+
this.chooseButton.disabled = Boolean(this.parameters.disabled);
|
|
153
|
+
this.drop.setAttribute('aria-disabled', String(Boolean(this.parameters.disabled)));
|
|
154
|
+
const {accept, maxBytes, disabled} = this.parameters;
|
|
155
|
+
if (maxBytes !== null && (!Number.isSafeInteger(maxBytes) || maxBytes < 0))
|
|
156
|
+
throw new TypeError('maxBytes must be a nonnegative safe integer');
|
|
157
|
+
const hints = [];
|
|
158
|
+
if (accept) hints.push(`${texts.accepted}: ${accept.split(',').map(value => value.trim()).join(', ')}`);
|
|
159
|
+
if (maxBytes !== null) hints.push(`${texts.limit}: ${sizeText(maxBytes, this.#language())}`);
|
|
160
|
+
this.hint.textContent = hints.join(' · ');
|
|
161
|
+
this.summary.setAttribute('aria-label', texts.summary);
|
|
162
|
+
this.preview.setAttribute('aria-label', texts.filePreview);
|
|
163
|
+
if (disabled) this.drop.removeAttribute('data-over');
|
|
164
|
+
this.startButton.textContent = texts.upload;
|
|
63
165
|
if (this.parameters.selectedPath !== this.#requestedPath) {
|
|
64
166
|
this.#requestedPath = this.parameters.selectedPath;
|
|
65
167
|
this.#selectedPath = this.parameters.selectedPath;
|
|
@@ -74,6 +176,11 @@ export const Uploader = defineComponent({
|
|
|
74
176
|
}
|
|
75
177
|
return rest;
|
|
76
178
|
}
|
|
179
|
+
#language() {
|
|
180
|
+
const language = this.#context.locale.split('-')[0].toLowerCase();
|
|
181
|
+
return TEXTS[language] ? language : 'en';
|
|
182
|
+
}
|
|
183
|
+
#texts() { return TEXTS[this.#language()]; }
|
|
77
184
|
#ensureEngine() {
|
|
78
185
|
if (!this.#engine && this.#context && this.parameters) {
|
|
79
186
|
const {gramlot, window} = this.#context.upload();
|
|
@@ -94,13 +201,18 @@ export const Uploader = defineComponent({
|
|
|
94
201
|
#add(files) {
|
|
95
202
|
if (!this.#engine || this.parameters.disabled) return;
|
|
96
203
|
const selected = [...files];
|
|
97
|
-
|
|
204
|
+
this.message.textContent = '';
|
|
205
|
+
if (!this.parameters.multiple && selected.length > 1) { this.message.textContent = this.#texts().one; return; }
|
|
98
206
|
const accepted = (this.parameters.accept || '').split(',').map(value => value.trim().toLowerCase()).filter(Boolean);
|
|
99
207
|
for (const file of selected) {
|
|
100
208
|
const filename = file.name.toLowerCase(), type = (file.type || '').toLowerCase();
|
|
101
209
|
if (accepted.length && !accepted.some(rule => rule.startsWith('.') ? filename.endsWith(rule) :
|
|
102
210
|
rule.endsWith('/*') ? type.startsWith(rule.slice(0, -1)) : type === rule)) {
|
|
103
|
-
this.message.textContent =
|
|
211
|
+
this.message.textContent = this.#texts().notAccepted(file.name); continue;
|
|
212
|
+
}
|
|
213
|
+
if (this.parameters.maxBytes !== null && file.size > this.parameters.maxBytes) {
|
|
214
|
+
this.message.textContent = this.#texts().tooLarge(file.name, sizeText(this.parameters.maxBytes, this.#language()));
|
|
215
|
+
continue;
|
|
104
216
|
}
|
|
105
217
|
this.#engine.add(file);
|
|
106
218
|
}
|
|
@@ -144,14 +256,15 @@ export const Uploader = defineComponent({
|
|
|
144
256
|
if (this.#previewId !== id || !this.isConnected) return;
|
|
145
257
|
const pre = this.ownerDocument.createElement('pre'); pre.textContent = text;
|
|
146
258
|
pre.style.maxHeight = '200px'; pre.style.overflow = 'auto'; this.preview.append(pre);
|
|
147
|
-
} catch (error) { if (this.#previewId === id) this.preview.textContent =
|
|
259
|
+
} catch (error) { if (this.#previewId === id) this.preview.textContent = this.#texts().previewFailed(error.message); }
|
|
148
260
|
return;
|
|
149
261
|
}
|
|
150
|
-
this.preview.textContent =
|
|
262
|
+
this.preview.textContent = this.#texts().noPreview;
|
|
151
263
|
}
|
|
152
264
|
#render() {
|
|
153
265
|
if (!this.list || !this.#context) return;
|
|
154
|
-
const nodes = this.#storeBag().getNodes();
|
|
266
|
+
const nodes = this.#storeBag().getNodes(), texts = this.#texts(), language = this.#language();
|
|
267
|
+
const doc = this.ownerDocument;
|
|
155
268
|
this.list.replaceChildren();
|
|
156
269
|
const counts = new Map();
|
|
157
270
|
let loaded = 0, size = 0;
|
|
@@ -160,51 +273,59 @@ export const Uploader = defineComponent({
|
|
|
160
273
|
counts.set(state, (counts.get(state) ?? 0) + 1);
|
|
161
274
|
loaded += row.getItem('loaded'); size += row.getItem('size');
|
|
162
275
|
}
|
|
163
|
-
this.summary.textContent =
|
|
164
|
-
[...counts].map(([state, count]) => ` · ${count} ${state}`).join('');
|
|
276
|
+
this.summary.textContent = nodes.length < 2 ? '' : `${texts.files(nodes.length)} — ${sizeText(loaded, language)} / ${sizeText(size, language)}` +
|
|
277
|
+
[...counts].map(([state, count]) => ` · ${count} ${texts.status[state] ?? state}`).join('');
|
|
278
|
+
const action = (caption, handler, disabled = false) => {
|
|
279
|
+
const button = doc.createElement('button'); button.type = 'button'; button.textContent = caption;
|
|
280
|
+
button.disabled = disabled; button.addEventListener('click', handler); return button;
|
|
281
|
+
};
|
|
165
282
|
for (const node of nodes) {
|
|
166
283
|
// The listeners keep the row key only: the row is read at the queue address when they run.
|
|
167
|
-
const row = node.value, label = node.label, item =
|
|
168
|
-
|
|
169
|
-
|
|
284
|
+
const row = node.value, label = node.label, state = row.getItem('status'), item = doc.createElement('li');
|
|
285
|
+
item.dataset.state = state;
|
|
286
|
+
if (label === this.#selectedPath) item.dataset.selected = '';
|
|
287
|
+
const file = doc.createElement('div'); file.className = 'file';
|
|
288
|
+
const select = doc.createElement('button');
|
|
289
|
+
select.type = 'button'; select.className = 'name'; select.textContent = row.getItem('name');
|
|
170
290
|
select.disabled = Boolean(this.parameters.disabled);
|
|
171
291
|
select.setAttribute('aria-pressed', String(label === this.#selectedPath));
|
|
172
|
-
select.setAttribute('aria-label',
|
|
292
|
+
select.setAttribute('aria-label', texts.select(row.getItem('name')));
|
|
173
293
|
select.addEventListener('click', () => {
|
|
174
294
|
this.#select(label);
|
|
175
|
-
const index = this.#storeBag().getNodes().findIndex(
|
|
176
|
-
if (index >= 0) this.list.children[index].
|
|
295
|
+
const index = this.#storeBag().getNodes().findIndex(entry => entry.label === label);
|
|
296
|
+
if (index >= 0) this.list.children[index].querySelector('.name').focus();
|
|
177
297
|
});
|
|
178
|
-
|
|
179
|
-
|
|
298
|
+
const meta = doc.createElement('span'); meta.className = 'meta';
|
|
299
|
+
meta.textContent = `${sizeText(row.getItem('size'), language)} · ${texts.status[state] ?? state}`;
|
|
300
|
+
file.append(select, meta);
|
|
301
|
+
const actions = doc.createElement('div'); actions.className = 'actions';
|
|
180
302
|
if (['preparing','uploading'].includes(state)) {
|
|
181
|
-
|
|
182
|
-
button.addEventListener('click', () => this.#engine.cancel(label)); item.append(button);
|
|
303
|
+
actions.append(action(texts.cancel, () => this.#engine.cancel(label)));
|
|
183
304
|
} else if (['failed','cancelled'].includes(state)) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
if (state === 'uncertain') {
|
|
188
|
-
const button = this.ownerDocument.createElement('button'); button.textContent = 'Check status'; button.type = 'button';
|
|
189
|
-
button.addEventListener('click', () => void this.#engine.reconcile(label)); item.append(button);
|
|
305
|
+
actions.append(action(texts.retry, () => { this.#engine.retry(label); if (this.parameters.autoUpload) void this.#startAll(); },
|
|
306
|
+
Boolean(this.parameters.disabled)));
|
|
190
307
|
}
|
|
308
|
+
if (state === 'uncertain') actions.append(action(texts.check, () => void this.#engine.reconcile(label)));
|
|
191
309
|
if (['queued','completed','failed','cancelled'].includes(state)) {
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
preview.addEventListener('click', () => void this.#showPreview(label, file)); item.append(preview);
|
|
196
|
-
}
|
|
197
|
-
const remove = this.ownerDocument.createElement('button'); remove.textContent = 'Remove'; remove.type = 'button';
|
|
198
|
-
remove.disabled = Boolean(this.parameters.disabled);
|
|
199
|
-
remove.addEventListener('click', () => {
|
|
310
|
+
const local = this.#engine?.localFile(label);
|
|
311
|
+
if (local) actions.append(action(texts.preview, () => void this.#showPreview(label, local)));
|
|
312
|
+
actions.append(action(texts.remove, () => {
|
|
200
313
|
if (this.#engine.remove(label)) {
|
|
201
314
|
if (this.#previewId === label) this.#clearPreview();
|
|
202
315
|
if (this.#selectedPath === label) this.#select(null);
|
|
203
316
|
}
|
|
204
|
-
});
|
|
205
|
-
|
|
317
|
+
}, Boolean(this.parameters.disabled)));
|
|
318
|
+
}
|
|
319
|
+
item.append(file, actions);
|
|
320
|
+
const progress = doc.createElement('progress');
|
|
321
|
+
progress.max = row.getItem('size') || 1;
|
|
322
|
+
progress.value = state === 'completed' ? progress.max : row.getItem('loaded');
|
|
323
|
+
progress.setAttribute('aria-label', row.getItem('name'));
|
|
324
|
+
item.append(progress);
|
|
325
|
+
if (row.getItem('error')) {
|
|
326
|
+
const error = doc.createElement('div'); error.className = 'error'; error.textContent = row.getItem('error');
|
|
327
|
+
item.append(error);
|
|
206
328
|
}
|
|
207
|
-
if (row.getItem('error')) item.append(this.ownerDocument.createTextNode(` ${row.getItem('error')}`));
|
|
208
329
|
this.list.append(item);
|
|
209
330
|
}
|
|
210
331
|
}
|
package/src/gramlot.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {IconRegistry} from './icons.js';
|
|
1
2
|
/* @ts-self-types="./gramlot.d.ts" */
|
|
2
3
|
/**
|
|
3
4
|
* One Gramlot page in the browser.
|
|
@@ -12,6 +13,7 @@ import {FrameMessages} from './frame-messages.js';
|
|
|
12
13
|
import {NAME_SYNTAX, NameSyntax} from './builder/name-syntax.js';
|
|
13
14
|
import {SourceHandler} from './handlers/source-handler.js';
|
|
14
15
|
import {RpcHandler} from './handlers/rpc-handler.js';
|
|
16
|
+
import {PageTheme} from './index-theme.js';
|
|
15
17
|
import {DomHandler} from './handlers/dom-handler.js';
|
|
16
18
|
import {SubscriptionsHandler} from './handlers/subscriptions-handler.js';
|
|
17
19
|
import {DeviceHandler} from './handlers/device-handler.js';
|
|
@@ -36,6 +38,7 @@ export class Gramlot {
|
|
|
36
38
|
this.#collections = [buttons, ...collections.filter(collection => collection instanceof ComponentCollection)];
|
|
37
39
|
this.registerDocument(document);
|
|
38
40
|
this.commands = new CommandRegistry();
|
|
41
|
+
Object.defineProperty(this, '_icons', {value:new IconRegistry()});
|
|
39
42
|
this.pageId = pageId;
|
|
40
43
|
this.uploadUrl = uploadUrl;
|
|
41
44
|
const browserContext = locale == null || timeZone == null
|
|
@@ -50,11 +53,11 @@ export class Gramlot {
|
|
|
50
53
|
subscriptions: this._subscriptions, nameSyntax: nameSyntax === null ? NAME_SYNTAX : NameSyntax.fromJSON(nameSyntax)});
|
|
51
54
|
this.#mobileEnvironment = MobileEnvironment.install({document,root:this.src.renderer.destination});
|
|
52
55
|
this.data = this.src.builder.data;
|
|
56
|
+
this.abort = new AbortController();
|
|
53
57
|
this.dvc = new DeviceHandler(this);
|
|
54
|
-
this.dom = new DomHandler(this);
|
|
58
|
+
this.dom = new DomHandler(this, new PageTheme(this));
|
|
55
59
|
this.utl = new UtilitiesHandler(this, document);
|
|
56
60
|
this.state = 'ready';
|
|
57
|
-
this.abort = new AbortController();
|
|
58
61
|
Object.defineProperty(this, '_frames', {value: new FrameMessages(this, document)});
|
|
59
62
|
if (websocketUrl !== null) this._pageStore = new PageStoreConnection(this, websocketUrl, document);
|
|
60
63
|
this.rpc = new RpcHandler(this, {rpcUrl, closeUrl, document, transport});
|
|
@@ -98,7 +101,7 @@ export class Gramlot {
|
|
|
98
101
|
}
|
|
99
102
|
if (this.state === 'disposed') throw new Error('Gramlot was disposed during main');
|
|
100
103
|
try {
|
|
101
|
-
return this.src.mountMainSource(wire.value, wire.data);
|
|
104
|
+
return this.src.mountMainSource(wire.value, wire.data, wire.sourceFormat);
|
|
102
105
|
} catch (error) {
|
|
103
106
|
if (this.state !== 'disposed') this.state = 'failed';
|
|
104
107
|
throw error;
|
|
@@ -10,6 +10,8 @@ import {Handler} from './handler.js';
|
|
|
10
10
|
|
|
11
11
|
/** The DOM and its correspondence with the Source. */
|
|
12
12
|
export class DomHandler extends Handler {
|
|
13
|
+
setFrameAppearance(appearance: string): void;
|
|
14
|
+
mountIndexTheme(path: string): () => void;
|
|
13
15
|
/** Navigate the current document to an absolute HTTP(S) URL without credentials. */
|
|
14
16
|
navigate(url: string): void;
|
|
15
17
|
setTouchMode(mode?: 'auto' | 'on' | 'off'): void;
|
|
@@ -8,6 +8,15 @@ import {Handler} from './handler.js';
|
|
|
8
8
|
|
|
9
9
|
/** The DOM and its correspondence with the Source. */
|
|
10
10
|
export class DomHandler extends Handler {
|
|
11
|
+
#theme;
|
|
12
|
+
constructor(gramlot, theme) {
|
|
13
|
+
super(gramlot);
|
|
14
|
+
this.#theme = theme;
|
|
15
|
+
}
|
|
16
|
+
setFrameAppearance(appearance) { this.#theme.setFrameAppearance(appearance); }
|
|
17
|
+
/** Connect the common index settings to this document and its owned pages. */
|
|
18
|
+
mountIndexTheme(path) { return this.#theme.mount(path); }
|
|
19
|
+
|
|
11
20
|
/** Size controls for the input device without rebuilding the page. */
|
|
12
21
|
setTouchMode(mode = 'auto') {
|
|
13
22
|
if (!['auto', 'on', 'off'].includes(mode)) throw new TypeError(`Invalid touch mode: ${mode}`);
|
|
@@ -86,7 +86,7 @@ export class RpcHandler extends Handler {
|
|
|
86
86
|
/** Return the checked response envelope, including optional initial-main Data. */
|
|
87
87
|
callEnvelope(contentType: 'source' | 'data' | 'upload', name: string, params?: Record<string, unknown>,
|
|
88
88
|
options?: {signal?: AbortSignal | null}): Promise<{
|
|
89
|
-
id: string; contentType: string; value: unknown; data?: import('@genrojs/bag').Bag;
|
|
89
|
+
id: string; contentType: string; value: unknown; sourceFormat?: 'xml'; data?: import('@genrojs/bag').Bag; icons?: Record<string, string>;
|
|
90
90
|
}>;
|
|
91
91
|
/** Send one envelope synchronously; only with a transport that supports it. */
|
|
92
92
|
callSync(contentType: 'source' | 'data' | 'upload', name: string, params?: Record<string, unknown>): unknown;
|
|
@@ -78,6 +78,7 @@ export class RpcHandler extends Handler {
|
|
|
78
78
|
const response = fromTytx(this.transport.callSync(toTytx({id, pageId: this.gramlot.pageId, contentType, name, params})));
|
|
79
79
|
if (response.id !== id) throw new Error('rpc response id mismatch');
|
|
80
80
|
if (response.error) throw new RpcError(response.error);
|
|
81
|
+
if (contentType === "source" && response.icons) this.gramlot._icons.receive(response.icons);
|
|
81
82
|
return response.value;
|
|
82
83
|
}
|
|
83
84
|
|
|
@@ -109,6 +110,7 @@ export class RpcHandler extends Handler {
|
|
|
109
110
|
const response = fromTytx(await Promise.race([this.transport.call(text, controller.signal), aborted]));
|
|
110
111
|
if (response.id !== id) throw new Error('rpc response id mismatch');
|
|
111
112
|
if (response.error) throw new RpcError(response.error);
|
|
113
|
+
if (contentType === "source" && response.icons) this.gramlot._icons.receive(response.icons);
|
|
112
114
|
return response;
|
|
113
115
|
} finally {
|
|
114
116
|
this.remoteRequests.delete(id);
|
|
@@ -35,16 +35,18 @@ export class SourceHandler extends Handler {
|
|
|
35
35
|
nameSyntax: NameSyntax});
|
|
36
36
|
/** The builder of a page: the prepared grammar of the standard collections in one step when `collections`
|
|
37
37
|
* start with them in its order, then the other collections one by one (GC-325 §165). */
|
|
38
|
+
createPageBuilder(): GramlotBuilder;
|
|
38
39
|
static pageBuilder(collections: unknown[], nameSyntax: NameSyntax | undefined): GramlotBuilder;
|
|
39
40
|
/** Start from an embedded typed Source, with the same observed insertion as `main`. */
|
|
40
41
|
startSource(wire: SourceBag | string): Gramlot;
|
|
41
42
|
/** Validate and insert `main` in the Source. */
|
|
42
|
-
mountMainSource(wire: SourceBag | string, initialData?: Bag | null): Gramlot;
|
|
43
|
+
mountMainSource(wire: SourceBag | string, initialData?: Bag | null, sourceFormat?: 'xml'): Gramlot;
|
|
44
|
+
installInitialData(data: Bag, destination?: Bag): void;
|
|
43
45
|
/**
|
|
44
|
-
* A Source from a SourceBag
|
|
46
|
+
* A typed Source from a SourceBag, a TYTX wire or an explicitly tagged XML recipe, before any
|
|
45
47
|
* effect. The received Source activates its inline code.
|
|
46
48
|
*/
|
|
47
|
-
prepareSource(wire: SourceBag | string, builder?: GramlotBuilder): SourceBag;
|
|
49
|
+
prepareSource(wire: SourceBag | string, builder?: GramlotBuilder, sourceFormat?: 'xml'): SourceBag;
|
|
48
50
|
/**
|
|
49
51
|
* Replace the children of one Source node with a remote Source, only after the incoming Source
|
|
50
52
|
* validates. Resolves with true when mounted, false otherwise: the target left the Source, the page
|