@deepnoid/canvas 0.1.67 → 0.1.68

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.
@@ -12,6 +12,8 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
12
12
  const annotationsCanvasRef = useRef(null);
13
13
  const engineRef = useRef(null);
14
14
  const pendingAnnotationsRef = useRef(null);
15
+ const imageLoadingRef = useRef(false);
16
+ const currentImageRef = useRef(image);
15
17
  const [, forceRender] = useState(0);
16
18
  /* ---------- Resize ---------- */
17
19
  useResizeObserver({
@@ -34,10 +36,19 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
34
36
  enabled: enableHotkeys,
35
37
  });
36
38
  /* ---------- Image / Engine lifecycle ---------- */
37
- const imageLoadingRef = useRef(false);
38
39
  useEffect(() => {
40
+ // 이미지가 실제로 변경되었는지 확인
41
+ const isImageChanged = currentImageRef.current !== image;
42
+ // 이미지 변경 전에 현재 상태 저장
43
+ const prevImageCanvasState = engineRef.current?.getImageCanvasState();
44
+ if (isImageChanged) {
45
+ // 이미지 변경 시: 즉시 엔진 제거 + pending 초기화
46
+ engineRef.current?.destroy();
47
+ engineRef.current = null;
48
+ pendingAnnotationsRef.current = null;
49
+ currentImageRef.current = image;
50
+ }
39
51
  imageLoadingRef.current = true;
40
- pendingAnnotationsRef.current = null;
41
52
  if (!image?.trim()) {
42
53
  engineRef.current?.destroy();
43
54
  engineRef.current = null;
@@ -50,8 +61,7 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
50
61
  if (cancelled || !imageCanvasRef.current || !annotationsCanvasRef.current) {
51
62
  return;
52
63
  }
53
- const prevEngine = engineRef.current;
54
- const prevImageCanvasState = prevEngine?.getImageCanvasState();
64
+ // resetOnImageChange 옵션에 따라 상태 결정
55
65
  const imageCanvasState = !resetOnImageChange && prevImageCanvasState
56
66
  ? prevImageCanvasState
57
67
  : {
@@ -66,17 +76,23 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
66
76
  dh: img.height,
67
77
  initZoom: 1,
68
78
  };
79
+ // pendingAnnotationsRef가 있으면 사용, 없으면 빈 배열
69
80
  const initialAnnotations = pendingAnnotationsRef.current ?? [];
70
- console.log('> AnnotationCanvas - initialAnnotations : ', initialAnnotations, ', pendingAnnotationsRef.current : ', pendingAnnotationsRef.current, ', annotations prop : ', annotations);
71
81
  pendingAnnotationsRef.current = null;
72
- prevEngine?.destroy();
82
+ // 현재 이미지 URL을 캡처 (클로저)
83
+ const currentImage = image;
73
84
  engineRef.current = new AnnotationEngine({
74
85
  imageCanvas: imageCanvasRef.current,
75
86
  image: img,
76
87
  imageCanvasState,
77
88
  annotationsCanvas: annotationsCanvasRef.current,
78
89
  annotations: initialAnnotations,
79
- setAnnotations: (next) => setAnnotations?.(next),
90
+ setAnnotations: (next) => {
91
+ // 현재 이미지와 일치할 때만 부모에게 알림
92
+ if (currentImageRef.current === currentImage) {
93
+ setAnnotations?.(next);
94
+ }
95
+ },
80
96
  drawing,
81
97
  editable,
82
98
  panZoomEnabled,
@@ -108,10 +124,12 @@ const AnnotationCanvas = ({ image, annotations = [], setAnnotations, options, dr
108
124
  useEffect(() => {
109
125
  if (!annotations)
110
126
  return;
127
+ // 이미지 로딩 중이거나 엔진이 없으면 pending에 저장
111
128
  if (imageLoadingRef.current || !engineRef.current) {
112
129
  pendingAnnotationsRef.current = annotations;
113
130
  return;
114
131
  }
132
+ // 엔진이 있으면 업데이트
115
133
  const before = engineRef.current.getAnnotations();
116
134
  if (JSON.stringify(before) !== JSON.stringify(annotations)) {
117
135
  engineRef.current.setAnnotations(annotations);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.67",
3
+ "version": "0.1.68",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",