@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
package/src/index-theme.js
CHANGED
|
@@ -1,5 +1,212 @@
|
|
|
1
|
-
|
|
1
|
+
import {appearanceStyles, paletteStyles} from './index-page-styles.js';
|
|
2
|
+
|
|
2
3
|
export const THEME_READY = 'index:theme-ready';
|
|
3
4
|
export const THEME_SET = 'index:theme-set';
|
|
4
|
-
|
|
5
|
-
export const
|
|
5
|
+
const THEME_CHANGE = 'index:theme-change';
|
|
6
|
+
export const THEMES = new Set(['auto', 'light', 'dark']);
|
|
7
|
+
export const APPEARANCES = new Set(Object.keys(appearanceStyles));
|
|
8
|
+
|
|
9
|
+
const defaults = {theme:'auto', appearance:'base', palette:'default'};
|
|
10
|
+
const choices = {theme:THEMES, appearance:APPEARANCES, palette:new Set(Object.keys(paletteStyles))};
|
|
11
|
+
const valid = (key, value) => Object.hasOwn(choices, key) && choices[key].has(value);
|
|
12
|
+
// The server serves each static theme by path; query parameters and fragments do not select another file.
|
|
13
|
+
const stylesheetUrl = link => link.href.split(/[?#]/, 1)[0];
|
|
14
|
+
const complete = values => values && Object.keys(defaults).every(key => valid(key, values[key]));
|
|
15
|
+
|
|
16
|
+
/** Document presentation. An index keeps its selection in its Source-scoped Data.
|
|
17
|
+
* Ordinary framed pages receive the same styles and relay them to their owned descendants.
|
|
18
|
+
*/
|
|
19
|
+
export class PageTheme {
|
|
20
|
+
#app; #document; #window; #origin; #style; #original; #current; #inherited = false;
|
|
21
|
+
#indexes = new Set(); #projecting = false;
|
|
22
|
+
#overlays; #baseUrl; #suspended = new Map(); #baseSheet; #styleObserver;
|
|
23
|
+
|
|
24
|
+
constructor(app) {
|
|
25
|
+
this.#app = app;
|
|
26
|
+
this.#document = app.src.renderer.destination.ownerDocument;
|
|
27
|
+
this.#window = this.#document.defaultView;
|
|
28
|
+
this.#origin = this.#window.location.origin;
|
|
29
|
+
this.#original = this.#document.documentElement.getAttribute('data-theme');
|
|
30
|
+
const directory = this.#document.documentElement.getAttribute('data-gramlot-theme-root')
|
|
31
|
+
?? (this.#window.location.protocol === 'file:' ? 'themes/' : '/themes/');
|
|
32
|
+
if (URL.canParse(directory, this.#document.baseURI)) {
|
|
33
|
+
const root = new URL(directory, this.#document.baseURI);
|
|
34
|
+
this.#baseUrl = new URL('gramlot-base/theme.css', root).href;
|
|
35
|
+
this.#overlays = new Set([
|
|
36
|
+
'gramlot-compact/theme.css', 'mimi/theme.css', 'joanna/theme.css', 'mela/theme.css',
|
|
37
|
+
'palettes/compact.css', 'palettes/mimi.css', 'palettes/joanna.css',
|
|
38
|
+
].map(path => new URL(path, root).href));
|
|
39
|
+
}
|
|
40
|
+
this.#window.addEventListener('message', this.#receive);
|
|
41
|
+
app.abort.signal.addEventListener('abort', () => this.dispose(), {once:true});
|
|
42
|
+
if (this.#framed) this.#post(this.#window.parent, {type:THEME_READY, devices:app.devices});
|
|
43
|
+
else this.#window.addEventListener('storage', this.#storage);
|
|
44
|
+
}
|
|
45
|
+
get #framed() { return this.#window.parent !== this.#window; }
|
|
46
|
+
#post(peer, message) {
|
|
47
|
+
// file: has an opaque origin. Being an owned frame and the matching WindowProxy are still required.
|
|
48
|
+
if (this.#origin === 'null' && this.#window.location.protocol !== 'file:') return;
|
|
49
|
+
peer.postMessage(message, this.#window.location.protocol === 'file:' ? '*' : this.#origin);
|
|
50
|
+
}
|
|
51
|
+
#frames() {
|
|
52
|
+
return [...this.#document.getElementsByTagName('iframe')].filter(frame => {
|
|
53
|
+
if (!frame.isConnected || !frame.contentWindow) return false;
|
|
54
|
+
if (frame.hasAttribute('sandbox') && !frame.getAttribute('sandbox').split(/\s+/).includes('allow-same-origin')) return false;
|
|
55
|
+
const src = frame.getAttribute('src') || this.#document.baseURI;
|
|
56
|
+
if (!URL.canParse(src, this.#document.baseURI)) return false;
|
|
57
|
+
const url = new URL(src, this.#document.baseURI);
|
|
58
|
+
if (!frame.hasAttribute('srcdoc') && url.href !== 'about:blank' && url.origin !== this.#origin) return false;
|
|
59
|
+
const node = this.#app.dom.getBaseSourceNode(frame);
|
|
60
|
+
return node && this.#app.dom.getDomNode(node) === frame;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
#receive = event => {
|
|
64
|
+
if (event.origin !== this.#origin || !event.source || !event.data) return;
|
|
65
|
+
const message = event.data;
|
|
66
|
+
if (this.#framed && event.source === this.#window.parent && message.type === THEME_SET && complete(message)) {
|
|
67
|
+
this.#inherited = true;
|
|
68
|
+
this.#ownProfiles();
|
|
69
|
+
this.#apply(message);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (message.type !== THEME_READY && message.type !== THEME_CHANGE) return;
|
|
73
|
+
if (!this.#frames().some(frame => frame.contentWindow === event.source)) return;
|
|
74
|
+
if (message.type === THEME_READY && this.#current) this.#post(event.source, {type:THEME_SET, ...this.#current});
|
|
75
|
+
if (message.type === THEME_CHANGE && valid(message.key, message.value) && (this.#indexes.size || this.#inherited)) {
|
|
76
|
+
this.#change(message.key, message.value);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
#change(key, value) {
|
|
80
|
+
if (!this.#current || !valid(key, value) || this.#current[key] === value) return;
|
|
81
|
+
this.#ownProfiles();
|
|
82
|
+
this.#apply({...this.#current, [key]:value});
|
|
83
|
+
if (this.#framed) this.#post(this.#window.parent, {type:THEME_CHANGE, key, value});
|
|
84
|
+
else this.#save();
|
|
85
|
+
}
|
|
86
|
+
// Only links to an exact framework profile/palette at the server's declared theme root
|
|
87
|
+
// can compete with the index preference. Page styles and inline styles remain authored.
|
|
88
|
+
#ownProfiles() {
|
|
89
|
+
if (!this.#overlays || this.#styleObserver) return;
|
|
90
|
+
this.#syncLinks();
|
|
91
|
+
this.#styleObserver = new this.#window.MutationObserver(records => {
|
|
92
|
+
if (records.some(record => record.target.localName === 'link' ||
|
|
93
|
+
[...record.addedNodes, ...record.removedNodes].some(node => node.localName === 'link'))) this.#syncLinks();
|
|
94
|
+
});
|
|
95
|
+
this.#styleObserver.observe(this.#document.head,{childList:true,subtree:true,attributes:true,attributeFilter:['href','rel','media']});
|
|
96
|
+
}
|
|
97
|
+
#syncLinks() {
|
|
98
|
+
const links = [...this.#document.head.querySelectorAll('link[rel~=stylesheet][href]')];
|
|
99
|
+
const overlays = links.filter(link => this.#overlays.has(stylesheetUrl(link)));
|
|
100
|
+
for (const link of this.#suspended.keys()) {
|
|
101
|
+
if (!overlays.includes(link)) this.#restoreLink(link);
|
|
102
|
+
}
|
|
103
|
+
for (const link of overlays) {
|
|
104
|
+
if (link.media === 'not all' || link.disabled || link.hasAttribute('disabled')) continue;
|
|
105
|
+
// media also suppresses a stylesheet whose fetch is still in flight.
|
|
106
|
+
this.#suspended.set(link, link.getAttribute('media'));
|
|
107
|
+
link.media = 'not all';
|
|
108
|
+
}
|
|
109
|
+
// A host can serve a named profile as its only theme. Restore Base at the
|
|
110
|
+
// same cascade position, using the host's published Base resource.
|
|
111
|
+
const first = overlays.find(link => this.#suspended.has(link));
|
|
112
|
+
if (first && !links.some(link => link !== this.#baseSheet && stylesheetUrl(link) === this.#baseUrl && !link.disabled)) {
|
|
113
|
+
if (!this.#baseSheet) {
|
|
114
|
+
this.#baseSheet = this.#document.createElement('link');
|
|
115
|
+
this.#baseSheet.rel = 'stylesheet';
|
|
116
|
+
this.#baseSheet.href = this.#baseUrl;
|
|
117
|
+
first.before(this.#baseSheet);
|
|
118
|
+
}
|
|
119
|
+
} else if (this.#baseSheet) {
|
|
120
|
+
this.#baseSheet.remove();
|
|
121
|
+
this.#baseSheet = null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
#restoreLink(link) {
|
|
125
|
+
const media = this.#suspended.get(link);
|
|
126
|
+
this.#suspended.delete(link);
|
|
127
|
+
if (media === null) link.removeAttribute('media');
|
|
128
|
+
else link.setAttribute('media', media);
|
|
129
|
+
}
|
|
130
|
+
#apply({theme, appearance, palette}) {
|
|
131
|
+
this.#current = {theme, appearance, palette};
|
|
132
|
+
const html = this.#document.documentElement;
|
|
133
|
+
if (theme === 'auto') html.removeAttribute('data-theme');
|
|
134
|
+
else html.setAttribute('data-theme', theme);
|
|
135
|
+
const css = appearanceStyles[appearance] + '\n' + paletteStyles[palette];
|
|
136
|
+
if (css.trim() && !this.#style) {
|
|
137
|
+
this.#style = this.#document.createElement('style');
|
|
138
|
+
this.#document.head.append(this.#style);
|
|
139
|
+
}
|
|
140
|
+
if (this.#style) this.#style.textContent = css;
|
|
141
|
+
this.#projecting = true;
|
|
142
|
+
try {
|
|
143
|
+
for (const {path} of this.#indexes) for (const [key,value] of Object.entries(this.#current)) {
|
|
144
|
+
if (this.#app.data.getItem(`${path}.${key}`) !== value) this.#app.data.setItem(`${path}.${key}`, value);
|
|
145
|
+
}
|
|
146
|
+
} finally { this.#projecting = false; }
|
|
147
|
+
for (const frame of this.#frames()) this.#post(frame.contentWindow, {type:THEME_SET, ...this.#current});
|
|
148
|
+
}
|
|
149
|
+
#storage = event => {
|
|
150
|
+
if (!event.key || ![...this.#indexes].some(index => index.key === event.key)) return;
|
|
151
|
+
const value = this.#read(event.key);
|
|
152
|
+
if (value) { this.#ownProfiles(); this.#apply(value); }
|
|
153
|
+
};
|
|
154
|
+
#read(key) {
|
|
155
|
+
try {
|
|
156
|
+
const value = JSON.parse(this.#window.localStorage.getItem(key));
|
|
157
|
+
return complete(value) ? value : null;
|
|
158
|
+
} catch (error) { if (error.name !== 'SecurityError' && !(error instanceof SyntaxError)) throw error; return null; }
|
|
159
|
+
}
|
|
160
|
+
#save() {
|
|
161
|
+
try {
|
|
162
|
+
const value = JSON.stringify(this.#current);
|
|
163
|
+
for (const key of new Set([...this.#indexes].map(index => index.key))) {
|
|
164
|
+
this.#window.localStorage.setItem(key, value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (error) { if (!['SecurityError','QuotaExceededError'].includes(error.name)) throw error; }
|
|
168
|
+
}
|
|
169
|
+
/** An explicit local appearance does not override a choice already received from the index. */
|
|
170
|
+
setFrameAppearance(appearance) {
|
|
171
|
+
if (!APPEARANCES.has(appearance)) throw new TypeError('Unknown frame appearance');
|
|
172
|
+
if (this.#inherited || this.#indexes.size) return;
|
|
173
|
+
this.#apply({theme:THEMES.has(this.#original) ? this.#original : 'auto', palette:'default', appearance});
|
|
174
|
+
}
|
|
175
|
+
mount(path) {
|
|
176
|
+
const group = this.#app.data.getItem(`${path}.themeGroup`);
|
|
177
|
+
const key = 'gramlot.index.theme:' + (group == null ? this.#window.location.pathname : `group:${group}`);
|
|
178
|
+
const entry = {path, key};
|
|
179
|
+
this.#indexes.add(entry);
|
|
180
|
+
const initial = {};
|
|
181
|
+
for (const key of Object.keys(defaults)) {
|
|
182
|
+
const value = this.#app.data.getItem(`${path}.${key}`);
|
|
183
|
+
initial[key] = valid(key, value) ? value : defaults[key];
|
|
184
|
+
if (key === 'theme' && value == null && THEMES.has(this.#original)) initial.theme = this.#original;
|
|
185
|
+
}
|
|
186
|
+
const saved = this.#framed ? null : this.#read(key);
|
|
187
|
+
if (saved) this.#ownProfiles();
|
|
188
|
+
this.#apply(saved ?? this.#current ?? initial);
|
|
189
|
+
const registrations = Object.keys(defaults).map(key => this.#app.src.binding.router.register({
|
|
190
|
+
path:`${path}.${key}`, recipient:{receive:() => {
|
|
191
|
+
if (!this.#projecting) this.#change(key, this.#app.data.getItem(`${path}.${key}`));
|
|
192
|
+
}},
|
|
193
|
+
}));
|
|
194
|
+
return () => {
|
|
195
|
+
for (const registration of registrations) registration.close();
|
|
196
|
+
this.#indexes.delete(entry);
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
dispose() {
|
|
200
|
+
this.#window.removeEventListener('message', this.#receive);
|
|
201
|
+
this.#window.removeEventListener('storage', this.#storage);
|
|
202
|
+
this.#styleObserver?.disconnect();
|
|
203
|
+
this.#baseSheet?.remove();
|
|
204
|
+
this.#style?.remove();
|
|
205
|
+
for (const link of this.#suspended.keys()) this.#restoreLink(link);
|
|
206
|
+
if (this.#current) {
|
|
207
|
+
if (this.#original === null) this.#document.documentElement.removeAttribute('data-theme');
|
|
208
|
+
else this.#document.documentElement.setAttribute('data-theme', this.#original);
|
|
209
|
+
}
|
|
210
|
+
this.#indexes.clear();
|
|
211
|
+
}
|
|
212
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -61,7 +61,7 @@ export {chatRender, chatSend, chatCompleted, chatReset, chatResize, chatPickAtta
|
|
|
61
61
|
|
|
62
62
|
// Preserve fluent Source identity across separately prepared collection bundles.
|
|
63
63
|
export {sourceTarget} from '@genrojs/builders';
|
|
64
|
-
export {TreeStore, TreeStoreResolver, TreeStoreLogic} from './tree-store.js';
|
|
64
|
+
export {treeStore, TreeStore, TreeStoreResolver, TreeStoreLogic} from './tree-store.js';
|
|
65
65
|
export {UploadTarget} from './server/upload.js';
|
|
66
66
|
|
|
67
67
|
export {VisualEditorLogic, visualEditorScope} from './visual-editor.js';
|
|
@@ -89,3 +89,7 @@ export interface LibraryEntry {
|
|
|
89
89
|
export declare const libraries: Readonly<Record<string, LibraryEntry>>;
|
|
90
90
|
/** The library `name` for a component of `document`: from jsDelivr when the document says `cdn`, else the local copy. */
|
|
91
91
|
export declare function loadLibrary(name: string, document: Document, localImport: () => Promise<unknown>): Promise<unknown>;
|
|
92
|
+
|
|
93
|
+
export {PageStore} from './server/page-store.js';
|
|
94
|
+
|
|
95
|
+
export {ChatPane, ChatPageStore} from './server/chat.js';
|
package/src/index.js
CHANGED
|
@@ -54,7 +54,7 @@ export {chatRender, chatSend, chatCompleted, chatReset, chatResize, chatPickAtta
|
|
|
54
54
|
|
|
55
55
|
// Preserve fluent Source identity across separately prepared collection bundles.
|
|
56
56
|
export {sourceTarget} from '@genrojs/builders';
|
|
57
|
-
export {TreeStore, TreeStoreResolver, TreeStoreLogic} from './tree-store.js';
|
|
57
|
+
export {treeStore, TreeStore, TreeStoreResolver, TreeStoreLogic} from './tree-store.js';
|
|
58
58
|
export {UploadTarget} from './server/upload.js';
|
|
59
59
|
|
|
60
60
|
export {VisualEditorLogic, visualEditorScope} from './visual-editor.js';
|
|
@@ -70,3 +70,7 @@ export {bundledCollections} from './bundled-collections.js';
|
|
|
70
70
|
export {libraries, loadLibrary} from './libraries.js';
|
|
71
71
|
// The random id of the runtime, for collections prepared outside this package.
|
|
72
72
|
export {randomId} from './random-id.js';
|
|
73
|
+
|
|
74
|
+
export {PageStore} from './server/page-store.js';
|
|
75
|
+
|
|
76
|
+
export {ChatPane, ChatPageStore} from './server/chat.js';
|
package/src/libraries.js
CHANGED
|
@@ -54,7 +54,7 @@ export const libraries = Object.freeze({
|
|
|
54
54
|
codemirror: Object.freeze({packages: {
|
|
55
55
|
'@codemirror/autocomplete': '6.20.4', '@codemirror/commands': '6.11.1', '@codemirror/lang-css': '6.3.1',
|
|
56
56
|
'@codemirror/lang-html': '6.4.12', '@codemirror/lang-javascript': '6.2.5', '@codemirror/lang-json': '6.0.2',
|
|
57
|
-
'@codemirror/lang-markdown': '6.5.2', '@codemirror/lang-python': '6.2.1', '@codemirror/lang-sql': '6.10.0',
|
|
57
|
+
'@codemirror/lang-markdown': '6.5.2', '@codemirror/lang-python': '6.2.1', '@codemirror/lang-rust': '6.0.2', '@codemirror/lang-sql': '6.10.0',
|
|
58
58
|
'@codemirror/lang-xml': '6.1.0', '@codemirror/language': '6.13.1', '@codemirror/lint': '6.9.8',
|
|
59
59
|
'@codemirror/search': '6.7.2', '@codemirror/state': '6.7.6', '@codemirror/view': '6.43.15',
|
|
60
60
|
'@lezer/highlight': '1.2.5', 'codemirror-lang-typst': '0.6.0'},
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {trackChoiceFocus} from '../view/choice-focus.js';
|
|
1
2
|
import {callLogic, invokeHook} from '../binding/hooks.js';
|
|
2
3
|
/* @ts-self-types="./gramlot-renderer.d.ts" */
|
|
3
4
|
/**
|
|
@@ -51,7 +52,7 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
51
52
|
this._binding = binding;
|
|
52
53
|
this.source = source;
|
|
53
54
|
this.destination = destination;
|
|
54
|
-
this.html = new HtmlElement({uploadContext: () => ({gramlot: binding.gramlot, window: destination.ownerDocument.defaultView}), metadataAttributes: ['__ref', ...SYMBOLIC_ATTRIBUTES],
|
|
55
|
+
this.html = new HtmlElement({icons: binding.gramlot._icons, uploadContext: () => ({gramlot: binding.gramlot, window: destination.ownerDocument.defaultView}), metadataAttributes: ['__ref', ...SYMBOLIC_ATTRIBUTES],
|
|
55
56
|
format: (value, options) => binding.gramlot.format(value, options),
|
|
56
57
|
reportError: (node, error, title) => { if (this.records.has(node)) binding.reportError(error, title); },
|
|
57
58
|
callLogic: (node, name, keywords) => {
|
|
@@ -232,6 +233,8 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
232
233
|
this.records.set(node, record);
|
|
233
234
|
this.elements.set(record.element, record);
|
|
234
235
|
this.elements.set(record.presentation, record);
|
|
236
|
+
if (record.element.namespaceURI === XHTML_NS && record.element.localName === 'select')
|
|
237
|
+
record.cleanup.push(trackChoiceFocus(record.element));
|
|
235
238
|
const control = ControlAdapter.for(record.binding, record);
|
|
236
239
|
// An input can acquire/change its adapter through type while its DOM record survives.
|
|
237
240
|
if (control || (record.element.namespaceURI === XHTML_NS && record.element.localName === 'input'))
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {Bag} from '@genrojs/bag';
|
|
2
|
+
import {fileTypes} from '../chat-file-types.js';
|
|
3
|
+
import {Exposed} from './page-base.js';
|
|
4
|
+
import {UploadTarget} from './upload.js';
|
|
5
|
+
|
|
6
|
+
// Files are owned by the page. Browser imports of the authoring API never open a filesystem.
|
|
7
|
+
const FS = 'node:fs';
|
|
8
|
+
const OS = 'node:os';
|
|
9
|
+
const PATH = 'node:path';
|
|
10
|
+
const BUFFER = 'node:buffer';
|
|
11
|
+
|
|
12
|
+
export class ChatAttachments extends Exposed {
|
|
13
|
+
#files = new Map();
|
|
14
|
+
#directory = null;
|
|
15
|
+
#fs = null;
|
|
16
|
+
#closed = false;
|
|
17
|
+
url = '';
|
|
18
|
+
|
|
19
|
+
async receive({name, mediaType, size}) {
|
|
20
|
+
if (typeof name !== 'string' || !name.includes('.') || /[/\\]/.test(name) || name.length > 255 ||
|
|
21
|
+
fileTypes[name.split('.').at(-1).toLowerCase()] !== mediaType || !Number.isSafeInteger(size) || size <= 0) {
|
|
22
|
+
throw new TypeError('Invalid chat attachment');
|
|
23
|
+
}
|
|
24
|
+
const [fs, os, path] = await Promise.all([import(FS), import(OS), import(PATH)]);
|
|
25
|
+
if (this.#closed) throw new Error('Closed chat');
|
|
26
|
+
this.#fs = fs;
|
|
27
|
+
this.#directory ??= fs.mkdtempSync(path.join(os.tmpdir(), 'gramlot-chat-'));
|
|
28
|
+
const ref = crypto.randomUUID().replaceAll('-', '');
|
|
29
|
+
const filename = path.join(this.#directory, ref);
|
|
30
|
+
const record = {name, mediaType, size, ref};
|
|
31
|
+
if (mediaType.startsWith('image/')) record.url = this.url + ref;
|
|
32
|
+
return new ChatFile(fs, filename, record, () => {
|
|
33
|
+
if (this.#closed) throw new Error('Closed chat');
|
|
34
|
+
this.#files.set(ref, {record, filename});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
references(refs) {
|
|
39
|
+
const records = new Bag();
|
|
40
|
+
for (const node of refs.getNodes()) {
|
|
41
|
+
const {record} = this.#files.get(node.value.getItem('ref'));
|
|
42
|
+
records.setItem(node.label, new Bag(record));
|
|
43
|
+
}
|
|
44
|
+
return records;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async contents(refs) {
|
|
48
|
+
const result = new Bag();
|
|
49
|
+
for (const node of refs.getNodes()) {
|
|
50
|
+
const {record, filename} = this.#files.get(node.value.getItem('ref'));
|
|
51
|
+
const data = await this.#fs.promises.readFile(filename, 'base64');
|
|
52
|
+
result.setItem(node.label, new Bag({...record, data}));
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async imageUrl(png) {
|
|
58
|
+
const {Buffer} = await import(BUFFER);
|
|
59
|
+
const bytes = Buffer.from(png, 'base64');
|
|
60
|
+
const file = await this.receive({name:'image.png', mediaType:'image/png', size:bytes.length});
|
|
61
|
+
try {
|
|
62
|
+
file.write(bytes);
|
|
63
|
+
return file.finish().result.url;
|
|
64
|
+
} catch (error) { file.abort(); throw error; }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async openImage(ref) {
|
|
68
|
+
const file = this.#files.get(ref);
|
|
69
|
+
if (!file || !file.record.mediaType.startsWith('image/')) return null;
|
|
70
|
+
return {type:file.record.mediaType, bytes:await this.#fs.promises.readFile(file.filename)};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
close() {
|
|
74
|
+
this.#closed = true;
|
|
75
|
+
this.#files.clear();
|
|
76
|
+
if (this.#directory) this.#fs.rmSync(this.#directory, {recursive:true, force:true});
|
|
77
|
+
this.#directory = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
ChatAttachments.registerUpload('receive');
|
|
81
|
+
|
|
82
|
+
class ChatFile extends UploadTarget {
|
|
83
|
+
#fs;
|
|
84
|
+
#filename;
|
|
85
|
+
#record;
|
|
86
|
+
#commit;
|
|
87
|
+
#fd;
|
|
88
|
+
#size = 0;
|
|
89
|
+
#decoder;
|
|
90
|
+
#header = [];
|
|
91
|
+
#outcome = {state:'pending'};
|
|
92
|
+
|
|
93
|
+
constructor(fs, filename, record, commit) {
|
|
94
|
+
super({maxBytes:record.size});
|
|
95
|
+
this.#fs = fs;
|
|
96
|
+
this.#filename = filename;
|
|
97
|
+
this.#record = record;
|
|
98
|
+
this.#commit = commit;
|
|
99
|
+
this.#fd = fs.openSync(filename, 'wx', 0o600);
|
|
100
|
+
if (record.mediaType === 'text/plain') this.#decoder = new TextDecoder('utf-8', {fatal:true});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
write(bytes) {
|
|
104
|
+
if (this.#size + bytes.length > this.maxBytes) throw new RangeError('Upload exceeds declared size');
|
|
105
|
+
this.#decoder?.decode(bytes, {stream:true});
|
|
106
|
+
if (this.#decoder && bytes.includes(0)) throw new TypeError('Invalid text attachment');
|
|
107
|
+
this.#header.push(...bytes.subarray(0, 12 - this.#header.length));
|
|
108
|
+
let written = 0;
|
|
109
|
+
while (written < bytes.length) written += this.#fs.writeSync(this.#fd, bytes, written, bytes.length - written);
|
|
110
|
+
this.#size += written;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
finish() {
|
|
114
|
+
if (this.#size !== this.maxBytes) throw new RangeError('Incomplete attachment');
|
|
115
|
+
const header = String.fromCharCode(...this.#header);
|
|
116
|
+
const signatures = {'image/png':'\x89PNG\r\n\x1a\n', 'image/jpeg':'\xff\xd8\xff', 'image/webp':'RIFF', 'application/pdf':'%PDF-'};
|
|
117
|
+
if (this.#decoder) this.#decoder.decode();
|
|
118
|
+
else if (!header.startsWith(signatures[this.#record.mediaType]) ||
|
|
119
|
+
(this.#record.mediaType === 'image/webp' && header.slice(8, 12) !== 'WEBP')) {
|
|
120
|
+
throw new TypeError('Invalid attachment content');
|
|
121
|
+
}
|
|
122
|
+
this.#fs.closeSync(this.#fd);
|
|
123
|
+
this.#fd = null;
|
|
124
|
+
this.#commit();
|
|
125
|
+
this.#outcome = {state:'completed', result:this.#record};
|
|
126
|
+
return this.#outcome;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
abort() {
|
|
130
|
+
if (this.#outcome.state === 'pending') {
|
|
131
|
+
if (this.#fd !== null) this.#fs.closeSync(this.#fd);
|
|
132
|
+
this.#fs.rmSync(this.#filename, {force:true});
|
|
133
|
+
this.#outcome = {state:'unsaved'};
|
|
134
|
+
}
|
|
135
|
+
return this.#outcome;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
status() { return this.#outcome; }
|
|
139
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type {Bag} from '@genrojs/bag';
|
|
2
|
+
import {PageStore} from './page-store.js';
|
|
3
|
+
import type {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
4
|
+
|
|
5
|
+
type Pane = GramlotBuilder['root'];
|
|
6
|
+
export class ChatPane {
|
|
7
|
+
constructor(parent: Pane, options: {datapath: string; chat_id?: string; [attribute: string]: unknown});
|
|
8
|
+
frame: Pane;
|
|
9
|
+
composer: Pane;
|
|
10
|
+
messages: Pane;
|
|
11
|
+
editor: Pane;
|
|
12
|
+
toolbar: Pane;
|
|
13
|
+
sendButton: Pane;
|
|
14
|
+
attachmentButton: Pane;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ChatRequestContext {
|
|
18
|
+
chatId: string;
|
|
19
|
+
attachments: Bag;
|
|
20
|
+
feedback: (text: string) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class ChatPageStore extends PageStore {
|
|
24
|
+
static chatIds: string[];
|
|
25
|
+
processMessage(text: string, context: ChatRequestContext): string | GramlotBuilder['source'] | Promise<string | GramlotBuilder['source']>;
|
|
26
|
+
resetConversation(chatId: string): void;
|
|
27
|
+
conversationMessages(chatId: string): Bag;
|
|
28
|
+
conversationAuthors(): [string, string];
|
|
29
|
+
attachmentContents(refs: Bag): Promise<Bag>;
|
|
30
|
+
imageUrl(base64Png: string): Promise<string>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {Bag} from '@genrojs/bag';
|
|
2
|
+
import {GramlotBuilderBag} from '../builder/source.js';
|
|
3
|
+
import {PageStore} from './page-store.js';
|
|
4
|
+
import {ChatAttachments} from './chat-attachments.js';
|
|
5
|
+
|
|
6
|
+
const identifier = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
7
|
+
const conversation = ready => new Bag({messages: new Bag(), ready, request: null, response: null, feedback: ''});
|
|
8
|
+
|
|
9
|
+
export class ChatPane {
|
|
10
|
+
constructor(parent, {datapath, chat_id = 'main', ...attributes}) {
|
|
11
|
+
if (typeof chat_id !== 'string' || !identifier.test(chat_id)) throw new TypeError('Invalid chat_id');
|
|
12
|
+
const frame = this.frame = parent.framePane({datapath, ...attributes});
|
|
13
|
+
const defaults = {
|
|
14
|
+
conversation: conversation(false), messageData: new Bag(), draft: '', pending: false,
|
|
15
|
+
requestId: '', error: '', attachments: new Bag(), picking: false, composerHeight: 96,
|
|
16
|
+
};
|
|
17
|
+
for (const [name, value] of Object.entries(defaults)) {
|
|
18
|
+
frame.dataSetter({destination_path: '.' + name, value});
|
|
19
|
+
}
|
|
20
|
+
frame.dataBinder({destination_path: '.conversation', server_path: 'chats.' + chat_id});
|
|
21
|
+
frame.dataController({func:'chatCompleted', response:'^.conversation.response'});
|
|
22
|
+
frame.top.setAttr({height:'44px'});
|
|
23
|
+
this.messages = frame.center.chatMessages({height:'100%', min_height:0});
|
|
24
|
+
this.messages.dataController({func:'chatRender', messages:'^.conversation.messages', _onBuilt:true});
|
|
25
|
+
frame.bottom.setAttr({height:'^.composerHeight', splitter:false});
|
|
26
|
+
this.composer = frame.bottom.framePane({height:'100%', 'data-chat-composer':true});
|
|
27
|
+
this.composer.top.setAttr({height:'2px', 'data-chat-top':true});
|
|
28
|
+
this.composer.bottom.setAttr({height:'44px'});
|
|
29
|
+
const pane = this.composer.center.contentPane({overflow:'hidden'});
|
|
30
|
+
const center = pane.div({display:'flex', flex_direction:'column', padding:'4px 10px', gap:'4px'});
|
|
31
|
+
center.dataFormula({result_path:'.status', formula:'pending ? (feedback || "Sending to the server…") : ""',
|
|
32
|
+
pending:'^.pending', feedback:'^.conversation.feedback', _init:true});
|
|
33
|
+
center.div('^.status', {node_label:'chatFeedback', class:'gramlot-chat-status', role:'status',
|
|
34
|
+
aria_live:'polite', display:'^.status:={} ? "flex" : "none"', 'data-pending':'^.pending',
|
|
35
|
+
height:'22px', min_height:'22px', overflow:'hidden', white_space:'nowrap',
|
|
36
|
+
text_overflow:'ellipsis', font_size:'13px'});
|
|
37
|
+
this.editor = center.textarea({value:'^.draft', live:true, aria_label:'Message', placeholder:'Write a message…',
|
|
38
|
+
class:'gramlot-chat-editor', rows:1, min_height:'44px', height:'44px', line_height:'20px',
|
|
39
|
+
resize:'none', width:'100%', box_sizing:'border-box', border_radius:'18px', padding:'10px 14px'});
|
|
40
|
+
const previews = center.div({display:'flex', gap:'6px', overflow_x:'auto', max_height:'76px'});
|
|
41
|
+
previews.dataController({func:'chatRenderAttachments', attachments:'^.attachments', _onBuilt:true});
|
|
42
|
+
center.dataController({func:'chatResize', draft:'^.draft', status:'^.status', error:'^.error',
|
|
43
|
+
attachments:'^.attachments', _onBuilt:true});
|
|
44
|
+
center.span('^.error', {role:'alert', hidden:'^.error:=!{}', font_size:'12px'});
|
|
45
|
+
const actions = this.composer.bottom.div({class:'gramlot-chat-toolbar', role:'toolbar',
|
|
46
|
+
'data-chat-toolbar':true, aria_label:'Chat controls', display:'flex', align_items:'center',
|
|
47
|
+
gap:'8px', height:'44px', padding:'0 14px', box_sizing:'border-box'});
|
|
48
|
+
this.attachmentButton = actions.button({icon:'new', aria_label:'Add files or images', title:'Add files or images',
|
|
49
|
+
class:'button--quiet', font_size:'24px', width:'36px', height:'36px',
|
|
50
|
+
style:'--gramlot-touch-target:36px;--gramlot-button-padding:0;--gramlot-field-height:36px;',
|
|
51
|
+
picking:'^.picking', disabled:'^.pending:={} || {picking}'});
|
|
52
|
+
this.attachmentButton.dataController({func:'chatPickAttachments'});
|
|
53
|
+
this.toolbar = actions.div({display:'flex', align_items:'center', gap:'10px', flex:1, min_width:0, overflow_x:'auto'});
|
|
54
|
+
this.sendButton = actions.button({aria_label:'Send', title:'Send message', class:'gramlot-chat-send',
|
|
55
|
+
font_size:'16px', flex_shrink:0, width:'36px', height:'36px', chat_id,
|
|
56
|
+
style:'--gramlot-radius-sm:50%;--gramlot-button-padding:9px;--gramlot-button-gap:0px;--gramlot-field-height:36px;--gramlot-touch-target:36px;',
|
|
57
|
+
ready:'^.conversation.ready', draft:'^.draft', attachments:'^.attachments', picking:'^.picking',
|
|
58
|
+
disabled:'^.pending:={} || {picking} || !{ready} || (!{draft}.trim() && !{attachments}.length)'});
|
|
59
|
+
this.sendButton.span({class:'gramlot-chat-send-icon', aria_hidden:'true'});
|
|
60
|
+
this.sendButton.dataController({func:'chatSend', chat_id});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class ChatPageStore extends PageStore {
|
|
65
|
+
static chatIds = ['main'];
|
|
66
|
+
#queues = new Map();
|
|
67
|
+
attachments = new ChatAttachments();
|
|
68
|
+
|
|
69
|
+
main(root) {
|
|
70
|
+
for (const chatId of this.constructor.chatIds) {
|
|
71
|
+
if (typeof chatId !== 'string' || !identifier.test(chatId)) throw new TypeError('Invalid chatId');
|
|
72
|
+
this.data.setItem('chats.' + chatId, conversation(true));
|
|
73
|
+
root.dataController({func:'chatRequest', chatId, request:'^chats.' + chatId + '.request'});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
close() {
|
|
78
|
+
super.close();
|
|
79
|
+
this.attachments.close();
|
|
80
|
+
this.#queues.clear();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
attachmentContents(refs) { return this.attachments.contents(refs); }
|
|
84
|
+
imageUrl(png) { return this.attachments.imageUrl(png); }
|
|
85
|
+
conversationMessages(chatId) { return this.data.getItem('chats.' + chatId + '.messages'); }
|
|
86
|
+
resetConversation(chatId) { this.data.setItem('chats.' + chatId + '.messages', new Bag()); }
|
|
87
|
+
conversationAuthors() { return ['You', 'Assistant']; }
|
|
88
|
+
processMessage(text, context) { return text; }
|
|
89
|
+
|
|
90
|
+
chatRequest(node, {chatId, request}) {
|
|
91
|
+
if (!(request instanceof Bag)) return;
|
|
92
|
+
const previous = this.#queues.get(chatId) ?? Promise.resolve();
|
|
93
|
+
const next = previous.then(() => this.#handle(chatId, request));
|
|
94
|
+
this.#queues.set(chatId, next);
|
|
95
|
+
next.finally(() => { if (this.#queues.get(chatId) === next) this.#queues.delete(chatId); });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async #handle(chatId, request) {
|
|
99
|
+
if (this.closed) return;
|
|
100
|
+
const base = 'chats.' + chatId;
|
|
101
|
+
const id = request.getItem('id');
|
|
102
|
+
const text = request.getItem('text');
|
|
103
|
+
if (typeof id !== 'string' || !id || this.data.getItem(base + '.response.id') === id) return;
|
|
104
|
+
let accepted = false;
|
|
105
|
+
let error = '';
|
|
106
|
+
try {
|
|
107
|
+
const action = request.getItem('action');
|
|
108
|
+
if (action === 'reset') {
|
|
109
|
+
await this.resetConversation(chatId);
|
|
110
|
+
accepted = true;
|
|
111
|
+
} else if (action !== 'send') {
|
|
112
|
+
error = 'Unknown chat action.';
|
|
113
|
+
} else {
|
|
114
|
+
const refs = request.getItem('attachments');
|
|
115
|
+
if (!(refs instanceof Bag) || refs.length > 5) throw new TypeError('Invalid attachments');
|
|
116
|
+
if (typeof text !== 'string' || (!text.trim() && !refs.length)) {
|
|
117
|
+
error = 'Enter a message.';
|
|
118
|
+
} else {
|
|
119
|
+
const attachments = this.attachments.references(refs);
|
|
120
|
+
const feedback = status => {
|
|
121
|
+
if (typeof status !== 'string') throw new TypeError('Feedback must be text');
|
|
122
|
+
if (!this.closed) this.data.setItem(base + '.feedback', status);
|
|
123
|
+
};
|
|
124
|
+
const result = await this.processMessage(text, {chatId, attachments, feedback});
|
|
125
|
+
if (this.closed) return;
|
|
126
|
+
if (typeof result !== 'string' && !(result instanceof GramlotBuilderBag)) throw new TypeError('Invalid reply');
|
|
127
|
+
const messages = this.conversationMessages(chatId);
|
|
128
|
+
const [user, assistant] = this.conversationAuthors();
|
|
129
|
+
const label = () => 'm_' + crypto.randomUUID().replaceAll('-', '');
|
|
130
|
+
messages.setItem(label(), this.#attachmentMessage(text, attachments), {role:'user', author:user});
|
|
131
|
+
messages.setItem(label(), result, {role:'assistant', author:assistant});
|
|
132
|
+
accepted = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
error = 'The server could not process the message. Your draft has been kept.';
|
|
137
|
+
}
|
|
138
|
+
if (this.closed) return;
|
|
139
|
+
this.data.setItem(base + '.feedback', '');
|
|
140
|
+
this.data.setItem(base + '.response', new Bag({id, submitted:text, accepted, error}));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
#attachmentMessage(text, attachments) {
|
|
144
|
+
if (!attachments.length) return text;
|
|
145
|
+
const builder = this.sourceBuilder();
|
|
146
|
+
builder.root.div('==' + JSON.stringify(text), {white_space:'pre-wrap'});
|
|
147
|
+
for (const node of attachments.getNodes()) {
|
|
148
|
+
const file = node.value;
|
|
149
|
+
const name = '==' + JSON.stringify(file.getItem('name'));
|
|
150
|
+
builder.root.div(name, {font_size:'13px'});
|
|
151
|
+
if (file.getItem('mediaType').startsWith('image/')) {
|
|
152
|
+
builder.root.img({src:file.getItem('url'), alt:name, max_width:'240px', max_height:'160px'});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return builder.source;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {Page} from './page-base.js';
|
|
2
|
+
|
|
3
|
+
/** Serve native dbSelect and dbComboBox from the selector service configured by the host. */
|
|
4
|
+
export class DatabaseSelectors extends Page {
|
|
5
|
+
dbSelect(parameters) {
|
|
6
|
+
if (Object.hasOwn(parameters, 'where')) {
|
|
7
|
+
throw new Error('SQL predicates belong in a dedicated server endpoint');
|
|
8
|
+
}
|
|
9
|
+
return this.database.select(parameters);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
DatabaseSelectors.registerEndpoint('dbSelect');
|
|
@@ -12,7 +12,7 @@ import {basename, dirname, extname, join, relative, resolve, sep} from 'node:pat
|
|
|
12
12
|
import {pathToFileURL} from 'node:url';
|
|
13
13
|
import {gramlotDev} from './assets.js';
|
|
14
14
|
import {fileBelow, inside, realFile} from './files.js';
|
|
15
|
-
import {Page} from './page.js';
|
|
15
|
+
import {Page, PageStore} from './page.js';
|
|
16
16
|
import {GramlotServer, PageNotFound} from './gramlot-server.js';
|
|
17
17
|
import {InvalidResourceName, SEGMENT, parseRequires} from './resources.js';
|
|
18
18
|
|
|
@@ -84,6 +84,11 @@ export class GramlotFileServer extends GramlotServer {
|
|
|
84
84
|
return (await import(await this._moduleUrl(await this.locatePage(path)))).Page;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
async resolvePageStore(PageClass, path) {
|
|
88
|
+
const module = await import(await this._moduleUrl(await this.locatePage(path)));
|
|
89
|
+
return Object.hasOwn(module, 'PageStore') ? module.PageStore : PageStore;
|
|
90
|
+
}
|
|
91
|
+
|
|
87
92
|
/** Import route-eligible page modules at startup, without calling their methods. */
|
|
88
93
|
async *iterPageClasses() {
|
|
89
94
|
const seenFiles = new Set(), seenClasses = new Set();
|