Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheets-note-ui): note display error on some conditions (#5134)

ZhangWei59c693b2d7d0
diff --git a/packages/sheets-note-ui/src/views/note.tsx b/packages/sheets-note-ui/src/views/note.tsx
index cfa9ab6b5d..b9c22f95dc 100644
--- a/packages/sheets-note-ui/src/views/note.tsx
+++ b/packages/sheets-note-ui/src/views/note.tsx
@@ -30,12 +30,20 @@ export const SheetsNote = (props: { popup: IPopup<{ location: ISheetLocationBase
const noteModel = useDependency(SheetsNoteModel);
const localeService = useDependency(LocaleService);
const config = useConfigValue<IUniverSheetsNoteUIPluginConfig>(SHEETS_NOTE_UI_PLUGIN_CONFIG_KEY);
- const [note, setNote] = useState<ISheetNote>({ width: config?.defaultNoteSize?.width || 216, height: config?.defaultNoteSize?.height || 92, note: '' });
const activePopup = props.popup.extraProps?.location;
+ if (!activePopup) {
+ console.error('Popup extraProps or location is undefined.');
+ return null; // Or handle this case appropriately
+ }
+ const [note, setNote] = useState<ISheetNote>(() => {
+ const defaultNote = { width: config?.defaultNoteSize?.width || 216, height: config?.defaultNoteSize?.height || 92, note: '' };
+ const existingNote = noteModel.getNote(activePopup.unitId, activePopup.subUnitId!, activePopup.row, activePopup.col);
+ return existingNote || defaultNote;
+ });
+
const textareaRef = useRef<HTMLTextAreaElement>(null);
const commandService = useDependency(ICommandService);
const cellNoteChange$ = useMemo(() => activePopup ? noteModel.getCellNoteChange$(activePopup.unitId, activePopup.subUnitId!, activePopup.row, activePopup.col) : of(null), [activePopup]);
-
const updateNote = useDebounceFn((newNote: ISheetNote) => {
if (!activePopup) return;
@@ -60,9 +68,7 @@ export const SheetsNote = (props: { popup: IPopup<{ location: ISheetLocationBase
useEffect(() => {
if (activePopup) {
const existingNote = noteModel.getNote(activePopup.unitId, activePopup.subUnitId!, activePopup.row, activePopup.col);
- if (existingNote) {
- setNote(existingNote);
- } else {
+ if (!existingNote) {
textareaRef.current?.focus();
}
}
@@ -108,15 +114,14 @@ export const SheetsNote = (props: { popup: IPopup<{ location: ISheetLocationBase
updateNote(newNote);
};
- if (!activePopup) return null;
-
return (
<textarea
ref={textareaRef}
data-u-comp="note-textarea"
className={clsx(`
- univer-resize-both univer-ml-[1px] univer-rounded univer-border univer-border-solid univer-border-gray-200
- univer-p-2 univer-shadow
+ univer-resize-both univer-ml-px univer-rounded univer-border univer-border-solid univer-border-gray-200
+ univer-bg-white univer-p-2 univer-text-gray-900 univer-shadow
+ dark:univer-bg-gray-800 dark:univer-text-white
focus:univer-outline-none
`, scrollbarClassName)}
style={{ width: note.width, height: note.height }}
diff --git a/packages/sheets-ui/src/views/cell-popup/index.tsx b/packages/sheets-ui/src/views/cell-popup/index.tsx
index 63e0bf9ac2..dbe3c4759c 100644
--- a/packages/sheets-ui/src/views/cell-popup/index.tsx
+++ b/packages/sheets-ui/src/views/cell-popup/index.tsx
@@ -26,16 +26,19 @@ export const CellPopup = (props: { popup: IPopup<ISheetLocationBase & { directio
const location = popup.extraProps!;
const { row, col, direction, unitId, subUnitId } = location;
const cellPopupManagerService = useDependency(CellPopupManagerService);
- useObservable(useMemo(() => cellPopupManagerService.change$.pipe(filter((change) => change.row === row && change.col === col && change.direction === direction)), [cellPopupManagerService, row, col, direction]));
+ useObservable(
+ useMemo(() => cellPopupManagerService.change$.pipe(
+ filter((change) => change.row === row && change.col === col && change.direction === direction)
+ ), [cellPopupManagerService, row, col, direction])
+ );
const popups = cellPopupManagerService.getPopups(unitId, subUnitId, row, col, direction);
const componentManager = useDependency(ComponentManager);
return (
- <div className="univer-cell-popup univer-flex univer-flex-col univer-ml-[1px]">
- {popups.map((item) => {
- const popup = item;
+ <div data-u-comp="cell-popup" className="univer-ml-px univer-flex univer-flex-col">
+ {popups.map((popup) => {
const Component = componentManager.get(popup.componentKey);
- return Component ? <Component popup={popup} /> : null;
+ return Component ? <Component key={popup.id ?? popup.componentKey} popup={popup} /> : null;
})}
</div>
);