@pixerate/editor-react 0.67.0 → 0.68.1

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,893 @@
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/paint-editor/index.ts
21
+ var paint_editor_exports = {};
22
+ __export(paint_editor_exports, {
23
+ BrushPanel: () => BrushPanel,
24
+ ColorPanel: () => ColorPanel,
25
+ PaintCanvas: () => PaintCanvas,
26
+ PaintEditor: () => PaintEditor,
27
+ PaintLayersPanel: () => PaintLayersPanel,
28
+ PaintSlider: () => PaintSlider,
29
+ PaintToolbar: () => PaintToolbar,
30
+ PaperPanel: () => PaperPanel,
31
+ usePaintEditor: () => usePaintEditor,
32
+ usePaintEditorState: () => usePaintEditorState
33
+ });
34
+ module.exports = __toCommonJS(paint_editor_exports);
35
+
36
+ // src/paint-editor/usePaintEditor.ts
37
+ var import_react = require("react");
38
+ var import_paint_editor = require("@pixerate/editor/paint-editor");
39
+ function usePaintEditor(options = {}) {
40
+ const [controller] = (0, import_react.useState)(() => options instanceof import_paint_editor.PaintEditorController ? options : new import_paint_editor.PaintEditorController(options));
41
+ const state = usePaintEditorState(controller);
42
+ return (0, import_react.useMemo)(() => ({ controller, state }), [controller, state]);
43
+ }
44
+ function usePaintEditorState(controller) {
45
+ return (0, import_react.useSyncExternalStore)(
46
+ (notify) => controller.subscribe(notify),
47
+ () => controller.getState(),
48
+ () => controller.getState()
49
+ );
50
+ }
51
+
52
+ // src/paint-editor/PaintCanvas.tsx
53
+ var import_react2 = require("react");
54
+ var import_paint_editor2 = require("@pixerate/editor/paint-editor");
55
+ var import_jsx_runtime = require("react/jsx-runtime");
56
+ var PaintCanvas = ({ controller, tool = "brush", showGuides = true, fitOnMount = true, onError, className }) => {
57
+ const state = usePaintEditorState(controller);
58
+ const viewport = (0, import_react2.useRef)(null);
59
+ const canvas = (0, import_react2.useRef)(null);
60
+ const [size, setSize] = (0, import_react2.useState)({ width: 800, height: 600 });
61
+ const measured = (0, import_react2.useRef)(false);
62
+ const [predicted, setPredicted] = (0, import_react2.useState)([]);
63
+ const presenter = (0, import_react2.useMemo)(() => new import_paint_editor2.PaintCanvasPresenter(controller), [controller]);
64
+ const gesture = (0, import_react2.useMemo)(
65
+ () => new import_paint_editor2.PaintGesture(controller, () => {
66
+ const r = viewport.current?.getBoundingClientRect();
67
+ return { left: r?.left ?? 0, top: r?.top ?? 0, width: r?.width || size.width, height: r?.height || size.height };
68
+ }),
69
+ // The box reads the element when called; `size` is only the jsdom fallback.
70
+ // eslint-disable-next-line react-hooks/exhaustive-deps
71
+ [controller]
72
+ );
73
+ gesture.tool = tool;
74
+ const { canvas: doc } = state.document;
75
+ (0, import_react2.useEffect)(() => {
76
+ const el = viewport.current;
77
+ if (!el || typeof ResizeObserver === "undefined") return;
78
+ const ro = new ResizeObserver(() => {
79
+ if (!el.clientWidth || !el.clientHeight) return;
80
+ measured.current = true;
81
+ setSize({ width: el.clientWidth, height: el.clientHeight });
82
+ });
83
+ ro.observe(el);
84
+ return () => ro.disconnect();
85
+ }, []);
86
+ const fitted = (0, import_react2.useRef)(null);
87
+ (0, import_react2.useEffect)(() => {
88
+ const key = `${doc.width}x${doc.height}`;
89
+ if (!fitOnMount || fitted.current === key) return;
90
+ if (typeof ResizeObserver !== "undefined" && !measured.current) return;
91
+ fitted.current = key;
92
+ controller.setView({ zoom: (0, import_paint_editor2.fitZoom)(doc, size), pan: { x: 0, y: 0 } });
93
+ }, [controller, doc, size, fitOnMount]);
94
+ (0, import_react2.useEffect)(() => {
95
+ const ctx = canvas.current?.getContext("2d");
96
+ if (ctx && typeof ctx.createImageData === "function") presenter.render(ctx);
97
+ else controller.takeDirty();
98
+ }, [state, presenter, controller]);
99
+ (0, import_react2.useEffect)(() => {
100
+ const el = viewport.current;
101
+ if (!el) return;
102
+ const onWheel = (e) => {
103
+ e.preventDefault();
104
+ gesture.wheel(e);
105
+ };
106
+ el.addEventListener("wheel", onWheel, { passive: false });
107
+ return () => el.removeEventListener("wheel", onWheel);
108
+ }, [gesture]);
109
+ const report = () => onError?.(gesture.error);
110
+ const transform = (0, import_paint_editor2.viewCssTransform)(state.view, doc, size);
111
+ const lines = showGuides ? (0, import_paint_editor2.guideOverlay)(controller) : [];
112
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
113
+ "div",
114
+ {
115
+ ref: viewport,
116
+ tabIndex: 0,
117
+ role: "application",
118
+ "aria-label": "Paint canvas",
119
+ "data-testid": "paint-canvas",
120
+ className: `relative overflow-hidden bg-slate-950 outline-none touch-none select-none ${className ?? ""}`,
121
+ style: { cursor: tool === "pan" ? "grab" : tool === "eyedropper" ? "copy" : "crosshair" },
122
+ onPointerDown: (e) => {
123
+ viewport.current?.focus();
124
+ if (gesture.down(e.nativeEvent)) e.target.setPointerCapture?.(e.pointerId);
125
+ report();
126
+ },
127
+ onPointerMove: (e) => {
128
+ gesture.move(e.nativeEvent);
129
+ if (gesture.predicted.length || predicted.length) setPredicted(gesture.predicted.map((p) => ({ x: p.x, y: p.y })));
130
+ },
131
+ onPointerUp: (e) => {
132
+ gesture.up(e.nativeEvent);
133
+ setPredicted([]);
134
+ },
135
+ onPointerCancel: () => {
136
+ gesture.cancel();
137
+ setPredicted([]);
138
+ },
139
+ onKeyDown: (e) => {
140
+ if ((0, import_paint_editor2.handlePaintShortcut)(controller, gesture, e)) e.preventDefault();
141
+ },
142
+ children: [
143
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
144
+ "canvas",
145
+ {
146
+ ref: canvas,
147
+ width: doc.width,
148
+ height: doc.height,
149
+ className: "absolute left-0 top-0 shadow-2xl",
150
+ style: { transform, transformOrigin: "0 0", imageRendering: state.view.zoom > 2 ? "pixelated" : "auto" }
151
+ }
152
+ ),
153
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("svg", { className: "absolute left-0 top-0 pointer-events-none overflow-visible", width: doc.width, height: doc.height, style: { transform, transformOrigin: "0 0" }, "aria-hidden": "true", children: [
154
+ lines.map((l, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
155
+ "line",
156
+ {
157
+ x1: l.from.x,
158
+ y1: l.from.y,
159
+ x2: l.to.x,
160
+ y2: l.to.y,
161
+ stroke: l.kind === "symmetry" ? "#22d3ee" : "#f472b6",
162
+ strokeOpacity: l.kind === "symmetry" ? 0.8 : 0.35,
163
+ strokeWidth: 1 / state.view.zoom,
164
+ strokeDasharray: l.kind === "symmetry" ? `${6 / state.view.zoom} ${4 / state.view.zoom}` : void 0
165
+ },
166
+ i
167
+ )),
168
+ predicted.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("polyline", { points: predicted.map((p) => `${p.x},${p.y}`).join(" "), fill: "none", stroke: state.color, strokeOpacity: 0.5, strokeWidth: Math.max(1, state.brush.tip.size / 2), strokeLinecap: "round" })
169
+ ] }),
170
+ state.guides.references.filter((r) => r.visible).map((r) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ReferencePanel, { controller, id: r.id }, r.id)),
171
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "absolute bottom-1 right-2 text-[10px] text-slate-500 pointer-events-none", "data-testid": "paint-view-status", children: [
172
+ Math.round(state.view.zoom * 100),
173
+ "% \xB7 ",
174
+ Math.round(state.view.rotation),
175
+ "\xB0",
176
+ state.view.mirror ? " \xB7 mirrored" : ""
177
+ ] })
178
+ ]
179
+ }
180
+ );
181
+ };
182
+ var ReferencePanel = ({ controller, id }) => {
183
+ const state = usePaintEditorState(controller);
184
+ const ref = state.guides.references.find((r) => r.id === id);
185
+ const drag = (0, import_react2.useRef)(null);
186
+ if (!ref) return null;
187
+ const h = ref.height / ref.width * ref.displayWidth;
188
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
189
+ "div",
190
+ {
191
+ className: "absolute rounded border border-slate-700 bg-slate-900/95 shadow-xl",
192
+ style: { left: ref.x, top: ref.y, width: ref.displayWidth },
193
+ "data-testid": "paint-reference",
194
+ onPointerDown: (e) => e.stopPropagation(),
195
+ children: [
196
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
197
+ "div",
198
+ {
199
+ className: "flex items-center justify-between px-1.5 py-0.5 text-[10px] text-slate-300 cursor-move",
200
+ onPointerDown: (e) => {
201
+ drag.current = { x: e.clientX, y: e.clientY, ox: ref.x, oy: ref.y };
202
+ e.target.setPointerCapture?.(e.pointerId);
203
+ },
204
+ onPointerMove: (e) => {
205
+ const d = drag.current;
206
+ if (d) controller.updateReferenceImage(id, { x: d.ox + e.clientX - d.x, y: d.oy + e.clientY - d.y });
207
+ },
208
+ onPointerUp: () => drag.current = null,
209
+ children: [
210
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "truncate", children: ref.name }),
211
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", "aria-label": `Hide ${ref.name}`, className: "px-1 text-slate-400 hover:text-white", onClick: () => controller.updateReferenceImage(id, { visible: false }), children: "\xD7" })
212
+ ]
213
+ }
214
+ ),
215
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
216
+ "img",
217
+ {
218
+ src: ref.src,
219
+ alt: ref.name,
220
+ draggable: false,
221
+ width: ref.displayWidth,
222
+ height: h,
223
+ className: "block cursor-copy",
224
+ onClick: (e) => {
225
+ const rect = e.target.getBoundingClientRect();
226
+ const k = ref.width / (rect.width || ref.displayWidth);
227
+ controller.setColor(controller.pickReferenceColor(id, Math.min(ref.width - 1, (e.clientX - rect.left) * k), Math.min(ref.height - 1, (e.clientY - rect.top) * k)));
228
+ }
229
+ }
230
+ )
231
+ ]
232
+ }
233
+ );
234
+ };
235
+
236
+ // src/paint-editor/PaintToolbar.tsx
237
+ var import_react4 = require("react");
238
+ var import_paint_editor3 = require("@pixerate/editor/paint-editor");
239
+
240
+ // src/paint-editor/shared.tsx
241
+ var import_react3 = require("react");
242
+ var import_jsx_runtime2 = require("react/jsx-runtime");
243
+ var chip = (on) => `px-2 py-1 rounded text-[11px] ${on ? "bg-indigo-600 text-white" : "bg-slate-800 text-slate-300 hover:text-white"}`;
244
+ var button = "px-2 py-1 rounded text-[11px] bg-slate-800 text-slate-200 hover:bg-slate-700 disabled:opacity-40";
245
+ var panel = "flex flex-col gap-2 text-xs text-slate-200";
246
+ var PixelCanvas = ({ pixels, label, className, style }) => {
247
+ const ref = (0, import_react3.useRef)(null);
248
+ (0, import_react3.useEffect)(() => {
249
+ const canvas = ref.current;
250
+ const ctx = canvas?.getContext("2d");
251
+ if (!canvas || !ctx || !pixels || typeof ctx.createImageData !== "function") return;
252
+ canvas.width = pixels.width;
253
+ canvas.height = pixels.height;
254
+ const img = ctx.createImageData(pixels.width, pixels.height);
255
+ img.data.set(pixels.data);
256
+ ctx.putImageData(img, 0, 0);
257
+ }, [pixels]);
258
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("canvas", { ref, role: "img", "aria-label": label, className, style, width: pixels?.width, height: pixels?.height });
259
+ };
260
+ var fmt = (c, v) => c.step >= 1 ? String(Math.round(v)) : v.toFixed(2);
261
+ var PaintSlider = ({ control, value, onChange, onCommit }) => {
262
+ const [draft, setDraft] = (0, import_react3.useState)(null);
263
+ const ref = (0, import_react3.useRef)(null);
264
+ const commit = (0, import_react3.useRef)(onCommit);
265
+ commit.current = onCommit;
266
+ (0, import_react3.useEffect)(() => {
267
+ const el = ref.current;
268
+ if (!el) return;
269
+ const done = () => {
270
+ commit.current?.(Number(el.value));
271
+ setDraft(null);
272
+ };
273
+ el.addEventListener("change", done);
274
+ return () => el.removeEventListener("change", done);
275
+ }, []);
276
+ const shown = draft ?? value;
277
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { className: "flex flex-col gap-0.5", "data-path": control.path, children: [
278
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "flex justify-between text-slate-400", children: [
279
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: control.label }),
280
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "font-mono", children: fmt(control, shown) })
281
+ ] }),
282
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
283
+ "input",
284
+ {
285
+ ref,
286
+ type: "range",
287
+ "aria-label": control.label,
288
+ min: control.min,
289
+ max: control.max,
290
+ step: control.step,
291
+ value: shown,
292
+ onChange: (e) => {
293
+ const v = Number(e.target.value);
294
+ if (onCommit) setDraft(v);
295
+ onChange?.(v);
296
+ },
297
+ className: "w-full accent-indigo-500"
298
+ }
299
+ )
300
+ ] });
301
+ };
302
+ function useAttempt() {
303
+ const [error, setError] = (0, import_react3.useState)(null);
304
+ const attempt = (fn) => {
305
+ try {
306
+ setError(null);
307
+ const r = fn();
308
+ if (r && typeof r.catch === "function") r.catch((e) => setError(e instanceof Error ? e.message : String(e)));
309
+ } catch (e) {
310
+ setError(e instanceof Error ? e.message : String(e));
311
+ }
312
+ };
313
+ return [error, attempt, setError];
314
+ }
315
+ var ErrorLine = ({ error }) => error ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { role: "alert", className: "text-[11px] text-rose-400", children: error }) : null;
316
+ var CommitColorInput = ({ value, label, onCommit, className }) => {
317
+ const ref = (0, import_react3.useRef)(null);
318
+ const commit = (0, import_react3.useRef)(onCommit);
319
+ commit.current = onCommit;
320
+ (0, import_react3.useEffect)(() => {
321
+ const el = ref.current;
322
+ if (!el) return;
323
+ const done = () => commit.current(el.value);
324
+ el.addEventListener("change", done);
325
+ return () => el.removeEventListener("change", done);
326
+ }, []);
327
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { ref, type: "color", "aria-label": label, defaultValue: value, className }, value);
328
+ };
329
+
330
+ // src/paint-editor/PaintToolbar.tsx
331
+ var import_jsx_runtime3 = require("react/jsx-runtime");
332
+ var PaintToolbar = ({ controller, tool, onToolChange, fileName = "painting", className }) => {
333
+ const state = usePaintEditorState(controller);
334
+ const [error, attempt] = useAttempt();
335
+ const openRef = (0, import_react4.useRef)(null);
336
+ const tracingRef = (0, import_react4.useRef)(null);
337
+ const refRef = (0, import_react4.useRef)(null);
338
+ const { symmetry, perspective, tracing } = state.guides;
339
+ const { canvas } = state.document;
340
+ const exportImage = (format) => attempt(async () => (0, import_paint_editor3.downloadBytes)(await controller.exportImage({ format }), `${fileName}.${format === "image/png" ? "png" : "jpg"}`, format));
341
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: `flex flex-wrap items-center gap-1.5 text-xs text-slate-200 ${className ?? ""}`, role: "toolbar", "aria-label": "Paint toolbar", "data-testid": "paint-toolbar", children: [
342
+ import_paint_editor3.PAINT_TOOLS.map((t) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", "aria-pressed": tool === t.id, title: `${t.label} (${t.key.toUpperCase()})`, className: chip(tool === t.id), onClick: () => onToolChange(t.id), children: t.label }, t.id)),
343
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mx-1 h-4 w-px bg-slate-700" }),
344
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, disabled: !state.canUndo, title: state.undoLabel ? `Undo ${state.undoLabel}` : "Undo", onClick: () => controller.undo(), children: "Undo" }),
345
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, disabled: !state.canRedo, title: state.redoLabel ? `Redo ${state.redoLabel}` : "Redo", onClick: () => controller.redo(), children: "Redo" }),
346
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mx-1 h-4 w-px bg-slate-700" }),
347
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, "aria-label": "Rotate view left", onClick: () => controller.setView({ rotation: state.view.rotation - 15 }), children: "\u27F2" }),
348
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, "aria-label": "Rotate view right", onClick: () => controller.setView({ rotation: state.view.rotation + 15 }), children: "\u27F3" }),
349
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: chip(state.view.mirror), "aria-pressed": state.view.mirror, onClick: () => controller.setView({ mirror: !state.view.mirror }), children: "Mirror view" }),
350
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => controller.resetView(), children: "Reset view" }),
351
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mx-1 h-4 w-px bg-slate-700" }),
352
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "flex items-center gap-1", children: [
353
+ "Symmetry",
354
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
355
+ "select",
356
+ {
357
+ "aria-label": "Symmetry",
358
+ value: symmetry.mode,
359
+ onChange: (e) => controller.setSymmetry({ mode: e.target.value, center: symmetry.mode === "off" ? { x: canvas.width / 2, y: canvas.height / 2 } : symmetry.center }),
360
+ className: "rounded bg-slate-800 px-1 py-0.5",
361
+ children: import_paint_editor3.SYMMETRY_MODES.map((m) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: m, children: m }, m))
362
+ }
363
+ )
364
+ ] }),
365
+ symmetry.mode === "kaleidoscope" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
366
+ "input",
367
+ {
368
+ type: "number",
369
+ "aria-label": "Kaleidoscope segments",
370
+ min: 2,
371
+ max: 32,
372
+ value: symmetry.segments,
373
+ onChange: (e) => controller.setSymmetry({ segments: Number(e.target.value) }),
374
+ className: "w-12 rounded bg-slate-800 px-1 py-0.5"
375
+ }
376
+ ),
377
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
378
+ "button",
379
+ {
380
+ type: "button",
381
+ className: chip(perspective.enabled),
382
+ "aria-pressed": perspective.enabled,
383
+ onClick: () => attempt(
384
+ () => controller.setPerspective(
385
+ perspective.enabled ? { enabled: false } : { enabled: true, points: perspective.points.length ? perspective.points : [{ x: canvas.width / 2, y: canvas.height * 0.4 }] }
386
+ )
387
+ ),
388
+ children: "Perspective"
389
+ }
390
+ ),
391
+ perspective.enabled && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
392
+ "select",
393
+ {
394
+ "aria-label": "Vanishing points",
395
+ value: perspective.points.length,
396
+ onChange: (e) => {
397
+ const n = Number(e.target.value);
398
+ const y = canvas.height * 0.4;
399
+ const pts = n === 1 ? [{ x: canvas.width / 2, y }] : n === 2 ? [{ x: -canvas.width * 0.5, y }, { x: canvas.width * 1.5, y }] : [{ x: -canvas.width * 0.5, y }, { x: canvas.width * 1.5, y }, { x: canvas.width / 2, y: canvas.height * 3 }];
400
+ attempt(() => controller.setPerspective({ points: pts }));
401
+ },
402
+ className: "rounded bg-slate-800 px-1 py-0.5",
403
+ children: [
404
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: 1, children: "1-point" }),
405
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: 2, children: "2-point" }),
406
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: 3, children: "3-point" })
407
+ ]
408
+ }
409
+ ),
410
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mx-1 h-4 w-px bg-slate-700" }),
411
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => tracingRef.current?.click(), children: "Tracing image\u2026" }),
412
+ tracing && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
413
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
414
+ "input",
415
+ {
416
+ type: "range",
417
+ "aria-label": "Tracing opacity",
418
+ min: 0,
419
+ max: 1,
420
+ step: 0.05,
421
+ value: tracing.opacity,
422
+ onChange: (e) => controller.updateTracing({ opacity: Number(e.target.value) }),
423
+ className: "w-16 accent-indigo-500"
424
+ }
425
+ ),
426
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: chip(tracing.sampleColors), "aria-pressed": tracing.sampleColors, onClick: () => controller.updateTracing({ sampleColors: !tracing.sampleColors }), children: "Trace colours" }),
427
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: chip(tracing.visible), "aria-pressed": tracing.visible, onClick: () => controller.updateTracing({ visible: !tracing.visible }), children: "Show tracing" }),
428
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => controller.clearTracingImage(), children: "Remove tracing" })
429
+ ] }),
430
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => refRef.current?.click(), children: "Reference\u2026" }),
431
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, title: "Which hand holds the pen (palm rejection)", onClick: () => controller.setHand(state.hand === "right" ? "left" : "right"), children: state.hand === "right" ? "Right-handed" : "Left-handed" }),
432
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mx-1 h-4 w-px bg-slate-700" }),
433
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => attempt(() => (0, import_paint_editor3.downloadBytes)(JSON.stringify(controller.toJSON()), `${fileName}.paint.json`, "application/json")), children: "Save" }),
434
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => openRef.current?.click(), children: "Open\u2026" }),
435
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => exportImage("image/png"), children: "PNG" }),
436
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => exportImage("image/jpeg"), children: "JPEG" }),
437
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: button, onClick: () => attempt(() => (0, import_paint_editor3.downloadBytes)(controller.exportPsd().bytes, `${fileName}.psd`, "image/vnd.adobe.photoshop")), children: "PSD" }),
438
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
439
+ "input",
440
+ {
441
+ ref: openRef,
442
+ type: "file",
443
+ accept: ".json,application/json",
444
+ "aria-label": "Open painting",
445
+ className: "hidden",
446
+ onChange: (e) => {
447
+ const f = e.target.files?.[0];
448
+ if (f) attempt(async () => controller.loadJSON(JSON.parse(await f.text())));
449
+ e.target.value = "";
450
+ }
451
+ }
452
+ ),
453
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
454
+ "input",
455
+ {
456
+ ref: tracingRef,
457
+ type: "file",
458
+ accept: "image/*",
459
+ "aria-label": "Load tracing image",
460
+ className: "hidden",
461
+ onChange: (e) => {
462
+ const f = e.target.files?.[0];
463
+ if (f) attempt(async () => controller.setTracingImage({ pixels: await (0, import_paint_editor3.decodeImageBlob)(f) }));
464
+ e.target.value = "";
465
+ }
466
+ }
467
+ ),
468
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
469
+ "input",
470
+ {
471
+ ref: refRef,
472
+ type: "file",
473
+ accept: "image/*",
474
+ "aria-label": "Add reference image",
475
+ className: "hidden",
476
+ onChange: (e) => {
477
+ const f = e.target.files?.[0];
478
+ if (f) attempt(async () => controller.addReferenceImage({ pixels: await (0, import_paint_editor3.decodeImageBlob)(f), name: f.name }));
479
+ e.target.value = "";
480
+ }
481
+ }
482
+ ),
483
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorLine, { error })
484
+ ] });
485
+ };
486
+
487
+ // src/paint-editor/BrushPanel.tsx
488
+ var import_react5 = require("react");
489
+ var import_paint_editor4 = require("@pixerate/editor/paint-editor");
490
+ var import_jsx_runtime4 = require("react/jsx-runtime");
491
+ var BrushPanel = ({ controller, thumbnailWidth = 112, thumbnailHeight = 32, className }) => {
492
+ const state = usePaintEditorState(controller);
493
+ const [error, attempt] = useAttempt();
494
+ const [saveName, setSaveName] = (0, import_react5.useState)("");
495
+ const fileRef = (0, import_react5.useRef)(null);
496
+ const { brush, brushes } = state;
497
+ const paper = state.document.paper;
498
+ const thumbs = (0, import_react5.useMemo)(
499
+ () => new Map(brushes.map((b) => [b.id, controller.brushThumbnail(b.id, { width: thumbnailWidth, height: thumbnailHeight, color: "#26324a" })])),
500
+ // Thumbnails follow the library and the paper.
501
+ // eslint-disable-next-line react-hooks/exhaustive-deps
502
+ [controller, brushes, paper, thumbnailWidth, thumbnailHeight]
503
+ );
504
+ const slug = (name) => name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "brush";
505
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: `${panel} ${className ?? ""}`, "data-testid": "brush-panel", children: [
506
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "grid grid-cols-2 gap-1", role: "listbox", "aria-label": "Brushes", children: brushes.map((b) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
507
+ "button",
508
+ {
509
+ type: "button",
510
+ role: "option",
511
+ "aria-selected": brush.id === b.id,
512
+ title: `${b.name} (${b.category})`,
513
+ className: `flex flex-col items-start gap-0.5 rounded p-1 text-left ${brush.id === b.id ? "bg-indigo-600/30 ring-1 ring-indigo-500" : "bg-slate-900 hover:bg-slate-800"}`,
514
+ onClick: () => controller.setBrush({ id: b.id }),
515
+ children: [
516
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(PixelCanvas, { pixels: thumbs.get(b.id) ?? null, label: `${b.name} thumbnail`, className: "rounded", style: { width: "100%", aspectRatio: `${thumbnailWidth} / ${thumbnailHeight}` } }),
517
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "truncate text-[10px] text-slate-300", children: b.name })
518
+ ]
519
+ },
520
+ b.id
521
+ )) }),
522
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "flex flex-col gap-1.5", children: [
523
+ import_paint_editor4.BRUSH_SLIDERS.map((c) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(PaintSlider, { control: c, value: (0, import_paint_editor4.readPath)(brush, c.path), onChange: (v) => controller.setBrush((0, import_paint_editor4.patchPath)(brush, c.path, v)) }, c.path)),
524
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { className: "flex items-center justify-between gap-2", children: [
525
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "text-slate-400", children: "Rotation" }),
526
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("select", { "aria-label": "Brush rotation", value: brush.rotation, onChange: (e) => controller.setBrush({ rotation: e.target.value }), className: "rounded bg-slate-800 px-1 py-0.5", children: import_paint_editor4.BRUSH_ROTATIONS.map((r) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("option", { value: r, children: r === "stroke" ? "Follow stroke" : r === "barrel" ? "Pen barrel" : "Fixed" }, r)) })
527
+ ] }),
528
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { className: "flex items-center gap-2", children: [
529
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { type: "checkbox", checked: brush.tip.pressureSize, onChange: (e) => controller.setBrush({ tip: { pressureSize: e.target.checked } }) }),
530
+ " Pressure \u2192 size"
531
+ ] }),
532
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { className: "flex items-center gap-2", children: [
533
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { type: "checkbox", checked: brush.tip.pressureOpacity, onChange: (e) => controller.setBrush({ tip: { pressureOpacity: e.target.checked } }) }),
534
+ " Pressure \u2192 opacity"
535
+ ] })
536
+ ] }),
537
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "flex flex-wrap items-center gap-1", children: [
538
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { value: saveName, placeholder: "Brush name", "aria-label": "Brush name", onChange: (e) => setSaveName(e.target.value), className: "w-28 rounded bg-slate-800 px-1.5 py-0.5" }),
539
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: button, disabled: !saveName.trim(), onClick: () => attempt(() => (controller.saveBrush({ id: slug(saveName), name: saveName.trim() }), setSaveName(""))), children: "Save brush" }),
540
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: button, onClick: () => attempt(() => (0, import_paint_editor4.downloadBytes)(JSON.stringify(controller.exportBrushes(), null, 2), "brushes.json", "application/json")), children: "Export" }),
541
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: button, onClick: () => fileRef.current?.click(), children: "Import\u2026" }),
542
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
543
+ "input",
544
+ {
545
+ ref: fileRef,
546
+ type: "file",
547
+ accept: ".json,application/json",
548
+ "aria-label": "Import brushes",
549
+ className: "hidden",
550
+ onChange: (e) => {
551
+ const f = e.target.files?.[0];
552
+ if (f) attempt(async () => controller.importBrushes(JSON.parse(await f.text())));
553
+ e.target.value = "";
554
+ }
555
+ }
556
+ )
557
+ ] }),
558
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ErrorLine, { error })
559
+ ] });
560
+ };
561
+
562
+ // src/paint-editor/PaperPanel.tsx
563
+ var import_react6 = require("react");
564
+ var import_paint_editor5 = require("@pixerate/editor/paint-editor");
565
+ var import_jsx_runtime5 = require("react/jsx-runtime");
566
+ var SWATCH = { x: 0, y: 0, width: 64, height: 40 };
567
+ var PaperPanel = ({ controller, className }) => {
568
+ const state = usePaintEditorState(controller);
569
+ const [error, attempt] = useAttempt();
570
+ const paper = state.document.paper;
571
+ const previews = (0, import_react6.useMemo)(() => new Map(import_paint_editor5.PAPER_LIBRARY.map((p) => [p.preset, { ...SWATCH, data: (0, import_paint_editor5.renderPaper)({ ...p, shading: Math.max(0.8, p.shading) }, SWATCH) }])), []);
572
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: `${panel} ${className ?? ""}`, "data-testid": "paper-panel", children: [
573
+ import_paint_editor5.PAPER_FAMILIES.map((family) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { children: [
574
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "mb-0.5 text-[10px] uppercase tracking-wide text-slate-500", children: family }),
575
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "flex flex-wrap gap-1", role: "listbox", "aria-label": `${family} papers`, children: import_paint_editor5.PAPER_LIBRARY.filter((p) => p.family === family).map((p) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
576
+ "button",
577
+ {
578
+ type: "button",
579
+ role: "option",
580
+ "aria-selected": paper.preset === p.preset,
581
+ title: p.name,
582
+ className: `rounded p-0.5 ${paper.preset === p.preset ? "ring-2 ring-indigo-500" : "ring-1 ring-slate-700 hover:ring-slate-500"}`,
583
+ onClick: () => attempt(() => controller.setPaper({ preset: p.preset })),
584
+ children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PixelCanvas, { pixels: previews.get(p.preset) ?? null, label: p.name, style: { width: 48, height: 30 } })
585
+ },
586
+ p.preset
587
+ )) })
588
+ ] }, family)),
589
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "text-slate-300", children: paper.name }),
590
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("label", { className: "flex items-center justify-between", children: [
591
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "text-slate-400", children: "Colour" }),
592
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(CommitColorInput, { label: "Paper colour", value: paper.color, onCommit: (v) => attempt(() => controller.setPaper({ color: v })), className: "h-6 w-10 rounded bg-transparent" })
593
+ ] }),
594
+ import_paint_editor5.PAPER_SLIDERS.map((c) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PaintSlider, { control: c, value: (0, import_paint_editor5.readPath)(paper, c.path), onCommit: (v) => attempt(() => controller.setPaper((0, import_paint_editor5.patchPath)(paper, c.path, v))) }, c.path)),
595
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("label", { className: "flex items-center gap-2", children: [
596
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("input", { type: "checkbox", checked: paper.tiling === "mirror", onChange: (e) => attempt(() => controller.setPaper({ tiling: e.target.checked ? "mirror" : "repeat" })) }),
597
+ " Mirror tiling"
598
+ ] }),
599
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("button", { type: "button", className: "self-start px-2 py-1 rounded text-[11px] bg-slate-800 text-slate-200 hover:bg-slate-700", onClick: () => attempt(() => controller.setPaper({ seed: paper.seed + 1 })), children: "New sheet" }),
600
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorLine, { error })
601
+ ] });
602
+ };
603
+
604
+ // src/paint-editor/ColorPanel.tsx
605
+ var import_react7 = require("react");
606
+ var import_paint_editor6 = require("@pixerate/editor/paint-editor");
607
+ var import_jsx_runtime6 = require("react/jsx-runtime");
608
+ var ColorPanel = ({ controller, wheelSize = 168, mixingPad = true, className }) => {
609
+ const state = usePaintEditorState(controller);
610
+ const [error, attempt] = useAttempt();
611
+ const [wheel, setWheel] = (0, import_react7.useState)("ryb");
612
+ const [kind, setKind] = (0, import_react7.useState)("complementary");
613
+ const [padTool, setPadTool] = (0, import_react7.useState)("paint");
614
+ const padPoints = (0, import_react7.useRef)(null);
615
+ const ring = (0, import_react7.useMemo)(() => (0, import_paint_editor6.renderColorWheel)(wheelSize, wheel), [wheelSize, wheel]);
616
+ const color = state.color;
617
+ const harmony = (0, import_react7.useMemo)(() => controller.harmony(kind, { color, wheel }), [controller, kind, color, wheel]);
618
+ const [r, g, b] = (0, import_paint_editor6.parseHexColor)(color);
619
+ const hsv = (0, import_paint_editor6.rgbToHsv)(r, g, b);
620
+ const pad = (0, import_react7.useMemo)(() => ({ width: import_paint_editor6.MIXING_PAD_WIDTH, height: import_paint_editor6.MIXING_PAD_HEIGHT, data: controller.mixingPad.pixels() }), [controller, state.mixingPadVersion]);
621
+ const R = wheelSize / 2;
622
+ const mid = R * 0.82;
623
+ const setHsv = (s, v) => controller.setColor((0, import_paint_editor6.rgbToHexColor)(...(0, import_paint_editor6.hsvToRgb)(hsv.h, s, v)));
624
+ const padPoint = (e) => {
625
+ const rect = e.currentTarget.getBoundingClientRect();
626
+ const kx = import_paint_editor6.MIXING_PAD_WIDTH / (rect.width || import_paint_editor6.MIXING_PAD_WIDTH);
627
+ const ky = import_paint_editor6.MIXING_PAD_HEIGHT / (rect.height || import_paint_editor6.MIXING_PAD_HEIGHT);
628
+ const x = Math.min(import_paint_editor6.MIXING_PAD_WIDTH, Math.max(0, (e.clientX - rect.left) * kx));
629
+ const y = Math.min(import_paint_editor6.MIXING_PAD_HEIGHT, Math.max(0, (e.clientY - rect.top) * ky));
630
+ return e.pointerType === "pen" && e.pressure > 0 ? { x, y, pressure: e.pressure } : { x, y };
631
+ };
632
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: `${panel} ${className ?? ""}`, "data-testid": "color-panel", children: [
633
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "relative self-center", style: { width: wheelSize, height: wheelSize }, children: [
634
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
635
+ "div",
636
+ {
637
+ role: "slider",
638
+ "aria-label": "Hue wheel",
639
+ "aria-valuemin": 0,
640
+ "aria-valuemax": 360,
641
+ "aria-valuenow": Math.round((0, import_paint_editor6.hueToWheel)(hsv.h, wheel)),
642
+ tabIndex: 0,
643
+ className: "absolute inset-0 cursor-crosshair",
644
+ onPointerDown: (e) => {
645
+ const rect = e.currentTarget.getBoundingClientRect();
646
+ controller.setColor((0, import_paint_editor6.wheelPick)(e.clientX - rect.left - R, e.clientY - rect.top - R, wheel, color));
647
+ },
648
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(PixelCanvas, { pixels: ring, label: "Colour wheel", style: { width: wheelSize, height: wheelSize } })
649
+ }
650
+ ),
651
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("svg", { className: "pointer-events-none absolute inset-0", width: wheelSize, height: wheelSize, "aria-hidden": "true", children: harmony.map((h, i) => {
652
+ const p = (0, import_paint_editor6.wheelPoint)(h.angle, mid);
653
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("circle", { cx: R + p.x, cy: R + p.y, r: i === 0 ? 7 : 5, fill: h.color, stroke: "#fff", strokeWidth: i === 0 ? 2 : 1 }, i);
654
+ }) }),
655
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "absolute rounded-full border border-slate-700", style: { left: R - R * 0.45, top: R - R * 0.45, width: R * 0.9, height: R * 0.9, background: color } })
656
+ ] }),
657
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex items-center gap-2", children: [
658
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("input", { type: "color", "aria-label": "Paint colour", value: color, onChange: (e) => controller.setColor(e.target.value), className: "h-7 w-9 rounded bg-transparent" }),
659
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("input", { type: "color", "aria-label": "Second colour", value: state.secondaryColor, onChange: (e) => controller.setSecondaryColor(e.target.value), className: "h-5 w-7 rounded bg-transparent" }),
660
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", className: button, title: "Swap colours (X)", onClick: () => controller.swapColors(), children: "Swap" }),
661
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
662
+ "input",
663
+ {
664
+ "aria-label": "Hex colour",
665
+ defaultValue: color,
666
+ onBlur: (e) => attempt(() => controller.setColor(e.target.value.startsWith("#") ? e.target.value : `#${e.target.value}`)),
667
+ className: "w-20 rounded bg-slate-800 px-1.5 py-0.5 font-mono"
668
+ },
669
+ color
670
+ )
671
+ ] }),
672
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("label", { className: "flex items-center gap-2", children: [
673
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "w-16 text-slate-400", children: "Saturation" }),
674
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("input", { type: "range", "aria-label": "Saturation", min: 0, max: 1, step: 0.01, value: hsv.s, onChange: (e) => setHsv(Number(e.target.value), hsv.v), className: "flex-1 accent-indigo-500" })
675
+ ] }),
676
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("label", { className: "flex items-center gap-2", children: [
677
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "w-16 text-slate-400", children: "Value" }),
678
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("input", { type: "range", "aria-label": "Value", min: 0, max: 1, step: 0.01, value: hsv.v, onChange: (e) => setHsv(hsv.s, Number(e.target.value)), className: "flex-1 accent-indigo-500" })
679
+ ] }),
680
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex items-center gap-1", children: [
681
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("select", { "aria-label": "Harmony", value: kind, onChange: (e) => setKind(e.target.value), className: "rounded bg-slate-800 px-1 py-0.5", children: import_paint_editor6.HARMONY_KINDS.map((k) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("option", { value: k, children: k }, k)) }),
682
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", className: chip(wheel === "ryb"), "aria-pressed": wheel === "ryb", onClick: () => setWheel("ryb"), children: "RYB" }),
683
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", className: chip(wheel === "rgb"), "aria-pressed": wheel === "rgb", onClick: () => setWheel("rgb"), children: "RGB" })
684
+ ] }),
685
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex flex-wrap gap-1", "aria-label": "Harmony colours", children: [
686
+ harmony.map((h, i) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", "aria-label": `Use ${h.color}`, title: h.color, className: "h-6 w-6 rounded ring-1 ring-slate-700", style: { background: h.color }, onClick: () => controller.setColor(h.color) }, i)),
687
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", className: button, onClick: () => harmony.forEach((h) => controller.addSwatch(h.color)), children: "+ swatches" })
688
+ ] }),
689
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex flex-wrap gap-1", role: "listbox", "aria-label": "Swatches", children: [
690
+ state.swatches.map((s, i) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
691
+ "button",
692
+ {
693
+ type: "button",
694
+ role: "option",
695
+ "aria-selected": s.color === color,
696
+ "aria-label": s.name ?? s.color,
697
+ title: `${s.name ?? s.color} (right-click to remove)`,
698
+ className: "h-5 w-5 rounded ring-1 ring-slate-700",
699
+ style: { background: s.color },
700
+ onClick: () => controller.setColor(s.color),
701
+ onContextMenu: (e) => {
702
+ e.preventDefault();
703
+ controller.removeSwatch(i);
704
+ }
705
+ },
706
+ i
707
+ )),
708
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", className: button, "aria-label": "Add swatch", onClick: () => controller.addSwatch(color), children: "+" })
709
+ ] }),
710
+ mixingPad && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex flex-col gap-1", "data-testid": "mixing-pad", children: [
711
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex items-center gap-1", children: [
712
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "text-slate-400", children: "Mixing pad" }),
713
+ ["paint", "knife", "pick"].map((t) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", "aria-pressed": padTool === t, className: chip(padTool === t), onClick: () => setPadTool(t), children: t === "paint" ? "Paint" : t === "knife" ? "Knife" : "Pick" }, t)),
714
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("button", { type: "button", className: button, onClick: () => controller.clearMixingPad(), children: "Clear" })
715
+ ] }),
716
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
717
+ "div",
718
+ {
719
+ role: "application",
720
+ "aria-label": "Mixing pad",
721
+ className: "touch-none cursor-crosshair",
722
+ onPointerDown: (e) => {
723
+ const p = padPoint(e);
724
+ if (padTool === "pick") {
725
+ attempt(() => controller.mixingPadPick(p.x, p.y, { setColor: true }));
726
+ return;
727
+ }
728
+ padPoints.current = [p];
729
+ e.target.setPointerCapture?.(e.pointerId);
730
+ },
731
+ onPointerMove: (e) => padPoints.current?.push(padPoint(e)),
732
+ onPointerUp: () => {
733
+ const pts = padPoints.current;
734
+ padPoints.current = null;
735
+ if (pts?.length) attempt(() => controller.mixingPadStroke(pts, { tool: padTool === "knife" ? "knife" : "paint" }));
736
+ },
737
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(PixelCanvas, { pixels: pad, label: "Mixing pad pixels", className: "w-full rounded", style: { aspectRatio: `${import_paint_editor6.MIXING_PAD_WIDTH} / ${import_paint_editor6.MIXING_PAD_HEIGHT}` } })
738
+ }
739
+ )
740
+ ] }),
741
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ErrorLine, { error })
742
+ ] });
743
+ };
744
+
745
+ // src/paint-editor/PaintLayersPanel.tsx
746
+ var import_react8 = require("react");
747
+ var import_paint_editor7 = require("@pixerate/editor/paint-editor");
748
+ var import_jsx_runtime7 = require("react/jsx-runtime");
749
+ function siblingIndex(layers, l) {
750
+ const sibs = layers.filter((x) => x.parentId === l.parentId).reverse();
751
+ return { index: sibs.findIndex((x) => x.id === l.id), count: sibs.length };
752
+ }
753
+ var PaintLayersPanel = ({ controller, className }) => {
754
+ usePaintEditorState(controller);
755
+ const [error, attempt] = useAttempt();
756
+ const [renaming, setRenaming] = (0, import_react8.useState)(null);
757
+ const layers = controller.listLayers();
758
+ const active = layers.find((l) => l.active) ?? null;
759
+ const move = (delta) => {
760
+ if (!active) return;
761
+ const { index, count } = siblingIndex(layers, active);
762
+ const to = Math.min(count - 1, Math.max(0, index + delta));
763
+ if (to !== index) attempt(() => controller.moveLayer(active.id, to));
764
+ };
765
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: `${panel} ${className ?? ""}`, "data-testid": "paint-layers-panel", children: [
766
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "flex flex-wrap gap-1", children: [
767
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, onClick: () => attempt(() => controller.addLayer()), children: "New layer" }),
768
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, onClick: () => attempt(() => controller.addLayer({ type: "group" })), children: "New group" }),
769
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, disabled: !active, onClick: () => active && attempt(() => controller.duplicateLayer(active.id)), children: "Duplicate" }),
770
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, disabled: !active, "aria-label": "Move layer up", onClick: () => move(1), children: "\u2191" }),
771
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, disabled: !active, "aria-label": "Move layer down", onClick: () => move(-1), children: "\u2193" }),
772
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, disabled: !active, onClick: () => active && attempt(() => controller.mergeDown(active.id)), children: "Merge down" }),
773
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: button, disabled: !active || layers.length < 2, onClick: () => active && attempt(() => controller.removeLayer(active.id)), children: "Delete" })
774
+ ] }),
775
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("ul", { role: "tree", "aria-label": "Layers", className: "flex flex-col gap-0.5", children: layers.map((l) => /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
776
+ "li",
777
+ {
778
+ role: "treeitem",
779
+ "aria-selected": l.active,
780
+ className: `flex items-center gap-1.5 rounded px-1.5 py-1 cursor-pointer ${l.active ? "bg-indigo-600/30 ring-1 ring-indigo-500" : "hover:bg-slate-800"}`,
781
+ style: { paddingLeft: 6 + l.depth * 12 },
782
+ onClick: () => controller.setActiveLayer(l.id),
783
+ children: [
784
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
785
+ "button",
786
+ {
787
+ type: "button",
788
+ "aria-label": l.visible ? `Hide ${l.name}` : `Show ${l.name}`,
789
+ className: `w-5 text-[11px] ${l.visible ? "text-slate-200" : "text-slate-600"}`,
790
+ onClick: (e) => {
791
+ e.stopPropagation();
792
+ attempt(() => controller.updateLayer(l.id, { visible: !l.visible }));
793
+ },
794
+ children: l.visible ? "\u25CF" : "\u25CB"
795
+ }
796
+ ),
797
+ renaming === l.id ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
798
+ "input",
799
+ {
800
+ autoFocus: true,
801
+ "aria-label": "Layer name",
802
+ defaultValue: l.name,
803
+ className: "flex-1 rounded bg-slate-800 px-1",
804
+ onClick: (e) => e.stopPropagation(),
805
+ onBlur: (e) => {
806
+ setRenaming(null);
807
+ if (e.target.value.trim() && e.target.value !== l.name) attempt(() => controller.updateLayer(l.id, { name: e.target.value }));
808
+ },
809
+ onKeyDown: (e) => {
810
+ if (e.key === "Enter") e.target.blur();
811
+ if (e.key === "Escape") setRenaming(null);
812
+ }
813
+ }
814
+ ) : /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("span", { className: "flex-1 truncate", onDoubleClick: () => setRenaming(l.id), children: [
815
+ l.type === "group" ? "\u25B8 " : "",
816
+ l.name
817
+ ] }),
818
+ l.locks.pixels || l.locks.all ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { title: "Locked", children: "\u{1F512}" }) : null
819
+ ]
820
+ },
821
+ l.id
822
+ )) }),
823
+ active && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "flex flex-col gap-1.5", children: [
824
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("label", { className: "flex items-center gap-2", children: [
825
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "w-14 text-slate-400", children: "Opacity" }),
826
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
827
+ "input",
828
+ {
829
+ type: "range",
830
+ "aria-label": "Layer opacity",
831
+ min: 0,
832
+ max: 1,
833
+ step: 0.01,
834
+ value: active.opacity,
835
+ onChange: (e) => attempt(() => controller.updateLayer(active.id, { opacity: Number(e.target.value) })),
836
+ className: "flex-1 accent-indigo-500"
837
+ }
838
+ )
839
+ ] }),
840
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("label", { className: "flex items-center gap-2", children: [
841
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "w-14 text-slate-400", children: "Blend" }),
842
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("select", { "aria-label": "Layer blend", value: active.blend, onChange: (e) => attempt(() => controller.updateLayer(active.id, { blend: e.target.value })), className: "flex-1 rounded bg-slate-800 px-1 py-0.5", children: [
843
+ active.type === "group" && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("option", { value: "pass-through", children: "Pass through" }),
844
+ import_paint_editor7.BLEND_MENU_GROUPS.map((g) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("optgroup", { label: g.label, children: g.modes.map((m) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("option", { value: m.id, title: m.description, children: m.label }, m.id)) }, g.label))
845
+ ] })
846
+ ] }),
847
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "flex gap-1", children: [
848
+ ["pixels", "transparency"].map((k) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", "aria-pressed": Boolean(active.locks[k]), className: chip(Boolean(active.locks[k])), onClick: () => attempt(() => controller.updateLayer(active.id, { locks: { [k]: !active.locks[k] } })), children: k === "pixels" ? "Lock pixels" : "Lock transparency" }, k)),
849
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", "aria-pressed": active.clipped, className: chip(active.clipped), onClick: () => attempt(() => controller.updateLayer(active.id, { clipped: !active.clipped })), children: "Clip" })
850
+ ] })
851
+ ] }),
852
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ErrorLine, { error })
853
+ ] });
854
+ };
855
+
856
+ // src/paint-editor/PaintEditor.tsx
857
+ var import_react9 = require("react");
858
+ var import_jsx_runtime8 = require("react/jsx-runtime");
859
+ var PaintEditor = ({ controller, fileName, height = 720, className }) => {
860
+ const [tool, setTool] = (0, import_react9.useState)("brush");
861
+ const [tab, setTab] = (0, import_react9.useState)("color");
862
+ const [error, setError] = (0, import_react9.useState)(null);
863
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: `flex flex-col gap-2 rounded-xl border border-slate-800 bg-slate-900 p-2 ${className ?? ""}`, style: { height }, "data-testid": "paint-editor", children: [
864
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(PaintToolbar, { controller, tool, onToolChange: setTool, fileName }),
865
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex min-h-0 flex-1 gap-2", children: [
866
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(BrushPanel, { controller, className: "w-60 shrink-0 overflow-y-auto pr-1" }),
867
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex min-w-0 flex-1 flex-col", children: [
868
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(PaintCanvas, { controller, tool, onError: setError, className: "flex-1 rounded-lg" }),
869
+ error && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("p", { role: "alert", className: "mt-1 text-[11px] text-rose-400", children: error })
870
+ ] }),
871
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "flex w-64 shrink-0 flex-col gap-2 overflow-y-auto pr-1", children: [
872
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "flex gap-1", role: "tablist", "aria-label": "Paint panels", children: ["color", "paper", "layers"].map((t) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "button", role: "tab", "aria-selected": tab === t, className: chip(tab === t), onClick: () => setTab(t), children: t === "color" ? "Colour" : t === "paper" ? "Paper" : "Layers" }, t)) }),
873
+ tab === "color" && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ColorPanel, { controller }),
874
+ tab === "paper" && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(PaperPanel, { controller }),
875
+ tab === "layers" && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(PaintLayersPanel, { controller })
876
+ ] })
877
+ ] })
878
+ ] });
879
+ };
880
+ // Annotate the CommonJS export names for ESM import in node:
881
+ 0 && (module.exports = {
882
+ BrushPanel,
883
+ ColorPanel,
884
+ PaintCanvas,
885
+ PaintEditor,
886
+ PaintLayersPanel,
887
+ PaintSlider,
888
+ PaintToolbar,
889
+ PaperPanel,
890
+ usePaintEditor,
891
+ usePaintEditorState
892
+ });
893
+ //# sourceMappingURL=index.cjs.map