@gramlot/gramlot 0.2.4 → 0.2.6
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/dist/gramlot.d.ts +8 -0
- package/dist/gramlot.js +287 -132
- package/package.json +4 -4
- package/src/adapters/file-host.d.ts +29 -0
- package/src/adapters/file-host.js +18 -4
- package/src/adapters/host.d.ts +101 -0
- package/src/adapters/host.js +9 -0
- package/src/adapters/index.d.ts +13 -0
- package/src/adapters/index.js +7 -0
- package/src/adapters/page.d.ts +33 -0
- package/src/adapters/page.js +6 -0
- package/src/adapters/resources.d.ts +28 -0
- package/src/adapters/resources.js +3 -0
- package/src/binding/inline.js +1 -1
- package/src/binding/logic.js +2 -2
- package/src/bootstrap.d.ts +48 -0
- package/src/bootstrap.js +6 -0
- package/src/builder/gramlot-builder.d.ts +50 -0
- package/src/builder/gramlot-builder.js +6 -0
- package/src/dom.d.ts +25 -0
- package/src/gramlot.d.ts +99 -0
- package/src/gramlot.js +8 -0
- package/src/index.d.ts +23 -0
- package/src/index.js +11 -0
- package/src/inout.d.ts +45 -0
- package/src/inout.js +135 -0
- package/src/references.d.ts +33 -0
- package/src/references.js +6 -1
- package/src/renderer/gramlot-html-renderer.d.ts +37 -0
- package/src/renderer/gramlot-html-renderer.js +6 -0
- package/src/renderer/gramlot-renderer.d.ts +75 -0
- package/src/renderer/gramlot-renderer.js +6 -0
- package/src/renderer/gramlot-svg-renderer.d.ts +27 -0
- package/src/renderer/gramlot-svg-renderer.js +6 -0
- package/src/transport.d.ts +34 -0
- package/src/transport.js +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"prepack": "node scripts/build-runtime.mjs"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@genrojs/bag": ">=0.10.
|
|
20
|
-
"@genrojs/builders": ">=0.4.
|
|
21
|
-
"@genrojs/tytx": ">=0.16.
|
|
19
|
+
"@genrojs/bag": ">=0.10.3",
|
|
20
|
+
"@genrojs/builders": ">=0.4.2",
|
|
21
|
+
"@genrojs/tytx": ">=0.16.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"c8": "10.1.3",
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A reference host on one pages folder.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import {Host} from './host.js';
|
|
7
|
+
import type {HostOptions} from './host.js';
|
|
8
|
+
import type {Page} from './page.js';
|
|
9
|
+
import type {Resources} from './resources.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Minimal reference `Host` on one pages folder, with APIs shared by Node.js and Bun. Page path
|
|
13
|
+
* `foo`: the file page `foo.js` first, then the folder page `foo/foo.js`. Beside the page file:
|
|
14
|
+
* `foo.css` and the page logic (the `Logic` export of the page module, or `foo_aux.js`).
|
|
15
|
+
*/
|
|
16
|
+
export class FileHost extends Host {
|
|
17
|
+
/** Create a host on `pagesDir`. */
|
|
18
|
+
constructor(pagesDir: string, options?: HostOptions);
|
|
19
|
+
/** The absolute pages folder. */
|
|
20
|
+
get pagesDir(): string;
|
|
21
|
+
/** Map `a/b` to `a/b.js`, else `a/b/b.js`, below the real pages folder; or raise `PageNotFound`. */
|
|
22
|
+
locatePage(path: string): Promise<string>;
|
|
23
|
+
/** Import the page module of `path` and return its `Page` export. */
|
|
24
|
+
resolvePage(path: string): Promise<typeof Page>;
|
|
25
|
+
/** `Page.css` URLs as written, then the companion CSS and the page logic. */
|
|
26
|
+
resolveResources(path: string, PageClass: typeof Page): Promise<Resources>;
|
|
27
|
+
/** Root-relative URL of a file below the pages folder; a file that leaves the folder raises. */
|
|
28
|
+
url(filename: string): Promise<string>;
|
|
29
|
+
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/* @ts-self-types="./file-host.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* A reference host on one pages folder.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
1
7
|
import {realpath, stat} from 'node:fs/promises';
|
|
2
8
|
import {basename, dirname, extname, isAbsolute, join, relative, resolve, sep} from 'node:path';
|
|
3
9
|
import {pathToFileURL} from 'node:url';
|
|
@@ -27,8 +33,11 @@ async function realFile(filename) {
|
|
|
27
33
|
/** Minimal reference Host on one pages folder, with APIs shared by Node.js and Bun.
|
|
28
34
|
* The Python counterpart is src/gramlot/server/file_host.py. Page path foo: the
|
|
29
35
|
* file page foo.js first, then the folder page foo/foo.js; the file wins when both
|
|
30
|
-
* exist. Beside the page file: foo.css (CSS)
|
|
31
|
-
*
|
|
36
|
+
* exist. Beside the page file: foo.css (CSS) and foo.md (README). The page logic
|
|
37
|
+
* (group null) is the Logic export of the page module itself, else foo_aux.js
|
|
38
|
+
* (JS module exporting Logic); both at once raise an Error. The page module then
|
|
39
|
+
* reaches the browser, so its imports must resolve there too. The _aux suffix is
|
|
40
|
+
* reserved. There are
|
|
32
41
|
* no resource levels: a name in css_requires/js_requires raises
|
|
33
42
|
* InvalidResourceName. Modules are trusted ESM application files; runtime module
|
|
34
43
|
* caching applies.
|
|
@@ -67,7 +76,7 @@ export class FileHost extends Host {
|
|
|
67
76
|
return (await import(pathToFileURL(await this.locatePage(path)).href)).Page;
|
|
68
77
|
}
|
|
69
78
|
|
|
70
|
-
/** Page.css URLs as written, then the
|
|
79
|
+
/** Page.css URLs as written, then the companion foo.css and the page logic. */
|
|
71
80
|
async resolveResources(path, PageClass) {
|
|
72
81
|
if (parseRequires(PageClass.css_requires).length || parseRequires(PageClass.js_requires).length) {
|
|
73
82
|
throw new InvalidResourceName('requires need a Host with a resource system');
|
|
@@ -76,7 +85,12 @@ export class FileHost extends Host {
|
|
|
76
85
|
const stem = join(dirname(pageFile), basename(pageFile, extname(pageFile)));
|
|
77
86
|
const css = [...PageClass.css], js = [];
|
|
78
87
|
if (await realFile(`${stem}.css`)) css.push(await this.url(`${stem}.css`));
|
|
79
|
-
|
|
88
|
+
const logic = [];
|
|
89
|
+
// The module is already in the runtime cache: resolvePage imported it.
|
|
90
|
+
if ('Logic' in await import(pathToFileURL(pageFile).href)) logic.push(await this.url(pageFile));
|
|
91
|
+
if (await realFile(`${stem}_aux.js`)) logic.push(await this.url(`${stem}_aux.js`));
|
|
92
|
+
if (logic.length === 2) throw new Error(`Two logic modules for one page: ${logic.join(' and ')}`);
|
|
93
|
+
if (logic.length) js.push({url: logic[0], group: null});
|
|
80
94
|
return {css, js};
|
|
81
95
|
}
|
|
82
96
|
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The neutral host: registers pages and builds their Sources, with no HTTP engine.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type {Page} from './page.js';
|
|
7
|
+
import type {Resources} from './resources.js';
|
|
8
|
+
|
|
9
|
+
export type {Resources};
|
|
10
|
+
|
|
11
|
+
/** The page is unknown, expired or owned by someone else. */
|
|
12
|
+
export class PageExpired extends Error {}
|
|
13
|
+
/** No page exists for the requested path. */
|
|
14
|
+
export class PageNotFound extends Error {}
|
|
15
|
+
/** An unknown remote Source method, as the Python `SourceNotFound`. */
|
|
16
|
+
export class SourceNotFound extends Error {}
|
|
17
|
+
/** The page registry is full. */
|
|
18
|
+
export class HostCapacity extends Error {}
|
|
19
|
+
|
|
20
|
+
/** The options of a `Host`. */
|
|
21
|
+
export interface HostOptions {
|
|
22
|
+
/** URL of the runtime bundle. */
|
|
23
|
+
runtimeUrl?: string;
|
|
24
|
+
/** URL of the `main` Source endpoint. */
|
|
25
|
+
mainUrl?: string;
|
|
26
|
+
/** URL of the remote Source endpoint. */
|
|
27
|
+
sourceUrl?: string;
|
|
28
|
+
/** URL of the page close endpoint. */
|
|
29
|
+
closeUrl?: string;
|
|
30
|
+
/** Id of the root element of the page. */
|
|
31
|
+
rootId?: string;
|
|
32
|
+
/** Seconds a registered page lives. */
|
|
33
|
+
pageTtl?: number;
|
|
34
|
+
/** Maximum number of registered pages. */
|
|
35
|
+
maxPages?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The result of `registerPage`. */
|
|
39
|
+
export interface RegisteredPage {
|
|
40
|
+
/** The id of the registered page. */
|
|
41
|
+
pageId: string;
|
|
42
|
+
/** The title of the page. */
|
|
43
|
+
title: string;
|
|
44
|
+
/** The resources of the page, in load order. */
|
|
45
|
+
resources: Resources;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The result of `openPage`. */
|
|
49
|
+
export interface OpenedPage {
|
|
50
|
+
/** The id of the registered page. */
|
|
51
|
+
pageId: string;
|
|
52
|
+
/** The bootstrap HTML document. */
|
|
53
|
+
html: string;
|
|
54
|
+
/** The nonce of the inline scripts, for the Content Security Policy. */
|
|
55
|
+
nonce: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Neutral host: no Node or Bun imports and no server startup side effects. The core never
|
|
60
|
+
* searches files: override `resolvePage` and `resolveResources` in concrete integrations.
|
|
61
|
+
*/
|
|
62
|
+
export class Host {
|
|
63
|
+
/** URL of the runtime bundle. */
|
|
64
|
+
runtimeUrl: string;
|
|
65
|
+
/** URL of the `main` Source endpoint. */
|
|
66
|
+
mainUrl: string;
|
|
67
|
+
/** URL of the remote Source endpoint. */
|
|
68
|
+
sourceUrl: string;
|
|
69
|
+
/** URL of the page close endpoint. */
|
|
70
|
+
closeUrl: string;
|
|
71
|
+
/** Id of the root element of the page. */
|
|
72
|
+
rootId: string;
|
|
73
|
+
/** Seconds a registered page lives. */
|
|
74
|
+
pageTtl: number;
|
|
75
|
+
/** Maximum number of registered pages. */
|
|
76
|
+
maxPages: number;
|
|
77
|
+
/** The registered pages by id. */
|
|
78
|
+
pages: Map<string, {PageClass: typeof Page; owner: unknown; expires: number}>;
|
|
79
|
+
/** Create a host. */
|
|
80
|
+
constructor(options?: HostOptions);
|
|
81
|
+
/** The Page class of `path`, or `PageNotFound`. */
|
|
82
|
+
resolvePage(path: string): Promise<typeof Page>;
|
|
83
|
+
/** The resources of the page at `path` in load order and without mount prefix, or `PageNotFound`. */
|
|
84
|
+
resolveResources(path: string, PageClass: typeof Page): Promise<Resources>;
|
|
85
|
+
/** Drop the expired pages. */
|
|
86
|
+
prune(): void;
|
|
87
|
+
/** Register a page independently of its HTTP or Worker transport. */
|
|
88
|
+
registerPage(path: string, options?: {owner?: unknown}): Promise<RegisteredPage>;
|
|
89
|
+
/** Register the page and return its bootstrap; `prefix` is the mount prefix added to root-relative URLs. */
|
|
90
|
+
openPage(path: string, options?: {owner?: unknown; prefix?: string}): Promise<OpenedPage>;
|
|
91
|
+
/** The `main` Source of a registered page, as TYTX text. */
|
|
92
|
+
main(pageId: string, options?: {owner?: unknown}): Promise<string>;
|
|
93
|
+
/** A remote Source of a registered page, as TYTX text; `params` null or omitted is `{}`. */
|
|
94
|
+
source(pageId: string, method: string, params?: Record<string, unknown> | null,
|
|
95
|
+
options?: {owner?: unknown}): Promise<string>;
|
|
96
|
+
/** Build the Source of a page by calling its `main` or a registered Source method. */
|
|
97
|
+
buildSource(pageId: string, method: string | null, params: Record<string, unknown>,
|
|
98
|
+
owner: unknown): Promise<string>;
|
|
99
|
+
/** Forget a registered page when `owner` owns it. */
|
|
100
|
+
closePage(pageId: string, options?: {owner?: unknown}): void;
|
|
101
|
+
}
|
package/src/adapters/host.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/* @ts-self-types="./host.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* The neutral host: registers pages and builds their Sources, with no HTTP engine.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
1
7
|
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
2
8
|
import {Page, sourceMethod} from './page.js';
|
|
3
9
|
import {loadOrder} from './resources.js';
|
|
@@ -81,6 +87,9 @@ export class Host {
|
|
|
81
87
|
const head = root.head();
|
|
82
88
|
head.meta({charset: 'utf-8'});
|
|
83
89
|
head.title(title);
|
|
90
|
+
// A page module served for its Logic imports the core from the runtime already loaded.
|
|
91
|
+
head.script(scriptJson({imports: {'@gramlot/gramlot/page': prefixed(prefix, this.runtimeUrl)}}),
|
|
92
|
+
{type: 'importmap', nonce});
|
|
84
93
|
const body = root.body();
|
|
85
94
|
body.div({id: this.rootId});
|
|
86
95
|
body.script(`import {PageBootstrap} from ${scriptJson(prefixed(prefix, this.runtimeUrl))};` +
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server entry point of Gramlot: the host-side `Page` and `Host` classes, the file-based
|
|
3
|
+
* reference host and the resource name helpers. It is not part of the browser entry point.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export {Page, source} from './page.js';
|
|
8
|
+
export {Host, PageExpired, PageNotFound, SourceNotFound, HostCapacity} from './host.js';
|
|
9
|
+
export type {HostOptions, OpenedPage, RegisteredPage} from './host.js';
|
|
10
|
+
export {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
11
|
+
export {FileHost} from './file-host.js';
|
|
12
|
+
export {InvalidResourceName, parseRequires} from './resources.js';
|
|
13
|
+
export type {Resources} from './resources.js';
|
package/src/adapters/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/* @ts-self-types="./index.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* The server entry point of Gramlot: the host-side `Page` and `Host` classes, the file-based
|
|
4
|
+
* reference host and the resource name helpers. It is not part of the browser entry point.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
1
8
|
// Server entry point: deliberately not re-exported by the browser entry point.
|
|
2
9
|
export {Page, source} from './page.js';
|
|
3
10
|
export {Host, PageExpired, PageNotFound, SourceNotFound, HostCapacity} from './host.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-side page base class and the `source` registration.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Explicit registration of a remote Source method, equivalent to the Python `@source`.
|
|
10
|
+
* A Source method populates its `root` argument and returns nothing.
|
|
11
|
+
*/
|
|
12
|
+
export function source<T extends Function>(method: T): T;
|
|
13
|
+
|
|
14
|
+
/** The registered Source method `name` of `page`, or null. */
|
|
15
|
+
export function sourceMethod(page: Page, name: string): Function | null;
|
|
16
|
+
|
|
17
|
+
/** Host-side page base; unrelated to browser view components. */
|
|
18
|
+
export class Page {
|
|
19
|
+
/** The title of the page document. */
|
|
20
|
+
static title: string;
|
|
21
|
+
/** CSS URLs of the page. */
|
|
22
|
+
static css: string[];
|
|
23
|
+
/** Comma-separated names of the CSS resources of the page. */
|
|
24
|
+
static css_requires: string;
|
|
25
|
+
/** Comma-separated names of the JS resources of the page. */
|
|
26
|
+
static js_requires: string;
|
|
27
|
+
/** The builder class that authors the Source of the page. */
|
|
28
|
+
static sourceBuilder: typeof GramlotBuilder;
|
|
29
|
+
/** The id of the page, set by the host. */
|
|
30
|
+
pageId?: string;
|
|
31
|
+
/** Populate the `main` Source in `root`; subclasses must implement it. */
|
|
32
|
+
main(root: unknown): void | Promise<void>;
|
|
33
|
+
}
|
package/src/adapters/page.js
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource names and bootstrap load order; no file lookup and no HTTP engine.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** One segment of a resource or page name. */
|
|
8
|
+
export const SEGMENT: RegExp;
|
|
9
|
+
|
|
10
|
+
/** The Unicode White_Space characters stripped around resource names. */
|
|
11
|
+
export const SPACES: string;
|
|
12
|
+
|
|
13
|
+
/** A page declares its resources in an invalid way. */
|
|
14
|
+
export class InvalidResourceName extends Error {}
|
|
15
|
+
|
|
16
|
+
/** Parse `css_requires` or `js_requires`: comma-separated resource names, without duplicates. */
|
|
17
|
+
export function parseRequires(text: string): string[];
|
|
18
|
+
|
|
19
|
+
/** The CSS and JS resources of a page. */
|
|
20
|
+
export interface Resources {
|
|
21
|
+
/** CSS URLs. */
|
|
22
|
+
css: string[];
|
|
23
|
+
/** JS module URLs with their Logic group. */
|
|
24
|
+
js: {url: string; group: string | null}[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The resources with each URL once, in its last position; one JS URL with two groups raises. */
|
|
28
|
+
export function loadOrder(resources: Resources): Resources;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
/* @ts-self-types="./resources.d.ts" */
|
|
1
2
|
/** Resource names and bootstrap load order; no file lookup and no HTTP engine.
|
|
2
3
|
* The Python counterpart is src/gramlot/server/resources.py; both apply the same
|
|
3
4
|
* rules. css_requires/js_requires names are interpreted by a Host with a resource
|
|
4
5
|
* system, not by the core.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
5
8
|
*/
|
|
6
9
|
/** One segment of a resource or page name. */
|
|
7
10
|
export const SEGMENT = /^[A-Za-z0-9_-]+$/;
|
package/src/binding/inline.js
CHANGED
|
@@ -176,7 +176,7 @@ function create(node, attr, text, params) {
|
|
|
176
176
|
} catch (error) {
|
|
177
177
|
if (error instanceof EvalError) {
|
|
178
178
|
throw new EvalError(`${declaration(node, attr)}: inline code blocked by the Content Security Policy of the page `
|
|
179
|
-
+ `(no 'unsafe-eval'); move the code to named logic (a method of the page
|
|
179
|
+
+ `(no 'unsafe-eval'); move the code to named logic (a method of the page's class Logic) `
|
|
180
180
|
+ `or serve the page with the permissive CSP profile, which allows 'unsafe-eval'`, {cause: error});
|
|
181
181
|
}
|
|
182
182
|
throw new SyntaxError(`${declaration(node, attr)}: ${error.message} in the inline code:\n${text}`, {cause: error});
|
package/src/binding/logic.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Named logic of one Gramlot page (source plan §4.9, decision P21).
|
|
3
3
|
*
|
|
4
4
|
* Every logic file exports `class Logic`; its prototype methods are copied into a
|
|
5
|
-
* `LogicGroup`: the root group for the page
|
|
5
|
+
* `LogicGroup`: the root group for the page logic (group null), a child group
|
|
6
6
|
* for each name of `js_requires` (`a/b` is the group `b` inside the group `a`).
|
|
7
7
|
* The constructor of `Logic` never runs.
|
|
8
8
|
*/
|
|
@@ -178,7 +178,7 @@ export class LogicRegistry {
|
|
|
178
178
|
|
|
179
179
|
/**
|
|
180
180
|
* Copy the methods of `logicClass` into the group `group` (a `js_requires` name, `a/b` nested,
|
|
181
|
-
* or null for the page
|
|
181
|
+
* or null for the page logic), creating the groups on the way. On an equal method name in
|
|
182
182
|
* the same group the last registration wins.
|
|
183
183
|
*/
|
|
184
184
|
register(logicClass, {group = null, resource}) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser start of one page.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type {DomDocument} from './dom.d.ts';
|
|
7
|
+
import type {Gramlot, GramlotOptions} from './gramlot.js';
|
|
8
|
+
|
|
9
|
+
/** The JavaScript modules and CSS links of a page, in load order and with the mount prefix. */
|
|
10
|
+
export interface PageResources {
|
|
11
|
+
/** CSS stylesheet URLs. */
|
|
12
|
+
css: string[];
|
|
13
|
+
/** JavaScript module URLs, each with its Logic group (null for the page logic). */
|
|
14
|
+
js: {url: string; group: string | null}[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The configuration written by the host into the bootstrap HTML. */
|
|
18
|
+
export interface PageBootstrapOptions {
|
|
19
|
+
/** The options of the `Gramlot` instance, with the page id. */
|
|
20
|
+
config: GramlotOptions;
|
|
21
|
+
/** The resources of the page. */
|
|
22
|
+
resources: PageResources;
|
|
23
|
+
/** The document of the page. */
|
|
24
|
+
document?: DomDocument;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The browser start of one page. The host writes
|
|
29
|
+
* `await new PageBootstrap({config, resources}).run()` into the bootstrap HTML.
|
|
30
|
+
*/
|
|
31
|
+
export class PageBootstrap {
|
|
32
|
+
/** The configuration of the page. */
|
|
33
|
+
config: GramlotOptions;
|
|
34
|
+
/** The resources of the page. */
|
|
35
|
+
resources: PageResources;
|
|
36
|
+
/** The document of the page. */
|
|
37
|
+
document: DomDocument;
|
|
38
|
+
/** Create the bootstrap of one page. */
|
|
39
|
+
constructor(options: PageBootstrapOptions);
|
|
40
|
+
/**
|
|
41
|
+
* Add the CSS links, import all JS modules, check the Logic classes, create the `Gramlot`
|
|
42
|
+
* instance, register the logic and start it. Resolves with the instance, or with null when the
|
|
43
|
+
* page closed during the imports.
|
|
44
|
+
*/
|
|
45
|
+
run(): Promise<Gramlot | null>;
|
|
46
|
+
/** Send the close request of a page that never got its `Gramlot` instance. */
|
|
47
|
+
closePage(): void;
|
|
48
|
+
}
|
package/src/bootstrap.js
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Gramlot vocabulary and inert authoring on the shared builder grammar.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import {HtmlBuilder} from '@genrojs/builders';
|
|
7
|
+
import type {SourceAttributes, SourceBag, SourceBagNode, ValidateNodeOptions} from '@genrojs/builders';
|
|
8
|
+
import type {GramlotHtmlRenderer} from '../renderer/gramlot-html-renderer.js';
|
|
9
|
+
import type {SourceReference} from '../references.js';
|
|
10
|
+
|
|
11
|
+
/** Gramlot vocabulary and inert authoring on the shared builder grammar. */
|
|
12
|
+
export class GramlotBuilder extends HtmlBuilder {
|
|
13
|
+
/** The dialect name, `'gramlot'`. */
|
|
14
|
+
static _name: string;
|
|
15
|
+
/** Tag prefixes of the parent dialects, from the names of the classes above `GramlotBuilder`. */
|
|
16
|
+
static get dialectPrefixes(): string[];
|
|
17
|
+
/** Create a builder; `collections` are grammar documents loaded into it. */
|
|
18
|
+
constructor(name?: string | null, options?: {collections?: unknown[]});
|
|
19
|
+
/** The binding runtime of the Gramlot page that owns this builder, or null for authoring alone; set by `Gramlot` right after creating the builder. */
|
|
20
|
+
binding: unknown;
|
|
21
|
+
/** The node with `nodeId`; inside a Gramlot page it reads the node ids of the binding runtime. */
|
|
22
|
+
nodeById(nodeId: string): SourceBagNode;
|
|
23
|
+
/** Load a grammar document into the builder. */
|
|
24
|
+
loadCollection(document: unknown): this;
|
|
25
|
+
/** The schema tag of an authored name, or null; the `gramlot_` prefix and excluded declarations raise. */
|
|
26
|
+
schemaTag(name: string): string | null;
|
|
27
|
+
/** An element call from the proxy on a Source Bag. */
|
|
28
|
+
bagCall(bag: SourceBag, tag: string, value?: unknown, attrs?: SourceAttributes): SourceBagNode;
|
|
29
|
+
/** An element call from the proxy on a Source node. */
|
|
30
|
+
commandOnNode(node: SourceBagNode, tag: string, value?: unknown, attrs?: SourceAttributes): SourceBagNode;
|
|
31
|
+
/** Check the value of a node, rejecting the excluded attributes. */
|
|
32
|
+
validateNodeValues(tag: string, value: unknown, attrs?: SourceAttributes): true;
|
|
33
|
+
/** Check a Source node, rejecting the excluded attributes. */
|
|
34
|
+
validateNode(node: SourceBagNode, options?: ValidateNodeOptions): true;
|
|
35
|
+
/** Insert a child; a plain object value of `dataSetter` becomes a Bag. */
|
|
36
|
+
setChild(bag: SourceBag, tag: string, value: unknown, attrs: SourceAttributes,
|
|
37
|
+
parentOverride?: SourceBagNode | null): SourceBagNode;
|
|
38
|
+
/** The string renderer of the Gramlot Source: static render, authoring stays inert. */
|
|
39
|
+
readonly renderer_html: GramlotHtmlRenderer;
|
|
40
|
+
/** Run `setup` on the Data and `main` on the Source root. */
|
|
41
|
+
create(): void;
|
|
42
|
+
/** Browser declarations are not executed by server authoring. */
|
|
43
|
+
computeLogic(): void;
|
|
44
|
+
/** Keep the mixed leading text when a scalar element gains children. */
|
|
45
|
+
promoteNodeValue(node: SourceBagNode, oldValue: unknown, branch: SourceBag): void;
|
|
46
|
+
/** Create a transportable reference descriptor for an authored node. */
|
|
47
|
+
reference(node: SourceBagNode, kind?: 'node' | 'dom'): SourceReference;
|
|
48
|
+
/** The Source encoded as TYTX text. */
|
|
49
|
+
toTytx(): string;
|
|
50
|
+
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/* @ts-self-types="./gramlot-builder.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* The Gramlot vocabulary and inert authoring on the shared builder grammar.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
1
7
|
import {Bag} from '@genrojs/bag';
|
|
2
8
|
import {HtmlBuilder, SourceBagNode, sourceTarget} from '@genrojs/builders';
|
|
3
9
|
import {toTytx} from '@genrojs/tytx';
|
package/src/dom.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loose names for the browser DOM objects that Gramlot receives and returns. A JSR package may not
|
|
3
|
+
* reference the `dom` library, so these are typed as `any` here; in a browser they are the objects
|
|
4
|
+
* of the same name.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// deno-lint-ignore no-explicit-any
|
|
10
|
+
/** A browser `Document`. */
|
|
11
|
+
export type DomDocument = any;
|
|
12
|
+
// deno-lint-ignore no-explicit-any
|
|
13
|
+
/** A browser `Element`. */
|
|
14
|
+
export type DomElement = any;
|
|
15
|
+
// deno-lint-ignore no-explicit-any
|
|
16
|
+
/** A browser `Node`. */
|
|
17
|
+
export type DomNode = any;
|
|
18
|
+
// deno-lint-ignore no-explicit-any
|
|
19
|
+
/** A browser `DocumentFragment`. */
|
|
20
|
+
export type DomFragment = any;
|
|
21
|
+
/** The part of a browser `Navigator` that sends the close beacon. */
|
|
22
|
+
export interface BeaconNavigator {
|
|
23
|
+
/** Queue `data` for delivery to `url`; returns false when the browser refuses it. */
|
|
24
|
+
sendBeacon(url: string, data?: unknown): boolean;
|
|
25
|
+
}
|
package/src/gramlot.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One Gramlot page in the browser.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type {DomElement, DomDocument, DomNode} from './dom.d.ts';
|
|
7
|
+
import type {Bag} from '@genrojs/bag';
|
|
8
|
+
import type {SourceBag, SourceBagNode} from '@genrojs/builders';
|
|
9
|
+
import type {GramlotBuilder} from './builder/gramlot-builder.js';
|
|
10
|
+
import type {GramlotRenderer} from './renderer/gramlot-renderer.js';
|
|
11
|
+
import type {InOut} from './inout.js';
|
|
12
|
+
import type {SourceReference} from './references.js';
|
|
13
|
+
import type {MainTransport} from './transport.js';
|
|
14
|
+
|
|
15
|
+
/** The options of a `Gramlot` instance. */
|
|
16
|
+
export interface GramlotOptions {
|
|
17
|
+
/** The id of the server page, as registered by the host. */
|
|
18
|
+
pageId?: string;
|
|
19
|
+
/** The URL of the `main` Source endpoint. */
|
|
20
|
+
mainUrl?: string;
|
|
21
|
+
/** The URL of the remote Source endpoint. */
|
|
22
|
+
sourceUrl?: string;
|
|
23
|
+
/** The URL of the page close endpoint. */
|
|
24
|
+
closeUrl?: string;
|
|
25
|
+
/** The id of the root element that receives the page. */
|
|
26
|
+
rootId?: string;
|
|
27
|
+
/** The root element; when null it is looked up by `rootId`. */
|
|
28
|
+
element?: DomElement | null;
|
|
29
|
+
/** The document of the page. */
|
|
30
|
+
document?: DomDocument;
|
|
31
|
+
/** A transport, or `false` for an instance with no server transport. */
|
|
32
|
+
transport?: MainTransport | false | null;
|
|
33
|
+
/** Grammar collections loaded into the builder. */
|
|
34
|
+
collections?: unknown[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The lifecycle state of a `Gramlot` instance. */
|
|
38
|
+
export type GramlotState = 'ready' | 'loading' | 'started' | 'failed' | 'disposed';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* One Gramlot page. The Data root is the builder's, the binding runtime subscribes to it before
|
|
42
|
+
* the renderer observes the Source.
|
|
43
|
+
*/
|
|
44
|
+
export class Gramlot {
|
|
45
|
+
/** The id of the server page. */
|
|
46
|
+
pageId: string | undefined;
|
|
47
|
+
/** The registry of the Logic classes of the page. */
|
|
48
|
+
logicRegistry: unknown;
|
|
49
|
+
/** The builder that owns the Source and the Data. */
|
|
50
|
+
builder: GramlotBuilder;
|
|
51
|
+
/** The binding runtime of the page. */
|
|
52
|
+
binding: unknown;
|
|
53
|
+
/** The Data Bag of the page. */
|
|
54
|
+
data: Bag;
|
|
55
|
+
/** The `gramlot.inout` object: send, receive, save and download Data branches. */
|
|
56
|
+
inout: InOut;
|
|
57
|
+
/** The Source Bag observed by the renderer. */
|
|
58
|
+
source: SourceBag;
|
|
59
|
+
/** The live renderer of the page. */
|
|
60
|
+
renderer: GramlotRenderer;
|
|
61
|
+
/** The server transport, or null. */
|
|
62
|
+
transport: MainTransport | null;
|
|
63
|
+
/** The remote Source requests in progress, by target node. */
|
|
64
|
+
remoteRequests: Map<SourceBagNode, {controller: AbortController}>;
|
|
65
|
+
/** The lifecycle state. */
|
|
66
|
+
state: GramlotState;
|
|
67
|
+
/** The controller aborted on dispose. */
|
|
68
|
+
abort: AbortController;
|
|
69
|
+
/** The pending `start` promise, while loading. */
|
|
70
|
+
loading?: Promise<Gramlot> | null;
|
|
71
|
+
/** Create the page instance; the construction order is the one of the source plan. */
|
|
72
|
+
constructor(options?: GramlotOptions);
|
|
73
|
+
/** Load `main` from the server and mount it; resolves with the instance. */
|
|
74
|
+
start(): Promise<Gramlot>;
|
|
75
|
+
/** Request `main` from the transport and mount it. */
|
|
76
|
+
loadMain(): Promise<Gramlot>;
|
|
77
|
+
/** Start from an embedded typed Source, with the same observed insertion as `main`. */
|
|
78
|
+
startSource(wire: SourceBag | string): Gramlot;
|
|
79
|
+
/** Validate and insert `main` in the Source. */
|
|
80
|
+
mountMainSource(wire: SourceBag | string): Gramlot;
|
|
81
|
+
/**
|
|
82
|
+
* A Source from a SourceBag or a TYTX wire; only Gramlot Source Bags are accepted, before any
|
|
83
|
+
* effect. The received Source activates its inline code.
|
|
84
|
+
*/
|
|
85
|
+
prepareSource(wire: SourceBag | string, builder?: GramlotBuilder): SourceBag;
|
|
86
|
+
/**
|
|
87
|
+
* Replace the children of one Source node with a remote Source, only after the incoming Source
|
|
88
|
+
* validates. Resolves with true when applied, false when superseded or cancelled.
|
|
89
|
+
*/
|
|
90
|
+
remoteSource(target: SourceBagNode, method: string, params?: Record<string, unknown>): Promise<boolean>;
|
|
91
|
+
/** Resolve a node or DOM reference descriptor. */
|
|
92
|
+
reference(descriptor: SourceReference): SourceBagNode | DomElement;
|
|
93
|
+
/** The Source node of the nearest element generated by the renderer. */
|
|
94
|
+
getBaseSourceNode(domNode: DomNode): SourceBagNode | null;
|
|
95
|
+
/** The element built from a Source node, or null. */
|
|
96
|
+
getDomNode(sourceNode: SourceBagNode): DomElement | null;
|
|
97
|
+
/** Dispose the page: abort requests, close bindings and the renderer, and close the server page. */
|
|
98
|
+
dispose(options?: {beacon?: boolean}): void;
|
|
99
|
+
}
|
package/src/gramlot.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/* @ts-self-types="./gramlot.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* One Gramlot page in the browser.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
1
7
|
import {SourceBag, sourceBagFromTytx} from '@genrojs/builders';
|
|
2
8
|
import {GramlotBuilder} from './builder/gramlot-builder.js';
|
|
3
9
|
import {GramlotBuilderBag} from './builder/source.js';
|
|
@@ -5,6 +11,7 @@ import {GramlotRenderer} from './renderer/gramlot-renderer.js';
|
|
|
5
11
|
import {BindingRuntime} from './binding/runtime.js';
|
|
6
12
|
import {LogicRegistry} from './binding/logic.js';
|
|
7
13
|
import {MainTransport} from './transport.js';
|
|
14
|
+
import {InOut} from './inout.js';
|
|
8
15
|
|
|
9
16
|
/**
|
|
10
17
|
* One Gramlot page. The construction order is the one of source plan §4.3: the Data
|
|
@@ -20,6 +27,7 @@ export class Gramlot {
|
|
|
20
27
|
this.binding = new BindingRuntime(this);
|
|
21
28
|
this.builder.binding = this.binding;
|
|
22
29
|
this.data = this.builder.data;
|
|
30
|
+
this.inout = new InOut(this, document);
|
|
23
31
|
this.binding.attach();
|
|
24
32
|
this.source = this.builder.source;
|
|
25
33
|
const destination = element ?? document.getElementById(rootId);
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser entry point of Gramlot: declarative HTML and SVG interfaces with live data binding.
|
|
3
|
+
* It exports the page instance (`Gramlot`), the bootstrap that starts it, the builder and the
|
|
4
|
+
* renderers, the transport and `gramlot.inout`, and re-exports `Bag` and `BagNode` from the Bag library.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
export {Gramlot} from './gramlot.js';
|
|
9
|
+
export type {GramlotOptions, GramlotState} from './gramlot.js';
|
|
10
|
+
export {PageBootstrap} from './bootstrap.js';
|
|
11
|
+
export type {PageBootstrapOptions, PageResources} from './bootstrap.js';
|
|
12
|
+
export {Page, source} from './adapters/page.js';
|
|
13
|
+
export {GramlotBuilder} from './builder/gramlot-builder.js';
|
|
14
|
+
export {GramlotRenderer} from './renderer/gramlot-renderer.js';
|
|
15
|
+
export {GramlotHtmlRenderer} from './renderer/gramlot-html-renderer.js';
|
|
16
|
+
export {GramlotSvgRenderer} from './renderer/gramlot-svg-renderer.js';
|
|
17
|
+
export {Bag, BagNode} from '@genrojs/bag';
|
|
18
|
+
export {References} from './references.js';
|
|
19
|
+
export type {SourceReference} from './references.js';
|
|
20
|
+
export {MainTransport} from './transport.js';
|
|
21
|
+
export type {Fetcher} from './transport.js';
|
|
22
|
+
export {InOut} from './inout.js';
|
|
23
|
+
export type {BeaconNavigator, DomDocument, DomElement, DomFragment, DomNode} from './dom.d.ts';
|