@gramlot/gramlot 0.3.40 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/assets/icons/applications.svg +4 -0
  2. package/assets/icons/archive.svg +4 -0
  3. package/assets/icons/attachment.svg +4 -0
  4. package/assets/icons/book.svg +4 -0
  5. package/assets/icons/bookmark.svg +4 -0
  6. package/assets/icons/calendar.svg +4 -0
  7. package/assets/icons/cancel.svg +4 -0
  8. package/assets/icons/chart.svg +4 -1
  9. package/assets/icons/chat.svg +4 -0
  10. package/assets/icons/clock.svg +4 -0
  11. package/assets/icons/code.svg +4 -0
  12. package/assets/icons/collapse.svg +4 -0
  13. package/assets/icons/columns.svg +4 -0
  14. package/assets/icons/confirm.svg +4 -0
  15. package/assets/icons/copy.svg +4 -0
  16. package/assets/icons/cut.svg +4 -0
  17. package/assets/icons/database.svg +4 -0
  18. package/assets/icons/delete.svg +4 -0
  19. package/assets/icons/detach.svg +4 -0
  20. package/assets/icons/document.svg +4 -0
  21. package/assets/icons/download.svg +4 -0
  22. package/assets/icons/duplicate.svg +4 -0
  23. package/assets/icons/edit.svg +4 -0
  24. package/assets/icons/expand.svg +4 -0
  25. package/assets/icons/export.svg +4 -0
  26. package/assets/icons/eye.svg +4 -0
  27. package/assets/icons/filter.svg +4 -0
  28. package/assets/icons/first.svg +4 -0
  29. package/assets/icons/folder.svg +4 -0
  30. package/assets/icons/group.svg +4 -0
  31. package/assets/icons/help.svg +4 -0
  32. package/assets/icons/home.svg +4 -0
  33. package/assets/icons/import.svg +4 -0
  34. package/assets/icons/info.svg +4 -0
  35. package/assets/icons/inspector.svg +4 -0
  36. package/assets/icons/last.svg +4 -0
  37. package/assets/icons/layers.svg +4 -0
  38. package/assets/icons/link.svg +4 -0
  39. package/assets/icons/list.svg +4 -0
  40. package/assets/icons/lock.svg +4 -0
  41. package/assets/icons/logout.svg +4 -0
  42. package/assets/icons/navigation.svg +4 -0
  43. package/assets/icons/new.svg +4 -0
  44. package/assets/icons/next.svg +4 -0
  45. package/assets/icons/open.svg +4 -0
  46. package/assets/icons/palette.svg +4 -0
  47. package/assets/icons/panel-bottom.svg +4 -0
  48. package/assets/icons/panel-left.svg +4 -0
  49. package/assets/icons/panel-right.svg +4 -0
  50. package/assets/icons/panel-top.svg +4 -0
  51. package/assets/icons/paste.svg +4 -0
  52. package/assets/icons/pause.svg +4 -0
  53. package/assets/icons/pin.svg +4 -0
  54. package/assets/icons/play.svg +4 -0
  55. package/assets/icons/previous.svg +4 -0
  56. package/assets/icons/print.svg +4 -0
  57. package/assets/icons/redo.svg +4 -0
  58. package/assets/icons/refresh.svg +4 -0
  59. package/assets/icons/save.svg +4 -0
  60. package/assets/icons/search.svg +4 -0
  61. package/assets/icons/send.svg +4 -0
  62. package/assets/icons/settings.svg +4 -0
  63. package/assets/icons/sort-ascending.svg +4 -0
  64. package/assets/icons/sort-descending.svg +4 -0
  65. package/assets/icons/stop.svg +4 -0
  66. package/assets/icons/table.svg +4 -0
  67. package/assets/icons/tree.svg +4 -0
  68. package/assets/icons/undo.svg +4 -0
  69. package/assets/icons/unlock.svg +4 -0
  70. package/assets/icons/upload.svg +4 -0
  71. package/assets/icons/user.svg +4 -0
  72. package/assets/icons/warning.svg +4 -0
  73. package/assets/icons/zoom-in.svg +4 -0
  74. package/assets/icons/zoom-out.svg +4 -0
  75. package/dist/components/charts/__init__.py +1 -1
  76. package/dist/components/charts/assets/index.js +1 -1
  77. package/dist/components/charts/assets/libraries/vega.js +11 -11
  78. package/dist/components/editors/__init__.py +1 -1
  79. package/dist/components/editors/assets/index.js +1 -1
  80. package/dist/components/editors/assets/libraries/codemirror.js +30 -31
  81. package/dist/components/editors/assets/libraries/jodit.js +3 -1
  82. package/dist/components/editors/assets/libraries/prosemirror.js +1 -1
  83. package/dist/components/editors/grammar.json +6 -1
  84. package/dist/components/grid/__init__.py +1 -1
  85. package/dist/components/grid/assets/index.js +1 -1
  86. package/dist/components/grid/commands.json +4 -6
  87. package/dist/components/grid/grammar.json +51 -0
  88. package/dist/components/inputs/__init__.py +1 -1
  89. package/dist/components/inputs/assets/index.js +1 -1
  90. package/dist/components/inputs/grammar.json +4987 -216
  91. package/dist/components/layout/__init__.py +1 -1
  92. package/dist/components/layout/assets/index.js +1 -1
  93. package/dist/components/layout/grammar.json +69 -4
  94. package/dist/components/tree/__init__.py +1 -1
  95. package/dist/components/tree/assets/index.js +1 -1
  96. package/dist/components/tree/grammar.json +16 -0
  97. package/dist/components/typst/__init__.py +1 -1
  98. package/dist/components/typst/assets/collection.js +6 -6
  99. package/dist/components/typst/assets/index.js +1 -1
  100. package/dist/components/uploads/__init__.py +1 -1
  101. package/dist/components/uploads/assets/index.js +1 -1
  102. package/dist/components/uploads/grammar.json +66 -0
  103. package/dist/declared-collections.js +1 -1
  104. package/dist/gramlot.js +4007 -1256
  105. package/dist/gramlot.min.js +329 -131
  106. package/dist/libraries.json +1 -0
  107. package/dist/standard-collections.js +1 -1
  108. package/dist/standard-grammar.js +1 -1
  109. package/package.json +3 -1
  110. package/src/binding/ask-dialog.js +17 -3
  111. package/src/binding/data-binder.js +5 -0
  112. package/src/binding/providers.js +1 -1
  113. package/src/binding/runtime.js +6 -3
  114. package/src/builder/chart-icon.js +2 -0
  115. package/src/builder/gramlot-builder.d.ts +3 -1
  116. package/src/builder/gramlot-builder.js +26 -9
  117. package/src/builder/source.js +12 -3
  118. package/src/builder/xml-recipe.js +144 -0
  119. package/src/chat-file-types.js +13 -0
  120. package/src/chat.js +23 -25
  121. package/src/commands.d.ts +1 -1
  122. package/src/commands.js +1 -1
  123. package/src/components/border-container.js +64 -6
  124. package/src/components/buttons.js +26 -31
  125. package/src/components/chart-geographic-options.js +1 -1
  126. package/src/components/chart-pane.js +5 -5
  127. package/src/components/chart-spatial-options.js +5 -1
  128. package/src/components/check-box-text-editor.js +1 -0
  129. package/src/components/check-box-text.js +2 -0
  130. package/src/components/choice-editor.js +3 -2
  131. package/src/components/choice-field-styles.js +9 -1
  132. package/src/components/code-mirror-engine.js +18 -5
  133. package/src/components/code-mirror-gramlot.js +85 -0
  134. package/src/components/code-mirror.js +3 -2
  135. package/src/components/drawing.js +43 -14
  136. package/src/components/editor-options.js +1 -1
  137. package/src/components/graphical-ranges.js +177 -0
  138. package/src/components/grid-footer.js +6 -1
  139. package/src/components/grid-styles.js +11 -2
  140. package/src/components/grid-tools.js +15 -7
  141. package/src/components/grid.js +69 -8
  142. package/src/components/inputs.js +93 -23
  143. package/src/components/jodit-engine.js +3 -1
  144. package/src/components/menus.js +83 -25
  145. package/src/components/numeric-range.js +39 -0
  146. package/src/components/selected-container.js +109 -63
  147. package/src/components/tab-bar.js +371 -0
  148. package/src/components/toolbar-icon-style.js +22 -0
  149. package/src/components/toolbar-overflow.js +74 -12
  150. package/src/components/tree.js +12 -3
  151. package/src/components/typst-preview.js +3 -3
  152. package/src/components/upload-blocks.js +88 -0
  153. package/src/components/upload-engine.js +21 -31
  154. package/src/components/uploader.js +168 -47
  155. package/src/gramlot.js +6 -3
  156. package/src/handlers/dom-handler.d.ts +2 -0
  157. package/src/handlers/dom-handler.js +9 -0
  158. package/src/handlers/rpc-handler.d.ts +1 -1
  159. package/src/handlers/rpc-handler.js +2 -0
  160. package/src/handlers/source-handler.d.ts +5 -3
  161. package/src/handlers/source-handler.js +31 -13
  162. package/src/icons.js +84 -0
  163. package/src/index-page-runtime.js +9 -2
  164. package/src/index-page-styles.js +1 -1
  165. package/src/index-theme.js +210 -3
  166. package/src/index.d.ts +5 -1
  167. package/src/index.js +5 -1
  168. package/src/libraries.js +1 -1
  169. package/src/page-store-connection.js +44 -7
  170. package/src/renderer/gramlot-renderer.js +4 -1
  171. package/src/server/chat-attachments.js +139 -0
  172. package/src/server/chat.d.ts +31 -0
  173. package/src/server/chat.js +157 -0
  174. package/src/server/database-selectors.d.ts +6 -0
  175. package/src/server/database-selectors.js +13 -0
  176. package/src/server/gramlot-file-server.js +6 -1
  177. package/src/server/gramlot-server.d.ts +19 -3
  178. package/src/server/gramlot-server.js +144 -18
  179. package/src/server/index-page.d.ts +10 -0
  180. package/src/server/index-page.js +73 -8
  181. package/src/server/index.d.ts +1 -1
  182. package/src/server/index.js +1 -1
  183. package/src/server/page-base.js +4 -0
  184. package/src/server/page-store.d.ts +21 -0
  185. package/src/server/page-store.js +219 -0
  186. package/src/server/page.d.ts +6 -1
  187. package/src/server/page.js +5 -1
  188. package/src/server/store-recipe.js +102 -0
  189. package/src/server/toolbar-icon-name.d.ts +76 -0
  190. package/src/server/upload.d.ts +2 -0
  191. package/src/server/upload.js +36 -18
  192. package/src/server/visual-editor.js +4 -2
  193. package/src/tree-store.d.ts +5 -1
  194. package/src/tree-store.js +11 -2
  195. package/src/view/choice-focus.js +17 -0
  196. package/src/view/html.js +21 -1
  197. package/src/view/menu.js +9 -3
  198. package/src/visual-editor.js +6 -2
  199. package/themes/gramlot-base/theme.css +53 -28
  200. package/themes/gramlot-compact/theme.css +2 -0
  201. package/themes/gramlot-index/icons/applications.svg +4 -0
  202. package/themes/gramlot-index/icons/archive.svg +4 -0
  203. package/themes/gramlot-index/icons/attachment.svg +4 -0
  204. package/themes/gramlot-index/icons/book.svg +4 -0
  205. package/themes/gramlot-index/icons/bookmark.svg +4 -0
  206. package/themes/gramlot-index/icons/calendar.svg +4 -0
  207. package/themes/gramlot-index/icons/cancel.svg +4 -0
  208. package/themes/gramlot-index/icons/chart.svg +4 -0
  209. package/themes/gramlot-index/icons/chat.svg +4 -0
  210. package/themes/gramlot-index/icons/clock.svg +4 -0
  211. package/themes/gramlot-index/icons/code.svg +4 -0
  212. package/themes/gramlot-index/icons/collapse.svg +4 -0
  213. package/themes/gramlot-index/icons/columns.svg +4 -0
  214. package/themes/gramlot-index/icons/confirm.svg +4 -0
  215. package/themes/gramlot-index/icons/copy.svg +4 -0
  216. package/themes/gramlot-index/icons/cut.svg +4 -0
  217. package/themes/gramlot-index/icons/database.svg +4 -0
  218. package/themes/gramlot-index/icons/delete.svg +4 -0
  219. package/themes/gramlot-index/icons/detach.svg +4 -0
  220. package/themes/gramlot-index/icons/document.svg +4 -0
  221. package/themes/gramlot-index/icons/download.svg +4 -0
  222. package/themes/gramlot-index/icons/duplicate.svg +4 -0
  223. package/themes/gramlot-index/icons/edit.svg +4 -0
  224. package/themes/gramlot-index/icons/expand.svg +4 -0
  225. package/themes/gramlot-index/icons/export.svg +4 -0
  226. package/themes/gramlot-index/icons/eye.svg +4 -0
  227. package/themes/gramlot-index/icons/filter.svg +4 -0
  228. package/themes/gramlot-index/icons/first.svg +4 -0
  229. package/themes/gramlot-index/icons/folder.svg +4 -0
  230. package/themes/gramlot-index/icons/group.svg +4 -0
  231. package/themes/gramlot-index/icons/help.svg +4 -0
  232. package/themes/gramlot-index/icons/home.svg +4 -0
  233. package/themes/gramlot-index/icons/import.svg +4 -0
  234. package/themes/gramlot-index/icons/info.svg +4 -0
  235. package/themes/gramlot-index/icons/inspector.svg +4 -0
  236. package/themes/gramlot-index/icons/last.svg +4 -0
  237. package/themes/gramlot-index/icons/layers.svg +4 -0
  238. package/themes/gramlot-index/icons/link.svg +4 -0
  239. package/themes/gramlot-index/icons/list.svg +4 -0
  240. package/themes/gramlot-index/icons/lock.svg +4 -0
  241. package/themes/gramlot-index/icons/logout.svg +4 -0
  242. package/themes/gramlot-index/icons/navigation.svg +4 -0
  243. package/themes/gramlot-index/icons/new.svg +4 -0
  244. package/themes/gramlot-index/icons/next.svg +4 -0
  245. package/themes/gramlot-index/icons/open.svg +4 -0
  246. package/themes/gramlot-index/icons/palette.svg +4 -0
  247. package/themes/gramlot-index/icons/panel-bottom.svg +4 -0
  248. package/themes/gramlot-index/icons/panel-left.svg +4 -0
  249. package/themes/gramlot-index/icons/panel-right.svg +4 -0
  250. package/themes/gramlot-index/icons/panel-top.svg +4 -0
  251. package/themes/gramlot-index/icons/paste.svg +4 -0
  252. package/themes/gramlot-index/icons/pause.svg +4 -0
  253. package/themes/gramlot-index/icons/pin.svg +4 -0
  254. package/themes/gramlot-index/icons/play.svg +4 -0
  255. package/themes/gramlot-index/icons/previous.svg +4 -0
  256. package/themes/gramlot-index/icons/print.svg +4 -0
  257. package/themes/gramlot-index/icons/redo.svg +4 -0
  258. package/themes/gramlot-index/icons/refresh.svg +4 -0
  259. package/themes/gramlot-index/icons/save.svg +4 -0
  260. package/themes/gramlot-index/icons/search.svg +4 -0
  261. package/themes/gramlot-index/icons/send.svg +4 -0
  262. package/themes/gramlot-index/icons/settings.svg +4 -0
  263. package/themes/gramlot-index/icons/sort-ascending.svg +4 -0
  264. package/themes/gramlot-index/icons/sort-descending.svg +4 -0
  265. package/themes/gramlot-index/icons/stop.svg +4 -0
  266. package/themes/gramlot-index/icons/table.svg +4 -0
  267. package/themes/gramlot-index/icons/tree.svg +4 -0
  268. package/themes/gramlot-index/icons/undo.svg +4 -0
  269. package/themes/gramlot-index/icons/unlock.svg +4 -0
  270. package/themes/gramlot-index/icons/upload.svg +4 -0
  271. package/themes/gramlot-index/icons/user.svg +4 -0
  272. package/themes/gramlot-index/icons/warning.svg +4 -0
  273. package/themes/gramlot-index/icons/zoom-in.svg +4 -0
  274. package/themes/gramlot-index/icons/zoom-out.svg +4 -0
  275. package/themes/gramlot-index/icons.json +76 -0
  276. package/themes/gramlot-index/theme.css +21 -0
  277. package/themes/joanna/theme.css +2 -0
  278. package/themes/mela/theme.css +2 -0
  279. package/themes/mimi/theme.css +2 -0
@@ -0,0 +1,219 @@
1
+ import {Bag} from '@genrojs/bag';
2
+ import {DATA_ROOT} from '@genrojs/builders';
3
+ import {fromTytx, toTytx} from '@genrojs/tytx';
4
+ import {GramlotBuilder} from '../builder/gramlot-builder.js';
5
+ import {StoreRecipe} from './store-recipe.js';
6
+
7
+ const mapping = value => value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype;
8
+ const intersects = (a,b) => a === b || a.startsWith(b + '.') || b.startsWith(a + '.');
9
+ function checkPath(path, relative = false) {
10
+ if (typeof path !== 'string' || !(relative && !path) && !/^[^.?#\[\]!:]+(?:\.[^.?#\[\]!:]+)*$/u.test(path)) {
11
+ throw new TypeError('A store path must contain dot-separated literal labels');
12
+ }
13
+ }
14
+
15
+ /** One live Data Bag and synchronous Source recipe per server page. */
16
+ export class PageStore {
17
+ #subscribers;
18
+ #bindings;
19
+ #clients;
20
+ #recipe;
21
+ #changes;
22
+ #currentChange;
23
+ #started;
24
+
25
+ constructor() {
26
+ this.builder = new GramlotBuilder();
27
+ this.data = this.builder.data;
28
+ this.#subscribers = new Map();
29
+ this.#bindings = new Map();
30
+ this.#clients = new Set();
31
+ this.#recipe = new StoreRecipe(this);
32
+ this.#changes = [];
33
+ this.#currentChange = null;
34
+ this.closed = false;
35
+ this.#started = false;
36
+ }
37
+
38
+ main(root) {}
39
+
40
+ start() {
41
+ this.#requireOpen();
42
+ if (this.#started) throw new Error('PageStore is already started');
43
+ this.#started = true;
44
+ try {
45
+ if (this.main.constructor.name === 'AsyncFunction' || this.main(this.builder.root)?.then) {
46
+ throw new TypeError('PageStore requires a synchronous main');
47
+ }
48
+ this.data.parent.subscribe('page-store', {any:event => this.#receive(event)});
49
+ this.#recipe.start();
50
+ } catch (error) { this.close(); throw error; }
51
+ }
52
+
53
+ sourceBuilder() {
54
+ if (!this._sourceBuilder) throw new Error('sourceBuilder requires a PageStore opened by GramlotServer');
55
+ return this._sourceBuilder();
56
+ }
57
+
58
+ sharedStore(key, factory) {
59
+ if (!this._sharedStore) throw new Error('sharedStore requires a PageStore opened by GramlotServer');
60
+ return this._sharedStore(key, factory);
61
+ }
62
+
63
+ bindPath(path, target, targetPath, {authorize, writable = false}) {
64
+ this.#requireOpen();
65
+ checkPath(path);
66
+ checkPath(targetPath);
67
+ if (!(target instanceof PageStore) || target === this || typeof authorize !== 'function') {
68
+ throw new TypeError('A binding requires another PageStore and an authorization function');
69
+ }
70
+ if (!authorize()) throw new Error('Shared path access denied');
71
+ if ([...this.#bindings.keys()].some(existing => intersects(existing, path))) throw new Error('Shared paths must not overlap');
72
+ const pending = [target], seen = new Set();
73
+ while (pending.length) {
74
+ const next = pending.pop();
75
+ if (next === this) throw new Error('Shared path cycles are not supported');
76
+ if (seen.has(next)) continue;
77
+ seen.add(next);
78
+ for (const entry of next.#bindings.values()) pending.push(entry.target);
79
+ }
80
+ const record = {page:this, path, target, targetPath, authorize, writable, id:'bound_' + crypto.randomUUID()};
81
+ this.#bindings.set(path, record);
82
+ target.#clients.add(record);
83
+ const receive = wire => {
84
+ if (this.closed || this.#bindings.get(path) !== record) return;
85
+ let allowed = false;
86
+ try { allowed = authorize(); } catch { /* A failed access check revokes the binding. */ }
87
+ if (!allowed) { this.unbindPath(path); return; }
88
+ const message = fromTytx(wire);
89
+ this.#apply(message.path ? path + '.' + message.path : path, message, null);
90
+ };
91
+ try { target.subscribe(record.id, targetPath, receive); }
92
+ catch (error) { this.unbindPath(path); throw error; }
93
+ return record.id;
94
+ }
95
+
96
+ isPathBound(path) { return this.#bindings.has(path); }
97
+
98
+ unbindPath(path) {
99
+ const record = this.#bindings.get(path);
100
+ if (!record) return;
101
+ this.#bindings.delete(path);
102
+ record.target.#clients.delete(record);
103
+ record.target.unsubscribe(record.id);
104
+ this.data.popNode(path);
105
+ }
106
+
107
+ unbindPaths() { for (const path of this.#bindings.keys()) this.unbindPath(path); }
108
+
109
+ subscribe(binderId, path, send) {
110
+ this.#requireOpen();
111
+ checkPath(path);
112
+ for (const record of this.#bindings.values()) {
113
+ if (intersects(path, record.path) && !record.authorize()) this.unbindPath(record.path);
114
+ }
115
+ if (this.#subscribers.has(binderId)) throw new Error('Binder is already registered');
116
+ this.#subscribers.set(binderId, {path, send});
117
+ try { send(this.#snapshot(binderId, path)); }
118
+ catch (error) { this.#subscribers.delete(binderId); throw error; }
119
+ }
120
+
121
+ #snapshot(binderId, path) {
122
+ const node = this.data.getNode(path);
123
+ return toTytx({op:'snapshot', binderId, path:'', exists:!!node, value:node?.value ?? null, attr:{...node?.attr}});
124
+ }
125
+
126
+ unsubscribe(binderId) { this.#subscribers.delete(binderId); }
127
+
128
+ change(binderId, message) {
129
+ this.#requireOpen();
130
+ const registration = this.#subscribers.get(binderId);
131
+ if (!registration) throw new Error('Unknown binder');
132
+ checkPath(message.path, true);
133
+ const path = message.path ? registration.path + '.' + message.path : registration.path;
134
+ for (const record of this.#bindings.values()) if (intersects(path, record.path)) {
135
+ if (!record.authorize()) { this.unbindPath(record.path); throw new Error('Shared path access denied'); }
136
+ if (!record.writable || path !== record.path && !path.startsWith(record.path + '.')) throw new Error('Shared path is read-only or an ancestor replacement was attempted');
137
+ record.target.change(record.id, {...message, path:path.slice(record.path.length + 1)});
138
+ record.target.#subscribers.get(record.id).send(record.target.#snapshot(record.id, record.targetPath));
139
+ return;
140
+ }
141
+ if (message.fired) throw new Error('Fired events are outside the PageStore contract');
142
+ if (!['del','ins','upd','upd_value','upd_attrs'].includes(message.evt)) throw new TypeError('Unsupported datachange event');
143
+ if (message.evt !== 'del' && !mapping(message.attr)) throw new TypeError('Change attributes must be a mapping');
144
+ this.#apply(path, message, binderId);
145
+ }
146
+
147
+ #apply(path, message, reason) {
148
+ if (message.evt === 'del' || message.op === 'snapshot' && !message.exists) this.data.popNode(path, reason);
149
+ else if (message.evt === 'upd_attrs') {
150
+ const node = this.data.getNode(path);
151
+ if (!node) throw new Error('Attribute target does not exist');
152
+ node.setAttr(message.attr, reason ?? true, false, false);
153
+ } else this.data.setItem(path, message.value, message.attr, message.index ?? '>', false, false, reason);
154
+ }
155
+
156
+ #receive(event) {
157
+ if (event.reason === 'autocreate' || event.pathlist?.[0] !== DATA_ROOT) return;
158
+ for (const node of Array.isArray(event.node) ? event.node : [event.node]) {
159
+ const parts = event.pathlist.slice(1);
160
+ if (['ins','del'].includes(event.evt)) parts.push(node.label);
161
+ const frozen = fromTytx(toTytx({value:node.value, attr:{...node.attr},
162
+ oldvalue:event.oldvalue ?? null, attrs_diff:event.attrs_diff ?? null}));
163
+ this.#changes.push({...frozen, path:parts.join('.'), event:{...event, node,
164
+ pathlist:[...event.pathlist], oldvalue:frozen.oldvalue, attrs_diff:frozen.attrs_diff}});
165
+ }
166
+ if (this.#currentChange) return;
167
+ try {
168
+ while (this.#changes.length) {
169
+ this.#currentChange = this.#changes.shift();
170
+ this.#broadcast(this.#currentChange);
171
+ this.#recipe.receive(this.#currentChange);
172
+ }
173
+ } finally { this.#currentChange = null; this.#changes.length = 0; }
174
+ }
175
+
176
+ #broadcast({path, value, attr, event}) {
177
+ const origin = this.#subscribers.has(event.reason) ? event.reason : null;
178
+ for (const [binderId, registration] of [...this.#subscribers]) {
179
+ const base = registration.path;
180
+ if (origin === binderId || this.#subscribers.get(binderId) !== registration || !intersects(path, base)) continue;
181
+ const message = {op:'change', binderId, origin};
182
+ if (path === base || path.startsWith(base + '.')) {
183
+ Object.assign(message, {path:path.slice(base.length + 1), evt:event.evt, index:event.ind ?? null, value, attr});
184
+ } else {
185
+ if (event.evt === 'upd_attrs') continue;
186
+ const child = event.evt !== 'del' && value instanceof Bag ? value.getNode(base.slice(path.length + 1)) : null;
187
+ Object.assign(message, {path:'', evt:child ? 'upd_value' : 'del', index:null,
188
+ value:child?.value ?? null, attr:{...child?.attr}});
189
+ }
190
+ registration.send(toTytx(message));
191
+ }
192
+ }
193
+
194
+ publish(topic, payload = {}, {metadata = null, pageId = null} = {}) {
195
+ this.#requireOpen();
196
+ if (typeof topic !== 'string' || !topic || !mapping(payload)) throw new TypeError('Publication requires a topic and named data');
197
+ if (pageId !== null) {
198
+ if (metadata !== null) throw new TypeError('Browser publication does not accept server subscription metadata');
199
+ if (!this._publishToPage) throw new Error('PageStore is not attached to a server');
200
+ return this._publishToPage(topic, payload, pageId);
201
+ }
202
+ if (!this.#recipe.active) throw new Error('PageStore recipe is not active');
203
+ this.#recipe.publish(topic, payload, metadata);
204
+ }
205
+
206
+ #requireOpen() { if (this.closed) throw new Error('PageStore is closed'); }
207
+
208
+ close() {
209
+ if (this.closed) return;
210
+ for (const record of this.#clients) record.page.unbindPath(record.path);
211
+ this.unbindPaths();
212
+ this.closed = true;
213
+ this._publishToPage = this._sourceBuilder = this._sharedStore = null;
214
+ this.#recipe.close();
215
+ this.data.parent.unsubscribe('page-store', {any:true});
216
+ this.#subscribers.clear();
217
+ this.#changes.length = 0;
218
+ }
219
+ }
@@ -49,6 +49,7 @@ export class Exposed {
49
49
 
50
50
  /** Server-side page base; unrelated to browser view components. */
51
51
  export class Page extends Exposed {
52
+ addIcon(name: string, asset: string): void;
52
53
  /** Trusted host identity of the current invocation, or null. */
53
54
  readonly requestIdentity: unknown;
54
55
  /** The title of the page document. */
@@ -92,8 +93,12 @@ export {codeMirrorWithOptions} from '../components/editor-options.js';
92
93
  export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "../filesystem.js";
93
94
  export {IdeLogic} from "../ide-runtime.js";
94
95
 
95
- export {TreeStore, TreeStoreResolver, TreeStoreLogic} from '../tree-store.js';
96
+ export {treeStore, TreeStore, TreeStoreResolver, TreeStoreLogic} from '../tree-store.js';
96
97
  export {UploadTarget} from './upload.js';
97
98
 
98
99
  export {VisualEditor} from './visual-editor.js';
99
100
  export {SourceImport} from './source-import.js';
101
+
102
+ export {PageStore} from './page-store.js';
103
+
104
+ export {ChatPane, ChatPageStore} from './chat.js';
@@ -14,8 +14,12 @@ export {codeMirrorWithOptions} from '../components/editor-options.js';
14
14
  export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "../filesystem.js";
15
15
  export {IdeLogic} from "../ide-runtime.js";
16
16
 
17
- export {TreeStore, TreeStoreResolver, TreeStoreLogic} from '../tree-store.js';
17
+ export {treeStore, TreeStore, TreeStoreResolver, TreeStoreLogic} from '../tree-store.js';
18
18
  export {UploadTarget} from './upload.js';
19
19
 
20
20
  export {VisualEditor} from './visual-editor.js';
21
21
  export {SourceImport} from './source-import.js';
22
+
23
+ export {PageStore} from './page-store.js';
24
+
25
+ export {ChatPane, ChatPageStore} from './chat.js';
@@ -0,0 +1,102 @@
1
+ import {DataRouter} from '../binding/router.js';
2
+
3
+ /** Run named server providers synchronously on the same Data paths as browser providers. */
4
+ export class StoreRecipe {
5
+ constructor(store) {
6
+ this.store = store;
7
+ this.router = new DataRouter(this);
8
+ this.subscriptions = new Map();
9
+ this.registrations = [];
10
+ this.active = false;
11
+ }
12
+
13
+ start() {
14
+ const nodes = this.store.builder.source.getNodes();
15
+ const starters = [];
16
+ for (const node of nodes) {
17
+ if (node.nodeTag === 'dataSetter') continue;
18
+ if (!['dataController', 'dataFormula'].includes(node.nodeTag)) {
19
+ throw new TypeError('Unsupported PageStore recipe element: ' + node.nodeTag);
20
+ }
21
+ const {func, script, formula, _onStart, _userChanges} = node.attr;
22
+ if (typeof func !== 'string' || !func || func.startsWith('_') || func.includes('.') ||
23
+ typeof this.store[func] !== 'function' || this.store[func].constructor.name === 'AsyncFunction') {
24
+ throw new TypeError('PageStore providers require a synchronous named method');
25
+ }
26
+ if (script != null || formula != null) throw new TypeError('PageStore providers require a named func');
27
+ for (const key of Object.keys(node.attr)) {
28
+ if (key.startsWith('_') && !['_meta','_onStart','_userChanges'].includes(key) || key.startsWith('selfsubscribe_')) {
29
+ throw new TypeError('Unsupported PageStore provider control: ' + key);
30
+ }
31
+ if (key === 'subscribe_') throw new TypeError('A subscription topic must be nonempty');
32
+ if (key.startsWith('subscribe_')) {
33
+ const topic = key.slice('subscribe_'.length);
34
+ if (!this.subscriptions.has(topic)) this.subscriptions.set(topic, []);
35
+ this.subscriptions.get(topic).push(node);
36
+ }
37
+ }
38
+ for (const flag of [_onStart, _userChanges]) {
39
+ if (flag !== undefined && typeof flag !== 'boolean') throw new TypeError('PageStore controls require literal booleans');
40
+ }
41
+ for (const [parameter, pointer] of node.pointers()) {
42
+ if (parameter === 'datapath') continue;
43
+ const [path, attr = null] = node.absDatapath(pointer).split('?');
44
+ const recipient = {receive:change => {
45
+ if (_userChanges && change.level !== 'node') return;
46
+ const context = {...change, value:this.currentChange.value, attr:this.currentChange.attr,
47
+ registration:{path, attr, parameter, sourceNode:node}};
48
+ this.execute(node, change.level, context);
49
+ }};
50
+ this.registrations.push(this.router.register({path, attr, recipient}));
51
+ }
52
+ if (_onStart) starters.push(node);
53
+ }
54
+ for (const node of nodes) if (node.nodeTag === 'dataSetter') {
55
+ this.store.data.setItem(node.absDatapath(node.attr.destination_path), node.attr.value);
56
+ }
57
+ this.active = true;
58
+ for (const node of starters) this.execute(node, 'start');
59
+ }
60
+
61
+ receive(change) {
62
+ if (!this.active) return;
63
+ this.currentChange = change;
64
+ try { this.router.deliver(change.event); }
65
+ finally { this.currentChange = null; }
66
+ }
67
+
68
+ publish(topic, payload, metadata) {
69
+ for (const node of this.subscriptions.get(topic) ?? []) {
70
+ if (!node.attr._userChanges || topic === 'node') this.execute(node, topic, null, payload, metadata);
71
+ }
72
+ }
73
+
74
+ execute(node, reason, change = null, payload = null, metadata = null) {
75
+ if (this.store.closed) return;
76
+ const [, resolved] = node.rootBuilder.runtimeValues(node);
77
+ const args = {...payload};
78
+ for (const [name, value] of Object.entries(resolved)) {
79
+ if (!name.startsWith('_') && !name.startsWith('subscribe_') &&
80
+ !['func','result_path','formula','script','datapath'].includes(name)) args[name] = value;
81
+ }
82
+ args._node = change?.node ?? null;
83
+ args._reason = reason;
84
+ args._triggerpars = {kw:change, trigger_reason:reason};
85
+ if (payload !== null) {
86
+ args._subscription_kwargs = payload;
87
+ args._subscription_metadata = {...metadata};
88
+ }
89
+ const method = this.store[node.attr.func];
90
+ const formula = node.nodeTag === 'dataFormula';
91
+ const result = formula ? method.call(this.store, args) : method.call(this.store, node, args);
92
+ if (result && typeof result.then === 'function') throw new TypeError('PageStore recipes must be synchronous');
93
+ if (formula && !this.store.closed) node.SET(node.attr.result_path, result);
94
+ }
95
+
96
+ close() {
97
+ this.active = false;
98
+ for (const registration of this.registrations) registration.close();
99
+ this.registrations.length = 0;
100
+ this.subscriptions.clear();
101
+ }
102
+ }
@@ -0,0 +1,76 @@
1
+ // Generated from assets/icons/.
2
+ export type ToolbarIconName =
3
+ | 'applications'
4
+ | 'archive'
5
+ | 'attachment'
6
+ | 'book'
7
+ | 'bookmark'
8
+ | 'calendar'
9
+ | 'cancel'
10
+ | 'chart'
11
+ | 'chat'
12
+ | 'clock'
13
+ | 'code'
14
+ | 'collapse'
15
+ | 'columns'
16
+ | 'confirm'
17
+ | 'copy'
18
+ | 'cut'
19
+ | 'database'
20
+ | 'delete'
21
+ | 'detach'
22
+ | 'document'
23
+ | 'download'
24
+ | 'duplicate'
25
+ | 'edit'
26
+ | 'expand'
27
+ | 'export'
28
+ | 'eye'
29
+ | 'filter'
30
+ | 'first'
31
+ | 'folder'
32
+ | 'group'
33
+ | 'help'
34
+ | 'home'
35
+ | 'import'
36
+ | 'info'
37
+ | 'inspector'
38
+ | 'last'
39
+ | 'layers'
40
+ | 'link'
41
+ | 'list'
42
+ | 'lock'
43
+ | 'logout'
44
+ | 'navigation'
45
+ | 'new'
46
+ | 'next'
47
+ | 'open'
48
+ | 'palette'
49
+ | 'panel-bottom'
50
+ | 'panel-left'
51
+ | 'panel-right'
52
+ | 'panel-top'
53
+ | 'paste'
54
+ | 'pause'
55
+ | 'pin'
56
+ | 'play'
57
+ | 'previous'
58
+ | 'print'
59
+ | 'redo'
60
+ | 'refresh'
61
+ | 'save'
62
+ | 'search'
63
+ | 'send'
64
+ | 'settings'
65
+ | 'sort-ascending'
66
+ | 'sort-descending'
67
+ | 'stop'
68
+ | 'table'
69
+ | 'tree'
70
+ | 'undo'
71
+ | 'unlock'
72
+ | 'upload'
73
+ | 'user'
74
+ | 'warning'
75
+ | 'zoom-in'
76
+ | 'zoom-out';
@@ -13,6 +13,8 @@ export type UploadOutcome = {state: 'completed'; result: unknown} | {state: 'uns
13
13
  export class UploadTransfer {
14
14
  constructor(target: UploadTarget, size: number);
15
15
  readonly size: number;
16
+ block(offset: number, chunk: Uint8Array): Promise<UploadOutcome | {state:'pending'; offset:number}>;
17
+ checkpoint(): Promise<UploadOutcome | {state:'pending'; offset:number}>;
16
18
  write(chunk: Uint8Array): Promise<void>;
17
19
  finish(): Promise<UploadOutcome>;
18
20
  abort(): Promise<UploadOutcome>;
@@ -47,23 +47,39 @@ export class UploadTransfer {
47
47
  }
48
48
  #record(outcome) { this.#state = checkedOutcome(outcome); if (this.#state.state === 'completed') this.onOutcome?.(); return {...this.#state}; }
49
49
  #terminal() { return ['completed', 'unsaved'].includes(this.#state.state); }
50
- write(chunk) {
50
+ write(chunk) { return this.#run(() => this.#write(chunk)); }
51
+ async #write(chunk) {
52
+ if (!(chunk instanceof Uint8Array)) throw new TypeError('Upload chunks must be Uint8Array');
53
+ if (this.#state.state !== 'pending' || this.#sealed) throw new Error('Upload is not writable');
54
+ if (this.#received + chunk.byteLength > this.size) throw new RangeError('Upload exceeds declared size');
55
+ try { await this.target.write(chunk); }
56
+ catch (error) { this.#state = {state:'uncertain'}; this.#sealed = true; this.onOutcome?.(); throw error; }
57
+ this.#received += chunk.byteLength;
58
+ }
59
+ finish() { return this.#run(() => this.#finish()); }
60
+ async #finish() {
61
+ if (this.#state.state !== 'pending' || this.#sealed) throw new Error('Upload cannot be finished');
62
+ if (this.#received !== this.size) throw new RangeError('Upload size does not match declaration');
63
+ this.#sealed = true;
64
+ try { return this.#record(await this.target.finish()); }
65
+ catch (error) { this.#state = {state:'uncertain'}; this.onOutcome?.(); throw error; }
66
+ }
67
+ get writable() { return !this.#sealed && this.#state.state === 'pending'; }
68
+ block(offset, bytes) {
51
69
  return this.#run(async () => {
52
- if (!(chunk instanceof Uint8Array)) throw new TypeError('Upload chunks must be Uint8Array');
53
- if (this.#state.state !== 'pending' || this.#sealed) throw new Error('Upload is not writable');
54
- if (this.#received + chunk.byteLength > this.size) throw new RangeError('Upload exceeds declared size');
55
- try { await this.target.write(chunk); }
56
- catch (error) { this.#state = {state:'uncertain'}; this.#sealed = true; this.onOutcome?.(); throw error; }
57
- this.#received += chunk.byteLength;
70
+ if (offset !== this.#received || !(bytes instanceof Uint8Array) ||
71
+ bytes.byteLength !== Math.min(256 * 1024, this.size - offset)) {
72
+ throw new RangeError('Incorrect upload offset or block length');
73
+ }
74
+ await this.#write(bytes);
75
+ return this.#received === this.size ? this.#finish() : {state:'pending', offset:this.#received};
58
76
  });
59
77
  }
60
- finish() {
78
+ checkpoint() {
61
79
  return this.#run(async () => {
62
- if (this.#state.state !== 'pending' || this.#sealed) throw new Error('Upload cannot be finished');
63
- if (this.#received !== this.size) throw new RangeError('Upload size does not match declaration');
64
- this.#sealed = true;
65
- try { return this.#record(await this.target.finish()); }
66
- catch (error) { this.#state = {state:'uncertain'}; this.onOutcome?.(); throw error; }
80
+ if (this.writable) return {state:'pending', offset:this.#received};
81
+ if (this.#terminal()) return {...this.#state};
82
+ return this.#record(await this.target.status());
67
83
  });
68
84
  }
69
85
  abort() {
@@ -83,7 +99,7 @@ export class UploadTransfer {
83
99
  }
84
100
  }
85
101
 
86
- /** Private server registry: grants are one-use and bound to a page and its owner. */
102
+ /** Private server registry: grants are transfer-scoped and bound to a page and its owner. */
87
103
  export class UploadRegistry {
88
104
  constructor({capacity = 1000, grantTtl = 300000, retention = 1800000} = {}) {
89
105
  this.records = new Map();
@@ -94,8 +110,10 @@ export class UploadRegistry {
94
110
  for (const [id, record] of this.records) {
95
111
  if (record.expires <= now || (!record.started && record.grantExpires <= now)) {
96
112
  const expires = record.expires;
97
- try { await record.transfer.abort(); } catch {} // The outcome can remain uncertain.
98
- if (record.expires === expires) this.records.delete(id);
113
+ try {
114
+ const outcome = await record.transfer.abort();
115
+ if (record.expires === expires && ['completed','unsaved'].includes(outcome.state)) this.records.delete(id);
116
+ } catch { /* Preserve the record until the destination can confirm its outcome. */ }
99
117
  }
100
118
  }
101
119
  }
@@ -120,11 +138,11 @@ export class UploadRegistry {
120
138
  }
121
139
  admit(transferId, pageId, owner, authorization) {
122
140
  const record = this.record(transferId, pageId, owner);
123
- if (record.started || record.grantExpires <= performance.now() || record.authorization !== authorization) {
141
+ if (!record.transfer.writable || record.expires <= performance.now() ||
142
+ (!record.started && record.grantExpires <= performance.now()) || record.authorization !== authorization) {
124
143
  throw new Error('Invalid upload authorization');
125
144
  }
126
145
  record.started = true;
127
- record.authorization = null;
128
146
  record.expires = performance.now() + this.retention;
129
147
  return record.transfer;
130
148
  }
@@ -1,6 +1,5 @@
1
1
  import {validateCatalogue} from '../visual-editor-catalogue.js';
2
2
  import {Bag} from '@genrojs/bag';
3
- import {sourceTarget} from '@genrojs/builders';
4
3
  import {toolIcons} from '../visual-editor-icons.js';
5
4
 
6
5
  export class VisualEditor {
@@ -59,7 +58,10 @@ export class VisualEditor {
59
58
  const source = sourceStack.contentPane({node_label:'source', region:'center', title:'Source', detachable:true,
60
59
  display:'flex', flex_direction:'column', min_height:0, overflow:'hidden'});
61
60
  this.search(source, 'sourceSearch', 'Search Source');
62
- source.tree({storepath:`*S.${sourceTarget(pane).fullpath.split('.').slice(1).join('.')}.layout.center.design`, hideValues:true, getLabel:'sourceLabel', searchText:'^.sourceSearch', selectedPath:'^.selection',
61
+ // XML recipes are composed in their own Source Bag before the page mounts them.
62
+ const documentRoot = pane.parentBag.root;
63
+ const sourceRoot = documentRoot === pane.builder.source.root ? pane.builder.source : documentRoot;
64
+ source.tree({storepath:`*S.${sourceRoot.relativePath(pane)}.layout.center.design`, hideValues:true, getLabel:'sourceLabel', searchText:'^.sourceSearch', selectedPath:'^.selection',
63
65
  draggable:true, dragTags:`${editorId}Source`, effectAllowed:'all', onDrag:'prepareSource', dropTarget:true,
64
66
  dropTypes:'app/widget,app/source,app/clipboard', dropTags:`widget,${editorId}Source,${editorId}Clipboard`, dropEffect:'^.sourceDropEffect', onDrop:'dropNode',
65
67
  id:`${editorId}-source-tree`, class:'visual-source-tree', flex:1, min_height:0});
@@ -1,8 +1,12 @@
1
1
  import {BagResolver} from '@genrojs/bag';
2
2
  import type {Bag, BagNode} from '@genrojs/bag';
3
- import type {SourceBagNode} from '@genrojs/builders';
3
+ import type {SourceBag, SourceBagNode} from '@genrojs/builders';
4
4
  import type {Gramlot} from './gramlot.js';
5
5
 
6
+ export function treeStore(parent: SourceBag | SourceBagNode, options: {
7
+ datapath: string; method: string; params?: Record<string, unknown>;
8
+ treeAttributes?: Record<string, unknown>; [key: string]: unknown;
9
+ }): SourceBagNode;
6
10
  export class TreeStoreResolver extends BagResolver {
7
11
  constructor(options?: {readOnly?: boolean});
8
12
  bind(store: TreeStore, path: string): void;
package/src/tree-store.js CHANGED
@@ -3,6 +3,15 @@ import {fromTytx, toTytx} from '@genrojs/tytx';
3
3
  import {GramlotBuilder} from './builder/gramlot-builder.js';
4
4
  import {randomId} from './random-id.js';
5
5
 
6
+ /** Compose the native tree and its lazy root loader in one Data scope. */
7
+ export function treeStore(parent, {datapath, method, params = {}, treeAttributes = {}, ...attributes}) {
8
+ const pane = parent.div({datapath, ...attributes});
9
+ pane.dataSetter({destination_path: '.store', value: new Bag()});
10
+ pane.dataController({func: 'treeStoreMount', storeMethod: method, storeParams: params, _onBuilt: true});
11
+ pane.tree({storepath: '.store', hideValues: true, ...treeAttributes});
12
+ return pane;
13
+ }
14
+
6
15
  /** A branch whose loader is supplied by its owning TreeStore after transport. */
7
16
  export class TreeStoreResolver extends BagResolver {
8
17
  constructor({readOnly = false} = {}) {
@@ -84,9 +93,9 @@ export class TreeStore {
84
93
  export class TreeStoreLogic {
85
94
  treeFavoriteActionState(node, {item, state}) {
86
95
  const hidden = item.resolver !== null || item.staticValue instanceof Bag;
87
- if (item.getAttr('favoriteEntry') === true) return {hidden: false, icon: '✕', title: 'Remove favorite'};
96
+ if (item.getAttr('favoriteEntry') === true) return {hidden: false, icon: 'cancel', title: 'Remove favorite'};
88
97
  const saved = !hidden && state.getNodes().some(favorite => favorite.getAttr('fieldpath') === item.getAttr('fieldpath'));
89
- return {hidden, icon: saved ? '♥' : '♡', title: saved ? 'Remove favorite' : 'Add favorite'};
98
+ return {hidden, icon: saved ? 'bookmark' : 'bookmark', title: saved ? 'Remove favorite' : 'Add favorite'};
90
99
  }
91
100
 
92
101
  treeFavoritesMount(node, {favoriteKey}) {
@@ -0,0 +1,17 @@
1
+ // Editable inputs can match :focus-visible after a mouse click. Keep the ring
2
+ // on the field when keyboard interaction resumes, including on its arrow.
3
+ export function trackChoiceFocus(field) {
4
+ const pointer = () => field.classList.add('pointer-focus');
5
+ const keyboard = () => field.classList.remove('pointer-focus');
6
+ const leave = event => {
7
+ if (!field.contains(event.relatedTarget)) field.classList.remove('pointer-focus');
8
+ };
9
+ field.addEventListener('pointerdown', pointer);
10
+ field.addEventListener('keydown', keyboard);
11
+ field.addEventListener('focusout', leave);
12
+ return () => {
13
+ field.removeEventListener('pointerdown', pointer);
14
+ field.removeEventListener('keydown', keyboard);
15
+ field.removeEventListener('focusout', leave);
16
+ };
17
+ }