@worm-print-platform/plugin-kit 1.5.3 → 1.6.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.
@@ -0,0 +1,349 @@
1
+ import {
2
+ h,
3
+ render
4
+ } from "./chunk-JABNJAZR.js";
5
+
6
+ // src/color-picker.ts
7
+ import { PRESET_COLORS } from "@worm-print-platform/core/designer";
8
+ var active = null;
9
+ function parseHex(color) {
10
+ const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());
11
+ if (!match) return null;
12
+ const hex = match[1];
13
+ const full = hex.length === 3 ? hex.split("").map((char) => char + char).join("") : hex;
14
+ return {
15
+ r: Number.parseInt(full.slice(0, 2), 16),
16
+ g: Number.parseInt(full.slice(2, 4), 16),
17
+ b: Number.parseInt(full.slice(4, 6), 16)
18
+ };
19
+ }
20
+ function normalizeHex(color) {
21
+ const rgb = parseHex(color);
22
+ return rgb ? rgbToHex(rgb) : "#000000";
23
+ }
24
+ function rgbToHex({ r, g, b }) {
25
+ return `#${[r, g, b].map((part) => Math.round(clamp01(part / 255) * 255).toString(16).padStart(2, "0")).join("")}`;
26
+ }
27
+ function rgbToHsv(r, g, b) {
28
+ const red = r / 255;
29
+ const green = g / 255;
30
+ const blue = b / 255;
31
+ const max = Math.max(red, green, blue);
32
+ const min = Math.min(red, green, blue);
33
+ const delta = max - min;
34
+ let h_ = 0;
35
+ if (delta !== 0) {
36
+ if (max === red) h_ = (green - blue) / delta % 6;
37
+ else if (max === green) h_ = (blue - red) / delta + 2;
38
+ else h_ = (red - green) / delta + 4;
39
+ h_ *= 60;
40
+ if (h_ < 0) h_ += 360;
41
+ }
42
+ return { h: h_, s: max === 0 ? 0 : delta / max, v: max };
43
+ }
44
+ function hsvToRgb(h_, s, v) {
45
+ const c = v * s;
46
+ const hh = (h_ % 360 + 360) % 360 / 60;
47
+ const x = c * (1 - Math.abs(hh % 2 - 1));
48
+ const [r1, g1, b1] = hh < 1 ? [c, x, 0] : hh < 2 ? [x, c, 0] : hh < 3 ? [0, c, x] : hh < 4 ? [0, x, c] : hh < 5 ? [x, 0, c] : [c, 0, x];
49
+ const m = v - c;
50
+ return { r: (r1 + m) * 255, g: (g1 + m) * 255, b: (b1 + m) * 255 };
51
+ }
52
+ function clampDegree(degree) {
53
+ return ((Number.isFinite(degree) ? degree : 0) % 360 + 360) % 360;
54
+ }
55
+ function clamp01(number) {
56
+ return Math.min(1, Math.max(0, Number.isFinite(number) ? number : 0));
57
+ }
58
+ function colorPicker(opts) {
59
+ const title = opts.title ?? "\u989C\u8272\u9009\u62E9";
60
+ return h("button", {
61
+ type: "button",
62
+ class: `preset-color-trigger${opts.triggerClass ? ` ${opts.triggerClass}` : ""}`,
63
+ "aria-expanded": "false",
64
+ "aria-haspopup": "dialog",
65
+ title,
66
+ onClick: (ev) => {
67
+ const btn = ev.currentTarget ?? ev.target;
68
+ const root = btn.getRootNode();
69
+ const sameField = !!active && active.root === root && active.title === title;
70
+ if (sameField && active.trigger === btn) {
71
+ closeColorPicker();
72
+ return;
73
+ }
74
+ closeColorPicker();
75
+ openPanel(btn, opts);
76
+ }
77
+ }, [
78
+ h("span", {
79
+ class: "preset-color-swatch",
80
+ style: opts.value ? { background: opts.value } : { background: "transparent" }
81
+ })
82
+ ]);
83
+ }
84
+ function closeColorPicker() {
85
+ const inst = active;
86
+ if (!inst) return;
87
+ active = null;
88
+ inst.teardowns.forEach((off) => off());
89
+ inst.trigger.setAttribute("aria-expanded", "false");
90
+ inst.panel.remove();
91
+ }
92
+ function openPanel(trigger, opts) {
93
+ const root = trigger.getRootNode();
94
+ const mount = root instanceof ShadowRoot ? root : document.body;
95
+ const inst = {
96
+ root,
97
+ title: opts.title ?? "\u989C\u8272\u9009\u62E9",
98
+ trigger,
99
+ panel: null,
100
+ clearable: opts.clearable !== false,
101
+ onChange: opts.onChange,
102
+ model: opts.value || void 0,
103
+ hue: 0,
104
+ saturation: 0,
105
+ value_: 0,
106
+ hexText: "",
107
+ dragging: false,
108
+ current: null,
109
+ hex: null,
110
+ area: null,
111
+ handle: null,
112
+ hueRange: null,
113
+ clearBtn: null,
114
+ items: [],
115
+ teardowns: []
116
+ };
117
+ if (opts.value && syncFromColor(inst, opts.value)) inst.hexText = normalizeHex(opts.value).toUpperCase();
118
+ else inst.hexText = opts.value ? String(opts.value) : "";
119
+ inst.panel = render(buildPanel(inst), mount);
120
+ inst.current = inst.panel.querySelector(".picker-current");
121
+ inst.hex = inst.panel.querySelector(".picker-hex");
122
+ inst.area = inst.panel.querySelector(".picker-area");
123
+ inst.handle = inst.panel.querySelector(".picker-handle");
124
+ inst.hueRange = inst.panel.querySelector(".picker-hue");
125
+ inst.clearBtn = inst.panel.querySelector(".picker-clear");
126
+ inst.items = Array.from(inst.panel.querySelectorAll(".preset-color-item")).map((el, i) => ({ el, color: PRESET_COLORS[i] }));
127
+ refresh(inst);
128
+ positionPanel(inst);
129
+ bindCloseEvents(inst);
130
+ active = inst;
131
+ trigger.setAttribute("aria-expanded", "true");
132
+ }
133
+ function positionPanel(inst) {
134
+ const rect = inst.trigger.getBoundingClientRect();
135
+ const panelWidth = 190;
136
+ const panelHeight = inst.panel.offsetHeight || 216;
137
+ const margin = 8;
138
+ const left = Math.max(margin, Math.min(rect.left, window.innerWidth - panelWidth - margin));
139
+ const below = rect.bottom + 6;
140
+ const top = below + panelHeight > window.innerHeight - margin ? Math.max(margin, rect.top - panelHeight - 6) : below;
141
+ inst.panel.style.left = `${left}px`;
142
+ inst.panel.style.top = `${top}px`;
143
+ }
144
+ function bindCloseEvents(inst) {
145
+ const onPointerDown = (ev) => {
146
+ const path = pathOf(ev);
147
+ if (path.includes(inst.trigger) || path.includes(inst.panel)) return;
148
+ closeColorPicker();
149
+ };
150
+ const onKeydown = (ev) => {
151
+ if (ev.key === "Escape") closeColorPicker();
152
+ };
153
+ const onViewportChange = () => closeColorPicker();
154
+ document.addEventListener("pointerdown", onPointerDown, true);
155
+ document.addEventListener("keydown", onKeydown);
156
+ window.addEventListener("scroll", onViewportChange, true);
157
+ window.addEventListener("resize", onViewportChange);
158
+ inst.root.addEventListener("scroll", onViewportChange, true);
159
+ inst.teardowns.push(
160
+ () => document.removeEventListener("pointerdown", onPointerDown, true),
161
+ () => document.removeEventListener("keydown", onKeydown),
162
+ () => window.removeEventListener("scroll", onViewportChange, true),
163
+ () => window.removeEventListener("resize", onViewportChange),
164
+ () => inst.root.removeEventListener("scroll", onViewportChange, true)
165
+ );
166
+ }
167
+ function pathOf(ev) {
168
+ const composed = typeof ev.composedPath === "function" ? ev.composedPath() : [];
169
+ return composed.length ? composed : [ev.target];
170
+ }
171
+ function buildPanel(inst) {
172
+ return h("div", {
173
+ class: "preset-color-panel",
174
+ role: "dialog",
175
+ "aria-label": "\u989C\u8272\u9009\u62E9",
176
+ style: { position: "fixed" },
177
+ onPointerdown: (ev) => ev.stopPropagation()
178
+ }, [
179
+ h("div", { class: "picker-preview" }, [
180
+ h("span", { class: "picker-current" }),
181
+ h("input", {
182
+ class: "picker-hex",
183
+ type: "text",
184
+ maxlength: 7,
185
+ spellcheck: "false",
186
+ "aria-label": "\u989C\u8272\u503C",
187
+ onInput: (ev) => onHexInput(inst, ev.target.value)
188
+ })
189
+ ]),
190
+ h("div", {
191
+ class: "picker-area",
192
+ "aria-label": "\u81EA\u5B9A\u4E49\u989C\u8272",
193
+ tabindex: 0,
194
+ onPointerdown: (ev) => startAreaDrag(inst, ev),
195
+ onKeydown: (ev) => onAreaKeydown(inst, ev)
196
+ }, [h("span", { class: "picker-handle" })]),
197
+ h("input", {
198
+ class: "picker-hue",
199
+ type: "range",
200
+ min: 0,
201
+ max: 360,
202
+ step: 1,
203
+ "aria-label": "\u8272\u76F8",
204
+ onInput: (ev) => onHueInput(inst, Number(ev.target.value)),
205
+ onChange: () => commitHsv(inst)
206
+ }),
207
+ h("div", { class: "picker-heading" }, [
208
+ h("span", { text: "\u5E38\u7528\u989C\u8272" }),
209
+ inst.clearable ? h("button", {
210
+ type: "button",
211
+ class: "picker-clear",
212
+ title: "\u6E05\u9664\u9009\u4E2D\u7684\u989C\u8272",
213
+ onClick: () => commitModel(inst, void 0)
214
+ }, [
215
+ h("svg", { viewBox: "0 0 16 16", width: 12, height: 12, "aria-hidden": "true" }, [
216
+ h("path", {
217
+ d: "M3 3l10 10M13 3L3 13",
218
+ stroke: "currentColor",
219
+ "stroke-width": "1.5",
220
+ "stroke-linecap": "round",
221
+ fill: "none"
222
+ })
223
+ ]),
224
+ "\u6E05\u9664"
225
+ ]) : ""
226
+ ].filter(Boolean)),
227
+ h("div", { class: "preset-color-grid" }, PRESET_COLORS.map((color) => h("button", {
228
+ type: "button",
229
+ class: "preset-color-item",
230
+ style: { background: color },
231
+ title: color,
232
+ "aria-label": color,
233
+ onClick: () => selectColor(inst, color)
234
+ })))
235
+ ]);
236
+ }
237
+ function activeColor(inst) {
238
+ return rgbToHex(hsvToRgb(inst.hue, inst.saturation, inst.value_));
239
+ }
240
+ function refresh(inst) {
241
+ const color = activeColor(inst);
242
+ if (inst.hex.value !== inst.hexText) inst.hex.value = inst.hexText;
243
+ inst.current.className = inst.model ? "picker-current" : "picker-current is-empty";
244
+ if (inst.model) inst.current.style.background = color;
245
+ else inst.current.style.removeProperty("background");
246
+ inst.area.style.setProperty("--picker-hue", `${Math.round(inst.hue)}deg`);
247
+ inst.handle.style.left = `${inst.saturation * 100}%`;
248
+ inst.handle.style.top = `${(1 - inst.value_) * 100}%`;
249
+ inst.handle.style.background = color;
250
+ const hueText = String(Math.round(inst.hue));
251
+ if (inst.hueRange.value !== hueText) inst.hueRange.value = hueText;
252
+ if (inst.clearBtn) inst.clearBtn.disabled = !inst.model;
253
+ for (const item of inst.items) {
254
+ item.el.className = inst.model && item.color.toLowerCase() === inst.model.toLowerCase() ? "preset-color-item active" : "preset-color-item";
255
+ }
256
+ }
257
+ function syncFromColor(inst, color) {
258
+ const rgb = parseHex(color);
259
+ if (!rgb) return false;
260
+ const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
261
+ inst.hue = hsv.h;
262
+ inst.saturation = hsv.s;
263
+ inst.value_ = hsv.v;
264
+ inst.hexText = normalizeHex(color).toUpperCase();
265
+ return true;
266
+ }
267
+ function setHsvState(inst, h_, s, v) {
268
+ inst.hue = clampDegree(h_);
269
+ inst.saturation = clamp01(s);
270
+ inst.value_ = clamp01(v);
271
+ inst.hexText = activeColor(inst).toUpperCase();
272
+ refresh(inst);
273
+ }
274
+ function commitHsv(inst) {
275
+ const color = activeColor(inst);
276
+ inst.model = color;
277
+ refresh(inst);
278
+ inst.onChange(color);
279
+ }
280
+ function commitModel(inst, color) {
281
+ inst.model = color;
282
+ if (!color) inst.hexText = "";
283
+ refresh(inst);
284
+ inst.onChange(color);
285
+ }
286
+ function selectColor(inst, color) {
287
+ syncFromColor(inst, color);
288
+ commitModel(inst, color);
289
+ }
290
+ function onHexInput(inst, raw) {
291
+ const color = raw.trim();
292
+ if (!color) {
293
+ if (inst.clearable) commitModel(inst, void 0);
294
+ return;
295
+ }
296
+ if (!parseHex(color)) return;
297
+ syncFromColor(inst, color);
298
+ commitModel(inst, normalizeHex(color));
299
+ }
300
+ function startAreaDrag(inst, ev) {
301
+ inst.dragging = true;
302
+ setAreaFromEvent(inst, ev);
303
+ const onMove = (m) => {
304
+ if (inst.dragging) setAreaFromEvent(inst, m);
305
+ };
306
+ const onUp = () => {
307
+ if (!inst.dragging) return;
308
+ inst.dragging = false;
309
+ document.removeEventListener("pointermove", onMove);
310
+ document.removeEventListener("pointerup", onUp);
311
+ document.removeEventListener("pointercancel", onUp);
312
+ commitHsv(inst);
313
+ };
314
+ document.addEventListener("pointermove", onMove);
315
+ document.addEventListener("pointerup", onUp);
316
+ document.addEventListener("pointercancel", onUp);
317
+ }
318
+ function setAreaFromEvent(inst, ev) {
319
+ const rect = inst.area.getBoundingClientRect();
320
+ if (!rect.width || !rect.height) return;
321
+ const s = clamp01((ev.clientX - rect.left) / rect.width);
322
+ const v = clamp01(1 - (ev.clientY - rect.top) / rect.height);
323
+ setHsvState(inst, inst.hue, s, v);
324
+ }
325
+ function onAreaKeydown(inst, ev) {
326
+ const step = ev.shiftKey ? 0.1 : 0.02;
327
+ let handled = true;
328
+ if (ev.key === "ArrowLeft") setHsvState(inst, inst.hue, inst.saturation - step, inst.value_);
329
+ else if (ev.key === "ArrowRight") setHsvState(inst, inst.hue, inst.saturation + step, inst.value_);
330
+ else if (ev.key === "ArrowUp") setHsvState(inst, inst.hue, inst.saturation, inst.value_ + step);
331
+ else if (ev.key === "ArrowDown") setHsvState(inst, inst.hue, inst.saturation, inst.value_ - step);
332
+ else handled = false;
333
+ if (handled) {
334
+ ev.preventDefault();
335
+ commitHsv(inst);
336
+ }
337
+ }
338
+ function onHueInput(inst, degree) {
339
+ setHsvState(inst, degree, inst.saturation, inst.value_);
340
+ }
341
+ export {
342
+ closeColorPicker,
343
+ colorPicker,
344
+ hsvToRgb,
345
+ normalizeHex,
346
+ parseHex,
347
+ rgbToHex,
348
+ rgbToHsv
349
+ };
package/dist/dom.cjs ADDED
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/dom.ts
21
+ var dom_exports = {};
22
+ __export(dom_exports, {
23
+ SVG_NS_CONST: () => SVG_NS_CONST,
24
+ clear: () => clear,
25
+ h: () => h,
26
+ mm: () => mm,
27
+ render: () => render
28
+ });
29
+ module.exports = __toCommonJS(dom_exports);
30
+ function h(tag, props = {}, children = []) {
31
+ return { tag, props, children };
32
+ }
33
+ var SVG_NS = "http://www.w3.org/2000/svg";
34
+ var ENUM_BOOLEAN_ATTRS = /* @__PURE__ */ new Set(["draggable", "contenteditable", "spellcheck"]);
35
+ var PROPERTY_ONLY_BOOLS = /* @__PURE__ */ new Set(["indeterminate"]);
36
+ function isSvg(tag) {
37
+ return tag === "svg" || tag === "path" || tag === "circle" || tag === "rect" || tag === "line" || tag === "g";
38
+ }
39
+ function inSvgContext(parent) {
40
+ return parent instanceof Element && parent.namespaceURI === SVG_NS;
41
+ }
42
+ function render(node, parent) {
43
+ if (typeof node === "string") {
44
+ const t = document.createTextNode(node);
45
+ parent.appendChild(t);
46
+ return t;
47
+ }
48
+ if (node instanceof Node) {
49
+ parent.appendChild(node);
50
+ return node;
51
+ }
52
+ const { tag, props, children } = node;
53
+ const el = isSvg(tag) || inSvgContext(parent) ? document.createElementNS(SVG_NS, tag) : document.createElement(tag);
54
+ for (const [key, raw] of Object.entries(props)) {
55
+ if (raw === null || raw === void 0 || raw === false) continue;
56
+ if (key === "class") el.setAttribute("class", String(raw));
57
+ else if (key === "style") {
58
+ if (typeof raw === "string") el.style.cssText = raw;
59
+ else Object.assign(el.style, raw);
60
+ } else if (key === "html") el.innerHTML = String(raw);
61
+ else if (key === "text") el.textContent = String(raw);
62
+ else if (key.startsWith("on") && typeof raw === "function") {
63
+ el.addEventListener(key.slice(2).toLowerCase(), raw);
64
+ } else if (key === "dataset") {
65
+ for (const [dk, dv] of Object.entries(raw)) el.dataset[dk] = dv;
66
+ } else if (PROPERTY_ONLY_BOOLS.has(key)) {
67
+ el[key] = true;
68
+ } else if (typeof raw === "boolean") {
69
+ if (raw) el.setAttribute(key, ENUM_BOOLEAN_ATTRS.has(key) ? "true" : "");
70
+ } else el.setAttribute(key, String(raw));
71
+ }
72
+ for (const child of children) render(child, el);
73
+ parent.appendChild(el);
74
+ return el;
75
+ }
76
+ function clear(node) {
77
+ while (node.firstChild) node.removeChild(node.firstChild);
78
+ }
79
+ function mm(v) {
80
+ return `${Math.round(v * 100) / 100}mm`;
81
+ }
82
+ var SVG_NS_CONST = SVG_NS;
83
+ // Annotate the CommonJS export names for ESM import in node:
84
+ 0 && (module.exports = {
85
+ SVG_NS_CONST,
86
+ clear,
87
+ h,
88
+ mm,
89
+ render
90
+ });
package/dist/dom.d.cts ADDED
@@ -0,0 +1,24 @@
1
+ /*!
2
+ * @worm-print-platform/plugin-kit v1.6.0
3
+ * Copyright (c) 2026 worm <liu_long_qqcom@qq.com>
4
+ * SPDX-FileCopyrightText: 2026 worm <liu_long_qqcom@qq.com>
5
+ * SPDX-License-Identifier: MIT
6
+ */
7
+ type AttrValue = string | number | boolean | null | undefined;
8
+ interface Child {
9
+ node: Node;
10
+ }
11
+ interface El {
12
+ tag: string;
13
+ props: Record<string, unknown>;
14
+ children: unknown[];
15
+ }
16
+ /** h('div', { class, style, onClick, html, ... }, [...]) */
17
+ declare function h(tag: string, props?: Record<string, unknown>, children?: unknown[]): El;
18
+ declare function render(node: El | string | Node, parent: Element | ShadowRoot): Element | Text;
19
+ /** 清空容器(重渲染前调用) */
20
+ declare function clear(node: Element | ShadowRoot): void;
21
+ declare function mm(v: number): string;
22
+ declare const SVG_NS_CONST = "http://www.w3.org/2000/svg";
23
+
24
+ export { type AttrValue, type Child, type El, SVG_NS_CONST, clear, h, mm, render };
package/dist/dom.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /*!
2
+ * @worm-print-platform/plugin-kit v1.6.0
3
+ * Copyright (c) 2026 worm <liu_long_qqcom@qq.com>
4
+ * SPDX-FileCopyrightText: 2026 worm <liu_long_qqcom@qq.com>
5
+ * SPDX-License-Identifier: MIT
6
+ */
7
+ type AttrValue = string | number | boolean | null | undefined;
8
+ interface Child {
9
+ node: Node;
10
+ }
11
+ interface El {
12
+ tag: string;
13
+ props: Record<string, unknown>;
14
+ children: unknown[];
15
+ }
16
+ /** h('div', { class, style, onClick, html, ... }, [...]) */
17
+ declare function h(tag: string, props?: Record<string, unknown>, children?: unknown[]): El;
18
+ declare function render(node: El | string | Node, parent: Element | ShadowRoot): Element | Text;
19
+ /** 清空容器(重渲染前调用) */
20
+ declare function clear(node: Element | ShadowRoot): void;
21
+ declare function mm(v: number): string;
22
+ declare const SVG_NS_CONST = "http://www.w3.org/2000/svg";
23
+
24
+ export { type AttrValue, type Child, type El, SVG_NS_CONST, clear, h, mm, render };
package/dist/dom.js ADDED
@@ -0,0 +1,14 @@
1
+ import {
2
+ SVG_NS_CONST,
3
+ clear,
4
+ h,
5
+ mm,
6
+ render
7
+ } from "./chunk-JABNJAZR.js";
8
+ export {
9
+ SVG_NS_CONST,
10
+ clear,
11
+ h,
12
+ mm,
13
+ render
14
+ };