@gramlot/gramlot 0.3.20 → 0.3.26

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 (172) hide show
  1. package/dist/components/chat/__init__.py +13 -0
  2. package/dist/components/chat/assets/collection.js +9 -0
  3. package/dist/components/chat/assets/index.js +3 -0
  4. package/dist/components/chat/commands.json +7 -0
  5. package/dist/components/chat/grammar.json +48 -0
  6. package/dist/components/editors/__init__.py +2 -2
  7. package/dist/components/editors/assets/collection.js +26 -120912
  8. package/dist/components/editors/assets/index.js +2 -1
  9. package/dist/components/editors/assets/modules/chunk-74O4WKIV.js +1 -0
  10. package/dist/components/editors/assets/modules/chunk-AU4HFPIY.js +1 -0
  11. package/dist/components/editors/assets/modules/chunk-YSGSC7KM.js +1 -0
  12. package/dist/components/editors/assets/modules/code-mirror-engine-RWQTR5G5.js +92 -0
  13. package/dist/components/editors/assets/modules/jodit-engine-MTSA4GZ3.js +1853 -0
  14. package/dist/components/editors/assets/modules/markdown-document-VFC23FAY.js +1 -0
  15. package/dist/components/editors/assets/modules/prose-mirror-engine-GMZPJPNN.js +24 -0
  16. package/dist/components/grid/__init__.py +14 -0
  17. package/dist/components/grid/assets/collection.js +262 -0
  18. package/dist/components/grid/assets/index.js +3 -0
  19. package/dist/components/grid/commands.json +7 -0
  20. package/dist/components/grid/grammar.json +770 -0
  21. package/dist/components/grouplets/__init__.py +13 -0
  22. package/dist/components/grouplets/assets/collection.js +185 -0
  23. package/dist/components/grouplets/assets/index.js +3 -0
  24. package/dist/components/grouplets/assets/modules/chunk-BLMUM44K.js +1 -0
  25. package/dist/components/grouplets/assets/modules/markdown-document-YWNZTOKK.js +1 -0
  26. package/dist/components/grouplets/commands.json +7 -0
  27. package/dist/components/grouplets/grammar.json +355 -0
  28. package/dist/components/inputs/__init__.py +1 -1
  29. package/dist/components/inputs/assets/collection.js +65 -24511
  30. package/dist/components/inputs/assets/index.js +2 -1
  31. package/dist/components/inputs/grammar.json +278 -6
  32. package/dist/components/layout/__init__.py +1 -1
  33. package/dist/components/layout/assets/collection.js +68 -26732
  34. package/dist/components/layout/assets/index.js +2 -1
  35. package/dist/components/layout/grammar.json +39 -0
  36. package/dist/components/selection_trial/__init__.py +13 -0
  37. package/dist/components/selection_trial/assets/collection.js +4 -0
  38. package/dist/components/selection_trial/assets/index.js +3 -0
  39. package/dist/components/selection_trial/commands.json +7 -0
  40. package/dist/components/selection_trial/grammar.json +587 -0
  41. package/dist/components/tree/__init__.py +1 -1
  42. package/dist/components/tree/assets/collection.js +27 -24612
  43. package/dist/components/tree/assets/index.js +2 -1
  44. package/dist/components/tree/grammar.json +341 -1
  45. package/dist/components/typst/__init__.py +2 -2
  46. package/dist/components/typst/assets/collection.js +12 -4
  47. package/dist/components/typst/assets/index.js +2 -1
  48. package/dist/components/typst/assets/modules/chunk-BLMUM44K.js +1 -0
  49. package/dist/components/typst/assets/modules/typst-client-JHC6UVYG.js +4 -0
  50. package/dist/components/uploads/__init__.py +13 -0
  51. package/dist/components/uploads/assets/collection.js +67 -0
  52. package/dist/components/uploads/assets/index.js +3 -0
  53. package/dist/components/uploads/commands.json +7 -0
  54. package/dist/components/uploads/grammar.json +789 -0
  55. package/dist/gramlot-markdown.js +16 -0
  56. package/dist/gramlot.js +6861 -1395
  57. package/dist/gramlot.min.js +1595 -46
  58. package/dist/runtime-notices.json +28 -0
  59. package/package.json +10 -3
  60. package/src/binding/grid-struct.js +63 -0
  61. package/src/binding/installation.js +5 -2
  62. package/src/binding/providers.js +4 -1
  63. package/src/binding/remote-resolver.js +1 -8
  64. package/src/binding/runtime.js +90 -6
  65. package/src/binding/validation-context.js +40 -0
  66. package/src/browser-camera-provider.d.ts +8 -0
  67. package/src/browser-camera-provider.js +104 -0
  68. package/src/builder/gramlot-builder.d.ts +5 -0
  69. package/src/builder/gramlot-builder.js +16 -0
  70. package/src/builder/source.js +34 -0
  71. package/src/chat.d.ts +12 -0
  72. package/src/chat.js +198 -0
  73. package/src/collection-artifact.js +1 -1
  74. package/src/components/attachment-manager.js +160 -0
  75. package/src/components/bases.js +19 -6
  76. package/src/components/border-container.js +86 -8
  77. package/src/components/buttons.js +11 -1
  78. package/src/components/chat.js +28 -0
  79. package/src/components/choice-editor.js +106 -38
  80. package/src/components/database-choices.js +133 -9
  81. package/src/components/editing-status-styles.js +26 -0
  82. package/src/components/editing-status.js +115 -0
  83. package/src/components/field-state.js +42 -54
  84. package/src/components/grid-cell-editor.js +181 -0
  85. package/src/components/grid-grammar.json +258 -0
  86. package/src/components/grid-header-interaction.js +196 -0
  87. package/src/components/grid-row-interaction.js +184 -0
  88. package/src/components/grid-row-layout.js +96 -0
  89. package/src/components/grid-rows.js +28 -0
  90. package/src/components/grid-search-box.js +142 -0
  91. package/src/components/grid-search-styles.js +42 -0
  92. package/src/components/grid-search.js +37 -0
  93. package/src/components/grid-structure.js +85 -0
  94. package/src/components/grid-styles.js +201 -0
  95. package/src/components/grid.js +666 -0
  96. package/src/components/grouplet-card.js +445 -0
  97. package/src/components/grouplets.js +17 -0
  98. package/src/components/html-preview.js +17 -4
  99. package/src/components/image-crop-dialog.js +272 -0
  100. package/src/components/image-crop.js +79 -0
  101. package/src/components/image-uploader.js +252 -0
  102. package/src/components/inputs.js +2 -1
  103. package/src/components/jodit-engine.js +1 -1
  104. package/src/components/markdown-document.js +2 -2
  105. package/src/components/menus.js +1 -0
  106. package/src/components/registry.js +4 -3
  107. package/src/components/screen-capture.js +43 -0
  108. package/src/components/selected-container.js +6 -2
  109. package/src/components/tray-pane.js +32 -4
  110. package/src/components/tree.js +416 -17
  111. package/src/components/typed-editor.js +23 -11
  112. package/src/components/upload-engine.js +156 -0
  113. package/src/components/uploader.js +208 -0
  114. package/src/components/uploads.js +7 -0
  115. package/src/editing-session.d.ts +25 -0
  116. package/src/editing-session.js +396 -0
  117. package/src/experiments/selection-transfer.js +78 -0
  118. package/src/experiments/selection-trial.js +64 -0
  119. package/src/experiments/window-selection.js +124 -0
  120. package/src/format.js +22 -7
  121. package/src/gramlot.d.ts +2 -0
  122. package/src/gramlot.js +2 -1
  123. package/src/handlers/device-handler.d.ts +2 -1
  124. package/src/handlers/rpc-handler.d.ts +3 -3
  125. package/src/index-page-styles.js +2 -1
  126. package/src/index.d.ts +16 -1
  127. package/src/index.js +16 -1
  128. package/src/inout.d.ts +2 -0
  129. package/src/inout.js +6 -0
  130. package/src/markdown-loader.d.ts +1 -0
  131. package/src/markdown-loader.js +4 -0
  132. package/src/markdown.d.ts +1 -0
  133. package/src/markdown.js +2 -0
  134. package/src/media-frame.js +9 -0
  135. package/src/renderer/attributes.js +7 -1
  136. package/src/renderer/gramlot-renderer.d.ts +2 -0
  137. package/src/renderer/gramlot-renderer.js +39 -3
  138. package/src/server/assets.d.ts +1 -1
  139. package/src/server/assets.js +1 -1
  140. package/src/server/gramlot-server.d.ts +10 -1
  141. package/src/server/gramlot-server.js +96 -8
  142. package/src/server/index.d.ts +2 -0
  143. package/src/server/index.js +2 -0
  144. package/src/server/page-base.js +17 -4
  145. package/src/server/page.d.ts +13 -1
  146. package/src/server/page.js +6 -0
  147. package/src/server/source-import.d.ts +2 -0
  148. package/src/server/source-import.js +31 -0
  149. package/src/server/upload.d.ts +20 -0
  150. package/src/server/upload.js +142 -0
  151. package/src/server/visual-editor.d.ts +11 -0
  152. package/src/server/visual-editor.js +149 -0
  153. package/src/source-picker.d.ts +9 -0
  154. package/src/source-picker.js +57 -0
  155. package/src/stores/bag-collection-store.js +51 -0
  156. package/src/stores/bag-collection-view.js +127 -0
  157. package/src/stores/collection-filter.js +77 -0
  158. package/src/tree-store.d.ts +24 -0
  159. package/src/tree-store.js +193 -0
  160. package/src/validation-run.js +50 -0
  161. package/src/view/button.js +8 -5
  162. package/src/view/controls.js +31 -32
  163. package/src/view/grouplet-card.js +54 -0
  164. package/src/view/html.js +29 -4
  165. package/src/view/labelbox.js +7 -3
  166. package/src/visual-editor-icons.js +10 -0
  167. package/src/visual-editor.d.ts +56 -0
  168. package/src/visual-editor.js +844 -0
  169. package/themes/gramlot-base/theme.css +127 -0
  170. package/themes/gramlot-compact/theme.css +8 -0
  171. package/themes/joanna/theme.css +8 -0
  172. package/themes/mimi/theme.css +8 -0
@@ -0,0 +1,181 @@
1
+ import {sourceTarget} from '@gramlot/gramlot';
2
+
3
+ /** Grid navigation and temporary Source controls; all editing registers belong to the shared session. */
4
+ export class GridCellEditor {
5
+ constructor(grid, context, cellFor, store, renderedRows, navigate, focusField) {
6
+ this.grid = grid;
7
+ this.context = context;
8
+ this.cellFor = cellFor;
9
+ this.store = store;
10
+ this.renderedRows = renderedRows;
11
+ this.navigate = navigate;
12
+ this.focusField = focusField;
13
+ this.retained = new Map();
14
+ this.declarations = new Map();
15
+ this.layer = grid.ownerDocument.createElement('div');
16
+ this.layer.className = 'cell-editor'; this.layer.hidden = true;
17
+ const slot = grid.ownerDocument.createElement('slot'); slot.name = 'cell-editor'; this.layer.append(slot);
18
+ grid.control.append(this.layer);
19
+ this.keydown = event => {
20
+ if (!this.active || event.isComposing) return;
21
+ if (event.key === 'Escape') {
22
+ event.preventDefault();
23
+ const cell = this.cellFor(this.active.row,this.active.column.field);
24
+ this.session.clearDraft(this.active.source);
25
+ this.close(); cell?.focus({preventScroll:true});
26
+ } else if (event.key === 'Enter' || event.key === 'Tab') {
27
+ if (event.key === 'Enter' && this.active.element.control.localName === 'textarea' && !event.ctrlKey && !event.metaKey) return;
28
+ event.preventDefault();
29
+ const active = this.active;
30
+ this.session.commitField(active.path);
31
+ const cell = this.cellFor(active.row,active.column.field);
32
+ this.close(); cell?.focus({preventScroll:true});
33
+ if (event.key === 'Tab') this.navigate(active.row,active.column.field,event.shiftKey ? -1 : 1);
34
+ }
35
+ };
36
+ this.arrowKeydown = event => {
37
+ if (!this.active || event.isComposing || !['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(event.key) || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
38
+ const control = this.active.element.control;
39
+ if (control.localName === 'textarea' || ['range','date','time','datetime-local'].includes(control.type)) return;
40
+ // Composite controls own their popup arrows; date/time fields own their editing keys.
41
+ if (['gramlot-date-text-box','gramlot-time-text-box','gramlot-date-calendar'].includes(this.active.element.localName)) return;
42
+ if (control.getAttribute('aria-expanded') === 'true' || (event.key === 'ArrowDown' && control.getAttribute('role') === 'combobox')) return;
43
+ const horizontal = event.key === 'ArrowLeft' || event.key === 'ArrowRight';
44
+ const backwards = event.key === 'ArrowLeft' || event.key === 'ArrowUp';
45
+ if (horizontal) {
46
+ if (typeof control.selectionStart !== 'number' || control.selectionStart !== control.selectionEnd) return;
47
+ if (control.selectionStart !== (backwards ? 0 : control.value.length)) return;
48
+ }
49
+ event.preventDefault(); event.stopPropagation();
50
+ const active = this.active;
51
+ this.session.commitField(active.path);
52
+ this.navigate(active.row,active.column.field,horizontal ? (backwards ? -1 : 1) : {row:backwards ? -1 : 1});
53
+ if (horizontal && this.active && this.active !== active) {
54
+ const target = this.active.element.control;
55
+ if (typeof target.selectionStart === 'number') {
56
+ const caret = backwards ? target.value.length : 0;
57
+ target.setSelectionRange(caret,caret);
58
+ }
59
+ }
60
+ };
61
+ this.connect();
62
+ context.sourceNode.rootBuilder.binding.bindingFor(context.sourceNode).track(() => this.dispose());
63
+ }
64
+ connect() {
65
+ this.grid.addEventListener('keydown',this.keydown);
66
+ this.grid.addEventListener('keydown',this.arrowKeydown,true);
67
+ if (this.session && !this.releaseNavigator) this.releaseNavigator = this.session.registerNavigator(this.focusField);
68
+ }
69
+ disconnect() {
70
+ this.close();
71
+ this.grid.removeEventListener('keydown',this.keydown);
72
+ this.grid.removeEventListener('keydown',this.arrowKeydown,true);
73
+ this.releaseNavigator?.(); this.releaseNavigator = null;
74
+ }
75
+ path(row, field) {
76
+ const {mode} = this.store();
77
+ return this.context.sourceNode.absDatapath(this.context.sourceNode.getAttr('storepath')) + '.' + row.label + (mode === 'attributes' ? '?' : '.') + field;
78
+ }
79
+ configure(columns) {
80
+ if (!columns.some(column => column.edit)) return;
81
+ if (this.store().window !== null) throw new Error('Editing requires a fully resident Bag; remote windows are read-only.');
82
+ const owner = this.context.sourceNode;
83
+ this.session = owner.editCollection(owner.absDatapath(owner.getAttr('storepath')));
84
+ if (!this.releaseStatus) this.releaseStatus = this.session.subscribe(() => this.refresh());
85
+ if (!this.releaseNavigator) this.releaseNavigator = this.session.registerNavigator(this.focusField);
86
+ }
87
+ open(row, column, cell) {
88
+ if (this.store().window !== null) throw new Error('Editing requires a fully resident Bag; remote windows are read-only.');
89
+ if (this.active?.row === row && this.active.column.field === column.field) { this.active.element.control.focus(); return; }
90
+ this.close();
91
+ const owner = this.context.sourceNode, path = this.path(row,column.field);
92
+ if (!this.retained.has(path)) { this.session.retainField(path); this.retained.set(path,row); }
93
+ let declaration = this.declarations.get(path);
94
+ if (!declaration) {
95
+ const options = column.edit === true ? {} : {...column.edit};
96
+ const tag = options.tag ?? ({L:'numberTextBox', R:'numberTextBox', N:'numberTextBox', D:'dateTextBox', H:'timeTextBox', B:'checkBox'}[column.dtype] ?? 'textbox');
97
+ delete options.tag;
98
+ if (column.dtype === 'DHZ' && !column.edit.tag) throw new Error('A datetime column requires an explicitly declared supported editor.');
99
+ if (!owner.rootBuilder.schemaTag(tag)) throw new Error(`Grid editor requires the ${tag} input collection.`);
100
+ const builder = owner.rootBuilder, id = `grid-cell-${crypto.randomUUID()}`;
101
+ const container = builder.wrapSource(owner).div({node_label:id, slot:'cell-editor'});
102
+ try {
103
+ const control = container[tag]({...options, value:'^'+path, dtype:column.dtype ?? 'T', id, width:'100%', margin:'0'});
104
+ declaration = {container:sourceTarget(container),source:sourceTarget(control),id};
105
+ if (!this.grid.ownerDocument.getElementById(id).supportsFormValidation) throw new TypeError('Grid editors require a Gramlot input with shared form validation.');
106
+ this.declarations.set(path,declaration);
107
+ } catch (error) {
108
+ const node = sourceTarget(container); node.parentBag.popNode(node.label);
109
+ this.releaseCleanField(path);
110
+ throw error;
111
+ }
112
+ } else owner.rootBuilder.binding.renderer.mountView(declaration.container);
113
+ const element = this.grid.ownerDocument.getElementById(declaration.id);
114
+ this.active = {row,column,path,...declaration,element};
115
+ this.layer.hidden = false;
116
+ this.position(cell);
117
+ element.control.focus();
118
+ }
119
+ close() {
120
+ const active = this.active;
121
+ this.active = null; this.layer.hidden = true;
122
+ if (active) {
123
+ this.context.sourceNode.rootBuilder.binding.renderer.unmountView(active.container);
124
+ this.releaseCleanField(active.path);
125
+ }
126
+ }
127
+ releaseCleanField(path) {
128
+ const state = this.session.fieldStatus(path);
129
+ if (state.error === null && state.pending === null && state.draft === null && this.retained.has(path)) {
130
+ this.releaseField(path);
131
+ }
132
+ }
133
+ releaseField(path) {
134
+ const declaration = this.declarations.get(path);
135
+ this.declarations.delete(path);
136
+ if (declaration?.container.parentBag) declaration.container.parentBag.popNode(declaration.container.label);
137
+ this.session.releaseField(path); this.retained.delete(path);
138
+ }
139
+ position(cell = null) {
140
+ if (!this.active) return;
141
+ cell ??= this.cellFor(this.active.row,this.active.column.field);
142
+ if (!cell) { this.close(); return; }
143
+ const rect = cell.getBoundingClientRect(), host = this.grid.control.getBoundingClientRect();
144
+ const viewport = this.grid.shadowRoot.querySelector('.body-viewport').getBoundingClientRect();
145
+ if (viewport.height && (rect.top < viewport.top || rect.bottom > viewport.bottom || rect.right <= viewport.left || rect.left >= viewport.right)) { this.close(); return; }
146
+ Object.assign(this.layer.style,{
147
+ left:`${rect.left-host.left-this.grid.control.clientLeft}px`,
148
+ top:`${rect.top-host.top-this.grid.control.clientTop}px`,width:`${rect.width}px`
149
+ });
150
+ this.layer.style.setProperty('--grid-editor-height',`${rect.height}px`);
151
+ }
152
+ decorate(tr, row) {
153
+ if (!this.session) return;
154
+ for (const cell of tr.querySelectorAll('td[data-field]')) {
155
+ const state = this.session.fieldStatus(this.path(row,cell.dataset.field));
156
+ cell.classList.toggle('invalid-cell',Boolean(state.error));
157
+ cell.classList.toggle('draft-cell',state.draft !== null);
158
+ if (state.error) { cell.setAttribute('aria-invalid','true'); cell.title = state.error.getNodes().map(n => n.value).join('\n'); }
159
+ else { cell.removeAttribute('aria-invalid'); cell.removeAttribute('title'); }
160
+ }
161
+ }
162
+ refresh() {
163
+ const bag = this.store().bag;
164
+ for (const [path,row] of this.retained) if (row.parentBag !== bag) {
165
+ this.releaseField(path);
166
+ if (this.active?.row === row) this.close();
167
+ }
168
+ for (const path of this.retained.keys()) if (path !== this.active?.path) this.releaseCleanField(path);
169
+ for (const [row,tr] of this.renderedRows()) this.decorate(tr,row);
170
+ }
171
+ replaceData() {
172
+ this.close();
173
+ if (this.session) {
174
+ for (const path of this.retained.keys()) this.releaseField(path);
175
+ this.retained.clear();
176
+ }
177
+ }
178
+ dispose() {
179
+ this.disconnect(); this.replaceData(); this.releaseStatus?.(); this.layer.remove();
180
+ }
181
+ }
@@ -0,0 +1,258 @@
1
+ {
2
+ "elements": {
3
+ "struct": {
4
+ "doc": null,
5
+ "sub_tags": "column,colgroup",
6
+ "parent_tags": "grid",
7
+ "inherits_from": null,
8
+ "ns": null,
9
+ "node_label": null,
10
+ "collection_key": null,
11
+ "attributes": {
12
+ "parameters": [],
13
+ "accepts_var_keyword": false,
14
+ "accepts_var_positional": false
15
+ },
16
+ "_meta": {
17
+ "data_element": true,
18
+ "gridStruct": true
19
+ }
20
+ },
21
+ "column": {
22
+ "doc": null,
23
+ "sub_tags": "",
24
+ "parent_tags": "struct,colgroup",
25
+ "inherits_from": null,
26
+ "ns": null,
27
+ "node_label": null,
28
+ "collection_key": null,
29
+ "attributes": {
30
+ "parameters": [
31
+ {
32
+ "name": "field",
33
+ "kind": "positional_or_keyword",
34
+ "role": "attribute",
35
+ "annotation": {
36
+ "kind": "annotated",
37
+ "base": {
38
+ "kind": "dtype",
39
+ "dtype": "T"
40
+ },
41
+ "metadata": [
42
+ {
43
+ "kind": "regex",
44
+ "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
45
+ "flags": 0
46
+ }
47
+ ]
48
+ },
49
+ "has_default": false
50
+ },
51
+ {
52
+ "name": "name",
53
+ "kind": "keyword_only",
54
+ "role": "attribute",
55
+ "annotation": {
56
+ "kind": "dtype",
57
+ "dtype": "T"
58
+ },
59
+ "has_default": true,
60
+ "default": null
61
+ },
62
+ {
63
+ "name": "dtype",
64
+ "kind": "keyword_only",
65
+ "role": "attribute",
66
+ "annotation": {
67
+ "kind": "annotated",
68
+ "base": {
69
+ "kind": "dtype",
70
+ "dtype": "T"
71
+ },
72
+ "metadata": [
73
+ {
74
+ "kind": "regex",
75
+ "pattern": "^(T|B|L|R|N|D|H|DHZ)$",
76
+ "flags": 0
77
+ }
78
+ ]
79
+ },
80
+ "has_default": true,
81
+ "default": null
82
+ },
83
+ {
84
+ "name": "width",
85
+ "kind": "keyword_only",
86
+ "role": "attribute",
87
+ "annotation": {
88
+ "kind": "annotated",
89
+ "base": {
90
+ "kind": "dtype",
91
+ "dtype": "T"
92
+ },
93
+ "metadata": [
94
+ {
95
+ "kind": "regex",
96
+ "pattern": "^[0-9]+([.][0-9]+)?(px|em|rem|ch|%)$",
97
+ "flags": 0
98
+ }
99
+ ]
100
+ },
101
+ "has_default": true,
102
+ "default": null
103
+ },
104
+ {
105
+ "name": "align",
106
+ "kind": "keyword_only",
107
+ "role": "attribute",
108
+ "annotation": {
109
+ "kind": "annotated",
110
+ "base": {
111
+ "kind": "dtype",
112
+ "dtype": "T"
113
+ },
114
+ "metadata": [
115
+ {
116
+ "kind": "regex",
117
+ "pattern": "^(start|center|end)$",
118
+ "flags": 0
119
+ }
120
+ ]
121
+ },
122
+ "has_default": true,
123
+ "default": null
124
+ },
125
+ {
126
+ "name": "format",
127
+ "kind": "keyword_only",
128
+ "role": "attribute",
129
+ "annotation": {
130
+ "kind": "dtype",
131
+ "dtype": "T"
132
+ },
133
+ "has_default": true,
134
+ "default": null
135
+ },
136
+ {
137
+ "name": "white_space",
138
+ "kind": "keyword_only",
139
+ "role": "attribute",
140
+ "annotation": {
141
+ "kind": "annotated",
142
+ "base": {
143
+ "kind": "dtype",
144
+ "dtype": "T"
145
+ },
146
+ "metadata": [
147
+ {
148
+ "kind": "regex",
149
+ "pattern": "^(normal|nowrap|pre-wrap)$",
150
+ "flags": 0
151
+ }
152
+ ]
153
+ },
154
+ "has_default": true,
155
+ "default": null
156
+ },
157
+ {
158
+ "name": "edit",
159
+ "kind": "keyword_only",
160
+ "role": "attribute",
161
+ "annotation": {
162
+ "kind": "union",
163
+ "items": [
164
+ {
165
+ "kind": "dtype",
166
+ "dtype": "B"
167
+ },
168
+ {
169
+ "kind": "type",
170
+ "module": "builtins",
171
+ "name": "dict"
172
+ }
173
+ ]
174
+ },
175
+ "has_default": true,
176
+ "default": false
177
+ }
178
+ ],
179
+ "accepts_var_keyword": false,
180
+ "accepts_var_positional": false
181
+ },
182
+ "_meta": {
183
+ "data_element": true
184
+ }
185
+ },
186
+ "colgroup": {
187
+ "doc": null,
188
+ "sub_tags": "column",
189
+ "parent_tags": "struct",
190
+ "inherits_from": null,
191
+ "ns": null,
192
+ "node_label": null,
193
+ "collection_key": null,
194
+ "attributes": {
195
+ "parameters": [
196
+ {
197
+ "name": "code",
198
+ "kind": "positional_or_keyword",
199
+ "role": "attribute",
200
+ "annotation": {
201
+ "kind": "annotated",
202
+ "base": {
203
+ "kind": "dtype",
204
+ "dtype": "T"
205
+ },
206
+ "metadata": [
207
+ {
208
+ "kind": "regex",
209
+ "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
210
+ "flags": 0
211
+ }
212
+ ]
213
+ },
214
+ "has_default": false
215
+ },
216
+ {
217
+ "name": "name",
218
+ "kind": "keyword_only",
219
+ "role": "attribute",
220
+ "annotation": {
221
+ "kind": "dtype",
222
+ "dtype": "T"
223
+ },
224
+ "has_default": true,
225
+ "default": null
226
+ },
227
+ {
228
+ "name": "background",
229
+ "kind": "keyword_only",
230
+ "role": "attribute",
231
+ "annotation": {
232
+ "kind": "dtype",
233
+ "dtype": "T"
234
+ },
235
+ "has_default": true,
236
+ "default": null
237
+ },
238
+ {
239
+ "name": "color",
240
+ "kind": "keyword_only",
241
+ "role": "attribute",
242
+ "annotation": {
243
+ "kind": "dtype",
244
+ "dtype": "T"
245
+ },
246
+ "has_default": true,
247
+ "default": null
248
+ }
249
+ ],
250
+ "accepts_var_keyword": false,
251
+ "accepts_var_positional": false
252
+ },
253
+ "_meta": {
254
+ "data_element": true
255
+ }
256
+ }
257
+ }
258
+ }
@@ -0,0 +1,196 @@
1
+ /** Shared column resize gestures; Data owns field widths, per-grid layout owns the row header. */
2
+ export class GridHeaderInteraction {
3
+ #head;
4
+ #control;
5
+ #structure;
6
+ #sort;
7
+ #rowLayout;
8
+ #layoutChanged;
9
+ #suppressClick = false;
10
+ #gesture = null;
11
+ #drag = null;
12
+ constructor(head, control, structure, sort, rowLayout, layoutChanged) {
13
+ this.#head = head;
14
+ this.#control = control;
15
+ this.#structure = structure;
16
+ this.#sort = sort;
17
+ this.#rowLayout = rowLayout;
18
+ this.#layoutChanged = layoutChanged;
19
+ }
20
+ connect() {
21
+ this.#head.addEventListener('click', this.#click);
22
+ this.#head.addEventListener('pointerdown', this.#start);
23
+ this.#head.addEventListener('pointermove', this.#move);
24
+ this.#head.addEventListener('pointerup', this.#finish);
25
+ this.#head.addEventListener('pointercancel', this.#cancel);
26
+ this.#head.addEventListener('lostpointercapture', this.#cancel);
27
+ this.#head.addEventListener('keydown', this.#key);
28
+ this.#head.addEventListener('dragstart', this.#dragStart);
29
+ this.#head.addEventListener('dragover', this.#dragOver);
30
+ this.#head.addEventListener('drop', this.#drop);
31
+ this.#head.addEventListener('dragend', this.#clearDrag);
32
+ }
33
+ disconnect() {
34
+ this.cancel();
35
+ this.#head.removeEventListener('click', this.#click);
36
+ this.#head.removeEventListener('pointerdown', this.#start);
37
+ this.#head.removeEventListener('pointermove', this.#move);
38
+ this.#head.removeEventListener('pointerup', this.#finish);
39
+ this.#head.removeEventListener('pointercancel', this.#cancel);
40
+ this.#head.removeEventListener('lostpointercapture', this.#cancel);
41
+ this.#head.removeEventListener('keydown', this.#key);
42
+ this.#head.removeEventListener('dragstart', this.#dragStart);
43
+ this.#head.removeEventListener('dragover', this.#dragOver);
44
+ this.#head.removeEventListener('drop', this.#drop);
45
+ this.#head.removeEventListener('dragend', this.#clearDrag);
46
+ }
47
+ #click = event => {
48
+ if (this.#suppressClick) { this.#suppressClick = false; return; }
49
+ if (event.target.closest('.column-resize')) return;
50
+ const header = event.target.closest('th[data-field]');
51
+ if (header) this.#sort(header.dataset.field, event.shiftKey);
52
+ };
53
+ #dragStart = event => {
54
+ const header = event.target.closest('th[data-field]');
55
+ if (!header || this.#gesture) { event.preventDefault(); return; }
56
+ this.#suppressClick = true;
57
+ this.#drag = {field:header.dataset.field, node:this.#structure().columnNode(header.dataset.field)};
58
+ event.dataTransfer.effectAllowed = 'move';
59
+ event.dataTransfer.setData('text/plain', header.textContent);
60
+ header.classList.add('column-dragging');
61
+ };
62
+ #dropTarget(event) {
63
+ const header = event.target.closest('th[data-field]');
64
+ if (!this.#drag || !header) return null;
65
+ const target = this.#structure().columnNode(header.dataset.field);
66
+ if (this.#structure().columnNode(this.#drag.field) !== this.#drag.node ||
67
+ target === this.#drag.node || target.parentBag !== this.#drag.node.parentBag) return null;
68
+ return {header, field:header.dataset.field,
69
+ after:event.clientX > header.getBoundingClientRect().left + header.getBoundingClientRect().width / 2};
70
+ }
71
+ #dragOver = event => {
72
+ const target = this.#dropTarget(event);
73
+ for (const cell of this.#head.querySelectorAll('[data-drop]')) cell.removeAttribute('data-drop');
74
+ if (!target) return;
75
+ event.preventDefault(); event.dataTransfer.dropEffect = 'move';
76
+ target.header.dataset.drop = target.after ? 'after' : 'before';
77
+ };
78
+ #drop = event => {
79
+ const target = this.#dropTarget(event);
80
+ if (!target) return;
81
+ event.preventDefault();
82
+ const field = this.#drag.field;
83
+ this.#clearDrag();
84
+ this.#structure().moveColumn(field, target.field, target.after);
85
+ this.#head.querySelector(`th[data-field="${field}"]`).focus({preventScroll:true});
86
+ };
87
+ #clearDrag = () => {
88
+ this.#drag = null;
89
+ for (const cell of this.#head.querySelectorAll('th')) {
90
+ cell.classList.remove('column-dragging'); cell.removeAttribute('data-drop');
91
+ }
92
+ };
93
+ #target(header) {
94
+ const field = header.dataset.field;
95
+ if (field === undefined) {
96
+ const layout = this.#rowLayout();
97
+ return {identity:layout, width:layout.headerWidth, setWidth:width => {
98
+ layout.setHeaderWidth(width); this.#layoutChanged();
99
+ }};
100
+ }
101
+ const structure = this.#structure(), node = structure.columnNode(field);
102
+ return {identity:node, width:node.getAttr('width'), setWidth:width => structure.setColumnWidth(field, width)};
103
+ }
104
+ #minimum(header) {
105
+ if (header.classList.contains('row-heading')) {
106
+ return Math.max(40, Math.ceil(header.querySelector('.row-number-measure').getBoundingClientRect().width));
107
+ }
108
+ return Number.parseFloat(this.#control.ownerDocument.defaultView.getComputedStyle(this.#control)
109
+ .getPropertyValue('--gramlot-grid-column-min-width')) || 40;
110
+ }
111
+ #start = event => {
112
+ this.#suppressClick = false;
113
+ const handle = event.target.closest('.column-resize');
114
+ if (!handle || event.button !== 0 || this.#gesture) return;
115
+ const header = handle.closest('th');
116
+ const field = header.dataset.field;
117
+ const target = this.#target(header);
118
+ const tables = [...this.#control.querySelectorAll('table')];
119
+ const headers = [...this.#head.querySelectorAll('th[scope=col]')];
120
+ const widths = headers.map(cell => cell.getBoundingClientRect().width);
121
+ this.#gesture = {pointerId:event.pointerId, field, header, identity:target.identity, originalWidth:target.width,
122
+ x:event.clientX, index:headers.indexOf(header), widths, width:header.getBoundingClientRect().width,
123
+ tables:tables.map(table => ({table, width:table.style.width,
124
+ columns:[...table.querySelectorAll('col')].map(col => ({col, width:col.style.width}))}))};
125
+ this.#head.setPointerCapture(event.pointerId);
126
+ this.#control.classList.add('resizing');
127
+ event.preventDefault();
128
+ };
129
+ #move = event => {
130
+ const gesture = this.#gesture;
131
+ if (!gesture || gesture.pointerId !== event.pointerId) return;
132
+ gesture.width = Math.max(this.#minimum(gesture.header), Math.round(gesture.widths[gesture.index] + event.clientX - gesture.x));
133
+ const widths = gesture.widths.map((width, index) => index === gesture.index ? gesture.width : width);
134
+ for (const {table, columns} of gesture.tables) {
135
+ table.style.width = `${widths.reduce((sum, width) => sum + width, 0)}px`;
136
+ columns.forEach(({col}, index) => { col.style.width = `${widths[index]}px`; });
137
+ }
138
+ };
139
+ #finish = event => {
140
+ const gesture = this.#gesture;
141
+ if (!gesture || gesture.pointerId !== event.pointerId) return;
142
+ this.#move(event);
143
+ const current = this.#target(gesture.header);
144
+ this.#suppressClick = true;
145
+ this.cancel();
146
+ // A retargeted/replaced struct or concurrent width edit must win over this gesture.
147
+ if (current.identity !== gesture.identity || current.width !== gesture.originalWidth) return;
148
+ current.setWidth(gesture.width);
149
+ this.#focus(gesture.field);
150
+ };
151
+ #key = event => {
152
+ if (event.key === 'Escape' && this.#gesture) {
153
+ this.cancel(); event.preventDefault(); return;
154
+ }
155
+ const handle = event.target.closest('.column-resize');
156
+ if (!handle && ['Enter', ' '].includes(event.key)) {
157
+ const header = event.target.closest('th[data-field]');
158
+ if (header) { event.preventDefault(); this.#sort(header.dataset.field, event.shiftKey); }
159
+ return;
160
+ }
161
+ if (!['ArrowLeft', 'ArrowRight'].includes(event.key)) return;
162
+ if (!handle) {
163
+ const header = event.target.closest('th[data-field]');
164
+ if (!header || !event.altKey) return;
165
+ const field = header.dataset.field, structure = this.#structure();
166
+ const node = structure.columnNode(field), siblings = node.parentBag.getNodes();
167
+ const target = siblings[siblings.indexOf(node) + (event.key === 'ArrowRight' ? 1 : -1)];
168
+ if (target?.getAttr('tag') === 'cell') {
169
+ structure.moveColumn(field, target.getAttr('field'), event.key === 'ArrowRight');
170
+ this.#head.querySelector(`th[data-field="${field}"]`).focus({preventScroll:true});
171
+ }
172
+ event.preventDefault(); return;
173
+ }
174
+ const header = handle.closest('th');
175
+ const delta = (event.key === 'ArrowRight' ? 1 : -1) * (event.shiftKey ? 1 : 10);
176
+ this.#target(header).setWidth(Math.max(this.#minimum(header), Math.round(header.getBoundingClientRect().width + delta)));
177
+ this.#focus(header.dataset.field);
178
+ event.preventDefault();
179
+ };
180
+ #focus(field) {
181
+ this.#head.querySelector(field === undefined ? '.row-heading .column-resize' : `th[data-field="${field}"] .column-resize`).focus({preventScroll:true});
182
+ }
183
+ #cancel = () => { if (this.#gesture) this.cancel(); };
184
+ cancel() {
185
+ this.#clearDrag();
186
+ const gesture = this.#gesture;
187
+ if (!gesture) return;
188
+ this.#gesture = null;
189
+ for (const {table, width, columns} of gesture.tables) {
190
+ table.style.width = width;
191
+ for (const {col, width} of columns) col.style.width = width;
192
+ }
193
+ this.#control.classList.remove('resizing');
194
+ if (this.#head.hasPointerCapture(gesture.pointerId)) this.#head.releasePointerCapture(gesture.pointerId);
195
+ }
196
+ }