Skip to content

Commit 97d2f06

Browse files
committed
Fix note autoresize loop
1 parent 08dce5a commit 97d2f06

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/components/EditorCanvas/Note.jsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ export default function Note({ data, onPointerDown }) {
7878

7979
const handleChange = (e) => {
8080
const textarea = document.getElementById(`note_${data.id}`);
81+
if (!textarea) return;
8182
textarea.style.height = "0";
8283
textarea.style.height = textarea.scrollHeight + "px";
8384
const newHeight = textarea.scrollHeight + 42;
84-
updateNote(data.id, { content: e.target.value, height: newHeight });
85+
const updates = { content: e.target.value };
86+
if (newHeight !== data.height) {
87+
updates.height = newHeight;
88+
}
89+
updateNote(data.id, updates);
8590
};
8691

8792
const handleBlur = (e) => {
@@ -181,11 +186,17 @@ export default function Note({ data, onPointerDown }) {
181186

182187
useEffect(() => {
183188
const textarea = document.getElementById(`note_${data.id}`);
189+
if (!textarea) return;
190+
184191
textarea.style.height = "0";
185-
textarea.style.height = textarea.scrollHeight + "px";
186-
const newHeight = textarea.scrollHeight + 42;
192+
const scrollHeight = textarea.scrollHeight;
193+
textarea.style.height = scrollHeight + "px";
194+
const newHeight = scrollHeight + 42;
195+
196+
if (newHeight === data.height) return;
197+
187198
updateNote(data.id, { height: newHeight });
188-
}, [data.id, updateNote]);
199+
}, [data.id, data.height, updateNote]);
189200

190201
return (
191202
<g
@@ -339,7 +350,10 @@ export default function Note({ data, onPointerDown }) {
339350
onPointerMove={(e) => {
340351
if (!resizing) return;
341352
const delta = e.movementX / (transform?.zoom || 1);
342-
const next = Math.max(MIN_NOTE_WIDTH, (data.width ?? noteWidth) + delta);
353+
const next = Math.max(
354+
MIN_NOTE_WIDTH,
355+
(data.width ?? noteWidth) + delta,
356+
);
343357
if (next !== data.width) {
344358
updateNote(data.id, { width: next });
345359
}
@@ -514,4 +528,4 @@ export default function Note({ data, onPointerDown }) {
514528
</foreignObject>
515529
</g>
516530
);
517-
}
531+
}

src/context/NotesContext.jsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { createContext, useState } from "react";
2-
import { Action, ObjectType, defaultNoteTheme, noteWidth } from "../data/constants";
1+
import { createContext, useState, useCallback } from "react";
2+
import {
3+
Action,
4+
ObjectType,
5+
defaultNoteTheme,
6+
noteWidth,
7+
} from "../data/constants";
38
import { useUndoRedo, useTransform, useSelect } from "../hooks";
49
import { Toast } from "@douyinfe/semi-ui";
510
import { useTranslation } from "react-i18next";
@@ -77,7 +82,7 @@ export default function NotesContextProvider({ children }) {
7782
}
7883
};
7984

80-
const updateNote = (id, values) => {
85+
const updateNote = useCallback((id, values) => {
8186
setNotes((prev) =>
8287
prev.map((t) => {
8388
if (t.id === id) {
@@ -89,7 +94,7 @@ export default function NotesContextProvider({ children }) {
8994
return t;
9095
}),
9196
);
92-
};
97+
}, []);
9398

9499
return (
95100
<NotesContext.Provider

0 commit comments

Comments
 (0)