@gramlot/gramlot 0.3.35 → 0.3.37

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 (117) hide show
  1. package/dist/components/charts/__init__.py +1 -1
  2. package/dist/components/charts/assets/collection.js +9 -9
  3. package/dist/components/charts/assets/index.js +1 -1
  4. package/dist/components/charts/grammar.json +0 -30
  5. package/dist/components/drawing/__init__.py +1 -1
  6. package/dist/components/drawing/assets/collection.js +3 -3
  7. package/dist/components/drawing/assets/index.js +1 -1
  8. package/dist/components/editors/__init__.py +2 -2
  9. package/dist/components/editors/assets/collection.js +4 -2
  10. package/dist/components/editors/assets/index.js +1 -1
  11. package/dist/components/editors/assets/modules/addon-fit-L6JK4OJX.js +12 -0
  12. package/dist/components/editors/assets/modules/xterm-BF3OSWRI.js +286 -0
  13. package/dist/components/editors/assets/modules/xterm-UBEXZEYY.js +47 -0
  14. package/dist/components/editors/grammar.json +207 -0
  15. package/dist/components/grid/__init__.py +1 -1
  16. package/dist/components/grid/assets/collection.js +6 -6
  17. package/dist/components/grid/assets/index.js +1 -1
  18. package/dist/components/grid/grammar.json +0 -2
  19. package/dist/components/inputs/__init__.py +1 -1
  20. package/dist/components/inputs/assets/collection.js +8 -8
  21. package/dist/components/inputs/assets/index.js +1 -1
  22. package/dist/components/inputs/grammar.json +2 -1
  23. package/dist/components/layout/__init__.py +1 -1
  24. package/dist/components/layout/assets/collection.js +9 -9
  25. package/dist/components/layout/assets/index.js +1 -1
  26. package/dist/components/layout/grammar.json +2 -2
  27. package/dist/components/selection_trial/__init__.py +1 -1
  28. package/dist/components/selection_trial/assets/collection.js +3 -3
  29. package/dist/components/selection_trial/assets/index.js +1 -1
  30. package/dist/components/tree/__init__.py +1 -1
  31. package/dist/components/tree/assets/collection.js +6 -6
  32. package/dist/components/tree/assets/index.js +1 -1
  33. package/dist/components/tree/grammar.json +0 -1
  34. package/dist/components/uploads/__init__.py +1 -1
  35. package/dist/components/uploads/assets/collection.js +3 -3
  36. package/dist/components/uploads/assets/index.js +1 -1
  37. package/dist/components/uploads/grammar.json +0 -3
  38. package/dist/gramlot.js +1238 -627
  39. package/dist/gramlot.min.js +45 -45
  40. package/dist/libraries.json +27 -0
  41. package/dist/standard-collections.js +1 -1
  42. package/package.json +4 -3
  43. package/src/binding/data-binder.js +11 -3
  44. package/src/binding/grid-struct.js +7 -4
  45. package/src/binding/runtime.js +203 -46
  46. package/src/builder/binding.json +23 -1
  47. package/src/builder/gramlot-builder.d.ts +1 -4
  48. package/src/builder/source.js +185 -29
  49. package/src/chat.js +26 -20
  50. package/src/components/attachment-manager.js +38 -34
  51. package/src/components/chart-data-view.js +4 -7
  52. package/src/components/chart-fields.js +18 -6
  53. package/src/components/chart-renderer.js +10 -3
  54. package/src/components/chart-timeline.js +7 -5
  55. package/src/components/charts.js +74 -67
  56. package/src/components/check-box-text-editor.js +3 -3
  57. package/src/components/check-box-text.js +7 -22
  58. package/src/components/check-box-tree.js +20 -16
  59. package/src/components/choice-editor.js +1 -1
  60. package/src/components/drawing.js +47 -34
  61. package/src/components/editors.js +3 -1
  62. package/src/components/grid-cell-editor.js +72 -29
  63. package/src/components/grid-column-filters.js +47 -40
  64. package/src/components/grid-formulas.js +22 -17
  65. package/src/components/grid-header-interaction.js +9 -11
  66. package/src/components/grid-row-interaction.js +25 -24
  67. package/src/components/grid-row-layout.js +28 -29
  68. package/src/components/grid-search-box.js +3 -3
  69. package/src/components/grid-search.js +3 -8
  70. package/src/components/grid-selection.js +33 -29
  71. package/src/components/grid-structure.js +48 -10
  72. package/src/components/grid-tools.js +14 -14
  73. package/src/components/grid.js +83 -79
  74. package/src/components/image-crop-dialog.js +2 -2
  75. package/src/components/image-crop.js +16 -9
  76. package/src/components/image-uploader.js +21 -28
  77. package/src/components/inputs.js +19 -1
  78. package/src/components/registry.js +3 -5
  79. package/src/components/terminal.js +158 -0
  80. package/src/components/tray-pane.js +85 -61
  81. package/src/components/tree.js +332 -239
  82. package/src/components/upload-engine.js +10 -2
  83. package/src/components/uploader.js +31 -27
  84. package/src/editing-session.d.ts +4 -1
  85. package/src/editing-session.js +120 -83
  86. package/src/experiments/window-selection.js +4 -6
  87. package/src/filesystem.js +2 -2
  88. package/src/gramlot.js +1 -0
  89. package/src/handlers/device-handler.js +219 -213
  90. package/src/handlers/rpc-handler.d.ts +17 -0
  91. package/src/handlers/rpc-handler.js +22 -0
  92. package/src/ide-documents.js +29 -11
  93. package/src/ide-runtime.js +33 -31
  94. package/src/index-page-runtime.d.ts +2 -2
  95. package/src/index-page-runtime.js +67 -48
  96. package/src/libraries.js +9 -0
  97. package/src/message-port-connection.js +100 -0
  98. package/src/message-port-store.js +158 -0
  99. package/src/page-store-connection.js +14 -1
  100. package/src/renderer/gramlot-renderer.js +21 -2
  101. package/src/source-picker.d.ts +2 -2
  102. package/src/source-picker.js +15 -6
  103. package/src/stores/bag-collection-store.js +25 -7
  104. package/src/stores/bag-collection-view.js +23 -22
  105. package/src/view/controls.js +1 -1
  106. package/src/view/html.js +45 -20
  107. package/src/visual-editor-canvas.js +1 -1
  108. package/src/visual-editor.js +18 -19
  109. package/tools/gramlot-tools-inspector.js +400 -374
  110. package/tools/inspector-agent.js +223 -49
  111. package/dist/components/vizabi/__init__.py +0 -13
  112. package/dist/components/vizabi/assets/collection.js +0 -4
  113. package/dist/components/vizabi/assets/index.js +0 -3
  114. package/dist/components/vizabi/commands.json +0 -7
  115. package/dist/components/vizabi/grammar.json +0 -128
  116. package/src/components/vizabi-session.js +0 -66
  117. package/src/components/vizabi.js +0 -66
@@ -0,0 +1,158 @@
1
+ /**
2
+ * The store side of the dataBinder protocol over one MessagePort: the browser counterpart of the
3
+ * protocol part of the Python `PageStore` (`subscribe`, `unsubscribe`, `change` and the dispatch of
4
+ * the changes of the served Bag to the binders whose path intersects).
5
+ *
6
+ * It imports no Bag or TYTX module: it uses the class of the Bag it serves, so a page-side bundle
7
+ * shares the Bag class and the TYTX types of the page.
8
+ *
9
+ * @module
10
+ */
11
+ const PATH = /^[^.?#\[\]!:]+(?:\.[^.?#\[\]!:]+)*$/;
12
+ const OPERATIONS = new Set(['subscribe', 'unsubscribe', 'change']);
13
+ let nextStore = 0;
14
+
15
+ function intersects(left, right) {
16
+ return left === right || left.startsWith(right + '.') || right.startsWith(left + '.');
17
+ }
18
+
19
+ /** Serve one Bag to the dataBinder connections at the other end of one MessagePort. */
20
+ export class MessagePortStore {
21
+ #subscribers = new Map();
22
+ #changes = [];
23
+ #dispatching = false;
24
+ #subscription;
25
+ #receive;
26
+
27
+ /**
28
+ * `bag` is the served root; `receive(message)` gets the decoded messages that are not
29
+ * `subscribe`, `unsubscribe` or `change`, for the owner of the port.
30
+ */
31
+ constructor(bag, port, receive = null) {
32
+ this.bag = bag;
33
+ this.port = port;
34
+ this.closed = false;
35
+ this.#receive = receive;
36
+ this.#subscription = `gramlot-message-port-store-${++nextStore}`;
37
+ bag.subscribe(this.#subscription, {any: event => this.#changed(event)});
38
+ port.addEventListener('message', this.#listener);
39
+ port.start();
40
+ }
41
+
42
+ /** Send one message to the other end as the TYTX text of a Bag of its fields. */
43
+ post(message) {
44
+ if (this.closed) return;
45
+ const wire = new this.bag.constructor();
46
+ for (const [name, value] of Object.entries(message)) {
47
+ if (value !== undefined) wire.setItem(name, value);
48
+ }
49
+ this.port.postMessage(wire.toTytx('json'));
50
+ }
51
+
52
+ #listener = event => {
53
+ if (this.closed) return;
54
+ const message = this.bag.constructor.fromTytx(event.data, 'json').asDict();
55
+ if (!OPERATIONS.has(message.op)) {
56
+ this.#receive?.(message);
57
+ return;
58
+ }
59
+ try {
60
+ if (message.op === 'subscribe') this.subscribe(message.binderId, message.path);
61
+ else if (message.op === 'unsubscribe') this.unsubscribe(message.binderId);
62
+ else this.change(message.binderId, message);
63
+ } catch (error) {
64
+ this.post({op: 'error', binderId: message.binderId, error: error.message});
65
+ }
66
+ };
67
+
68
+ /** Register a binder and send its snapshot before any later change. */
69
+ subscribe(binderId, path) {
70
+ if (typeof binderId !== 'string' || !binderId) throw new TypeError('A binder requires an id');
71
+ if (typeof path !== 'string' || !PATH.test(path)) throw new TypeError('A store path must contain dot-separated literal labels');
72
+ if (this.#subscribers.has(binderId)) throw new Error('Binder is already registered');
73
+ this.#subscribers.set(binderId, path);
74
+ const node = this.bag.getNode(path);
75
+ this.post({op: 'snapshot', binderId, path: '', exists: node !== null,
76
+ value: node ? this.#copy(node.getValue(true)) : null, attr: node ? {...node.getAttr()} : {}});
77
+ }
78
+
79
+ unsubscribe(binderId) {
80
+ this.#subscribers.delete(binderId);
81
+ }
82
+
83
+ /** Apply a change of a binder inside its branch; the dispatch skips that binder. */
84
+ change(binderId, message) {
85
+ const base = this.#subscribers.get(binderId);
86
+ if (base === undefined) throw new Error('Binder is not registered');
87
+ const relative = message.path;
88
+ if (typeof relative !== 'string' || (relative !== '' && !PATH.test(relative))) {
89
+ throw new TypeError('A change path must contain dot-separated literal labels');
90
+ }
91
+ if (message.fired) throw new Error('Fired events are outside the dataBinder protocol');
92
+ const path = relative ? base + '.' + relative : base;
93
+ if (message.evt === 'del') this.bag.popNode(path, binderId);
94
+ else if (message.evt === 'upd_attrs') {
95
+ const node = this.bag.getNode(path);
96
+ if (!node) throw new Error('Attribute target does not exist');
97
+ node.setAttr(message.attr, binderId, false, false);
98
+ } else if (['ins', 'upd_value', 'upd'].includes(message.evt)) {
99
+ this.bag.setItem(path, message.value, message.attr, message.index ?? '>', false, false, binderId);
100
+ } else throw new Error('Unsupported datachange event');
101
+ }
102
+
103
+ /** A static copy: resolvers are never run, a resolver node travels with its cached value. */
104
+ #copy(value) {
105
+ if (!(value instanceof this.bag.constructor)) return value;
106
+ const copy = new this.bag.constructor();
107
+ for (const node of value.getNodes()) {
108
+ copy.setItem(node.label, this.#copy(node.getValue(true)), {...node.getAttr()}, '>', false, false);
109
+ }
110
+ return copy;
111
+ }
112
+
113
+ #changed(event) {
114
+ if (event.reason === 'autocreate') return;
115
+ const structural = event.evt === 'ins' || event.evt === 'del';
116
+ const pathlist = event.pathlist ?? [];
117
+ for (const node of Array.isArray(event.node) ? event.node : [event.node]) {
118
+ // The copy is taken now: a later write must not change an earlier message.
119
+ this.#changes.push({path: (structural ? [...pathlist, node.label] : pathlist).join('.'), evt: event.evt,
120
+ origin: event.reason, index: event.ind ?? null,
121
+ value: event.evt === 'del' ? null : this.#copy(node.getValue(true)), attr: {...node.getAttr()}});
122
+ }
123
+ if (this.#dispatching) return;
124
+ this.#dispatching = true;
125
+ try {
126
+ while (this.#changes.length) this.#dispatch(this.#changes.shift());
127
+ } finally {
128
+ this.#dispatching = false;
129
+ }
130
+ }
131
+
132
+ #dispatch({path, evt, origin, index, value, attr}) {
133
+ for (const [binderId, base] of [...this.#subscribers]) {
134
+ if (binderId === origin || !intersects(path, base)) continue;
135
+ if (path === base || path.startsWith(base + '.')) {
136
+ this.post({op: 'change', binderId, path: path === base ? '' : path.slice(base.length + 1),
137
+ evt, value, attr, index});
138
+ } else {
139
+ if (evt === 'upd_attrs') continue;
140
+ // The change of an ancestor: the binder's branch inside the copied value.
141
+ const child = evt !== 'del' && value instanceof this.bag.constructor ? value.getNode(base.slice(path.length + 1)) : null;
142
+ this.post({op: 'change', binderId, path: '', evt: child ? 'upd_value' : 'del',
143
+ value: child ? child.getValue(true) : null, attr: child ? {...child.getAttr()} : {}, index: null});
144
+ }
145
+ }
146
+ }
147
+
148
+ /** Tell the other end that the store is closed, then release the Bag and the port listener. */
149
+ close() {
150
+ if (this.closed) return;
151
+ this.post({op: 'closed'});
152
+ this.closed = true;
153
+ this.bag.unsubscribe(this.#subscription, {any: true});
154
+ this.port.removeEventListener('message', this.#listener);
155
+ this.#subscribers.clear();
156
+ this.#changes = [];
157
+ }
158
+ }
@@ -37,6 +37,14 @@ export class PageStoreConnection {
37
37
  if (!this.parent) this.open();
38
38
  }
39
39
 
40
+ /** The address of the terminal socket of `session` for this page: `terminal` beside the store route. */
41
+ terminalUrl(session) {
42
+ const address = new URL('terminal', this.endpoint);
43
+ address.searchParams.set('pageId', this.gramlot.pageId);
44
+ address.searchParams.set('session', session);
45
+ return address.href;
46
+ }
47
+
40
48
  /** Opens this page's own WebSocket: the page is the root of its family channel. */
41
49
  open() {
42
50
  const address = this.address;
@@ -203,10 +211,15 @@ export class PageStoreConnection {
203
211
  }
204
212
  }
205
213
 
214
+ /**
215
+ * The first failure of an open channel is reported once. `Gramlot.dispose`, also on the
216
+ * page's own pagehide, closes the channel first: a later failure reports nothing.
217
+ */
206
218
  fail(error) {
219
+ const report = !this.closed;
207
220
  this.error = error;
208
221
  this.close();
209
- this.window.console.error('PageStore channel failed', error);
222
+ if (report) this.gramlot.src.binding.reportError(error, 'PageStore channel failed');
210
223
  }
211
224
 
212
225
  close() {
@@ -5,6 +5,7 @@ import {callLogic, invokeHook} from '../binding/hooks.js';
5
5
  *
6
6
  * @module
7
7
  */
8
+ import {Bag} from '@genrojs/bag';
8
9
  import {BuilderBase, SourceBag, sourceTarget} from '@genrojs/builders';
9
10
  import {HtmlElement} from '../view/html.js';
10
11
  import {ControlAdapter, RadioControl, RadioGroups, SelectControl} from '../view/controls.js';
@@ -14,7 +15,7 @@ import {GroupletCardActions} from '../view/grouplet-card.js';
14
15
  import {NativeEventBinding} from '../view/events.js';
15
16
  import {References} from '../references.js';
16
17
  import {randomId} from '../random-id.js';
17
- import {GramlotBuilderBag, GramlotBuilderBagNode} from '../builder/source.js';
18
+ import {GramlotBuilderBag, GramlotBuilderBagNode, SYMBOLIC_ATTRIBUTES} from '../builder/source.js';
18
19
  import {BindingRuntime} from '../binding/runtime.js';
19
20
  import {requireInstallable} from '../binding/installation.js';
20
21
  import {GramlotHtmlRenderer} from './gramlot-html-renderer.js';
@@ -50,7 +51,7 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
50
51
  this._binding = binding;
51
52
  this.source = source;
52
53
  this.destination = destination;
53
- this.html = new HtmlElement({uploadContext: () => ({gramlot: binding.gramlot, window: destination.ownerDocument.defaultView}), metadataAttributes: ['__ref'],
54
+ this.html = new HtmlElement({uploadContext: () => ({gramlot: binding.gramlot, window: destination.ownerDocument.defaultView}), metadataAttributes: ['__ref', ...SYMBOLIC_ATTRIBUTES],
54
55
  format: (value, options) => binding.gramlot.format(value, options),
55
56
  reportError: (node, error, title) => { if (this.records.has(node)) binding.reportError(error, title); },
56
57
  callLogic: (node, name, keywords) => {
@@ -129,10 +130,28 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
129
130
  attrs = Object.assign({}, ...ancestors, attrs);
130
131
  }
131
132
  this.#rebuildAttributes.delete(node);
133
+ attrs = this.#withoutHeldBags(attrs, component);
132
134
  this.#resolvedAttributes.set(node, attrs);
133
135
  return attrs;
134
136
  }
135
137
 
138
+ /**
139
+ * A dtype 'X' component parameter whose value is a Bag with a parent (a sub Bag of Data or Source,
140
+ * reached through a pointer) is projected as null: the component reads it at its address when it uses
141
+ * it and never holds it (owner, 2026-11-10: "basta che non abbia parent"). A parentless Bag, such as a
142
+ * Source-literal value, and every non-Bag value are projected as they are.
143
+ */
144
+ #withoutHeldBags(attrs, component) {
145
+ let result = attrs;
146
+ for (const [name, spec] of Object.entries(component?.parameters ?? {})) {
147
+ const value = attrs[name];
148
+ if (!(value instanceof Bag) || !(value.parent || value.parentNode) || ![spec.dtype].flat().includes('X')) continue;
149
+ if (result === attrs) result = {...attrs};
150
+ result[name] = null;
151
+ }
152
+ return result;
153
+ }
154
+
136
155
  /**
137
156
  * Structural Source fragments are owned by the live renderer.
138
157
  * A data-element has no DOM: it is recognized before validation and runtimeValues (R08).
@@ -1,9 +1,9 @@
1
- import type {SourceBagNode} from '@genrojs/builders';
1
+ import type {SourceBag, SourceBagNode} from '@genrojs/builders';
2
2
  import type {GramlotRenderer} from './renderer/gramlot-renderer.js';
3
3
 
4
4
  /** Reusable, lifecycle-bound selection of rendered Source nodes in an editor preview. */
5
5
  export class SourcePicker {
6
- constructor(options: {renderer: GramlotRenderer; root: SourceBagNode; canvas?:Element; onSelect: (node: SourceBagNode) => void});
6
+ constructor(options: {renderer: GramlotRenderer; source: SourceBag; path: string; canvas?:Element; onSelect: (node: SourceBagNode) => void});
7
7
  select(node: SourceBagNode | null): void;
8
8
  dispose(): void;
9
9
  }
@@ -1,20 +1,27 @@
1
1
  /** Select rendered Source descendants in an editor preview without activating them. */
2
2
  export class SourcePicker {
3
- #renderer; #root; #canvas; #onSelect; #observer; #selectedPath = null;
3
+ #renderer; #source; #path; #canvas; #onSelect; #observer; #selectedPath = null;
4
4
  #highlighted = null; #release = null; #disposed = false;
5
- constructor({renderer, root, onSelect, canvas = renderer.records.get(root.parentNode)?.presentation}) {
5
+ /** `source` is the Source root and `path` the address of the preview root in it; the root node is never held. */
6
+ constructor({renderer, source, path, onSelect, canvas}) {
7
+ const root = typeof path === 'string' ? source?.getNode(path) : null;
6
8
  if (!root?.parentNode || typeof onSelect !== 'function') throw new TypeError('SourcePicker requires a mounted Source root and onSelect');
9
+ canvas ??= renderer.records.get(root.parentNode)?.presentation;
7
10
  if (!canvas || canvas.nodeType !== 1) throw new TypeError('SourcePicker requires a mounted preview');
8
- this.#renderer = renderer; this.#root = root; this.#canvas = canvas; this.#onSelect = onSelect;
11
+ this.#renderer = renderer; this.#source = source; this.#path = path; this.#canvas = canvas; this.#onSelect = onSelect;
9
12
  canvas.addEventListener('pointerdown', this, true);
10
13
  canvas.addEventListener('click', this, true);
11
14
  this.#observer = new canvas.ownerDocument.defaultView.MutationObserver(() => this.#paint());
12
15
  this.#observer.observe(canvas, {subtree:true, childList:true, attributes:true, attributeFilter:['class']});
13
16
  this.#release = renderer.onDispose(root.parentNode, () => this.dispose());
14
17
  }
18
+ /** The preview root node now at the picker's address, or null. */
19
+ #root() { return this.#source.getNode(this.#path); }
15
20
  handleEvent(event) {
16
21
  if (this.#disposed) return;
17
- const prefix = this.#root.fullpath + '.';
22
+ const root = this.#root();
23
+ if (!root) return;
24
+ const prefix = root.fullpath + '.';
18
25
  const record = event.composedPath().map(element => this.#renderer.elements.get(element)).find(item =>
19
26
  item?.node.fullpath.startsWith(prefix));
20
27
  if (!record) return;
@@ -28,7 +35,9 @@ export class SourcePicker {
28
35
  if (this.#disposed) return;
29
36
  if (node === null) this.#selectedPath = null;
30
37
  else {
31
- const prefix = this.#root.fullpath + '.';
38
+ const root = this.#root();
39
+ if (!root) throw new TypeError('SourcePicker preview root is missing');
40
+ const prefix = root.fullpath + '.';
32
41
  if (!node.fullpath.startsWith(prefix)) throw new TypeError('Selected node must be inside the preview Source root');
33
42
  this.#selectedPath = node.fullpath.slice(prefix.length);
34
43
  }
@@ -36,7 +45,7 @@ export class SourcePicker {
36
45
  }
37
46
  #paint() {
38
47
  if (this.#disposed) return;
39
- const node = this.#selectedPath ? this.#root.staticValue.getNode(this.#selectedPath) : null;
48
+ const node = this.#selectedPath ? this.#root()?.staticValue.getNode(this.#selectedPath) ?? null : null;
40
49
  const element = node ? this.#renderer.records.get(node)?.presentation : null;
41
50
  const next = element?.nodeType === 1 ? element : null;
42
51
  if (this.#highlighted !== next) {
@@ -1,15 +1,28 @@
1
1
  import {Bag} from '@genrojs/bag';
2
2
 
3
- /** Shared resident data. Ordering and filtering belong to each consuming view. */
3
+ /**
4
+ * Shared resident data at one Data address. The store keeps the page's Data root and the
5
+ * absolute `path`, never the collection Bag: rows are read from the root at each access,
6
+ * without running resolvers. Ordering and filtering belong to each consuming view.
7
+ */
4
8
  export class BagCollectionStore {
5
9
  #listeners = new Set();
6
- constructor(bag, rowMode) {
7
- if (!(bag instanceof Bag)) throw new TypeError('collection store requires a Bag');
10
+ #data;
11
+ #path;
12
+ #rowMode;
13
+ constructor(data, path, rowMode) {
14
+ if (!(data instanceof Bag)) throw new TypeError('collection store requires the Data root Bag');
15
+ if (typeof path !== 'string' || !path) throw new TypeError(`collection store requires a Data path: ${path}`);
8
16
  if (!['values','attributes'].includes(rowMode)) throw new TypeError('collection store rowMode must explicitly be values or attributes');
9
- if (!bag.parentNode) throw new TypeError('collection store requires a Bag attached to Data');
10
- this.bag = bag;
11
- this.rowMode = rowMode;
17
+ this.#data = data;
18
+ this.#path = path;
19
+ this.#rowMode = rowMode;
12
20
  }
21
+ /** The absolute Data path of the collection. */
22
+ get path() { return this.#path; }
23
+ get rowMode() { return this.#rowMode; }
24
+ /** The key of the store among the page's stores: one store per absolute path and rowMode. */
25
+ get key() { return collectionKey(this.#path, this.#rowMode); }
13
26
  subscribe(listener) {
14
27
  this.#listeners.add(listener);
15
28
  return () => this.#listeners.delete(listener);
@@ -24,7 +37,9 @@ export class BagCollectionStore {
24
37
  publishCollection(this.#listeners, {full:row === null, rows:row ? [row] : [], event, field});
25
38
  }
26
39
  getRows() {
27
- const nodes = this.bag.getNodes();
40
+ const bag = this.#data.getItem(this.path, null, true);
41
+ if (!(bag instanceof Bag)) throw new TypeError(`collection store requires a Bag at ${this.path}`);
42
+ const nodes = bag.getNodes();
28
43
  for (const node of nodes) {
29
44
  if (node.resolver) throw new TypeError('collection store requires loaded rows');
30
45
  if (this.rowMode === 'values' && !(node.staticValue instanceof Bag)) {
@@ -44,6 +59,9 @@ export class BagCollectionStore {
44
59
  }
45
60
  }
46
61
 
62
+ /** The key of the collection store at the absolute Data `path` read in `rowMode`. */
63
+ export function collectionKey(path, rowMode) { return `${rowMode}:${path}`; }
64
+
47
65
  /**
48
66
  * One invalidation protocol at both collection levels; failures cannot skip siblings.
49
67
  * A store notice also carries its Data `event` and changed `field`; a view notice carries null.
@@ -4,7 +4,11 @@ import {compileFilter} from './collection-filter.js';
4
4
  import {containsId, requireId, requireIdSet} from './id-set.js';
5
5
  import {publishCollection} from './bag-collection-store.js';
6
6
 
7
- /** Source-owned view: original BagNodes, independent indices and criteria. */
7
+ /**
8
+ * Source-owned view over one collection store: independent indices and criteria. A row is
9
+ * identified by its key (node label), as the legacy Bag store identifier: the manual order,
10
+ * sort and filters survive a replacement of the Bag at the store path.
11
+ */
8
12
  export class BagCollectionView {
9
13
  #store;
10
14
  #release = null;
@@ -13,9 +17,15 @@ export class BagCollectionView {
13
17
  #sortedBy = '';
14
18
  #filters = new Map();
15
19
  #filtered = null;
16
- constructor(store) { this.bindStore(store); }
17
- get bag() { return this.#store.bag; }
20
+ constructor(store) {
21
+ this.#store = store;
22
+ this.synchronize();
23
+ }
24
+ /** The absolute Data path of the store: two views over the same path and rowMode share the collection. */
25
+ get path() { return this.#store.path; }
18
26
  get rowMode() { return this.#store.rowMode; }
27
+ /** The key of the store the view reads: two views over the same key show the same collection. */
28
+ get storeKey() { return this.#store.key; }
19
29
  get sortedBy() { return this.#sortedBy; }
20
30
  get criteria() { return parseSort(this.#sortedBy); }
21
31
  get filter() { return this.#filters.get('search')?.definition ?? null; }
@@ -43,22 +53,12 @@ export class BagCollectionView {
43
53
  this.#release = this.#store.subscribe(change => this.synchronize(change));
44
54
  }
45
55
  disconnect() { this.#release?.(); this.#release = null; }
46
- bindStore(store) {
47
- if (this.#store === store) return;
48
- const connected = this.#release !== null;
49
- this.disconnect();
50
- this.#store = store;
51
- // Only node identities are retained, never cell values. A replacement Bag
52
- // keeps this widget's criteria but starts a fresh manual index.
53
- this.#indices.clear();
54
- if (connected) this.connect(); else this.synchronize();
55
- }
56
56
  getRows(filtered = true) {
57
57
  const nodes = this.#orderedNodes();
58
58
  return filtered && this.#filtered !== null ? this.#filtered.map(index => nodes[index]) : nodes;
59
59
  }
60
60
  #orderedNodes() {
61
- return this.#store.getRows().toSorted((a,b) => (this.#indices.get(a) ?? Infinity) - (this.#indices.get(b) ?? Infinity));
61
+ return this.#store.getRows().toSorted((a,b) => (this.#indices.get(a.label) ?? Infinity) - (this.#indices.get(b.label) ?? Infinity));
62
62
  }
63
63
  synchronize(change = {full:true, rows:[]}) {
64
64
  if (!change.full && !this.#sortedBy && !this.#filters.size) {
@@ -71,11 +71,12 @@ export class BagCollectionView {
71
71
  const nodes = this.#sorted(this.#orderedNodes(),parseSort(sortedBy));
72
72
  this.#commit(nodes,sortedBy,this.#filters);
73
73
  }
74
- moveRow(node, target, after = false) {
75
- if (node === target || node.parentBag !== this.bag || target.parentBag !== this.bag) return;
76
- const nodes = this.getRows(false);
77
- nodes.splice(nodes.indexOf(node),1);
78
- nodes.splice(nodes.indexOf(target)+Number(after),0,node);
74
+ /** Move the row of key `label` before or after the row of key `target`; a key absent from the collection moves nothing. */
75
+ moveRow(label, target, after = false) {
76
+ const nodes = this.getRows(false), from = nodes.findIndex(row => row.label === label);
77
+ if (label === target || from < 0 || !nodes.some(row => row.label === target)) return;
78
+ const [moved] = nodes.splice(from,1);
79
+ nodes.splice(nodes.findIndex(row => row.label === target)+Number(after),0,moved);
79
80
  this.#commit(nodes,'',this.#filters);
80
81
  }
81
82
  applyFilter(filter, contribution = 'search') {
@@ -138,12 +139,12 @@ export class BagCollectionView {
138
139
  const filtered = predicates.length ? nodes.flatMap((node,index) =>
139
140
  predicates.every(predicate => predicate(field => this.value(node,field))) ? [index] : []) : null;
140
141
  const full = change.full || sortedBy !== this.#sortedBy || filters !== this.#filters ||
141
- nodes.length !== this.#indices.size || nodes.some((node,index) => this.#indices.get(node) !== index) ||
142
+ nodes.length !== this.#indices.size || nodes.some((node,index) => this.#indices.get(node.label) !== index) ||
142
143
  JSON.stringify(filtered) !== JSON.stringify(this.#filtered);
143
- this.#indices = new Map(nodes.map((node,index) => [node,index]));
144
+ this.#indices = new Map(nodes.map((node,index) => [node.label,index]));
144
145
  this.#sortedBy = sortedBy; this.#filters = filters; this.#filtered = filtered;
145
146
  const visible = filtered === null ? null : new Set(filtered);
146
- const rows = full ? [] : change.rows.filter(node => this.#indices.has(node) && (visible === null || visible.has(this.#indices.get(node))));
147
+ const rows = full ? [] : change.rows.filter(node => this.#indices.has(node.label) && (visible === null || visible.has(this.#indices.get(node.label))));
147
148
  if (full || rows.length || change.rows.length) publishCollection(this.#listeners,{full,rows});
148
149
  }
149
150
  #sorted(nodes, criteria) {
@@ -627,7 +627,7 @@ function componentContext(adapter) {
627
627
  if (binding.closed) return;
628
628
  const pointer = node.getAttr('value');
629
629
  if (node.pointerType(pointer) !== '^' || pointer.includes('?') || !Object.is(node.getRelativeData(pointer), value)) return;
630
- const path = pointer.slice(1) + '?_displayedValue';
630
+ const path = pointer.slice(1) + '?_dv_displayedValue';
631
631
  if (node.getRelativeData(path) !== caption) node.setRelativeData(path, caption);
632
632
  },
633
633
  periodOutput:(path, value) => node.setRelativeData(path, value),
package/src/view/html.js CHANGED
@@ -130,12 +130,24 @@ export class HtmlElement {
130
130
  frameRecipient: () => frameRecipient(commandFrame(node)),
131
131
  locale: node.rootBuilder.binding.gramlot.locale,
132
132
  timeZone: node.rootBuilder.binding.gramlot.timeZone,
133
- collectionView: (bag, rowMode) => node.rootBuilder.binding.collectionView(node, bag, rowMode),
133
+ collectionView: (path, rowMode) => node.rootBuilder.binding.collectionView(node, path, rowMode),
134
134
  format: this.format,
135
135
  upload: () => this.uploadContext(),
136
+ terminalUrl: session => {
137
+ const store = node.rootBuilder.binding.gramlot._pageStore;
138
+ if (!store) throw new Error('A terminal requires a server with a PageStore channel (websocket)');
139
+ return store.terminalUrl(session);
140
+ },
136
141
  reportError: (error, title) => this.reportError(node, error, title),
137
142
  publish: (topic, payload) => node.publish(topic, payload),
138
- writeParameter: (name, value) => this.writeParameter(node, name, value),
143
+ writeParameter: (name, value, attributes = null) => this.writeParameter(node, name, value, attributes),
144
+ attrDatapath: name => this.attrDatapath(node, name),
145
+ getStaticData: path => this.getStaticData(node, path),
146
+ // The value at the Data `path` relative to the node, read without running resolvers, and its write.
147
+ readRelativeData: path => this.getStaticData(node, node.absDatapath(path)),
148
+ setRelativeData: (path, value, attributes = null) => node.setRelativeData(path, value, {attributes}),
149
+ // The tree's store, read from its storepath (`*S`, `*D` or a Data path) at each call.
150
+ treeStore: () => node.rootBuilder.binding.treeStore(node),
139
151
  callLogic: (name, keywords) => this.callLogic(node, name, keywords),
140
152
  });
141
153
  }
@@ -173,15 +185,41 @@ export class HtmlElement {
173
185
  * pointer writes; a literal, a `=` pointer and a pointer with a suffix write nothing (no inverse
174
186
  * conversion, 11.81). A value equal (`Object.is`) to the Data value is not written, as the legacy
175
187
  * `setAttributeInDatasource` with `lazySet`: a null value on a missing path creates no Data node.
188
+ * `attributes` (legacy `setRelativeData` attributes, such as `_valuelabel`) go on the written Data node.
176
189
  */
177
- writeParameter(node, name, value) {
190
+ writeParameter(node, name, value, attributes = null) {
178
191
  const pointer = node.getAttr(name);
179
192
  if (node.pointerType(pointer) === '^' && !pointerTransform(pointer)
180
193
  && !Object.is(node.getRelativeData(pointer.slice(1)), value)) {
181
- node.setRelativeData(pointer.slice(1), value);
194
+ node.setRelativeData(pointer.slice(1), value, {attributes});
182
195
  }
183
196
  }
184
197
 
198
+ /**
199
+ * The absolute Data path of the original attribute `name` of `node`, as the legacy
200
+ * `attrDatapath`: a `^`/`=` pointer loses its symbol, a plain string (`storepath`) is a path;
201
+ * relative paths and symbolic anchors resolve in the node's context. A missing attribute and a
202
+ * pointer on a null path (Q7) give null. The path is resolved at each call, never kept.
203
+ */
204
+ attrDatapath(node, name) {
205
+ const value = node.getAttr(name);
206
+ if (value == null) return null;
207
+ if (typeof value !== 'string' || value.startsWith('==')) {
208
+ throw new TypeError(`${node.nodeTag} '${node.label}': attribute '${name}' is not a Data path`);
209
+ }
210
+ return node.absDatapath(value);
211
+ }
212
+
213
+ /**
214
+ * The value at the absolute Data `path`, read without running resolvers (Bag `getItem` with
215
+ * `isStatic`); null on a null path (Q7). The caller reads at each use and keeps nothing.
216
+ */
217
+ getStaticData(node, path) {
218
+ if (path === null) return null;
219
+ if (typeof path !== 'string' || !path) throw new TypeError(`A static Data read requires a path: ${path}`);
220
+ return node.data.getItem(path, null, true);
221
+ }
222
+
185
223
  compose(record, text, children) {
186
224
  record.text.data = text ?? '';
187
225
  for (const child of children) record.element.append(child);
@@ -236,22 +274,9 @@ export class HtmlElement {
236
274
  projectLabelbox(record.labelbox, {...defaults, ...attrs}, adapt);
237
275
  }
238
276
  if (this.definition(node).meta.gramlot_component) {
239
- if (this.definition(node).meta.gramlot_component.bagStore) {
240
- if (node.nodeTag === 'tree') element.setStoreBag(node.rootBuilder.binding.treeStore(node));
241
- else {
242
- const path = node.absDatapath(node.getAttr('storepath'));
243
- if (!change || change.path === path || change.path.startsWith(`${path}.`) || path.startsWith(`${change.path}.`)) {
244
- element.setStoreBag(node.getRelativeData(node.getAttr('storepath')));
245
- }
246
- }
247
- }
248
- if (this.definition(node).meta.gramlot_component.bagStruct) {
249
- const path = node.absDatapath(node.getAttr('structpath'));
250
- if (!change || change.path === path || change.path.startsWith(`${path}.`) || path.startsWith(`${change.path}.`)) {
251
- element.setStructBag(node.getRelativeData(node.getAttr('structpath')));
252
- }
253
- }
254
- attrs = element.projectAttributes(attrs, adapt);
277
+ // A component reads its data and structure by address when it draws; the path of the
278
+ // Data change that asked for the projection (null otherwise) tells it what changed.
279
+ attrs = element.projectAttributes(attrs, adapt, change?.path ?? null);
255
280
  }
256
281
  if (record.labelbox) {
257
282
  attrs = Object.fromEntries(Object.entries(attrs).filter(([name]) =>
@@ -2,7 +2,7 @@
2
2
  export class VisualEditorCanvas {
3
3
  constructor({app, editor, design}) {
4
4
  this.renderer = app.src.renderer;
5
- this.design = design;
5
+ // The design node is used only while mounting; the canvas never holds it.
6
6
  const presentation = this.renderer.records.get(design).presentation;
7
7
  this.frame = presentation.ownerDocument.createElement('iframe');
8
8
  this.frame.title = 'Page canvas';
@@ -71,10 +71,6 @@ const specifications = (node, tag, current = null) => {
71
71
  ...(node._getMeta('gramlot_component')?.childParameters ?? {})};
72
72
  };
73
73
 
74
- // Editor-local view resources; selection itself remains in the Data Bag.
75
- const canvasSelections = new WeakMap();
76
- const canvasDocuments = new WeakMap();
77
-
78
74
  export class VisualEditorLogic {
79
75
  chatRender(node, params) { return chatRender(this.page, node, params); }
80
76
  chatCompleted(node, params) {
@@ -233,33 +229,36 @@ export class VisualEditorLogic {
233
229
  node.SET('.selection', selected);
234
230
  }
235
231
 
232
+ // Editor view resources live in the editor node's _internal (GC-225) and reach the design root by its
233
+ // path; selection itself remains in the Data Bag.
236
234
  initializeCanvasSelection(node) {
237
235
  const renderer = node.rootBuilder.binding.renderer;
238
- const design = node.builder.source.getNode(sourceRoot(node));
239
- const editor = visualEditorScope(node), css = editor.GET('.canvasCss');
236
+ const editor = visualEditorScope(node), css = editor.GET('.canvasCss'), path = sourceRoot(editor);
240
237
  if (css != null) this.applyCanvasCss(editor, css);
241
- const previous = canvasSelections.get(design);
238
+ const previous = editor._internal.canvasSelection;
242
239
  previous?.dispose();
243
- const picker = new SourcePicker({renderer, root:design,
244
- ...(canvasDocuments.has(design) ? {canvas:canvasDocuments.get(design).body} : {}),
240
+ const canvas = editor._internal.canvasDocument;
241
+ const picker = new SourcePicker({renderer, source:editor.builder.source, path,
242
+ ...(canvas ? {canvas:canvas.body} : {}),
245
243
  onSelect:selected => {
246
- node.SET('.sourceSearch', '');
247
- node.SET('.selection', selected.fullpath.slice(design.fullpath.length + 1));
244
+ const design = editor.builder.source.getNode(path);
245
+ editor.SET('.sourceSearch', '');
246
+ editor.SET('.selection', selected.fullpath.slice(design.fullpath.length + 1));
248
247
  }});
249
- canvasSelections.set(design, picker);
250
- if (!previous) renderer.onDispose(design.parentNode, () => {
251
- canvasSelections.delete(design); canvasDocuments.delete(design);
248
+ editor._internal.canvasSelection = picker;
249
+ if (!previous) renderer.onDispose(editor.builder.source.getNode(path).parentNode, () => {
250
+ delete editor._internal.canvasSelection; delete editor._internal.canvasDocument;
252
251
  });
253
252
  this.highlightSelection(node);
254
253
  }
255
254
  applyCanvasCss(node, texts) {
256
255
  if (!Array.isArray(texts) || texts.some(text => typeof text !== 'string'))
257
256
  throw new TypeError('canvas CSS must be a list of CSS texts');
258
- const editor = visualEditorScope(node), design = editor.builder.source.getNode(sourceRoot(editor));
259
- let canvas = canvasDocuments.get(design);
257
+ const editor = visualEditorScope(node);
258
+ let canvas = editor._internal.canvasDocument;
260
259
  if (!canvas) {
261
- canvas = new VisualEditorCanvas({app:editor.rootBuilder.binding.gramlot, editor, design});
262
- canvasDocuments.set(design, canvas);
260
+ canvas = editor._internal.canvasDocument = new VisualEditorCanvas({app:editor.rootBuilder.binding.gramlot, editor,
261
+ design:editor.builder.source.getNode(sourceRoot(editor))});
263
262
  }
264
263
  canvas.setCss(texts);
265
264
  }
@@ -272,7 +271,7 @@ export class VisualEditorLogic {
272
271
  highlightSelection(node) {
273
272
  const design = node.builder.source.getNode(sourceRoot(node));
274
273
  const path = node.GET('.selection');
275
- canvasSelections.get(design)?.select(path ? design.staticValue.getNode(path) : null);
274
+ visualEditorScope(node)._internal.canvasSelection?.select(path ? design.staticValue.getNode(path) : null);
276
275
  }
277
276
 
278
277
  clipboardShortcut(node, event) {