@octolo/js-contextmenu-builder 0.2.0 → 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.
package/dist/index.js CHANGED
@@ -15,6 +15,14 @@ function cloneItems(items) {
15
15
  return { type: "heading", id, label: item.label };
16
16
  }
17
17
  if (item.type === "separator") return { type: "separator", id };
18
+ if (item.type === "block") {
19
+ return {
20
+ type: "block",
21
+ id,
22
+ ...item.lines !== void 0 ? { lines: item.lines } : {},
23
+ items: cloneItems(item.items ?? [])
24
+ };
25
+ }
18
26
  if (item.type === "checkbox") {
19
27
  return {
20
28
  type: "checkbox",
@@ -25,7 +33,8 @@ function cloneItems(items) {
25
33
  ...item.disabled !== void 0 ? { disabled: item.disabled } : {},
26
34
  ...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
27
35
  ...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
28
- ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {}
36
+ ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
37
+ ...presentation(item)
29
38
  };
30
39
  }
31
40
  if (item.type === "radio") {
@@ -39,7 +48,8 @@ function cloneItems(items) {
39
48
  ...item.disabled !== void 0 ? { disabled: item.disabled } : {},
40
49
  ...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
41
50
  ...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
42
- ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {}
51
+ ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
52
+ ...presentation(item)
43
53
  };
44
54
  }
45
55
  return {
@@ -51,10 +61,24 @@ function cloneItems(items) {
51
61
  ...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
52
62
  ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
53
63
  ...item.input !== void 0 ? { input: item.input } : {},
64
+ ...presentation(item),
65
+ ...linkFields(item),
54
66
  ...item.subMenu ? { subMenu: cloneItems(item.subMenu) } : {}
55
67
  };
56
68
  });
57
69
  }
70
+ function presentation(item) {
71
+ return {
72
+ ...item.danger ? { danger: true } : {},
73
+ ...item.detail ? { detail: item.detail } : {}
74
+ };
75
+ }
76
+ function linkFields(item) {
77
+ return {
78
+ ...item.href ? { href: item.href } : {},
79
+ ...item.target ? { target: item.target } : {}
80
+ };
81
+ }
58
82
  function cloneInlineActions(actions) {
59
83
  return actions.map((action) => ({ ...action, icon: cloneIcon(action.icon) }));
60
84
  }
@@ -72,6 +96,12 @@ function toPublic(items) {
72
96
  return out2;
73
97
  }
74
98
  if (item.type === "separator") return { type: "separator" };
99
+ if (item.type === "block") {
100
+ const out2 = { type: "block" };
101
+ if (item.lines !== void 0) out2.lines = item.lines;
102
+ if (item.items.length > 0) out2.items = toPublic(item.items);
103
+ return out2;
104
+ }
75
105
  if (item.type === "checkbox") {
76
106
  const out2 = {
77
107
  type: "checkbox",
@@ -84,6 +114,7 @@ function toPublic(items) {
84
114
  if (item.shortcut) out2.shortcut = item.shortcut;
85
115
  if (item.badge !== void 0) out2.badge = { ...item.badge };
86
116
  if (item.inlineActions !== void 0) out2.inlineActions = cloneInlineActions(item.inlineActions);
117
+ Object.assign(out2, presentation(item));
87
118
  return out2;
88
119
  }
89
120
  if (item.type === "radio") {
@@ -99,6 +130,7 @@ function toPublic(items) {
99
130
  if (item.shortcut) out2.shortcut = item.shortcut;
100
131
  if (item.badge !== void 0) out2.badge = { ...item.badge };
101
132
  if (item.inlineActions !== void 0) out2.inlineActions = cloneInlineActions(item.inlineActions);
133
+ Object.assign(out2, presentation(item));
102
134
  return out2;
103
135
  }
104
136
  const out = { label: item.label };
@@ -109,6 +141,7 @@ function toPublic(items) {
109
141
  if (item.badge !== void 0) out.badge = { ...item.badge };
110
142
  if (item.inlineActions !== void 0) out.inlineActions = cloneInlineActions(item.inlineActions);
111
143
  if (item.input) out.input = true;
144
+ Object.assign(out, presentation(item), linkFields(item));
112
145
  if (item.subMenu && item.subMenu.length > 0) out.subMenu = toPublic(item.subMenu);
113
146
  return out;
114
147
  });
@@ -130,8 +163,9 @@ function findItem(items, id) {
130
163
  const item = items[index];
131
164
  if (!item) continue;
132
165
  if (item.id === id) return { item, list: items, index };
133
- if (item.type === void 0 && item.subMenu) {
134
- const nested = findItem(item.subMenu, id);
166
+ const children = item.type === "block" ? item.items : item.type === void 0 ? item.subMenu : void 0;
167
+ if (children) {
168
+ const nested = findItem(children, id);
135
169
  if (nested) return nested;
136
170
  }
137
171
  }
@@ -164,6 +198,9 @@ function emptySeparator() {
164
198
  function emptyHeading(label = "New section") {
165
199
  return { type: "heading", id: nextId(), label };
166
200
  }
201
+ function emptyBlock() {
202
+ return { type: "block", id: nextId(), items: [] };
203
+ }
167
204
  function readIconFields(icon) {
168
205
  if (icon == null || icon === "") return { text: "", className: "" };
169
206
  if (typeof icon === "string") return { text: icon, className: "" };
@@ -189,6 +226,7 @@ function itemKind(item) {
189
226
  if (item.type === "separator") return "separator";
190
227
  if (item.type === "checkbox") return `checkbox (${item.checked ? "checked" : "unchecked"})`;
191
228
  if (item.type === "radio") return `option (${item.checked ? "selected" : "not selected"})`;
229
+ if (item.type === "block") return item.lines ? `block, ${item.lines} lines` : "block";
192
230
  if (item.subMenu && item.subMenu.length > 0) return "submenu";
193
231
  if (item.input) return "file";
194
232
  if (item.disabled) return "disabled";
@@ -199,7 +237,7 @@ function leafActions(id, canHaveChildren) {
199
237
  return `<span class="jscmb-actions">${actionIcon("edit", id, "Edit", "\u2699")}${addChild}${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
200
238
  }
201
239
  function nestedActions(id) {
202
- return `<span class="jscmb-actions">${actionIcon("add-child", id, "Add sub-item", "\u25A3")}${actionIcon("add-heading", id, "Add heading", "T")}${actionIcon("add-separator", id, "Add separator", "\u2015")}${actionIcon("edit", id, "Edit", "\u2699")}${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
240
+ return `<span class="jscmb-actions">${actionIcon("add-child", id, "Add sub-item", "\u25A3")}${actionIcon("add-heading", id, "Add heading", "T")}${actionIcon("add-separator", id, "Add separator", "\u2015")}${actionIcon("add-block", id, "Add block", "\u25A2")}${actionIcon("edit", id, "Edit", "\u2699")}${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
203
241
  }
204
242
  function separatorActions(id) {
205
243
  return `<span class="jscmb-actions">${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
@@ -214,11 +252,17 @@ function renderHeading(item) {
214
252
  function renderLeaf(item) {
215
253
  const label = escapeText(item.label);
216
254
  const kind = escapeText(itemKind(item));
255
+ const notes = [
256
+ kind,
257
+ item.danger ? "destructive" : "",
258
+ item.detail ? "detail" : "",
259
+ item.type === void 0 && item.href ? "link" : ""
260
+ ].filter(Boolean).join(", ");
217
261
  const fields = readIconFields(item.icon);
218
262
  const preview = escapeText(iconPreview(item.icon) || (fields.className ? "" : "\xB7"));
219
263
  const iconClass = ["jscmb-item-icon", fields.className].filter(Boolean).join(" ");
220
264
  const shortcut = item.shortcut ? `<kbd class="jscmb-shortcut">${escapeText(item.shortcut)}</kbd>` : "";
221
- return `<div class="jscmb-item" data-node-id="${escapeText(item.id)}"><b class="jscmb-handle" title="${preview || fields.className}">\u2630</b><span class="jscmb-name" title="${label} (${kind})"><span class="${escapeText(iconClass)}" aria-hidden="true">${preview}</span>${label} <small>(${kind})</small></span>${shortcut}${leafActions(item.id, item.type === void 0)}</div>`;
265
+ return `<div class="jscmb-item" data-node-id="${escapeText(item.id)}"><b class="jscmb-handle" title="${preview || fields.className}">\u2630</b><span class="jscmb-name" title="${label} (${notes})"><span class="${escapeText(iconClass)}" aria-hidden="true">${preview}</span>${label} <small>(${notes})</small></span>${shortcut}${leafActions(item.id, item.type === void 0)}</div>`;
222
266
  }
223
267
  function renderSeparator(item) {
224
268
  return `<div class="jscmb-item jscmb-separator-node" data-node-id="${escapeText(item.id)}"><b class="jscmb-handle" title="Separator">\u2630</b><span class="jscmb-separator-preview" role="separator" aria-label="Separator"></span>${separatorActions(item.id)}</div>`;
@@ -233,9 +277,15 @@ function renderNested(item) {
233
277
  const children = item.subMenu ?? [];
234
278
  return `<div class="jscmb-nested" data-node-id="${escapeText(item.id)}"><div class="jscmb-nested-header"><b class="jscmb-handle" title="${preview}">\u2630</b><strong class="jscmb-name" title="${label}"><span class="jscmb-title">${label}</span><small>submenu</small></strong>${nestedActions(item.id)}</div>${renderList(children, item.id)}</div>`;
235
279
  }
280
+ function renderBlock(item) {
281
+ const children = item.items ?? [];
282
+ const kind = item.lines ? `block, ${item.lines} lines` : "block";
283
+ return `<div class="jscmb-nested" data-node-id="${escapeText(item.id)}"><div class="jscmb-nested-header"><b class="jscmb-handle" title="Block">\u2630</b><strong class="jscmb-name" title="Block"><span class="jscmb-title">Block</span><small>${escapeText(kind)}</small></strong>${nestedActions(item.id)}</div>${renderList(children, item.id)}</div>`;
284
+ }
236
285
  function renderNode(item) {
237
286
  if (item.type === "heading") return renderHeading(item);
238
287
  if (item.type === "separator") return renderSeparator(item);
288
+ if (item.type === "block") return renderBlock(item);
239
289
  if (item.type === void 0 && item.subMenu && item.subMenu.length > 0) {
240
290
  return renderNested(item);
241
291
  }
@@ -243,7 +293,7 @@ function renderNode(item) {
243
293
  }
244
294
  function renderTree(items, title = "Menu") {
245
295
  const heading = escapeText(title);
246
- return `<div class="jscmb-nested jscmb-root"><div class="jscmb-nested-header"><strong class="jscmb-name" title="${heading}"><span class="jscmb-title">${heading}</span><small>menu</small></strong><span class="jscmb-actions">${actionIcon("add-root", "root", "Add action", "\u25A3")}${actionIcon("add-heading", "root", "Add heading", "T")}${actionIcon("add-separator", "root", "Add separator", "\u2015")}</span></div>${renderList(items, "root")}</div>`;
296
+ return `<div class="jscmb-nested jscmb-root"><div class="jscmb-nested-header"><strong class="jscmb-name" title="${heading}"><span class="jscmb-title">${heading}</span><small>menu</small></strong><span class="jscmb-actions">${actionIcon("add-root", "root", "Add action", "\u25A3")}${actionIcon("add-heading", "root", "Add heading", "T")}${actionIcon("add-separator", "root", "Add separator", "\u2015")}${actionIcon("add-block", "root", "Add block", "\u25A2")}</span></div>${renderList(items, "root")}</div>`;
247
297
  }
248
298
 
249
299
  // src/palette.ts
@@ -415,6 +465,7 @@ function shell(options, name, appearance) {
415
465
  <section class="jscmb-preview-panel" data-preview tabindex="0">
416
466
  <strong>Test area</strong>
417
467
  <p>${hint}</p>
468
+ ${options.previewDropdown ? '<button type="button" data-preview-dropdown>Open dropdown <span aria-hidden="true">\u25BE</span></button>' : ""}
418
469
  <p class="jscmb-log" data-log></p>
419
470
  </section>
420
471
  <details>
@@ -435,16 +486,20 @@ function shell(options, name, appearance) {
435
486
  <form method="dialog" data-editor>
436
487
  <h2 data-editor-title>Item</h2>
437
488
  <div class="jscmb-dialog-body">
438
- <label>Label<input name="label" type="text" required></label>
489
+ <label data-label-field>Label<input name="label" type="text" required></label>
439
490
  <label>Type<select name="itemType">
440
491
  <option value="action">Action</option>
441
492
  <option value="checkbox">Checkbox</option>
442
493
  <option value="radio">Radio option</option>
443
494
  <option value="heading">Heading</option>
495
+ <option value="block">Block</option>
444
496
  </select></label>
445
497
  <label data-item-field>Symbol (Unicode / text)<input name="iconText" type="text" placeholder="\u2605 or content_copy"></label>
446
498
  <label data-item-field>CSS class (optional)<input name="iconClass" type="text" placeholder="material-symbols-outlined or fa-solid fa-star"></label>
447
499
  <label data-item-field>Displayed shortcut<input name="shortcut" type="text" placeholder="\u2318S"></label>
500
+ <label data-item-field>Detail<input name="detail" type="text" placeholder="Second line"></label>
501
+ <label data-item-field data-link-field>Link URL<input name="href" type="text" placeholder="https://\u2026"></label>
502
+ <label data-item-field data-link-field>Link target<input name="target" type="text" placeholder="_self or _blank"></label>
448
503
  <label class="jscmb-badge-toggle" data-item-field><input name="badgeEnabled" type="checkbox"> Notification badge</label>
449
504
  <fieldset class="jscmb-badge-fields" data-badge-fields data-item-field disabled>
450
505
  <legend>Notification badge</legend>
@@ -455,10 +510,12 @@ function shell(options, name, appearance) {
455
510
  </fieldset>
456
511
  <div class="jscmb-checks" data-item-field>
457
512
  <label><input name="disabled" type="checkbox"> Disabled</label>
513
+ <label><input name="danger" type="checkbox"> Destructive</label>
458
514
  <label data-file-input-field><input name="input" type="checkbox"> File picker</label>
459
515
  <label data-checked-field><input name="checked" type="checkbox"> Checked / selected</label>
460
516
  </div>
461
517
  <label data-radio-group-field data-item-field>Radio group<input name="radioGroup" type="text" value="default"></label>
518
+ <label data-block-field>Visible lines<input name="lines" type="number" min="1" step="1" placeholder="All"></label>
462
519
  <fieldset data-item-field data-inline-field>
463
520
  <legend>Inline icon actions</legend>
464
521
  <div data-inline-actions></div>
@@ -474,15 +531,35 @@ function shell(options, name, appearance) {
474
531
  </dialog>
475
532
  </div>`;
476
533
  }
534
+ function childList(item) {
535
+ if (item.type === "block") {
536
+ if (!item.items) item.items = [];
537
+ return item.items;
538
+ }
539
+ if (item.type === void 0) {
540
+ if (!item.subMenu) item.subMenu = [];
541
+ return item.subMenu;
542
+ }
543
+ return null;
544
+ }
477
545
  function runtimeItems(items, onAction) {
478
546
  return items.map((item) => {
479
547
  if (item.type === "heading") return { type: "heading", label: item.label };
480
548
  if (item.type === "separator") return { type: "separator" };
549
+ if (item.type === "block") {
550
+ return {
551
+ type: "block",
552
+ ...item.lines !== void 0 ? { lines: item.lines } : {},
553
+ ...item.items.length > 0 ? { items: runtimeItems(item.items, onAction) } : {}
554
+ };
555
+ }
481
556
  const base = {
482
557
  label: item.label,
483
558
  ...item.icon ? { icon: item.icon } : {},
484
559
  ...item.disabled ? { disabled: true } : {},
485
560
  ...item.shortcut ? { shortcut: item.shortcut } : {},
561
+ ...item.detail ? { detail: item.detail } : {},
562
+ ...item.danger ? { danger: true } : {},
486
563
  ...item.badge ? { badge: { ...item.badge } } : {},
487
564
  ...item.inlineActions ? {
488
565
  inlineActions: item.inlineActions.map((inlineAction) => ({
@@ -519,11 +596,16 @@ function runtimeItems(items, onAction) {
519
596
  };
520
597
  }
521
598
  if (item.type === void 0 && item.subMenu && item.subMenu.length > 0) {
522
- return { ...base, subMenu: runtimeItems(item.subMenu, onAction) };
599
+ return {
600
+ ...base,
601
+ subMenu: runtimeItems(item.subMenu, onAction)
602
+ };
523
603
  }
524
604
  return {
525
605
  ...base,
526
606
  ...item.input ? { input: true } : {},
607
+ ...item.href ? { href: item.href } : {},
608
+ ...item.target ? { target: item.target } : {},
527
609
  action: (_data, file) => {
528
610
  onAction(file ? `${item.label} \u2192 ${file.name}` : item.label);
529
611
  }
@@ -534,6 +616,7 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
534
616
  let items = cloneItems(initialItems);
535
617
  let editingId = null;
536
618
  let menu = null;
619
+ let previewBindings = [];
537
620
  let theme = normalizeTheme(options.theme);
538
621
  let size = normalizeSize(options.size);
539
622
  let radius = options.radius;
@@ -667,6 +750,8 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
667
750
  optionsJson.textContent = JSON.stringify({ itemStyle, wrapLabels, closeOnMouseLeave, ...radius !== void 0 ? { radius } : {} }, null, 2);
668
751
  }
669
752
  function bindPreview() {
753
+ for (const unbind of previewBindings) unbind();
754
+ previewBindings = [];
670
755
  menu?.destroy();
671
756
  const previewMenu = createContextMenu({
672
757
  mount: document.body,
@@ -680,7 +765,7 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
680
765
  ...options.showShortcuts !== void 0 ? { showShortcuts: options.showShortcuts } : {}
681
766
  });
682
767
  menu = previewMenu;
683
- previewMenu.attach(preview, () => {
768
+ const getPreviewItems = () => {
684
769
  const previewItems = runtimeItems(items, (label, checked) => {
685
770
  log.textContent = checked === void 0 ? `Action: ${label}` : `${label}: ${checked ? "checked" : "unchecked"}`;
686
771
  if (checked !== void 0) {
@@ -689,7 +774,17 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
689
774
  }
690
775
  });
691
776
  return options.transformPreviewItems?.(previewItems, previewMenu) ?? previewItems;
692
- });
777
+ };
778
+ previewBindings.push(previewMenu.attach(preview, getPreviewItems));
779
+ const dropdown = preview.querySelector("[data-preview-dropdown]");
780
+ if (dropdown instanceof HTMLElement && options.previewDropdown) {
781
+ previewBindings.push(previewMenu.attach(dropdown, getPreviewItems, {
782
+ openEvents: ["click"],
783
+ position: "anchor",
784
+ offset: options.previewDropdown.offset ?? 0,
785
+ longPressMs: false
786
+ }));
787
+ }
693
788
  }
694
789
  function redraw() {
695
790
  treeHost.innerHTML = renderTree(items, title);
@@ -836,6 +931,10 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
836
931
  const iconTextInput = form.elements.namedItem("iconText");
837
932
  const iconClassInput = form.elements.namedItem("iconClass");
838
933
  const shortcutInput = form.elements.namedItem("shortcut");
934
+ const detailInput = form.elements.namedItem("detail");
935
+ const hrefInput = form.elements.namedItem("href");
936
+ const targetInput = form.elements.namedItem("target");
937
+ const dangerInput = form.elements.namedItem("danger");
839
938
  const badgeEnabled = form.elements.namedItem("badgeEnabled");
840
939
  const badgeText = form.elements.namedItem("badgeText");
841
940
  const badgeColor = form.elements.namedItem("badgeColor");
@@ -845,11 +944,16 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
845
944
  const fileInput = form.elements.namedItem("input");
846
945
  const checkedInput = form.elements.namedItem("checked");
847
946
  const radioGroupInput = form.elements.namedItem("radioGroup");
947
+ const linesInput = form.elements.namedItem("lines");
848
948
  if (!(labelInput instanceof HTMLInputElement)) return;
849
949
  if (!(typeInput instanceof HTMLSelectElement)) return;
850
950
  if (!(iconTextInput instanceof HTMLInputElement)) return;
851
951
  if (!(iconClassInput instanceof HTMLInputElement)) return;
852
952
  if (!(shortcutInput instanceof HTMLInputElement)) return;
953
+ if (!(detailInput instanceof HTMLInputElement)) return;
954
+ if (!(hrefInput instanceof HTMLInputElement)) return;
955
+ if (!(targetInput instanceof HTMLInputElement)) return;
956
+ if (!(dangerInput instanceof HTMLInputElement)) return;
853
957
  if (!(badgeEnabled instanceof HTMLInputElement)) return;
854
958
  if (!(badgeText instanceof HTMLInputElement)) return;
855
959
  if (!(badgeColor instanceof HTMLInputElement)) return;
@@ -859,14 +963,19 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
859
963
  if (!(fileInput instanceof HTMLInputElement)) return;
860
964
  if (!(checkedInput instanceof HTMLInputElement)) return;
861
965
  if (!(radioGroupInput instanceof HTMLInputElement)) return;
862
- const actionItem = item?.type === "heading" ? void 0 : item;
966
+ if (!(linesInput instanceof HTMLInputElement)) return;
967
+ const actionItem = item && item.type !== "heading" && item.type !== "block" ? item : void 0;
863
968
  const iconFields = readIconFields(actionItem?.icon);
864
- typeInput.value = item?.type === "checkbox" ? "checkbox" : item?.type === "radio" ? "radio" : item?.type === "heading" ? "heading" : "action";
865
- editorTitle.textContent = item ? "Edit item" : "New action";
866
- labelInput.value = item?.label ?? "New action";
969
+ typeInput.value = item?.type === "checkbox" ? "checkbox" : item?.type === "radio" ? "radio" : item?.type === "heading" ? "heading" : item?.type === "block" ? "block" : "action";
970
+ editorTitle.textContent = item?.type === "block" ? "Edit block" : item ? "Edit item" : "New action";
971
+ labelInput.value = item?.type === "block" ? "" : item?.label ?? "New action";
867
972
  iconTextInput.value = iconFields.text || (item ? "" : "\u25CF");
868
973
  iconClassInput.value = iconFields.className;
869
974
  shortcutInput.value = actionItem?.shortcut ?? "";
975
+ detailInput.value = actionItem?.detail ?? "";
976
+ dangerInput.checked = Boolean(actionItem?.danger);
977
+ hrefInput.value = item && item.type === void 0 ? item.href ?? "" : "";
978
+ targetInput.value = item && item.type === void 0 ? item.target ?? "" : "";
870
979
  badgeEnabled.checked = actionItem?.badge !== void 0;
871
980
  badgeText.value = actionItem?.badge?.text === void 0 ? "" : String(actionItem.badge.text);
872
981
  badgeColor.value = actionItem?.badge?.color ?? "";
@@ -879,6 +988,8 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
879
988
  fileInput.checked = Boolean(item && item.type === void 0 && item.input);
880
989
  checkedInput.checked = (item?.type === "checkbox" || item?.type === "radio") && item.checked;
881
990
  radioGroupInput.value = item?.type === "radio" ? item.radioGroup : "default";
991
+ linesInput.value = item && item.type === "block" && item.lines !== void 0 ? String(item.lines) : "";
992
+ linesInput.setCustomValidity("");
882
993
  inlineActionsHost.innerHTML = "";
883
994
  for (const action of actionItem?.inlineActions ?? []) addInlineAction(action);
884
995
  syncEditorTypeFields(typeInput.value);
@@ -980,16 +1091,28 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
980
1091
  }
981
1092
  function syncEditorTypeFields(type) {
982
1093
  const heading = type === "heading";
1094
+ const block = type === "block";
1095
+ const structural = heading || block;
983
1096
  for (const field of root.querySelectorAll("[data-item-field]")) {
984
- if (field instanceof HTMLElement) field.hidden = heading;
1097
+ if (field instanceof HTMLElement) field.hidden = structural;
985
1098
  }
1099
+ const labelField = query(container, "[data-label-field]");
1100
+ if (labelField instanceof HTMLElement) labelField.hidden = block;
1101
+ const labelInput = form.elements.namedItem("label");
1102
+ if (labelInput instanceof HTMLInputElement) labelInput.required = !block;
986
1103
  const checkedField = query(container, "[data-checked-field]");
987
1104
  const fileInputField = query(container, "[data-file-input-field]");
988
1105
  const radioGroupField = query(container, "[data-radio-group-field]");
989
1106
  const inlineField = query(container, "[data-inline-field]");
990
1107
  if (!(inlineField instanceof HTMLFieldSetElement)) throw new TypeError("Missing inline action controls");
991
- inlineField.disabled = heading;
992
- checkedField.hidden = type === "action";
1108
+ inlineField.disabled = structural;
1109
+ for (const field of root.querySelectorAll("[data-link-field]")) {
1110
+ if (field instanceof HTMLElement) field.hidden = type !== "action";
1111
+ }
1112
+ for (const field of root.querySelectorAll("[data-block-field]")) {
1113
+ if (field instanceof HTMLElement) field.hidden = !block;
1114
+ }
1115
+ checkedField.hidden = type === "action" || structural;
993
1116
  fileInputField.hidden = type !== "action";
994
1117
  radioGroupField.hidden = type !== "radio";
995
1118
  }
@@ -1003,8 +1126,9 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1003
1126
  event.preventDefault();
1004
1127
  const data = new FormData(form);
1005
1128
  const itemType = String(data.get("itemType") ?? "action");
1006
- if (itemType !== "heading" && data.get("badgeEnabled") === "on" && !validateBadgeColors()) return;
1007
- const inlineActions = itemType === "heading" ? [] : readInlineActions();
1129
+ const structural = itemType === "heading" || itemType === "block";
1130
+ if (!structural && data.get("badgeEnabled") === "on" && !validateBadgeColors()) return;
1131
+ const inlineActions = structural ? [] : readInlineActions();
1008
1132
  if (!inlineActions) return;
1009
1133
  const label = String(data.get("label") ?? "").trim() || "Untitled";
1010
1134
  const icon = writeIcon(
@@ -1012,10 +1136,26 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1012
1136
  String(data.get("iconClass") ?? "")
1013
1137
  );
1014
1138
  const shortcut = String(data.get("shortcut") ?? "").trim();
1139
+ const detail = String(data.get("detail") ?? "").trim();
1140
+ const href = String(data.get("href") ?? "").trim();
1141
+ const target = String(data.get("target") ?? "").trim();
1015
1142
  const disabled = data.get("disabled") === "on";
1143
+ const danger = data.get("danger") === "on";
1016
1144
  const input = data.get("input") === "on";
1017
1145
  const checked = data.get("checked") === "on";
1018
1146
  const radioGroup = String(data.get("radioGroup") ?? "").trim() || "default";
1147
+ const linesText = String(data.get("lines") ?? "").trim();
1148
+ const lines = Number(linesText);
1149
+ const linesInput = form.elements.namedItem("lines");
1150
+ if (itemType === "block" && linesText) {
1151
+ if (!(linesInput instanceof HTMLInputElement)) throw new TypeError("Missing lines");
1152
+ const valid = Number.isInteger(lines) && lines >= 1;
1153
+ linesInput.setCustomValidity(valid ? "" : "Enter an integer of at least 1.");
1154
+ if (!valid) {
1155
+ linesInput.reportValidity();
1156
+ return;
1157
+ }
1158
+ }
1019
1159
  const existing = editingId ? findItem(items, editingId)?.item : void 0;
1020
1160
  const badge = readBadge(data, existing && "badge" in existing ? existing.badge : void 0);
1021
1161
  const id = existing?.id ?? emptyItem(label).id;
@@ -1025,12 +1165,21 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1025
1165
  ...icon !== void 0 ? { icon } : {},
1026
1166
  ...disabled ? { disabled: true } : {},
1027
1167
  ...shortcut ? { shortcut } : {},
1168
+ ...detail ? { detail } : {},
1169
+ ...danger ? { danger: true } : {},
1028
1170
  ...badge !== void 0 ? { badge } : {},
1029
1171
  ...inlineActions.length > 0 ? { inlineActions } : {}
1030
1172
  };
1031
1173
  let updated;
1032
1174
  if (itemType === "heading") {
1033
1175
  updated = { type: "heading", id, label };
1176
+ } else if (itemType === "block") {
1177
+ updated = {
1178
+ type: "block",
1179
+ id,
1180
+ ...linesText ? { lines } : {},
1181
+ items: existing?.type === "block" ? existing.items : []
1182
+ };
1034
1183
  } else if (itemType === "checkbox") {
1035
1184
  updated = { ...base, type: "checkbox", checked };
1036
1185
  } else if (itemType === "radio") {
@@ -1039,6 +1188,8 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1039
1188
  updated = {
1040
1189
  ...base,
1041
1190
  ...input ? { input: true } : {},
1191
+ ...href ? { href } : {},
1192
+ ...target ? { target } : {},
1042
1193
  ...existing && existing.type === void 0 && existing.subMenu ? { subMenu: existing.subMenu } : {}
1043
1194
  };
1044
1195
  }
@@ -1119,7 +1270,7 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1119
1270
  });
1120
1271
  form.addEventListener("input", (event) => {
1121
1272
  const target = event.target;
1122
- if (target instanceof HTMLInputElement && (target.name === "badgeColor" || target.name === "badgeBackgroundColor")) {
1273
+ if (target instanceof HTMLInputElement && (target.name === "badgeColor" || target.name === "badgeBackgroundColor" || target.name === "lines")) {
1123
1274
  target.setCustomValidity("");
1124
1275
  }
1125
1276
  if (target instanceof HTMLInputElement && target.closest("[data-inline-action]")) {
@@ -1145,6 +1296,22 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1145
1296
  openEditor(null);
1146
1297
  return;
1147
1298
  }
1299
+ const addBlock = event.target.closest("[data-add-block]");
1300
+ if (addBlock instanceof HTMLElement && addBlock.dataset.addBlock) {
1301
+ const owner = addBlock.dataset.addBlock;
1302
+ const block = emptyBlock();
1303
+ if (owner === "root") {
1304
+ items.push(block);
1305
+ } else {
1306
+ const found = findItem(items, owner);
1307
+ const children = found ? childList(found.item) : null;
1308
+ if (!children) return;
1309
+ children.push(block);
1310
+ }
1311
+ redraw();
1312
+ openEditor(block);
1313
+ return;
1314
+ }
1148
1315
  const addHeading = event.target.closest("[data-add-heading]");
1149
1316
  if (addHeading instanceof HTMLElement && addHeading.dataset.addHeading) {
1150
1317
  const owner = addHeading.dataset.addHeading;
@@ -1153,9 +1320,9 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1153
1320
  items.push(heading);
1154
1321
  } else {
1155
1322
  const found = findItem(items, owner);
1156
- if (!found || found.item.type !== void 0) return;
1157
- if (!found.item.subMenu) found.item.subMenu = [];
1158
- found.item.subMenu.push(heading);
1323
+ const children = found ? childList(found.item) : null;
1324
+ if (!children) return;
1325
+ children.push(heading);
1159
1326
  }
1160
1327
  redraw();
1161
1328
  openEditor(heading);
@@ -1170,10 +1337,10 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1170
1337
  const addChild = event.target.closest("[data-add-child]");
1171
1338
  if (addChild instanceof HTMLElement) {
1172
1339
  const found = findItem(items, addChild.dataset.addChild);
1173
- if (!found || found.item.type !== void 0) return;
1174
- if (!found.item.subMenu) found.item.subMenu = [];
1340
+ const children = found ? childList(found.item) : null;
1341
+ if (!children) return;
1175
1342
  const child = emptyItem("Sub-action");
1176
- found.item.subMenu.push(child);
1343
+ children.push(child);
1177
1344
  redraw();
1178
1345
  openEditor(child);
1179
1346
  return;
@@ -1186,9 +1353,9 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1186
1353
  items.push(separator);
1187
1354
  } else {
1188
1355
  const found = findItem(items, owner);
1189
- if (!found || found.item.type !== void 0) return;
1190
- if (!found.item.subMenu) found.item.subMenu = [];
1191
- found.item.subMenu.push(separator);
1356
+ const children = found ? childList(found.item) : null;
1357
+ if (!children) return;
1358
+ children.push(separator);
1192
1359
  }
1193
1360
  redraw();
1194
1361
  return;
@@ -1231,6 +1398,8 @@ function createContextMenuBuilder(container, initialItems = [], options = {}) {
1231
1398
  setCloseOnMouseLeave,
1232
1399
  destroy() {
1233
1400
  darkPreference.removeEventListener("change", onSystemColorSchemeChange);
1401
+ for (const unbind of previewBindings) unbind();
1402
+ previewBindings = [];
1234
1403
  menu?.destroy();
1235
1404
  menu = null;
1236
1405
  container.innerHTML = "";