@pixerate/editor-react 0.103.0 → 0.104.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.
@@ -106,14 +106,20 @@ function useAutosave(options) {
106
106
  }
107
107
 
108
108
  // src/persistence/useEditorAutosave.ts
109
- import { useCallback as useCallback2 } from "react";
109
+ import { useCallback as useCallback2, useMemo } from "react";
110
110
  import {
111
111
  parseEditorDocument,
112
- toEditorDocumentFile
112
+ toEditorDocumentFile,
113
+ withEditorAssets
113
114
  } from "@pixerate/editor/persistence";
114
115
  function useEditorAutosave(doc, options) {
116
+ const storage = useMemo(
117
+ () => doc.assets ? withEditorAssets(options.storage, doc.assets) : options.storage,
118
+ [options.storage, doc.assets]
119
+ );
115
120
  const autosave = useAutosave({
116
121
  ...options,
122
+ storage,
117
123
  key: options.key ?? doc.kind,
118
124
  getSnapshot: () => toEditorDocumentFile(doc),
119
125
  deserialize: (text) => parseEditorDocument(text, { kind: doc.kind }),
@@ -17,6 +17,7 @@ import {
17
17
  import { useRef, useState, useEffect, useCallback, useMemo } from "react";
18
18
  import {
19
19
  ImageEditorController,
20
+ createImageEditorDocument,
20
21
  createImageEffectsToolProvider
21
22
  } from "@pixerate/editor/image-editor";
22
23
  function useImageEditor(options = {}) {
@@ -119,9 +120,12 @@ function useImageEditor(options = {}) {
119
120
  (newDataUrl, options2) => controller.applyBackgroundRemovedImage(newDataUrl, options2),
120
121
  [controller]
121
122
  );
123
+ const editorDocument = useMemo(() => createImageEditorDocument(controller), [controller]);
124
+ useEffect(() => () => editorDocument.destroy(), [editorDocument]);
122
125
  return useMemo(
123
126
  () => ({
124
127
  controller,
128
+ editorDocument,
125
129
  state,
126
130
  canUndo: controller.canUndo,
127
131
  canRedo: controller.canRedo,
@@ -170,7 +174,7 @@ function useImageEditor(options = {}) {
170
174
  applyBackgroundRemovedImage
171
175
  }),
172
176
  // eslint-disable-next-line react-hooks/exhaustive-deps
173
- [controller, state, historyTicks, histogram]
177
+ [controller, editorDocument, state, historyTicks, histogram]
174
178
  );
175
179
  }
176
180
  function useImageEffectsTools(registry, controller, options = {}) {
@@ -158,9 +158,12 @@ function useImageEditor(options = {}) {
158
158
  (newDataUrl, options2) => controller.applyBackgroundRemovedImage(newDataUrl, options2),
159
159
  [controller]
160
160
  );
161
+ const editorDocument = (0, import_react.useMemo)(() => (0, import_image_editor.createImageEditorDocument)(controller), [controller]);
162
+ (0, import_react.useEffect)(() => () => editorDocument.destroy(), [editorDocument]);
161
163
  return (0, import_react.useMemo)(
162
164
  () => ({
163
165
  controller,
166
+ editorDocument,
164
167
  state,
165
168
  canUndo: controller.canUndo,
166
169
  canRedo: controller.canRedo,
@@ -209,7 +212,7 @@ function useImageEditor(options = {}) {
209
212
  applyBackgroundRemovedImage
210
213
  }),
211
214
  // eslint-disable-next-line react-hooks/exhaustive-deps
212
- [controller, state, historyTicks, histogram]
215
+ [controller, editorDocument, state, historyTicks, histogram]
213
216
  );
214
217
  }
215
218
  function useImageEffectsTools(registry, controller, options = {}) {
@@ -20,7 +20,7 @@ import {
20
20
  useImageEditor,
21
21
  useImageEffectsTools,
22
22
  useImageHistogram
23
- } from "../chunk-GJ2TYERJ.js";
23
+ } from "../chunk-CEV4OEOY.js";
24
24
  import "../chunk-AJCWF6SN.js";
25
25
  import "../chunk-Q7LAFHGP.js";
26
26
  import "../chunk-5LFQ2UQZ.js";
@@ -1,7 +1,10 @@
1
1
  import type { ToolRegistry } from '@pixerate/editor/tools';
2
2
  import { ImageEditorController, type ImageEffectsToolProviderOptions, type ImageEditorOptions, type ImageEditorState, type ImageEditorTool, type CropBox, type AspectRatio, type ImageAdjustments, type DepthMaskOptions, type Annotation, type CreateAnnotationPayload, type ExportOptions, type SerializedImageEditorState, type AIResultOptions, type ImagePoint, type AdjustmentKind, type AdjustmentLayerInit, type AdjustmentLayerUpdate, type ImageHistogram } from '@pixerate/editor/image-editor';
3
+ import type { EditorDocument } from '@pixerate/editor/persistence';
3
4
  export interface UseImageEditorReturn {
4
5
  controller: ImageEditorController;
6
+ /** The save / load / undo contract, with layer pixels as asset references (for `useEditorAutosave` or `createDocumentToolProvider`). */
7
+ editorDocument: EditorDocument<SerializedImageEditorState>;
5
8
  state: ImageEditorState;
6
9
  canUndo: boolean;
7
10
  canRedo: boolean;
@@ -1,7 +1,10 @@
1
1
  import type { ToolRegistry } from '@pixerate/editor/tools';
2
2
  import { ImageEditorController, type ImageEffectsToolProviderOptions, type ImageEditorOptions, type ImageEditorState, type ImageEditorTool, type CropBox, type AspectRatio, type ImageAdjustments, type DepthMaskOptions, type Annotation, type CreateAnnotationPayload, type ExportOptions, type SerializedImageEditorState, type AIResultOptions, type ImagePoint, type AdjustmentKind, type AdjustmentLayerInit, type AdjustmentLayerUpdate, type ImageHistogram } from '@pixerate/editor/image-editor';
3
+ import type { EditorDocument } from '@pixerate/editor/persistence';
3
4
  export interface UseImageEditorReturn {
4
5
  controller: ImageEditorController;
6
+ /** The save / load / undo contract, with layer pixels as asset references (for `useEditorAutosave` or `createDocumentToolProvider`). */
7
+ editorDocument: EditorDocument<SerializedImageEditorState>;
5
8
  state: ImageEditorState;
6
9
  canUndo: boolean;
7
10
  canRedo: boolean;
package/dist/index.cjs CHANGED
@@ -3960,9 +3960,12 @@ function useImageEditor(options = {}) {
3960
3960
  (newDataUrl, options2) => controller.applyBackgroundRemovedImage(newDataUrl, options2),
3961
3961
  [controller]
3962
3962
  );
3963
+ const editorDocument = (0, import_react40.useMemo)(() => (0, import_image_editor.createImageEditorDocument)(controller), [controller]);
3964
+ (0, import_react40.useEffect)(() => () => editorDocument.destroy(), [editorDocument]);
3963
3965
  return (0, import_react40.useMemo)(
3964
3966
  () => ({
3965
3967
  controller,
3968
+ editorDocument,
3966
3969
  state,
3967
3970
  canUndo: controller.canUndo,
3968
3971
  canRedo: controller.canRedo,
@@ -4011,7 +4014,7 @@ function useImageEditor(options = {}) {
4011
4014
  applyBackgroundRemovedImage
4012
4015
  }),
4013
4016
  // eslint-disable-next-line react-hooks/exhaustive-deps
4014
- [controller, state, historyTicks, histogram]
4017
+ [controller, editorDocument, state, historyTicks, histogram]
4015
4018
  );
4016
4019
  }
4017
4020
  function useImageEffectsTools(registry, controller, options = {}) {
@@ -10333,8 +10336,13 @@ function useAutosave(options) {
10333
10336
  var import_react72 = require("react");
10334
10337
  var import_persistence2 = require("@pixerate/editor/persistence");
10335
10338
  function useEditorAutosave(doc, options) {
10339
+ const storage = (0, import_react72.useMemo)(
10340
+ () => doc.assets ? (0, import_persistence2.withEditorAssets)(options.storage, doc.assets) : options.storage,
10341
+ [options.storage, doc.assets]
10342
+ );
10336
10343
  const autosave = useAutosave({
10337
10344
  ...options,
10345
+ storage,
10338
10346
  key: options.key ?? doc.kind,
10339
10347
  getSnapshot: () => (0, import_persistence2.toEditorDocumentFile)(doc),
10340
10348
  deserialize: (text) => (0, import_persistence2.parseEditorDocument)(text, { kind: doc.kind }),
package/dist/index.js CHANGED
@@ -44,7 +44,7 @@ import {
44
44
  useImageEditor,
45
45
  useImageEffectsTools,
46
46
  useImageHistogram
47
- } from "./chunk-GJ2TYERJ.js";
47
+ } from "./chunk-CEV4OEOY.js";
48
48
  import "./chunk-AJCWF6SN.js";
49
49
  import "./chunk-Q7LAFHGP.js";
50
50
  import "./chunk-5LFQ2UQZ.js";
@@ -66,7 +66,7 @@ import {
66
66
  RecoveryPrompt,
67
67
  useAutosave,
68
68
  useEditorAutosave
69
- } from "./chunk-R7MEGRPV.js";
69
+ } from "./chunk-5YTP5RVI.js";
70
70
  import {
71
71
  AssetMentionChips,
72
72
  GenerationHistory,
@@ -58,7 +58,9 @@ var import_paint_editor = require("@pixerate/editor/paint-editor");
58
58
  function usePaintEditor(options = {}) {
59
59
  const [controller] = (0, import_react.useState)(() => options instanceof import_paint_editor.PaintEditorController ? options : new import_paint_editor.PaintEditorController(options));
60
60
  const state = usePaintEditorState(controller);
61
- return (0, import_react.useMemo)(() => ({ controller, state }), [controller, state]);
61
+ const editorDocument = (0, import_react.useMemo)(() => (0, import_paint_editor.createPaintEditorDocument)(controller), [controller]);
62
+ (0, import_react.useEffect)(() => () => editorDocument.destroy(), [editorDocument]);
63
+ return (0, import_react.useMemo)(() => ({ controller, state, editorDocument }), [controller, state, editorDocument]);
62
64
  }
63
65
  function usePaintEditorState(controller) {
64
66
  return (0, import_react.useSyncExternalStore)(
@@ -16,12 +16,14 @@ import {
16
16
  } from "../chunk-GNO54NFG.js";
17
17
 
18
18
  // src/paint-editor/usePaintEditor.ts
19
- import { useMemo, useState, useSyncExternalStore } from "react";
20
- import { PaintDirector, PaintEditorController, PaintPresence } from "@pixerate/editor/paint-editor";
19
+ import { useEffect, useMemo, useState, useSyncExternalStore } from "react";
20
+ import { PaintDirector, PaintEditorController, PaintPresence, createPaintEditorDocument } from "@pixerate/editor/paint-editor";
21
21
  function usePaintEditor(options = {}) {
22
22
  const [controller] = useState(() => options instanceof PaintEditorController ? options : new PaintEditorController(options));
23
23
  const state = usePaintEditorState(controller);
24
- return useMemo(() => ({ controller, state }), [controller, state]);
24
+ const editorDocument = useMemo(() => createPaintEditorDocument(controller), [controller]);
25
+ useEffect(() => () => editorDocument.destroy(), [editorDocument]);
26
+ return useMemo(() => ({ controller, state, editorDocument }), [controller, state, editorDocument]);
25
27
  }
26
28
  function usePaintEditorState(controller) {
27
29
  return useSyncExternalStore(
@@ -39,7 +41,7 @@ function usePaintDirector(controller, options = {}) {
39
41
  }
40
42
 
41
43
  // src/paint-editor/PaintCanvas.tsx
42
- import { useEffect, useMemo as useMemo2, useRef, useState as useState2, useSyncExternalStore as useSyncExternalStore2 } from "react";
44
+ import { useEffect as useEffect2, useMemo as useMemo2, useRef, useState as useState2, useSyncExternalStore as useSyncExternalStore2 } from "react";
43
45
  import {
44
46
  PaintCanvasPresenter,
45
47
  PaintGesture,
@@ -72,7 +74,7 @@ var PaintCanvas = ({ controller, tool = "brush", showGuides = true, fitOnMount =
72
74
  );
73
75
  gesture.tool = tool;
74
76
  const { canvas: doc } = state.document;
75
- useEffect(() => {
77
+ useEffect2(() => {
76
78
  const el = viewport.current;
77
79
  if (!el || typeof ResizeObserver === "undefined") return;
78
80
  const ro = new ResizeObserver(() => {
@@ -84,24 +86,24 @@ var PaintCanvas = ({ controller, tool = "brush", showGuides = true, fitOnMount =
84
86
  return () => ro.disconnect();
85
87
  }, []);
86
88
  const fitted = useRef(null);
87
- useEffect(() => {
89
+ useEffect2(() => {
88
90
  const key = `${doc.width}x${doc.height}`;
89
91
  if (!fitOnMount || fitted.current === key) return;
90
92
  if (typeof ResizeObserver !== "undefined" && !measured.current) return;
91
93
  fitted.current = key;
92
94
  controller.setView({ zoom: fitZoom(doc, size), pan: { x: 0, y: 0 } });
93
95
  }, [controller, doc, size, fitOnMount]);
94
- useEffect(() => {
96
+ useEffect2(() => {
95
97
  const ctx = canvas.current?.getContext("2d");
96
98
  if (ctx && typeof ctx.createImageData === "function") presenter.render(ctx);
97
99
  else controller.takeDirty();
98
100
  }, [state, presenter, controller]);
99
- useEffect(() => startWetClock(controller), [controller]);
100
- useEffect(() => {
101
+ useEffect2(() => startWetClock(controller), [controller]);
102
+ useEffect2(() => {
101
103
  const ctx = water.current?.getContext("2d");
102
104
  if (ctx && typeof ctx.createImageData === "function") drawWaterOverlay(controller, ctx);
103
105
  }, [state, controller]);
104
- useEffect(() => {
106
+ useEffect2(() => {
105
107
  const el = viewport.current;
106
108
  if (!el) return;
107
109
  const onWheel = (e) => {
@@ -265,14 +267,14 @@ import { useRef as useRef3, useState as useState4 } from "react";
265
267
  import { PAINT_TOOLS, SYMMETRY_MODES, decodeImageBlob, downloadBytes, quickStencil, timeLapseZip } from "@pixerate/editor/paint-editor";
266
268
 
267
269
  // src/paint-editor/shared.tsx
268
- import { useEffect as useEffect2, useRef as useRef2, useState as useState3 } from "react";
270
+ import { useEffect as useEffect3, useRef as useRef2, useState as useState3 } from "react";
269
271
  import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
270
272
  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"}`;
271
273
  var button = "px-2 py-1 rounded text-[11px] bg-slate-800 text-slate-200 hover:bg-slate-700 disabled:opacity-40";
272
274
  var panel = "flex flex-col gap-2 text-xs text-slate-200";
273
275
  var PixelCanvas = ({ pixels, label, className, style }) => {
274
276
  const ref = useRef2(null);
275
- useEffect2(() => {
277
+ useEffect3(() => {
276
278
  const canvas = ref.current;
277
279
  const ctx = canvas?.getContext("2d");
278
280
  if (!canvas || !ctx || !pixels || typeof ctx.createImageData !== "function") return;
@@ -290,7 +292,7 @@ var PaintSlider = ({ control, value, onChange, onCommit }) => {
290
292
  const ref = useRef2(null);
291
293
  const commit = useRef2(onCommit);
292
294
  commit.current = onCommit;
293
- useEffect2(() => {
295
+ useEffect3(() => {
294
296
  const el = ref.current;
295
297
  if (!el) return;
296
298
  const done = () => {
@@ -344,7 +346,7 @@ var CommitColorInput = ({ value, label, onCommit, className }) => {
344
346
  const ref = useRef2(null);
345
347
  const commit = useRef2(onCommit);
346
348
  commit.current = onCommit;
347
- useEffect2(() => {
349
+ useEffect3(() => {
348
350
  const el = ref.current;
349
351
  if (!el) return;
350
352
  const done = () => commit.current(el.value);
@@ -1226,12 +1228,12 @@ import { useRef as useRef7, useState as useState10 } from "react";
1226
1228
  import { PaintPresence as PaintPresence2, documentToScreen as documentToScreen2, screenToDocument } from "@pixerate/editor/paint-editor";
1227
1229
 
1228
1230
  // src/paint-editor/PaintEffectsPanel.tsx
1229
- import { useEffect as useEffect3, useRef as useRef6, useState as useState9 } from "react";
1231
+ import { useEffect as useEffect4, useRef as useRef6, useState as useState9 } from "react";
1230
1232
  import { createPaintEffectsToolProvider, PAPER_EFFECT_BLENDS } from "@pixerate/editor/paint-editor";
1231
1233
  import { jsx as jsx11, jsxs as jsxs10 } from "react/jsx-runtime";
1232
1234
  function usePaintEffectsProvider(controller) {
1233
1235
  const [provider, setProvider] = useState9(controller.effectsProvider);
1234
- useEffect3(() => {
1236
+ useEffect4(() => {
1235
1237
  setProvider(controller.effectsProvider);
1236
1238
  return controller.effects.subscribe(setProvider);
1237
1239
  }, [controller]);
@@ -1239,7 +1241,7 @@ function usePaintEffectsProvider(controller) {
1239
1241
  }
1240
1242
  function usePaintEffectsTools(registry, controller, options = {}) {
1241
1243
  const { id, namespace, readOnly, playbackSurface } = options;
1242
- useEffect3(() => {
1244
+ useEffect4(() => {
1243
1245
  if (!registry) return;
1244
1246
  return registry.register(createPaintEffectsToolProvider(controller, { id, namespace, readOnly, playbackSurface }));
1245
1247
  }, [registry, controller, id, namespace, readOnly, playbackSurface]);
@@ -1,8 +1,11 @@
1
- import { PaintDirector, PaintEditorController, PaintPresence, type PaintDirectorOptions, type PaintEditorOptions, type PaintEditorState } from '@pixerate/editor/paint-editor';
1
+ import { PaintDirector, PaintEditorController, PaintPresence, type PaintDirectorOptions, type PaintEditorOptions, type PaintEditorState, type SerializedPaintDocument } from '@pixerate/editor/paint-editor';
2
+ import type { EditorDocument } from '@pixerate/editor/persistence';
2
3
  export interface UsePaintEditorReturn {
3
4
  controller: PaintEditorController;
4
5
  /** The controller's snapshot (a new object after every change). */
5
6
  state: PaintEditorState;
7
+ /** The save / load / undo contract, with every image as an asset reference (for `useEditorAutosave` or `createDocumentToolProvider`). */
8
+ editorDocument: EditorDocument<SerializedPaintDocument>;
6
9
  }
7
10
  /**
8
11
  * Creates a `PaintEditorController` once (from `options`, read on the first render) or uses the
@@ -1,8 +1,11 @@
1
- import { PaintDirector, PaintEditorController, PaintPresence, type PaintDirectorOptions, type PaintEditorOptions, type PaintEditorState } from '@pixerate/editor/paint-editor';
1
+ import { PaintDirector, PaintEditorController, PaintPresence, type PaintDirectorOptions, type PaintEditorOptions, type PaintEditorState, type SerializedPaintDocument } from '@pixerate/editor/paint-editor';
2
+ import type { EditorDocument } from '@pixerate/editor/persistence';
2
3
  export interface UsePaintEditorReturn {
3
4
  controller: PaintEditorController;
4
5
  /** The controller's snapshot (a new object after every change). */
5
6
  state: PaintEditorState;
7
+ /** The save / load / undo contract, with every image as an asset reference (for `useEditorAutosave` or `createDocumentToolProvider`). */
8
+ editorDocument: EditorDocument<SerializedPaintDocument>;
6
9
  }
7
10
  /**
8
11
  * Creates a `PaintEditorController` once (from `options`, read on the first render) or uses the
@@ -134,8 +134,13 @@ function useAutosave(options) {
134
134
  var import_react2 = require("react");
135
135
  var import_persistence2 = require("@pixerate/editor/persistence");
136
136
  function useEditorAutosave(doc, options) {
137
+ const storage = (0, import_react2.useMemo)(
138
+ () => doc.assets ? (0, import_persistence2.withEditorAssets)(options.storage, doc.assets) : options.storage,
139
+ [options.storage, doc.assets]
140
+ );
137
141
  const autosave = useAutosave({
138
142
  ...options,
143
+ storage,
139
144
  key: options.key ?? doc.kind,
140
145
  getSnapshot: () => (0, import_persistence2.toEditorDocumentFile)(doc),
141
146
  deserialize: (text) => (0, import_persistence2.parseEditorDocument)(text, { kind: doc.kind }),
@@ -2,7 +2,7 @@ import {
2
2
  RecoveryPrompt,
3
3
  useAutosave,
4
4
  useEditorAutosave
5
- } from "../chunk-R7MEGRPV.js";
5
+ } from "../chunk-5YTP5RVI.js";
6
6
  export {
7
7
  RecoveryPrompt,
8
8
  useAutosave,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixerate/editor-react",
3
- "version": "0.103.0",
3
+ "version": "0.104.0",
4
4
  "description": "React bindings and prompt/rich-text components for @pixerate/editor",
5
5
  "author": "Pixerate",
6
6
  "license": "MIT",
@@ -245,7 +245,7 @@
245
245
  },
246
246
  "peerDependencies": {
247
247
  "@dagrejs/dagre": "^1.1.8",
248
- "@pixerate/editor": ">=0.154.0 <1.0.0-0",
248
+ "@pixerate/editor": ">=0.155.0 <1.0.0-0",
249
249
  "@tiptap/core": "^2.27.3",
250
250
  "@tiptap/pm": "^2.27.3",
251
251
  "@tiptap/react": "^2.27.3",
@@ -272,7 +272,7 @@
272
272
  "tsup": "^9.0.0",
273
273
  "typescript": "^5.7.3",
274
274
  "vitest": "^5.0.5",
275
- "@pixerate/editor": "0.154.0"
275
+ "@pixerate/editor": "0.155.0"
276
276
  },
277
277
  "scripts": {
278
278
  "build": "tsup && tsc -p tsconfig.build.json && node ../../scripts/finalize-declarations.mjs dist",