@octolo/js-contextmenu-builder 0.0.0-stage → 0.2.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 +89 -2
- package/browser/js-contextmenu-builder.js +1273 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1243 -0
- package/dist/index.js.map +7 -0
- package/dist/model.d.ts +22 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/palette.d.ts +7 -0
- package/dist/palette.d.ts.map +1 -0
- package/dist/types.d.ts +62 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +32 -4
- package/src/style.css +632 -0
|
@@ -0,0 +1,1273 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var JSContextMenuBuilder = (() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
createContextMenuBuilder: () => createContextMenuBuilder
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// js-contextmenu-global:@octolo/js-contextmenu
|
|
28
|
+
var api = globalThis.JSContextMenu;
|
|
29
|
+
var createContextMenu = api.createContextMenu;
|
|
30
|
+
var DEFAULT_CLASS_NAMES = api.DEFAULT_CLASS_NAMES;
|
|
31
|
+
var escapeText = api.escapeText;
|
|
32
|
+
var iconPreview = api.iconPreview;
|
|
33
|
+
var mergeClassNames = api.mergeClassNames;
|
|
34
|
+
var resolveAction = api.resolveAction;
|
|
35
|
+
var resolveIcon = api.resolveIcon;
|
|
36
|
+
|
|
37
|
+
// src/model.ts
|
|
38
|
+
var idCounter = 0;
|
|
39
|
+
function nextId(prefix = "jscmb") {
|
|
40
|
+
idCounter += 1;
|
|
41
|
+
return `${prefix}-${idCounter}`;
|
|
42
|
+
}
|
|
43
|
+
function cloneItems(items) {
|
|
44
|
+
return items.map((item) => {
|
|
45
|
+
const id = item.id && item.id.length > 0 ? item.id : nextId();
|
|
46
|
+
if (item.type === "heading") {
|
|
47
|
+
return { type: "heading", id, label: item.label };
|
|
48
|
+
}
|
|
49
|
+
if (item.type === "separator") return { type: "separator", id };
|
|
50
|
+
if (item.type === "checkbox") {
|
|
51
|
+
return {
|
|
52
|
+
type: "checkbox",
|
|
53
|
+
id,
|
|
54
|
+
label: item.label,
|
|
55
|
+
checked: item.checked,
|
|
56
|
+
...item.icon !== void 0 ? { icon: cloneIcon(item.icon) } : {},
|
|
57
|
+
...item.disabled !== void 0 ? { disabled: item.disabled } : {},
|
|
58
|
+
...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
|
|
59
|
+
...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
|
|
60
|
+
...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (item.type === "radio") {
|
|
64
|
+
return {
|
|
65
|
+
type: "radio",
|
|
66
|
+
id,
|
|
67
|
+
label: item.label,
|
|
68
|
+
checked: item.checked,
|
|
69
|
+
radioGroup: item.radioGroup,
|
|
70
|
+
...item.icon !== void 0 ? { icon: cloneIcon(item.icon) } : {},
|
|
71
|
+
...item.disabled !== void 0 ? { disabled: item.disabled } : {},
|
|
72
|
+
...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
|
|
73
|
+
...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
|
|
74
|
+
...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
id,
|
|
79
|
+
label: item.label,
|
|
80
|
+
...item.icon !== void 0 ? { icon: cloneIcon(item.icon) } : {},
|
|
81
|
+
...item.disabled !== void 0 ? { disabled: item.disabled } : {},
|
|
82
|
+
...item.shortcut !== void 0 ? { shortcut: item.shortcut } : {},
|
|
83
|
+
...item.badge !== void 0 ? { badge: { ...item.badge } } : {},
|
|
84
|
+
...item.inlineActions !== void 0 ? { inlineActions: cloneInlineActions(item.inlineActions) } : {},
|
|
85
|
+
...item.input !== void 0 ? { input: item.input } : {},
|
|
86
|
+
...item.subMenu ? { subMenu: cloneItems(item.subMenu) } : {}
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function cloneInlineActions(actions) {
|
|
91
|
+
return actions.map((action) => ({ ...action, icon: cloneIcon(action.icon) }));
|
|
92
|
+
}
|
|
93
|
+
function cloneIcon(icon) {
|
|
94
|
+
if (typeof icon === "string") return icon;
|
|
95
|
+
return {
|
|
96
|
+
...icon.class !== void 0 ? { class: icon.class } : {},
|
|
97
|
+
...icon.text !== void 0 ? { text: icon.text } : {}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function toPublic(items) {
|
|
101
|
+
return items.map((item) => {
|
|
102
|
+
if (item.type === "heading") {
|
|
103
|
+
const out2 = { type: "heading", label: item.label };
|
|
104
|
+
return out2;
|
|
105
|
+
}
|
|
106
|
+
if (item.type === "separator") return { type: "separator" };
|
|
107
|
+
if (item.type === "checkbox") {
|
|
108
|
+
const out2 = {
|
|
109
|
+
type: "checkbox",
|
|
110
|
+
label: item.label,
|
|
111
|
+
checked: item.checked
|
|
112
|
+
};
|
|
113
|
+
const icon2 = publicIcon(item.icon);
|
|
114
|
+
if (icon2 !== void 0) out2.icon = icon2;
|
|
115
|
+
if (item.disabled) out2.disabled = true;
|
|
116
|
+
if (item.shortcut) out2.shortcut = item.shortcut;
|
|
117
|
+
if (item.badge !== void 0) out2.badge = { ...item.badge };
|
|
118
|
+
if (item.inlineActions !== void 0) out2.inlineActions = cloneInlineActions(item.inlineActions);
|
|
119
|
+
return out2;
|
|
120
|
+
}
|
|
121
|
+
if (item.type === "radio") {
|
|
122
|
+
const out2 = {
|
|
123
|
+
type: "radio",
|
|
124
|
+
label: item.label,
|
|
125
|
+
checked: item.checked,
|
|
126
|
+
radioGroup: item.radioGroup
|
|
127
|
+
};
|
|
128
|
+
const icon2 = publicIcon(item.icon);
|
|
129
|
+
if (icon2 !== void 0) out2.icon = icon2;
|
|
130
|
+
if (item.disabled) out2.disabled = true;
|
|
131
|
+
if (item.shortcut) out2.shortcut = item.shortcut;
|
|
132
|
+
if (item.badge !== void 0) out2.badge = { ...item.badge };
|
|
133
|
+
if (item.inlineActions !== void 0) out2.inlineActions = cloneInlineActions(item.inlineActions);
|
|
134
|
+
return out2;
|
|
135
|
+
}
|
|
136
|
+
const out = { label: item.label };
|
|
137
|
+
const icon = publicIcon(item.icon);
|
|
138
|
+
if (icon !== void 0) out.icon = icon;
|
|
139
|
+
if (item.disabled) out.disabled = true;
|
|
140
|
+
if (item.shortcut) out.shortcut = item.shortcut;
|
|
141
|
+
if (item.badge !== void 0) out.badge = { ...item.badge };
|
|
142
|
+
if (item.inlineActions !== void 0) out.inlineActions = cloneInlineActions(item.inlineActions);
|
|
143
|
+
if (item.input) out.input = true;
|
|
144
|
+
if (item.subMenu && item.subMenu.length > 0) out.subMenu = toPublic(item.subMenu);
|
|
145
|
+
return out;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function publicIcon(icon) {
|
|
149
|
+
if (icon == null || icon === "") return void 0;
|
|
150
|
+
if (typeof icon === "string") return icon;
|
|
151
|
+
const out = {};
|
|
152
|
+
if (icon.class) out.class = icon.class;
|
|
153
|
+
if (icon.text) out.text = icon.text;
|
|
154
|
+
if (!out.class && !out.text) return void 0;
|
|
155
|
+
if (out.class && !out.text) return out;
|
|
156
|
+
if (!out.class && out.text) return out.text;
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
function findItem(items, id) {
|
|
160
|
+
if (!id) return null;
|
|
161
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
162
|
+
const item = items[index];
|
|
163
|
+
if (!item) continue;
|
|
164
|
+
if (item.id === id) return { item, list: items, index };
|
|
165
|
+
if (item.type === void 0 && item.subMenu) {
|
|
166
|
+
const nested = findItem(item.subMenu, id);
|
|
167
|
+
if (nested) return nested;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
function removeItem(items, id) {
|
|
173
|
+
const found = findItem(items, id);
|
|
174
|
+
if (!found) return false;
|
|
175
|
+
found.list.splice(found.index, 1);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
function moveItem(items, id, direction) {
|
|
179
|
+
const found = findItem(items, id);
|
|
180
|
+
if (!found) return false;
|
|
181
|
+
const target = found.index + direction;
|
|
182
|
+
if (target < 0 || target >= found.list.length) return false;
|
|
183
|
+
const current = found.list[found.index];
|
|
184
|
+
const swap = found.list[target];
|
|
185
|
+
if (!current || !swap) return false;
|
|
186
|
+
found.list[found.index] = swap;
|
|
187
|
+
found.list[target] = current;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
function emptyItem(label = "New action") {
|
|
191
|
+
return { id: nextId(), label, icon: "\u25CF" };
|
|
192
|
+
}
|
|
193
|
+
function emptySeparator() {
|
|
194
|
+
return { type: "separator", id: nextId() };
|
|
195
|
+
}
|
|
196
|
+
function emptyHeading(label = "New section") {
|
|
197
|
+
return { type: "heading", id: nextId(), label };
|
|
198
|
+
}
|
|
199
|
+
function readIconFields(icon) {
|
|
200
|
+
if (icon == null || icon === "") return { text: "", className: "" };
|
|
201
|
+
if (typeof icon === "string") return { text: icon, className: "" };
|
|
202
|
+
return { text: icon.text ?? "", className: icon.class ?? "" };
|
|
203
|
+
}
|
|
204
|
+
function writeIcon(text, className) {
|
|
205
|
+
const trimmedText = text.trim();
|
|
206
|
+
const trimmedClass = className.trim();
|
|
207
|
+
if (!trimmedText && !trimmedClass) return void 0;
|
|
208
|
+
if (trimmedClass) {
|
|
209
|
+
return {
|
|
210
|
+
...trimmedClass ? { class: trimmedClass } : {},
|
|
211
|
+
...trimmedText ? { text: trimmedText } : {}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
return trimmedText;
|
|
215
|
+
}
|
|
216
|
+
function actionIcon(name, id, title, symbol) {
|
|
217
|
+
return `<button type="button" class="jscmb-icon" data-${name}="${escapeText(id)}" title="${escapeText(title)}" aria-label="${escapeText(title)}">${symbol}</button>`;
|
|
218
|
+
}
|
|
219
|
+
function itemKind(item) {
|
|
220
|
+
if (item.type === "heading") return "heading";
|
|
221
|
+
if (item.type === "separator") return "separator";
|
|
222
|
+
if (item.type === "checkbox") return `checkbox (${item.checked ? "checked" : "unchecked"})`;
|
|
223
|
+
if (item.type === "radio") return `option (${item.checked ? "selected" : "not selected"})`;
|
|
224
|
+
if (item.subMenu && item.subMenu.length > 0) return "submenu";
|
|
225
|
+
if (item.input) return "file";
|
|
226
|
+
if (item.disabled) return "disabled";
|
|
227
|
+
return "action";
|
|
228
|
+
}
|
|
229
|
+
function leafActions(id, canHaveChildren) {
|
|
230
|
+
const addChild = canHaveChildren ? actionIcon("add-child", id, "Add sub-item", "\u25A3") : "";
|
|
231
|
+
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
|
+
}
|
|
233
|
+
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>`;
|
|
235
|
+
}
|
|
236
|
+
function separatorActions(id) {
|
|
237
|
+
return `<span class="jscmb-actions">${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
|
|
238
|
+
}
|
|
239
|
+
function headingActions(id) {
|
|
240
|
+
return `<span class="jscmb-actions">${actionIcon("edit", id, "Edit", "\u2699")}${actionIcon("up", id, "Move up", "\u2191")}${actionIcon("down", id, "Move down", "\u2193")}${actionIcon("remove", id, "Delete", "\xD7")}</span>`;
|
|
241
|
+
}
|
|
242
|
+
function renderHeading(item) {
|
|
243
|
+
const label = escapeText(item.label);
|
|
244
|
+
return `<div class="jscmb-item jscmb-heading-node" data-node-id="${escapeText(item.id)}"><b class="jscmb-handle" title="Heading">\u2630</b><span class="jscmb-name" title="${label}">${label} <small>(heading)</small></span>${headingActions(item.id)}</div>`;
|
|
245
|
+
}
|
|
246
|
+
function renderLeaf(item) {
|
|
247
|
+
const label = escapeText(item.label);
|
|
248
|
+
const kind = escapeText(itemKind(item));
|
|
249
|
+
const fields = readIconFields(item.icon);
|
|
250
|
+
const preview = escapeText(iconPreview(item.icon) || (fields.className ? "" : "\xB7"));
|
|
251
|
+
const iconClass = ["jscmb-item-icon", fields.className].filter(Boolean).join(" ");
|
|
252
|
+
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>`;
|
|
254
|
+
}
|
|
255
|
+
function renderSeparator(item) {
|
|
256
|
+
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>`;
|
|
257
|
+
}
|
|
258
|
+
function renderList(items, owner) {
|
|
259
|
+
const body = items.length ? items.map(renderNode).join("") : "<em>Add an item\u2026</em>";
|
|
260
|
+
return `<div class="jscmb-list" data-list="${escapeText(owner)}">${body}</div>`;
|
|
261
|
+
}
|
|
262
|
+
function renderNested(item) {
|
|
263
|
+
const label = escapeText(item.label);
|
|
264
|
+
const preview = escapeText(iconPreview(item.icon) || "\u25B8");
|
|
265
|
+
const children = item.subMenu ?? [];
|
|
266
|
+
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
|
+
}
|
|
268
|
+
function renderNode(item) {
|
|
269
|
+
if (item.type === "heading") return renderHeading(item);
|
|
270
|
+
if (item.type === "separator") return renderSeparator(item);
|
|
271
|
+
if (item.type === void 0 && item.subMenu && item.subMenu.length > 0) {
|
|
272
|
+
return renderNested(item);
|
|
273
|
+
}
|
|
274
|
+
return renderLeaf(item);
|
|
275
|
+
}
|
|
276
|
+
function renderTree(items, title = "Menu") {
|
|
277
|
+
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>`;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/palette.ts
|
|
282
|
+
function parseHexColor(value) {
|
|
283
|
+
if (!/^#([\da-f]{3}|[\da-f]{6})$/i.test(value)) return void 0;
|
|
284
|
+
const digits = value.slice(1);
|
|
285
|
+
const hex = digits.length === 3 ? [...digits].map((digit) => digit + digit).join("") : digits;
|
|
286
|
+
return [
|
|
287
|
+
Number.parseInt(hex.slice(0, 2), 16),
|
|
288
|
+
Number.parseInt(hex.slice(2, 4), 16),
|
|
289
|
+
Number.parseInt(hex.slice(4, 6), 16)
|
|
290
|
+
];
|
|
291
|
+
}
|
|
292
|
+
function luminance(color) {
|
|
293
|
+
const linear = (component) => {
|
|
294
|
+
const value = component / 255;
|
|
295
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
296
|
+
};
|
|
297
|
+
return 0.2126 * linear(color[0]) + 0.7152 * linear(color[1]) + 0.0722 * linear(color[2]);
|
|
298
|
+
}
|
|
299
|
+
function contrastRatio(first, second) {
|
|
300
|
+
const a = luminance(first);
|
|
301
|
+
const b = luminance(second);
|
|
302
|
+
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
|
|
303
|
+
}
|
|
304
|
+
function mixColor(color, target, weight) {
|
|
305
|
+
return [
|
|
306
|
+
Math.round(color[0] * weight + target[0] * (1 - weight)),
|
|
307
|
+
Math.round(color[1] * weight + target[1] * (1 - weight)),
|
|
308
|
+
Math.round(color[2] * weight + target[2] * (1 - weight))
|
|
309
|
+
];
|
|
310
|
+
}
|
|
311
|
+
function toHex(color) {
|
|
312
|
+
return `#${color.map((component) => component.toString(16).padStart(2, "0")).join("")}`;
|
|
313
|
+
}
|
|
314
|
+
function contrastingColor(color, background, minimum = 4.5) {
|
|
315
|
+
const black = [0, 0, 0];
|
|
316
|
+
const white = [255, 255, 255];
|
|
317
|
+
const target = contrastRatio(black, background) >= contrastRatio(white, background) ? black : white;
|
|
318
|
+
for (let step = 0; step <= 255; step++) {
|
|
319
|
+
const adjusted = mixColor(color, target, 1 - step / 255);
|
|
320
|
+
if (contrastRatio(adjusted, background) >= minimum) return toHex(adjusted);
|
|
321
|
+
}
|
|
322
|
+
throw new Error("Unable to generate a contrasting text color");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/index.ts
|
|
326
|
+
var DEFAULT_THEMES = [
|
|
327
|
+
{ value: "default", label: "Default" },
|
|
328
|
+
{ value: "glass", label: "Glass" }
|
|
329
|
+
];
|
|
330
|
+
function query(root, selector) {
|
|
331
|
+
const found = root.querySelector(selector);
|
|
332
|
+
if (!(found instanceof HTMLElement)) throw new TypeError(`Missing ${selector}`);
|
|
333
|
+
return found;
|
|
334
|
+
}
|
|
335
|
+
function normalizeScheme(value) {
|
|
336
|
+
return value === "light" || value === "dark" ? value : "auto";
|
|
337
|
+
}
|
|
338
|
+
function normalizeSize(value) {
|
|
339
|
+
return value === "small" || value === "large" ? value : "normal";
|
|
340
|
+
}
|
|
341
|
+
function normalizeTheme(value) {
|
|
342
|
+
return typeof value === "string" && value.length > 0 ? value : "default";
|
|
343
|
+
}
|
|
344
|
+
function safeHref(value) {
|
|
345
|
+
const text = String(value ?? "").trim();
|
|
346
|
+
if (!text || /^[\s\u0000]*(javascript|vbscript|data):/i.test(text)) return "";
|
|
347
|
+
return text;
|
|
348
|
+
}
|
|
349
|
+
function structureHeading(options, name) {
|
|
350
|
+
const href = safeHref(options.structureTitleHref);
|
|
351
|
+
const title = escapeText(options.structureTitle || name);
|
|
352
|
+
if (href) {
|
|
353
|
+
return `<h2><a href="${escapeText(href)}" title="Back to presets">\u2039 ${title}</a></h2>`;
|
|
354
|
+
}
|
|
355
|
+
return `<h2>${title}</h2>`;
|
|
356
|
+
}
|
|
357
|
+
function themeOptions(themes, selected) {
|
|
358
|
+
return themes.map((theme) => {
|
|
359
|
+
const current = theme.value === selected ? " selected" : "";
|
|
360
|
+
return `<option value="${escapeText(theme.value)}"${current}>${escapeText(theme.label)}</option>`;
|
|
361
|
+
}).join("");
|
|
362
|
+
}
|
|
363
|
+
function sizeOptions(selected) {
|
|
364
|
+
return [
|
|
365
|
+
{ value: "large", label: "Large" },
|
|
366
|
+
{ value: "normal", label: "Normal" },
|
|
367
|
+
{ value: "small", label: "Small" }
|
|
368
|
+
].map((size) => {
|
|
369
|
+
const current = size.value === selected ? " selected" : "";
|
|
370
|
+
return `<option value="${size.value}"${current}>${size.label}</option>`;
|
|
371
|
+
}).join("");
|
|
372
|
+
}
|
|
373
|
+
function schemeButtons(selected) {
|
|
374
|
+
return [
|
|
375
|
+
{ value: "auto", label: "Automatic" },
|
|
376
|
+
{ value: "light", label: "Light" },
|
|
377
|
+
{ value: "dark", label: "Dark" }
|
|
378
|
+
].map((scheme) => {
|
|
379
|
+
const checked = scheme.value === selected ? " checked" : "";
|
|
380
|
+
return `<label><input type="radio" name="colorScheme" value="${scheme.value}"${checked}> ${scheme.label}</label>`;
|
|
381
|
+
}).join("");
|
|
382
|
+
}
|
|
383
|
+
function colorControl(label, name, value = "", placeholder = "Default") {
|
|
384
|
+
return `<label>${label}<span class="jscmb-color-control">
|
|
385
|
+
<input type="text" name="${name}" value="${escapeText(value)}" placeholder="${placeholder}" aria-label="${label}">
|
|
386
|
+
<input type="color" data-color-for="${name}" aria-label="Choose ${label.toLowerCase()}">
|
|
387
|
+
</span></label>`;
|
|
388
|
+
}
|
|
389
|
+
function itemStyleControls(style) {
|
|
390
|
+
const checked = (value) => value ? " checked" : "";
|
|
391
|
+
return `<fieldset data-item-style>
|
|
392
|
+
<legend>Item style</legend>
|
|
393
|
+
<div class="jscmb-style-flags">
|
|
394
|
+
<label><input type="checkbox" name="bold"${checked(style.bold)}> Bold</label>
|
|
395
|
+
<label><input type="checkbox" name="italic"${checked(style.italic)}> Italic</label>
|
|
396
|
+
<label><input type="checkbox" name="underline"${checked(style.underline)}> Underline</label>
|
|
397
|
+
</div>
|
|
398
|
+
<div class="jscmb-theme-controls">
|
|
399
|
+
${colorControl("Primary color", "primaryColor", "", "#rrggbb")}
|
|
400
|
+
${colorControl("Secondary color", "secondaryColor", "", "#rrggbb")}
|
|
401
|
+
</div>
|
|
402
|
+
<small>The primary color is the menu background; the secondary influences the text and accents. Either color can be used alone.</small>
|
|
403
|
+
<div class="jscmb-theme-controls">
|
|
404
|
+
${colorControl("Menu background", "background", style.background, "Theme default")}
|
|
405
|
+
${colorControl("Border and separator", "borderColor", style.borderColor, "Theme default")}
|
|
406
|
+
${colorControl("Text color", "color", style.color, "Theme default")}
|
|
407
|
+
${colorControl("Hover background", "hoverBackground", style.hoverBackground, "Theme default")}
|
|
408
|
+
${colorControl("Hover text color", "hoverColor", style.hoverColor, "Theme default")}
|
|
409
|
+
</div>
|
|
410
|
+
<small>Overrides accept CSS colors. Check their contrast in each color scheme.</small>
|
|
411
|
+
</fieldset>`;
|
|
412
|
+
}
|
|
413
|
+
function shell(options, name, appearance) {
|
|
414
|
+
const themes = options.themes?.length ? options.themes : DEFAULT_THEMES;
|
|
415
|
+
const hint = escapeText(
|
|
416
|
+
options.previewHint ?? "Right-click in this area to test the menu."
|
|
417
|
+
);
|
|
418
|
+
const schemeAttr = appearance.colorScheme === "auto" ? "" : ` data-jscm-color-scheme="${appearance.colorScheme}"`;
|
|
419
|
+
return `<div class="jscmb"${schemeAttr}>
|
|
420
|
+
<div class="jscmb-layout">
|
|
421
|
+
<div class="jscmb-section-heading jscmb-structure-heading">${structureHeading(options, name)}</div>
|
|
422
|
+
<div class="jscmb-section-heading jscmb-preview-heading">
|
|
423
|
+
<h2>Preview</h2>
|
|
424
|
+
<span class="jscmb-preview-name" data-preview-name>${escapeText(name)}</span>
|
|
425
|
+
</div>
|
|
426
|
+
<div class="jscmb-preview-bar jscmb-theme-bar" data-theme-bar>
|
|
427
|
+
<fieldset>
|
|
428
|
+
<legend>Mode</legend>
|
|
429
|
+
<div class="jscmb-scheme">${schemeButtons(appearance.colorScheme)}</div>
|
|
430
|
+
</fieldset>
|
|
431
|
+
<div class="jscmb-theme-controls">
|
|
432
|
+
<label>Theme<select name="theme" data-theme>${themeOptions(themes, appearance.theme)}</select></label>
|
|
433
|
+
<label>Size<select name="size" data-size>${sizeOptions(appearance.size)}</select></label>
|
|
434
|
+
<label>Radius<input type="text" name="radius" data-radius value="${escapeText(options.radius ?? "")}" placeholder="Size default" aria-label="Menu border radius"></label>
|
|
435
|
+
</div>
|
|
436
|
+
<div class="jscmb-style-flags jscmb-wrap-control">
|
|
437
|
+
<label><input type="checkbox" name="wrapLabels" data-wrap-labels${options.wrapLabels ? " checked" : ""}> Wrap long labels</label>
|
|
438
|
+
<label><input type="checkbox" name="closeOnMouseLeave" data-close-on-mouse-leave${options.closeOnMouseLeave !== false ? " checked" : ""}> Close when pointer leaves menu</label>
|
|
439
|
+
</div>
|
|
440
|
+
${itemStyleControls(options.itemStyle ?? {})}
|
|
441
|
+
</div>
|
|
442
|
+
<div class="jscmb-preview-bar jscmb-notes">
|
|
443
|
+
<p>The left panel edits the structure. The preview applies the theme, color scheme, size and radius. The JSON configuration is at the bottom of the page.</p>
|
|
444
|
+
</div>
|
|
445
|
+
<div class="jscmb-structure" data-tree></div>
|
|
446
|
+
<div class="jscmb-preview-column">
|
|
447
|
+
<section class="jscmb-preview-panel" data-preview tabindex="0">
|
|
448
|
+
<strong>Test area</strong>
|
|
449
|
+
<p>${hint}</p>
|
|
450
|
+
<p class="jscmb-log" data-log></p>
|
|
451
|
+
</section>
|
|
452
|
+
<details>
|
|
453
|
+
<summary>Menu data</summary>
|
|
454
|
+
<pre class="jscmb-json" data-form-json></pre>
|
|
455
|
+
</details>
|
|
456
|
+
</div>
|
|
457
|
+
</div>
|
|
458
|
+
<details>
|
|
459
|
+
<summary>Configuration JSON</summary>
|
|
460
|
+
<pre class="jscmb-json" data-json></pre>
|
|
461
|
+
</details>
|
|
462
|
+
<details>
|
|
463
|
+
<summary>Menu options JSON</summary>
|
|
464
|
+
<pre class="jscmb-json" data-options-json></pre>
|
|
465
|
+
</details>
|
|
466
|
+
<dialog class="jscmb-dialog" data-dialog>
|
|
467
|
+
<form method="dialog" data-editor>
|
|
468
|
+
<h2 data-editor-title>Item</h2>
|
|
469
|
+
<div class="jscmb-dialog-body">
|
|
470
|
+
<label>Label<input name="label" type="text" required></label>
|
|
471
|
+
<label>Type<select name="itemType">
|
|
472
|
+
<option value="action">Action</option>
|
|
473
|
+
<option value="checkbox">Checkbox</option>
|
|
474
|
+
<option value="radio">Radio option</option>
|
|
475
|
+
<option value="heading">Heading</option>
|
|
476
|
+
</select></label>
|
|
477
|
+
<label data-item-field>Symbol (Unicode / text)<input name="iconText" type="text" placeholder="\u2605 or content_copy"></label>
|
|
478
|
+
<label data-item-field>CSS class (optional)<input name="iconClass" type="text" placeholder="material-symbols-outlined or fa-solid fa-star"></label>
|
|
479
|
+
<label data-item-field>Displayed shortcut<input name="shortcut" type="text" placeholder="\u2318S"></label>
|
|
480
|
+
<label class="jscmb-badge-toggle" data-item-field><input name="badgeEnabled" type="checkbox"> Notification badge</label>
|
|
481
|
+
<fieldset class="jscmb-badge-fields" data-badge-fields data-item-field disabled>
|
|
482
|
+
<legend>Notification badge</legend>
|
|
483
|
+
<label>Text or number (empty for a dot)<input name="badgeText" type="text"></label>
|
|
484
|
+
<label>Text color<input name="badgeColor" type="text" placeholder="Default"></label>
|
|
485
|
+
<label>Background color<input name="badgeBackgroundColor" type="text" placeholder="Default"></label>
|
|
486
|
+
<label>Accessible description (optional)<input name="badgeAriaLabel" type="text"></label>
|
|
487
|
+
</fieldset>
|
|
488
|
+
<div class="jscmb-checks" data-item-field>
|
|
489
|
+
<label><input name="disabled" type="checkbox"> Disabled</label>
|
|
490
|
+
<label data-file-input-field><input name="input" type="checkbox"> File picker</label>
|
|
491
|
+
<label data-checked-field><input name="checked" type="checkbox"> Checked / selected</label>
|
|
492
|
+
</div>
|
|
493
|
+
<label data-radio-group-field data-item-field>Radio group<input name="radioGroup" type="text" value="default"></label>
|
|
494
|
+
<fieldset data-item-field data-inline-field>
|
|
495
|
+
<legend>Inline icon actions</legend>
|
|
496
|
+
<div data-inline-actions></div>
|
|
497
|
+
<button type="button" data-add-inline-action>Add icon action</button>
|
|
498
|
+
</fieldset>
|
|
499
|
+
</div>
|
|
500
|
+
<menu>
|
|
501
|
+
<button type="button" class="jscmb-danger" data-delete hidden>Delete</button>
|
|
502
|
+
<button value="cancel">Cancel</button>
|
|
503
|
+
<button value="save">Save</button>
|
|
504
|
+
</menu>
|
|
505
|
+
</form>
|
|
506
|
+
</dialog>
|
|
507
|
+
</div>`;
|
|
508
|
+
}
|
|
509
|
+
function runtimeItems(items, onAction) {
|
|
510
|
+
return items.map((item) => {
|
|
511
|
+
if (item.type === "heading") return { type: "heading", label: item.label };
|
|
512
|
+
if (item.type === "separator") return { type: "separator" };
|
|
513
|
+
const base = {
|
|
514
|
+
label: item.label,
|
|
515
|
+
...item.icon ? { icon: item.icon } : {},
|
|
516
|
+
...item.disabled ? { disabled: true } : {},
|
|
517
|
+
...item.shortcut ? { shortcut: item.shortcut } : {},
|
|
518
|
+
...item.badge ? { badge: { ...item.badge } } : {},
|
|
519
|
+
...item.inlineActions ? {
|
|
520
|
+
inlineActions: item.inlineActions.map((inlineAction) => ({
|
|
521
|
+
...inlineAction,
|
|
522
|
+
action: () => onAction(`${item.label} \u2192 ${inlineAction.label}`)
|
|
523
|
+
}))
|
|
524
|
+
} : {}
|
|
525
|
+
};
|
|
526
|
+
if (item.type === "checkbox") {
|
|
527
|
+
return {
|
|
528
|
+
...base,
|
|
529
|
+
type: "checkbox",
|
|
530
|
+
checked: item.checked,
|
|
531
|
+
action: (_data, _file, checked) => {
|
|
532
|
+
item.checked = checked ?? false;
|
|
533
|
+
onAction(item.label, item.checked);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
if (item.type === "radio") {
|
|
538
|
+
return {
|
|
539
|
+
...base,
|
|
540
|
+
type: "radio",
|
|
541
|
+
checked: item.checked,
|
|
542
|
+
radioGroup: item.radioGroup,
|
|
543
|
+
action: (_data, _file, checked) => {
|
|
544
|
+
for (const sibling of items) {
|
|
545
|
+
if (sibling.type === "radio" && sibling.radioGroup === item.radioGroup) {
|
|
546
|
+
sibling.checked = sibling === item && checked === true;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
onAction(item.label, item.checked);
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
if (item.type === void 0 && item.subMenu && item.subMenu.length > 0) {
|
|
554
|
+
return { ...base, subMenu: runtimeItems(item.subMenu, onAction) };
|
|
555
|
+
}
|
|
556
|
+
return {
|
|
557
|
+
...base,
|
|
558
|
+
...item.input ? { input: true } : {},
|
|
559
|
+
action: (_data, file) => {
|
|
560
|
+
onAction(file ? `${item.label} \u2192 ${file.name}` : item.label);
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
function createContextMenuBuilder(container, initialItems = [], options = {}) {
|
|
566
|
+
let items = cloneItems(initialItems);
|
|
567
|
+
let editingId = null;
|
|
568
|
+
let menu = null;
|
|
569
|
+
let theme = normalizeTheme(options.theme);
|
|
570
|
+
let size = normalizeSize(options.size);
|
|
571
|
+
let radius = options.radius;
|
|
572
|
+
let colorScheme = normalizeScheme(options.colorScheme);
|
|
573
|
+
let wrapLabels = options.wrapLabels ?? false;
|
|
574
|
+
let closeOnMouseLeave = options.closeOnMouseLeave !== false;
|
|
575
|
+
let itemStyle = { ...options.itemStyle };
|
|
576
|
+
let hasPalette = false;
|
|
577
|
+
const title = options.structureTitle || "Menu";
|
|
578
|
+
container.innerHTML = shell(options, title, { theme, size, colorScheme });
|
|
579
|
+
const root = query(container, ".jscmb");
|
|
580
|
+
const treeHost = query(container, "[data-tree]");
|
|
581
|
+
const preview = query(container, "[data-preview]");
|
|
582
|
+
const jsonOut = query(container, "[data-json]");
|
|
583
|
+
const optionsJson = query(container, "[data-options-json]");
|
|
584
|
+
const styleControls = query(container, "[data-item-style]");
|
|
585
|
+
const formJson = query(container, "[data-form-json]");
|
|
586
|
+
const log = query(container, "[data-log]");
|
|
587
|
+
const themeSelectEl = query(container, "[data-theme]");
|
|
588
|
+
const sizeSelectEl = query(container, "[data-size]");
|
|
589
|
+
const radiusEl = query(container, "[data-radius]");
|
|
590
|
+
const wrapLabelsEl = query(container, "[data-wrap-labels]");
|
|
591
|
+
const closeOnMouseLeaveEl = query(container, "[data-close-on-mouse-leave]");
|
|
592
|
+
const dialogEl = query(container, "[data-dialog]");
|
|
593
|
+
const formEl = query(container, "[data-editor]");
|
|
594
|
+
const deleteBtn = query(container, "[data-delete]");
|
|
595
|
+
const editorTitle = query(container, "[data-editor-title]");
|
|
596
|
+
const inlineActionsHost = query(container, "[data-inline-actions]");
|
|
597
|
+
if (!(dialogEl instanceof HTMLDialogElement) || !(formEl instanceof HTMLFormElement)) {
|
|
598
|
+
throw new TypeError("Missing builder dialog");
|
|
599
|
+
}
|
|
600
|
+
if (!(themeSelectEl instanceof HTMLSelectElement) || !(sizeSelectEl instanceof HTMLSelectElement)) {
|
|
601
|
+
throw new TypeError("Missing theme controls");
|
|
602
|
+
}
|
|
603
|
+
if (!(wrapLabelsEl instanceof HTMLInputElement)) {
|
|
604
|
+
throw new TypeError("Missing label wrapping control");
|
|
605
|
+
}
|
|
606
|
+
if (!(closeOnMouseLeaveEl instanceof HTMLInputElement)) {
|
|
607
|
+
throw new TypeError("Missing pointer leave control");
|
|
608
|
+
}
|
|
609
|
+
if (!(radiusEl instanceof HTMLInputElement)) throw new TypeError("Missing radius control");
|
|
610
|
+
const dialog = dialogEl;
|
|
611
|
+
const form = formEl;
|
|
612
|
+
const themeSelect = themeSelectEl;
|
|
613
|
+
const sizeSelect = sizeSelectEl;
|
|
614
|
+
const radiusInput = radiusEl;
|
|
615
|
+
const wrapLabelsInput = wrapLabelsEl;
|
|
616
|
+
const closeOnMouseLeaveInput = closeOnMouseLeaveEl;
|
|
617
|
+
const darkPreference = window.matchMedia("(prefers-color-scheme: dark)");
|
|
618
|
+
function renderedBackground(background, overlay) {
|
|
619
|
+
if (!menu) throw new Error("Missing preview menu");
|
|
620
|
+
const sample = document.createElement("span");
|
|
621
|
+
menu.element.append(sample);
|
|
622
|
+
sample.style.backgroundColor = background;
|
|
623
|
+
const resolvedBackground = getComputedStyle(sample).backgroundColor;
|
|
624
|
+
sample.style.backgroundColor = overlay ?? "";
|
|
625
|
+
const resolvedOverlay = overlay ? getComputedStyle(sample).backgroundColor : "";
|
|
626
|
+
sample.remove();
|
|
627
|
+
const canvas = document.createElement("canvas");
|
|
628
|
+
canvas.width = canvas.height = 1;
|
|
629
|
+
const context = canvas.getContext("2d");
|
|
630
|
+
if (!context) throw new Error("Unable to calculate palette colors");
|
|
631
|
+
context.fillStyle = colorScheme === "dark" || colorScheme === "auto" && darkPreference.matches ? "#000000" : "#ffffff";
|
|
632
|
+
context.fillRect(0, 0, 1, 1);
|
|
633
|
+
context.fillStyle = resolvedBackground;
|
|
634
|
+
context.fillRect(0, 0, 1, 1);
|
|
635
|
+
if (resolvedOverlay) {
|
|
636
|
+
context.fillStyle = resolvedOverlay;
|
|
637
|
+
context.fillRect(0, 0, 1, 1);
|
|
638
|
+
}
|
|
639
|
+
const pixel = context.getImageData(0, 0, 1, 1).data;
|
|
640
|
+
const [red, green, blue] = pixel;
|
|
641
|
+
if (red === void 0 || green === void 0 || blue === void 0) {
|
|
642
|
+
throw new Error("Unable to read palette background");
|
|
643
|
+
}
|
|
644
|
+
return [red, green, blue];
|
|
645
|
+
}
|
|
646
|
+
function syncColorPicker(input, colorValue = input.value) {
|
|
647
|
+
const picker = root.querySelector(`[data-color-for="${input.name}"]`);
|
|
648
|
+
if (!(picker instanceof HTMLInputElement)) return;
|
|
649
|
+
const value = colorValue.trim();
|
|
650
|
+
picker.value = "#000000";
|
|
651
|
+
if (!value || !CSS.supports("color", value)) return;
|
|
652
|
+
const sample = document.createElement("span");
|
|
653
|
+
sample.style.color = value;
|
|
654
|
+
input.parentElement?.append(sample);
|
|
655
|
+
const color = getComputedStyle(sample).color;
|
|
656
|
+
sample.remove();
|
|
657
|
+
const rgb = color.match(/^rgba?\(\s*(\d+),\s*(\d+),\s*(\d+)/);
|
|
658
|
+
if (rgb) {
|
|
659
|
+
picker.value = `#${rgb.slice(1).map((component) => Number(component).toString(16).padStart(2, "0")).join("")}`;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
for (const input of root.querySelectorAll('.jscmb-color-control input[type="text"]')) {
|
|
663
|
+
syncColorPicker(input);
|
|
664
|
+
}
|
|
665
|
+
function applyDocumentScheme() {
|
|
666
|
+
if (colorScheme === "auto") {
|
|
667
|
+
delete document.documentElement.dataset.jscmColorScheme;
|
|
668
|
+
delete root.dataset.jscmColorScheme;
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
document.documentElement.dataset.jscmColorScheme = colorScheme;
|
|
672
|
+
root.dataset.jscmColorScheme = colorScheme;
|
|
673
|
+
}
|
|
674
|
+
function emitAppearance() {
|
|
675
|
+
options.onThemeChange?.(theme);
|
|
676
|
+
options.onSizeChange?.(size);
|
|
677
|
+
options.onColorSchemeChange?.(colorScheme);
|
|
678
|
+
container.dispatchEvent(
|
|
679
|
+
new CustomEvent("contextmenubuilder:themechange", { detail: theme })
|
|
680
|
+
);
|
|
681
|
+
container.dispatchEvent(
|
|
682
|
+
new CustomEvent("contextmenubuilder:sizechange", { detail: size })
|
|
683
|
+
);
|
|
684
|
+
container.dispatchEvent(
|
|
685
|
+
new CustomEvent("contextmenubuilder:colorschemechange", { detail: colorScheme })
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
function emit() {
|
|
689
|
+
const publicItems = toPublic(items);
|
|
690
|
+
const json = JSON.stringify(publicItems, null, 2);
|
|
691
|
+
jsonOut.textContent = json;
|
|
692
|
+
emitOptions();
|
|
693
|
+
formJson.textContent = json;
|
|
694
|
+
container.dispatchEvent(
|
|
695
|
+
new CustomEvent("contextmenubuilder:change", { detail: publicItems })
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
function emitOptions() {
|
|
699
|
+
optionsJson.textContent = JSON.stringify({ itemStyle, wrapLabels, closeOnMouseLeave, ...radius !== void 0 ? { radius } : {} }, null, 2);
|
|
700
|
+
}
|
|
701
|
+
function bindPreview() {
|
|
702
|
+
menu?.destroy();
|
|
703
|
+
const previewMenu = createContextMenu({
|
|
704
|
+
mount: document.body,
|
|
705
|
+
theme,
|
|
706
|
+
size,
|
|
707
|
+
...radius !== void 0 ? { radius } : {},
|
|
708
|
+
colorScheme,
|
|
709
|
+
itemStyle,
|
|
710
|
+
wrapLabels,
|
|
711
|
+
closeOnMouseLeave,
|
|
712
|
+
...options.showShortcuts !== void 0 ? { showShortcuts: options.showShortcuts } : {}
|
|
713
|
+
});
|
|
714
|
+
menu = previewMenu;
|
|
715
|
+
previewMenu.attach(preview, () => {
|
|
716
|
+
const previewItems = runtimeItems(items, (label, checked) => {
|
|
717
|
+
log.textContent = checked === void 0 ? `Action: ${label}` : `${label}: ${checked ? "checked" : "unchecked"}`;
|
|
718
|
+
if (checked !== void 0) {
|
|
719
|
+
treeHost.innerHTML = renderTree(items, title);
|
|
720
|
+
emit();
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
return options.transformPreviewItems?.(previewItems, previewMenu) ?? previewItems;
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
function redraw() {
|
|
727
|
+
treeHost.innerHTML = renderTree(items, title);
|
|
728
|
+
emit();
|
|
729
|
+
bindPreview();
|
|
730
|
+
}
|
|
731
|
+
function setTheme(next) {
|
|
732
|
+
theme = normalizeTheme(next);
|
|
733
|
+
themeSelect.value = theme;
|
|
734
|
+
emitAppearance();
|
|
735
|
+
if (hasPalette) updateItemStyle();
|
|
736
|
+
else bindPreview();
|
|
737
|
+
}
|
|
738
|
+
function setSize(next) {
|
|
739
|
+
size = normalizeSize(next);
|
|
740
|
+
sizeSelect.value = size;
|
|
741
|
+
menu?.setSize(size);
|
|
742
|
+
emitAppearance();
|
|
743
|
+
}
|
|
744
|
+
function setRadius(next) {
|
|
745
|
+
if (next !== void 0 && !CSS.supports("border-radius", next)) {
|
|
746
|
+
throw new TypeError(`Invalid CSS border radius: ${next}`);
|
|
747
|
+
}
|
|
748
|
+
menu?.setRadius(next);
|
|
749
|
+
radius = next;
|
|
750
|
+
radiusInput.value = next ?? "";
|
|
751
|
+
radiusInput.setCustomValidity("");
|
|
752
|
+
emitOptions();
|
|
753
|
+
options.onRadiusChange?.(radius);
|
|
754
|
+
container.dispatchEvent(
|
|
755
|
+
new CustomEvent("contextmenubuilder:radiuschange", { detail: radius ?? null })
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
function setColorScheme(next) {
|
|
759
|
+
colorScheme = normalizeScheme(next);
|
|
760
|
+
applyDocumentScheme();
|
|
761
|
+
for (const input of root.querySelectorAll('input[name="colorScheme"]')) {
|
|
762
|
+
if (input instanceof HTMLInputElement) {
|
|
763
|
+
input.checked = input.value === colorScheme;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
menu?.setColorScheme(colorScheme);
|
|
767
|
+
emitAppearance();
|
|
768
|
+
if (hasPalette) updateItemStyle();
|
|
769
|
+
}
|
|
770
|
+
function setWrapLabels(next) {
|
|
771
|
+
wrapLabels = next;
|
|
772
|
+
wrapLabelsInput.checked = next;
|
|
773
|
+
emitOptions();
|
|
774
|
+
bindPreview();
|
|
775
|
+
options.onWrapLabelsChange?.(next);
|
|
776
|
+
container.dispatchEvent(
|
|
777
|
+
new CustomEvent("contextmenubuilder:wraplabelschange", { detail: next })
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
function setCloseOnMouseLeave(next) {
|
|
781
|
+
closeOnMouseLeave = next;
|
|
782
|
+
closeOnMouseLeaveInput.checked = next;
|
|
783
|
+
emitOptions();
|
|
784
|
+
bindPreview();
|
|
785
|
+
options.onCloseOnMouseLeaveChange?.(next);
|
|
786
|
+
container.dispatchEvent(
|
|
787
|
+
new CustomEvent("contextmenubuilder:closeonmouseleavechange", { detail: next })
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
function updateItemStyle() {
|
|
791
|
+
const values = {
|
|
792
|
+
...options.itemStyle?.badgeBackground ? { badgeBackground: options.itemStyle.badgeBackground } : {},
|
|
793
|
+
...options.itemStyle?.badgeColor ? { badgeColor: options.itemStyle.badgeColor } : {}
|
|
794
|
+
};
|
|
795
|
+
let primary;
|
|
796
|
+
let secondary;
|
|
797
|
+
let primaryValue = "";
|
|
798
|
+
let secondaryValue = "";
|
|
799
|
+
for (const input of styleControls.querySelectorAll("input")) {
|
|
800
|
+
if (!(input instanceof HTMLInputElement)) continue;
|
|
801
|
+
if (input.name === "bold" || input.name === "italic" || input.name === "underline") {
|
|
802
|
+
if (input.checked) values[input.name] = true;
|
|
803
|
+
} else if (input.name === "primaryColor" || input.name === "secondaryColor") {
|
|
804
|
+
const value = input.value.trim();
|
|
805
|
+
const parsed = value ? parseHexColor(value) : void 0;
|
|
806
|
+
input.setCustomValidity(!value || parsed ? "" : "Enter a hex color such as #2979ff.");
|
|
807
|
+
if (value && !parsed) {
|
|
808
|
+
input.reportValidity();
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
if (input.name === "primaryColor") {
|
|
812
|
+
primary = parsed;
|
|
813
|
+
primaryValue = value;
|
|
814
|
+
} else {
|
|
815
|
+
secondary = parsed;
|
|
816
|
+
secondaryValue = value;
|
|
817
|
+
}
|
|
818
|
+
} else if (input.name === "background" || input.name === "borderColor" || input.name === "color" || input.name === "hoverBackground" || input.name === "hoverColor") {
|
|
819
|
+
const value = input.value.trim();
|
|
820
|
+
const valid = !value || CSS.supports("color", value);
|
|
821
|
+
input.setCustomValidity(valid ? "" : "Enter a valid CSS color.");
|
|
822
|
+
if (!valid) {
|
|
823
|
+
input.reportValidity();
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
if (value) values[input.name] = value;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
const base = primary ?? secondary;
|
|
830
|
+
hasPalette = Boolean(base);
|
|
831
|
+
if (base) {
|
|
832
|
+
const baseValue = primary ? primaryValue : secondaryValue;
|
|
833
|
+
const text = secondary ?? base;
|
|
834
|
+
const surface = secondary ? mixColor(base, secondary, 0.7) : base;
|
|
835
|
+
values.background ??= baseValue;
|
|
836
|
+
const background = renderedBackground(values.background);
|
|
837
|
+
values.borderColor ??= contrastingColor(surface, background, 3);
|
|
838
|
+
values.color ??= contrastingColor(text, background);
|
|
839
|
+
values.hoverBackground ??= contrastingColor(surface, background, 1.5);
|
|
840
|
+
values.hoverColor ??= contrastingColor(text, renderedBackground(values.background, values.hoverBackground));
|
|
841
|
+
values.badgeBackground ??= contrastingColor(surface, background, 3);
|
|
842
|
+
values.badgeColor ??= contrastingColor(text, renderedBackground(values.background, values.badgeBackground));
|
|
843
|
+
}
|
|
844
|
+
for (const name of ["background", "borderColor", "color", "hoverBackground", "hoverColor"]) {
|
|
845
|
+
const input = styleControls.querySelector(`input[name="${name}"]`);
|
|
846
|
+
if (!input) throw new TypeError(`Missing ${name}`);
|
|
847
|
+
const generated = base && !input.value.trim() ? values[name] : void 0;
|
|
848
|
+
input.placeholder = generated ? `Generated: ${generated}` : "Theme default";
|
|
849
|
+
if (generated) syncColorPicker(input, generated);
|
|
850
|
+
else if (!input.value.trim()) syncColorPicker(input);
|
|
851
|
+
}
|
|
852
|
+
itemStyle = values;
|
|
853
|
+
emitOptions();
|
|
854
|
+
bindPreview();
|
|
855
|
+
options.onItemStyleChange?.({ ...itemStyle });
|
|
856
|
+
container.dispatchEvent(
|
|
857
|
+
new CustomEvent("contextmenubuilder:itemstylechange", { detail: { ...itemStyle } })
|
|
858
|
+
);
|
|
859
|
+
}
|
|
860
|
+
function onSystemColorSchemeChange() {
|
|
861
|
+
if (colorScheme === "auto" && hasPalette) updateItemStyle();
|
|
862
|
+
}
|
|
863
|
+
darkPreference.addEventListener("change", onSystemColorSchemeChange);
|
|
864
|
+
function openEditor(item) {
|
|
865
|
+
editingId = item?.id ?? null;
|
|
866
|
+
const labelInput = form.elements.namedItem("label");
|
|
867
|
+
const typeInput = form.elements.namedItem("itemType");
|
|
868
|
+
const iconTextInput = form.elements.namedItem("iconText");
|
|
869
|
+
const iconClassInput = form.elements.namedItem("iconClass");
|
|
870
|
+
const shortcutInput = form.elements.namedItem("shortcut");
|
|
871
|
+
const badgeEnabled = form.elements.namedItem("badgeEnabled");
|
|
872
|
+
const badgeText = form.elements.namedItem("badgeText");
|
|
873
|
+
const badgeColor = form.elements.namedItem("badgeColor");
|
|
874
|
+
const badgeBackgroundColor = form.elements.namedItem("badgeBackgroundColor");
|
|
875
|
+
const badgeAriaLabel = form.elements.namedItem("badgeAriaLabel");
|
|
876
|
+
const disabledInput = form.elements.namedItem("disabled");
|
|
877
|
+
const fileInput = form.elements.namedItem("input");
|
|
878
|
+
const checkedInput = form.elements.namedItem("checked");
|
|
879
|
+
const radioGroupInput = form.elements.namedItem("radioGroup");
|
|
880
|
+
if (!(labelInput instanceof HTMLInputElement)) return;
|
|
881
|
+
if (!(typeInput instanceof HTMLSelectElement)) return;
|
|
882
|
+
if (!(iconTextInput instanceof HTMLInputElement)) return;
|
|
883
|
+
if (!(iconClassInput instanceof HTMLInputElement)) return;
|
|
884
|
+
if (!(shortcutInput instanceof HTMLInputElement)) return;
|
|
885
|
+
if (!(badgeEnabled instanceof HTMLInputElement)) return;
|
|
886
|
+
if (!(badgeText instanceof HTMLInputElement)) return;
|
|
887
|
+
if (!(badgeColor instanceof HTMLInputElement)) return;
|
|
888
|
+
if (!(badgeBackgroundColor instanceof HTMLInputElement)) return;
|
|
889
|
+
if (!(badgeAriaLabel instanceof HTMLInputElement)) return;
|
|
890
|
+
if (!(disabledInput instanceof HTMLInputElement)) return;
|
|
891
|
+
if (!(fileInput instanceof HTMLInputElement)) return;
|
|
892
|
+
if (!(checkedInput instanceof HTMLInputElement)) return;
|
|
893
|
+
if (!(radioGroupInput instanceof HTMLInputElement)) return;
|
|
894
|
+
const actionItem = item?.type === "heading" ? void 0 : item;
|
|
895
|
+
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";
|
|
899
|
+
iconTextInput.value = iconFields.text || (item ? "" : "\u25CF");
|
|
900
|
+
iconClassInput.value = iconFields.className;
|
|
901
|
+
shortcutInput.value = actionItem?.shortcut ?? "";
|
|
902
|
+
badgeEnabled.checked = actionItem?.badge !== void 0;
|
|
903
|
+
badgeText.value = actionItem?.badge?.text === void 0 ? "" : String(actionItem.badge.text);
|
|
904
|
+
badgeColor.value = actionItem?.badge?.color ?? "";
|
|
905
|
+
badgeBackgroundColor.value = actionItem?.badge?.backgroundColor ?? "";
|
|
906
|
+
badgeAriaLabel.value = actionItem?.badge?.ariaLabel ?? "";
|
|
907
|
+
badgeColor.setCustomValidity("");
|
|
908
|
+
badgeBackgroundColor.setCustomValidity("");
|
|
909
|
+
syncBadgeFields(badgeEnabled.checked);
|
|
910
|
+
disabledInput.checked = Boolean(actionItem?.disabled);
|
|
911
|
+
fileInput.checked = Boolean(item && item.type === void 0 && item.input);
|
|
912
|
+
checkedInput.checked = (item?.type === "checkbox" || item?.type === "radio") && item.checked;
|
|
913
|
+
radioGroupInput.value = item?.type === "radio" ? item.radioGroup : "default";
|
|
914
|
+
inlineActionsHost.innerHTML = "";
|
|
915
|
+
for (const action of actionItem?.inlineActions ?? []) addInlineAction(action);
|
|
916
|
+
syncEditorTypeFields(typeInput.value);
|
|
917
|
+
deleteBtn.hidden = !item;
|
|
918
|
+
dialog.showModal();
|
|
919
|
+
labelInput.focus();
|
|
920
|
+
labelInput.select();
|
|
921
|
+
}
|
|
922
|
+
function addInlineAction(action) {
|
|
923
|
+
const row = document.createElement("fieldset");
|
|
924
|
+
row.dataset.inlineAction = "true";
|
|
925
|
+
const icon = readIconFields(action?.icon);
|
|
926
|
+
row.innerHTML = `<legend>Icon action</legend>
|
|
927
|
+
<label>Name<input data-inline-name type="text" value="${escapeText(action?.name ?? "")}"></label>
|
|
928
|
+
<label>Accessible label<input data-inline-label type="text" value="${escapeText(action?.label ?? "")}"></label>
|
|
929
|
+
<label>Symbol<input data-inline-icon-text type="text" value="${escapeText(icon.text)}"></label>
|
|
930
|
+
<label>CSS class<input data-inline-icon-class type="text" value="${escapeText(icon.className)}"></label>
|
|
931
|
+
<label>Position<select data-inline-position>
|
|
932
|
+
<option value="left"${action?.position === "left" ? " selected" : ""}>Before text</option>
|
|
933
|
+
<option value="right"${action?.position !== "left" ? " selected" : ""}>After text</option>
|
|
934
|
+
</select></label>
|
|
935
|
+
<label><input data-inline-disabled type="checkbox"${action?.disabled ? " checked" : ""}> Disabled</label>
|
|
936
|
+
<label><input data-inline-close type="checkbox"${action?.closeOnActivate ? " checked" : ""}> Close menu on activation</label>
|
|
937
|
+
<button type="button" data-remove-inline-action>Remove icon action</button>`;
|
|
938
|
+
inlineActionsHost.appendChild(row);
|
|
939
|
+
}
|
|
940
|
+
function readInlineActions() {
|
|
941
|
+
const actions = [];
|
|
942
|
+
for (const row of inlineActionsHost.querySelectorAll("[data-inline-action]")) {
|
|
943
|
+
const name = query(row, "[data-inline-name]");
|
|
944
|
+
const label = query(row, "[data-inline-label]");
|
|
945
|
+
const iconText = query(row, "[data-inline-icon-text]");
|
|
946
|
+
const iconClass = query(row, "[data-inline-icon-class]");
|
|
947
|
+
const position = query(row, "[data-inline-position]");
|
|
948
|
+
const disabled = query(row, "[data-inline-disabled]");
|
|
949
|
+
const close = query(row, "[data-inline-close]");
|
|
950
|
+
if (!(name instanceof HTMLInputElement && label instanceof HTMLInputElement && iconText instanceof HTMLInputElement && iconClass instanceof HTMLInputElement && position instanceof HTMLSelectElement && disabled instanceof HTMLInputElement && close instanceof HTMLInputElement)) throw new TypeError("Missing inline action controls");
|
|
951
|
+
const icon = writeIcon(iconText.value, iconClass.value);
|
|
952
|
+
for (const [input, valid] of [
|
|
953
|
+
[name, Boolean(name.value.trim())],
|
|
954
|
+
[label, Boolean(label.value.trim())]
|
|
955
|
+
]) {
|
|
956
|
+
input.setCustomValidity(valid ? "" : "This field is required.");
|
|
957
|
+
if (!valid) {
|
|
958
|
+
input.reportValidity();
|
|
959
|
+
return void 0;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
if (icon === void 0) {
|
|
963
|
+
iconText.setCustomValidity("Enter a symbol or CSS class.");
|
|
964
|
+
iconText.reportValidity();
|
|
965
|
+
return void 0;
|
|
966
|
+
}
|
|
967
|
+
actions.push({
|
|
968
|
+
name: name.value.trim(),
|
|
969
|
+
label: label.value.trim(),
|
|
970
|
+
icon,
|
|
971
|
+
...position.value === "left" ? { position: "left" } : {},
|
|
972
|
+
...disabled.checked ? { disabled: true } : {},
|
|
973
|
+
...close.checked ? { closeOnActivate: true } : {}
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
return actions;
|
|
977
|
+
}
|
|
978
|
+
function syncBadgeFields(enabled) {
|
|
979
|
+
const fields = query(container, "[data-badge-fields]");
|
|
980
|
+
if (!(fields instanceof HTMLFieldSetElement)) {
|
|
981
|
+
throw new TypeError("Missing badge controls");
|
|
982
|
+
}
|
|
983
|
+
fields.disabled = !enabled;
|
|
984
|
+
}
|
|
985
|
+
function readBadge(data, existing) {
|
|
986
|
+
if (data.get("badgeEnabled") !== "on") return void 0;
|
|
987
|
+
const text = String(data.get("badgeText") ?? "").trim();
|
|
988
|
+
const color = String(data.get("badgeColor") ?? "").trim();
|
|
989
|
+
const backgroundColor = String(data.get("badgeBackgroundColor") ?? "").trim();
|
|
990
|
+
const ariaLabel = String(data.get("badgeAriaLabel") ?? "").trim();
|
|
991
|
+
const originalText = existing?.text;
|
|
992
|
+
return {
|
|
993
|
+
...text ? { text: originalText !== void 0 && String(originalText) === text ? originalText : text } : {},
|
|
994
|
+
...color ? { color } : {},
|
|
995
|
+
...backgroundColor ? { backgroundColor } : {},
|
|
996
|
+
...ariaLabel ? { ariaLabel } : {}
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
function validateBadgeColors() {
|
|
1000
|
+
for (const name of ["badgeColor", "badgeBackgroundColor"]) {
|
|
1001
|
+
const input = form.elements.namedItem(name);
|
|
1002
|
+
if (!(input instanceof HTMLInputElement)) throw new TypeError(`Missing ${name}`);
|
|
1003
|
+
const value = input.value.trim();
|
|
1004
|
+
const valid = !value || CSS.supports("color", value);
|
|
1005
|
+
input.setCustomValidity(valid ? "" : "Enter a valid CSS color.");
|
|
1006
|
+
if (!valid) {
|
|
1007
|
+
input.reportValidity();
|
|
1008
|
+
return false;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return true;
|
|
1012
|
+
}
|
|
1013
|
+
function syncEditorTypeFields(type) {
|
|
1014
|
+
const heading = type === "heading";
|
|
1015
|
+
for (const field of root.querySelectorAll("[data-item-field]")) {
|
|
1016
|
+
if (field instanceof HTMLElement) field.hidden = heading;
|
|
1017
|
+
}
|
|
1018
|
+
const checkedField = query(container, "[data-checked-field]");
|
|
1019
|
+
const fileInputField = query(container, "[data-file-input-field]");
|
|
1020
|
+
const radioGroupField = query(container, "[data-radio-group-field]");
|
|
1021
|
+
const inlineField = query(container, "[data-inline-field]");
|
|
1022
|
+
if (!(inlineField instanceof HTMLFieldSetElement)) throw new TypeError("Missing inline action controls");
|
|
1023
|
+
inlineField.disabled = heading;
|
|
1024
|
+
checkedField.hidden = type === "action";
|
|
1025
|
+
fileInputField.hidden = type !== "action";
|
|
1026
|
+
radioGroupField.hidden = type !== "radio";
|
|
1027
|
+
}
|
|
1028
|
+
form.addEventListener("submit", (event) => {
|
|
1029
|
+
const submitter = event.submitter;
|
|
1030
|
+
const value = submitter instanceof HTMLButtonElement ? submitter.value : "cancel";
|
|
1031
|
+
if (value !== "save") {
|
|
1032
|
+
editingId = null;
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
event.preventDefault();
|
|
1036
|
+
const data = new FormData(form);
|
|
1037
|
+
const itemType = String(data.get("itemType") ?? "action");
|
|
1038
|
+
if (itemType !== "heading" && data.get("badgeEnabled") === "on" && !validateBadgeColors()) return;
|
|
1039
|
+
const inlineActions = itemType === "heading" ? [] : readInlineActions();
|
|
1040
|
+
if (!inlineActions) return;
|
|
1041
|
+
const label = String(data.get("label") ?? "").trim() || "Untitled";
|
|
1042
|
+
const icon = writeIcon(
|
|
1043
|
+
String(data.get("iconText") ?? ""),
|
|
1044
|
+
String(data.get("iconClass") ?? "")
|
|
1045
|
+
);
|
|
1046
|
+
const shortcut = String(data.get("shortcut") ?? "").trim();
|
|
1047
|
+
const disabled = data.get("disabled") === "on";
|
|
1048
|
+
const input = data.get("input") === "on";
|
|
1049
|
+
const checked = data.get("checked") === "on";
|
|
1050
|
+
const radioGroup = String(data.get("radioGroup") ?? "").trim() || "default";
|
|
1051
|
+
const existing = editingId ? findItem(items, editingId)?.item : void 0;
|
|
1052
|
+
const badge = readBadge(data, existing && "badge" in existing ? existing.badge : void 0);
|
|
1053
|
+
const id = existing?.id ?? emptyItem(label).id;
|
|
1054
|
+
const base = {
|
|
1055
|
+
id,
|
|
1056
|
+
label,
|
|
1057
|
+
...icon !== void 0 ? { icon } : {},
|
|
1058
|
+
...disabled ? { disabled: true } : {},
|
|
1059
|
+
...shortcut ? { shortcut } : {},
|
|
1060
|
+
...badge !== void 0 ? { badge } : {},
|
|
1061
|
+
...inlineActions.length > 0 ? { inlineActions } : {}
|
|
1062
|
+
};
|
|
1063
|
+
let updated;
|
|
1064
|
+
if (itemType === "heading") {
|
|
1065
|
+
updated = { type: "heading", id, label };
|
|
1066
|
+
} else if (itemType === "checkbox") {
|
|
1067
|
+
updated = { ...base, type: "checkbox", checked };
|
|
1068
|
+
} else if (itemType === "radio") {
|
|
1069
|
+
updated = { ...base, type: "radio", checked, radioGroup };
|
|
1070
|
+
} else {
|
|
1071
|
+
updated = {
|
|
1072
|
+
...base,
|
|
1073
|
+
...input ? { input: true } : {},
|
|
1074
|
+
...existing && existing.type === void 0 && existing.subMenu ? { subMenu: existing.subMenu } : {}
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
if (editingId) {
|
|
1078
|
+
const found = findItem(items, editingId);
|
|
1079
|
+
if (found) found.list[found.index] = updated;
|
|
1080
|
+
} else {
|
|
1081
|
+
items.push(updated);
|
|
1082
|
+
}
|
|
1083
|
+
editingId = null;
|
|
1084
|
+
dialog.close();
|
|
1085
|
+
redraw();
|
|
1086
|
+
});
|
|
1087
|
+
deleteBtn.addEventListener("click", () => {
|
|
1088
|
+
if (!editingId) return;
|
|
1089
|
+
removeItem(items, editingId);
|
|
1090
|
+
editingId = null;
|
|
1091
|
+
dialog.close();
|
|
1092
|
+
redraw();
|
|
1093
|
+
});
|
|
1094
|
+
root.addEventListener("change", (event) => {
|
|
1095
|
+
const target = event.target;
|
|
1096
|
+
if (!(target instanceof HTMLInputElement || target instanceof HTMLSelectElement)) return;
|
|
1097
|
+
if (target instanceof HTMLInputElement && target.dataset.colorFor) {
|
|
1098
|
+
const input = target.parentElement?.querySelector('input[type="text"]');
|
|
1099
|
+
if (!input) throw new TypeError(`Missing ${target.dataset.colorFor}`);
|
|
1100
|
+
input.value = target.value;
|
|
1101
|
+
input.setCustomValidity("");
|
|
1102
|
+
if (styleControls.contains(target)) updateItemStyle();
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
if (target instanceof HTMLInputElement && target.parentElement?.classList.contains("jscmb-color-control")) {
|
|
1106
|
+
syncColorPicker(target);
|
|
1107
|
+
}
|
|
1108
|
+
if (target === wrapLabelsInput) {
|
|
1109
|
+
setWrapLabels(wrapLabelsInput.checked);
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
if (target === closeOnMouseLeaveInput) {
|
|
1113
|
+
setCloseOnMouseLeave(closeOnMouseLeaveInput.checked);
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
if (target === radiusInput) {
|
|
1117
|
+
const value = radiusInput.value.trim();
|
|
1118
|
+
if (value && !CSS.supports("border-radius", value)) {
|
|
1119
|
+
radiusInput.setCustomValidity("Enter a valid CSS border radius.");
|
|
1120
|
+
radiusInput.reportValidity();
|
|
1121
|
+
} else {
|
|
1122
|
+
setRadius(value || void 0);
|
|
1123
|
+
}
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
if (target instanceof HTMLInputElement && target.name === "badgeEnabled") {
|
|
1127
|
+
syncBadgeFields(target.checked);
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
if (styleControls.contains(target)) {
|
|
1131
|
+
updateItemStyle();
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
if (target.name === "colorScheme") {
|
|
1135
|
+
setColorScheme(normalizeScheme(target.value));
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
if (target.name === "itemType") {
|
|
1139
|
+
syncEditorTypeFields(target.value);
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
if (target.name === "itemType") {
|
|
1143
|
+
syncEditorTypeFields(target.value);
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
if (target === themeSelect) {
|
|
1147
|
+
setTheme(themeSelect.value);
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (target === sizeSelect) setSize(normalizeSize(sizeSelect.value));
|
|
1151
|
+
});
|
|
1152
|
+
form.addEventListener("input", (event) => {
|
|
1153
|
+
const target = event.target;
|
|
1154
|
+
if (target instanceof HTMLInputElement && (target.name === "badgeColor" || target.name === "badgeBackgroundColor")) {
|
|
1155
|
+
target.setCustomValidity("");
|
|
1156
|
+
}
|
|
1157
|
+
if (target instanceof HTMLInputElement && target.closest("[data-inline-action]")) {
|
|
1158
|
+
for (const input of target.closest("[data-inline-action]")?.querySelectorAll("input") ?? []) {
|
|
1159
|
+
input.setCustomValidity("");
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
});
|
|
1163
|
+
radiusInput.addEventListener("input", () => radiusInput.setCustomValidity(""));
|
|
1164
|
+
container.addEventListener("click", (event) => {
|
|
1165
|
+
if (!(event.target instanceof Element)) return;
|
|
1166
|
+
if (event.target.closest("[data-add-inline-action]")) {
|
|
1167
|
+
addInlineAction();
|
|
1168
|
+
return;
|
|
1169
|
+
}
|
|
1170
|
+
const removeInlineAction = event.target.closest("[data-remove-inline-action]");
|
|
1171
|
+
if (removeInlineAction) {
|
|
1172
|
+
removeInlineAction.closest("[data-inline-action]")?.remove();
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
const addRoot = event.target.closest("[data-add-root]");
|
|
1176
|
+
if (addRoot) {
|
|
1177
|
+
openEditor(null);
|
|
1178
|
+
return;
|
|
1179
|
+
}
|
|
1180
|
+
const addHeading = event.target.closest("[data-add-heading]");
|
|
1181
|
+
if (addHeading instanceof HTMLElement && addHeading.dataset.addHeading) {
|
|
1182
|
+
const owner = addHeading.dataset.addHeading;
|
|
1183
|
+
const heading = emptyHeading();
|
|
1184
|
+
if (owner === "root") {
|
|
1185
|
+
items.push(heading);
|
|
1186
|
+
} else {
|
|
1187
|
+
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);
|
|
1191
|
+
}
|
|
1192
|
+
redraw();
|
|
1193
|
+
openEditor(heading);
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
const edit = event.target.closest("[data-edit]");
|
|
1197
|
+
if (edit instanceof HTMLElement) {
|
|
1198
|
+
const found = findItem(items, edit.dataset.edit);
|
|
1199
|
+
if (found && found.item.type !== "separator") openEditor(found.item);
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
const addChild = event.target.closest("[data-add-child]");
|
|
1203
|
+
if (addChild instanceof HTMLElement) {
|
|
1204
|
+
const found = findItem(items, addChild.dataset.addChild);
|
|
1205
|
+
if (!found || found.item.type !== void 0) return;
|
|
1206
|
+
if (!found.item.subMenu) found.item.subMenu = [];
|
|
1207
|
+
const child = emptyItem("Sub-action");
|
|
1208
|
+
found.item.subMenu.push(child);
|
|
1209
|
+
redraw();
|
|
1210
|
+
openEditor(child);
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
const addSeparator = event.target.closest("[data-add-separator]");
|
|
1214
|
+
if (addSeparator instanceof HTMLElement && addSeparator.dataset.addSeparator) {
|
|
1215
|
+
const owner = addSeparator.dataset.addSeparator;
|
|
1216
|
+
const separator = emptySeparator();
|
|
1217
|
+
if (owner === "root") {
|
|
1218
|
+
items.push(separator);
|
|
1219
|
+
} else {
|
|
1220
|
+
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);
|
|
1224
|
+
}
|
|
1225
|
+
redraw();
|
|
1226
|
+
return;
|
|
1227
|
+
}
|
|
1228
|
+
const up = event.target.closest("[data-up]");
|
|
1229
|
+
if (up instanceof HTMLElement && up.dataset.up) {
|
|
1230
|
+
moveItem(items, up.dataset.up, -1);
|
|
1231
|
+
redraw();
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
const down = event.target.closest("[data-down]");
|
|
1235
|
+
if (down instanceof HTMLElement && down.dataset.down) {
|
|
1236
|
+
moveItem(items, down.dataset.down, 1);
|
|
1237
|
+
redraw();
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
const remove = event.target.closest("[data-remove]");
|
|
1241
|
+
if (remove instanceof HTMLElement && remove.dataset.remove) {
|
|
1242
|
+
removeItem(items, remove.dataset.remove);
|
|
1243
|
+
redraw();
|
|
1244
|
+
}
|
|
1245
|
+
});
|
|
1246
|
+
applyDocumentScheme();
|
|
1247
|
+
redraw();
|
|
1248
|
+
return {
|
|
1249
|
+
getItems: () => toPublic(items),
|
|
1250
|
+
getItemStyle: () => ({ ...itemStyle }),
|
|
1251
|
+
getRadius: () => radius,
|
|
1252
|
+
getWrapLabels: () => wrapLabels,
|
|
1253
|
+
getCloseOnMouseLeave: () => closeOnMouseLeave,
|
|
1254
|
+
setItems(next) {
|
|
1255
|
+
items = cloneItems(next);
|
|
1256
|
+
redraw();
|
|
1257
|
+
},
|
|
1258
|
+
setTheme,
|
|
1259
|
+
setSize,
|
|
1260
|
+
setRadius,
|
|
1261
|
+
setColorScheme,
|
|
1262
|
+
setWrapLabels,
|
|
1263
|
+
setCloseOnMouseLeave,
|
|
1264
|
+
destroy() {
|
|
1265
|
+
darkPreference.removeEventListener("change", onSystemColorSchemeChange);
|
|
1266
|
+
menu?.destroy();
|
|
1267
|
+
menu = null;
|
|
1268
|
+
container.innerHTML = "";
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
return __toCommonJS(index_exports);
|
|
1273
|
+
})();
|