@pixerate/editor-react 0.91.0 → 0.92.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.
Files changed (33) hide show
  1. package/dist/canvas/AgentPresenceLayer.d.cts +3 -0
  2. package/dist/canvas/AgentPresenceLayer.d.ts +3 -0
  3. package/dist/canvas/index.cjs +1032 -893
  4. package/dist/canvas/index.d.cts +2 -0
  5. package/dist/canvas/index.d.ts +2 -0
  6. package/dist/canvas/index.js +125 -74
  7. package/dist/canvas/useCanvasPlaybackSurface.d.cts +28 -0
  8. package/dist/canvas/useCanvasPlaybackSurface.d.ts +28 -0
  9. package/dist/{chunk-SMMO7CZB.js → chunk-6MDTFAPY.js} +84 -2
  10. package/dist/chunk-ECBC4HD7.js +95 -0
  11. package/dist/hooks/useAgentToolProvider.d.cts +6 -2
  12. package/dist/hooks/useAgentToolProvider.d.ts +6 -2
  13. package/dist/image-editor/ImageEditor.d.cts +10 -0
  14. package/dist/image-editor/ImageEditor.d.ts +10 -0
  15. package/dist/image-editor/index.cjs +1027 -864
  16. package/dist/image-editor/index.js +2 -1
  17. package/dist/index.cjs +1986 -1810
  18. package/dist/index.d.cts +1 -0
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.js +16 -2
  21. package/dist/playback/AgentPlaybackContext.d.cts +9 -0
  22. package/dist/playback/AgentPlaybackContext.d.ts +9 -0
  23. package/dist/playback/PresenceLayer.d.cts +28 -0
  24. package/dist/playback/PresenceLayer.d.ts +28 -0
  25. package/dist/playback/index.d.cts +3 -0
  26. package/dist/playback/index.d.ts +3 -0
  27. package/dist/playback/usePlaybackSurface.d.cts +9 -0
  28. package/dist/playback/usePlaybackSurface.d.ts +9 -0
  29. package/dist/vector/VectorCanvas.d.cts +11 -1
  30. package/dist/vector/VectorCanvas.d.ts +11 -1
  31. package/dist/vector/index.cjs +945 -826
  32. package/dist/vector/index.js +42 -3
  33. package/package.json +13 -13
@@ -1,3 +1,8 @@
1
+ import {
2
+ PresenceLayer,
3
+ useAgentPlayback,
4
+ usePlaybackSurface
5
+ } from "./chunk-ECBC4HD7.js";
1
6
  import {
2
7
  PaintGradientEditorView
3
8
  } from "./chunk-AJCWF6SN.js";
@@ -180,6 +185,8 @@ function useImageEffectsTools(registry, controller, options = {}) {
180
185
  import React14, { useRef as useRef5, useEffect as useEffect12, useState as useState15, useCallback as useCallback2 } from "react";
181
186
  import {
182
187
  canvasToImagePoint,
188
+ imageToCanvasPoint,
189
+ createImagePlaybackLocator,
183
190
  hitTestCrop,
184
191
  getCropCursor,
185
192
  calculateCropDrag,
@@ -2364,7 +2371,10 @@ var ImageEditor = ({
2364
2371
  onInpaint,
2365
2372
  onRemoveBackground,
2366
2373
  aiHooks,
2367
- effects
2374
+ effects,
2375
+ playback,
2376
+ playbackId = "image",
2377
+ onPlaybackReveal
2368
2378
  }) => {
2369
2379
  const internalEditor = useImageEditor({ image: src });
2370
2380
  const editor = externalEditor || internalEditor;
@@ -2377,6 +2387,68 @@ var ImageEditor = ({
2377
2387
  const canvasRef = useRef5(null);
2378
2388
  const containerRef = useRef5(null);
2379
2389
  const [activeTab, setActiveTab] = useState15("crop");
2390
+ const rootRef = useRef5(null);
2391
+ const activeTabRef = useRef5(activeTab);
2392
+ activeTabRef.current = activeTab;
2393
+ const ambientPlayback = useAgentPlayback();
2394
+ const playing = (playback === void 0 ? ambientPlayback : playback) ?? null;
2395
+ const [locateLayer] = useState15(() => createImagePlaybackLocator(controller));
2396
+ const docToRoot = (p) => {
2397
+ const canvas = canvasRef.current;
2398
+ const root = rootRef.current;
2399
+ if (!canvas || !root || !canvas.width) return p;
2400
+ const r = canvas.getBoundingClientRect();
2401
+ const box = root.getBoundingClientRect();
2402
+ const c = imageToCanvasPoint(p, controller.getState(), canvas.width, canvas.height, activeTabRef.current === "crop");
2403
+ return { x: r.left - box.left + c.x * r.width / canvas.width, y: r.top - box.top + c.y * r.height / canvas.height };
2404
+ };
2405
+ const clientToDoc = (p) => {
2406
+ const canvas = canvasRef.current;
2407
+ if (!canvas || !canvas.width) return p;
2408
+ const r = canvas.getBoundingClientRect();
2409
+ const sx = r.width > 0 ? canvas.width / r.width : 1;
2410
+ const sy = r.height > 0 ? canvas.height / r.height : 1;
2411
+ return canvasToImagePoint({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(), canvas.width, canvas.height, activeTabRef.current === "crop");
2412
+ };
2413
+ const elementRect = (selector) => {
2414
+ const el = rootRef.current?.querySelector(selector);
2415
+ const r = el?.getBoundingClientRect();
2416
+ if (!r || r.width <= 0) return null;
2417
+ const a = clientToDoc({ x: r.left, y: r.top });
2418
+ const b = clientToDoc({ x: r.right, y: r.bottom });
2419
+ return { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), width: Math.abs(b.x - a.x), height: Math.abs(b.y - a.y) };
2420
+ };
2421
+ const agentPresence = usePlaybackSurface(playing, playbackId, {
2422
+ defaultKind: "layer",
2423
+ locate: (ref) => {
2424
+ if (ref.kind === "panel") return elementRect(`[data-image-tab="${CSS.escape(ref.id)}"]`);
2425
+ if (ref.kind === "control") return elementRect(`[data-control-id="${CSS.escape(ref.id)}"]`);
2426
+ return locateLayer(ref) ?? (ref.kind === "layer" || ref.kind === "item" ? elementRect(`[data-layer-id="${CSS.escape(ref.id)}"]`) : null);
2427
+ },
2428
+ open: async (ref) => {
2429
+ if (ref.kind !== "panel" || activeTabRef.current === ref.id) return;
2430
+ rootRef.current?.querySelector(`[data-image-tab="${CSS.escape(ref.id)}"]`)?.click();
2431
+ await new Promise((resolve) => setTimeout(resolve, 60));
2432
+ },
2433
+ bounds: () => {
2434
+ const r = canvasRef.current?.getBoundingClientRect();
2435
+ if (!r || r.width <= 0) return null;
2436
+ const a = clientToDoc({ x: r.left, y: r.top });
2437
+ const b = clientToDoc({ x: r.right, y: r.bottom });
2438
+ return { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), width: Math.abs(b.x - a.x), height: Math.abs(b.y - a.y) };
2439
+ },
2440
+ visible: () => {
2441
+ const r = rootRef.current?.getBoundingClientRect();
2442
+ return !!r && r.width > 0 && r.height > 0 && !(typeof document !== "undefined" && document.visibilityState === "hidden");
2443
+ },
2444
+ toViewport: (p) => {
2445
+ const box = rootRef.current?.getBoundingClientRect();
2446
+ const q = docToRoot(p);
2447
+ return { x: (box?.left ?? 0) + q.x, y: (box?.top ?? 0) + q.y };
2448
+ },
2449
+ fromViewport: clientToDoc,
2450
+ reveal: () => onPlaybackReveal?.()
2451
+ });
2380
2452
  const [activeCropRatio, setActiveCropRatio] = useState15(null);
2381
2453
  const [selectionOptions, setSelectionOptions] = useState15(DEFAULT_SELECTION_TOOL_OPTIONS);
2382
2454
  const selectionOptionsRef = useRef5(selectionOptions);
@@ -3094,15 +3166,18 @@ var ImageEditor = ({
3094
3166
  return /* @__PURE__ */ jsxs15(
3095
3167
  "div",
3096
3168
  {
3097
- className: `flex flex-col bg-slate-900 border border-slate-800 rounded-xl overflow-hidden shadow-2xl text-slate-100 ${className}`,
3169
+ ref: rootRef,
3170
+ className: `relative flex flex-col bg-slate-900 border border-slate-800 rounded-xl overflow-hidden shadow-2xl text-slate-100 ${className}`,
3098
3171
  style,
3099
3172
  children: [
3173
+ playing && /* @__PURE__ */ jsx16(PresenceLayer, { presence: agentPresence, viewport: docToRoot }),
3100
3174
  /* @__PURE__ */ jsxs15("div", { className: "flex flex-wrap items-center justify-between px-4 py-3 bg-slate-950/80 border-b border-slate-800 gap-3", children: [
3101
3175
  /* @__PURE__ */ jsxs15("div", { className: "flex items-center gap-1 bg-slate-900 p-1 rounded-lg border border-slate-800 text-xs", children: [
3102
3176
  /* @__PURE__ */ jsx16(
3103
3177
  "button",
3104
3178
  {
3105
3179
  type: "button",
3180
+ "data-image-tab": "crop",
3106
3181
  onClick: () => {
3107
3182
  setActiveTab("crop");
3108
3183
  editor.setTool("crop");
@@ -3115,6 +3190,7 @@ var ImageEditor = ({
3115
3190
  "button",
3116
3191
  {
3117
3192
  type: "button",
3193
+ "data-image-tab": "adjust",
3118
3194
  onClick: () => {
3119
3195
  setActiveTab("adjust");
3120
3196
  editor.setTool("select");
@@ -3127,6 +3203,7 @@ var ImageEditor = ({
3127
3203
  "button",
3128
3204
  {
3129
3205
  type: "button",
3206
+ "data-image-tab": "layers",
3130
3207
  onClick: () => {
3131
3208
  setActiveTab("layers");
3132
3209
  editor.setTool("select");
@@ -3139,6 +3216,7 @@ var ImageEditor = ({
3139
3216
  "button",
3140
3217
  {
3141
3218
  type: "button",
3219
+ "data-image-tab": "paint",
3142
3220
  onClick: () => {
3143
3221
  setActiveTab("paint");
3144
3222
  editor.setTool("brush");
@@ -3151,6 +3229,7 @@ var ImageEditor = ({
3151
3229
  "button",
3152
3230
  {
3153
3231
  type: "button",
3232
+ "data-image-tab": "select",
3154
3233
  onClick: () => {
3155
3234
  setActiveTab("select");
3156
3235
  editor.setTool("select");
@@ -3163,6 +3242,7 @@ var ImageEditor = ({
3163
3242
  "button",
3164
3243
  {
3165
3244
  type: "button",
3245
+ "data-image-tab": "annotate",
3166
3246
  onClick: () => {
3167
3247
  setActiveTab("annotate");
3168
3248
  editor.setTool("pen");
@@ -3175,6 +3255,7 @@ var ImageEditor = ({
3175
3255
  "button",
3176
3256
  {
3177
3257
  type: "button",
3258
+ "data-image-tab": "depth",
3178
3259
  onClick: () => {
3179
3260
  setActiveTab("depth");
3180
3261
  editor.setTool("select");
@@ -3187,6 +3268,7 @@ var ImageEditor = ({
3187
3268
  "button",
3188
3269
  {
3189
3270
  type: "button",
3271
+ "data-image-tab": "ai",
3190
3272
  onClick: () => {
3191
3273
  setActiveTab("ai");
3192
3274
  editor.setTool("select");
@@ -0,0 +1,95 @@
1
+ // src/playback/PresenceLayer.tsx
2
+ import React, { useSyncExternalStore } from "react";
3
+ import { jsx, jsxs } from "react/jsx-runtime";
4
+ function PresenceLayer({ presence, viewport, className = "" }) {
5
+ const { actors } = useSyncExternalStore(presence.subscribe, presence.getState, presence.getState);
6
+ if (!actors.length) return null;
7
+ const map = typeof viewport === "function" ? viewport : viewport ? (p) => ({ x: p.x * viewport.zoom + viewport.x, y: p.y * viewport.zoom + viewport.y }) : (p) => p;
8
+ return /* @__PURE__ */ jsx(
9
+ "div",
10
+ {
11
+ className: `pixerate-presence ${className}`.trim(),
12
+ "aria-hidden": "true",
13
+ "data-testid": "presence-layer",
14
+ style: { position: "absolute", inset: 0, pointerEvents: "none", overflow: "hidden", zIndex: 50 },
15
+ children: actors.map((a) => {
16
+ const at = map(a);
17
+ const ring = a.focus && (() => {
18
+ const tl = map(a.focus);
19
+ const br = map({ x: a.focus.x + a.focus.width, y: a.focus.y + a.focus.height });
20
+ return { left: tl.x - 4, top: tl.y - 4, width: br.x - tl.x + 8, height: br.y - tl.y + 8 };
21
+ })();
22
+ return /* @__PURE__ */ jsxs(React.Fragment, { children: [
23
+ ring && /* @__PURE__ */ jsx(
24
+ "div",
25
+ {
26
+ "data-actor-focus": a.id,
27
+ style: { position: "absolute", ...ring, borderRadius: 8, border: `2px solid ${a.color}`, background: `${a.color}14`, transition: "all 160ms ease-out" }
28
+ }
29
+ ),
30
+ /* @__PURE__ */ jsxs("div", { "data-actor": a.id, style: { position: "absolute", left: 0, top: 0, transform: `translate(${at.x}px, ${at.y}px)` }, children: [
31
+ /* @__PURE__ */ jsx("svg", { width: "18", height: "20", viewBox: "0 0 12 18", style: { display: "block", transform: a.pressed ? "scale(0.85)" : void 0, transformOrigin: "0 0", filter: "drop-shadow(0 1px 2px rgba(0,0,0,.35))" }, children: /* @__PURE__ */ jsx("path", { d: "M0,0 L0,14 L4,10.5 L7,17 L9.5,16 L6.5,9.5 L11.5,9.5 Z", style: { fill: a.color, stroke: "#ffffff", strokeWidth: 1 } }) }),
32
+ /* @__PURE__ */ jsxs("span", { style: { position: "absolute", left: 14, top: 16, display: "flex", flexDirection: "column", alignItems: "flex-start", gap: 4 }, children: [
33
+ /* @__PURE__ */ jsx("span", { style: { whiteSpace: "nowrap", borderRadius: 999, padding: "1px 7px", fontSize: 11, fontWeight: 600, color: "#ffffff", background: a.color }, children: a.name }),
34
+ a.message && /* @__PURE__ */ jsx("span", { "data-actor-message": a.id, style: { width: "max-content", maxWidth: 240, borderRadius: 8, borderTopLeftRadius: 0, padding: "4px 8px", fontSize: 12, color: "#1e293b", background: "#ffffff", boxShadow: "0 4px 12px rgba(15,23,42,.18)" }, children: a.message })
35
+ ] })
36
+ ] })
37
+ ] }, a.id);
38
+ })
39
+ }
40
+ );
41
+ }
42
+
43
+ // src/playback/AgentPlaybackContext.tsx
44
+ import { createContext, useContext } from "react";
45
+ import { jsx as jsx2 } from "react/jsx-runtime";
46
+ var AgentPlaybackContext = createContext(null);
47
+ function AgentPlaybackProvider({ playback, children }) {
48
+ return /* @__PURE__ */ jsx2(AgentPlaybackContext.Provider, { value: playback ?? null, children });
49
+ }
50
+ function useAgentPlayback() {
51
+ return useContext(AgentPlaybackContext);
52
+ }
53
+
54
+ // src/playback/usePlaybackSurface.ts
55
+ import { useEffect, useRef, useState } from "react";
56
+ import { PresenceStore } from "@pixerate/editor/tools";
57
+ function usePlaybackSurface(playback, id, options = {}) {
58
+ const ambient = useAgentPlayback();
59
+ const target = playback === void 0 ? ambient : playback;
60
+ const [presence] = useState(() => new PresenceStore());
61
+ const latest = useRef(options);
62
+ latest.current = options;
63
+ useEffect(() => {
64
+ if (!target || !id) return;
65
+ const o = () => latest.current;
66
+ const surface = {
67
+ presence,
68
+ locate: (ref) => o().locate?.(ref) ?? null,
69
+ bounds: () => o().bounds?.() ?? null,
70
+ visible: () => o().visible?.() ?? true,
71
+ toViewport: (p) => o().toViewport?.(p) ?? p,
72
+ fromViewport: (p) => o().fromViewport?.(p) ?? p,
73
+ beginGesture: (key) => o().beginGesture?.(key),
74
+ endGesture: () => o().endGesture?.(),
75
+ reveal: () => o().reveal?.(),
76
+ open: (ref) => o().open?.(ref),
77
+ get defaultKind() {
78
+ return o().defaultKind;
79
+ }
80
+ };
81
+ const detach = target.attach(id, surface);
82
+ return () => {
83
+ detach();
84
+ for (const a of presence.getState().actors) presence.remove(a.id);
85
+ };
86
+ }, [target, id, presence]);
87
+ return presence;
88
+ }
89
+
90
+ export {
91
+ PresenceLayer,
92
+ AgentPlaybackProvider,
93
+ useAgentPlayback,
94
+ usePlaybackSurface
95
+ };
@@ -1,4 +1,8 @@
1
- import type { AgentToolProvider, ToolRegistry } from "@pixerate/editor/tools";
1
+ import type { AgentPlayback, AgentToolProvider, ToolRegistry } from "@pixerate/editor/tools";
2
+ export interface UseAgentToolProviderOptions {
3
+ /** Plays the registry's calls with this playback (installed once per registry). */
4
+ playback?: AgentPlayback | null;
5
+ }
2
6
  /**
3
7
  * Registers a tool provider with a `ToolRegistry` while the component is
4
8
  * mounted and unregisters it on unmount (the registry then announces the
@@ -8,4 +12,4 @@ import type { AgentToolProvider, ToolRegistry } from "@pixerate/editor/tools";
8
12
  * { id }), [editor])`): a new provider object re-registers it. Pass `null`
9
13
  * while it isn't ready.
10
14
  */
11
- export declare function useAgentToolProvider(registry: ToolRegistry | null | undefined, provider: AgentToolProvider | null | undefined): void;
15
+ export declare function useAgentToolProvider(registry: ToolRegistry | null | undefined, provider: AgentToolProvider | null | undefined, options?: UseAgentToolProviderOptions): void;
@@ -1,4 +1,8 @@
1
- import type { AgentToolProvider, ToolRegistry } from "@pixerate/editor/tools";
1
+ import type { AgentPlayback, AgentToolProvider, ToolRegistry } from "@pixerate/editor/tools";
2
+ export interface UseAgentToolProviderOptions {
3
+ /** Plays the registry's calls with this playback (installed once per registry). */
4
+ playback?: AgentPlayback | null;
5
+ }
2
6
  /**
3
7
  * Registers a tool provider with a `ToolRegistry` while the component is
4
8
  * mounted and unregisters it on unmount (the registry then announces the
@@ -8,4 +12,4 @@ import type { AgentToolProvider, ToolRegistry } from "@pixerate/editor/tools";
8
12
  * { id }), [editor])`): a new provider object re-registers it. Pass `null`
9
13
  * while it isn't ready.
10
14
  */
11
- export declare function useAgentToolProvider(registry: ToolRegistry | null | undefined, provider: AgentToolProvider | null | undefined): void;
15
+ export declare function useAgentToolProvider(registry: ToolRegistry | null | undefined, provider: AgentToolProvider | null | undefined, options?: UseAgentToolProviderOptions): void;
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import type { AgentPlayback } from '@pixerate/editor/tools';
2
3
  import type { ExportOptions, InpaintHook, RemoveBackgroundHook, ImageEditorAIHooks } from '@pixerate/editor/image-editor';
3
4
  import { type UseImageEditorReturn } from './useImageEditor.cjs';
4
5
  import type { EffectProvider } from '@pixerate/editor/effects';
@@ -18,5 +19,14 @@ export interface ImageEditorProps {
18
19
  * with an Effects section in the Layers tab. Optional: without one the editor is as before.
19
20
  */
20
21
  effects?: EffectProvider | null;
22
+ /**
23
+ * Agent playback: act out the image tools' calls here with the agent's cursor, opening the panel
24
+ * each tool belongs to (default: the nearest `AgentPlaybackProvider`; `null` turns it off).
25
+ */
26
+ playback?: AgentPlayback | null;
27
+ /** The playback surface id (default `image`, where the image tool providers play). */
28
+ playbackId?: string;
29
+ /** Bring the editor on screen (switch to its tab) before the agent acts. */
30
+ onPlaybackReveal?: () => void | Promise<void>;
21
31
  }
22
32
  export declare const ImageEditor: React.FC<ImageEditorProps>;
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import type { AgentPlayback } from '@pixerate/editor/tools';
2
3
  import type { ExportOptions, InpaintHook, RemoveBackgroundHook, ImageEditorAIHooks } from '@pixerate/editor/image-editor';
3
4
  import { type UseImageEditorReturn } from './useImageEditor.js';
4
5
  import type { EffectProvider } from '@pixerate/editor/effects';
@@ -18,5 +19,14 @@ export interface ImageEditorProps {
18
19
  * with an Effects section in the Layers tab. Optional: without one the editor is as before.
19
20
  */
20
21
  effects?: EffectProvider | null;
22
+ /**
23
+ * Agent playback: act out the image tools' calls here with the agent's cursor, opening the panel
24
+ * each tool belongs to (default: the nearest `AgentPlaybackProvider`; `null` turns it off).
25
+ */
26
+ playback?: AgentPlayback | null;
27
+ /** The playback surface id (default `image`, where the image tool providers play). */
28
+ playbackId?: string;
29
+ /** Bring the editor on screen (switch to its tab) before the agent acts. */
30
+ onPlaybackReveal?: () => void | Promise<void>;
21
31
  }
22
32
  export declare const ImageEditor: React.FC<ImageEditorProps>;