@pixerate/editor-react 0.179.0 → 0.181.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/dist/brushes/BrushEditor.d.cts +40 -0
- package/dist/brushes/BrushEditor.d.ts +40 -0
- package/dist/brushes/BrushLibraryEditor.d.cts +31 -0
- package/dist/brushes/BrushLibraryEditor.d.ts +31 -0
- package/dist/brushes/index.cjs +292 -20
- package/dist/brushes/index.d.cts +2 -0
- package/dist/brushes/index.d.ts +2 -0
- package/dist/brushes/index.js +6 -2
- package/dist/chunk-THMOZMEE.js +122 -0
- package/dist/{chunk-UR4KL7WE.js → chunk-WCSMREA4.js} +26 -6
- package/dist/chunk-XORL5PGL.js +263 -0
- package/dist/image-editor/BrushOptionsBar.d.cts +6 -1
- package/dist/image-editor/BrushOptionsBar.d.ts +6 -1
- package/dist/image-editor/index.cjs +1181 -894
- package/dist/image-editor/index.js +3 -3
- package/dist/index.cjs +1722 -1435
- package/dist/index.js +3 -3
- package/dist/paint-editor/index.cjs +22 -24
- package/dist/paint-editor/index.d.cts +1 -1
- package/dist/paint-editor/index.d.ts +1 -1
- package/dist/paint-editor/index.js +415 -610
- package/dist/vector/VectorAppearancePanel.d.cts +8 -2
- package/dist/vector/VectorAppearancePanel.d.ts +8 -2
- package/dist/vector/index.cjs +1113 -748
- package/dist/vector/index.js +90 -59
- package/package.json +3 -3
- package/dist/chunk-62FMEDGG.js +0 -54
- package/dist/chunk-PQK2UMAY.js +0 -23
- package/dist/paint-editor/BrushEditor.d.cts +0 -29
- package/dist/paint-editor/BrushEditor.d.ts +0 -29
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BrushEditor,
|
|
3
|
+
button
|
|
4
|
+
} from "./chunk-XORL5PGL.js";
|
|
5
|
+
|
|
6
|
+
// src/brushes/BrushLibraryEditor.tsx
|
|
7
|
+
import { useEffect, useMemo, useState } from "react";
|
|
8
|
+
import { brushPreviewColours, checkBrushEdit, newBrushFor, saveBrushEdit } from "@pixerate/editor/brushes";
|
|
9
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
var BrushLibraryEditor = ({ library, brushId, profile, renderPreview, previewPaper = "#ffffff", previewColor, onSaved, onClose, className }) => {
|
|
11
|
+
const [original, setOriginal] = useState(null);
|
|
12
|
+
const [builtin, setBuiltin] = useState(false);
|
|
13
|
+
const [draft, setDraft] = useState(null);
|
|
14
|
+
const [status, setStatus] = useState(null);
|
|
15
|
+
const [preview, setPreview] = useState(null);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
let live = true;
|
|
18
|
+
setStatus(null);
|
|
19
|
+
void (async () => {
|
|
20
|
+
const entry = brushId ? await library.get(brushId) : null;
|
|
21
|
+
if (!live) return;
|
|
22
|
+
setOriginal(entry?.brush ?? null);
|
|
23
|
+
setBuiltin(!!entry?.builtin);
|
|
24
|
+
setDraft(entry?.brush ?? newBrushFor(profile));
|
|
25
|
+
})();
|
|
26
|
+
return () => {
|
|
27
|
+
live = false;
|
|
28
|
+
};
|
|
29
|
+
}, [library, brushId, profile]);
|
|
30
|
+
const paper = renderPreview?.paper ?? previewPaper;
|
|
31
|
+
const colours = useMemo(() => draft ? brushPreviewColours(draft, { paper, profile }) : null, [draft, paper, profile]);
|
|
32
|
+
const colour = previewColor ?? colours?.color ?? "#475569";
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (!draft || !renderPreview) return;
|
|
35
|
+
let live = true;
|
|
36
|
+
Promise.resolve(renderPreview(draft, { width: 240, height: 60, color: colour.slice(0, 7) })).then((img) => live && setPreview(img.data ? `data:${img.mimeType};base64,${img.data}` : null)).catch(() => live && setPreview(null));
|
|
37
|
+
return () => {
|
|
38
|
+
live = false;
|
|
39
|
+
};
|
|
40
|
+
}, [draft, renderPreview, colour]);
|
|
41
|
+
const check = useMemo(() => draft ? checkBrushEdit(original, draft, profile) : null, [original, draft, profile]);
|
|
42
|
+
if (!draft) return null;
|
|
43
|
+
const save = async (name) => {
|
|
44
|
+
const saved = await saveBrushEdit(library, { original, brush: draft, profile, ...name !== void 0 ? { name } : {} });
|
|
45
|
+
setOriginal(saved.brush);
|
|
46
|
+
setBuiltin(false);
|
|
47
|
+
setDraft(saved.brush);
|
|
48
|
+
setStatus(`Saved \u201C${saved.brush.name}\u201D${saved.kept.length ? `, keeping what ${profile.name} can't use` : ""}.`);
|
|
49
|
+
onSaved?.(saved.brush);
|
|
50
|
+
};
|
|
51
|
+
const errors = check?.issues.filter((i) => i.severity === "error") ?? [];
|
|
52
|
+
return /* @__PURE__ */ jsxs("div", { className: `flex flex-col gap-2 text-xs text-slate-200 ${className ?? ""}`, "data-testid": "brush-library-editor", children: [
|
|
53
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
|
|
54
|
+
/* @__PURE__ */ jsx("span", { className: "font-medium", "data-testid": "brush-library-editor-title", children: original ? `${draft.name}${builtin ? " (built in: saves a copy)" : ""}` : "New brush" }),
|
|
55
|
+
/* @__PURE__ */ jsxs("span", { className: "flex gap-1", children: [
|
|
56
|
+
/* @__PURE__ */ jsx(
|
|
57
|
+
"button",
|
|
58
|
+
{
|
|
59
|
+
type: "button",
|
|
60
|
+
className: button,
|
|
61
|
+
title: `A new brush from this one, with only what ${profile.name} uses`,
|
|
62
|
+
onClick: () => {
|
|
63
|
+
setOriginal(null);
|
|
64
|
+
setBuiltin(false);
|
|
65
|
+
setDraft(newBrushFor(profile, draft));
|
|
66
|
+
setStatus(null);
|
|
67
|
+
},
|
|
68
|
+
children: "New brush"
|
|
69
|
+
}
|
|
70
|
+
),
|
|
71
|
+
onClose && /* @__PURE__ */ jsx("button", { type: "button", className: button, onClick: onClose, children: "Close" })
|
|
72
|
+
] })
|
|
73
|
+
] }),
|
|
74
|
+
/* @__PURE__ */ jsx(
|
|
75
|
+
BrushEditor,
|
|
76
|
+
{
|
|
77
|
+
brush: draft,
|
|
78
|
+
profile,
|
|
79
|
+
preview: renderPreview ? preview : void 0,
|
|
80
|
+
previewBackground: colours?.background,
|
|
81
|
+
onChange: (next) => {
|
|
82
|
+
setDraft(next);
|
|
83
|
+
setStatus(null);
|
|
84
|
+
},
|
|
85
|
+
onSave: original ? () => save() : void 0,
|
|
86
|
+
saveLabel: builtin ? "Save a copy" : "Save",
|
|
87
|
+
onSaveAs: (name) => save(name)
|
|
88
|
+
}
|
|
89
|
+
),
|
|
90
|
+
errors.length > 0 && /* @__PURE__ */ jsx("ul", { className: "text-[11px] text-rose-400", "data-testid": "brush-library-editor-issues", children: errors.map((i) => /* @__PURE__ */ jsxs("li", { children: [
|
|
91
|
+
i.reason,
|
|
92
|
+
" ",
|
|
93
|
+
i.fix
|
|
94
|
+
] }, i.facet)) }),
|
|
95
|
+
status && /* @__PURE__ */ jsx("p", { role: "status", className: "text-[11px] text-emerald-400", children: status })
|
|
96
|
+
] });
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/brushes/useBrushLibrary.ts
|
|
100
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
101
|
+
function useBrushLibrary(library, filter = {}) {
|
|
102
|
+
const [entries, setEntries] = useState2([]);
|
|
103
|
+
const { profile, ...rest } = filter;
|
|
104
|
+
const key = `${profile?.id ?? ""}:${JSON.stringify(rest)}`;
|
|
105
|
+
useEffect2(() => {
|
|
106
|
+
if (!library) return;
|
|
107
|
+
let live = true;
|
|
108
|
+
const read = () => void library.list(filter).then((list) => live && setEntries(list), () => void 0);
|
|
109
|
+
read();
|
|
110
|
+
const unsubscribe = library.subscribe(read);
|
|
111
|
+
return () => {
|
|
112
|
+
live = false;
|
|
113
|
+
unsubscribe();
|
|
114
|
+
};
|
|
115
|
+
}, [library, key]);
|
|
116
|
+
return entries;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export {
|
|
120
|
+
BrushLibraryEditor,
|
|
121
|
+
useBrushLibrary
|
|
122
|
+
};
|
|
@@ -42,11 +42,12 @@ import {
|
|
|
42
42
|
usePlaybackSurface
|
|
43
43
|
} from "./chunk-W5CMGX3I.js";
|
|
44
44
|
import {
|
|
45
|
+
BrushLibraryEditor,
|
|
45
46
|
useBrushLibrary
|
|
46
|
-
} from "./chunk-
|
|
47
|
+
} from "./chunk-THMOZMEE.js";
|
|
47
48
|
import {
|
|
48
49
|
BrushLibraryBrowser
|
|
49
|
-
} from "./chunk-
|
|
50
|
+
} from "./chunk-XORL5PGL.js";
|
|
50
51
|
|
|
51
52
|
// src/image-editor/useImageEditor.ts
|
|
52
53
|
import { useRef, useState, useEffect, useCallback, useMemo } from "react";
|
|
@@ -2420,7 +2421,7 @@ var PaintGradientEditor = (props) => /* @__PURE__ */ jsx12(PaintGradientEditorVi
|
|
|
2420
2421
|
// src/image-editor/BrushOptionsBar.tsx
|
|
2421
2422
|
import { IMAGE_PROFILE } from "@pixerate/editor/brushes";
|
|
2422
2423
|
import { jsx as jsx13, jsxs as jsxs11 } from "react/jsx-runtime";
|
|
2423
|
-
var BrushOptionsBar = ({ controller, showTools = true, className }) => {
|
|
2424
|
+
var BrushOptionsBar = ({ controller, showTools = true, renderBrushPreview, className }) => {
|
|
2424
2425
|
const [, setTick] = useState10(0);
|
|
2425
2426
|
useEffect7(() => controller.subscribe(() => setTick((n) => n + 1)), [controller]);
|
|
2426
2427
|
const [error, setError] = useState10(null);
|
|
@@ -2428,6 +2429,7 @@ var BrushOptionsBar = ({ controller, showTools = true, className }) => {
|
|
|
2428
2429
|
const { brush, foreground, background, mixer, mixerLoad, retouch, cloneSource, paint } = state.paint;
|
|
2429
2430
|
const tool = state.activeTool;
|
|
2430
2431
|
const [presetsOpen, setPresetsOpen] = useState10(false);
|
|
2432
|
+
const [editing, setEditing] = useState10(false);
|
|
2431
2433
|
const presets = useBrushLibrary(presetsOpen ? controller.brushLibrary : null, { profile: IMAGE_PROFILE });
|
|
2432
2434
|
const presetName = presets.find((e) => e.brush.id === state.paint.brushPreset)?.brush.name ?? state.paint.brushPreset;
|
|
2433
2435
|
const attempt = (fn) => {
|
|
@@ -2452,10 +2454,28 @@ var BrushOptionsBar = ({ controller, showTools = true, className }) => {
|
|
|
2452
2454
|
t.id
|
|
2453
2455
|
)) }),
|
|
2454
2456
|
(tool === "brush" || tool === "mixer" || tool === "eraser") && /* @__PURE__ */ jsxs11("div", { className: "flex flex-col gap-1", "data-testid": "brush-presets", children: [
|
|
2455
|
-
/* @__PURE__ */ jsxs11("
|
|
2456
|
-
"
|
|
2457
|
-
|
|
2457
|
+
/* @__PURE__ */ jsxs11("div", { className: "flex flex-wrap gap-1", children: [
|
|
2458
|
+
/* @__PURE__ */ jsxs11("button", { type: "button", "aria-expanded": presetsOpen, className: imageChipClass(presetsOpen), onClick: () => setPresetsOpen((o) => !o), title: "Paint with a brush from the brush library", children: [
|
|
2459
|
+
"Brush presets",
|
|
2460
|
+
presetName ? `: ${presetName}` : ""
|
|
2461
|
+
] }),
|
|
2462
|
+
/* @__PURE__ */ jsx13("button", { type: "button", "aria-expanded": editing, className: imageChipClass(editing), onClick: () => setEditing((o) => !o), title: "Edit this brush, or make a new one, in the shared brush library", children: "Edit brush\u2026" })
|
|
2458
2463
|
] }),
|
|
2464
|
+
editing && /* @__PURE__ */ jsx13(
|
|
2465
|
+
BrushLibraryEditor,
|
|
2466
|
+
{
|
|
2467
|
+
library: controller.brushLibrary,
|
|
2468
|
+
brushId: state.paint.brushPreset,
|
|
2469
|
+
profile: IMAGE_PROFILE,
|
|
2470
|
+
renderPreview: renderBrushPreview ?? controller.brushPreview ?? void 0,
|
|
2471
|
+
className: "rounded border border-border bg-slate-950 p-2",
|
|
2472
|
+
onSaved: (b) => {
|
|
2473
|
+
setError(null);
|
|
2474
|
+
controller.setBrushPreset(b.id).catch((err) => setError(userErrorMessage(err)));
|
|
2475
|
+
},
|
|
2476
|
+
onClose: () => setEditing(false)
|
|
2477
|
+
}
|
|
2478
|
+
),
|
|
2459
2479
|
presetsOpen && /* @__PURE__ */ jsx13(
|
|
2460
2480
|
BrushLibraryBrowser,
|
|
2461
2481
|
{
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// src/brushes/BrushLibraryBrowser.tsx
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { brushLibraryGroups } from "@pixerate/editor/brushes";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
var Thumbnail = ({ image, label, style }) => {
|
|
6
|
+
const ref = useRef(null);
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const canvas = ref.current;
|
|
9
|
+
const ctx = canvas?.getContext("2d");
|
|
10
|
+
if (!canvas || !ctx || !image || typeof image === "string" || typeof ctx.createImageData !== "function") return;
|
|
11
|
+
canvas.width = image.width;
|
|
12
|
+
canvas.height = image.height;
|
|
13
|
+
const img = ctx.createImageData(image.width, image.height);
|
|
14
|
+
img.data.set(image.data);
|
|
15
|
+
ctx.putImageData(img, 0, 0);
|
|
16
|
+
}, [image]);
|
|
17
|
+
if (typeof image === "string") return /* @__PURE__ */ jsx("img", { src: image, alt: label, className: "rounded object-contain", style });
|
|
18
|
+
return /* @__PURE__ */ jsx("canvas", { ref, role: "img", "aria-label": label, className: "rounded", style, width: image?.width, height: image?.height });
|
|
19
|
+
};
|
|
20
|
+
var BrushLibraryBrowser = ({ entries, activeId, onSelect, profile, thumbnail, thumbnailWidth = 112, thumbnailHeight = 32, className }) => {
|
|
21
|
+
const [text, setText] = useState("");
|
|
22
|
+
const groups = useMemo(() => brushLibraryGroups(entries, { text, profile }), [entries, text, profile]);
|
|
23
|
+
return /* @__PURE__ */ jsxs("div", { className: `flex flex-col gap-1 ${className ?? ""}`, "data-testid": "brush-library", children: [
|
|
24
|
+
/* @__PURE__ */ jsx("input", { type: "search", value: text, placeholder: "Find a brush", "aria-label": "Find a brush", onChange: (e) => setText(e.target.value), className: "rounded bg-slate-800 px-1.5 py-0.5" }),
|
|
25
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1.5", role: "listbox", "aria-label": "Brushes", children: [
|
|
26
|
+
groups.map((g) => /* @__PURE__ */ jsxs("div", { role: "group", "aria-label": g.title, className: "flex flex-col gap-0.5", children: [
|
|
27
|
+
/* @__PURE__ */ jsx("span", { className: "text-[10px] uppercase tracking-wide text-slate-500", children: g.title }),
|
|
28
|
+
/* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 gap-1", children: g.varieties.flatMap(
|
|
29
|
+
(v) => v.entries.map((e) => /* @__PURE__ */ jsxs(
|
|
30
|
+
"button",
|
|
31
|
+
{
|
|
32
|
+
type: "button",
|
|
33
|
+
role: "option",
|
|
34
|
+
"aria-selected": activeId === e.brush.id,
|
|
35
|
+
title: [e.brush.name, v.title.toLowerCase() === e.brush.name.toLowerCase() ? "" : v.title, e.builtin ? "" : "saved"].filter(Boolean).join(" \xB7 "),
|
|
36
|
+
className: `flex flex-col items-start gap-0.5 rounded p-1 text-left ${activeId === e.brush.id ? "bg-indigo-600/30 ring-1 ring-indigo-500" : "bg-slate-900 hover:bg-slate-800"}`,
|
|
37
|
+
onClick: () => onSelect(e.brush.id),
|
|
38
|
+
children: [
|
|
39
|
+
thumbnail && /* @__PURE__ */ jsx(Thumbnail, { image: thumbnail(e.brush.id) ?? null, label: `${e.brush.name} thumbnail`, style: { width: "100%", aspectRatio: `${thumbnailWidth} / ${thumbnailHeight}` } }),
|
|
40
|
+
/* @__PURE__ */ jsx("span", { className: "truncate text-[10px] text-slate-300", children: e.brush.name })
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
e.brush.id
|
|
44
|
+
))
|
|
45
|
+
) })
|
|
46
|
+
] }, g.id)),
|
|
47
|
+
!groups.length && /* @__PURE__ */ jsx("span", { className: "text-slate-500", children: "No brush matches." })
|
|
48
|
+
] })
|
|
49
|
+
] });
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// src/paint-editor/shared.tsx
|
|
53
|
+
import { useEffect as useEffect2, useRef as useRef2, useState as useState2 } from "react";
|
|
54
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
55
|
+
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"}`;
|
|
56
|
+
var button = "px-2 py-1 rounded text-[11px] bg-slate-800 text-slate-200 hover:bg-slate-700 disabled:opacity-40";
|
|
57
|
+
var panel = "flex flex-col gap-2 text-xs text-slate-200";
|
|
58
|
+
var PixelCanvas = ({ pixels, label, className, style }) => {
|
|
59
|
+
const ref = useRef2(null);
|
|
60
|
+
useEffect2(() => {
|
|
61
|
+
const canvas = ref.current;
|
|
62
|
+
const ctx = canvas?.getContext("2d");
|
|
63
|
+
if (!canvas || !ctx || !pixels || typeof ctx.createImageData !== "function") return;
|
|
64
|
+
canvas.width = pixels.width;
|
|
65
|
+
canvas.height = pixels.height;
|
|
66
|
+
const img = ctx.createImageData(pixels.width, pixels.height);
|
|
67
|
+
img.data.set(pixels.data);
|
|
68
|
+
ctx.putImageData(img, 0, 0);
|
|
69
|
+
}, [pixels]);
|
|
70
|
+
return /* @__PURE__ */ jsx2("canvas", { ref, role: "img", "aria-label": label, className, style, width: pixels?.width, height: pixels?.height });
|
|
71
|
+
};
|
|
72
|
+
var fmt = (c, v) => c.step >= 1 ? String(Math.round(v)) : v.toFixed(2);
|
|
73
|
+
var PaintSlider = ({ control, value, onChange, onCommit }) => {
|
|
74
|
+
const [draft, setDraft] = useState2(null);
|
|
75
|
+
const ref = useRef2(null);
|
|
76
|
+
const commit = useRef2(onCommit);
|
|
77
|
+
commit.current = onCommit;
|
|
78
|
+
useEffect2(() => {
|
|
79
|
+
const el = ref.current;
|
|
80
|
+
if (!el) return;
|
|
81
|
+
const done = () => {
|
|
82
|
+
commit.current?.(Number(el.value));
|
|
83
|
+
setDraft(null);
|
|
84
|
+
};
|
|
85
|
+
el.addEventListener("change", done);
|
|
86
|
+
return () => el.removeEventListener("change", done);
|
|
87
|
+
}, []);
|
|
88
|
+
const shown = draft ?? value;
|
|
89
|
+
return /* @__PURE__ */ jsxs2("label", { className: "flex flex-col gap-0.5", "data-path": control.path, children: [
|
|
90
|
+
/* @__PURE__ */ jsxs2("span", { className: "flex justify-between text-slate-400", children: [
|
|
91
|
+
/* @__PURE__ */ jsx2("span", { children: control.label }),
|
|
92
|
+
/* @__PURE__ */ jsx2("span", { className: "font-mono", children: fmt(control, shown) })
|
|
93
|
+
] }),
|
|
94
|
+
/* @__PURE__ */ jsx2(
|
|
95
|
+
"input",
|
|
96
|
+
{
|
|
97
|
+
ref,
|
|
98
|
+
type: "range",
|
|
99
|
+
"aria-label": control.label,
|
|
100
|
+
min: control.min,
|
|
101
|
+
max: control.max,
|
|
102
|
+
step: control.step,
|
|
103
|
+
value: shown,
|
|
104
|
+
onChange: (e) => {
|
|
105
|
+
const v = Number(e.target.value);
|
|
106
|
+
if (onCommit) setDraft(v);
|
|
107
|
+
onChange?.(v);
|
|
108
|
+
},
|
|
109
|
+
className: "w-full accent-indigo-500"
|
|
110
|
+
}
|
|
111
|
+
)
|
|
112
|
+
] });
|
|
113
|
+
};
|
|
114
|
+
function useAttempt() {
|
|
115
|
+
const [error, setError] = useState2(null);
|
|
116
|
+
const attempt = (fn) => {
|
|
117
|
+
try {
|
|
118
|
+
setError(null);
|
|
119
|
+
const r = fn();
|
|
120
|
+
if (r && typeof r.catch === "function") r.catch((e) => setError(e instanceof Error ? e.message : String(e)));
|
|
121
|
+
} catch (e) {
|
|
122
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return [error, attempt, setError];
|
|
126
|
+
}
|
|
127
|
+
var ErrorLine = ({ error }) => error ? /* @__PURE__ */ jsx2("p", { role: "alert", className: "text-[11px] text-rose-400", children: error }) : null;
|
|
128
|
+
var CommitColorInput = ({ value, label, onCommit, className }) => {
|
|
129
|
+
const ref = useRef2(null);
|
|
130
|
+
const commit = useRef2(onCommit);
|
|
131
|
+
commit.current = onCommit;
|
|
132
|
+
useEffect2(() => {
|
|
133
|
+
const el = ref.current;
|
|
134
|
+
if (!el) return;
|
|
135
|
+
const done = () => commit.current(el.value);
|
|
136
|
+
el.addEventListener("change", done);
|
|
137
|
+
return () => el.removeEventListener("change", done);
|
|
138
|
+
}, []);
|
|
139
|
+
return /* @__PURE__ */ jsx2("input", { ref, type: "color", "aria-label": label, defaultValue: value, className }, value);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/brushes/BrushEditor.tsx
|
|
143
|
+
import { useMemo as useMemo2, useState as useState3 } from "react";
|
|
144
|
+
import { brushEditorScope, brushEditorSections, PAINT_PROFILE, setBrushEditorValue } from "@pixerate/editor/brushes";
|
|
145
|
+
import { Fragment, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
146
|
+
var BrushEditor = ({ brush, profile = PAINT_PROFILE, onChange, preview, previewBackground, onSave, saveLabel = "Save", onSaveAs, renderExtra, className }) => {
|
|
147
|
+
const sections = useMemo2(() => brushEditorSections(brush, profile), [brush, profile]);
|
|
148
|
+
const scope = useMemo2(() => brushEditorScope(brush, profile), [brush, profile]);
|
|
149
|
+
const [problems, setProblems] = useState3(null);
|
|
150
|
+
const [name, setName] = useState3("");
|
|
151
|
+
const save = async (fn, after) => {
|
|
152
|
+
try {
|
|
153
|
+
await fn();
|
|
154
|
+
after?.();
|
|
155
|
+
setProblems(null);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
setProblems(e.message);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const set = (path, value) => {
|
|
161
|
+
const r = setBrushEditorValue(brush, path, value);
|
|
162
|
+
setProblems(r.problems.length ? r.problems.join(" ") : null);
|
|
163
|
+
if (r.brush !== brush) onChange(r.brush);
|
|
164
|
+
};
|
|
165
|
+
return /* @__PURE__ */ jsxs3("div", { className: `flex flex-col gap-2 ${className ?? ""}`, "data-testid": "brush-editor", children: [
|
|
166
|
+
typeof preview === "string" ? /* @__PURE__ */ jsx3("img", { src: preview, alt: "Brush preview", className: "rounded object-contain", style: { width: "100%", aspectRatio: "4 / 1", ...previewBackground ? { background: previewBackground } : {} } }) : preview !== void 0 && /* @__PURE__ */ jsx3(PixelCanvas, { pixels: preview, label: "Brush preview", className: "rounded", style: { width: "100%", aspectRatio: preview ? `${preview.width} / ${preview.height}` : "4 / 1" } }),
|
|
167
|
+
scope.note && /* @__PURE__ */ jsx3("p", { className: "text-[10px] text-amber-400", "data-testid": "brush-editor-scope", title: `${profile.name} shows only what it can use; these are kept unchanged when you save.`, children: scope.note }),
|
|
168
|
+
sections.map((s) => /* @__PURE__ */ jsxs3("section", { className: "flex flex-col gap-1.5", "data-section": s.id, ...s.id === "behaviour" ? { "data-testid": "brush-engine" } : {}, children: [
|
|
169
|
+
/* @__PURE__ */ jsx3("span", { className: "font-medium text-slate-300", children: s.title }),
|
|
170
|
+
s.controls.map((c) => /* @__PURE__ */ jsx3(EditorControl, { control: c, onSet: set }, c.path)),
|
|
171
|
+
renderExtra?.(s.id)
|
|
172
|
+
] }, s.id)),
|
|
173
|
+
(onSave || onSaveAs) && /* @__PURE__ */ jsxs3("div", { className: "flex flex-wrap items-center gap-1", children: [
|
|
174
|
+
onSave && /* @__PURE__ */ jsx3("button", { type: "button", className: button, onClick: () => save(onSave), children: saveLabel }),
|
|
175
|
+
onSaveAs && /* @__PURE__ */ jsxs3(Fragment, { children: [
|
|
176
|
+
/* @__PURE__ */ jsx3("input", { value: name, placeholder: "Brush name", "aria-label": "Brush name", onChange: (e) => setName(e.target.value), className: "w-28 rounded bg-slate-800 px-1.5 py-0.5" }),
|
|
177
|
+
/* @__PURE__ */ jsx3("button", { type: "button", className: button, disabled: !name.trim(), onClick: () => save(() => onSaveAs(name.trim()), () => setName("")), children: "Save brush" })
|
|
178
|
+
] })
|
|
179
|
+
] }),
|
|
180
|
+
/* @__PURE__ */ jsx3(ErrorLine, { error: problems })
|
|
181
|
+
] });
|
|
182
|
+
};
|
|
183
|
+
var EditorControl = ({ control: c, onSet }) => {
|
|
184
|
+
const note = c.note ? /* @__PURE__ */ jsx3("span", { className: "text-[10px] text-amber-400", children: c.note }) : null;
|
|
185
|
+
switch (c.kind) {
|
|
186
|
+
case "slider":
|
|
187
|
+
return /* @__PURE__ */ jsxs3("div", { title: c.note, children: [
|
|
188
|
+
/* @__PURE__ */ jsx3(PaintSlider, { control: c, value: c.value, onChange: (v) => onSet(c.path, v) }),
|
|
189
|
+
note
|
|
190
|
+
] });
|
|
191
|
+
case "choice":
|
|
192
|
+
return /* @__PURE__ */ jsxs3("label", { className: "flex items-center justify-between gap-2", title: c.note, children: [
|
|
193
|
+
/* @__PURE__ */ jsx3("span", { className: "text-slate-400", children: c.label }),
|
|
194
|
+
/* @__PURE__ */ jsx3("select", { "aria-label": c.ariaLabel, value: c.value, onChange: (e) => onSet(c.path, e.target.value), className: "max-w-[60%] rounded bg-slate-800 px-1 py-0.5", children: c.options.map((o) => /* @__PURE__ */ jsx3("option", { value: o.value, children: o.label }, o.value)) })
|
|
195
|
+
] });
|
|
196
|
+
case "toggle":
|
|
197
|
+
return /* @__PURE__ */ jsxs3("label", { className: "flex items-center gap-2", title: c.note, children: [
|
|
198
|
+
/* @__PURE__ */ jsx3("input", { type: "checkbox", "aria-label": c.ariaLabel, checked: c.value, onChange: (e) => onSet(c.path, e.target.checked) }),
|
|
199
|
+
" ",
|
|
200
|
+
c.label
|
|
201
|
+
] });
|
|
202
|
+
case "text":
|
|
203
|
+
return /* @__PURE__ */ jsxs3("label", { className: "flex items-center justify-between gap-2", children: [
|
|
204
|
+
/* @__PURE__ */ jsx3("span", { className: "text-slate-400", children: c.label }),
|
|
205
|
+
/* @__PURE__ */ jsx3("input", { "aria-label": c.ariaLabel, defaultValue: c.value, placeholder: c.placeholder, onBlur: (e) => e.target.value !== c.value && onSet(c.path, e.target.value.trim().toLowerCase()), className: "w-28 rounded bg-slate-800 px-1.5 py-0.5" }, c.value)
|
|
206
|
+
] });
|
|
207
|
+
case "colour": {
|
|
208
|
+
const set = (light, dark) => onSet(c.path, dark === null ? light : { light, dark });
|
|
209
|
+
return /* @__PURE__ */ jsxs3("div", { className: "flex flex-col gap-1", children: [
|
|
210
|
+
/* @__PURE__ */ jsxs3("div", { className: "flex items-center justify-between gap-2", children: [
|
|
211
|
+
/* @__PURE__ */ jsx3("span", { className: "text-slate-400", children: c.label }),
|
|
212
|
+
/* @__PURE__ */ jsxs3("span", { className: "flex items-center gap-1", children: [
|
|
213
|
+
/* @__PURE__ */ jsx3("input", { type: "color", "aria-label": c.ariaLabel, title: c.dark !== null ? "On light backgrounds" : void 0, value: c.value ?? "#000000", onChange: (e) => set(e.target.value, c.dark), className: "h-5 w-8" }),
|
|
214
|
+
c.dark !== null && /* @__PURE__ */ jsx3("input", { type: "color", "aria-label": `${c.ariaLabel} (dark)`, title: "On dark backgrounds", value: c.dark, onChange: (e) => set(c.value ?? "#000000", e.target.value), className: "h-5 w-8" }),
|
|
215
|
+
(c.value || c.gradient) && /* @__PURE__ */ jsx3("button", { type: "button", className: button, onClick: () => onSet(c.path, void 0), children: "None" })
|
|
216
|
+
] })
|
|
217
|
+
] }),
|
|
218
|
+
c.gradient ? /* @__PURE__ */ jsx3("span", { className: "text-[10px] text-slate-500", children: "A gradient (its own paint)." }) : /* @__PURE__ */ jsxs3("label", { className: "flex items-center gap-2", children: [
|
|
219
|
+
/* @__PURE__ */ jsx3("input", { type: "checkbox", "aria-label": `${c.ariaLabel}: light and dark`, checked: c.dark !== null, onChange: (e) => set(c.value ?? "#000000", e.target.checked ? c.value ?? "#ffffff" : null) }),
|
|
220
|
+
" Light / dark"
|
|
221
|
+
] })
|
|
222
|
+
] });
|
|
223
|
+
}
|
|
224
|
+
case "palette": {
|
|
225
|
+
const at = (i, entry) => onSet(c.path, c.value.map((x, j) => j === i ? entry : x));
|
|
226
|
+
return /* @__PURE__ */ jsxs3("div", { className: "flex flex-col gap-1", role: "group", "aria-label": c.ariaLabel, children: [
|
|
227
|
+
/* @__PURE__ */ jsx3("span", { className: "text-slate-400", children: c.label }),
|
|
228
|
+
/* @__PURE__ */ jsxs3("div", { className: "flex flex-wrap items-center gap-1", children: [
|
|
229
|
+
c.value.map((col, i) => {
|
|
230
|
+
const light = typeof col === "string" ? col : col.light;
|
|
231
|
+
return /* @__PURE__ */ jsxs3("span", { className: "flex items-center gap-0.5 rounded bg-slate-900 p-0.5", children: [
|
|
232
|
+
/* @__PURE__ */ jsx3("input", { type: "color", "aria-label": `Palette colour ${i + 1}`, value: light, onChange: (e) => at(i, typeof col === "string" ? e.target.value : { ...col, light: e.target.value }), className: "h-4 w-5" }),
|
|
233
|
+
typeof col !== "string" && /* @__PURE__ */ jsx3("input", { type: "color", "aria-label": `Palette colour ${i + 1} (dark)`, value: col.dark, onChange: (e) => at(i, { ...col, dark: e.target.value }), className: "h-4 w-5" }),
|
|
234
|
+
/* @__PURE__ */ jsx3("button", { type: "button", "aria-label": `Remove palette colour ${i + 1}`, className: "px-0.5 text-slate-400 hover:text-white", onClick: () => onSet(c.path, c.value.filter((_, j) => j !== i)), children: "\xD7" })
|
|
235
|
+
] }, i);
|
|
236
|
+
}),
|
|
237
|
+
c.value.length < c.max && /* @__PURE__ */ jsxs3(Fragment, { children: [
|
|
238
|
+
/* @__PURE__ */ jsx3("input", { type: "color", "aria-label": "Add a palette colour", onChange: (e) => onSet(c.path, [...c.value, e.target.value]), className: "h-4 w-6" }),
|
|
239
|
+
/* @__PURE__ */ jsx3("button", { type: "button", className: button, onClick: () => onSet(c.path, [...c.value, { light: "#334155", dark: "#e2e8f0" }]), children: "Add light / dark pair" })
|
|
240
|
+
] })
|
|
241
|
+
] })
|
|
242
|
+
] });
|
|
243
|
+
}
|
|
244
|
+
case "stamps":
|
|
245
|
+
return /* @__PURE__ */ jsx3("div", { className: "flex flex-wrap gap-1", role: "group", "aria-label": c.ariaLabel, title: `Up to ${c.max}`, children: c.options.map((o) => {
|
|
246
|
+
const on = c.value.includes(o.value);
|
|
247
|
+
return /* @__PURE__ */ jsx3("button", { type: "button", "aria-pressed": on, className: chip(on), onClick: () => onSet(c.path, on ? c.value.filter((v) => v !== o.value) : [...c.value, o.value].slice(-c.max)), children: o.label }, o.value);
|
|
248
|
+
}) });
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export {
|
|
253
|
+
BrushLibraryBrowser,
|
|
254
|
+
chip,
|
|
255
|
+
button,
|
|
256
|
+
panel,
|
|
257
|
+
PixelCanvas,
|
|
258
|
+
PaintSlider,
|
|
259
|
+
useAttempt,
|
|
260
|
+
ErrorLine,
|
|
261
|
+
CommitColorInput,
|
|
262
|
+
BrushEditor
|
|
263
|
+
};
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { ImageEditorController } from '@pixerate/editor/image-editor';
|
|
3
|
+
import { type BrushPreviewRenderer } from '@pixerate/editor/brushes';
|
|
3
4
|
export interface BrushOptionsBarProps {
|
|
4
5
|
controller: ImageEditorController;
|
|
5
6
|
/** The row of paint tool buttons (default true); hide it when the host has its own toolbar. */
|
|
6
7
|
showTools?: boolean;
|
|
8
|
+
/** Draws the brush editor's preview stroke (default: the controller's `brushPreview` option). */
|
|
9
|
+
renderBrushPreview?: BrushPreviewRenderer;
|
|
7
10
|
className?: string;
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
13
|
* The paint tools: Move / Brush / Mixer / Eraser, brush presets from the editor's brush library
|
|
11
|
-
* (`BrushLibraryBrowser` with `IMAGE_PROFILE`: choosing one sets the tool and brush)
|
|
14
|
+
* (`BrushLibraryBrowser` with `IMAGE_PROFILE`: choosing one sets the tool and brush) and "Edit brush…"
|
|
15
|
+
* (`BrushLibraryEditor` with `IMAGE_PROFILE`: saves go to the library and the saved brush is
|
|
16
|
+
* picked), the brush's size, hardness, opacity, flow and
|
|
12
17
|
* smoothing, pressure toggles, the foreground and background colours (D resets, X swaps) and
|
|
13
18
|
* Edit › Fill / Clear on the active layer, and gradient paint for the brush (the colour changes as
|
|
14
19
|
* the stroke goes; `PaintGradientEditor`). With the Mixer Brush: its load (Load brush / Clean
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { ImageEditorController } from '@pixerate/editor/image-editor';
|
|
3
|
+
import { type BrushPreviewRenderer } from '@pixerate/editor/brushes';
|
|
3
4
|
export interface BrushOptionsBarProps {
|
|
4
5
|
controller: ImageEditorController;
|
|
5
6
|
/** The row of paint tool buttons (default true); hide it when the host has its own toolbar. */
|
|
6
7
|
showTools?: boolean;
|
|
8
|
+
/** Draws the brush editor's preview stroke (default: the controller's `brushPreview` option). */
|
|
9
|
+
renderBrushPreview?: BrushPreviewRenderer;
|
|
7
10
|
className?: string;
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
13
|
* The paint tools: Move / Brush / Mixer / Eraser, brush presets from the editor's brush library
|
|
11
|
-
* (`BrushLibraryBrowser` with `IMAGE_PROFILE`: choosing one sets the tool and brush)
|
|
14
|
+
* (`BrushLibraryBrowser` with `IMAGE_PROFILE`: choosing one sets the tool and brush) and "Edit brush…"
|
|
15
|
+
* (`BrushLibraryEditor` with `IMAGE_PROFILE`: saves go to the library and the saved brush is
|
|
16
|
+
* picked), the brush's size, hardness, opacity, flow and
|
|
12
17
|
* smoothing, pressure toggles, the foreground and background colours (D resets, X swaps) and
|
|
13
18
|
* Edit › Fill / Clear on the active layer, and gradient paint for the brush (the colour changes as
|
|
14
19
|
* the stroke goes; `PaintGradientEditor`). With the Mixer Brush: its load (Load brush / Clean
|