@pixerate/editor-react 0.99.0 → 0.100.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.
- package/dist/{chunk-EFF4YWJK.js → chunk-GJ2TYERJ.js} +70 -49
- package/dist/image-editor/index.cjs +68 -48
- package/dist/image-editor/index.js +1 -1
- package/dist/index.cjs +66 -48
- package/dist/index.js +1 -1
- package/package.json +5 -5
|
@@ -200,6 +200,7 @@ import {
|
|
|
200
200
|
canvasToImagePoint,
|
|
201
201
|
imageToCanvasPoint,
|
|
202
202
|
createImagePlaybackLocator,
|
|
203
|
+
agentDragDraft,
|
|
203
204
|
hitTestCrop,
|
|
204
205
|
getCropCursor,
|
|
205
206
|
calculateCropDrag,
|
|
@@ -211,11 +212,14 @@ import {
|
|
|
211
212
|
pointerPressure,
|
|
212
213
|
FreeTransformTool,
|
|
213
214
|
getViewportProjection,
|
|
215
|
+
measureCanvasViewport,
|
|
216
|
+
watchCanvasViewport,
|
|
214
217
|
ShapeDrawGesture,
|
|
215
218
|
TypeToolGesture,
|
|
216
219
|
DEFAULT_SHAPE_TOOL_OPTIONS,
|
|
217
220
|
DEFAULT_TYPE_TOOL_OPTIONS
|
|
218
221
|
} from "@pixerate/editor/image-editor";
|
|
222
|
+
import { trackPresenceDrag } from "@pixerate/editor/tools";
|
|
219
223
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
220
224
|
var ImageEditorCanvas = ({
|
|
221
225
|
editor,
|
|
@@ -245,6 +249,7 @@ var ImageEditorCanvas = ({
|
|
|
245
249
|
const canvasRef = useRef2(null);
|
|
246
250
|
const containerRef = useRef2(null);
|
|
247
251
|
const rootRef = useRef2(null);
|
|
252
|
+
const viewRef = useRef2({ width: 0, height: 0 });
|
|
248
253
|
const modeRef = useRef2(mode);
|
|
249
254
|
modeRef.current = mode;
|
|
250
255
|
const latest = useRef2({ onError, onTypeSessionChange, locate, locateElement, open, chromeRoot, onPlaybackReveal });
|
|
@@ -256,19 +261,19 @@ var ImageEditorCanvas = ({
|
|
|
256
261
|
const docToRoot = (p) => {
|
|
257
262
|
const canvas = canvasRef.current;
|
|
258
263
|
const root = rootRef.current;
|
|
259
|
-
if (!canvas || !root || !
|
|
264
|
+
if (!canvas || !root || !viewRef.current.width) return p;
|
|
260
265
|
const r = canvas.getBoundingClientRect();
|
|
261
266
|
const box = root.getBoundingClientRect();
|
|
262
|
-
const c = imageToCanvasPoint(p, controller.getState(),
|
|
263
|
-
return { x: r.left - box.left + c.x * r.width /
|
|
267
|
+
const c = imageToCanvasPoint(p, controller.getState(), viewRef.current.width, viewRef.current.height, modeRef.current === "crop");
|
|
268
|
+
return { x: r.left - box.left + c.x * r.width / viewRef.current.width, y: r.top - box.top + c.y * r.height / viewRef.current.height };
|
|
264
269
|
};
|
|
265
270
|
const clientToDoc = (p) => {
|
|
266
271
|
const canvas = canvasRef.current;
|
|
267
|
-
if (!canvas || !
|
|
272
|
+
if (!canvas || !viewRef.current.width) return p;
|
|
268
273
|
const r = canvas.getBoundingClientRect();
|
|
269
|
-
const sx = r.width > 0 ?
|
|
270
|
-
const sy = r.height > 0 ?
|
|
271
|
-
return canvasToImagePoint({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(),
|
|
274
|
+
const sx = r.width > 0 ? viewRef.current.width / r.width : 1;
|
|
275
|
+
const sy = r.height > 0 ? viewRef.current.height / r.height : 1;
|
|
276
|
+
return canvasToImagePoint({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(), viewRef.current.width, viewRef.current.height, modeRef.current === "crop");
|
|
272
277
|
};
|
|
273
278
|
const clientRect = (r) => {
|
|
274
279
|
if (!r || r.width <= 0) return null;
|
|
@@ -367,11 +372,12 @@ var ImageEditorCanvas = ({
|
|
|
367
372
|
const canvas = canvasRef.current;
|
|
368
373
|
const container = containerRef.current;
|
|
369
374
|
if (!canvas || !container) return;
|
|
370
|
-
const
|
|
371
|
-
|
|
375
|
+
const viewport = measureCanvasViewport(container);
|
|
376
|
+
viewRef.current = { width: viewport.width, height: viewport.height };
|
|
372
377
|
await controller.render(canvas, {
|
|
373
|
-
viewportWidth:
|
|
374
|
-
viewportHeight:
|
|
378
|
+
viewportWidth: viewport.width,
|
|
379
|
+
viewportHeight: viewport.height,
|
|
380
|
+
pixelRatio: viewport.pixelRatio,
|
|
375
381
|
fit,
|
|
376
382
|
showCropOverlay: mode === "crop",
|
|
377
383
|
showSelectionOverlay: true,
|
|
@@ -386,6 +392,29 @@ var ImageEditorCanvas = ({
|
|
|
386
392
|
},
|
|
387
393
|
[controller, mode, fit, transformTool]
|
|
388
394
|
);
|
|
395
|
+
useEffect2(() => {
|
|
396
|
+
let drafting = false;
|
|
397
|
+
return agentPresence.subscribe(
|
|
398
|
+
trackPresenceDrag({
|
|
399
|
+
onDrag: (box) => {
|
|
400
|
+
if (cropDragInfoRef.current || selectionGestureRef.current?.active) return;
|
|
401
|
+
const draft = agentDragDraft(modeRef.current, box, controller.getState().imageDimensions);
|
|
402
|
+
if (!draft) return;
|
|
403
|
+
drafting = true;
|
|
404
|
+
if ("crop" in draft) draftCropRef.current = draft.crop;
|
|
405
|
+
else selectionDraftRef.current = draft.selectionPath;
|
|
406
|
+
void renderCanvas();
|
|
407
|
+
},
|
|
408
|
+
onEnd: () => {
|
|
409
|
+
if (!drafting) return;
|
|
410
|
+
drafting = false;
|
|
411
|
+
draftCropRef.current = null;
|
|
412
|
+
selectionDraftRef.current = null;
|
|
413
|
+
void renderCanvas();
|
|
414
|
+
}
|
|
415
|
+
})
|
|
416
|
+
);
|
|
417
|
+
}, [agentPresence, controller, renderCanvas]);
|
|
389
418
|
useEffect2(() => typeSession?.subscribe(() => void renderCanvas()), [typeSession, renderCanvas]);
|
|
390
419
|
const finishTyping = () => {
|
|
391
420
|
typeToolRef.current?.finish();
|
|
@@ -431,12 +460,12 @@ var ImageEditorCanvas = ({
|
|
|
431
460
|
const transformPoint = (e) => {
|
|
432
461
|
const canvas = canvasRef.current;
|
|
433
462
|
const rect = canvas.getBoundingClientRect();
|
|
434
|
-
const sx = rect.width > 0 ?
|
|
435
|
-
const sy = rect.height > 0 ?
|
|
463
|
+
const sx = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
464
|
+
const sy = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
436
465
|
const s = controller.getState();
|
|
437
466
|
const cropping = mode === "crop";
|
|
438
|
-
const projection = getViewportProjection(s,
|
|
439
|
-
const pt = canvasToImagePoint({ x: (e.clientX - rect.left) * sx, y: (e.clientY - rect.top) * sy }, s,
|
|
467
|
+
const projection = getViewportProjection(s, viewRef.current.width, viewRef.current.height, cropping);
|
|
468
|
+
const pt = canvasToImagePoint({ x: (e.clientX - rect.left) * sx, y: (e.clientY - rect.top) * sy }, s, viewRef.current.width, viewRef.current.height, cropping);
|
|
440
469
|
transformTool.setView(projection.scale);
|
|
441
470
|
return { pt, tolerance: 6 / (projection.scale || 1) };
|
|
442
471
|
};
|
|
@@ -486,24 +515,16 @@ var ImageEditorCanvas = ({
|
|
|
486
515
|
useEffect2(() => {
|
|
487
516
|
const container = containerRef.current;
|
|
488
517
|
if (!container) return;
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
});
|
|
493
|
-
observer.observe(container);
|
|
494
|
-
return () => observer.disconnect();
|
|
495
|
-
} else {
|
|
496
|
-
const handleResize = () => renderCanvas();
|
|
497
|
-
window.addEventListener("resize", handleResize);
|
|
498
|
-
return () => window.removeEventListener("resize", handleResize);
|
|
499
|
-
}
|
|
518
|
+
const { width, height } = measureCanvasViewport(container);
|
|
519
|
+
viewRef.current = { width, height };
|
|
520
|
+
return watchCanvasViewport(container, () => renderCanvas());
|
|
500
521
|
}, [renderCanvas]);
|
|
501
522
|
const getCanvasEventPoint = (e) => {
|
|
502
523
|
const canvas = canvasRef.current;
|
|
503
524
|
if (!canvas) return { x: 0, y: 0 };
|
|
504
525
|
const rect = canvas.getBoundingClientRect();
|
|
505
|
-
const scaleX = rect.width > 0 ?
|
|
506
|
-
const scaleY = rect.height > 0 ?
|
|
526
|
+
const scaleX = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
527
|
+
const scaleY = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
507
528
|
return {
|
|
508
529
|
x: (e.clientX - rect.left) * scaleX,
|
|
509
530
|
y: (e.clientY - rect.top) * scaleY
|
|
@@ -515,11 +536,11 @@ var ImageEditorCanvas = ({
|
|
|
515
536
|
const events = typeof native.getCoalescedEvents === "function" ? native.getCoalescedEvents() : [];
|
|
516
537
|
const list = events.length ? events : [native];
|
|
517
538
|
const rect = canvas.getBoundingClientRect();
|
|
518
|
-
const sx = rect.width > 0 ?
|
|
519
|
-
const sy = rect.height > 0 ?
|
|
539
|
+
const sx = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
540
|
+
const sy = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
520
541
|
const s = controller.getState();
|
|
521
542
|
return list.map((ev) => ({
|
|
522
|
-
...canvasToImagePoint({ x: (ev.clientX - rect.left) * sx, y: (ev.clientY - rect.top) * sy }, s,
|
|
543
|
+
...canvasToImagePoint({ x: (ev.clientX - rect.left) * sx, y: (ev.clientY - rect.top) * sy }, s, viewRef.current.width, viewRef.current.height, false),
|
|
523
544
|
pressure: pointerPressure(ev)
|
|
524
545
|
}));
|
|
525
546
|
};
|
|
@@ -565,7 +586,7 @@ var ImageEditorCanvas = ({
|
|
|
565
586
|
const canvasPt = getCanvasEventPoint(e);
|
|
566
587
|
if (mode === "select") {
|
|
567
588
|
const gesture = selectionGestureRef.current;
|
|
568
|
-
const imgPt = canvasToImagePoint(canvasPt, state,
|
|
589
|
+
const imgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
569
590
|
void gesture.begin(imgPt, e).then(() => report("select", gesture.error));
|
|
570
591
|
return;
|
|
571
592
|
}
|
|
@@ -576,8 +597,8 @@ var ImageEditorCanvas = ({
|
|
|
576
597
|
width: state.imageDimensions.width,
|
|
577
598
|
height: state.imageDimensions.height
|
|
578
599
|
};
|
|
579
|
-
const handle = hitTestCrop(canvasPt, state,
|
|
580
|
-
const startImgPt = canvasToImagePoint(canvasPt, state,
|
|
600
|
+
const handle = hitTestCrop(canvasPt, state, viewRef.current.width, viewRef.current.height, 14, activeCrop);
|
|
601
|
+
const startImgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, true);
|
|
581
602
|
if (handle) {
|
|
582
603
|
cropDragInfoRef.current = {
|
|
583
604
|
handle,
|
|
@@ -596,7 +617,7 @@ var ImageEditorCanvas = ({
|
|
|
596
617
|
return;
|
|
597
618
|
}
|
|
598
619
|
if (mode === "annotate" && (state.activeTool === "shape" || state.activeTool === "type")) {
|
|
599
|
-
const imgPt = canvasToImagePoint(canvasPt, state,
|
|
620
|
+
const imgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
600
621
|
report(state.activeTool, null);
|
|
601
622
|
if (state.activeTool === "shape") shapeGestureRef.current.down(imgPt);
|
|
602
623
|
else {
|
|
@@ -607,7 +628,7 @@ var ImageEditorCanvas = ({
|
|
|
607
628
|
return;
|
|
608
629
|
}
|
|
609
630
|
if (mode === "annotate") {
|
|
610
|
-
const imgPt = canvasToImagePoint(canvasPt, state,
|
|
631
|
+
const imgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
611
632
|
dragStartPointRef.current = imgPt;
|
|
612
633
|
if (state.activeTool === "select") {
|
|
613
634
|
for (let i = state.annotations.length - 1; i >= 0; i--) {
|
|
@@ -648,7 +669,7 @@ var ImageEditorCanvas = ({
|
|
|
648
669
|
const canvasPt = getCanvasEventPoint(e);
|
|
649
670
|
if (mode === "select") {
|
|
650
671
|
const gesture = selectionGestureRef.current;
|
|
651
|
-
if (gesture.active && gesture.move(canvasToImagePoint(canvasPt, state,
|
|
672
|
+
if (gesture.active && gesture.move(canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false))) {
|
|
652
673
|
selectionDraftRef.current = gesture.draftPath;
|
|
653
674
|
renderCanvas();
|
|
654
675
|
}
|
|
@@ -657,7 +678,7 @@ var ImageEditorCanvas = ({
|
|
|
657
678
|
if (mode === "crop") {
|
|
658
679
|
const drag = cropDragInfoRef.current;
|
|
659
680
|
if (drag) {
|
|
660
|
-
const currentImgPt = canvasToImagePoint(canvasPt, state,
|
|
681
|
+
const currentImgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, true);
|
|
661
682
|
let newCrop;
|
|
662
683
|
if (drag.handle === "new") {
|
|
663
684
|
const minX = Math.max(0, Math.min(drag.startImgPt.x, currentImgPt.x));
|
|
@@ -691,7 +712,7 @@ var ImageEditorCanvas = ({
|
|
|
691
712
|
width: state.imageDimensions.width,
|
|
692
713
|
height: state.imageDimensions.height
|
|
693
714
|
};
|
|
694
|
-
const handle = hitTestCrop(canvasPt, state,
|
|
715
|
+
const handle = hitTestCrop(canvasPt, state, viewRef.current.width, viewRef.current.height, 14, activeCrop);
|
|
695
716
|
canvas.style.cursor = getCropCursor(handle, state.transform);
|
|
696
717
|
}
|
|
697
718
|
return;
|
|
@@ -699,7 +720,7 @@ var ImageEditorCanvas = ({
|
|
|
699
720
|
if (mode === "annotate" && state.activeTool === "shape") {
|
|
700
721
|
const g = shapeGestureRef.current;
|
|
701
722
|
if (g.active) {
|
|
702
|
-
g.move(canvasToImagePoint(canvasPt, state,
|
|
723
|
+
g.move(canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false), { shift: e.shiftKey, alt: e.altKey });
|
|
703
724
|
if (g.error) report("shape", g.error);
|
|
704
725
|
}
|
|
705
726
|
return;
|
|
@@ -707,7 +728,7 @@ var ImageEditorCanvas = ({
|
|
|
707
728
|
if (mode === "annotate") {
|
|
708
729
|
if (annotationDragInfoRef.current) {
|
|
709
730
|
const drag = annotationDragInfoRef.current;
|
|
710
|
-
const currentImgPt2 = canvasToImagePoint(canvasPt, state,
|
|
731
|
+
const currentImgPt2 = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
711
732
|
const dx = currentImgPt2.x - drag.startImgPt.x;
|
|
712
733
|
const dy = currentImgPt2.y - drag.startImgPt.y;
|
|
713
734
|
editor.updateAnnotation(drag.annId, {
|
|
@@ -719,7 +740,7 @@ var ImageEditorCanvas = ({
|
|
|
719
740
|
const startPt = dragStartPointRef.current;
|
|
720
741
|
if (!startPt) {
|
|
721
742
|
if (state.activeTool === "select") {
|
|
722
|
-
const imgPt = canvasToImagePoint(canvasPt, state,
|
|
743
|
+
const imgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
723
744
|
let hovered = false;
|
|
724
745
|
for (let i = state.annotations.length - 1; i >= 0; i--) {
|
|
725
746
|
if (hitTestAnnotation(imgPt, state.annotations[i])) {
|
|
@@ -733,7 +754,7 @@ var ImageEditorCanvas = ({
|
|
|
733
754
|
}
|
|
734
755
|
return;
|
|
735
756
|
}
|
|
736
|
-
const currentImgPt = canvasToImagePoint(canvasPt, state,
|
|
757
|
+
const currentImgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
737
758
|
const stroke = { strokeColor: annotationColor, strokeWidth: annotationWidth };
|
|
738
759
|
if (state.activeTool === "pen" && isDrawingRef.current) {
|
|
739
760
|
currentPathRef.current.push(currentImgPt);
|
|
@@ -790,7 +811,7 @@ var ImageEditorCanvas = ({
|
|
|
790
811
|
if (mode === "select") {
|
|
791
812
|
const gesture = selectionGestureRef.current;
|
|
792
813
|
if (gesture.active) {
|
|
793
|
-
gesture.end(canvasToImagePoint(canvasPt, state,
|
|
814
|
+
gesture.end(canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false), e);
|
|
794
815
|
selectionDraftRef.current = null;
|
|
795
816
|
renderCanvas();
|
|
796
817
|
}
|
|
@@ -813,7 +834,7 @@ var ImageEditorCanvas = ({
|
|
|
813
834
|
}
|
|
814
835
|
if (mode === "annotate" && state.activeTool === "type") {
|
|
815
836
|
const tool = typeToolRef.current;
|
|
816
|
-
tool.up(canvasToImagePoint(canvasPt, state,
|
|
837
|
+
tool.up(canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false));
|
|
817
838
|
setTypeSession(tool.session);
|
|
818
839
|
if (tool.error) report("type", tool.error);
|
|
819
840
|
void renderCanvas();
|
|
@@ -827,7 +848,7 @@ var ImageEditorCanvas = ({
|
|
|
827
848
|
}
|
|
828
849
|
const startPt = dragStartPointRef.current;
|
|
829
850
|
if (startPt) {
|
|
830
|
-
const currentImgPt = canvasToImagePoint(canvasPt, state,
|
|
851
|
+
const currentImgPt = canvasToImagePoint(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
831
852
|
const stroke = { strokeColor: annotationColor, strokeWidth: annotationWidth };
|
|
832
853
|
if (state.activeTool === "pen" && isDrawingRef.current) {
|
|
833
854
|
if (currentPathRef.current.length > 1) {
|
|
@@ -894,7 +915,7 @@ var ImageEditorCanvas = ({
|
|
|
894
915
|
onPointerUp: handlePaintPointerUp,
|
|
895
916
|
onPointerCancel: () => paintGestureRef.current?.cancel(),
|
|
896
917
|
"data-testid": "image-editor-canvas",
|
|
897
|
-
style: transformTool.active ? { cursor: transformCursor } :
|
|
918
|
+
style: transformTool.active ? { width: "100%", height: "100%", cursor: transformCursor } : { width: "100%", height: "100%" },
|
|
898
919
|
className: `absolute inset-0 w-full h-full block ${transformTool.active ? "touch-none" : mode === "paint" ? "touch-none cursor-crosshair" : mode === "annotate" && state.activeTool === "type" ? "cursor-text" : mode === "annotate" && state.activeTool === "shape" ? "cursor-crosshair" : ""}`
|
|
899
920
|
}
|
|
900
921
|
),
|
|
@@ -2170,7 +2191,7 @@ import {
|
|
|
2170
2191
|
rgbUnitToHex,
|
|
2171
2192
|
setRetouchChoice
|
|
2172
2193
|
} from "@pixerate/editor/image-editor";
|
|
2173
|
-
import { DEFAULT_PAINT_GRADIENT } from "@pixerate/editor/image-editor";
|
|
2194
|
+
import { DEFAULT_PAINT_GRADIENT, userErrorMessage } from "@pixerate/editor/image-editor";
|
|
2174
2195
|
|
|
2175
2196
|
// src/image-editor/PaintGradientEditor.tsx
|
|
2176
2197
|
import { PAINT_GRADIENT_KIT } from "@pixerate/editor/image-editor";
|
|
@@ -2191,7 +2212,7 @@ var BrushOptionsBar = ({ controller, className }) => {
|
|
|
2191
2212
|
setError(null);
|
|
2192
2213
|
fn();
|
|
2193
2214
|
} catch (err) {
|
|
2194
|
-
setError(
|
|
2215
|
+
setError(userErrorMessage(err));
|
|
2195
2216
|
}
|
|
2196
2217
|
};
|
|
2197
2218
|
return /* @__PURE__ */ jsxs10("div", { className: `flex flex-col gap-2 text-xs text-foreground ${className ?? ""}`, "data-testid": "brush-options", children: [
|
|
@@ -277,6 +277,9 @@ function usePlaybackSurface(playback, id, options = {}, sharedPresence) {
|
|
|
277
277
|
return presence;
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
+
// src/image-editor/ImageEditorCanvas.tsx
|
|
281
|
+
var import_tools2 = require("@pixerate/editor/tools");
|
|
282
|
+
|
|
280
283
|
// src/playback/PresenceLayer.tsx
|
|
281
284
|
var import_react4 = __toESM(require("react"), 1);
|
|
282
285
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
@@ -349,6 +352,7 @@ var ImageEditorCanvas = ({
|
|
|
349
352
|
const canvasRef = (0, import_react5.useRef)(null);
|
|
350
353
|
const containerRef = (0, import_react5.useRef)(null);
|
|
351
354
|
const rootRef = (0, import_react5.useRef)(null);
|
|
355
|
+
const viewRef = (0, import_react5.useRef)({ width: 0, height: 0 });
|
|
352
356
|
const modeRef = (0, import_react5.useRef)(mode);
|
|
353
357
|
modeRef.current = mode;
|
|
354
358
|
const latest = (0, import_react5.useRef)({ onError, onTypeSessionChange, locate, locateElement, open, chromeRoot, onPlaybackReveal });
|
|
@@ -360,19 +364,19 @@ var ImageEditorCanvas = ({
|
|
|
360
364
|
const docToRoot = (p) => {
|
|
361
365
|
const canvas = canvasRef.current;
|
|
362
366
|
const root = rootRef.current;
|
|
363
|
-
if (!canvas || !root || !
|
|
367
|
+
if (!canvas || !root || !viewRef.current.width) return p;
|
|
364
368
|
const r = canvas.getBoundingClientRect();
|
|
365
369
|
const box = root.getBoundingClientRect();
|
|
366
|
-
const c = (0, import_image_editor2.imageToCanvasPoint)(p, controller.getState(),
|
|
367
|
-
return { x: r.left - box.left + c.x * r.width /
|
|
370
|
+
const c = (0, import_image_editor2.imageToCanvasPoint)(p, controller.getState(), viewRef.current.width, viewRef.current.height, modeRef.current === "crop");
|
|
371
|
+
return { x: r.left - box.left + c.x * r.width / viewRef.current.width, y: r.top - box.top + c.y * r.height / viewRef.current.height };
|
|
368
372
|
};
|
|
369
373
|
const clientToDoc = (p) => {
|
|
370
374
|
const canvas = canvasRef.current;
|
|
371
|
-
if (!canvas || !
|
|
375
|
+
if (!canvas || !viewRef.current.width) return p;
|
|
372
376
|
const r = canvas.getBoundingClientRect();
|
|
373
|
-
const sx = r.width > 0 ?
|
|
374
|
-
const sy = r.height > 0 ?
|
|
375
|
-
return (0, import_image_editor2.canvasToImagePoint)({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(),
|
|
377
|
+
const sx = r.width > 0 ? viewRef.current.width / r.width : 1;
|
|
378
|
+
const sy = r.height > 0 ? viewRef.current.height / r.height : 1;
|
|
379
|
+
return (0, import_image_editor2.canvasToImagePoint)({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(), viewRef.current.width, viewRef.current.height, modeRef.current === "crop");
|
|
376
380
|
};
|
|
377
381
|
const clientRect = (r) => {
|
|
378
382
|
if (!r || r.width <= 0) return null;
|
|
@@ -471,11 +475,12 @@ var ImageEditorCanvas = ({
|
|
|
471
475
|
const canvas = canvasRef.current;
|
|
472
476
|
const container = containerRef.current;
|
|
473
477
|
if (!canvas || !container) return;
|
|
474
|
-
const
|
|
475
|
-
|
|
478
|
+
const viewport = (0, import_image_editor2.measureCanvasViewport)(container);
|
|
479
|
+
viewRef.current = { width: viewport.width, height: viewport.height };
|
|
476
480
|
await controller.render(canvas, {
|
|
477
|
-
viewportWidth:
|
|
478
|
-
viewportHeight:
|
|
481
|
+
viewportWidth: viewport.width,
|
|
482
|
+
viewportHeight: viewport.height,
|
|
483
|
+
pixelRatio: viewport.pixelRatio,
|
|
479
484
|
fit,
|
|
480
485
|
showCropOverlay: mode === "crop",
|
|
481
486
|
showSelectionOverlay: true,
|
|
@@ -490,6 +495,29 @@ var ImageEditorCanvas = ({
|
|
|
490
495
|
},
|
|
491
496
|
[controller, mode, fit, transformTool]
|
|
492
497
|
);
|
|
498
|
+
(0, import_react5.useEffect)(() => {
|
|
499
|
+
let drafting = false;
|
|
500
|
+
return agentPresence.subscribe(
|
|
501
|
+
(0, import_tools2.trackPresenceDrag)({
|
|
502
|
+
onDrag: (box) => {
|
|
503
|
+
if (cropDragInfoRef.current || selectionGestureRef.current?.active) return;
|
|
504
|
+
const draft = (0, import_image_editor2.agentDragDraft)(modeRef.current, box, controller.getState().imageDimensions);
|
|
505
|
+
if (!draft) return;
|
|
506
|
+
drafting = true;
|
|
507
|
+
if ("crop" in draft) draftCropRef.current = draft.crop;
|
|
508
|
+
else selectionDraftRef.current = draft.selectionPath;
|
|
509
|
+
void renderCanvas();
|
|
510
|
+
},
|
|
511
|
+
onEnd: () => {
|
|
512
|
+
if (!drafting) return;
|
|
513
|
+
drafting = false;
|
|
514
|
+
draftCropRef.current = null;
|
|
515
|
+
selectionDraftRef.current = null;
|
|
516
|
+
void renderCanvas();
|
|
517
|
+
}
|
|
518
|
+
})
|
|
519
|
+
);
|
|
520
|
+
}, [agentPresence, controller, renderCanvas]);
|
|
493
521
|
(0, import_react5.useEffect)(() => typeSession?.subscribe(() => void renderCanvas()), [typeSession, renderCanvas]);
|
|
494
522
|
const finishTyping = () => {
|
|
495
523
|
typeToolRef.current?.finish();
|
|
@@ -535,12 +563,12 @@ var ImageEditorCanvas = ({
|
|
|
535
563
|
const transformPoint = (e) => {
|
|
536
564
|
const canvas = canvasRef.current;
|
|
537
565
|
const rect = canvas.getBoundingClientRect();
|
|
538
|
-
const sx = rect.width > 0 ?
|
|
539
|
-
const sy = rect.height > 0 ?
|
|
566
|
+
const sx = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
567
|
+
const sy = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
540
568
|
const s = controller.getState();
|
|
541
569
|
const cropping = mode === "crop";
|
|
542
|
-
const projection = (0, import_image_editor2.getViewportProjection)(s,
|
|
543
|
-
const pt = (0, import_image_editor2.canvasToImagePoint)({ x: (e.clientX - rect.left) * sx, y: (e.clientY - rect.top) * sy }, s,
|
|
570
|
+
const projection = (0, import_image_editor2.getViewportProjection)(s, viewRef.current.width, viewRef.current.height, cropping);
|
|
571
|
+
const pt = (0, import_image_editor2.canvasToImagePoint)({ x: (e.clientX - rect.left) * sx, y: (e.clientY - rect.top) * sy }, s, viewRef.current.width, viewRef.current.height, cropping);
|
|
544
572
|
transformTool.setView(projection.scale);
|
|
545
573
|
return { pt, tolerance: 6 / (projection.scale || 1) };
|
|
546
574
|
};
|
|
@@ -590,24 +618,16 @@ var ImageEditorCanvas = ({
|
|
|
590
618
|
(0, import_react5.useEffect)(() => {
|
|
591
619
|
const container = containerRef.current;
|
|
592
620
|
if (!container) return;
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
});
|
|
597
|
-
observer.observe(container);
|
|
598
|
-
return () => observer.disconnect();
|
|
599
|
-
} else {
|
|
600
|
-
const handleResize = () => renderCanvas();
|
|
601
|
-
window.addEventListener("resize", handleResize);
|
|
602
|
-
return () => window.removeEventListener("resize", handleResize);
|
|
603
|
-
}
|
|
621
|
+
const { width, height } = (0, import_image_editor2.measureCanvasViewport)(container);
|
|
622
|
+
viewRef.current = { width, height };
|
|
623
|
+
return (0, import_image_editor2.watchCanvasViewport)(container, () => renderCanvas());
|
|
604
624
|
}, [renderCanvas]);
|
|
605
625
|
const getCanvasEventPoint = (e) => {
|
|
606
626
|
const canvas = canvasRef.current;
|
|
607
627
|
if (!canvas) return { x: 0, y: 0 };
|
|
608
628
|
const rect = canvas.getBoundingClientRect();
|
|
609
|
-
const scaleX = rect.width > 0 ?
|
|
610
|
-
const scaleY = rect.height > 0 ?
|
|
629
|
+
const scaleX = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
630
|
+
const scaleY = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
611
631
|
return {
|
|
612
632
|
x: (e.clientX - rect.left) * scaleX,
|
|
613
633
|
y: (e.clientY - rect.top) * scaleY
|
|
@@ -619,11 +639,11 @@ var ImageEditorCanvas = ({
|
|
|
619
639
|
const events = typeof native.getCoalescedEvents === "function" ? native.getCoalescedEvents() : [];
|
|
620
640
|
const list = events.length ? events : [native];
|
|
621
641
|
const rect = canvas.getBoundingClientRect();
|
|
622
|
-
const sx = rect.width > 0 ?
|
|
623
|
-
const sy = rect.height > 0 ?
|
|
642
|
+
const sx = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
643
|
+
const sy = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
624
644
|
const s = controller.getState();
|
|
625
645
|
return list.map((ev) => ({
|
|
626
|
-
...(0, import_image_editor2.canvasToImagePoint)({ x: (ev.clientX - rect.left) * sx, y: (ev.clientY - rect.top) * sy }, s,
|
|
646
|
+
...(0, import_image_editor2.canvasToImagePoint)({ x: (ev.clientX - rect.left) * sx, y: (ev.clientY - rect.top) * sy }, s, viewRef.current.width, viewRef.current.height, false),
|
|
627
647
|
pressure: (0, import_image_editor2.pointerPressure)(ev)
|
|
628
648
|
}));
|
|
629
649
|
};
|
|
@@ -669,7 +689,7 @@ var ImageEditorCanvas = ({
|
|
|
669
689
|
const canvasPt = getCanvasEventPoint(e);
|
|
670
690
|
if (mode === "select") {
|
|
671
691
|
const gesture = selectionGestureRef.current;
|
|
672
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
692
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
673
693
|
void gesture.begin(imgPt, e).then(() => report("select", gesture.error));
|
|
674
694
|
return;
|
|
675
695
|
}
|
|
@@ -680,8 +700,8 @@ var ImageEditorCanvas = ({
|
|
|
680
700
|
width: state.imageDimensions.width,
|
|
681
701
|
height: state.imageDimensions.height
|
|
682
702
|
};
|
|
683
|
-
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state,
|
|
684
|
-
const startImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
703
|
+
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state, viewRef.current.width, viewRef.current.height, 14, activeCrop);
|
|
704
|
+
const startImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, true);
|
|
685
705
|
if (handle) {
|
|
686
706
|
cropDragInfoRef.current = {
|
|
687
707
|
handle,
|
|
@@ -700,7 +720,7 @@ var ImageEditorCanvas = ({
|
|
|
700
720
|
return;
|
|
701
721
|
}
|
|
702
722
|
if (mode === "annotate" && (state.activeTool === "shape" || state.activeTool === "type")) {
|
|
703
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
723
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
704
724
|
report(state.activeTool, null);
|
|
705
725
|
if (state.activeTool === "shape") shapeGestureRef.current.down(imgPt);
|
|
706
726
|
else {
|
|
@@ -711,7 +731,7 @@ var ImageEditorCanvas = ({
|
|
|
711
731
|
return;
|
|
712
732
|
}
|
|
713
733
|
if (mode === "annotate") {
|
|
714
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
734
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
715
735
|
dragStartPointRef.current = imgPt;
|
|
716
736
|
if (state.activeTool === "select") {
|
|
717
737
|
for (let i = state.annotations.length - 1; i >= 0; i--) {
|
|
@@ -752,7 +772,7 @@ var ImageEditorCanvas = ({
|
|
|
752
772
|
const canvasPt = getCanvasEventPoint(e);
|
|
753
773
|
if (mode === "select") {
|
|
754
774
|
const gesture = selectionGestureRef.current;
|
|
755
|
-
if (gesture.active && gesture.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
775
|
+
if (gesture.active && gesture.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false))) {
|
|
756
776
|
selectionDraftRef.current = gesture.draftPath;
|
|
757
777
|
renderCanvas();
|
|
758
778
|
}
|
|
@@ -761,7 +781,7 @@ var ImageEditorCanvas = ({
|
|
|
761
781
|
if (mode === "crop") {
|
|
762
782
|
const drag = cropDragInfoRef.current;
|
|
763
783
|
if (drag) {
|
|
764
|
-
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
784
|
+
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, true);
|
|
765
785
|
let newCrop;
|
|
766
786
|
if (drag.handle === "new") {
|
|
767
787
|
const minX = Math.max(0, Math.min(drag.startImgPt.x, currentImgPt.x));
|
|
@@ -795,7 +815,7 @@ var ImageEditorCanvas = ({
|
|
|
795
815
|
width: state.imageDimensions.width,
|
|
796
816
|
height: state.imageDimensions.height
|
|
797
817
|
};
|
|
798
|
-
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state,
|
|
818
|
+
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state, viewRef.current.width, viewRef.current.height, 14, activeCrop);
|
|
799
819
|
canvas.style.cursor = (0, import_image_editor2.getCropCursor)(handle, state.transform);
|
|
800
820
|
}
|
|
801
821
|
return;
|
|
@@ -803,7 +823,7 @@ var ImageEditorCanvas = ({
|
|
|
803
823
|
if (mode === "annotate" && state.activeTool === "shape") {
|
|
804
824
|
const g = shapeGestureRef.current;
|
|
805
825
|
if (g.active) {
|
|
806
|
-
g.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
826
|
+
g.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false), { shift: e.shiftKey, alt: e.altKey });
|
|
807
827
|
if (g.error) report("shape", g.error);
|
|
808
828
|
}
|
|
809
829
|
return;
|
|
@@ -811,7 +831,7 @@ var ImageEditorCanvas = ({
|
|
|
811
831
|
if (mode === "annotate") {
|
|
812
832
|
if (annotationDragInfoRef.current) {
|
|
813
833
|
const drag = annotationDragInfoRef.current;
|
|
814
|
-
const currentImgPt2 = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
834
|
+
const currentImgPt2 = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
815
835
|
const dx = currentImgPt2.x - drag.startImgPt.x;
|
|
816
836
|
const dy = currentImgPt2.y - drag.startImgPt.y;
|
|
817
837
|
editor.updateAnnotation(drag.annId, {
|
|
@@ -823,7 +843,7 @@ var ImageEditorCanvas = ({
|
|
|
823
843
|
const startPt = dragStartPointRef.current;
|
|
824
844
|
if (!startPt) {
|
|
825
845
|
if (state.activeTool === "select") {
|
|
826
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
846
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
827
847
|
let hovered = false;
|
|
828
848
|
for (let i = state.annotations.length - 1; i >= 0; i--) {
|
|
829
849
|
if ((0, import_image_editor2.hitTestAnnotation)(imgPt, state.annotations[i])) {
|
|
@@ -837,7 +857,7 @@ var ImageEditorCanvas = ({
|
|
|
837
857
|
}
|
|
838
858
|
return;
|
|
839
859
|
}
|
|
840
|
-
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
860
|
+
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
841
861
|
const stroke = { strokeColor: annotationColor, strokeWidth: annotationWidth };
|
|
842
862
|
if (state.activeTool === "pen" && isDrawingRef.current) {
|
|
843
863
|
currentPathRef.current.push(currentImgPt);
|
|
@@ -894,7 +914,7 @@ var ImageEditorCanvas = ({
|
|
|
894
914
|
if (mode === "select") {
|
|
895
915
|
const gesture = selectionGestureRef.current;
|
|
896
916
|
if (gesture.active) {
|
|
897
|
-
gesture.end((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
917
|
+
gesture.end((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false), e);
|
|
898
918
|
selectionDraftRef.current = null;
|
|
899
919
|
renderCanvas();
|
|
900
920
|
}
|
|
@@ -917,7 +937,7 @@ var ImageEditorCanvas = ({
|
|
|
917
937
|
}
|
|
918
938
|
if (mode === "annotate" && state.activeTool === "type") {
|
|
919
939
|
const tool = typeToolRef.current;
|
|
920
|
-
tool.up((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
940
|
+
tool.up((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false));
|
|
921
941
|
setTypeSession(tool.session);
|
|
922
942
|
if (tool.error) report("type", tool.error);
|
|
923
943
|
void renderCanvas();
|
|
@@ -931,7 +951,7 @@ var ImageEditorCanvas = ({
|
|
|
931
951
|
}
|
|
932
952
|
const startPt = dragStartPointRef.current;
|
|
933
953
|
if (startPt) {
|
|
934
|
-
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
954
|
+
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
935
955
|
const stroke = { strokeColor: annotationColor, strokeWidth: annotationWidth };
|
|
936
956
|
if (state.activeTool === "pen" && isDrawingRef.current) {
|
|
937
957
|
if (currentPathRef.current.length > 1) {
|
|
@@ -998,7 +1018,7 @@ var ImageEditorCanvas = ({
|
|
|
998
1018
|
onPointerUp: handlePaintPointerUp,
|
|
999
1019
|
onPointerCancel: () => paintGestureRef.current?.cancel(),
|
|
1000
1020
|
"data-testid": "image-editor-canvas",
|
|
1001
|
-
style: transformTool.active ? { cursor: transformCursor } :
|
|
1021
|
+
style: transformTool.active ? { width: "100%", height: "100%", cursor: transformCursor } : { width: "100%", height: "100%" },
|
|
1002
1022
|
className: `absolute inset-0 w-full h-full block ${transformTool.active ? "touch-none" : mode === "paint" ? "touch-none cursor-crosshair" : mode === "annotate" && state.activeTool === "type" ? "cursor-text" : mode === "annotate" && state.activeTool === "shape" ? "cursor-crosshair" : ""}`
|
|
1003
1023
|
}
|
|
1004
1024
|
),
|
|
@@ -3990,7 +4010,7 @@ var BrushOptionsBar = ({ controller, className }) => {
|
|
|
3990
4010
|
setError(null);
|
|
3991
4011
|
fn();
|
|
3992
4012
|
} catch (err) {
|
|
3993
|
-
setError(
|
|
4013
|
+
setError((0, import_image_editor12.userErrorMessage)(err));
|
|
3994
4014
|
}
|
|
3995
4015
|
};
|
|
3996
4016
|
return /* @__PURE__ */ (0, import_jsx_runtime20.jsxs)("div", { className: `flex flex-col gap-2 text-xs text-foreground ${className ?? ""}`, "data-testid": "brush-options", children: [
|
package/dist/index.cjs
CHANGED
|
@@ -4015,6 +4015,7 @@ var import_image_editor19 = require("@pixerate/editor/image-editor");
|
|
|
4015
4015
|
// src/image-editor/ImageEditorCanvas.tsx
|
|
4016
4016
|
var import_react40 = require("react");
|
|
4017
4017
|
var import_image_editor2 = require("@pixerate/editor/image-editor");
|
|
4018
|
+
var import_tools4 = require("@pixerate/editor/tools");
|
|
4018
4019
|
var import_jsx_runtime19 = require("react/jsx-runtime");
|
|
4019
4020
|
var ImageEditorCanvas = ({
|
|
4020
4021
|
editor,
|
|
@@ -4044,6 +4045,7 @@ var ImageEditorCanvas = ({
|
|
|
4044
4045
|
const canvasRef = (0, import_react40.useRef)(null);
|
|
4045
4046
|
const containerRef = (0, import_react40.useRef)(null);
|
|
4046
4047
|
const rootRef = (0, import_react40.useRef)(null);
|
|
4048
|
+
const viewRef = (0, import_react40.useRef)({ width: 0, height: 0 });
|
|
4047
4049
|
const modeRef = (0, import_react40.useRef)(mode);
|
|
4048
4050
|
modeRef.current = mode;
|
|
4049
4051
|
const latest = (0, import_react40.useRef)({ onError, onTypeSessionChange, locate, locateElement, open, chromeRoot, onPlaybackReveal });
|
|
@@ -4055,19 +4057,19 @@ var ImageEditorCanvas = ({
|
|
|
4055
4057
|
const docToRoot = (p) => {
|
|
4056
4058
|
const canvas = canvasRef.current;
|
|
4057
4059
|
const root = rootRef.current;
|
|
4058
|
-
if (!canvas || !root || !
|
|
4060
|
+
if (!canvas || !root || !viewRef.current.width) return p;
|
|
4059
4061
|
const r = canvas.getBoundingClientRect();
|
|
4060
4062
|
const box = root.getBoundingClientRect();
|
|
4061
|
-
const c = (0, import_image_editor2.imageToCanvasPoint)(p, controller.getState(),
|
|
4062
|
-
return { x: r.left - box.left + c.x * r.width /
|
|
4063
|
+
const c = (0, import_image_editor2.imageToCanvasPoint)(p, controller.getState(), viewRef.current.width, viewRef.current.height, modeRef.current === "crop");
|
|
4064
|
+
return { x: r.left - box.left + c.x * r.width / viewRef.current.width, y: r.top - box.top + c.y * r.height / viewRef.current.height };
|
|
4063
4065
|
};
|
|
4064
4066
|
const clientToDoc = (p) => {
|
|
4065
4067
|
const canvas = canvasRef.current;
|
|
4066
|
-
if (!canvas || !
|
|
4068
|
+
if (!canvas || !viewRef.current.width) return p;
|
|
4067
4069
|
const r = canvas.getBoundingClientRect();
|
|
4068
|
-
const sx = r.width > 0 ?
|
|
4069
|
-
const sy = r.height > 0 ?
|
|
4070
|
-
return (0, import_image_editor2.canvasToImagePoint)({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(),
|
|
4070
|
+
const sx = r.width > 0 ? viewRef.current.width / r.width : 1;
|
|
4071
|
+
const sy = r.height > 0 ? viewRef.current.height / r.height : 1;
|
|
4072
|
+
return (0, import_image_editor2.canvasToImagePoint)({ x: (p.x - r.left) * sx, y: (p.y - r.top) * sy }, controller.getState(), viewRef.current.width, viewRef.current.height, modeRef.current === "crop");
|
|
4071
4073
|
};
|
|
4072
4074
|
const clientRect = (r) => {
|
|
4073
4075
|
if (!r || r.width <= 0) return null;
|
|
@@ -4166,11 +4168,12 @@ var ImageEditorCanvas = ({
|
|
|
4166
4168
|
const canvas = canvasRef.current;
|
|
4167
4169
|
const container = containerRef.current;
|
|
4168
4170
|
if (!canvas || !container) return;
|
|
4169
|
-
const
|
|
4170
|
-
|
|
4171
|
+
const viewport = (0, import_image_editor2.measureCanvasViewport)(container);
|
|
4172
|
+
viewRef.current = { width: viewport.width, height: viewport.height };
|
|
4171
4173
|
await controller.render(canvas, {
|
|
4172
|
-
viewportWidth:
|
|
4173
|
-
viewportHeight:
|
|
4174
|
+
viewportWidth: viewport.width,
|
|
4175
|
+
viewportHeight: viewport.height,
|
|
4176
|
+
pixelRatio: viewport.pixelRatio,
|
|
4174
4177
|
fit,
|
|
4175
4178
|
showCropOverlay: mode === "crop",
|
|
4176
4179
|
showSelectionOverlay: true,
|
|
@@ -4185,6 +4188,29 @@ var ImageEditorCanvas = ({
|
|
|
4185
4188
|
},
|
|
4186
4189
|
[controller, mode, fit, transformTool]
|
|
4187
4190
|
);
|
|
4191
|
+
(0, import_react40.useEffect)(() => {
|
|
4192
|
+
let drafting = false;
|
|
4193
|
+
return agentPresence.subscribe(
|
|
4194
|
+
(0, import_tools4.trackPresenceDrag)({
|
|
4195
|
+
onDrag: (box) => {
|
|
4196
|
+
if (cropDragInfoRef.current || selectionGestureRef.current?.active) return;
|
|
4197
|
+
const draft = (0, import_image_editor2.agentDragDraft)(modeRef.current, box, controller.getState().imageDimensions);
|
|
4198
|
+
if (!draft) return;
|
|
4199
|
+
drafting = true;
|
|
4200
|
+
if ("crop" in draft) draftCropRef.current = draft.crop;
|
|
4201
|
+
else selectionDraftRef.current = draft.selectionPath;
|
|
4202
|
+
void renderCanvas();
|
|
4203
|
+
},
|
|
4204
|
+
onEnd: () => {
|
|
4205
|
+
if (!drafting) return;
|
|
4206
|
+
drafting = false;
|
|
4207
|
+
draftCropRef.current = null;
|
|
4208
|
+
selectionDraftRef.current = null;
|
|
4209
|
+
void renderCanvas();
|
|
4210
|
+
}
|
|
4211
|
+
})
|
|
4212
|
+
);
|
|
4213
|
+
}, [agentPresence, controller, renderCanvas]);
|
|
4188
4214
|
(0, import_react40.useEffect)(() => typeSession?.subscribe(() => void renderCanvas()), [typeSession, renderCanvas]);
|
|
4189
4215
|
const finishTyping = () => {
|
|
4190
4216
|
typeToolRef.current?.finish();
|
|
@@ -4230,12 +4256,12 @@ var ImageEditorCanvas = ({
|
|
|
4230
4256
|
const transformPoint = (e) => {
|
|
4231
4257
|
const canvas = canvasRef.current;
|
|
4232
4258
|
const rect = canvas.getBoundingClientRect();
|
|
4233
|
-
const sx = rect.width > 0 ?
|
|
4234
|
-
const sy = rect.height > 0 ?
|
|
4259
|
+
const sx = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
4260
|
+
const sy = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
4235
4261
|
const s = controller.getState();
|
|
4236
4262
|
const cropping = mode === "crop";
|
|
4237
|
-
const projection = (0, import_image_editor2.getViewportProjection)(s,
|
|
4238
|
-
const pt = (0, import_image_editor2.canvasToImagePoint)({ x: (e.clientX - rect.left) * sx, y: (e.clientY - rect.top) * sy }, s,
|
|
4263
|
+
const projection = (0, import_image_editor2.getViewportProjection)(s, viewRef.current.width, viewRef.current.height, cropping);
|
|
4264
|
+
const pt = (0, import_image_editor2.canvasToImagePoint)({ x: (e.clientX - rect.left) * sx, y: (e.clientY - rect.top) * sy }, s, viewRef.current.width, viewRef.current.height, cropping);
|
|
4239
4265
|
transformTool.setView(projection.scale);
|
|
4240
4266
|
return { pt, tolerance: 6 / (projection.scale || 1) };
|
|
4241
4267
|
};
|
|
@@ -4285,24 +4311,16 @@ var ImageEditorCanvas = ({
|
|
|
4285
4311
|
(0, import_react40.useEffect)(() => {
|
|
4286
4312
|
const container = containerRef.current;
|
|
4287
4313
|
if (!container) return;
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
});
|
|
4292
|
-
observer.observe(container);
|
|
4293
|
-
return () => observer.disconnect();
|
|
4294
|
-
} else {
|
|
4295
|
-
const handleResize = () => renderCanvas();
|
|
4296
|
-
window.addEventListener("resize", handleResize);
|
|
4297
|
-
return () => window.removeEventListener("resize", handleResize);
|
|
4298
|
-
}
|
|
4314
|
+
const { width, height } = (0, import_image_editor2.measureCanvasViewport)(container);
|
|
4315
|
+
viewRef.current = { width, height };
|
|
4316
|
+
return (0, import_image_editor2.watchCanvasViewport)(container, () => renderCanvas());
|
|
4299
4317
|
}, [renderCanvas]);
|
|
4300
4318
|
const getCanvasEventPoint = (e) => {
|
|
4301
4319
|
const canvas = canvasRef.current;
|
|
4302
4320
|
if (!canvas) return { x: 0, y: 0 };
|
|
4303
4321
|
const rect = canvas.getBoundingClientRect();
|
|
4304
|
-
const scaleX = rect.width > 0 ?
|
|
4305
|
-
const scaleY = rect.height > 0 ?
|
|
4322
|
+
const scaleX = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
4323
|
+
const scaleY = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
4306
4324
|
return {
|
|
4307
4325
|
x: (e.clientX - rect.left) * scaleX,
|
|
4308
4326
|
y: (e.clientY - rect.top) * scaleY
|
|
@@ -4314,11 +4332,11 @@ var ImageEditorCanvas = ({
|
|
|
4314
4332
|
const events = typeof native.getCoalescedEvents === "function" ? native.getCoalescedEvents() : [];
|
|
4315
4333
|
const list = events.length ? events : [native];
|
|
4316
4334
|
const rect = canvas.getBoundingClientRect();
|
|
4317
|
-
const sx = rect.width > 0 ?
|
|
4318
|
-
const sy = rect.height > 0 ?
|
|
4335
|
+
const sx = rect.width > 0 ? viewRef.current.width / rect.width : 1;
|
|
4336
|
+
const sy = rect.height > 0 ? viewRef.current.height / rect.height : 1;
|
|
4319
4337
|
const s = controller.getState();
|
|
4320
4338
|
return list.map((ev) => ({
|
|
4321
|
-
...(0, import_image_editor2.canvasToImagePoint)({ x: (ev.clientX - rect.left) * sx, y: (ev.clientY - rect.top) * sy }, s,
|
|
4339
|
+
...(0, import_image_editor2.canvasToImagePoint)({ x: (ev.clientX - rect.left) * sx, y: (ev.clientY - rect.top) * sy }, s, viewRef.current.width, viewRef.current.height, false),
|
|
4322
4340
|
pressure: (0, import_image_editor2.pointerPressure)(ev)
|
|
4323
4341
|
}));
|
|
4324
4342
|
};
|
|
@@ -4364,7 +4382,7 @@ var ImageEditorCanvas = ({
|
|
|
4364
4382
|
const canvasPt = getCanvasEventPoint(e);
|
|
4365
4383
|
if (mode === "select") {
|
|
4366
4384
|
const gesture = selectionGestureRef.current;
|
|
4367
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4385
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4368
4386
|
void gesture.begin(imgPt, e).then(() => report("select", gesture.error));
|
|
4369
4387
|
return;
|
|
4370
4388
|
}
|
|
@@ -4375,8 +4393,8 @@ var ImageEditorCanvas = ({
|
|
|
4375
4393
|
width: state.imageDimensions.width,
|
|
4376
4394
|
height: state.imageDimensions.height
|
|
4377
4395
|
};
|
|
4378
|
-
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state,
|
|
4379
|
-
const startImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4396
|
+
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state, viewRef.current.width, viewRef.current.height, 14, activeCrop);
|
|
4397
|
+
const startImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, true);
|
|
4380
4398
|
if (handle) {
|
|
4381
4399
|
cropDragInfoRef.current = {
|
|
4382
4400
|
handle,
|
|
@@ -4395,7 +4413,7 @@ var ImageEditorCanvas = ({
|
|
|
4395
4413
|
return;
|
|
4396
4414
|
}
|
|
4397
4415
|
if (mode === "annotate" && (state.activeTool === "shape" || state.activeTool === "type")) {
|
|
4398
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4416
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4399
4417
|
report(state.activeTool, null);
|
|
4400
4418
|
if (state.activeTool === "shape") shapeGestureRef.current.down(imgPt);
|
|
4401
4419
|
else {
|
|
@@ -4406,7 +4424,7 @@ var ImageEditorCanvas = ({
|
|
|
4406
4424
|
return;
|
|
4407
4425
|
}
|
|
4408
4426
|
if (mode === "annotate") {
|
|
4409
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4427
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4410
4428
|
dragStartPointRef.current = imgPt;
|
|
4411
4429
|
if (state.activeTool === "select") {
|
|
4412
4430
|
for (let i = state.annotations.length - 1; i >= 0; i--) {
|
|
@@ -4447,7 +4465,7 @@ var ImageEditorCanvas = ({
|
|
|
4447
4465
|
const canvasPt = getCanvasEventPoint(e);
|
|
4448
4466
|
if (mode === "select") {
|
|
4449
4467
|
const gesture = selectionGestureRef.current;
|
|
4450
|
-
if (gesture.active && gesture.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4468
|
+
if (gesture.active && gesture.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false))) {
|
|
4451
4469
|
selectionDraftRef.current = gesture.draftPath;
|
|
4452
4470
|
renderCanvas();
|
|
4453
4471
|
}
|
|
@@ -4456,7 +4474,7 @@ var ImageEditorCanvas = ({
|
|
|
4456
4474
|
if (mode === "crop") {
|
|
4457
4475
|
const drag = cropDragInfoRef.current;
|
|
4458
4476
|
if (drag) {
|
|
4459
|
-
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4477
|
+
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, true);
|
|
4460
4478
|
let newCrop;
|
|
4461
4479
|
if (drag.handle === "new") {
|
|
4462
4480
|
const minX = Math.max(0, Math.min(drag.startImgPt.x, currentImgPt.x));
|
|
@@ -4490,7 +4508,7 @@ var ImageEditorCanvas = ({
|
|
|
4490
4508
|
width: state.imageDimensions.width,
|
|
4491
4509
|
height: state.imageDimensions.height
|
|
4492
4510
|
};
|
|
4493
|
-
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state,
|
|
4511
|
+
const handle = (0, import_image_editor2.hitTestCrop)(canvasPt, state, viewRef.current.width, viewRef.current.height, 14, activeCrop);
|
|
4494
4512
|
canvas.style.cursor = (0, import_image_editor2.getCropCursor)(handle, state.transform);
|
|
4495
4513
|
}
|
|
4496
4514
|
return;
|
|
@@ -4498,7 +4516,7 @@ var ImageEditorCanvas = ({
|
|
|
4498
4516
|
if (mode === "annotate" && state.activeTool === "shape") {
|
|
4499
4517
|
const g = shapeGestureRef.current;
|
|
4500
4518
|
if (g.active) {
|
|
4501
|
-
g.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4519
|
+
g.move((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false), { shift: e.shiftKey, alt: e.altKey });
|
|
4502
4520
|
if (g.error) report("shape", g.error);
|
|
4503
4521
|
}
|
|
4504
4522
|
return;
|
|
@@ -4506,7 +4524,7 @@ var ImageEditorCanvas = ({
|
|
|
4506
4524
|
if (mode === "annotate") {
|
|
4507
4525
|
if (annotationDragInfoRef.current) {
|
|
4508
4526
|
const drag = annotationDragInfoRef.current;
|
|
4509
|
-
const currentImgPt2 = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4527
|
+
const currentImgPt2 = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4510
4528
|
const dx = currentImgPt2.x - drag.startImgPt.x;
|
|
4511
4529
|
const dy = currentImgPt2.y - drag.startImgPt.y;
|
|
4512
4530
|
editor.updateAnnotation(drag.annId, {
|
|
@@ -4518,7 +4536,7 @@ var ImageEditorCanvas = ({
|
|
|
4518
4536
|
const startPt = dragStartPointRef.current;
|
|
4519
4537
|
if (!startPt) {
|
|
4520
4538
|
if (state.activeTool === "select") {
|
|
4521
|
-
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4539
|
+
const imgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4522
4540
|
let hovered = false;
|
|
4523
4541
|
for (let i = state.annotations.length - 1; i >= 0; i--) {
|
|
4524
4542
|
if ((0, import_image_editor2.hitTestAnnotation)(imgPt, state.annotations[i])) {
|
|
@@ -4532,7 +4550,7 @@ var ImageEditorCanvas = ({
|
|
|
4532
4550
|
}
|
|
4533
4551
|
return;
|
|
4534
4552
|
}
|
|
4535
|
-
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4553
|
+
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4536
4554
|
const stroke = { strokeColor: annotationColor, strokeWidth: annotationWidth };
|
|
4537
4555
|
if (state.activeTool === "pen" && isDrawingRef.current) {
|
|
4538
4556
|
currentPathRef.current.push(currentImgPt);
|
|
@@ -4589,7 +4607,7 @@ var ImageEditorCanvas = ({
|
|
|
4589
4607
|
if (mode === "select") {
|
|
4590
4608
|
const gesture = selectionGestureRef.current;
|
|
4591
4609
|
if (gesture.active) {
|
|
4592
|
-
gesture.end((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4610
|
+
gesture.end((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false), e);
|
|
4593
4611
|
selectionDraftRef.current = null;
|
|
4594
4612
|
renderCanvas();
|
|
4595
4613
|
}
|
|
@@ -4612,7 +4630,7 @@ var ImageEditorCanvas = ({
|
|
|
4612
4630
|
}
|
|
4613
4631
|
if (mode === "annotate" && state.activeTool === "type") {
|
|
4614
4632
|
const tool = typeToolRef.current;
|
|
4615
|
-
tool.up((0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4633
|
+
tool.up((0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false));
|
|
4616
4634
|
setTypeSession(tool.session);
|
|
4617
4635
|
if (tool.error) report("type", tool.error);
|
|
4618
4636
|
void renderCanvas();
|
|
@@ -4626,7 +4644,7 @@ var ImageEditorCanvas = ({
|
|
|
4626
4644
|
}
|
|
4627
4645
|
const startPt = dragStartPointRef.current;
|
|
4628
4646
|
if (startPt) {
|
|
4629
|
-
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state,
|
|
4647
|
+
const currentImgPt = (0, import_image_editor2.canvasToImagePoint)(canvasPt, state, viewRef.current.width, viewRef.current.height, false);
|
|
4630
4648
|
const stroke = { strokeColor: annotationColor, strokeWidth: annotationWidth };
|
|
4631
4649
|
if (state.activeTool === "pen" && isDrawingRef.current) {
|
|
4632
4650
|
if (currentPathRef.current.length > 1) {
|
|
@@ -4693,7 +4711,7 @@ var ImageEditorCanvas = ({
|
|
|
4693
4711
|
onPointerUp: handlePaintPointerUp,
|
|
4694
4712
|
onPointerCancel: () => paintGestureRef.current?.cancel(),
|
|
4695
4713
|
"data-testid": "image-editor-canvas",
|
|
4696
|
-
style: transformTool.active ? { cursor: transformCursor } :
|
|
4714
|
+
style: transformTool.active ? { width: "100%", height: "100%", cursor: transformCursor } : { width: "100%", height: "100%" },
|
|
4697
4715
|
className: `absolute inset-0 w-full h-full block ${transformTool.active ? "touch-none" : mode === "paint" ? "touch-none cursor-crosshair" : mode === "annotate" && state.activeTool === "type" ? "cursor-text" : mode === "annotate" && state.activeTool === "shape" ? "cursor-crosshair" : ""}`
|
|
4698
4716
|
}
|
|
4699
4717
|
),
|
|
@@ -7685,7 +7703,7 @@ var BrushOptionsBar = ({ controller, className }) => {
|
|
|
7685
7703
|
setError(null);
|
|
7686
7704
|
fn();
|
|
7687
7705
|
} catch (err) {
|
|
7688
|
-
setError(
|
|
7706
|
+
setError((0, import_image_editor12.userErrorMessage)(err));
|
|
7689
7707
|
}
|
|
7690
7708
|
};
|
|
7691
7709
|
return /* @__PURE__ */ (0, import_jsx_runtime36.jsxs)("div", { className: `flex flex-col gap-2 text-xs text-foreground ${className ?? ""}`, "data-testid": "brush-options", children: [
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pixerate/editor-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.100.1",
|
|
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.
|
|
248
|
+
"@pixerate/editor": ">=0.150.1 <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",
|
|
@@ -267,12 +267,12 @@
|
|
|
267
267
|
"@tiptap/react": "^2.27.3",
|
|
268
268
|
"@types/react": "^19.3.0",
|
|
269
269
|
"@types/react-dom": "^19.3.0",
|
|
270
|
-
"react": "^19.3.
|
|
271
|
-
"react-dom": "^19.3.
|
|
270
|
+
"react": "^19.3.1",
|
|
271
|
+
"react-dom": "^19.3.1",
|
|
272
272
|
"tsup": "^9.0.0",
|
|
273
273
|
"typescript": "^5.7.3",
|
|
274
274
|
"vitest": "^5.0.5",
|
|
275
|
-
"@pixerate/editor": "0.
|
|
275
|
+
"@pixerate/editor": "0.150.1"
|
|
276
276
|
},
|
|
277
277
|
"scripts": {
|
|
278
278
|
"build": "tsup && tsc -p tsconfig.build.json && node ../../scripts/finalize-declarations.mjs dist",
|