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