@gramlot/gramlot 0.3.9 → 0.3.15

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 (95) hide show
  1. package/dist/components/editors/__init__.py +13 -0
  2. package/dist/components/{editors.js → editors/assets/collection.js} +354 -418
  3. package/dist/components/editors/assets/index.js +2 -0
  4. package/dist/components/editors/commands.json +7 -0
  5. package/dist/components/editors/grammar.json +7045 -0
  6. package/dist/components/inputs/__init__.py +13 -0
  7. package/dist/components/{inputs.js → inputs/assets/collection.js} +291 -200
  8. package/dist/components/inputs/assets/index.js +2 -0
  9. package/dist/components/inputs/commands.json +7 -0
  10. package/dist/components/inputs/grammar.json +42881 -0
  11. package/dist/components/layout/__init__.py +13 -0
  12. package/dist/components/layout/assets/collection.js +25737 -0
  13. package/dist/components/layout/assets/index.js +2 -0
  14. package/dist/components/layout/commands.json +7 -0
  15. package/dist/components/layout/grammar.json +1798 -0
  16. package/dist/components/tree/__init__.py +13 -0
  17. package/dist/components/{tree.js → tree/assets/collection.js} +62 -175
  18. package/dist/components/tree/assets/index.js +2 -0
  19. package/dist/components/tree/commands.json +7 -0
  20. package/dist/components/tree/grammar.json +339 -0
  21. package/dist/components/typst/__init__.py +13 -0
  22. package/dist/components/typst/assets/collection.js +4 -0
  23. package/dist/components/typst/assets/index.js +2 -0
  24. package/dist/components/typst/commands.json +7 -0
  25. package/dist/components/typst/grammar.json +81 -0
  26. package/dist/gramlot.js +2525 -905
  27. package/dist/gramlot.min.js +109 -28
  28. package/dist/runtime-notices.json +0 -196
  29. package/package.json +1 -1
  30. package/src/binding/ask-dialog.js +1 -1
  31. package/src/binding/installation.js +5 -6
  32. package/src/binding/remote.js +9 -2
  33. package/src/binding/runtime.js +12 -5
  34. package/src/bootstrap.d.ts +1 -1
  35. package/src/bootstrap.js +52 -30
  36. package/src/builder/binding.json +25 -0
  37. package/src/builder/gramlot-builder.js +16 -3
  38. package/src/builder/source.js +41 -4
  39. package/src/collection-artifact.d.ts +11 -0
  40. package/src/collection-artifact.js +57 -0
  41. package/src/commands.d.ts +9 -0
  42. package/src/commands.js +36 -0
  43. package/src/components/bases.js +1 -1
  44. package/src/components/border-container.js +33 -2
  45. package/src/components/buttons.js +84 -161
  46. package/src/components/choice-editor.js +31 -10
  47. package/src/components/database-choices.js +110 -0
  48. package/src/components/inputs.js +15 -2
  49. package/src/components/layout.js +5 -3
  50. package/src/components/menus.js +346 -0
  51. package/src/components/popup.js +15 -2
  52. package/src/components/registry.js +18 -3
  53. package/src/components/toolbar-overflow.js +181 -0
  54. package/src/components/tree.js +14 -4
  55. package/src/components/validation.js +1 -1
  56. package/src/components/windows.js +284 -0
  57. package/src/gramlot.d.ts +2 -0
  58. package/src/gramlot.js +4 -2
  59. package/src/handlers/rpc-handler.d.ts +6 -1
  60. package/src/handlers/rpc-handler.js +7 -2
  61. package/src/handlers/source-handler.d.ts +2 -1
  62. package/src/handlers/source-handler.js +10 -1
  63. package/src/index-page-styles.js +1 -1
  64. package/src/index-theme.js +1 -1
  65. package/src/index.d.ts +5 -0
  66. package/src/index.js +5 -0
  67. package/src/renderer/attributes.js +11 -2
  68. package/src/renderer/gramlot-html-renderer.js +1 -0
  69. package/src/renderer/gramlot-renderer.js +13 -3
  70. package/src/server/assets.js +2 -2
  71. package/src/server/gramlot-file-server.d.ts +2 -0
  72. package/src/server/gramlot-file-server.js +45 -1
  73. package/src/server/gramlot-server.d.ts +20 -2
  74. package/src/server/gramlot-server.js +86 -6
  75. package/src/server/page-base.js +1 -0
  76. package/src/server/page.d.ts +3 -0
  77. package/src/view/button.js +47 -3
  78. package/src/view/controls.js +8 -0
  79. package/src/view/html.js +8 -0
  80. package/src/view/menu-command.js +45 -0
  81. package/src/view/menu.js +87 -0
  82. package/themes/gramlot-compact/README.md +19 -0
  83. package/themes/gramlot-compact/theme.css +138 -45
  84. package/themes/joanna/README.md +19 -0
  85. package/themes/joanna/theme.css +134 -78
  86. package/themes/mela/theme.css +184 -0
  87. package/themes/mimi/README.md +19 -0
  88. package/themes/mimi/theme.css +133 -77
  89. package/themes/palettes/README.md +3 -0
  90. package/themes/palettes/compact.css +86 -0
  91. package/themes/palettes/joanna.css +86 -0
  92. package/themes/palettes/mimi.css +86 -0
  93. package/tools/gramlot-tools-inspector.js +3 -3
  94. package/dist/components/layout.js +0 -1402
  95. package/dist/components/typst.js +0 -4
@@ -0,0 +1,346 @@
1
+ /** Shared popup/permanent menu presentation. Source and execution stay in bindings. */
2
+ import {projectParameters} from './parameters.js';
3
+ import {positionPopup} from './popup.js';
4
+ import {ToolbarOverflow} from './toolbar-overflow.js';
5
+
6
+ export function MenuContainer(HTMLElement) {
7
+ return class extends HTMLElement {
8
+ #events; #observer; #active = null; #previous = []; #opener = null;
9
+ #overflow; #message; #retry; #slot; #opened = false; #attachment; #attachmentEvents;
10
+ constructor() {
11
+ super();
12
+ const doc = this.ownerDocument, shadow = this.attachShadow({mode: 'open'});
13
+ const style = doc.createElement('style');
14
+ style.textContent = `:host{box-sizing:border-box;color:var(--gramlot-ink,CanvasText)}
15
+ :host([role=menu]){position:fixed;inset:auto;margin:0;padding:5px;min-width:min(16rem,var(--gramlot-popup-available-width));width:max-content;font:inherit;
16
+ max-width:min(28rem,var(--gramlot-popup-available-width));max-height:var(--gramlot-popup-available-height);overflow:auto;
17
+ background:var(--gramlot-field-background,Canvas);border:1px solid var(--gramlot-border-control,#888);border-radius:var(--gramlot-radius-lg,9px);box-shadow:0 8px 28px #0004}
18
+ :host([role=toolbar][aria-orientation=vertical]){flex-direction:column;align-items:stretch}
19
+ :host([role=toolbar]){display:flex;gap:var(--gramlot-toolbar-gap,4px);align-items:center}
20
+ :host([role=menu]) ::slotted([role=separator]){margin:5px 7px 5px 2.5rem;border-top:1px solid var(--gramlot-border-control,#888)}
21
+ :host([role=toolbar][aria-orientation=horizontal]) ::slotted([role=separator]){border-top:0;border-left:1px solid var(--gramlot-border-control,#888);align-self:stretch;margin:4px}
22
+ :host([hidden]){display:none!important} slot{display:contents} .status{padding:.4em;opacity:.75}
23
+ .status:empty{display:none} button{font:inherit}`;
24
+ this.#slot = doc.createElement('slot');
25
+ this.#message = doc.createElement('span'); this.#message.className = 'status'; this.#message.setAttribute('role', 'status');
26
+ this.#retry = doc.createElement('button'); this.#retry.textContent = 'Riprova'; this.#retry.hidden = true;
27
+ this.#retry.addEventListener('click', event => { event.stopPropagation(); this.menuBinding.load(true); });
28
+ shadow.append(style, this.#slot, this.#message, this.#retry);
29
+ this.#overflow=new ToolbarOverflow(this,this.#slot);
30
+ this.addEventListener('keydown', event => this.#key(event));
31
+ this.addEventListener('focusin', event => {
32
+ const item = event.composedPath().find(node => this.items.includes(node));
33
+ if (item) { this.#active = item; this.refreshItems(); }
34
+ });
35
+ }
36
+ get permanent() { return this.parameters.presentation === 'bar'; }
37
+ get opened() { return this.permanent || this.#opened; }
38
+ get items() { return [...this.querySelectorAll('gramlot-menu-line')].filter(item => item.menuOwner===this && !item.parameters.group); }
39
+ get overflow() { return this.#overflow; }
40
+ isOverflowed(item) { return this.#overflow.contains(item); }
41
+ get usableItems() { return this.items.filter(item => item.slot!=='suppressed' && !item.disabled && !item.hidden && !item.closest('[hidden]') && item.getAttribute('role') !== 'separator'); }
42
+ get parentMenu() { return this.parentElement?.closest('gramlot-menu') ?? null; }
43
+ connectedCallback() {
44
+ this.#observer = new this.ownerDocument.defaultView.MutationObserver(() => {this.refreshItems();this.#overflow.schedule();});
45
+ this.#observer.observe(this, {childList:true, subtree:true, characterData:true, attributes:true, attributeFilter:['hidden','disabled']});
46
+ this.refreshItems();
47
+ this.#attachOpening();
48
+ this.#overflow.connect();
49
+ if (this.permanent) this.menuBinding?.load();
50
+ }
51
+ disconnectedCallback() { this.#overflow.disconnect(); this.#observer?.disconnect(); this.#events?.abort(); this.#attachmentEvents?.abort(); this.#attachment=null; this.#opened = false; }
52
+ projectAttributes(attrs) {
53
+ const rest = projectParameters(this, attrs);
54
+ this.#modifierKeys();
55
+ if(!Number.isFinite(this.parameters.iconSize)||this.parameters.iconSize<=0)throw new TypeError('menu iconSize must be positive');
56
+ this.setAttribute('role', this.permanent ? 'toolbar' : 'menu');
57
+ this.setAttribute('aria-orientation', this.permanent ? this.parameters.orientation : 'vertical');
58
+ this.tabIndex = -1;
59
+ if (this.permanent) this.removeAttribute('popover');
60
+ else this.setAttribute('popover','manual');
61
+ this.refreshItems();
62
+ this.#overflow.schedule();
63
+ return rest;
64
+ }
65
+ #modifierKeys() {
66
+ const value=this.parameters.modifiers;
67
+ if (value==null) return null;
68
+ if (value==='*') return [];
69
+ const keys=value.trim().toLowerCase().split(/[,\s]+/);
70
+ if (!keys.length || keys.some(key=>!['shift','ctrl','alt','meta'].includes(key)) || new Set(keys).size!==keys.length) {
71
+ throw new TypeError('Menu modifiers require *, or a combination of Shift, Ctrl, Alt and Meta');
72
+ }
73
+ return keys;
74
+ }
75
+ #attachOpening() {
76
+ const owner=this.parentElement;
77
+ if (this.permanent || !owner) return;
78
+ if (['gramlot-menu-line','gramlot-drop-down-button'].includes(owner.localName)) { this.slot='menu'; return; }
79
+ this.removeAttribute('slot');
80
+ if (owner.localName==='gramlot-menu') return;
81
+ this.#attachment=owner;
82
+ this.#attachmentEvents=new this.ownerDocument.defaultView.AbortController();
83
+ const options={signal:this.#attachmentEvents.signal};
84
+ const outsideMenus=event=>!event.composedPath().some(node=>node.localName==='gramlot-menu');
85
+ const open=event=>{
86
+ if (this.hidden || this.permanent || !outsideMenus(event)) return;
87
+ if (event.composedPath().some(node=>node instanceof this.ownerDocument.defaultView.Element && (node.disabled===true || node.getAttribute('aria-disabled')==='true'))) return;
88
+ const keys=this.#modifierKeys();
89
+ if (event.type==='click') {
90
+ if (keys===null || event.button!==0 || ['shift','ctrl','alt','meta'].some(key=>Boolean(event[key+'Key'])!==keys.includes(key))) return;
91
+ } else if (keys!==null) return;
92
+ event.preventDefault(); event.stopPropagation();
93
+ const keyboard=event.type==='keydown';
94
+ const rect=keyboard ? owner.getBoundingClientRect() : null;
95
+ this.open(owner,false,keyboard ? {x:rect.left+8,y:rect.top+8} : {x:event.clientX,y:event.clientY});
96
+ };
97
+ owner.addEventListener('click',open,options);
98
+ owner.addEventListener('contextmenu',open,options);
99
+ owner.addEventListener('keydown',event=>{
100
+ if (event.key==='ContextMenu' || event.key==='F10' && event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey) open(event);
101
+ },options);
102
+ }
103
+ setStatus(status) {
104
+ this.status = status;
105
+ this.#message.textContent = ({loading:'Caricamento…',empty:'Nessuna voce',error:'Impossibile caricare'})[status] ?? '';
106
+ this.#retry.hidden = status !== 'error';
107
+ this.setAttribute('aria-busy', String(status === 'loading'));
108
+ for (const item of this.items) item.refreshAvailability();
109
+ this.refreshItems();
110
+ }
111
+ refreshItems() {
112
+ for(const group of this.querySelectorAll('gramlot-menu-line'))if(group.menuOwner===this && group.parameters.group)group.refreshAvailability();
113
+ for(const item of this.items)item.refreshAvailability();
114
+ const usable = this.usableItems;
115
+ if (this.#active && !usable.includes(this.#active)) {
116
+ const index = this.#previous.indexOf(this.#active);
117
+ const next = this.#previous.slice(index+1).find(item => usable.includes(item))
118
+ ?? this.#previous.slice(0,index).reverse().find(item => usable.includes(item));
119
+ this.#active = next ?? null;
120
+ if (this.opened && this.status !== 'loading') {
121
+ if (next) next.focus();
122
+ else if (this.permanent) this.focus(); else this.close();
123
+ }
124
+ }
125
+ const visible=usable.filter(item=>!this.isOverflowed(item));
126
+ for (const item of this.items) item.control.tabIndex = this.permanent && item === (visible.includes(this.#active)?this.#active:visible[0]) ? 0 : -1;
127
+ this.#previous = this.items;
128
+ }
129
+ open(opener, last = false, point = null) {
130
+ if (this.permanent) return;
131
+ this.#opener = opener;
132
+ if (!this.#opened) { this.showPopover(); this.#opened = true; }
133
+ opener.setAttribute('aria-expanded','true');
134
+ this.#events?.abort();
135
+ this.#events = new this.ownerDocument.defaultView.AbortController();
136
+ const options = {signal:this.#events.signal};
137
+ const place = () => positionPopup(this, opener, {point,placement:this.parentMenu && (!this.parentMenu.permanent || this.parentMenu.parameters.orientation==='vertical' || this.parentMenu.isOverflowed(this.parentElement)) ? 'side' : 'bottom'});
138
+ place();
139
+ this.ownerDocument.defaultView.addEventListener('resize',place,options);
140
+ this.ownerDocument.addEventListener('scroll',place,{...options,capture:true});
141
+ this.ownerDocument.addEventListener('pointerdown',event => {
142
+ if (!event.composedPath().includes(this) && (opener===this.#attachment || !event.composedPath().includes(opener))) this.closeChain(false);
143
+ },options);
144
+ this.menuBinding?.load().then(() => {
145
+ if (this.#opened && (this.ownerDocument.activeElement === this || !this.contains(this.ownerDocument.activeElement))) this.focusEntry(last);
146
+ if (this.#opened) place();
147
+ });
148
+ this.focusEntry(last);
149
+ }
150
+ focusEntry(last = false) {
151
+ const usable = this.usableItems;
152
+ const target = usable[last ? usable.length-1 : 0];
153
+ if (target) { this.#active=target; target.focus(); } else this.focus();
154
+ }
155
+ close(restore = true) {
156
+ for (const child of this.querySelectorAll('gramlot-menu')) if (!child.permanent) child.close(false);
157
+ if (this.permanent) {this.#overflow.close(restore);return;}
158
+ if (!this.#opened) return;
159
+ this.hidePopover(); this.#opened = false; this.#events?.abort();
160
+ this.#opener?.setAttribute('aria-expanded','false');
161
+ if (restore && this.#opener?.isConnected) this.#opener.focus();
162
+ }
163
+ closeChain(restore = true) {
164
+ const parent = this.parentMenu;
165
+ this.close(restore && !parent);
166
+ if (parent && !parent.permanent) parent.closeChain(restore);
167
+ else if(parent?.isOverflowed(this.parentElement))parent.overflow.close(restore);
168
+ else if (restore && this.#opener?.isConnected) this.#opener.focus();
169
+ }
170
+ #key(event) {
171
+ if (event.defaultPrevented || event.composedPath().find(node => node.localName === 'gramlot-menu') !== this) return;
172
+ const item = event.composedPath().find(node => this.items.includes(node));
173
+ // Embedded editors retain their editing and Tab behavior.
174
+ if (!item && event.target !== this) return;
175
+ const inOverflow=item && this.isOverflowed(item);
176
+ const items=this.usableItems.filter(entry=>this.isOverflowed(entry)===Boolean(inOverflow)), index=items.indexOf(item);
177
+ const nextKey=this.permanent && !inOverflow && this.parameters.orientation==='horizontal' ? 'ArrowRight':'ArrowDown';
178
+ const prevKey=this.permanent && !inOverflow && this.parameters.orientation==='horizontal' ? 'ArrowLeft':'ArrowUp';
179
+ let next;
180
+ if (event.key===nextKey) next=Math.min(items.length-1,index+1);
181
+ if (event.key===prevKey) next=Math.max(0,index-1);
182
+ if (event.key==='Home') next=0;
183
+ if (event.key==='End') next=items.length-1;
184
+ if (next!==undefined) { event.preventDefault(); event.stopPropagation(); items[next]?.focus(); return; }
185
+ if ((!this.permanent || inOverflow) && event.key==='ArrowRight' && item?.submenu) {
186
+ event.preventDefault(); event.stopPropagation(); item.openSubmenu();
187
+ } else if ((event.key==='Escape' && (!this.permanent || this.overflow.opened || this.items.some(entry=>entry.submenu?.opened))) || (!this.permanent && event.key==='ArrowLeft' && this.parentMenu)) {
188
+ event.preventDefault(); event.stopPropagation(); this.close();
189
+ } else if (event.key==='Tab') this.closeChain(false);
190
+ }
191
+ };
192
+ }
193
+
194
+ export function menuLine(Base) {
195
+ return class extends Base {
196
+ #accessibleLabel; #group; #caption; #check; #arrow; #hover; #registry = null; #authoredDisabled = false;
197
+ constructor() {
198
+ super();
199
+ const doc=this.ownerDocument;
200
+ this.#caption=doc.createElement('span'); this.#check=doc.createElement('span');
201
+ this.#caption.className='menu-caption'; this.#check.className='menu-check';
202
+ this.#arrow=doc.createElement('span'); this.#arrow.className='menu-arrow';
203
+ this.#arrow.setAttribute('aria-hidden','true');
204
+ this.#check.setAttribute('aria-hidden','true');
205
+ this.control.prepend(this.#check); this.control.append(this.#caption,this.#arrow);
206
+ const style=doc.createElement('style');
207
+ style.textContent=`:host{color:var(--gramlot-ink,CanvasText)} .control-box{color:inherit;height:100%} :host button{color:inherit}
208
+ .menu-check:empty,.menu-arrow:empty{display:none}
209
+ button.menu-row{display:grid;grid-template-columns:1em 1.15em minmax(0,1fr) .7em;column-gap:.5em;
210
+ align-items:center;width:100%;min-height:max(min(var(--gramlot-field-height,28px),28px),var(--gramlot-touch-target,0px));padding:var(--gramlot-button-padding,3px 8px);
211
+ line-height:1.3;text-align:left;border:0;border-radius:var(--gramlot-radius-sm,5px);background:transparent;cursor:default}
212
+ .menu-row .menu-check,.menu-row .icon,.menu-row .menu-arrow{display:flex;align-items:center;justify-content:center;min-width:0}
213
+ .menu-row .menu-check{grid-column:1}.menu-row .icon{grid-column:2}
214
+ .menu-row .menu-caption{grid-column:3;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
215
+ .menu-row .menu-arrow{grid-column:4;font-size:1.2em;line-height:1}
216
+ button.menu-row:hover:not(:disabled),button.menu-row:focus-visible,button.menu-row[aria-expanded=true]{
217
+ background:var(--gramlot-selected,Highlight);color:var(--gramlot-action-hover,HighlightText);outline:none}
218
+ button.menu-row:disabled{opacity:.45;cursor:default}
219
+ :host button.toolbar-button{position:relative;display:inline-flex;align-items:center;justify-content:center;gap:var(--gramlot-button-gap,6px);
220
+ box-sizing:border-box;width:100%;height:100%;min-width:max(var(--gramlot-field-height,38px),var(--gramlot-touch-target,0px));min-height:max(var(--gramlot-field-height,38px),var(--gramlot-touch-target,0px));padding:var(--gramlot-button-padding,6px 5px);border:1px solid transparent;border-radius:var(--gramlot-radius-sm,8px);
221
+ background:transparent;cursor:pointer;line-height:1.2;white-space:nowrap}
222
+ :host button.toolbar-button .icon{display:inline-flex;align-items:center;justify-content:center;
223
+ width:var(--gramlot-toolbar-icon-size);height:var(--gramlot-toolbar-icon-size);font-size:var(--gramlot-toolbar-icon-size);line-height:1}
224
+ :host button.toolbar-button .icon:empty{display:none}
225
+ :host button.toolbar-button:hover:not(:disabled){background:color-mix(in srgb,currentColor 10%,transparent)}
226
+ :host button.toolbar-button:focus-visible{outline:2px solid var(--gramlot-focus-ring,Highlight);outline-offset:-2px}
227
+ :host button.toolbar-button[aria-checked=true],:host button.toolbar-button[aria-expanded=true]{background:var(--gramlot-selected,color-mix(in srgb,currentColor 14%,transparent))}
228
+ :host button.toolbar-button:disabled{opacity:.45;cursor:default}
229
+ :host button.toolbar-below{flex-direction:column;gap:3px;min-width:54px}
230
+ :host button.toolbar-below .menu-caption{font-size:11px;line-height:1.2}
231
+ :host button.toolbar-tooltip .menu-caption{display:none}
232
+ :host button.toolbar-button .menu-check{display:none}
233
+ :host button.toolbar-tooltip .menu-arrow{position:absolute;right:3px;top:50%;transform:translateY(-50%);font-size:12px}
234
+ :host button.toolbar-below .menu-arrow{position:absolute;inset:4px 3px auto auto;font-size:12px}`;
235
+ style.textContent+=`
236
+ :host([role=group]){display:block;min-width:0}
237
+ :host([role=group]) .control-box{display:none}
238
+ .menu-group{display:flex;align-items:center;gap:var(--gramlot-toolbar-gap,4px);flex-wrap:nowrap}
239
+ .menu-group ::slotted(*){flex-shrink:0}
240
+ .menu-group[hidden]{display:none}
241
+ :host([data-menu-overflow]) .menu-group,:host([data-menu-group-rows]) .menu-group{flex-direction:column;align-items:stretch;padding:3px 0}
242
+ :host([data-menu-overflow][role=group]){border-block-end:1px solid var(--gramlot-border-control,#888);margin-block-end:4px}
243
+ :host([role=group][aria-disabled=true]) .menu-group{opacity:.45;pointer-events:none}
244
+ :host([hidden]){display:none!important}`;
245
+ this.caption.name='command-caption';
246
+ this.#group=doc.createElement('div');this.#group.className='menu-group';
247
+ this.#group.append(doc.createElement('slot'));this.#group.hidden=true;
248
+ this.shadowRoot.append(style,this.#group);
249
+ const slot=doc.createElement('slot'); slot.name='menu'; this.shadowRoot.append(slot);
250
+ this.addEventListener('pointerenter',() => {
251
+ if (this.menuOwner && (!this.menuOwner.permanent || this.menuOwner.isOverflowed(this)) && !this.disabled) {
252
+ clearTimeout(this.#hover);
253
+ this.#hover=setTimeout(()=>{
254
+ if (!this.isConnected || this.disabled || !this.menuOwner.opened) return;
255
+ this.control.focus({preventScroll:true});
256
+ if (this.submenu) this.openSubmenu();
257
+ else for (const sibling of this.menuOwner.items) sibling.submenu?.close(false);
258
+ },180);
259
+ }
260
+ });
261
+ this.addEventListener('pointerleave',()=>clearTimeout(this.#hover));
262
+ }
263
+ get menuOwner() { return this.parentElement?.closest('gramlot-menu') ?? null; }
264
+ get submenu() { return [...this.children].find(child=>child.localName==='gramlot-menu') ?? null; }
265
+ get entryCaption() { return this.parameters.caption ?? this.#registry?.label ?? ''; }
266
+ get entryIcon() { return this.parameters.icon || this.parameters.iconSrc ? this.parameters.icon : this.#registry?.icon; }
267
+ get entryIconSrc() { return this.parameters.icon || this.parameters.iconSrc ? this.parameters.iconSrc : this.#registry?.iconSrc; }
268
+ get occurrence() { return this.parameters.code ?? this.sourceLabel; }
269
+ setCommandDefinition(definition, sourceLabel) {
270
+ this.#registry=definition; this.sourceLabel=sourceLabel; this.#render();
271
+ }
272
+ projectAttributes(attrs,adapt) {
273
+ const rest=super.projectAttributes(attrs,adapt);
274
+ this.#accessibleLabel=attrs['aria-label'];
275
+ if(this.parameters.iconSize!=null&&(!Number.isFinite(this.parameters.iconSize)||this.parameters.iconSize<=0))throw new TypeError('menuline iconSize must be positive');
276
+ if(!Number.isFinite(this.parameters.priority))throw new TypeError('menuline priority must be finite');
277
+ this.#authoredDisabled=Boolean(this.parameters.disabled);
278
+ this.#render(); this.menuOwner?.overflow.schedule(); return rest;
279
+ }
280
+ #render() {
281
+ const separator=[...this.childNodes].filter(node=>node.nodeType===3)
282
+ .map(node=>node.textContent).join('').trim()==='-' && !this.parameters.command;
283
+ this.setAttribute('role',this.parameters.group?'group':separator?'separator':this.parameters.checked==null?'menuitem':'menuitemcheckbox');
284
+ this.#group.hidden=!this.parameters.group;
285
+ this.setAttribute('aria-disabled',String(this.#authoredDisabled));
286
+ if (this.parameters.checked!=null) {
287
+ this.setAttribute('aria-checked',String(this.parameters.checked));
288
+ this.control.setAttribute('aria-checked',String(this.parameters.checked));
289
+ } else { this.removeAttribute('aria-checked'); this.control.removeAttribute('aria-checked'); }
290
+ this.#check.textContent=this.parameters.checked ? '✓':'';
291
+ this.#caption.textContent=this.parameters.command && !this.#registry ? '???':this.entryCaption;
292
+ this.caption.hidden=true;
293
+ this.renderIcon(this.entryIcon, this.entryIconSrc);
294
+ const tooltip=this.parameters.tooltip ?? this.#registry?.tooltip ?? this.entryCaption;
295
+ this.control.title=tooltip;
296
+ this.control.setAttribute('aria-label',this.#accessibleLabel ?? (this.entryCaption || tooltip));
297
+ if (this.submenu) this.control.setAttribute('aria-haspopup','menu');
298
+ else this.control.removeAttribute('aria-haspopup');
299
+ this.refreshAvailability();
300
+ }
301
+ refreshAvailability() {
302
+ const owner=this.menuOwner;
303
+ const popup = owner && (!owner.permanent || owner.isOverflowed(this));
304
+ const inherited=name=>{
305
+ for(let node=this;node && node!==owner;node=node.parentElement){
306
+ if(node.localName==='gramlot-menu-line' && node.parameters[name]!=null)return node.parameters[name];
307
+ }
308
+ return owner?.parameters[name];
309
+ };
310
+ const toolbar=Boolean(owner?.permanent && !popup);
311
+ const labelPosition=inherited('labelPosition');
312
+ this.control.classList.toggle('toolbar-button',toolbar);
313
+ this.control.classList.toggle('toolbar-below',toolbar && labelPosition==='below');
314
+ this.control.classList.toggle('toolbar-tooltip',toolbar && labelPosition==='tooltip');
315
+ if(toolbar)this.control.style.setProperty('--gramlot-toolbar-icon-size',`${inherited('iconSize')}px`);
316
+ else this.control.style.removeProperty('--gramlot-toolbar-icon-size');
317
+ this.toggleAttribute('data-menu-group-rows',Boolean(popup || owner?.parameters.orientation==='vertical'));
318
+ this.control.classList.toggle('menu-row', popup);
319
+ this.#arrow.textContent = this.submenu ? (popup || owner?.parameters.orientation==='vertical' ? '›' : '▾') : '';
320
+ const disabled=this.#authoredDisabled || this.getAttribute('role')==='separator'
321
+ || Boolean(this.parameters.command && !this.#registry)
322
+ || ['loading','error'].includes(owner?.status)
323
+ || Boolean(this.closest('gramlot-drop-down-button')?.parameters.disabled);
324
+ if(this.control.disabled!==disabled)this.control.disabled=disabled;
325
+ }
326
+ get disabled() {
327
+ for(let p=this.parentElement;p && p!==this.menuOwner;p=p.parentElement)if(p.localName==='gramlot-menu-line' && p.parameters.disabled)return true;
328
+ return this.control.disabled || Boolean(this.closest('gramlot-drop-down-button')?.parameters.disabled); }
329
+ set disabled(value) { this.control.disabled=Boolean(value); }
330
+ attachCommand() { this.#render(); }
331
+ releaseCommand() { clearTimeout(this.#hover); this.submenu?.close(false); }
332
+ openSubmenu() {
333
+ if (this.disabled || !this.submenu) return;
334
+ for (const sibling of this.menuOwner.items) if (sibling!==this) sibling.submenu?.close(false);
335
+ this.submenu.open(this.control);
336
+ }
337
+ prepareCommand(event) {
338
+ if (this.parameters.group || this.disabled || this.hidden) return false;
339
+ if (this.submenu) { event.stopPropagation(); if(this.submenu.opened)this.submenu.close();else this.openSubmenu();return false; }
340
+ const drop=this.menuOwner?.parentElement;
341
+ if (drop?.localName==='gramlot-drop-down-button') drop.selectCommand(this,event.shiftKey);
342
+ else this.menuOwner.closeChain();
343
+ return true;
344
+ }
345
+ };
346
+ }
@@ -2,14 +2,27 @@
2
2
  const VIEWPORT_MARGIN = 8;
3
3
  const ANCHOR_GAP = 3;
4
4
 
5
- export function positionPopup(popup, anchor, {align = 'start', matchWidth = false, inset = 0} = {}) {
5
+ export function positionPopup(popup, anchor, {align = 'start', matchWidth = false, inset = 0, placement = 'bottom', point = null} = {}) {
6
6
  const view = anchor.ownerDocument.defaultView;
7
- const field = anchor.getBoundingClientRect();
7
+ const field = point ? {left:point.x,right:point.x,top:point.y,bottom:point.y,width:0}
8
+ : anchor.getBoundingClientRect();
8
9
  const availableWidth = Math.max(0, view.innerWidth - 2 * VIEWPORT_MARGIN);
9
10
  popup.style.setProperty('--gramlot-popup-available-width', `${availableWidth}px`);
10
11
  if (matchWidth) popup.style.width = `${Math.min(Math.max(0, field.width - 2 * inset), availableWidth)}px`;
11
12
  popup.style.removeProperty('--gramlot-popup-available-height');
12
13
  const natural = popup.getBoundingClientRect();
14
+ if (placement === 'side') {
15
+ const right = Math.max(0, view.innerWidth - VIEWPORT_MARGIN - field.right - ANCHOR_GAP);
16
+ const left = Math.max(0, field.left - VIEWPORT_MARGIN - ANCHOR_GAP);
17
+ const opensLeft = natural.width > right && left > right;
18
+ popup.style.setProperty('--gramlot-popup-available-width', `${opensLeft ? left : right}px`);
19
+ popup.style.setProperty('--gramlot-popup-available-height', `${Math.max(0, view.innerHeight - 2 * VIEWPORT_MARGIN)}px`);
20
+ const size = popup.getBoundingClientRect();
21
+ const x = opensLeft ? field.left - size.width - ANCHOR_GAP : field.right + ANCHOR_GAP;
22
+ popup.style.left = `${Math.max(VIEWPORT_MARGIN, Math.min(x, view.innerWidth - size.width - VIEWPORT_MARGIN))}px`;
23
+ popup.style.top = `${Math.max(VIEWPORT_MARGIN, Math.min(field.top, view.innerHeight - size.height - VIEWPORT_MARGIN))}px`;
24
+ return;
25
+ }
13
26
  const below = Math.max(0, view.innerHeight - VIEWPORT_MARGIN - field.bottom - ANCHOR_GAP);
14
27
  const above = Math.max(0, field.top - VIEWPORT_MARGIN - ANCHOR_GAP);
15
28
  const upwards = natural.height > below && above > below;
@@ -6,8 +6,9 @@ function freeze(value) {
6
6
  return Object.freeze(value);
7
7
  }
8
8
  function annotation(parameter) {
9
- if (typeof parameter.dtype !== 'string' && !Array.isArray(parameter.dtype)) throw new TypeError('Component parameter requires dtype');
10
- const alternatives = (Array.isArray(parameter.dtype) ? parameter.dtype : [parameter.dtype]).map(dtype => ({kind: 'dtype', dtype}));
9
+ if (!parameter.annotation && typeof parameter.dtype !== 'string' && !Array.isArray(parameter.dtype)) throw new TypeError('Component parameter requires dtype');
10
+ const alternatives = parameter.annotation ? [structuredClone(parameter.annotation)]
11
+ : (Array.isArray(parameter.dtype) ? parameter.dtype : [parameter.dtype]).map(dtype => ({kind: 'dtype', dtype}));
11
12
  if (parameter.nullable) alternatives.push({kind: 'dtype', dtype: 'NN'});
12
13
  if (parameter.binding && parameter.dtype !== 'T') alternatives.push({kind: 'annotated',
13
14
  base: {kind: 'dtype', dtype: 'T'},
@@ -34,7 +35,21 @@ export function defineComponent({name, tag, base, mixins = [], parameters = {},
34
35
 
35
36
  export class ComponentCollection {
36
37
  #classes = new WeakMap();
37
- constructor({name, components}) {
38
+ constructor({name, components, dependencies = [], css = [], assets = {}, registerCommands = () => {}}) {
39
+ if (typeof name !== 'string' || !name) throw new TypeError('Collection name must be nonempty');
40
+ if (!Array.isArray(dependencies) || dependencies.some(item => !(item instanceof ComponentCollection))) {
41
+ throw new TypeError('Collection dependencies must be collection objects');
42
+ }
43
+ if (!Array.isArray(css) || css.some(url => !(url instanceof URL)) ||
44
+ !assets || typeof assets !== 'object' || Array.isArray(assets) ||
45
+ Object.values(assets).some(url => !(url instanceof URL))) {
46
+ throw new TypeError('Collection css/assets require URL objects');
47
+ }
48
+ if (typeof registerCommands !== 'function') throw new TypeError('registerCommands must be a function');
49
+ this.dependencies = Object.freeze([...dependencies]);
50
+ this.css = Object.freeze([...css]);
51
+ this.assets = Object.freeze({...assets});
52
+ this.registerCommands = registerCommands;
38
53
  this.name = name;
39
54
  this.components = Object.freeze([...components]);
40
55
  const names = new Set(), tags = new Set();
@@ -0,0 +1,181 @@
1
+ import {SVG_NS} from '../renderer/attributes.js';
2
+ /** Responsive menu projection. Original Source and light-DOM nodes never move. */
3
+ import {positionPopup} from './popup.js';
4
+
5
+ export class ToolbarOverflow {
6
+ #menu; #viewport; #panel; #button; #resize; #events; #openEvents;
7
+ #frame = null; #opened = false; #units = []; #overflow = new Set();
8
+ constructor(menu, slot) {
9
+ this.#menu=menu;
10
+ const doc=menu.ownerDocument;
11
+ this.#viewport=doc.createElement('div'); this.#viewport.className='toolbar-viewport';
12
+ this.#viewport.append(slot);
13
+ this.#button=doc.createElement('button'); this.#button.type='button';
14
+ this.#button.className='toolbar-more'; this.#button.hidden=true;
15
+ this.#button.title='More commands';
16
+ const icon=doc.createElementNS(SVG_NS,'svg');
17
+ icon.setAttribute('viewBox','0 0 24 24'); icon.setAttribute('aria-hidden','true'); icon.setAttribute('focusable','false');
18
+ const path=doc.createElementNS(SVG_NS,'path');
19
+ path.setAttribute('d','m5 6 6 6-6 6m8-12 6 6-6 6'); path.setAttribute('fill','none');
20
+ path.setAttribute('stroke','currentColor'); path.setAttribute('stroke-width','1.8');
21
+ path.setAttribute('stroke-linecap','round'); path.setAttribute('stroke-linejoin','round');
22
+ icon.append(path); this.#button.append(icon);
23
+ this.#button.setAttribute('aria-label','More commands'); this.#button.setAttribute('aria-haspopup','menu');
24
+ this.#button.setAttribute('aria-expanded','false');
25
+ this.#panel=doc.createElement('div'); this.#panel.className='toolbar-overflow';
26
+ this.#panel.setAttribute('popover','manual'); this.#panel.setAttribute('role','menu'); this.#panel.tabIndex=-1;
27
+ const overflowSlot=doc.createElement('slot'); overflowSlot.name='overflow'; this.#panel.append(overflowSlot);
28
+ this.#button.addEventListener('click',()=>this.#opened?this.close():this.open());
29
+ this.#button.addEventListener('keydown',event=>{
30
+ if(['ArrowDown','ArrowRight'].includes(event.key)){event.preventDefault();event.stopPropagation();this.open();}
31
+ });
32
+ this.#panel.addEventListener('keydown',event=>{
33
+ if(event.key==='Escape' && !event.composedPath().some(node=>node.localName==='gramlot-menu' && node!==menu)){
34
+ event.preventDefault();event.stopPropagation();this.close();
35
+ }
36
+ });
37
+ const style=doc.createElement('style');
38
+ style.textContent=`
39
+ :host([role=toolbar]){min-width:0;min-height:0;max-width:100%;width:100%;flex-shrink:1;gap:0}
40
+ :host([role=toolbar][aria-orientation=vertical]){width:auto;height:100%;max-height:100%}
41
+ .toolbar-viewport{display:contents}
42
+ :host([role=toolbar]) .toolbar-viewport{display:flex;align-items:center;gap:var(--gramlot-toolbar-gap,4px);flex:1 1 0%;min-width:0;min-height:0;overflow-x:auto;overflow-y:hidden}
43
+ :host([role=toolbar][aria-orientation=vertical]) .toolbar-viewport{flex-direction:column;align-items:stretch;overflow-x:hidden;overflow-y:auto;width:100%}
44
+ :host([role=toolbar]) ::slotted(*){flex-shrink:0}
45
+ .toolbar-more{flex:0 0 auto;width:max(var(--gramlot-field-height,32px),var(--gramlot-touch-target,0px));height:max(var(--gramlot-field-height,32px),var(--gramlot-touch-target,0px));padding:0;border:0;border-radius:4px;background:transparent;color:inherit;font:inherit;display:grid;place-items:center}
46
+ .toolbar-more svg{display:block;width:18px;height:18px;pointer-events:none}
47
+ .toolbar-more:hover,.toolbar-more:focus-visible{background:var(--gramlot-selected,Highlight)}
48
+ :host([role=toolbar][aria-orientation=vertical]) .toolbar-more{width:100%}
49
+ .toolbar-more[hidden]{display:none}
50
+ .toolbar-overflow{position:fixed;inset:auto;margin:0;padding:5px;box-sizing:border-box;min-width:min(16rem,var(--gramlot-popup-available-width));width:max-content;
51
+ max-width:var(--gramlot-popup-available-width);max-height:var(--gramlot-popup-available-height);overflow:auto;
52
+ background:var(--gramlot-field-background,Canvas);color:var(--gramlot-ink,CanvasText);border:1px solid var(--gramlot-border-control,#888);border-radius:var(--gramlot-radius-lg,9px);box-shadow:0 8px 28px #0004}
53
+ .toolbar-overflow ::slotted([role=separator]){margin:5px 7px;border-top:1px solid var(--gramlot-border-control,#888)}
54
+ `;
55
+ menu.shadowRoot.append(style,this.#viewport,this.#button,this.#panel);
56
+ }
57
+ get opened(){return this.#opened;}
58
+ get button(){return this.#button;}
59
+ get panel(){return this.#panel;}
60
+ contains(item){
61
+ let unit=item;
62
+ while(unit && unit.parentElement!==this.#menu)unit=unit.parentElement;
63
+ return this.#overflow.has(unit);
64
+ }
65
+ connect(){
66
+ if(!this.#menu.permanent || this.#resize)return;
67
+ const win=this.#menu.ownerDocument.defaultView;
68
+ this.#events=new win.AbortController();
69
+ this.#resize=new win.ResizeObserver(()=>this.schedule());
70
+ this.#resize.observe(this.#menu);
71
+ this.#menu.ownerDocument.fonts.addEventListener('loadingdone',()=>this.schedule(),{signal:this.#events.signal});
72
+ this.schedule();
73
+ }
74
+ disconnect(){
75
+ this.close(false); this.#resize?.disconnect();this.#resize=null;this.#events?.abort();
76
+ if(this.#frame!==null)this.#menu.ownerDocument.defaultView.cancelAnimationFrame(this.#frame);
77
+ this.#frame=null;this.#units=[];
78
+ }
79
+ schedule(){
80
+ if(!this.#menu.isConnected)return;
81
+ if(!this.#menu.permanent){
82
+ this.disconnect();
83
+ for(const unit of this.#menu.children){unit.removeAttribute('slot');unit.removeAttribute('data-menu-overflow');}
84
+ this.#overflow.clear();this.#button.hidden=true;return;
85
+ }
86
+ if(!this.#resize)this.connect();
87
+ if(this.#frame!==null)return;
88
+ this.#frame=this.#menu.ownerDocument.defaultView.requestAnimationFrame(()=>{this.#frame=null;this.layout();});
89
+ }
90
+ layout(){
91
+ const menu=this.#menu,win=menu.ownerDocument.defaultView;
92
+ if(!menu.permanent || !menu.isConnected)return;
93
+ const vertical=menu.parameters.orientation==='vertical',axis=vertical?'height':'width';
94
+ const style=win.getComputedStyle(menu);
95
+ const available=(vertical?menu.clientHeight:menu.clientWidth)-parseFloat(style[vertical?'paddingTop':'paddingLeft']||0)-parseFloat(style[vertical?'paddingBottom':'paddingRight']||0);
96
+ if(available<=0)return;
97
+ let active=menu.ownerDocument.activeElement;
98
+ while(active?.shadowRoot?.activeElement)active=active.shadowRoot.activeElement;
99
+ const units=[...menu.children].filter(unit=>!unit.hidden && (unit.localName!=='gramlot-menu' || unit.permanent));
100
+ for(const unit of this.#units)if(!units.includes(unit)){
101
+ this.#resize.unobserve(unit);unit.removeAttribute('slot');unit.removeAttribute('data-menu-overflow');
102
+ }
103
+ for(const unit of units)if(!this.#units.includes(unit))this.#resize.observe(unit);
104
+ this.#units=units;
105
+ // Measure each original node in its toolbar presentation, then distribute
106
+ // slots synchronously before paint. No clones or Source changes are made.
107
+ for(const unit of units){unit.removeAttribute('slot');unit.removeAttribute('data-menu-overflow');}
108
+ this.#overflow.clear();
109
+ menu.refreshItems();
110
+ for(const item of menu.items)item.refreshAvailability();
111
+ const gap=parseFloat(win.getComputedStyle(this.#viewport).gap)||0;
112
+ const lengths=new Map(units.map(unit=>{
113
+ const css=win.getComputedStyle(unit);
114
+ const length=unit.localName==='gramlot-spacer'?(unit.parameters.size??0):unit.getBoundingClientRect()[axis];
115
+ return [unit,length+(parseFloat(css[vertical?'marginTop':'marginLeft'])||0)+(parseFloat(css[vertical?'marginBottom':'marginRight'])||0)];
116
+ }));
117
+ const separator=unit=>unit.getAttribute('role')==='separator';
118
+ const tidy=list=>list.filter((unit,index)=>!separator(unit) ||
119
+ list.slice(0,index).some(item=>item.localName!=='gramlot-spacer'&&!separator(item)) &&
120
+ list.slice(index+1).find(item=>item.localName!=='gramlot-spacer') &&
121
+ !separator(list.slice(index+1).find(item=>item.localName!=='gramlot-spacer')));
122
+ const required=remaining=>remaining.reduce((sum,unit)=>sum+lengths.get(unit),0)+Math.max(0,remaining.length-1)*gap;
123
+ let remaining=tidy(units);
124
+ if(required(remaining)>available){
125
+ const candidates=units.map((unit,index)=>({unit,index})).filter(({unit})=>unit.localName==='gramlot-menu-line' && !separator(unit) && unit.parameters.overflowable)
126
+ .sort((a,b)=>a.unit.parameters.priority-b.unit.parameters.priority || b.index-a.index);
127
+ for(const {unit} of candidates){
128
+ if(required(remaining)<=available-32)break;
129
+ this.#overflow.add(unit);remaining=tidy(remaining.filter(item=>item!==unit));
130
+ }
131
+ }
132
+ const overflowRows=tidy(units.filter(unit=>this.#overflow.has(unit)||separator(unit)&&!remaining.includes(unit)));
133
+ for(const unit of overflowRows)this.#overflow.add(unit);
134
+ for(const unit of units)if(separator(unit)&&!remaining.includes(unit)&&!this.#overflow.has(unit))unit.slot='suppressed';
135
+ for(const unit of this.#overflow){unit.slot='overflow';unit.setAttribute('data-menu-overflow','');}
136
+ this.#button.hidden=this.#overflow.size===0;
137
+ for(const item of menu.items)item.refreshAvailability();
138
+ menu.refreshItems();
139
+ const activeUnit=units.find(unit=>{
140
+ let node=active;
141
+ while(node){if(node===unit)return true;node=node.parentNode??node.host;}
142
+ return false;
143
+ });
144
+ if(this.#overflow.size===0)this.close(false);
145
+ else if(activeUnit && this.#overflow.has(activeUnit))this.open(false);
146
+ else if(this.#opened)this.#place();
147
+ if(activeUnit && active?.isConnected)active.focus({preventScroll:true});
148
+ menu.refreshItems();
149
+ }
150
+ #place(){positionPopup(this.#panel,this.#button,{placement:this.#menu.parameters.orientation==='vertical'?'side':'bottom'});}
151
+ open(focus=true){
152
+ if(!this.#overflow.size)return;
153
+ if(!this.#opened){
154
+ this.#panel.showPopover();this.#opened=true;this.#button.setAttribute('aria-expanded','true');
155
+ this.#openEvents=new this.#menu.ownerDocument.defaultView.AbortController();
156
+ const options={signal:this.#openEvents.signal};
157
+ this.#menu.ownerDocument.addEventListener('pointerdown',event=>{
158
+ if(!event.composedPath().includes(this.#panel)&&!event.composedPath().includes(this.#button))this.close(false);
159
+ },options);
160
+ this.#menu.ownerDocument.addEventListener('scroll',()=>this.#place(),{...options,capture:true});
161
+ }
162
+ this.#menu.refreshItems();this.#place();
163
+ if(focus){
164
+ const focusable=element=>{
165
+ if(element.hidden || element.disabled)return null;
166
+ if(element.localName==='gramlot-menu-line'&&!element.parameters.group)return element.getAttribute('role')==='separator'?null:element;
167
+ if(element.matches('input:not([type=hidden]),textarea,select,button,[tabindex="0"]'))return element;
168
+ for(const child of element.children){const found=focusable(child);if(found)return found;}
169
+ return null;
170
+ };
171
+ const target=[...this.#menu.children].filter(unit=>this.#overflow.has(unit)).map(focusable).find(Boolean);
172
+ (target??this.#panel).focus();
173
+ }
174
+ }
175
+ close(restore=true){
176
+ if(!this.#opened)return;
177
+ for(const unit of this.#overflow)for(const child of unit.querySelectorAll('gramlot-menu'))if(!child.permanent)child.close(false);
178
+ this.#panel.hidePopover();this.#opened=false;this.#openEvents?.abort();this.#button.setAttribute('aria-expanded','false');
179
+ if(restore&&!this.#button.hidden)this.#button.focus();
180
+ }
181
+ }