trebol 0.1.1 → 0.1.2

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.
Files changed (84) hide show
  1. package/README.md +10 -1
  2. package/dist/cli.js +13747 -11409
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +2 -1
  12. package/dist/lib/core/func.d.ts +4 -0
  13. package/dist/lib/core/natives/control.js +1 -1
  14. package/dist/lib/core/natives/series.js +11 -1
  15. package/dist/lib/core/natives/text.js +3 -0
  16. package/dist/lib/host/browser.js +951 -58
  17. package/dist/lib/host/devlog.d.ts +11 -0
  18. package/dist/lib/host/devlog.js +13 -0
  19. package/dist/lib/host/devtools.d.ts +23 -0
  20. package/dist/lib/host/devtools.js +626 -0
  21. package/dist/lib/host/fonts/OFL.txt +93 -0
  22. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  23. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  24. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  25. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  26. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  27. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  28. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  29. package/dist/lib/host/fonts.d.ts +11 -0
  30. package/dist/lib/host/fonts.js +29 -0
  31. package/dist/lib/host/index.d.ts +2 -0
  32. package/dist/lib/host/index.js +3 -1
  33. package/dist/lib/host/js.d.ts +5 -0
  34. package/dist/lib/host/js.js +130 -0
  35. package/dist/lib/host/markdown.d.ts +2 -0
  36. package/dist/lib/host/markdown.js +10 -0
  37. package/dist/lib/host/modules.d.ts +4 -0
  38. package/dist/lib/host/modules.js +39 -1
  39. package/dist/lib/host/net.js +9 -1
  40. package/dist/lib/host/orbit.d.ts +121 -0
  41. package/dist/lib/host/orbit.js +374 -0
  42. package/dist/lib/host/rows.d.ts +14 -0
  43. package/dist/lib/host/rows.js +29 -0
  44. package/dist/lib/host/scheduler.d.ts +6 -0
  45. package/dist/lib/host/scheduler.js +60 -36
  46. package/dist/lib/host/theme.js +207 -2
  47. package/dist/lib/ui/i18n.trb +6 -0
  48. package/dist/lib/ui/test.trb +565 -0
  49. package/dist/lib/ui/ui.trb +523 -82
  50. package/dist/trebol.js +1346 -124
  51. package/editors/vscode/README.md +15 -0
  52. package/editors/vscode/language-configuration.json +28 -0
  53. package/editors/vscode/package.json +42 -0
  54. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  55. package/package.json +7 -3
  56. package/src/core/boot.ts +1 -1
  57. package/src/core/func.ts +4 -0
  58. package/src/core/natives/control.ts +1 -1
  59. package/src/core/natives/series.ts +9 -1
  60. package/src/core/natives/text.ts +2 -0
  61. package/src/host/browser.ts +962 -51
  62. package/src/host/devlog.ts +16 -0
  63. package/src/host/devtools.ts +641 -0
  64. package/src/host/fonts/OFL.txt +93 -0
  65. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  66. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  67. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  68. package/src/host/fonts/inter-greek.woff2 +0 -0
  69. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  70. package/src/host/fonts/inter-latin.woff2 +0 -0
  71. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  72. package/src/host/fonts.ts +39 -0
  73. package/src/host/index.ts +3 -1
  74. package/src/host/js.ts +127 -0
  75. package/src/host/markdown.ts +9 -0
  76. package/src/host/modules.ts +33 -1
  77. package/src/host/net.ts +9 -1
  78. package/src/host/orbit.ts +432 -0
  79. package/src/host/rows.ts +30 -0
  80. package/src/host/scheduler.ts +62 -36
  81. package/src/host/theme.ts +207 -2
  82. package/src/ui/i18n.trb +6 -0
  83. package/src/ui/test.trb +565 -0
  84. package/src/ui/ui.trb +523 -82
@@ -0,0 +1,93 @@
1
+ Copyright 2016 The Inter Project Authors (https://github.com/rsms/inter) Inter-Italic[opsz,wght].ttf: Copyright 2016 The Inter Project Authors (https://github.com/rsms/inter)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ http://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,11 @@
1
+ export interface FontFile {
2
+ /** File name in fonts/. */
3
+ file: string;
4
+ /** The characters the file has, as CSS unicode-range. */
5
+ range: string;
6
+ }
7
+ export declare const INTER: readonly FontFile[];
8
+ /** The @font-face rules of Inter, each file at the address `src` gives it. */
9
+ export declare function interCss(src: (file: string) => string, faces?: readonly FontFile[]): string;
10
+ /** Whether a code point is in a unicode-range. */
11
+ export declare function inRange(range: string, cp: number): boolean;
@@ -0,0 +1,29 @@
1
+ // Inter, the font of the default theme, as variable woff2 files in fonts/:
2
+ // one per script, so a browser downloads only those its text needs (the
3
+ // unicode-range of each face). They come from @fontsource-variable/inter
4
+ // 5.3.0, under the SIL Open Font License (fonts/OFL.txt).
5
+ //
6
+ // trebol.js declares them with addresses next to itself; a page made by
7
+ // `trebol build` carries the files inside and declares them in its own
8
+ // <style data-tb-fonts>, which the host then leaves alone.
9
+ // The order of @fontsource: where ranges overlap, the last face declared wins.
10
+ export const INTER = [
11
+ { file: 'inter-cyrillic-ext.woff2', range: 'U+0460-052F,U+1C80-1C8A,U+20B4,U+2DE0-2DFF,U+A640-A69F,U+FE2E-FE2F' },
12
+ { file: 'inter-cyrillic.woff2', range: 'U+0301,U+0400-045F,U+0490-0491,U+04B0-04B1,U+2116' },
13
+ { file: 'inter-greek-ext.woff2', range: 'U+1F00-1FFF' },
14
+ { file: 'inter-greek.woff2', range: 'U+0370-0377,U+037A-037F,U+0384-038A,U+038C,U+038E-03A1,U+03A3-03FF' },
15
+ { file: 'inter-vietnamese.woff2', range: 'U+0102-0103,U+0110-0111,U+0128-0129,U+0168-0169,U+01A0-01A1,U+01AF-01B0,U+0300-0301,U+0303-0304,U+0308-0309,U+0323,U+0329,U+1EA0-1EF9,U+20AB' },
16
+ { file: 'inter-latin-ext.woff2', range: 'U+0100-02BA,U+02BD-02C5,U+02C7-02CC,U+02CE-02D7,U+02DD-02FF,U+0304,U+0308,U+0329,U+1D00-1DBF,U+1E00-1E9F,U+1EF2-1EFF,U+2020,U+20A0-20AB,U+20AD-20C0,U+2113,U+2C60-2C7F,U+A720-A7FF' },
17
+ { file: 'inter-latin.woff2', range: 'U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD' },
18
+ ];
19
+ /** The @font-face rules of Inter, each file at the address `src` gives it. */
20
+ export function interCss(src, faces = INTER) {
21
+ return faces.map(f => `@font-face { font-family: Inter; font-style: normal; font-weight: 100 900; font-display: swap; src: url(${src(f.file)}) format("woff2"); unicode-range: ${f.range}; }`).join('\n');
22
+ }
23
+ /** Whether a code point is in a unicode-range. */
24
+ export function inRange(range, cp) {
25
+ return range.split(',').some(r => {
26
+ const [lo, hi] = r.slice(2).split('-').map(h => parseInt(h, 16));
27
+ return cp >= lo && cp <= (hi ?? lo);
28
+ });
29
+ }
@@ -29,6 +29,8 @@ export declare function startHost(opts?: HostOptions): Host;
29
29
  export declare const UI_SOURCE: string;
30
30
  /** URL of the languages module (i18n, tr, translated). */
31
31
  export declare const I18N_SOURCE: string;
32
+ /** URL of the words of screen tests (click, type, see...), which `trebol test` loads. */
33
+ export declare const TEST_SOURCE: string;
32
34
  /** Loads languages and the UI dialect (view, layout, stylize, theme...) into the user context. */
33
35
  export declare function startUi(host: Host): Promise<Cell>;
34
36
  /**
@@ -173,8 +173,10 @@ export function startHost(opts = {}) {
173
173
  export const UI_SOURCE = new URL('../ui/ui.trb', import.meta.url).href;
174
174
  /** URL of the languages module (i18n, tr, translated). */
175
175
  export const I18N_SOURCE = new URL('../ui/i18n.trb', import.meta.url).href;
176
+ /** URL of the words of screen tests (click, type, see...), which `trebol test` loads. */
177
+ export const TEST_SOURCE = new URL('../ui/test.trb', import.meta.url).href;
176
178
  // Trebol's own code: its errors point at the program's call (see LIBRARY_FILES).
177
- LIBRARY_FILES.add('host').add(UI_SOURCE).add(I18N_SOURCE);
179
+ LIBRARY_FILES.add('host').add(UI_SOURCE).add(I18N_SOURCE).add(TEST_SOURCE);
178
180
  /** Loads languages and the UI dialect (view, layout, stylize, theme...) into the user context. */
179
181
  export function startUi(host) {
180
182
  return runSource(host, `import ${I18N_SOURCE} import ${UI_SOURCE}`);
@@ -0,0 +1,5 @@
1
+ import { type ObjectCell } from '../core/index.ts';
2
+ /** countPages is count-pages, as Trebol names words. */
3
+ export declare function trebolName(js: string): string;
4
+ /** The object for a loaded JavaScript module, or an error message. */
5
+ export declare function jsModule(ns: Record<string, unknown>): ObjectCell | string;
@@ -0,0 +1,130 @@
1
+ // JavaScript modules as Trebol modules.
2
+ //
3
+ // `import %pdf-tools.js` loads a JavaScript module and gives an object with a
4
+ // function for each function it exports, named in Trebol's way (countPages is
5
+ // count-pages), and the other exports as values:
6
+ //
7
+ // pdf: import %pdf-tools.js
8
+ // pages: pdf/count-pages %contract.pdf
9
+ //
10
+ // Arguments and results cross as JSON, so text, numbers, blocks, maps, logic
11
+ // and none keep their shape; a promise is awaited while other tasks run, and
12
+ // a JavaScript exception is a Trebol error. A function's spec comes from its
13
+ // parameters, or from a `spec` property written as Trebol source:
14
+ //
15
+ // export function countPages(file) { ... }
16
+ // countPages.spec = '["The pages of a PDF." file [file! url!]]';
17
+ //
18
+ // No Trebol value holds the JavaScript function: each is a native built on one
19
+ // hidden native, carrying the function's index in a table here as its data.
20
+ import { K, NONE, registerNative, load, parseSpec, parseJson, toJson, context, ctxAdd, intern, makeError, value, throwError, } from "../core/index.js";
21
+ const functions = [];
22
+ const call = registerNative({
23
+ name: 'js-function', hidden: true, spec: parseSpec(load('[]').s.data[0]),
24
+ specBlock: load('[]').s.data[0],
25
+ }, (_t, f, v) => {
26
+ if (f.phase === 1)
27
+ return value(v);
28
+ const fn = functions[f.def.data];
29
+ const name = f.def.name;
30
+ let result;
31
+ try {
32
+ result = fn(...f.args.map(a => JSON.parse(toJson(a))));
33
+ }
34
+ catch (e) {
35
+ return throwError('script', 'js-error', `${name}: ${e?.message ?? String(e)}`);
36
+ }
37
+ if (!(result instanceof Promise))
38
+ return toTrebol(result, name);
39
+ f.phase = 1;
40
+ return {
41
+ k: 'await',
42
+ p: result.then(r => {
43
+ const step = toTrebol(r, name);
44
+ if (step.k === 'value')
45
+ return step.v;
46
+ throw step.e.v;
47
+ }, (e) => {
48
+ throw makeError('script', 'js-error', `${name}: ${e?.message ?? String(e)}`);
49
+ }),
50
+ };
51
+ }).p;
52
+ function toTrebol(r, name) {
53
+ if (r === undefined)
54
+ return value(NONE);
55
+ let json;
56
+ try {
57
+ json = JSON.stringify(r);
58
+ }
59
+ catch {
60
+ json = undefined;
61
+ }
62
+ if (json === undefined)
63
+ return throwError('script', 'js-error', `${name} gave a value that is not JSON`);
64
+ const v = parseJson(json);
65
+ return typeof v === 'string' ? throwError('script', 'js-error', `${name}: ${v}`) : value(v);
66
+ }
67
+ /** countPages is count-pages, as Trebol names words. */
68
+ export function trebolName(js) {
69
+ return js.replace(/([a-z0-9])([A-Z])/g, '$1-$2').replace(/_/g, '-').toLowerCase();
70
+ }
71
+ /** The parameter names of a plain function or an arrow, or none when they cannot be read. */
72
+ function params(fn) {
73
+ const src = fn.toString();
74
+ const m = /^\s*(?:async\s+)?([\w$]+)\s*=>/.exec(src) ?? /^[^(=]*\(([^)]*)\)/.exec(src);
75
+ if (m === null)
76
+ return null;
77
+ const names = m[1].split(',').map(p => p.trim()).filter(p => p !== '');
78
+ return names.every(p => /^[A-Za-z_$][\w$]*$/.test(p)) ? names.map(trebolName) : null;
79
+ }
80
+ function specOf(fn, name) {
81
+ const own = fn.spec;
82
+ const src = typeof own === 'string' ? own
83
+ : `[${(params(fn) ?? Array.from({ length: fn.length }, (_, i) => `arg${i + 1}`)).join(' ')}]`;
84
+ let specBlock;
85
+ try {
86
+ specBlock = load(src).s.data[0];
87
+ }
88
+ catch (e) {
89
+ return `${name}: bad spec: ${e.message}`;
90
+ }
91
+ if (specBlock?.t !== K.Block)
92
+ return `${name}: its spec is not a block`;
93
+ const spec = parseSpec(specBlock);
94
+ return typeof spec === 'string' ? `${name}: ${spec}` : { spec, specBlock };
95
+ }
96
+ /** The object for a loaded JavaScript module, or an error message. */
97
+ export function jsModule(ns) {
98
+ const ctx = context();
99
+ ctx.self = true;
100
+ for (const [key, x] of Object.entries(ns)) {
101
+ if (key === 'default')
102
+ continue;
103
+ const name = trebolName(key);
104
+ let v;
105
+ if (typeof x === 'function') {
106
+ const spec = specOf(x, name);
107
+ if (typeof spec === 'string')
108
+ return spec;
109
+ functions.push(x);
110
+ v = { t: K.Native, p: { ...call, ...spec, name, data: functions.length - 1, hidden: true } };
111
+ }
112
+ else {
113
+ let json;
114
+ try {
115
+ json = JSON.stringify(x);
116
+ }
117
+ catch {
118
+ json = undefined;
119
+ }
120
+ if (json === undefined)
121
+ continue;
122
+ const parsed = parseJson(json);
123
+ if (typeof parsed === 'string')
124
+ continue;
125
+ v = parsed;
126
+ }
127
+ ctxAdd(ctx, intern(name), v);
128
+ }
129
+ return { t: K.Object, ctx };
130
+ }
@@ -24,5 +24,7 @@ export type Block = {
24
24
  };
25
25
  export declare function parseMd(src: string): Block[];
26
26
  export declare function printMd(blocks: Block[]): string;
27
+ /** Markdown with plain text added at its end, in its last paragraph or a new one after a heading or list. */
28
+ export declare function appendText(src: string, text: string): string;
27
29
  /** Only web and mail links open; anything else (javascript:...) is shown as plain text. */
28
30
  export declare function safeHref(href: string): boolean;
@@ -145,6 +145,16 @@ export function printMd(blocks) {
145
145
  }
146
146
  }).join('\n\n');
147
147
  }
148
+ /** Markdown with plain text added at its end, in its last paragraph or a new one after a heading or list. */
149
+ export function appendText(src, text) {
150
+ const blocks = parseMd(src);
151
+ const last = blocks[blocks.length - 1];
152
+ if (last?.k === 'p')
153
+ last.kids.push({ k: 'text', text });
154
+ else
155
+ blocks.push({ k: 'p', kids: [{ k: 'text', text }] });
156
+ return printMd(blocks);
157
+ }
148
158
  /** Only web and mail links open; anything else (javascript:...) is shown as plain text. */
149
159
  export function safeHref(href) {
150
160
  return /^(https?:|mailto:)/i.test(href.trim());
@@ -4,7 +4,11 @@ export interface Loader {
4
4
  base: string;
5
5
  /** Reads a module's source by its resolved URL. */
6
6
  read: (href: string) => Promise<string>;
7
+ /** Loads a JavaScript module by its resolved URL; the platform's import when not given. */
8
+ importJs?: (href: string) => Promise<Record<string, unknown>>;
7
9
  }
8
10
  export declare function setLoader(rt: Runtime, loader: Loader): void;
9
11
  export declare function defaultLoader(): Loader;
10
12
  export declare function addBuiltIn(href: string, src: string): void;
13
+ /** The source built in under a URL, or undefined. */
14
+ export declare function builtInText(href: string): string | undefined;
@@ -9,11 +9,22 @@
9
9
  // Trebol [exports: [load-clients]]
10
10
  // export save-client: func [...] [...]
11
11
  //
12
+ // A `.js` file is a JavaScript module instead (js.ts).
13
+ //
12
14
  // `do %file` runs a script in the user context each time; `read` gives a
13
15
  // file's text and `load %file` its values.
14
16
  //
15
17
  // Local storage keeps values molded with mold/all, so they load back exact.
18
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
19
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
20
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
21
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
22
+ });
23
+ }
24
+ return path;
25
+ };
16
26
  import { K, NONE, UNSET, native, value, text, load, mold, string, context, collectSetWords, bindSeries, bindSelf, copyDeep, setWord, getWord, splice, currentFile, throwError, makeError, scriptStart, LoadError, T, canon, intern, } from "../core/index.js";
27
+ import { jsModule } from "./js.js";
17
28
  const loaders = new WeakMap();
18
29
  const modules = new WeakMap();
19
30
  export function setLoader(rt, loader) {
@@ -88,6 +99,10 @@ const builtIn = new Map();
88
99
  export function addBuiltIn(href, src) {
89
100
  builtIn.set(href, src);
90
101
  }
102
+ /** The source built in under a URL, or undefined. */
103
+ export function builtInText(href) {
104
+ return builtIn.get(href);
105
+ }
91
106
  function readText(loader, href, path) {
92
107
  const src = builtIn.get(href);
93
108
  if (src !== undefined)
@@ -160,6 +175,29 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
160
175
  const entry = { obj: null, done, settle };
161
176
  mods.set(href, entry);
162
177
  f.data = { href, entry, ctx: null, exports: [] };
178
+ if (/\.m?js$/i.test(new URL(href).pathname)) {
179
+ f.phase = 4;
180
+ const load = loader.importJs ?? ((h) => import(__rewriteRelativeImportExtension(/* @vite-ignore */ h)));
181
+ return {
182
+ k: 'await',
183
+ p: load(href).then(ns => {
184
+ const obj = jsModule(ns);
185
+ if (typeof obj === 'string')
186
+ throw makeError('script', 'bad-module', obj);
187
+ return obj;
188
+ }, (e) => {
189
+ throw makeError('access', 'cannot-open', `cannot open ${mold(f.args[0])}: ${e?.message ?? String(e)}`);
190
+ }).then(obj => {
191
+ entry.obj = obj;
192
+ entry.settle(true, obj);
193
+ return obj;
194
+ }, (err) => {
195
+ mods.delete(href);
196
+ entry.settle(false, err);
197
+ throw err;
198
+ }),
199
+ };
200
+ }
163
201
  f.phase = 1;
164
202
  return {
165
203
  k: 'await',
@@ -170,7 +208,7 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
170
208
  }),
171
209
  };
172
210
  }
173
- if (f.phase === 3)
211
+ if (f.phase === 3 || f.phase === 4)
174
212
  return value(v);
175
213
  const st = f.data;
176
214
  const failed = (e) => {
@@ -7,6 +7,7 @@
7
7
  import { K, NONE, I64, native, value, string, stringOf, text, binary, makeError, throwError, form, spell, ctxFind, intern, parseJson, toJson, queryText, } from "../core/index.js";
8
8
  import { schedulerOf, spawn, spawnHandler, makeHandle, handleOf } from "./scheduler.js";
9
9
  import { cancellers } from "./natives.js";
10
+ import { devLog } from "./devlog.js";
10
11
  // --- files -----------------------------------------------------------------------
11
12
  //
12
13
  // A file the user chose (an upload field) is a handle of kind 'file' over a
@@ -155,7 +156,8 @@ function decodeBody(type, bytes) {
155
156
  const r = parseJson(new TextDecoder().decode(bytes));
156
157
  return typeof r === 'string' ? string(new TextDecoder().decode(bytes)) : r;
157
158
  }
158
- if (/^text\/|xml|javascript|x-www-form-urlencoded/i.test(type) || type === '') {
159
+ // xml only as the subtype or its +xml suffix: the type of an Excel file (…openxmlformats…) is binary
160
+ if (/^text\/|^[^;]*\/(?:xml|[^;]*\+xml)\s*(?:;|$)|javascript|x-www-form-urlencoded/i.test(type) || type === '') {
159
161
  return string(new TextDecoder().decode(bytes));
160
162
  }
161
163
  return { t: K.Binary, s: { data: [...bytes] }, i: 0 };
@@ -342,11 +344,15 @@ native('http-request', '["Sends an HTTP request; used by fetch." url [url! strin
342
344
  const watch = field(opts, 'progress');
343
345
  const shown = watch === null ? null : progressOf(t, watch);
344
346
  const p = (async () => {
347
+ const started = Date.now();
348
+ let status = null;
349
+ let problem = null;
345
350
  try {
346
351
  const init = { method: verb, headers, body, credentials, signal: ctl.signal };
347
352
  const res = shown !== null && typeof XMLHttpRequest !== 'undefined'
348
353
  ? await sendXhr(url, init, shown.tell)
349
354
  : await sendFetch(url, init, shown === null ? null : shown.tell);
355
+ status = res.status;
350
356
  const type = res.headers.find(([k]) => k === 'content-type')?.[1] ?? '';
351
357
  const data = decodeBody(type, res.bytes);
352
358
  if (full) {
@@ -369,6 +375,7 @@ native('http-request', '["Sends an HTTP request; used by fetch." url [url! strin
369
375
  catch (e) {
370
376
  if (e.t === K.Error)
371
377
  throw e;
378
+ problem = timedOut ? 'timed out' : ctl.signal.aborted ? 'cancelled' : e.message;
372
379
  if (timedOut)
373
380
  throw makeError('access', 'timeout', `request to ${url} timed out`);
374
381
  if (ctl.signal.aborted)
@@ -379,6 +386,7 @@ native('http-request', '["Sends an HTTP request; used by fetch." url [url! strin
379
386
  if (timer !== null)
380
387
  clearTimeout(timer);
381
388
  shown?.end();
389
+ devLog.request(verb, url, status, Date.now() - started, problem);
382
390
  }
383
391
  })();
384
392
  return { k: 'await', p };
@@ -0,0 +1,121 @@
1
+ import { type Cell } from '../core/index.ts';
2
+ import { type El } from './draw.ts';
3
+ export type EdgeLook = 'solid' | 'dashed' | 'faint';
4
+ export interface OrbitNode {
5
+ key: string;
6
+ /** The id as the app gave it: what face/value is when the person picks it. */
7
+ id: Cell;
8
+ /** A string, or a text per language. */
9
+ title: Cell;
10
+ color: string;
11
+ /** How many it holds: small moons around it and a number under its title. */
12
+ count: number | null;
13
+ /** It stands for many: a second ring. */
14
+ many: boolean;
15
+ /** A shortcut or link: a dashed border. */
16
+ dashed: boolean;
17
+ /** A ring of steps around it, the one at `at` the current one (1-based); 0 for none. */
18
+ steps: number;
19
+ at: number;
20
+ }
21
+ export interface OrbitEdge {
22
+ from: string;
23
+ to: string;
24
+ label: Cell | null;
25
+ look: EdgeLook;
26
+ }
27
+ export interface OrbitSpec {
28
+ focus: string | null;
29
+ nodes: Map<string, OrbitNode>;
30
+ edges: OrbitEdge[];
31
+ /** The nodes the person came from, most recent last. */
32
+ trail: string[];
33
+ }
34
+ /** What tells nodes apart: an id is a word, an integer, a string or an issue. */
35
+ export declare function idKey(c: Cell): string | null;
36
+ /** The orbit a block describes, and what was wrong with it; what is wrong is left out. */
37
+ export declare function readOrbit(data: Cell): {
38
+ spec: OrbitSpec;
39
+ error: string | null;
40
+ };
41
+ export type Role = 'focus' | 'near' | 'more' | 'trail';
42
+ /** Where a bubble is, in the orbit's own space with the focus at 0,0: its centre, radius and opacity. */
43
+ export interface Place {
44
+ x: number;
45
+ y: number;
46
+ r: number;
47
+ o: number;
48
+ }
49
+ export interface Placed extends Place {
50
+ key: string;
51
+ role: Role;
52
+ }
53
+ export declare const FOCUS_R = 92;
54
+ /** Neighbours on show at once; past that, a "+N more" bubble pages through them. */
55
+ export declare const PAGE = 30;
56
+ /** Past this many neighbours, edge labels show only on hover. */
57
+ export declare const MANY = 18;
58
+ /** The key of the "+N more" bubble: no id gives it. */
59
+ export declare const MORE = "\0more";
60
+ export interface Box {
61
+ x0: number;
62
+ y0: number;
63
+ x1: number;
64
+ y1: number;
65
+ }
66
+ export interface OrbitLayout {
67
+ placed: Placed[];
68
+ /** The box everything drawn fits in, titles too: the camera fits it. */
69
+ box: Box;
70
+ /** The neighbours not on this page; 0 when they all fit. */
71
+ left: number;
72
+ pages: number;
73
+ }
74
+ /** The neighbours of the focus: every node given that is neither it nor in the trail, in the order given. */
75
+ export declare function neighbours(spec: OrbitSpec): string[];
76
+ /** Places the focus, its neighbours on rings (the given page of them) and the last few of the trail. */
77
+ export declare function layoutOrbit(spec: OrbitSpec, page?: number): OrbitLayout;
78
+ /** The edges to draw between bubbles on show: those given between the focus and its neighbours, and the trail's dotted line. */
79
+ export declare function shownEdges(spec: OrbitSpec, placed: Placed[]): Array<OrbitEdge & {
80
+ curve: number;
81
+ trail: boolean;
82
+ }>;
83
+ /** The label of the edge between the focus and a neighbour, either way round; null if none. */
84
+ export declare function edgeLabel(spec: OrbitSpec, key: string): Cell | null;
85
+ /** A title in lines of at most `max` characters, three lines at most. */
86
+ export declare function wrapTitle(title: string, max: number): string[];
87
+ /** An arc of a circle of radius r from angle a0 to a1, as path data. */
88
+ export declare function arcPath(r: number, a0: number, a1: number): string;
89
+ export interface BubbleLook {
90
+ role: Role;
91
+ r: number;
92
+ /** The camera's zoom once it fits: titles are sized for the screen at it, as the rest of the page's text. */
93
+ scale: number;
94
+ /** A title outside the bubble goes above it rather than under it: for bubbles above the focus, away from their edge. */
95
+ above: boolean;
96
+ title: string;
97
+ color: string;
98
+ /** The ids of the gradients for this color: body and halo. */
99
+ body: string;
100
+ halo: string;
101
+ node: OrbitNode | null;
102
+ }
103
+ /** What a bubble draws, in unit space: halo, body, its rings, moons, title and count. */
104
+ export declare function bubble(b: BubbleLook): El[];
105
+ /** The two gradients of a color: a body lighter in the middle and a soft halo. */
106
+ export declare function gradients(color: string, body: string, halo: string): El[];
107
+ /** Where an edge's line goes, from bubble a to bubble b, cut at their borders; and the middle and angle for its label. */
108
+ export declare function edgePath(a: Place, b: Place, curve: number): {
109
+ d: string;
110
+ x: number;
111
+ y: number;
112
+ angle: number;
113
+ };
114
+ /** The ease of a move: slow at both ends. */
115
+ export declare const ease: (k: number) => number;
116
+ /** Where the camera goes to show the whole box in a view of w by h: centred on it, zoomed to fit, within limits. */
117
+ export declare function fitCamera(w: number, h: number, box: Box): {
118
+ x: number;
119
+ y: number;
120
+ s: number;
121
+ };