@gramlot/gramlot 0.2.15 → 0.2.17
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/branding/app-icon.png +0 -0
- package/dist/gramlot.js +1223 -219
- package/dist/gramlot.min.js +77 -20
- package/package.json +8 -5
- package/src/binding/installation.js +15 -5
- package/src/binding/providers.js +63 -6
- package/src/binding/remote.js +77 -0
- package/src/binding/rpc-resolver.js +93 -0
- package/src/binding/runtime.js +34 -9
- package/src/builder/binding.json +76 -0
- package/src/builder/gramlot-builder.js +16 -4
- package/src/builder/source.js +35 -3
- package/src/builder/webcomponent.json +51 -0
- package/src/handlers/source-handler.d.ts +1 -1
- package/src/handlers/source-handler.js +1 -1
- package/src/index-page-runtime.d.ts +11 -0
- package/src/index-page-runtime.js +287 -0
- package/src/index-page-styles.js +2 -0
- package/src/index-theme.js +5 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +3 -0
- package/src/inout.js +3 -1
- package/src/renderer/attributes.js +13 -1
- package/src/renderer/gramlot-html-renderer.js +9 -2
- package/src/renderer/gramlot-renderer.js +2 -1
- package/src/renderer/gramlot-svg-renderer.js +2 -1
- package/src/server/conformance.js +22 -2
- package/src/server/gramlot-server.d.ts +1 -0
- package/src/server/gramlot-server.js +12 -0
- package/src/server/index-page.d.ts +14 -0
- package/src/server/index-page.js +102 -0
- package/src/server/page-base.js +88 -0
- package/src/server/page.d.ts +8 -2
- package/src/server/page.js +3 -76
- package/src/view/html.js +2 -1
- package/src/webcomponent.js +13 -0
- package/themes/gramlot-base/README.md +8 -0
- package/themes/gramlot-compact/README.md +49 -0
- package/themes/gramlot-compact/theme.css +57 -0
- package/themes/gramlot-index/theme.css +51 -0
- package/tools/gramlot-tools-inspector.js +1460 -0
|
@@ -13,17 +13,20 @@ import {GramlotBuilderBag} from './source.js';
|
|
|
13
13
|
import {GramlotHtmlRenderer} from '../renderer/gramlot-html-renderer.js';
|
|
14
14
|
// Byte copy of src/gramlot/collections/binding.json: the JS package ships js/ only.
|
|
15
15
|
import BINDING from './binding.json' with {type: 'json'};
|
|
16
|
+
import WEBCOMPONENT from './webcomponent.json' with {type: 'json'};
|
|
17
|
+
import {webcomponentCode} from '../webcomponent.js';
|
|
16
18
|
|
|
17
19
|
// P19: `data` is ambiguous between the legacy data-element and the HTML5 element.
|
|
18
20
|
const DATA_FORBIDDEN = 'data is forbidden: write dataSetter for a Data value or html_data for the HTML5 <data> element';
|
|
19
21
|
// Declarations outside Gramlot 0.2.0 that fail explicitly (source plan, section 2).
|
|
20
|
-
const EXCLUDED_ELEMENTS = new Set(
|
|
21
|
-
const EXCLUDED_ATTRIBUTES = new Set(['serverpath', 'dbenv', 'shared_id', '
|
|
22
|
+
const EXCLUDED_ELEMENTS = new Set();
|
|
23
|
+
const EXCLUDED_ATTRIBUTES = new Set(['serverpath', 'dbenv', 'shared_id', '_ask', 'ask']);
|
|
22
24
|
const EXCLUDED_ATTRIBUTE_PREFIXES = ['subscribe_', 'selfsubscribe_', 'formsubscribe_'];
|
|
23
25
|
|
|
24
26
|
// P18 and B8: the Data path of each data-element, which does not accept `?attr`,
|
|
25
27
|
// and its inline attribute, which excludes `func` on the same node, as `_if` does (Q11.1).
|
|
26
|
-
const DATA_PATH_ATTRIBUTES = {dataSetter: 'destination_path', dataFormula: 'result_path', dataRpc: 'result_path'
|
|
28
|
+
const DATA_PATH_ATTRIBUTES = {dataSetter: 'destination_path', dataFormula: 'result_path', dataRpc: 'result_path',
|
|
29
|
+
dataRemote: 'destination_path'};
|
|
27
30
|
const DATA_INLINE_ATTRIBUTES = {dataFormula: 'formula', dataController: 'script'};
|
|
28
31
|
|
|
29
32
|
/** The P18 violation of a data-element's attributes, or null. */
|
|
@@ -54,6 +57,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
54
57
|
static _sourceClass = GramlotBuilderBag;
|
|
55
58
|
// Composed on the inherited HtmlBuilder grammar; its data-elements replace Builder's.
|
|
56
59
|
static { this.defineGrammar(BINDING); }
|
|
60
|
+
static { this.defineGrammar(WEBCOMPONENT); }
|
|
57
61
|
|
|
58
62
|
/** Tag prefixes of the parent dialects, from the `_name` of the classes above GramlotBuilder. */
|
|
59
63
|
static get dialectPrefixes() {
|
|
@@ -127,6 +131,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
127
131
|
|
|
128
132
|
/** Builder's value check, plus the excluded attributes (authoring names the tag). */
|
|
129
133
|
validateNodeValues(tag, value, attrs = {}) {
|
|
134
|
+
if (tag === 'webcomponent') webcomponentCode(attrs);
|
|
130
135
|
const excluded = excludedAttribute(attrs);
|
|
131
136
|
if (excluded) throw new Error(`${tag}: attribute '${excluded}' is excluded from Gramlot 0.2.0`);
|
|
132
137
|
const error = dataElementError(tag, attrs);
|
|
@@ -136,6 +141,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
136
141
|
|
|
137
142
|
/** Builder's node check, plus the excluded attributes (a Source node names tag and label). */
|
|
138
143
|
validateNode(node, options = {}) {
|
|
144
|
+
if (node.nodeTag === 'webcomponent') webcomponentCode(options.attrs ?? node.getAttr());
|
|
139
145
|
const excluded = excludedAttribute(options.attrs ?? node.getAttr());
|
|
140
146
|
if (excluded) throw new Error(`${node.nodeTag} '${node.label}': attribute '${excluded}' is excluded from Gramlot 0.2.0`);
|
|
141
147
|
const error = dataElementError(node.nodeTag, options.attrs ?? node.getAttr());
|
|
@@ -143,6 +149,11 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
143
149
|
return super.validateNode(node, options);
|
|
144
150
|
}
|
|
145
151
|
|
|
152
|
+
/** Custom elements are outside the native HTML child catalogue, as components are. */
|
|
153
|
+
_countsForGrammar(info) {
|
|
154
|
+
return info !== this.schema.webcomponent && super._countsForGrammar(info);
|
|
155
|
+
}
|
|
156
|
+
|
|
146
157
|
/** Builder's child insertion; a plain object `value` of `dataSetter` becomes `new Bag(value)` (P25). */
|
|
147
158
|
setChild(bag, tag, value, attrs, parentOverride = null) {
|
|
148
159
|
const plain = tag === 'dataSetter' && attrs?.value !== null && typeof attrs?.value === 'object'
|
|
@@ -182,7 +193,8 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
182
193
|
}
|
|
183
194
|
let id = node.getAttr('__ref');
|
|
184
195
|
if (id === null || id === undefined) {
|
|
185
|
-
id =
|
|
196
|
+
id = Array.from(crypto.getRandomValues(new Uint8Array(16)),
|
|
197
|
+
byte => byte.toString(16).padStart(2, '0')).join('');
|
|
186
198
|
node.setAttr({__ref: id});
|
|
187
199
|
}
|
|
188
200
|
return {$gramlotRef: id, kind};
|
package/src/builder/source.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {SourceBag, SourceBagNode, sourceAttributeItems} from '@genrojs/builders';
|
|
1
|
+
import {SourceBag, SourceBagNode, sourceAttributeItems, wrapSource} from '@genrojs/builders';
|
|
2
2
|
import {getSubtypeDict, setSubtypeDict} from '@genrojs/tytx';
|
|
3
3
|
|
|
4
4
|
// The inline code attributes of each tag, besides `connect_on<event>` and the `==` expressions of any tag.
|
|
@@ -6,9 +6,16 @@ const CODE_ATTRIBUTES = {
|
|
|
6
6
|
dataFormula: ['formula', '_if', '_else'],
|
|
7
7
|
dataController: ['script', '_if', '_else'],
|
|
8
8
|
dataRpc: ['_if', '_else', '_onCalling', '_onResult', '_onError'],
|
|
9
|
+
dataRemote: ['_if', '_else'],
|
|
9
10
|
button: ['action'],
|
|
10
11
|
};
|
|
11
12
|
|
|
13
|
+
// The control attributes of `remote`, written on the node as they are; every other parameter becomes `remote_<k>`.
|
|
14
|
+
const REMOTE_CONTROLS = ['_cachedRemote', '_if', '_else', '_onRemote'];
|
|
15
|
+
// The inline code attributes of any node whose `remote` is a string.
|
|
16
|
+
const REMOTE_CODE_ATTRIBUTES = ['_if', '_else', '_onRemote'];
|
|
17
|
+
const REMOTE_NAME = /^[A-Za-z]\w*$/;
|
|
18
|
+
|
|
12
19
|
/** Whether `value` is a `==` expression (Q11.2). */
|
|
13
20
|
function isExpression(value) {
|
|
14
21
|
return typeof value === 'string' && value.startsWith('==');
|
|
@@ -31,10 +38,12 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
31
38
|
* The inline code attributes of this node as `[name, text]`, under the names InlineCompiler uses:
|
|
32
39
|
* `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of the three providers and the
|
|
33
40
|
* `_onCalling`/`_onResult`/`_onError` of a dataRpc, `action` of a
|
|
34
|
-
* button,
|
|
41
|
+
* button, `_if`/`_else`/`_onRemote` of a node whose `remote` is a string, every `connect_on<event>`,
|
|
42
|
+
* every `==` attribute (canonical name) and a `==` node value (`''`).
|
|
35
43
|
*/
|
|
36
44
|
codeAttributes() {
|
|
37
|
-
const own = CODE_ATTRIBUTES[this.nodeTag] ?? []
|
|
45
|
+
const own = [...(CODE_ATTRIBUTES[this.nodeTag] ?? []),
|
|
46
|
+
...(typeof this.getAttr('remote') === 'string' ? REMOTE_CODE_ATTRIBUTES : [])];
|
|
38
47
|
const items = [];
|
|
39
48
|
for (const [name, value] of Object.entries(this.getAttr() ?? {})) {
|
|
40
49
|
if (typeof value === 'string' && (own.includes(name) || name.startsWith('connect_on'))) items.push([name, value]);
|
|
@@ -69,6 +78,29 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
69
78
|
return this.#activatedCode?.get(name) ?? null;
|
|
70
79
|
}
|
|
71
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Declare the content of this element as the fragment `name` of a `@source` method: writes
|
|
83
|
+
* `remote = name` and one `remote_<k> = v` per entry of `params` on this node; the control attributes
|
|
84
|
+
* `_cachedRemote`, `_if`, `_else` and `_onRemote` are written as they are. Returns this node. A bad
|
|
85
|
+
* name or another `_` parameter is a TypeError.
|
|
86
|
+
*/
|
|
87
|
+
remote(name, params = {}) {
|
|
88
|
+
if (typeof name !== 'string' || !REMOTE_NAME.test(name)) {
|
|
89
|
+
throw new TypeError(`${this.nodeTag} '${this.label}': remote name must match ${REMOTE_NAME.source}, not ${JSON.stringify(name)}`);
|
|
90
|
+
}
|
|
91
|
+
const invalid = Object.keys(params).find(key => key.startsWith('_') && !REMOTE_CONTROLS.includes(key));
|
|
92
|
+
if (invalid) {
|
|
93
|
+
throw new TypeError(`${this.nodeTag} '${this.label}': remote parameter '${invalid}' is not accepted; `
|
|
94
|
+
+ `the control attributes are ${REMOTE_CONTROLS.join(', ')}`);
|
|
95
|
+
}
|
|
96
|
+
const attributes = {remote: name};
|
|
97
|
+
for (const [key, value] of Object.entries(params)) {
|
|
98
|
+
attributes[REMOTE_CONTROLS.includes(key) ? key : `remote_${key}`] = value;
|
|
99
|
+
}
|
|
100
|
+
this.setAttr(attributes);
|
|
101
|
+
return wrapSource(this);
|
|
102
|
+
}
|
|
103
|
+
|
|
72
104
|
/** Builder's pointerType, except that a string starting with `==` is not a pointer. */
|
|
73
105
|
pointerType(v) {
|
|
74
106
|
if (typeof v === 'string' && v.startsWith('==')) return null;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"document_format": {
|
|
3
|
+
"name": "builder_grammar",
|
|
4
|
+
"version": "1.1"
|
|
5
|
+
},
|
|
6
|
+
"grammar": {
|
|
7
|
+
"name": "webcomponent",
|
|
8
|
+
"version": null,
|
|
9
|
+
"title": "Gramlot webcomponent",
|
|
10
|
+
"description": "An explicitly loaded autonomous custom element."
|
|
11
|
+
},
|
|
12
|
+
"abstracts": {},
|
|
13
|
+
"elements": {
|
|
14
|
+
"webcomponent": {
|
|
15
|
+
"doc": "Create the custom-element tag named by literal code.",
|
|
16
|
+
"sub_tags": "*",
|
|
17
|
+
"parent_tags": null,
|
|
18
|
+
"inherits_from": null,
|
|
19
|
+
"ns": null,
|
|
20
|
+
"attributes": {
|
|
21
|
+
"parameters": [
|
|
22
|
+
{
|
|
23
|
+
"name": "code",
|
|
24
|
+
"kind": "keyword_only",
|
|
25
|
+
"role": "attribute",
|
|
26
|
+
"annotation": {
|
|
27
|
+
"kind": "type",
|
|
28
|
+
"module": "builtins",
|
|
29
|
+
"name": "str"
|
|
30
|
+
},
|
|
31
|
+
"has_default": false
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "attr",
|
|
35
|
+
"kind": "var_keyword",
|
|
36
|
+
"role": "attribute",
|
|
37
|
+
"annotation": {
|
|
38
|
+
"kind": "any"
|
|
39
|
+
},
|
|
40
|
+
"has_default": false
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"accepts_var_keyword": true,
|
|
44
|
+
"accepts_var_positional": false
|
|
45
|
+
},
|
|
46
|
+
"node_label": null,
|
|
47
|
+
"collection_key": null,
|
|
48
|
+
"_meta": null
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -41,7 +41,7 @@ export class SourceHandler extends Handler {
|
|
|
41
41
|
* validates. Resolves with true when applied, false when superseded or cancelled.
|
|
42
42
|
*
|
|
43
43
|
* A declared fragment is requested from code with `gramlot.src.remoteSource` (GC-230 §035); the
|
|
44
|
-
*
|
|
44
|
+
* `remote` attribute requests it declaratively (GC-095 §105).
|
|
45
45
|
*/
|
|
46
46
|
remoteSource(target: SourceBagNode, method: string, params?: Record<string, unknown>): Promise<boolean>;
|
|
47
47
|
}
|
|
@@ -80,7 +80,7 @@ export class SourceHandler extends Handler {
|
|
|
80
80
|
* or a freeze keeps it, the removal of the target cancels it.
|
|
81
81
|
*
|
|
82
82
|
* `@source` / `registerSource` declare the fragments this method requests (GC-230 §035); the
|
|
83
|
-
*
|
|
83
|
+
* `remote` attribute requests them declaratively (GC-095 §105).
|
|
84
84
|
*/
|
|
85
85
|
async remoteSource(target, method, params = {}) {
|
|
86
86
|
const rpc = this.gramlot.rpc;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function mountIndexPage(renderer: any, root: any,
|
|
2
|
+
onSelection?: (entries: Map<string, any>, active: string) => void,
|
|
3
|
+
prepareEntry?: (entry: any, context: any) => void): {dispose(): void};
|
|
4
|
+
/** Re-export as Logic, or delegate indexMount from an application's Logic. */
|
|
5
|
+
export class IndexLogic {
|
|
6
|
+
indexMount(node: any, args: {shell_id: string}): void;
|
|
7
|
+
}
|
|
8
|
+
export const THEME_READY: string;
|
|
9
|
+
export const THEME_SET: string;
|
|
10
|
+
export const THEMES: Set<string>;
|
|
11
|
+
export const APPEARANCES: Set<string>;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/* @ts-self-types="./index-page-runtime.d.ts" */
|
|
2
|
+
import {compactStyle} from './index-page-styles.js';
|
|
3
|
+
import {THEME_READY, THEME_SET, THEMES, APPEARANCES} from './index-theme.js';
|
|
4
|
+
|
|
5
|
+
/** Index-page behavior; writes UI state through the existing Source records. */
|
|
6
|
+
export function mountIndexPage(renderer, root, onSelection = () => {}, prepareEntry = () => {}) {
|
|
7
|
+
const shell = renderer.records.get(root).element;
|
|
8
|
+
const document = shell.ownerDocument;
|
|
9
|
+
const window = document.defaultView;
|
|
10
|
+
const byId = id => document.getElementById(id);
|
|
11
|
+
const node = element => renderer.elements.get(element).node;
|
|
12
|
+
const set = (element, attrs) => node(element).setAttr(attrs);
|
|
13
|
+
const keyboard = byId('keyboard-navigation');
|
|
14
|
+
const theme = byId('index-theme');
|
|
15
|
+
const appearance = byId('index-appearance');
|
|
16
|
+
const sidebar = byId('index-sidebar');
|
|
17
|
+
const sidebarHandle = byId('index-sidebar-divider');
|
|
18
|
+
// localStorage is scoped to this origin; the path separates mounted galleries.
|
|
19
|
+
const preferencesKey = `gramlot.${shell.id}.preferences:${window.location.pathname}`;
|
|
20
|
+
let preferences = {};
|
|
21
|
+
try {
|
|
22
|
+
const saved = JSON.parse(window.localStorage.getItem(preferencesKey));
|
|
23
|
+
if (saved !== null && typeof saved === 'object' && !Array.isArray(saved)) preferences = saved;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.name !== 'SyntaxError' && error.name !== 'SecurityError') throw error;
|
|
26
|
+
}
|
|
27
|
+
const restoreChoice = (control, value, allowed) => {
|
|
28
|
+
if (!control || !allowed.has(value)) return;
|
|
29
|
+
set(control, {value});
|
|
30
|
+
for (const option of control.options) set(option, {selected: option.value === value});
|
|
31
|
+
};
|
|
32
|
+
restoreChoice(theme, preferences.theme, THEMES);
|
|
33
|
+
restoreChoice(appearance, preferences.appearance, APPEARANCES);
|
|
34
|
+
const families = [...shell.querySelectorAll('.index-sidebar details')];
|
|
35
|
+
const Bag = root.builder.data.constructor;
|
|
36
|
+
const state = new Bag();
|
|
37
|
+
const opened = new Bag();
|
|
38
|
+
state.setItem('opened', opened);
|
|
39
|
+
state.setItem('keyboard', (keyboard?.checked ?? false));
|
|
40
|
+
state.setItem('theme', (theme?.value ?? 'light'));
|
|
41
|
+
state.setItem('appearance', (appearance?.value ?? 'compact'));
|
|
42
|
+
const savePreferences = () => {
|
|
43
|
+
try {
|
|
44
|
+
window.localStorage.setItem(preferencesKey, JSON.stringify({
|
|
45
|
+
theme: state.getItem('theme'), appearance: state.getItem('appearance'),
|
|
46
|
+
sidebarWidth: state.getItem('preferredSidebarWidth'),
|
|
47
|
+
}));
|
|
48
|
+
} catch (error) {
|
|
49
|
+
// A browser may deny storage (including opaque file origins) or exhaust its quota.
|
|
50
|
+
// Keep the current interaction usable; no other persistence mechanism is used.
|
|
51
|
+
if (error.name !== 'SecurityError' && error.name !== 'QuotaExceededError') throw error;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const entries = new Map();
|
|
55
|
+
const releases = [];
|
|
56
|
+
const style = root.builder.wrapSource(root.value).style(compactStyle, {media: (appearance?.value ?? 'compact') === 'compact' ? 'all' : 'not all'});
|
|
57
|
+
let drag = null;
|
|
58
|
+
let disposed = false;
|
|
59
|
+
let installPrompt = null;
|
|
60
|
+
const previousTheme = document.documentElement.getAttribute('data-theme');
|
|
61
|
+
const listen = (element, type, handler) => {
|
|
62
|
+
if (!element) return;
|
|
63
|
+
element.addEventListener(type, handler);
|
|
64
|
+
releases.push(() => element.removeEventListener(type, handler));
|
|
65
|
+
};
|
|
66
|
+
const stopDrag = () => {
|
|
67
|
+
if (!drag) return;
|
|
68
|
+
const {handle, pointerId} = drag;
|
|
69
|
+
drag = null;
|
|
70
|
+
if (!disposed) set(shell, {data_resizing: null});
|
|
71
|
+
if (handle.hasPointerCapture(pointerId)) handle.releasePointerCapture(pointerId);
|
|
72
|
+
};
|
|
73
|
+
const dispose = () => {
|
|
74
|
+
if (disposed) return;
|
|
75
|
+
disposed = true;
|
|
76
|
+
installPrompt = null;
|
|
77
|
+
stopDrag();
|
|
78
|
+
for (const release of releases.reverse()) release();
|
|
79
|
+
if (style.parentBag) style.parentBag.popNode(style.label);
|
|
80
|
+
if (previousTheme == null) document.documentElement.removeAttribute('data-theme');
|
|
81
|
+
else document.documentElement.setAttribute('data-theme', previousTheme);
|
|
82
|
+
};
|
|
83
|
+
const send = frame => frame.contentWindow.postMessage({type: THEME_SET, theme: state.getItem('theme'), appearance: state.getItem('appearance')}, '*');
|
|
84
|
+
const applyTheme = () => {
|
|
85
|
+
document.documentElement.setAttribute('data-theme', state.getItem('theme'));
|
|
86
|
+
style.setAttr({media: state.getItem('appearance') === 'compact' ? 'all' : 'not all'});
|
|
87
|
+
for (const {frame} of entries.values()) if (frame) send(frame);
|
|
88
|
+
};
|
|
89
|
+
const updateInspector = () => onSelection(entries, state.getItem('active'));
|
|
90
|
+
function select(key) {
|
|
91
|
+
const entry = entries.get(key);
|
|
92
|
+
if (!opened.getItem(key)) {
|
|
93
|
+
opened.setItem(key, true);
|
|
94
|
+
set(entry.tab, {hidden: false});
|
|
95
|
+
if (entry.frame) set(entry.frame, {src: entry.open.getAttribute('href')});
|
|
96
|
+
}
|
|
97
|
+
if (entry.family) set(entry.family, {open: true});
|
|
98
|
+
for (const [current, item] of entries) {
|
|
99
|
+
const active = current === key;
|
|
100
|
+
set(item.tab, {aria_selected: String(active), tabindex: active && state.getItem('keyboard') ? 0 : -1});
|
|
101
|
+
set(item.panel, {hidden: !active});
|
|
102
|
+
if (item.open) set(item.open, {aria_current: active ? 'page' : null});
|
|
103
|
+
}
|
|
104
|
+
state.setItem('active', key);
|
|
105
|
+
updateInspector();
|
|
106
|
+
}
|
|
107
|
+
function setRatio(entry, value) {
|
|
108
|
+
const ratio = Math.max(20, Math.min(80, value));
|
|
109
|
+
state.setItem(`ratio.${entry.key}`, ratio);
|
|
110
|
+
set(entry.split, {style: `--split-position: ${ratio}%`});
|
|
111
|
+
set(entry.handle, {aria_valuenow: ratio});
|
|
112
|
+
}
|
|
113
|
+
const sidebarMaximum = () => Math.max(120, Math.min(460, Math.floor(window.innerWidth * .6)));
|
|
114
|
+
const setSidebarWidth = (value, remember = false) => {
|
|
115
|
+
const maximum = sidebarMaximum();
|
|
116
|
+
const width = Math.max(120, Math.min(maximum, value));
|
|
117
|
+
state.setItem('sidebarWidth', width);
|
|
118
|
+
set(shell, {style: `--sidebar-width: ${width}px`});
|
|
119
|
+
set(sidebarHandle, {aria_valuenow: width, aria_valuemax: maximum});
|
|
120
|
+
if (remember) {
|
|
121
|
+
state.setItem('preferredSidebarWidth', width);
|
|
122
|
+
savePreferences();
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const beginDrag = (handle, event, move) => {
|
|
126
|
+
if (event.button !== 0) return;
|
|
127
|
+
stopDrag();
|
|
128
|
+
handle.setPointerCapture(event.pointerId);
|
|
129
|
+
drag = {handle, pointerId: event.pointerId, move};
|
|
130
|
+
set(shell, {data_resizing: 'true'});
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
};
|
|
133
|
+
try {
|
|
134
|
+
const install = byId('index-install-button');
|
|
135
|
+
if (install) {
|
|
136
|
+
listen(window, 'beforeinstallprompt', event => {
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
installPrompt = event;
|
|
139
|
+
set(install, {hidden: false, disabled: false});
|
|
140
|
+
});
|
|
141
|
+
listen(window, 'appinstalled', () => {
|
|
142
|
+
installPrompt = null;
|
|
143
|
+
set(install, {hidden: true});
|
|
144
|
+
node(byId('index-install-status')).setValue('App installed.');
|
|
145
|
+
});
|
|
146
|
+
listen(install, 'click', async () => {
|
|
147
|
+
if (!installPrompt) return;
|
|
148
|
+
const prompt = installPrompt;
|
|
149
|
+
installPrompt = null;
|
|
150
|
+
set(install, {disabled: true});
|
|
151
|
+
try {
|
|
152
|
+
await prompt.prompt();
|
|
153
|
+
const {outcome} = await prompt.userChoice;
|
|
154
|
+
if (!disposed) node(byId('index-install-status')).setValue(
|
|
155
|
+
outcome === 'accepted' ? 'Installation accepted.' : 'Installation cancelled. You can also use the browser menu.');
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (!disposed) node(byId('index-install-status')).setValue(`Installation failed: ${error.message}`);
|
|
158
|
+
} finally {
|
|
159
|
+
if (!disposed) set(install, {hidden: true});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
set(shell, {data_resizing: null});
|
|
164
|
+
// Retain the preferred width when a narrow viewport temporarily limits the visible sidebar.
|
|
165
|
+
const savedWidth = preferences.sidebarWidth;
|
|
166
|
+
const width = typeof savedWidth === 'number' && Number.isFinite(savedWidth) && savedWidth >= 120 && savedWidth <= 460
|
|
167
|
+
? savedWidth : sidebar.getBoundingClientRect().width || Number(sidebarHandle.getAttribute('aria-valuenow'));
|
|
168
|
+
state.setItem('preferredSidebarWidth', width);
|
|
169
|
+
setSidebarWidth(width);
|
|
170
|
+
listen(sidebarHandle, 'keydown', event => {
|
|
171
|
+
const current = state.getItem('sidebarWidth');
|
|
172
|
+
const value = event.key === 'ArrowLeft' ? current - 10 : event.key === 'ArrowRight' ? current + 10
|
|
173
|
+
: event.key === 'Home' ? 120 : event.key === 'End' ? sidebarMaximum() : null;
|
|
174
|
+
if (value == null) return;
|
|
175
|
+
event.preventDefault();
|
|
176
|
+
setSidebarWidth(value, true);
|
|
177
|
+
});
|
|
178
|
+
listen(sidebarHandle, 'pointerdown', event => beginDrag(sidebarHandle, event,
|
|
179
|
+
x => setSidebarWidth(Math.round(x - shell.getBoundingClientRect().left), true)));
|
|
180
|
+
listen(sidebarHandle, 'lostpointercapture', () => {
|
|
181
|
+
if (drag?.handle === sidebarHandle) stopDrag();
|
|
182
|
+
});
|
|
183
|
+
listen(window, 'resize', () => setSidebarWidth(state.getItem('preferredSidebarWidth')));
|
|
184
|
+
for (const tab of shell.querySelectorAll('.index-tabs button')) {
|
|
185
|
+
const key = tab.id.slice('tab-'.length);
|
|
186
|
+
const entry = {key, tab, panel: byId(`panel-${key}`),
|
|
187
|
+
open: key === 'intro' ? null : byId(`open-${key}`)};
|
|
188
|
+
entries.set(key, entry);
|
|
189
|
+
if (!tab.hidden) opened.setItem(key, true);
|
|
190
|
+
if (tab.getAttribute('aria-selected') === 'true') state.setItem('active', key);
|
|
191
|
+
listen(tab, 'click', () => select(key));
|
|
192
|
+
if (entry.open) listen(entry.open, 'click', event => { event.preventDefault(); select(key); });
|
|
193
|
+
listen(tab, 'keydown', event => {
|
|
194
|
+
if (!state.getItem('keyboard') || !['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
|
|
195
|
+
const keys = [...entries.keys()].filter(key => opened.getItem(key));
|
|
196
|
+
const index = keys.indexOf(key);
|
|
197
|
+
const next = event.key === 'Home' ? 0 : event.key === 'End' ? keys.length - 1
|
|
198
|
+
: (index + (event.key === 'ArrowRight' ? 1 : -1) + keys.length) % keys.length;
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
select(keys[next]);
|
|
201
|
+
entries.get(keys[next]).tab.focus();
|
|
202
|
+
});
|
|
203
|
+
if (key === 'intro') continue;
|
|
204
|
+
entry.family = entry.open.closest('details');
|
|
205
|
+
prepareEntry(entry, {byId, node, set, listen});
|
|
206
|
+
if (entry.panel.classList.contains('index-category')) continue;
|
|
207
|
+
entry.frame = byId(`frame-${key}`);
|
|
208
|
+
entry.split = byId(`split-${key}`);
|
|
209
|
+
entry.handle = byId(`divider-${key}`);
|
|
210
|
+
if (entry.handle) state.setItem(`ratio.${key}`, Number(entry.handle.getAttribute('aria-valuenow')));
|
|
211
|
+
listen(entry.frame, 'load', () => send(entry.frame));
|
|
212
|
+
if (!entry.handle) continue;
|
|
213
|
+
listen(entry.handle, 'keydown', event => {
|
|
214
|
+
const current = state.getItem(`ratio.${key}`);
|
|
215
|
+
const value = event.key === 'ArrowLeft' ? current - 5 : event.key === 'ArrowRight' ? current + 5
|
|
216
|
+
: event.key === 'Home' ? 20 : event.key === 'End' ? 80 : null;
|
|
217
|
+
if (value == null) return;
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
setRatio(entry, value);
|
|
220
|
+
});
|
|
221
|
+
listen(entry.handle, 'pointerdown', event => beginDrag(entry.handle, event, x => {
|
|
222
|
+
const bounds = entry.split.getBoundingClientRect();
|
|
223
|
+
if (bounds.width > 0) setRatio(entry, Math.round((x - bounds.left) * 100 / bounds.width));
|
|
224
|
+
}));
|
|
225
|
+
listen(entry.handle, 'lostpointercapture', () => {
|
|
226
|
+
if (drag?.handle === entry.handle) stopDrag();
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
listen(document, 'pointermove', event => {
|
|
230
|
+
if (!drag || event.pointerId !== drag.pointerId) return;
|
|
231
|
+
drag.move(event.clientX);
|
|
232
|
+
});
|
|
233
|
+
for (const type of ['pointerup', 'pointercancel']) listen(document, type, event => {
|
|
234
|
+
if (drag && event.pointerId === drag.pointerId) stopDrag();
|
|
235
|
+
});
|
|
236
|
+
for (const family of families) listen(family, 'toggle', () => set(family, {open: family.open}));
|
|
237
|
+
listen(keyboard, 'change', () => {
|
|
238
|
+
state.setItem('keyboard', (keyboard?.checked ?? false));
|
|
239
|
+
set(keyboard, {checked: (keyboard?.checked ?? false)});
|
|
240
|
+
for (const [key, entry] of entries) {
|
|
241
|
+
if (entry.open) set(entry.open, {tabindex: (keyboard?.checked ?? false) ? 0 : -1});
|
|
242
|
+
set(entry.tab, {tabindex: (keyboard?.checked ?? false) && key === state.getItem('active') ? 0 : -1});
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
listen(appearance, 'change', () => {
|
|
246
|
+
if (!APPEARANCES.has((appearance?.value ?? 'compact'))) throw new Error('Index appearance must be base or compact');
|
|
247
|
+
state.setItem('appearance', (appearance?.value ?? 'compact'));
|
|
248
|
+
set(appearance, {value: (appearance?.value ?? 'compact')});
|
|
249
|
+
for (const option of appearance.options) set(option, {selected: option.value === (appearance?.value ?? 'compact')});
|
|
250
|
+
applyTheme();
|
|
251
|
+
savePreferences();
|
|
252
|
+
});
|
|
253
|
+
listen(theme, 'change', () => {
|
|
254
|
+
if (!THEMES.has((theme?.value ?? 'light'))) throw new Error('Index theme must be light or dark');
|
|
255
|
+
state.setItem('theme', (theme?.value ?? 'light'));
|
|
256
|
+
set(theme, {value: (theme?.value ?? 'light')});
|
|
257
|
+
for (const option of theme.options) set(option, {selected: option.value === (theme?.value ?? 'light')});
|
|
258
|
+
applyTheme();
|
|
259
|
+
savePreferences();
|
|
260
|
+
});
|
|
261
|
+
listen(window, 'message', event => {
|
|
262
|
+
if (event.data?.type !== THEME_READY) return;
|
|
263
|
+
for (const {frame} of entries.values()) {
|
|
264
|
+
if (frame && frame.contentWindow === event.source) { send(frame); updateInspector(); break; }
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
applyTheme();
|
|
268
|
+
updateInspector();
|
|
269
|
+
renderer.onDispose(root, dispose);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
dispose();
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
return {dispose};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
/** Re-export as the page's Logic, or delegate indexMount from a custom Logic. */
|
|
279
|
+
export class IndexLogic {
|
|
280
|
+
indexMount(node, {shell_id}) {
|
|
281
|
+
const renderer = this.page.src.renderer;
|
|
282
|
+
const element = renderer.destination.ownerDocument.getElementById(shell_id);
|
|
283
|
+
this.index = mountIndexPage(renderer, renderer.elements.get(element).node);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export {THEME_READY, THEME_SET, THEMES, APPEARANCES} from "./index-theme.js";
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// Generated from themes/gramlot-compact/theme.css.
|
|
2
|
+
export const compactStyle = "/* Gramlot Compact: load after gramlot-base/theme.css. Colors and behavior stay in Base. */\n@layer gramlot.tokens {\n :root {\n --gramlot-space-1: .2rem;\n --gramlot-space-2: .4rem;\n --gramlot-space-3: .5rem;\n --gramlot-space-4: .6rem;\n --gramlot-space-6: .9rem;\n --gramlot-space-8: 1.2rem;\n --gramlot-space-12: 1.8rem;\n --gramlot-radius-sm: .25rem;\n --gramlot-radius: .35rem;\n --gramlot-radius-lg: .4rem;\n }\n}\n\n@layer gramlot.base {\n html { font-size: 13px; }\n body { line-height: 1.35; }\n :where(h1, h2, h3, h4, h5, h6, p, ul, ol, dl, blockquote, pre, figure) { margin-block: 0 .5rem; }\n h1 { font-size: 1.35rem; }\n h2 { font-size: 1.15rem; }\n h3 { font-size: 1.05rem; }\n pre { padding: .5rem; }\n blockquote { padding-inline-start: .6rem; }\n hr { margin-block: 1rem; }\n}\n\n@layer gramlot.components {\n :where(button, input[type=\"button\"], input[type=\"submit\"], input[type=\"reset\"], .button) {\n min-block-size: 28px;\n padding: .25rem .5rem;\n font-size: 1rem;\n line-height: 1.35;\n }\n :where(label) { font-size: .95rem; }\n :where(input:not([type=\"button\"]):not([type=\"submit\"]):not([type=\"reset\"]):not([type=\"checkbox\"]):not([type=\"radio\"]):not([type=\"range\"]):not([type=\"color\"]):not([type=\"file\"]):not([type=\"hidden\"]), select, textarea) {\n min-block-size: 28px;\n padding: .25rem .5rem;\n font-size: 1rem;\n line-height: 1.35;\n }\n :where(textarea) { min-block-size: 5rem; }\n :where(input[type=\"color\"]) { min-block-size: 28px; }\n :where(input[type=\"file\"])::file-selector-button { margin-inline-end: .5rem; padding: .25rem .5rem; }\n :where(fieldset) { margin-block-end: .6rem; padding: .5rem; }\n :where(table) { font-size: 1rem; }\n :where(th, td) { padding: .4rem .5rem; }\n :where(details) { padding: .4rem .5rem; }\n :where(details[open] > summary) { margin-block-end: .5rem; }\n :where(dialog) { padding: .75rem; }\n}\n\n@layer gramlot.utilities {\n .example-page { inline-size: min(100% - 1.2rem, var(--gramlot-page-width)); padding-block: .6rem; }\n .card { padding: .6rem; box-shadow: none; }\n}\n";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** The same bounded parent/frame theme messages serve hosted and local-file pages. */
|
|
2
|
+
export const THEME_READY = 'index:theme-ready';
|
|
3
|
+
export const THEME_SET = 'index:theme-set';
|
|
4
|
+
export const THEMES = new Set(['light', 'dark']);
|
|
5
|
+
export const APPEARANCES = new Set(['base', 'compact']);
|
package/src/index.d.ts
CHANGED
|
@@ -28,3 +28,6 @@ export {RpcError} from './handlers/rpc-handler.js';
|
|
|
28
28
|
export type {RpcErrorOutcome, RpcTransport} from './handlers/rpc-handler.js';
|
|
29
29
|
export {InOut} from './inout.js';
|
|
30
30
|
export type {BeaconNavigator, DomDocument, DomElement, DomEvent, DomFragment, DomNode, DomWindow} from './dom.d.ts';
|
|
31
|
+
|
|
32
|
+
export {IndexPage} from "./server/index-page.js";
|
|
33
|
+
export {IndexLogic, mountIndexPage} from "./index-page-runtime.js";
|
package/src/index.js
CHANGED
|
@@ -24,3 +24,6 @@ export {References} from './references.js';
|
|
|
24
24
|
export {HttpTransport} from './http-transport.js';
|
|
25
25
|
export {RpcError} from './handlers/rpc-handler.js';
|
|
26
26
|
export {InOut} from './inout.js';
|
|
27
|
+
|
|
28
|
+
export {IndexPage} from './server/index-page.js';
|
|
29
|
+
export {IndexLogic, mountIndexPage} from './index-page-runtime.js';
|
package/src/inout.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
import {Bag} from '@genrojs/bag';
|
|
8
|
+
import {fromTytx} from '@genrojs/tytx';
|
|
8
9
|
|
|
9
10
|
/** The practical length limit of a mailto: URL in mail programs and browsers. */
|
|
10
11
|
export const MAILTO_LIMIT = 2000;
|
|
@@ -88,7 +89,8 @@ export class InOut {
|
|
|
88
89
|
return new Promise((resolve, reject) => {
|
|
89
90
|
input.addEventListener('change', async () => {
|
|
90
91
|
try {
|
|
91
|
-
const bag =
|
|
92
|
+
const bag = fromTytx(await input.files[0].text());
|
|
93
|
+
if (!(bag instanceof Bag)) throw new TypeError('the file must contain a typed Bag');
|
|
92
94
|
this.gramlot.data.setItem(path, bag);
|
|
93
95
|
resolve(bag);
|
|
94
96
|
} catch (error) {
|
|
@@ -10,8 +10,9 @@ export const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
|
10
10
|
const BINDING_ATTRIBUTES = new Set([
|
|
11
11
|
'default', 'default_value', 'live', 'group', 'visible', 'action', 'fire',
|
|
12
12
|
'if', 'else', '_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
|
|
13
|
+
'remote', '_cachedRemote', '_onRemote',
|
|
13
14
|
]);
|
|
14
|
-
const BINDING_ATTRIBUTE_PREFIXES = ['default_', 'attr_', 'fire_', 'connect_on'];
|
|
15
|
+
const BINDING_ATTRIBUTE_PREFIXES = ['default_', 'attr_', 'fire_', 'connect_on', 'remote_'];
|
|
15
16
|
|
|
16
17
|
// Legacy noConvertStyle (genro_wdg.js:102-108): these attributes stay native attributes, never style.
|
|
17
18
|
const NATIVE_ATTRIBUTES = {
|
|
@@ -37,6 +38,17 @@ export function withoutBindingAttributes(attrs) {
|
|
|
37
38
|
&& !BINDING_ATTRIBUTE_PREFIXES.some(prefix => name.startsWith(prefix))));
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
/** Validate resolved style/class before composition, including literal dialect attributes. */
|
|
42
|
+
export function requireStyleClassAttributes(node, attrs, prefixes = []) {
|
|
43
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
44
|
+
const prefix = prefixes.find(candidate => name.startsWith(candidate)) ?? '';
|
|
45
|
+
const bare = name.slice(prefix.length);
|
|
46
|
+
if (['style', 'class', '_class'].includes(bare) && value != null && typeof value !== 'string') {
|
|
47
|
+
throw new TypeError(`${node.nodeTag} '${node.label}': '${name}' must be a string or null`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
40
52
|
/**
|
|
41
53
|
* No attribute of `attrs` (resolved, `==` evaluated) makes the browser run a text outside InlineCompiler:
|
|
42
54
|
* a name starting with `on` and longer than it, the form of the native event handlers, is an error pointing
|
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import {HtmlBuilder, HtmlRenderer, SvgBuilder, sourceAttributeItems} from '@genrojs/builders';
|
|
8
8
|
import {GramlotSvgRenderer} from './gramlot-svg-renderer.js';
|
|
9
|
+
import {webcomponentCode} from '../webcomponent.js';
|
|
9
10
|
import {
|
|
10
11
|
SVG_NS, XHTML_NS, boundaryAttributes, displayItem, domNames, isExpression, requireNoExpressionTemplate,
|
|
11
|
-
requireNoScriptAttributes, splitNativeAttributes, textValue, withDataSrcdocSandbox, withoutBindingAttributes,
|
|
12
|
+
requireNoScriptAttributes, requireStyleClassAttributes, splitNativeAttributes, textValue, withDataSrcdocSandbox, withoutBindingAttributes,
|
|
12
13
|
withoutNullValues,
|
|
13
14
|
} from './attributes.js';
|
|
14
15
|
|
|
@@ -52,7 +53,13 @@ export class GramlotHtmlRenderer extends HtmlRenderer {
|
|
|
52
53
|
const prefixes = this.builder.constructor.dialectPrefixes;
|
|
53
54
|
const evaluated = this.evaluateExpressions(node, runtimeAttrs);
|
|
54
55
|
requireNoScriptAttributes(node, evaluated, prefixes);
|
|
55
|
-
|
|
56
|
+
let [tag, attrs] = super._handleMeta(node, withDataSrcdocSandbox(node, evaluated, prefixes));
|
|
57
|
+
requireStyleClassAttributes(node, attrs, prefixes);
|
|
58
|
+
if (node.nodeTag === 'webcomponent') {
|
|
59
|
+
tag = webcomponentCode(node.getAttr());
|
|
60
|
+
const {code, ...rest} = attrs;
|
|
61
|
+
attrs = rest;
|
|
62
|
+
}
|
|
56
63
|
if (node._getMeta('subbuilder')) {
|
|
57
64
|
const boundary = boundaryAttributes(attrs);
|
|
58
65
|
if (node._getMeta('subbuilder') === 'html') boundary.xmlns = SVG_NS;
|
|
@@ -48,7 +48,8 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
48
48
|
this.disposed = false;
|
|
49
49
|
this.elements = new WeakMap();
|
|
50
50
|
// The instance id scopes the generated radio names to this page (P11, S11).
|
|
51
|
-
this.instanceId = crypto.
|
|
51
|
+
this.instanceId = Array.from(crypto.getRandomValues(new Uint8Array(16)),
|
|
52
|
+
byte => byte.toString(16).padStart(2, '0')).join('');
|
|
52
53
|
this.radioGroups = new RadioGroups(this);
|
|
53
54
|
this.subscription = `html-source-renderer-${this.instanceId}`;
|
|
54
55
|
source.subscribe(this.subscription, {any: event => this.receive(event)});
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import {SvgRenderer, svgAttributes} from '@genrojs/builders';
|
|
8
8
|
import {
|
|
9
|
-
boundaryAttributes, domNames, requireNoScriptAttributes, textValue, withoutBindingAttributes, withoutNullValues,
|
|
9
|
+
boundaryAttributes, domNames, requireNoScriptAttributes, requireStyleClassAttributes, textValue, withoutBindingAttributes, withoutNullValues,
|
|
10
10
|
} from './attributes.js';
|
|
11
11
|
|
|
12
12
|
// SVG elements written `<tag … />`, as the Python SvgRenderer of Builder (the grammar gives them no children).
|
|
@@ -42,6 +42,7 @@ export class GramlotSvgRenderer extends SvgRenderer {
|
|
|
42
42
|
const evaluated = this.owner.evaluateExpressions(node, runtimeAttrs);
|
|
43
43
|
requireNoScriptAttributes(node, evaluated);
|
|
44
44
|
const [tag, attrs] = super._handleMeta(node, evaluated);
|
|
45
|
+
requireStyleClassAttributes(node, attrs);
|
|
45
46
|
return node._getMeta('subbuilder') ? [tag, svgAttributes(boundaryAttributes(attrs))] : [tag, attrs];
|
|
46
47
|
}
|
|
47
48
|
|