@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/README.md CHANGED
@@ -6,7 +6,7 @@ Small / Normal / Large sizes.
6
6
  The "Add heading" button inserts a non-clickable `{ type: "heading", label }`
7
7
  section title at the root or in a submenu. The "Add separator" button inserts
8
8
  a `{ type: "separator" }` item. The form also supports checkboxes, radio
9
- groups, initial states and displayed shortcuts.
9
+ groups, initial states, a second line, a destructive style, links and displayed shortcuts.
10
10
  The item editor also supports notification dots and text or number badges,
11
11
  with per-item text and background colors and an accessible description.
12
12
  Leave badge text empty for a dot. Badges are included in the exported menu JSON.
@@ -17,12 +17,19 @@ can be tested in the preview; connect application behavior to the core's
17
17
  `contextmenu:action` event or action callbacks after export.
18
18
  Open `presets/inline-actions.html` to try and edit several icon actions,
19
19
  including actions in a submenu. Right-click the preview area to open the menu.
20
+ Open `presets/dropdown.html` to try an anchored dropdown with an 8px gap.
21
+ The `previewDropdown: { offset: 8 }` builder option adds the trigger to the
22
+ preview; positioning is configured through the core's `attach` options, not
23
+ the exported menu JSON.
20
24
  Shortcuts are hidden by default in the preview; pass `showShortcuts: true`
21
25
  to display them. The `complete.html` preset enables label wrapping and limits
22
26
  menu width to 260px. It shows every item kind together, including a five-level
23
- submenu that moves left, up or down when it would leave the screen.
27
+ submenu, a submenu loaded on open, a destructive action, a link, a detail line
28
+ and a right-to-left switch. Submenus move left, up or down when they would leave
29
+ the screen. Moving toward an open submenu does not switch to the row underneath.
24
30
  Each kind also has its own page, listed in `presets/index.html`. The `long.html` preset tests scrolling
25
- in a long root menu.
31
+ in a long root menu. The `scroll.html` preset wraps recent files in a block
32
+ capped at four rows with `lines: 4`.
26
33
  The `sections.html` preset demonstrates editable headings at the root and in a
27
34
  submenu, together with an input, checkbox, select, button and div in the same
28
35
  preview menu. The custom renderer cannot be represented in the builder's JSON.
@@ -47,6 +47,14 @@ var JSContextMenuBuilder = (() => {
47
47
  return { type: "heading", id, label: item.label };
48
48
  }
49
49
  if (item.type === "separator") return { type: "separator", id };
50
+ if (item.type === "block") {
51
+ return {
52
+ type: "block",
53
+ id,
54
+ ...item.lines !== void 0 ? { lines: item.lines } : {},
55
+ items: cloneItems(item.items ?? [])
56
+ };
57
+ }
50
58
  if (item.type === "checkbox") {
51
59
  return {
52
60
  type: "checkbox",
@@ -57,7 +65,8 @@ var JSContextMenuBuilder = (() => {
57
65
  ...item.disabled !== void 0 ? { disabled: item.disabled } : {},
58
66
  ...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
59
67
  ...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
60
- ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {}
68
+ ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
69
+ ...presentation(item)
61
70
  };
62
71
  }
63
72
  if (item.type === "radio") {
@@ -71,7 +80,8 @@ var JSContextMenuBuilder = (() => {
71
80
  ...item.disabled !== void 0 ? { disabled: item.disabled } : {},
72
81
  ...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
73
82
  ...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
74
- ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {}
83
+ ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
84
+ ...presentation(item)
75
85
  };
76
86
  }
77
87
  return {
@@ -83,10 +93,24 @@ var JSContextMenuBuilder = (() => {
83
93
  ...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
84
94
  ...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
85
95
  ...item.input !== void 0 ? { input: item.input } : {},
96
+ ...presentation(item),
97
+ ...linkFields(item),
86
98
  ...item.subMenu ? { subMenu: cloneItems(item.subMenu) } : {}
87
99
  };
88
100
  });
89
101
  }
102
+ function presentation(item) {
103
+ return {
104
+ ...item.danger ? { danger: true } : {},
105
+ ...item.detail ? { detail: item.detail } : {}
106
+ };
107
+ }
108
+ function linkFields(item) {
109
+ return {
110
+ ...item.href ? { href: item.href } : {},
111
+ ...item.target ? { target: item.target } : {}
112
+ };
113
+ }
90
114
  function cloneInlineActions(actions) {
91
115
  return actions.map((action) => ({ ...action, icon: cloneIcon(action.icon) }));
92
116
  }
@@ -104,6 +128,12 @@ var JSContextMenuBuilder = (() => {
104
128
  return out2;
105
129
  }
106
130
  if (item.type === "separator") return { type: "separator" };
131
+ if (item.type === "block") {
132
+ const out2 = { type: "block" };
133
+ if (item.lines !== void 0) out2.lines = item.lines;
134
+ if (item.items.length > 0) out2.items = toPublic(item.items);
135
+ return out2;
136
+ }
107
137
  if (item.type === "checkbox") {
108
138
  const out2 = {
109
139
  type: "checkbox",
@@ -116,6 +146,7 @@ var JSContextMenuBuilder = (() => {
116
146
  if (item.shortcut) out2.shortcut = item.shortcut;
117
147
  if (item.badge !== void 0) out2.badge = { ...item.badge };
118
148
  if (item.inlineActions !== void 0) out2.inlineActions = cloneInlineActions(item.inlineActions);
149
+ Object.assign(out2, presentation(item));
119
150
  return out2;
120
151
  }
121
152
  if (item.type === "radio") {
@@ -131,6 +162,7 @@ var JSContextMenuBuilder = (() => {
131
162
  if (item.shortcut) out2.shortcut = item.shortcut;
132
163
  if (item.badge !== void 0) out2.badge = { ...item.badge };
133
164
  if (item.inlineActions !== void 0) out2.inlineActions = cloneInlineActions(item.inlineActions);
165
+ Object.assign(out2, presentation(item));
134
166
  return out2;
135
167
  }
136
168
  const out = { label: item.label };
@@ -141,6 +173,7 @@ var JSContextMenuBuilder = (() => {
141
173
  if (item.badge !== void 0) out.badge = { ...item.badge };
142
174
  if (item.inlineActions !== void 0) out.inlineActions = cloneInlineActions(item.inlineActions);
143
175
  if (item.input) out.input = true;
176
+ Object.assign(out, presentation(item), linkFields(item));
144
177
  if (item.subMenu && item.subMenu.length > 0) out.subMenu = toPublic(item.subMenu);
145
178
  return out;
146
179
  });
@@ -162,8 +195,9 @@ var JSContextMenuBuilder = (() => {
162
195
  const item = items[index];
163
196
  if (!item) continue;
164
197
  if (item.id === id) return { item, list: items, index };
165
- if (item.type === void 0 && item.subMenu) {
166
- const nested = findItem(item.subMenu, id);
198
+ const children = item.type === "block" ? item.items : item.type === void 0 ? item.subMenu : void 0;
199
+ if (children) {
200
+ const nested = findItem(children, id);
167
201
  if (nested) return nested;
168
202
  }
169
203
  }
@@ -196,6 +230,9 @@ var JSContextMenuBuilder = (() => {
196
230
  function emptyHeading(label = "New section") {
197
231
  return { type: "heading", id: nextId(), label };
198
232
  }
233
+ function emptyBlock() {
234
+ return { type: "block", id: nextId(), items: [] };
235
+ }
199
236
  function readIconFields(icon) {
200
237
  if (icon == null || icon === "") return { text: "", className: "" };
201
238
  if (typeof icon === "string") return { text: icon, className: "" };
@@ -221,6 +258,7 @@ var JSContextMenuBuilder = (() => {
221
258
  if (item.type === "separator") return "separator";
222
259
  if (item.type === "checkbox") return `checkbox (${item.checked ? "checked" : "unchecked"})`;
223
260
  if (item.type === "radio") return `option (${item.checked ? "selected" : "not selected"})`;
261
+ if (item.type === "block") return item.lines ? `block, ${item.lines} lines` : "block";
224
262
  if (item.subMenu && item.subMenu.length > 0) return "submenu";
225
263
  if (item.input) return "file";
226
264
  if (item.disabled) return "disabled";
@@ -231,7 +269,7 @@ var JSContextMenuBuilder = (() => {
231
269
  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>`;
232
270
  }
233
271
  function nestedActions(id) {
234
- 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>`;
272
+ 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>`;
235
273
  }
236
274
  function separatorActions(id) {
237
275
  return `<span class="jscmb-actions">${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
@@ -246,11 +284,17 @@ var JSContextMenuBuilder = (() => {
246
284
  function renderLeaf(item) {
247
285
  const label = escapeText(item.label);
248
286
  const kind = escapeText(itemKind(item));
287
+ const notes = [
288
+ kind,
289
+ item.danger ? "destructive" : "",
290
+ item.detail ? "detail" : "",
291
+ item.type === void 0 && item.href ? "link" : ""
292
+ ].filter(Boolean).join(", ");
249
293
  const fields = readIconFields(item.icon);
250
294
  const preview = escapeText(iconPreview(item.icon) || (fields.className ? "" : "\xB7"));
251
295
  const iconClass = ["jscmb-item-icon", fields.className].filter(Boolean).join(" ");
252
296
  const shortcut = item.shortcut ? `<kbd class="jscmb-shortcut">${escapeText(item.shortcut)}</kbd>` : "";
253
- 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>`;
297
+ 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>`;
254
298
  }
255
299
  function renderSeparator(item) {
256
300
  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>`;
@@ -265,9 +309,15 @@ var JSContextMenuBuilder = (() => {
265
309
  const children = item.subMenu ?? [];
266
310
  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>`;
267
311
  }
312
+ function renderBlock(item) {
313
+ const children = item.items ?? [];
314
+ const kind = item.lines ? `block, ${item.lines} lines` : "block";
315
+ 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>`;
316
+ }
268
317
  function renderNode(item) {
269
318
  if (item.type === "heading") return renderHeading(item);
270
319
  if (item.type === "separator") return renderSeparator(item);
320
+ if (item.type === "block") return renderBlock(item);
271
321
  if (item.type === void 0 && item.subMenu && item.subMenu.length > 0) {
272
322
  return renderNested(item);
273
323
  }
@@ -275,7 +325,7 @@ var JSContextMenuBuilder = (() => {
275
325
  }
276
326
  function renderTree(items, title = "Menu") {
277
327
  const heading = escapeText(title);
278
- 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>`;
328
+ 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>`;
279
329
  }
280
330
 
281
331
  // src/palette.ts
@@ -447,6 +497,7 @@ var JSContextMenuBuilder = (() => {
447
497
  <section class="jscmb-preview-panel" data-preview tabindex="0">
448
498
  <strong>Test area</strong>
449
499
  <p>${hint}</p>
500
+ ${options.previewDropdown ? '<button type="button" data-preview-dropdown>Open dropdown <span aria-hidden="true">\u25BE</span></button>' : ""}
450
501
  <p class="jscmb-log" data-log></p>
451
502
  </section>
452
503
  <details>
@@ -467,16 +518,20 @@ var JSContextMenuBuilder = (() => {
467
518
  <form method="dialog" data-editor>
468
519
  <h2 data-editor-title>Item</h2>
469
520
  <div class="jscmb-dialog-body">
470
- <label>Label<input name="label" type="text" required></label>
521
+ <label data-label-field>Label<input name="label" type="text" required></label>
471
522
  <label>Type<select name="itemType">
472
523
  <option value="action">Action</option>
473
524
  <option value="checkbox">Checkbox</option>
474
525
  <option value="radio">Radio option</option>
475
526
  <option value="heading">Heading</option>
527
+ <option value="block">Block</option>
476
528
  </select></label>
477
529
  <label data-item-field>Symbol (Unicode / text)<input name="iconText" type="text" placeholder="\u2605 or content_copy"></label>
478
530
  <label data-item-field>CSS class (optional)<input name="iconClass" type="text" placeholder="material-symbols-outlined or fa-solid fa-star"></label>
479
531
  <label data-item-field>Displayed shortcut<input name="shortcut" type="text" placeholder="\u2318S"></label>
532
+ <label data-item-field>Detail<input name="detail" type="text" placeholder="Second line"></label>
533
+ <label data-item-field data-link-field>Link URL<input name="href" type="text" placeholder="https://\u2026"></label>
534
+ <label data-item-field data-link-field>Link target<input name="target" type="text" placeholder="_self or _blank"></label>
480
535
  <label class="jscmb-badge-toggle" data-item-field><input name="badgeEnabled" type="checkbox"> Notification badge</label>
481
536
  <fieldset class="jscmb-badge-fields" data-badge-fields data-item-field disabled>
482
537
  <legend>Notification badge</legend>
@@ -487,10 +542,12 @@ var JSContextMenuBuilder = (() => {
487
542
  </fieldset>
488
543
  <div class="jscmb-checks" data-item-field>
489
544
  <label><input name="disabled" type="checkbox"> Disabled</label>
545
+ <label><input name="danger" type="checkbox"> Destructive</label>
490
546
  <label data-file-input-field><input name="input" type="checkbox"> File picker</label>
491
547
  <label data-checked-field><input name="checked" type="checkbox"> Checked / selected</label>
492
548
  </div>
493
549
  <label data-radio-group-field data-item-field>Radio group<input name="radioGroup" type="text" value="default"></label>
550
+ <label data-block-field>Visible lines<input name="lines" type="number" min="1" step="1" placeholder="All"></label>
494
551
  <fieldset data-item-field data-inline-field>
495
552
  <legend>Inline icon actions</legend>
496
553
  <div data-inline-actions></div>
@@ -506,15 +563,35 @@ var JSContextMenuBuilder = (() => {
506
563
  </dialog>
507
564
  </div>`;
508
565
  }
566
+ function childList(item) {
567
+ if (item.type === "block") {
568
+ if (!item.items) item.items = [];
569
+ return item.items;
570
+ }
571
+ if (item.type === void 0) {
572
+ if (!item.subMenu) item.subMenu = [];
573
+ return item.subMenu;
574
+ }
575
+ return null;
576
+ }
509
577
  function runtimeItems(items, onAction) {
510
578
  return items.map((item) => {
511
579
  if (item.type === "heading") return { type: "heading", label: item.label };
512
580
  if (item.type === "separator") return { type: "separator" };
581
+ if (item.type === "block") {
582
+ return {
583
+ type: "block",
584
+ ...item.lines !== void 0 ? { lines: item.lines } : {},
585
+ ...item.items.length > 0 ? { items: runtimeItems(item.items, onAction) } : {}
586
+ };
587
+ }
513
588
  const base = {
514
589
  label: item.label,
515
590
  ...item.icon ? { icon: item.icon } : {},
516
591
  ...item.disabled ? { disabled: true } : {},
517
592
  ...item.shortcut ? { shortcut: item.shortcut } : {},
593
+ ...item.detail ? { detail: item.detail } : {},
594
+ ...item.danger ? { danger: true } : {},
518
595
  ...item.badge ? { badge: { ...item.badge } } : {},
519
596
  ...item.inlineActions ? {
520
597
  inlineActions: item.inlineActions.map((inlineAction) => ({
@@ -551,11 +628,16 @@ var JSContextMenuBuilder = (() => {
551
628
  };
552
629
  }
553
630
  if (item.type === void 0 && item.subMenu && item.subMenu.length > 0) {
554
- return { ...base, subMenu: runtimeItems(item.subMenu, onAction) };
631
+ return {
632
+ ...base,
633
+ subMenu: runtimeItems(item.subMenu, onAction)
634
+ };
555
635
  }
556
636
  return {
557
637
  ...base,
558
638
  ...item.input ? { input: true } : {},
639
+ ...item.href ? { href: item.href } : {},
640
+ ...item.target ? { target: item.target } : {},
559
641
  action: (_data, file) => {
560
642
  onAction(file ? `${item.label} \u2192 ${file.name}` : item.label);
561
643
  }
@@ -566,6 +648,7 @@ var JSContextMenuBuilder = (() => {
566
648
  let items = cloneItems(initialItems);
567
649
  let editingId = null;
568
650
  let menu = null;
651
+ let previewBindings = [];
569
652
  let theme = normalizeTheme(options.theme);
570
653
  let size = normalizeSize(options.size);
571
654
  let radius = options.radius;
@@ -699,6 +782,8 @@ var JSContextMenuBuilder = (() => {
699
782
  optionsJson.textContent = JSON.stringify({ itemStyle, wrapLabels, closeOnMouseLeave, ...radius !== void 0 ? { radius } : {} }, null, 2);
700
783
  }
701
784
  function bindPreview() {
785
+ for (const unbind of previewBindings) unbind();
786
+ previewBindings = [];
702
787
  menu?.destroy();
703
788
  const previewMenu = createContextMenu({
704
789
  mount: document.body,
@@ -712,7 +797,7 @@ var JSContextMenuBuilder = (() => {
712
797
  ...options.showShortcuts !== void 0 ? { showShortcuts: options.showShortcuts } : {}
713
798
  });
714
799
  menu = previewMenu;
715
- previewMenu.attach(preview, () => {
800
+ const getPreviewItems = () => {
716
801
  const previewItems = runtimeItems(items, (label, checked) => {
717
802
  log.textContent = checked === void 0 ? `Action: ${label}` : `${label}: ${checked ? "checked" : "unchecked"}`;
718
803
  if (checked !== void 0) {
@@ -721,7 +806,17 @@ var JSContextMenuBuilder = (() => {
721
806
  }
722
807
  });
723
808
  return options.transformPreviewItems?.(previewItems, previewMenu) ?? previewItems;
724
- });
809
+ };
810
+ previewBindings.push(previewMenu.attach(preview, getPreviewItems));
811
+ const dropdown = preview.querySelector("[data-preview-dropdown]");
812
+ if (dropdown instanceof HTMLElement && options.previewDropdown) {
813
+ previewBindings.push(previewMenu.attach(dropdown, getPreviewItems, {
814
+ openEvents: ["click"],
815
+ position: "anchor",
816
+ offset: options.previewDropdown.offset ?? 0,
817
+ longPressMs: false
818
+ }));
819
+ }
725
820
  }
726
821
  function redraw() {
727
822
  treeHost.innerHTML = renderTree(items, title);
@@ -868,6 +963,10 @@ var JSContextMenuBuilder = (() => {
868
963
  const iconTextInput = form.elements.namedItem("iconText");
869
964
  const iconClassInput = form.elements.namedItem("iconClass");
870
965
  const shortcutInput = form.elements.namedItem("shortcut");
966
+ const detailInput = form.elements.namedItem("detail");
967
+ const hrefInput = form.elements.namedItem("href");
968
+ const targetInput = form.elements.namedItem("target");
969
+ const dangerInput = form.elements.namedItem("danger");
871
970
  const badgeEnabled = form.elements.namedItem("badgeEnabled");
872
971
  const badgeText = form.elements.namedItem("badgeText");
873
972
  const badgeColor = form.elements.namedItem("badgeColor");
@@ -877,11 +976,16 @@ var JSContextMenuBuilder = (() => {
877
976
  const fileInput = form.elements.namedItem("input");
878
977
  const checkedInput = form.elements.namedItem("checked");
879
978
  const radioGroupInput = form.elements.namedItem("radioGroup");
979
+ const linesInput = form.elements.namedItem("lines");
880
980
  if (!(labelInput instanceof HTMLInputElement)) return;
881
981
  if (!(typeInput instanceof HTMLSelectElement)) return;
882
982
  if (!(iconTextInput instanceof HTMLInputElement)) return;
883
983
  if (!(iconClassInput instanceof HTMLInputElement)) return;
884
984
  if (!(shortcutInput instanceof HTMLInputElement)) return;
985
+ if (!(detailInput instanceof HTMLInputElement)) return;
986
+ if (!(hrefInput instanceof HTMLInputElement)) return;
987
+ if (!(targetInput instanceof HTMLInputElement)) return;
988
+ if (!(dangerInput instanceof HTMLInputElement)) return;
885
989
  if (!(badgeEnabled instanceof HTMLInputElement)) return;
886
990
  if (!(badgeText instanceof HTMLInputElement)) return;
887
991
  if (!(badgeColor instanceof HTMLInputElement)) return;
@@ -891,14 +995,19 @@ var JSContextMenuBuilder = (() => {
891
995
  if (!(fileInput instanceof HTMLInputElement)) return;
892
996
  if (!(checkedInput instanceof HTMLInputElement)) return;
893
997
  if (!(radioGroupInput instanceof HTMLInputElement)) return;
894
- const actionItem = item?.type === "heading" ? void 0 : item;
998
+ if (!(linesInput instanceof HTMLInputElement)) return;
999
+ const actionItem = item && item.type !== "heading" && item.type !== "block" ? item : void 0;
895
1000
  const iconFields = readIconFields(actionItem?.icon);
896
- typeInput.value = item?.type === "checkbox" ? "checkbox" : item?.type === "radio" ? "radio" : item?.type === "heading" ? "heading" : "action";
897
- editorTitle.textContent = item ? "Edit item" : "New action";
898
- labelInput.value = item?.label ?? "New action";
1001
+ typeInput.value = item?.type === "checkbox" ? "checkbox" : item?.type === "radio" ? "radio" : item?.type === "heading" ? "heading" : item?.type === "block" ? "block" : "action";
1002
+ editorTitle.textContent = item?.type === "block" ? "Edit block" : item ? "Edit item" : "New action";
1003
+ labelInput.value = item?.type === "block" ? "" : item?.label ?? "New action";
899
1004
  iconTextInput.value = iconFields.text || (item ? "" : "\u25CF");
900
1005
  iconClassInput.value = iconFields.className;
901
1006
  shortcutInput.value = actionItem?.shortcut ?? "";
1007
+ detailInput.value = actionItem?.detail ?? "";
1008
+ dangerInput.checked = Boolean(actionItem?.danger);
1009
+ hrefInput.value = item && item.type === void 0 ? item.href ?? "" : "";
1010
+ targetInput.value = item && item.type === void 0 ? item.target ?? "" : "";
902
1011
  badgeEnabled.checked = actionItem?.badge !== void 0;
903
1012
  badgeText.value = actionItem?.badge?.text === void 0 ? "" : String(actionItem.badge.text);
904
1013
  badgeColor.value = actionItem?.badge?.color ?? "";
@@ -911,6 +1020,8 @@ var JSContextMenuBuilder = (() => {
911
1020
  fileInput.checked = Boolean(item && item.type === void 0 && item.input);
912
1021
  checkedInput.checked = (item?.type === "checkbox" || item?.type === "radio") && item.checked;
913
1022
  radioGroupInput.value = item?.type === "radio" ? item.radioGroup : "default";
1023
+ linesInput.value = item && item.type === "block" && item.lines !== void 0 ? String(item.lines) : "";
1024
+ linesInput.setCustomValidity("");
914
1025
  inlineActionsHost.innerHTML = "";
915
1026
  for (const action of actionItem?.inlineActions ?? []) addInlineAction(action);
916
1027
  syncEditorTypeFields(typeInput.value);
@@ -1012,16 +1123,28 @@ var JSContextMenuBuilder = (() => {
1012
1123
  }
1013
1124
  function syncEditorTypeFields(type) {
1014
1125
  const heading = type === "heading";
1126
+ const block = type === "block";
1127
+ const structural = heading || block;
1015
1128
  for (const field of root.querySelectorAll("[data-item-field]")) {
1016
- if (field instanceof HTMLElement) field.hidden = heading;
1129
+ if (field instanceof HTMLElement) field.hidden = structural;
1017
1130
  }
1131
+ const labelField = query(container, "[data-label-field]");
1132
+ if (labelField instanceof HTMLElement) labelField.hidden = block;
1133
+ const labelInput = form.elements.namedItem("label");
1134
+ if (labelInput instanceof HTMLInputElement) labelInput.required = !block;
1018
1135
  const checkedField = query(container, "[data-checked-field]");
1019
1136
  const fileInputField = query(container, "[data-file-input-field]");
1020
1137
  const radioGroupField = query(container, "[data-radio-group-field]");
1021
1138
  const inlineField = query(container, "[data-inline-field]");
1022
1139
  if (!(inlineField instanceof HTMLFieldSetElement)) throw new TypeError("Missing inline action controls");
1023
- inlineField.disabled = heading;
1024
- checkedField.hidden = type === "action";
1140
+ inlineField.disabled = structural;
1141
+ for (const field of root.querySelectorAll("[data-link-field]")) {
1142
+ if (field instanceof HTMLElement) field.hidden = type !== "action";
1143
+ }
1144
+ for (const field of root.querySelectorAll("[data-block-field]")) {
1145
+ if (field instanceof HTMLElement) field.hidden = !block;
1146
+ }
1147
+ checkedField.hidden = type === "action" || structural;
1025
1148
  fileInputField.hidden = type !== "action";
1026
1149
  radioGroupField.hidden = type !== "radio";
1027
1150
  }
@@ -1035,8 +1158,9 @@ var JSContextMenuBuilder = (() => {
1035
1158
  event.preventDefault();
1036
1159
  const data = new FormData(form);
1037
1160
  const itemType = String(data.get("itemType") ?? "action");
1038
- if (itemType !== "heading" && data.get("badgeEnabled") === "on" && !validateBadgeColors()) return;
1039
- const inlineActions = itemType === "heading" ? [] : readInlineActions();
1161
+ const structural = itemType === "heading" || itemType === "block";
1162
+ if (!structural && data.get("badgeEnabled") === "on" && !validateBadgeColors()) return;
1163
+ const inlineActions = structural ? [] : readInlineActions();
1040
1164
  if (!inlineActions) return;
1041
1165
  const label = String(data.get("label") ?? "").trim() || "Untitled";
1042
1166
  const icon = writeIcon(
@@ -1044,10 +1168,26 @@ var JSContextMenuBuilder = (() => {
1044
1168
  String(data.get("iconClass") ?? "")
1045
1169
  );
1046
1170
  const shortcut = String(data.get("shortcut") ?? "").trim();
1171
+ const detail = String(data.get("detail") ?? "").trim();
1172
+ const href = String(data.get("href") ?? "").trim();
1173
+ const target = String(data.get("target") ?? "").trim();
1047
1174
  const disabled = data.get("disabled") === "on";
1175
+ const danger = data.get("danger") === "on";
1048
1176
  const input = data.get("input") === "on";
1049
1177
  const checked = data.get("checked") === "on";
1050
1178
  const radioGroup = String(data.get("radioGroup") ?? "").trim() || "default";
1179
+ const linesText = String(data.get("lines") ?? "").trim();
1180
+ const lines = Number(linesText);
1181
+ const linesInput = form.elements.namedItem("lines");
1182
+ if (itemType === "block" && linesText) {
1183
+ if (!(linesInput instanceof HTMLInputElement)) throw new TypeError("Missing lines");
1184
+ const valid = Number.isInteger(lines) && lines >= 1;
1185
+ linesInput.setCustomValidity(valid ? "" : "Enter an integer of at least 1.");
1186
+ if (!valid) {
1187
+ linesInput.reportValidity();
1188
+ return;
1189
+ }
1190
+ }
1051
1191
  const existing = editingId ? findItem(items, editingId)?.item : void 0;
1052
1192
  const badge = readBadge(data, existing && "badge" in existing ? existing.badge : void 0);
1053
1193
  const id = existing?.id ?? emptyItem(label).id;
@@ -1057,12 +1197,21 @@ var JSContextMenuBuilder = (() => {
1057
1197
  ...icon !== void 0 ? { icon } : {},
1058
1198
  ...disabled ? { disabled: true } : {},
1059
1199
  ...shortcut ? { shortcut } : {},
1200
+ ...detail ? { detail } : {},
1201
+ ...danger ? { danger: true } : {},
1060
1202
  ...badge !== void 0 ? { badge } : {},
1061
1203
  ...inlineActions.length > 0 ? { inlineActions } : {}
1062
1204
  };
1063
1205
  let updated;
1064
1206
  if (itemType === "heading") {
1065
1207
  updated = { type: "heading", id, label };
1208
+ } else if (itemType === "block") {
1209
+ updated = {
1210
+ type: "block",
1211
+ id,
1212
+ ...linesText ? { lines } : {},
1213
+ items: existing?.type === "block" ? existing.items : []
1214
+ };
1066
1215
  } else if (itemType === "checkbox") {
1067
1216
  updated = { ...base, type: "checkbox", checked };
1068
1217
  } else if (itemType === "radio") {
@@ -1071,6 +1220,8 @@ var JSContextMenuBuilder = (() => {
1071
1220
  updated = {
1072
1221
  ...base,
1073
1222
  ...input ? { input: true } : {},
1223
+ ...href ? { href } : {},
1224
+ ...target ? { target } : {},
1074
1225
  ...existing && existing.type === void 0 && existing.subMenu ? { subMenu: existing.subMenu } : {}
1075
1226
  };
1076
1227
  }
@@ -1151,7 +1302,7 @@ var JSContextMenuBuilder = (() => {
1151
1302
  });
1152
1303
  form.addEventListener("input", (event) => {
1153
1304
  const target = event.target;
1154
- if (target instanceof HTMLInputElement && (target.name === "badgeColor" || target.name === "badgeBackgroundColor")) {
1305
+ if (target instanceof HTMLInputElement && (target.name === "badgeColor" || target.name === "badgeBackgroundColor" || target.name === "lines")) {
1155
1306
  target.setCustomValidity("");
1156
1307
  }
1157
1308
  if (target instanceof HTMLInputElement && target.closest("[data-inline-action]")) {
@@ -1177,6 +1328,22 @@ var JSContextMenuBuilder = (() => {
1177
1328
  openEditor(null);
1178
1329
  return;
1179
1330
  }
1331
+ const addBlock = event.target.closest("[data-add-block]");
1332
+ if (addBlock instanceof HTMLElement && addBlock.dataset.addBlock) {
1333
+ const owner = addBlock.dataset.addBlock;
1334
+ const block = emptyBlock();
1335
+ if (owner === "root") {
1336
+ items.push(block);
1337
+ } else {
1338
+ const found = findItem(items, owner);
1339
+ const children = found ? childList(found.item) : null;
1340
+ if (!children) return;
1341
+ children.push(block);
1342
+ }
1343
+ redraw();
1344
+ openEditor(block);
1345
+ return;
1346
+ }
1180
1347
  const addHeading = event.target.closest("[data-add-heading]");
1181
1348
  if (addHeading instanceof HTMLElement && addHeading.dataset.addHeading) {
1182
1349
  const owner = addHeading.dataset.addHeading;
@@ -1185,9 +1352,9 @@ var JSContextMenuBuilder = (() => {
1185
1352
  items.push(heading);
1186
1353
  } else {
1187
1354
  const found = findItem(items, owner);
1188
- if (!found || found.item.type !== void 0) return;
1189
- if (!found.item.subMenu) found.item.subMenu = [];
1190
- found.item.subMenu.push(heading);
1355
+ const children = found ? childList(found.item) : null;
1356
+ if (!children) return;
1357
+ children.push(heading);
1191
1358
  }
1192
1359
  redraw();
1193
1360
  openEditor(heading);
@@ -1202,10 +1369,10 @@ var JSContextMenuBuilder = (() => {
1202
1369
  const addChild = event.target.closest("[data-add-child]");
1203
1370
  if (addChild instanceof HTMLElement) {
1204
1371
  const found = findItem(items, addChild.dataset.addChild);
1205
- if (!found || found.item.type !== void 0) return;
1206
- if (!found.item.subMenu) found.item.subMenu = [];
1372
+ const children = found ? childList(found.item) : null;
1373
+ if (!children) return;
1207
1374
  const child = emptyItem("Sub-action");
1208
- found.item.subMenu.push(child);
1375
+ children.push(child);
1209
1376
  redraw();
1210
1377
  openEditor(child);
1211
1378
  return;
@@ -1218,9 +1385,9 @@ var JSContextMenuBuilder = (() => {
1218
1385
  items.push(separator);
1219
1386
  } else {
1220
1387
  const found = findItem(items, owner);
1221
- if (!found || found.item.type !== void 0) return;
1222
- if (!found.item.subMenu) found.item.subMenu = [];
1223
- found.item.subMenu.push(separator);
1388
+ const children = found ? childList(found.item) : null;
1389
+ if (!children) return;
1390
+ children.push(separator);
1224
1391
  }
1225
1392
  redraw();
1226
1393
  return;
@@ -1263,6 +1430,8 @@ var JSContextMenuBuilder = (() => {
1263
1430
  setCloseOnMouseLeave,
1264
1431
  destroy() {
1265
1432
  darkPreference.removeEventListener("change", onSystemColorSchemeChange);
1433
+ for (const unbind of previewBindings) unbind();
1434
+ previewBindings = [];
1266
1435
  menu?.destroy();
1267
1436
  menu = null;
1268
1437
  container.innerHTML = "";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA0BA,OAAO,KAAK,EACV,kBAAkB,EAClB,yBAAyB,EAIzB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,WAAW,EACX,kBAAkB,EAClB,yBAAyB,EACzB,uBAAuB,EACvB,aAAa,EACb,QAAQ,EACR,SAAS,EACT,cAAc,EACf,MAAM,YAAY,CAAC;AAkRpB;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,WAAW,EACtB,YAAY,GAAE,cAAc,EAAO,EACnC,OAAO,GAAE,yBAA8B,GACtC,kBAAkB,CA8vBpB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EACV,kBAAkB,EAClB,yBAAyB,EAIzB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,WAAW,EACX,kBAAkB,EAClB,yBAAyB,EACzB,uBAAuB,EACvB,aAAa,EACb,QAAQ,EACR,SAAS,EACT,cAAc,EACf,MAAM,YAAY,CAAC;AAmTpB;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,WAAW,EACtB,YAAY,GAAE,cAAc,EAAO,EACnC,OAAO,GAAE,yBAA8B,GACtC,kBAAkB,CAu1BpB"}