@worm-print-platform/plugin-kit 1.5.2 → 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.
- package/README.md +1 -1
- package/dist/chunk-JABNJAZR.js +62 -0
- package/dist/controls.cjs +425 -0
- package/dist/controls.d.cts +47 -0
- package/dist/controls.d.ts +47 -0
- package/dist/controls.js +349 -0
- package/dist/dom.cjs +90 -0
- package/dist/dom.d.cts +24 -0
- package/dist/dom.d.ts +24 -0
- package/dist/dom.js +14 -0
- package/dist/expression-editor.cjs +484 -0
- package/dist/expression-editor.d.cts +42 -0
- package/dist/expression-editor.d.ts +42 -0
- package/dist/expression-editor.js +411 -0
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +17 -2
package/README.md
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/dom.ts
|
|
2
|
+
function h(tag, props = {}, children = []) {
|
|
3
|
+
return { tag, props, children };
|
|
4
|
+
}
|
|
5
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
6
|
+
var ENUM_BOOLEAN_ATTRS = /* @__PURE__ */ new Set(["draggable", "contenteditable", "spellcheck"]);
|
|
7
|
+
var PROPERTY_ONLY_BOOLS = /* @__PURE__ */ new Set(["indeterminate"]);
|
|
8
|
+
function isSvg(tag) {
|
|
9
|
+
return tag === "svg" || tag === "path" || tag === "circle" || tag === "rect" || tag === "line" || tag === "g";
|
|
10
|
+
}
|
|
11
|
+
function inSvgContext(parent) {
|
|
12
|
+
return parent instanceof Element && parent.namespaceURI === SVG_NS;
|
|
13
|
+
}
|
|
14
|
+
function render(node, parent) {
|
|
15
|
+
if (typeof node === "string") {
|
|
16
|
+
const t = document.createTextNode(node);
|
|
17
|
+
parent.appendChild(t);
|
|
18
|
+
return t;
|
|
19
|
+
}
|
|
20
|
+
if (node instanceof Node) {
|
|
21
|
+
parent.appendChild(node);
|
|
22
|
+
return node;
|
|
23
|
+
}
|
|
24
|
+
const { tag, props, children } = node;
|
|
25
|
+
const el = isSvg(tag) || inSvgContext(parent) ? document.createElementNS(SVG_NS, tag) : document.createElement(tag);
|
|
26
|
+
for (const [key, raw] of Object.entries(props)) {
|
|
27
|
+
if (raw === null || raw === void 0 || raw === false) continue;
|
|
28
|
+
if (key === "class") el.setAttribute("class", String(raw));
|
|
29
|
+
else if (key === "style") {
|
|
30
|
+
if (typeof raw === "string") el.style.cssText = raw;
|
|
31
|
+
else Object.assign(el.style, raw);
|
|
32
|
+
} else if (key === "html") el.innerHTML = String(raw);
|
|
33
|
+
else if (key === "text") el.textContent = String(raw);
|
|
34
|
+
else if (key.startsWith("on") && typeof raw === "function") {
|
|
35
|
+
el.addEventListener(key.slice(2).toLowerCase(), raw);
|
|
36
|
+
} else if (key === "dataset") {
|
|
37
|
+
for (const [dk, dv] of Object.entries(raw)) el.dataset[dk] = dv;
|
|
38
|
+
} else if (PROPERTY_ONLY_BOOLS.has(key)) {
|
|
39
|
+
el[key] = true;
|
|
40
|
+
} else if (typeof raw === "boolean") {
|
|
41
|
+
if (raw) el.setAttribute(key, ENUM_BOOLEAN_ATTRS.has(key) ? "true" : "");
|
|
42
|
+
} else el.setAttribute(key, String(raw));
|
|
43
|
+
}
|
|
44
|
+
for (const child of children) render(child, el);
|
|
45
|
+
parent.appendChild(el);
|
|
46
|
+
return el;
|
|
47
|
+
}
|
|
48
|
+
function clear(node) {
|
|
49
|
+
while (node.firstChild) node.removeChild(node.firstChild);
|
|
50
|
+
}
|
|
51
|
+
function mm(v) {
|
|
52
|
+
return `${Math.round(v * 100) / 100}mm`;
|
|
53
|
+
}
|
|
54
|
+
var SVG_NS_CONST = SVG_NS;
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
h,
|
|
58
|
+
render,
|
|
59
|
+
clear,
|
|
60
|
+
mm,
|
|
61
|
+
SVG_NS_CONST
|
|
62
|
+
};
|
|
@@ -0,0 +1,425 @@
|
|
|
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/color-picker.ts
|
|
21
|
+
var color_picker_exports = {};
|
|
22
|
+
__export(color_picker_exports, {
|
|
23
|
+
closeColorPicker: () => closeColorPicker,
|
|
24
|
+
colorPicker: () => colorPicker,
|
|
25
|
+
hsvToRgb: () => hsvToRgb,
|
|
26
|
+
normalizeHex: () => normalizeHex,
|
|
27
|
+
parseHex: () => parseHex,
|
|
28
|
+
rgbToHex: () => rgbToHex,
|
|
29
|
+
rgbToHsv: () => rgbToHsv
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(color_picker_exports);
|
|
32
|
+
var import_designer = require("@worm-print-platform/core/designer");
|
|
33
|
+
|
|
34
|
+
// src/dom.ts
|
|
35
|
+
function h(tag, props = {}, children = []) {
|
|
36
|
+
return { tag, props, children };
|
|
37
|
+
}
|
|
38
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
39
|
+
var ENUM_BOOLEAN_ATTRS = /* @__PURE__ */ new Set(["draggable", "contenteditable", "spellcheck"]);
|
|
40
|
+
var PROPERTY_ONLY_BOOLS = /* @__PURE__ */ new Set(["indeterminate"]);
|
|
41
|
+
function isSvg(tag) {
|
|
42
|
+
return tag === "svg" || tag === "path" || tag === "circle" || tag === "rect" || tag === "line" || tag === "g";
|
|
43
|
+
}
|
|
44
|
+
function inSvgContext(parent) {
|
|
45
|
+
return parent instanceof Element && parent.namespaceURI === SVG_NS;
|
|
46
|
+
}
|
|
47
|
+
function render(node, parent) {
|
|
48
|
+
if (typeof node === "string") {
|
|
49
|
+
const t = document.createTextNode(node);
|
|
50
|
+
parent.appendChild(t);
|
|
51
|
+
return t;
|
|
52
|
+
}
|
|
53
|
+
if (node instanceof Node) {
|
|
54
|
+
parent.appendChild(node);
|
|
55
|
+
return node;
|
|
56
|
+
}
|
|
57
|
+
const { tag, props, children } = node;
|
|
58
|
+
const el = isSvg(tag) || inSvgContext(parent) ? document.createElementNS(SVG_NS, tag) : document.createElement(tag);
|
|
59
|
+
for (const [key, raw] of Object.entries(props)) {
|
|
60
|
+
if (raw === null || raw === void 0 || raw === false) continue;
|
|
61
|
+
if (key === "class") el.setAttribute("class", String(raw));
|
|
62
|
+
else if (key === "style") {
|
|
63
|
+
if (typeof raw === "string") el.style.cssText = raw;
|
|
64
|
+
else Object.assign(el.style, raw);
|
|
65
|
+
} else if (key === "html") el.innerHTML = String(raw);
|
|
66
|
+
else if (key === "text") el.textContent = String(raw);
|
|
67
|
+
else if (key.startsWith("on") && typeof raw === "function") {
|
|
68
|
+
el.addEventListener(key.slice(2).toLowerCase(), raw);
|
|
69
|
+
} else if (key === "dataset") {
|
|
70
|
+
for (const [dk, dv] of Object.entries(raw)) el.dataset[dk] = dv;
|
|
71
|
+
} else if (PROPERTY_ONLY_BOOLS.has(key)) {
|
|
72
|
+
el[key] = true;
|
|
73
|
+
} else if (typeof raw === "boolean") {
|
|
74
|
+
if (raw) el.setAttribute(key, ENUM_BOOLEAN_ATTRS.has(key) ? "true" : "");
|
|
75
|
+
} else el.setAttribute(key, String(raw));
|
|
76
|
+
}
|
|
77
|
+
for (const child of children) render(child, el);
|
|
78
|
+
parent.appendChild(el);
|
|
79
|
+
return el;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/color-picker.ts
|
|
83
|
+
var active = null;
|
|
84
|
+
function parseHex(color) {
|
|
85
|
+
const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());
|
|
86
|
+
if (!match) return null;
|
|
87
|
+
const hex = match[1];
|
|
88
|
+
const full = hex.length === 3 ? hex.split("").map((char) => char + char).join("") : hex;
|
|
89
|
+
return {
|
|
90
|
+
r: Number.parseInt(full.slice(0, 2), 16),
|
|
91
|
+
g: Number.parseInt(full.slice(2, 4), 16),
|
|
92
|
+
b: Number.parseInt(full.slice(4, 6), 16)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function normalizeHex(color) {
|
|
96
|
+
const rgb = parseHex(color);
|
|
97
|
+
return rgb ? rgbToHex(rgb) : "#000000";
|
|
98
|
+
}
|
|
99
|
+
function rgbToHex({ r, g, b }) {
|
|
100
|
+
return `#${[r, g, b].map((part) => Math.round(clamp01(part / 255) * 255).toString(16).padStart(2, "0")).join("")}`;
|
|
101
|
+
}
|
|
102
|
+
function rgbToHsv(r, g, b) {
|
|
103
|
+
const red = r / 255;
|
|
104
|
+
const green = g / 255;
|
|
105
|
+
const blue = b / 255;
|
|
106
|
+
const max = Math.max(red, green, blue);
|
|
107
|
+
const min = Math.min(red, green, blue);
|
|
108
|
+
const delta = max - min;
|
|
109
|
+
let h_ = 0;
|
|
110
|
+
if (delta !== 0) {
|
|
111
|
+
if (max === red) h_ = (green - blue) / delta % 6;
|
|
112
|
+
else if (max === green) h_ = (blue - red) / delta + 2;
|
|
113
|
+
else h_ = (red - green) / delta + 4;
|
|
114
|
+
h_ *= 60;
|
|
115
|
+
if (h_ < 0) h_ += 360;
|
|
116
|
+
}
|
|
117
|
+
return { h: h_, s: max === 0 ? 0 : delta / max, v: max };
|
|
118
|
+
}
|
|
119
|
+
function hsvToRgb(h_, s, v) {
|
|
120
|
+
const c = v * s;
|
|
121
|
+
const hh = (h_ % 360 + 360) % 360 / 60;
|
|
122
|
+
const x = c * (1 - Math.abs(hh % 2 - 1));
|
|
123
|
+
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];
|
|
124
|
+
const m = v - c;
|
|
125
|
+
return { r: (r1 + m) * 255, g: (g1 + m) * 255, b: (b1 + m) * 255 };
|
|
126
|
+
}
|
|
127
|
+
function clampDegree(degree) {
|
|
128
|
+
return ((Number.isFinite(degree) ? degree : 0) % 360 + 360) % 360;
|
|
129
|
+
}
|
|
130
|
+
function clamp01(number) {
|
|
131
|
+
return Math.min(1, Math.max(0, Number.isFinite(number) ? number : 0));
|
|
132
|
+
}
|
|
133
|
+
function colorPicker(opts) {
|
|
134
|
+
const title = opts.title ?? "\u989C\u8272\u9009\u62E9";
|
|
135
|
+
return h("button", {
|
|
136
|
+
type: "button",
|
|
137
|
+
class: `preset-color-trigger${opts.triggerClass ? ` ${opts.triggerClass}` : ""}`,
|
|
138
|
+
"aria-expanded": "false",
|
|
139
|
+
"aria-haspopup": "dialog",
|
|
140
|
+
title,
|
|
141
|
+
onClick: (ev) => {
|
|
142
|
+
const btn = ev.currentTarget ?? ev.target;
|
|
143
|
+
const root = btn.getRootNode();
|
|
144
|
+
const sameField = !!active && active.root === root && active.title === title;
|
|
145
|
+
if (sameField && active.trigger === btn) {
|
|
146
|
+
closeColorPicker();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
closeColorPicker();
|
|
150
|
+
openPanel(btn, opts);
|
|
151
|
+
}
|
|
152
|
+
}, [
|
|
153
|
+
h("span", {
|
|
154
|
+
class: "preset-color-swatch",
|
|
155
|
+
style: opts.value ? { background: opts.value } : { background: "transparent" }
|
|
156
|
+
})
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
function closeColorPicker() {
|
|
160
|
+
const inst = active;
|
|
161
|
+
if (!inst) return;
|
|
162
|
+
active = null;
|
|
163
|
+
inst.teardowns.forEach((off) => off());
|
|
164
|
+
inst.trigger.setAttribute("aria-expanded", "false");
|
|
165
|
+
inst.panel.remove();
|
|
166
|
+
}
|
|
167
|
+
function openPanel(trigger, opts) {
|
|
168
|
+
const root = trigger.getRootNode();
|
|
169
|
+
const mount = root instanceof ShadowRoot ? root : document.body;
|
|
170
|
+
const inst = {
|
|
171
|
+
root,
|
|
172
|
+
title: opts.title ?? "\u989C\u8272\u9009\u62E9",
|
|
173
|
+
trigger,
|
|
174
|
+
panel: null,
|
|
175
|
+
clearable: opts.clearable !== false,
|
|
176
|
+
onChange: opts.onChange,
|
|
177
|
+
model: opts.value || void 0,
|
|
178
|
+
hue: 0,
|
|
179
|
+
saturation: 0,
|
|
180
|
+
value_: 0,
|
|
181
|
+
hexText: "",
|
|
182
|
+
dragging: false,
|
|
183
|
+
current: null,
|
|
184
|
+
hex: null,
|
|
185
|
+
area: null,
|
|
186
|
+
handle: null,
|
|
187
|
+
hueRange: null,
|
|
188
|
+
clearBtn: null,
|
|
189
|
+
items: [],
|
|
190
|
+
teardowns: []
|
|
191
|
+
};
|
|
192
|
+
if (opts.value && syncFromColor(inst, opts.value)) inst.hexText = normalizeHex(opts.value).toUpperCase();
|
|
193
|
+
else inst.hexText = opts.value ? String(opts.value) : "";
|
|
194
|
+
inst.panel = render(buildPanel(inst), mount);
|
|
195
|
+
inst.current = inst.panel.querySelector(".picker-current");
|
|
196
|
+
inst.hex = inst.panel.querySelector(".picker-hex");
|
|
197
|
+
inst.area = inst.panel.querySelector(".picker-area");
|
|
198
|
+
inst.handle = inst.panel.querySelector(".picker-handle");
|
|
199
|
+
inst.hueRange = inst.panel.querySelector(".picker-hue");
|
|
200
|
+
inst.clearBtn = inst.panel.querySelector(".picker-clear");
|
|
201
|
+
inst.items = Array.from(inst.panel.querySelectorAll(".preset-color-item")).map((el, i) => ({ el, color: import_designer.PRESET_COLORS[i] }));
|
|
202
|
+
refresh(inst);
|
|
203
|
+
positionPanel(inst);
|
|
204
|
+
bindCloseEvents(inst);
|
|
205
|
+
active = inst;
|
|
206
|
+
trigger.setAttribute("aria-expanded", "true");
|
|
207
|
+
}
|
|
208
|
+
function positionPanel(inst) {
|
|
209
|
+
const rect = inst.trigger.getBoundingClientRect();
|
|
210
|
+
const panelWidth = 190;
|
|
211
|
+
const panelHeight = inst.panel.offsetHeight || 216;
|
|
212
|
+
const margin = 8;
|
|
213
|
+
const left = Math.max(margin, Math.min(rect.left, window.innerWidth - panelWidth - margin));
|
|
214
|
+
const below = rect.bottom + 6;
|
|
215
|
+
const top = below + panelHeight > window.innerHeight - margin ? Math.max(margin, rect.top - panelHeight - 6) : below;
|
|
216
|
+
inst.panel.style.left = `${left}px`;
|
|
217
|
+
inst.panel.style.top = `${top}px`;
|
|
218
|
+
}
|
|
219
|
+
function bindCloseEvents(inst) {
|
|
220
|
+
const onPointerDown = (ev) => {
|
|
221
|
+
const path = pathOf(ev);
|
|
222
|
+
if (path.includes(inst.trigger) || path.includes(inst.panel)) return;
|
|
223
|
+
closeColorPicker();
|
|
224
|
+
};
|
|
225
|
+
const onKeydown = (ev) => {
|
|
226
|
+
if (ev.key === "Escape") closeColorPicker();
|
|
227
|
+
};
|
|
228
|
+
const onViewportChange = () => closeColorPicker();
|
|
229
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
230
|
+
document.addEventListener("keydown", onKeydown);
|
|
231
|
+
window.addEventListener("scroll", onViewportChange, true);
|
|
232
|
+
window.addEventListener("resize", onViewportChange);
|
|
233
|
+
inst.root.addEventListener("scroll", onViewportChange, true);
|
|
234
|
+
inst.teardowns.push(
|
|
235
|
+
() => document.removeEventListener("pointerdown", onPointerDown, true),
|
|
236
|
+
() => document.removeEventListener("keydown", onKeydown),
|
|
237
|
+
() => window.removeEventListener("scroll", onViewportChange, true),
|
|
238
|
+
() => window.removeEventListener("resize", onViewportChange),
|
|
239
|
+
() => inst.root.removeEventListener("scroll", onViewportChange, true)
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
function pathOf(ev) {
|
|
243
|
+
const composed = typeof ev.composedPath === "function" ? ev.composedPath() : [];
|
|
244
|
+
return composed.length ? composed : [ev.target];
|
|
245
|
+
}
|
|
246
|
+
function buildPanel(inst) {
|
|
247
|
+
return h("div", {
|
|
248
|
+
class: "preset-color-panel",
|
|
249
|
+
role: "dialog",
|
|
250
|
+
"aria-label": "\u989C\u8272\u9009\u62E9",
|
|
251
|
+
style: { position: "fixed" },
|
|
252
|
+
onPointerdown: (ev) => ev.stopPropagation()
|
|
253
|
+
}, [
|
|
254
|
+
h("div", { class: "picker-preview" }, [
|
|
255
|
+
h("span", { class: "picker-current" }),
|
|
256
|
+
h("input", {
|
|
257
|
+
class: "picker-hex",
|
|
258
|
+
type: "text",
|
|
259
|
+
maxlength: 7,
|
|
260
|
+
spellcheck: "false",
|
|
261
|
+
"aria-label": "\u989C\u8272\u503C",
|
|
262
|
+
onInput: (ev) => onHexInput(inst, ev.target.value)
|
|
263
|
+
})
|
|
264
|
+
]),
|
|
265
|
+
h("div", {
|
|
266
|
+
class: "picker-area",
|
|
267
|
+
"aria-label": "\u81EA\u5B9A\u4E49\u989C\u8272",
|
|
268
|
+
tabindex: 0,
|
|
269
|
+
onPointerdown: (ev) => startAreaDrag(inst, ev),
|
|
270
|
+
onKeydown: (ev) => onAreaKeydown(inst, ev)
|
|
271
|
+
}, [h("span", { class: "picker-handle" })]),
|
|
272
|
+
h("input", {
|
|
273
|
+
class: "picker-hue",
|
|
274
|
+
type: "range",
|
|
275
|
+
min: 0,
|
|
276
|
+
max: 360,
|
|
277
|
+
step: 1,
|
|
278
|
+
"aria-label": "\u8272\u76F8",
|
|
279
|
+
onInput: (ev) => onHueInput(inst, Number(ev.target.value)),
|
|
280
|
+
onChange: () => commitHsv(inst)
|
|
281
|
+
}),
|
|
282
|
+
h("div", { class: "picker-heading" }, [
|
|
283
|
+
h("span", { text: "\u5E38\u7528\u989C\u8272" }),
|
|
284
|
+
inst.clearable ? h("button", {
|
|
285
|
+
type: "button",
|
|
286
|
+
class: "picker-clear",
|
|
287
|
+
title: "\u6E05\u9664\u9009\u4E2D\u7684\u989C\u8272",
|
|
288
|
+
onClick: () => commitModel(inst, void 0)
|
|
289
|
+
}, [
|
|
290
|
+
h("svg", { viewBox: "0 0 16 16", width: 12, height: 12, "aria-hidden": "true" }, [
|
|
291
|
+
h("path", {
|
|
292
|
+
d: "M3 3l10 10M13 3L3 13",
|
|
293
|
+
stroke: "currentColor",
|
|
294
|
+
"stroke-width": "1.5",
|
|
295
|
+
"stroke-linecap": "round",
|
|
296
|
+
fill: "none"
|
|
297
|
+
})
|
|
298
|
+
]),
|
|
299
|
+
"\u6E05\u9664"
|
|
300
|
+
]) : ""
|
|
301
|
+
].filter(Boolean)),
|
|
302
|
+
h("div", { class: "preset-color-grid" }, import_designer.PRESET_COLORS.map((color) => h("button", {
|
|
303
|
+
type: "button",
|
|
304
|
+
class: "preset-color-item",
|
|
305
|
+
style: { background: color },
|
|
306
|
+
title: color,
|
|
307
|
+
"aria-label": color,
|
|
308
|
+
onClick: () => selectColor(inst, color)
|
|
309
|
+
})))
|
|
310
|
+
]);
|
|
311
|
+
}
|
|
312
|
+
function activeColor(inst) {
|
|
313
|
+
return rgbToHex(hsvToRgb(inst.hue, inst.saturation, inst.value_));
|
|
314
|
+
}
|
|
315
|
+
function refresh(inst) {
|
|
316
|
+
const color = activeColor(inst);
|
|
317
|
+
if (inst.hex.value !== inst.hexText) inst.hex.value = inst.hexText;
|
|
318
|
+
inst.current.className = inst.model ? "picker-current" : "picker-current is-empty";
|
|
319
|
+
if (inst.model) inst.current.style.background = color;
|
|
320
|
+
else inst.current.style.removeProperty("background");
|
|
321
|
+
inst.area.style.setProperty("--picker-hue", `${Math.round(inst.hue)}deg`);
|
|
322
|
+
inst.handle.style.left = `${inst.saturation * 100}%`;
|
|
323
|
+
inst.handle.style.top = `${(1 - inst.value_) * 100}%`;
|
|
324
|
+
inst.handle.style.background = color;
|
|
325
|
+
const hueText = String(Math.round(inst.hue));
|
|
326
|
+
if (inst.hueRange.value !== hueText) inst.hueRange.value = hueText;
|
|
327
|
+
if (inst.clearBtn) inst.clearBtn.disabled = !inst.model;
|
|
328
|
+
for (const item of inst.items) {
|
|
329
|
+
item.el.className = inst.model && item.color.toLowerCase() === inst.model.toLowerCase() ? "preset-color-item active" : "preset-color-item";
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function syncFromColor(inst, color) {
|
|
333
|
+
const rgb = parseHex(color);
|
|
334
|
+
if (!rgb) return false;
|
|
335
|
+
const hsv = rgbToHsv(rgb.r, rgb.g, rgb.b);
|
|
336
|
+
inst.hue = hsv.h;
|
|
337
|
+
inst.saturation = hsv.s;
|
|
338
|
+
inst.value_ = hsv.v;
|
|
339
|
+
inst.hexText = normalizeHex(color).toUpperCase();
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
function setHsvState(inst, h_, s, v) {
|
|
343
|
+
inst.hue = clampDegree(h_);
|
|
344
|
+
inst.saturation = clamp01(s);
|
|
345
|
+
inst.value_ = clamp01(v);
|
|
346
|
+
inst.hexText = activeColor(inst).toUpperCase();
|
|
347
|
+
refresh(inst);
|
|
348
|
+
}
|
|
349
|
+
function commitHsv(inst) {
|
|
350
|
+
const color = activeColor(inst);
|
|
351
|
+
inst.model = color;
|
|
352
|
+
refresh(inst);
|
|
353
|
+
inst.onChange(color);
|
|
354
|
+
}
|
|
355
|
+
function commitModel(inst, color) {
|
|
356
|
+
inst.model = color;
|
|
357
|
+
if (!color) inst.hexText = "";
|
|
358
|
+
refresh(inst);
|
|
359
|
+
inst.onChange(color);
|
|
360
|
+
}
|
|
361
|
+
function selectColor(inst, color) {
|
|
362
|
+
syncFromColor(inst, color);
|
|
363
|
+
commitModel(inst, color);
|
|
364
|
+
}
|
|
365
|
+
function onHexInput(inst, raw) {
|
|
366
|
+
const color = raw.trim();
|
|
367
|
+
if (!color) {
|
|
368
|
+
if (inst.clearable) commitModel(inst, void 0);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (!parseHex(color)) return;
|
|
372
|
+
syncFromColor(inst, color);
|
|
373
|
+
commitModel(inst, normalizeHex(color));
|
|
374
|
+
}
|
|
375
|
+
function startAreaDrag(inst, ev) {
|
|
376
|
+
inst.dragging = true;
|
|
377
|
+
setAreaFromEvent(inst, ev);
|
|
378
|
+
const onMove = (m) => {
|
|
379
|
+
if (inst.dragging) setAreaFromEvent(inst, m);
|
|
380
|
+
};
|
|
381
|
+
const onUp = () => {
|
|
382
|
+
if (!inst.dragging) return;
|
|
383
|
+
inst.dragging = false;
|
|
384
|
+
document.removeEventListener("pointermove", onMove);
|
|
385
|
+
document.removeEventListener("pointerup", onUp);
|
|
386
|
+
document.removeEventListener("pointercancel", onUp);
|
|
387
|
+
commitHsv(inst);
|
|
388
|
+
};
|
|
389
|
+
document.addEventListener("pointermove", onMove);
|
|
390
|
+
document.addEventListener("pointerup", onUp);
|
|
391
|
+
document.addEventListener("pointercancel", onUp);
|
|
392
|
+
}
|
|
393
|
+
function setAreaFromEvent(inst, ev) {
|
|
394
|
+
const rect = inst.area.getBoundingClientRect();
|
|
395
|
+
if (!rect.width || !rect.height) return;
|
|
396
|
+
const s = clamp01((ev.clientX - rect.left) / rect.width);
|
|
397
|
+
const v = clamp01(1 - (ev.clientY - rect.top) / rect.height);
|
|
398
|
+
setHsvState(inst, inst.hue, s, v);
|
|
399
|
+
}
|
|
400
|
+
function onAreaKeydown(inst, ev) {
|
|
401
|
+
const step = ev.shiftKey ? 0.1 : 0.02;
|
|
402
|
+
let handled = true;
|
|
403
|
+
if (ev.key === "ArrowLeft") setHsvState(inst, inst.hue, inst.saturation - step, inst.value_);
|
|
404
|
+
else if (ev.key === "ArrowRight") setHsvState(inst, inst.hue, inst.saturation + step, inst.value_);
|
|
405
|
+
else if (ev.key === "ArrowUp") setHsvState(inst, inst.hue, inst.saturation, inst.value_ + step);
|
|
406
|
+
else if (ev.key === "ArrowDown") setHsvState(inst, inst.hue, inst.saturation, inst.value_ - step);
|
|
407
|
+
else handled = false;
|
|
408
|
+
if (handled) {
|
|
409
|
+
ev.preventDefault();
|
|
410
|
+
commitHsv(inst);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
function onHueInput(inst, degree) {
|
|
414
|
+
setHsvState(inst, degree, inst.saturation, inst.value_);
|
|
415
|
+
}
|
|
416
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
417
|
+
0 && (module.exports = {
|
|
418
|
+
closeColorPicker,
|
|
419
|
+
colorPicker,
|
|
420
|
+
hsvToRgb,
|
|
421
|
+
normalizeHex,
|
|
422
|
+
parseHex,
|
|
423
|
+
rgbToHex,
|
|
424
|
+
rgbToHsv
|
|
425
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
import { El } from './dom.cjs';
|
|
8
|
+
|
|
9
|
+
interface ColorPickerOptions {
|
|
10
|
+
/** 当前色值;空串/undefined 均视为「未设置」 */
|
|
11
|
+
value?: string;
|
|
12
|
+
/** 是否允许清除(Vue 3 轨 边框等必填场景传 false),默认 true */
|
|
13
|
+
clearable?: boolean;
|
|
14
|
+
/** 触发按钮 title(Vue 3 轨 侧为色值或「自动」,本包保留调用方传入的字段名语义) */
|
|
15
|
+
title?: string;
|
|
16
|
+
/** 追加在触发钮上的类名(Vue 3 轨 给组件根挂的 class 会与其自身类名合并,如 `page-bg-picker`) */
|
|
17
|
+
triggerClass?: string;
|
|
18
|
+
/** 选中回调;undefined 表示清除(Vue 3 轨 emit '' 的等价归一) */
|
|
19
|
+
onChange: (value: string | undefined) => void;
|
|
20
|
+
}
|
|
21
|
+
declare function parseHex(color: string): {
|
|
22
|
+
r: number;
|
|
23
|
+
g: number;
|
|
24
|
+
b: number;
|
|
25
|
+
} | null;
|
|
26
|
+
declare function normalizeHex(color: string): string;
|
|
27
|
+
declare function rgbToHex({ r, g, b }: {
|
|
28
|
+
r: number;
|
|
29
|
+
g: number;
|
|
30
|
+
b: number;
|
|
31
|
+
}): string;
|
|
32
|
+
declare function rgbToHsv(r: number, g: number, b: number): {
|
|
33
|
+
h: number;
|
|
34
|
+
s: number;
|
|
35
|
+
v: number;
|
|
36
|
+
};
|
|
37
|
+
declare function hsvToRgb(h_: number, s: number, v: number): {
|
|
38
|
+
r: number;
|
|
39
|
+
g: number;
|
|
40
|
+
b: number;
|
|
41
|
+
};
|
|
42
|
+
/** 触发按钮(对应 Vue 3 轨 的 button.preset-color-trigger);点击在挂载根上开/关面板 */
|
|
43
|
+
declare function colorPicker(opts: ColorPickerOptions): El;
|
|
44
|
+
/** 关闭当前打开的面板并解除全部监听(宿主 disconnectedCallback 亦可用) */
|
|
45
|
+
declare function closeColorPicker(): void;
|
|
46
|
+
|
|
47
|
+
export { type ColorPickerOptions, closeColorPicker, colorPicker, hsvToRgb, normalizeHex, parseHex, rgbToHex, rgbToHsv };
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
import { El } from './dom.js';
|
|
8
|
+
|
|
9
|
+
interface ColorPickerOptions {
|
|
10
|
+
/** 当前色值;空串/undefined 均视为「未设置」 */
|
|
11
|
+
value?: string;
|
|
12
|
+
/** 是否允许清除(Vue 3 轨 边框等必填场景传 false),默认 true */
|
|
13
|
+
clearable?: boolean;
|
|
14
|
+
/** 触发按钮 title(Vue 3 轨 侧为色值或「自动」,本包保留调用方传入的字段名语义) */
|
|
15
|
+
title?: string;
|
|
16
|
+
/** 追加在触发钮上的类名(Vue 3 轨 给组件根挂的 class 会与其自身类名合并,如 `page-bg-picker`) */
|
|
17
|
+
triggerClass?: string;
|
|
18
|
+
/** 选中回调;undefined 表示清除(Vue 3 轨 emit '' 的等价归一) */
|
|
19
|
+
onChange: (value: string | undefined) => void;
|
|
20
|
+
}
|
|
21
|
+
declare function parseHex(color: string): {
|
|
22
|
+
r: number;
|
|
23
|
+
g: number;
|
|
24
|
+
b: number;
|
|
25
|
+
} | null;
|
|
26
|
+
declare function normalizeHex(color: string): string;
|
|
27
|
+
declare function rgbToHex({ r, g, b }: {
|
|
28
|
+
r: number;
|
|
29
|
+
g: number;
|
|
30
|
+
b: number;
|
|
31
|
+
}): string;
|
|
32
|
+
declare function rgbToHsv(r: number, g: number, b: number): {
|
|
33
|
+
h: number;
|
|
34
|
+
s: number;
|
|
35
|
+
v: number;
|
|
36
|
+
};
|
|
37
|
+
declare function hsvToRgb(h_: number, s: number, v: number): {
|
|
38
|
+
r: number;
|
|
39
|
+
g: number;
|
|
40
|
+
b: number;
|
|
41
|
+
};
|
|
42
|
+
/** 触发按钮(对应 Vue 3 轨 的 button.preset-color-trigger);点击在挂载根上开/关面板 */
|
|
43
|
+
declare function colorPicker(opts: ColorPickerOptions): El;
|
|
44
|
+
/** 关闭当前打开的面板并解除全部监听(宿主 disconnectedCallback 亦可用) */
|
|
45
|
+
declare function closeColorPicker(): void;
|
|
46
|
+
|
|
47
|
+
export { type ColorPickerOptions, closeColorPicker, colorPicker, hsvToRgb, normalizeHex, parseHex, rgbToHex, rgbToHsv };
|