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(note): fix sheets-note undo/redo (#6562)

wpxp123456ff8a18628bec
diff --git a/packages/sheets-note-ui/src/controllers/note.menu.ts b/packages/sheets-note-ui/src/controllers/note.menu.ts
index 84a518b765..170890a6c6 100644
--- a/packages/sheets-note-ui/src/controllers/note.menu.ts
+++ b/packages/sheets-note-ui/src/controllers/note.menu.ts
@@ -37,7 +37,7 @@ function getHasNote$(accessor: IAccessor): Observable<boolean> {
if (!target) return false;
const { actualColumn, actualRow } = selection.primary;
const noteModel = accessor.get(SheetsNoteModel);
- return Boolean(noteModel.getNote(target.unitId, target.subUnitId, actualRow, actualColumn));
+ return Boolean(noteModel.getNote(target.unitId, target.subUnitId, { row: actualRow, col: actualColumn }));
}));
}
diff --git a/packages/sheets-note-ui/src/controllers/sheets-cell-content.controller.ts b/packages/sheets-note-ui/src/controllers/sheets-cell-content.controller.ts
index 5d5eba49a2..2d0b96211f 100644
--- a/packages/sheets-note-ui/src/controllers/sheets-cell-content.controller.ts
+++ b/packages/sheets-note-ui/src/controllers/sheets-cell-content.controller.ts
@@ -41,7 +41,7 @@ export class SheetsCellContentController extends Disposable {
effect: InterceptorEffectEnum.Style,
handler: (cell, pos, next) => {
const { row, col, unitId, subUnitId } = pos;
- const note = this._sheetsNoteModel.getNote(unitId, subUnitId, row, col);
+ const note = this._sheetsNoteModel.getNote(unitId, subUnitId, { row, col });
if (note) {
if (!cell || cell === pos.rawData) {
cell = { ...pos.rawData };
diff --git a/packages/sheets-note-ui/src/controllers/sheets-note-popup.controller.ts b/packages/sheets-note-ui/src/controllers/sheets-note-popup.controller.ts
index 0d7a2e84f5..9d373b307d 100644
--- a/packages/sheets-note-ui/src/controllers/sheets-note-popup.controller.ts
+++ b/packages/sheets-note-ui/src/controllers/sheets-note-popup.controller.ts
@@ -41,6 +41,7 @@ export class SheetsNotePopupController extends Disposable {
this._initSelectionUpdateListener();
this._initEditorBridge();
this._initHoverEvent();
+ this._initDeleteNoteListener();
}
private _handleSelectionChange(selections: ISelectionWithStyle[], unitId: string, subUnitId: string) {
@@ -64,13 +65,14 @@ export class SheetsNotePopupController extends Disposable {
const row = actualCell.actualRow;
const col = actualCell.actualColumn;
- const note = this._sheetsNoteModel.getNote(unitId, subUnitId, row, col);
+ const note = this._sheetsNoteModel.getNote(unitId, subUnitId, { row, col });
if (note?.show) return;
if (note) {
this._sheetsNotePopupService.showPopup({
unitId,
subUnitId,
+ noteId: note.id,
row,
col,
});
@@ -115,7 +117,7 @@ export class SheetsNotePopupController extends Disposable {
let targetRow = row;
let targetCol = col;
- let note = this._sheetsNoteModel.getNote(unitId, subUnitId, targetRow, targetCol);
+ let note = this._sheetsNoteModel.getNote(unitId, subUnitId, { row: targetRow, col: targetCol });
if (!note && skeleton) {
const actualCell = skeleton.getCellWithCoordByIndex(row, col);
const { startRow, endRow, startColumn, endColumn } = actualCell.mergeInfo;
@@ -138,6 +140,7 @@ export class SheetsNotePopupController extends Disposable {
this._sheetsNotePopupService.showPopup({
unitId,
subUnitId,
+ noteId: note.id,
row: targetRow,
col: targetCol,
temp: true,
@@ -148,4 +151,19 @@ export class SheetsNotePopupController extends Disposable {
})
);
}
+
+ private _initDeleteNoteListener() {
+ this.disposeWithMe(
+ this._sheetsNoteModel.change$.subscribe((change) => {
+ if (!this._sheetsNotePopupService.activePopup) return;
+
+ const { unitId, subUnitId, noteId, row, col } = this._sheetsNotePopupService.activePopup;
+ const { oldNote, newNote } = change;
+
+ if (newNote === null && change.unitId === unitId && change.subUnitId === subUnitId && ((oldNote?.id && oldNote.id === noteId) || (oldNote?.row === row && oldNote.col === col))) {
+ this._sheetsNotePopupService.hidePopup(true);
+ }
+ })
+ );
+ }
}
diff --git a/packages/sheets-note-ui/src/views/note.tsx b/packages/sheets-note-ui/src/views/note.tsx
index d476ef6bd0..027f1ac035 100644
--- a/packages/sheets-note-ui/src/views/note.tsx
+++ b/packages/sheets-note-ui/src/views/note.tsx
@@ -21,10 +21,11 @@ import type { IUniverSheetsNoteUIConfig } from '../controllers/config.schema';
import { ICommandService, LocaleService } from '@univerjs/core';
import { clsx, Textarea } from '@univerjs/design';
import { IRenderManagerService } from '@univerjs/engine-render';
-import { SheetDeleteNoteCommand, SheetsNoteModel, SheetUpdateNoteCommand } from '@univerjs/sheets-note';
+import { SheetsNoteModel, SheetUpdateNoteCommand } from '@univerjs/sheets-note';
import { useConfigValue, useDebounceFn, useDependency } from '@univerjs/ui';
import { useCallback, useEffect, useRef, useState } from 'react';
import { SHEETS_NOTE_UI_PLUGIN_CONFIG_KEY } from '../controllers/config.schema';
+import { SheetsNotePopupService } from '../services/sheets-note-popup.service';
type INotePopupLocation = ISheetLocationBase & {
temp?: boolean;
@@ -37,7 +38,9 @@ export const SheetsNote = (props: { popup: IPopup<{ location: INotePopupLocation
const noteModel = useDependency(SheetsNoteModel);
const localeService = useDependency(LocaleService);
const renderManagerService = useDependency(IRenderManagerService);
+ const notePopupService = useDependency(SheetsNotePopupService);
const config = useConfigValue<IUniverSheetsNoteUIConfig>(SHEETS_NOTE_UI_PLUGIN_CONFIG_KEY);
+
const activePopup = popup.extraProps?.location;
if (!activePopup) {
console.error('Popup extraProps or location is undefined.');
@@ -51,16 +54,25 @@ export const SheetsNote = (props: { popup: IPopup<{ location: INotePopupLocation
useEffect(() => {
const { unitId, subUnitId, row, col } = activePopup;
-
- const { width = 160, height = 72 } = config?.defaultNoteSize ?? {};
- const note = noteModel.getNote(unitId, subUnitId, row, col) ?? { width, height, note: '' };
-
- if (textareaRef.current) {
- // eslint-disable-next-line react-hooks-extra/no-direct-set-state-in-use-effect
+ const note = noteModel.getNote(unitId, subUnitId, { row, col });
+ const width = note?.width ?? config?.defaultNoteSize?.width ?? 160;
+ const height = note?.height ?? config?.defaultNoteSize?.height ?? 72;
+
+ if (!note) {
+ const initNote: Partial<ISheetNote> = {
+ width,
+ height,
+ note: '',
+ };
+ setNote(initNote);
+ updateNote(initNote);
+ } else {
setNote(note);
+ }
- textareaRef.current.style.width = `${note.width}px`;
- textareaRef.current.style.height = `${note.height}px`;
+ if (textareaRef.current) {
+ textareaRef.current.style.width = `${width}px`;
+ textareaRef.current.style.height = `${height}px`;
}
}, [activePopup, textareaRef]);
@@ -79,26 +91,32 @@ export const SheetsNote = (props: { popup: IPopup<{ location: INotePopupLocation
const updateNote = useDebounceFn((newNote: Partial<ISheetNote>) => {
if (!activePopup) return;
- if (newNote.note) {
- commandService.executeCommand(SheetUpdateNoteCommand.id, {
- unitId: activePopup.unitId,
- sheetId: activePopup.subUnitId!,
- row: activePopup.row,
- col: activePopup.col,
- note: newNote,
- });
- } else {
- commandService.executeCommand(SheetDeleteNoteCommand.id, {
- unitId: activePopup.unitId,
- sheetId: activePopup.subUnitId!,
- row: activePopup.row,
- col: activePopup.col,
- });
+ const { unitId, subUnitId, row, col } = activePopup;
+ const result = commandService.syncExecuteCommand(SheetUpdateNoteCommand.id, {
+ unitId,
+ sheetId: subUnitId,
+ row,
+ col,
+ note: newNote,
+ });
+
+ // If the update fails
+ if (!result) {
+ const oldNote = noteModel.getNote(unitId, subUnitId, { noteId: newNote.id, row, col });
+
+ if (oldNote) {
+ // Revert to old note
+ setNote(oldNote);
+ } else {
+ // Hide popup if no old note exists
+ notePopupService.hidePopup(true);
+ }
}
});
const handleNoteChange = useCallback((value: string) => {
if (!note) return;
+ if (value === note.note) return;
const newNote = { ...note, note: value };
setNote(newNote);
@@ -107,6 +125,7 @@ export const SheetsNote = (props: { popup: IPopup<{ location: INotePopupLocation
const handleResize = useCallback((width: number, height: number) => {
if (!note) return;
+ if (width === note.width && height === note.height) return;
const newNote = { ...note, width, height };
setNote(newNote);
diff --git a/packages/sheets-note/src/commands/commands/note.command.ts b/packages/sheets-note/src/commands/commands/note.command.ts
index b853311c60..cb4c9c3fd8 100644
--- a/packages/sheets-note/src/commands/commands/note.command.ts
+++ b/packages/sheets-note/src/commands/commands/note.command.ts
@@ -14,60 +14,179 @@
* limitations under the License.
*/
-import type { ICommand } from '@univerjs/core';
-import type { IUpdateNoteMutationParams } from '../mutations/note.mutation';
-import { CommandType, ICommandService, IUniverInstanceService } from '@univerjs/core';
+import type { ICommand, IMutationInfo } from '@univerjs/core';
+import type { IRemoveNoteMutationParams, IToggleNotePopupMutationParams, IUpdateNoteMutationParams } from '../mutations/note.mutation';
+import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
import { getSheetCommandTarget, SheetsSelectionsService } from '@univerjs/sheets';
+import { SheetsNoteModel } from '../../models/sheets-note.model';
import { RemoveNoteMutation, ToggleNotePopupMutation, UpdateNoteMutation } from '../mutations/note.mutation';
export const SheetDeleteNoteCommand: ICommand = {
id: 'sheet.command.delete-note',
type: CommandType.COMMAND,
- handler: (accessor, params) => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const target = getSheetCommandTarget(univerInstanceService);
+ handler: (accessor) => {
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
if (!target) return false;
+
const sheetsSelectionsService = accessor.get(SheetsSelectionsService);
const selection = sheetsSelectionsService.getCurrentLastSelection();
if (!selection?.primary) return false;
+
+ const sheetsNoteModel = accessor.get(SheetsNoteModel);
+ const { unitId, subUnitId } = target;
const { actualColumn, actualRow } = selection.primary;
+ const note = sheetsNoteModel.getNote(unitId, subUnitId, { row: actualRow, col: actualColumn });
+ if (!note) return false;
+
const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+
+ const redoMutation: IMutationInfo<IRemoveNoteMutationParams> = {
+ id: RemoveNoteMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ noteId: note.id,
+ },
+ };
+ const undoMutation: IMutationInfo<IUpdateNoteMutationParams> = {
+ id: UpdateNoteMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ row: actualRow,
+ col: actualColumn,
+ note: { ...note },
+ },
+ };
+
+ const result = commandService.syncExecuteCommand(redoMutation.id, redoMutation.params);
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ redoMutations: [redoMutation],
+ undoMutations: [undoMutation],
+ });
+ return true;
+ }
- return commandService.executeCommand(RemoveNoteMutation.id, {
- unitId: target.unitId,
- sheetId: target.subUnitId,
- row: actualRow,
- col: actualColumn,
- });
+ return false;
},
};
export const SheetToggleNotePopupCommand: ICommand = {
id: 'sheet.command.toggle-note-popup',
type: CommandType.COMMAND,
- handler: (accessor, params) => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const target = getSheetCommandTarget(univerInstanceService);
+ handler: (accessor) => {
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
if (!target) return false;
+
const sheetsSelectionsService = accessor.get(SheetsSelectionsService);
const selection = sheetsSelectionsService.getCurrentLastSelection();
if (!selection?.primary) return false;
+
+ const sheetsNoteModel = accessor.get(SheetsNoteModel);
+ const { unitId, subUnitId } = target;
const { actualColumn, actualRow } = selection.primary;
+ const note = sheetsNoteModel.getNote(unitId, subUnitId, { row: actualRow, col: actualColumn });
+ if (!note) return false;
+
const commandService = accessor.get(ICommandService);
- return commandService.executeCommand(ToggleNotePopupMutation.id, {
- unitId: target.unitId,
- sheetId: target.subUnitId,
- row: actualRow,
- col: actualColumn,
- });
+ const undoRedoService = accessor.get(IUndoRedoService);
+
+ const redoMutation: IMutationInfo<IToggleNotePopupMutationParams> = {
+ id: ToggleNotePopupMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ noteId: note.id,
+ },
+ };
+ const undoMutation: IMutationInfo<IToggleNotePopupMutationParams> = {
+ id: ToggleNotePopupMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ noteId: note.id,
+ },
+ };
+
+ const result = commandService.syncExecuteCommand(redoMutation.id, redoMutation.params);
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ redoMutations: [redoMutation],
+ undoMutations: [undoMutation],
+ });
+ return true;
+ }
+
+ return false;
},
};
export const SheetUpdateNoteCommand: ICommand<IUpdateNoteMutationParams> = {
id: 'sheet.command.update-note',
type: CommandType.COMMAND,
- handler: (accessor, params) => {
+ handler: (accessor, params: IUpdateNoteMutationParams) => {
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) return false;
+
const commandService = accessor.get(ICommandService);
- return commandService.syncExecuteCommand(UpdateNoteMutation.id, params);
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const sheetsNoteModel = accessor.get(SheetsNoteModel);
+
+ const { unitId, subUnitId } = target;
+ const { row, col, note } = params;
+ const oldNote = sheetsNoteModel.getNote(unitId, subUnitId, { noteId: note.id, row, col });
+
+ const redoMutation: IMutationInfo<IUpdateNoteMutationParams> = {
+ id: UpdateNoteMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ row,
+ col,
+ note,
+ },
+ };
+ const undoMutations: IMutationInfo[] = [];
+
+ if (oldNote) {
+ const undoMutation: IMutationInfo<IUpdateNoteMutationParams> = {
+ id: UpdateNoteMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ row,
+ col,
+ note: { ...oldNote },
+ },
+ };
+ undoMutations.push(undoMutation);
+ } else {
+ const undoMutation: IMutationInfo<IRemoveNoteMutationParams> = {
+ id: RemoveNoteMutation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ row,
+ col,
+ },
+ };
+ undoMutations.push(undoMutation);
+ }
+
+ const result = commandService.syncExecuteCommand(redoMutation.id, redoMutation.params);
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ redoMutations: [redoMutation],
+ undoMutations,
+ });
+ return true;
+ }
+
+ return false;
},
};
diff --git a/packages/sheets-note/src/controllers/__tests__/ref-range.controller.spec.ts b/packages/sheets-note/src/controllers/__tests__/ref-range.controller.spec.ts
index cf3cbbea64..cf8f7403cd 100644
--- a/packages/sheets-note/src/controllers/__tests__/ref-range.controller.spec.ts
+++ b/packages/sheets-note/src/controllers/__tests__/ref-range.controller.spec.ts
@@ -14,8 +14,9 @@
* limitations under the License.
*/
-import type { Dependency, IWorkbookData, Workbook } from '@univerjs/core';
+import type { Dependency, IWorkbookData, Nullable, Workbook } from '@univerjs/core';
import type { IInsertColCommandParams, IInsertRowCommandParams, IRemoveColByRangeCommandParams, IRemoveRowByRangeCommandParams } from '@univerjs/sheets';
+import type { ISheetNote } from '../../models/sheets-note.model';
import { Direction, ICommandService, ILogService, Inject, Injector, IUndoRedoService, IUniverInstanceService, LocaleType, LogLevel, Plugin, touchDependencies, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
import {
InsertColByRangeCommand,
@@ -128,6 +129,7 @@ describe('test note ref range controller', () => {
let get: Injector['get'];
let commandService: ICommandService;
let sheetsNoteModel: SheetsNoteModel;
+ let getNoteByPosition: (row: number, col: number) => Nullable<ISheetNote>;
beforeEach(() => {
const testBed = createRefRangeTestBed();
@@ -147,6 +149,8 @@ describe('test note ref range controller', () => {
commandService.registerCommand(SetSelectionsOperation);
sheetsNoteModel = get(SheetsNoteModel);
+
+ getNoteByPosition = (row: number, col: number) => sheetsNoteModel.getNote(unitId, subUnitId, { row, col });
});
afterEach(() => univer.dispose());
@@ -169,12 +173,12 @@ describe('test note ref range controller', () => {
direction: Direction.DOWN,
};
expect(commandService.syncExecuteCommand(InsertRowByRangeCommand.id, params)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toBeUndefined();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 4, 3)).toStrictEqual({ ...D4CellNote, row: 4, col: 3 });
+ expect(getNoteByPosition(3, 3)).toBeUndefined();
+ expect(getNoteByPosition(4, 3)).toStrictEqual({ ...D4CellNote, row: 4, col: 3 });
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 4, 3)).toBeUndefined();
+ expect(getNoteByPosition(3, 3)).toStrictEqual(D4CellNote);
+ expect(getNoteByPosition(4, 3)).toBeUndefined();
});
it('test D4 cell note when insert column', async () => {
@@ -194,12 +198,12 @@ describe('test note ref range controller', () => {
direction: Direction.RIGHT,
};
expect(commandService.syncExecuteCommand(InsertColByRangeCommand.id, params)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toBeUndefined();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 4)).toStrictEqual({ ...D4CellNote, row: 3, col: 4 });
+ expect(getNoteByPosition(3, 3)).toBeUndefined();
+ expect(getNoteByPosition(3, 4)).toStrictEqual({ ...D4CellNote, row: 3, col: 4 });
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 4)).toBeUndefined();
+ expect(getNoteByPosition(3, 3)).toStrictEqual(D4CellNote);
+ expect(getNoteByPosition(3, 4)).toBeUndefined();
});
it('test D4 cell note when remove row', async () => {
@@ -218,12 +222,12 @@ describe('test note ref range controller', () => {
range: { startRow: 2, endRow: 2, startColumn: 0, endColumn: 9 },
};
expect(commandService.syncExecuteCommand(RemoveRowByRangeCommand.id, params)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toBeUndefined();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 2, 3)).toStrictEqual({ ...D4CellNote, row: 2, col: 3 });
+ expect(getNoteByPosition(3, 3)).toBeUndefined();
+ expect(getNoteByPosition(2, 3)).toStrictEqual({ ...D4CellNote, row: 2, col: 3 });
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 2, 3)).toBeUndefined();
+ expect(getNoteByPosition(3, 3)).toStrictEqual(D4CellNote);
+ expect(getNoteByPosition(2, 3)).toBeUndefined();
});
it('test D4 cell note when remove column', async () => {
@@ -242,12 +246,12 @@ describe('test note ref range controller', () => {
range: { startRow: 0, endRow: 9, startColumn: 2, endColumn: 2 },
};
expect(commandService.syncExecuteCommand(RemoveColByRangeCommand.id, params)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toBeUndefined();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 2)).toStrictEqual({ ...D4CellNote, row: 3, col: 2 });
+ expect(getNoteByPosition(3, 3)).toBeUndefined();
+ expect(getNoteByPosition(3, 2)).toStrictEqual({ ...D4CellNote, row: 3, col: 2 });
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
- expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 2)).toBeUndefined();
+ expect(getNoteByPosition(3, 3)).toStrictEqual(D4CellNote);
+ expect(getNoteByPosition(3, 2)).toBeUndefined();
});
});
});
diff --git a/packages/sheets-note/src/facade/f-range.ts b/packages/sheets-note/src/facade/f-range.ts
index 8a6b760bb3..ba540a6c49 100644
--- a/packages/sheets-note/src/facade/f-range.ts
+++ b/packages/sheets-note/src/facade/f-range.ts
@@ -106,7 +106,7 @@ export class FSheetsNoteRangeMixin extends FRange implements IFSheetsNoteRange {
override getNote(): Nullable<ISheetNote> {
const model = this._injector.get(SheetsNoteModel);
- return model.getNote(this.getUnitId(), this.getSheetId(), this.getRow(), this.getColumn());
+ return model.getNote(this.getUnitId(), this.getSheetId(), { row: this.getRow(), col: this.getColumn() });
}
}
diff --git a/packages/sheets-note/src/facade/f-univer.ts b/packages/sheets-note/src/facade/f-univer.ts
index 937c508825..614470e2da 100644
--- a/packages/sheets-note/src/facade/f-univer.ts
+++ b/packages/sheets-note/src/facade/f-univer.ts
@@ -16,8 +16,9 @@
import type { Injector } from '@univerjs/core';
import type { IRemoveNoteMutationParams, IUpdateNoteMutationParams } from '@univerjs/sheets-note';
-import { CanceledError, ICommandService } from '@univerjs/core';
+import { CanceledError, ICommandService, IUniverInstanceService } from '@univerjs/core';
import { FUniver } from '@univerjs/core/facade';
+import { getSheetCommandTarget, SheetsSelectionsService } from '@univerjs/sheets';
import { SheetDeleteNoteCommand, SheetsNoteModel, SheetToggleNotePopupCommand, SheetUpdateNoteCommand } from '@univerjs/sheets-note';
/**
@@ -171,14 +172,15 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.Event.BeforeSheetNoteAdd,
() => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetUpdateNoteCommand.id) {
- const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col, note } = command.params as IUpdateNoteMutationParams;
- const oldNote = model.getNote(unitId, sheetId, row, col);
- if (oldNote) return;
+
const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
+ if (!target) return;
+
+ const model = injector.get(SheetsNoteModel);
+ const oldNote = model.getNote(unitId, sheetId, { noteId: note.id, row, col });
+ if (oldNote) return;
+
const { workbook, worksheet } = target;
const cancel = this.fireEvent(this.Event.BeforeSheetNoteAdd, {
workbook,
@@ -200,15 +202,13 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.Event.BeforeSheetNoteDelete,
() => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetDeleteNoteCommand.id) {
- const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col } = command.params as IRemoveNoteMutationParams;
- if (row === undefined || col === undefined) return;
-
- const oldNote = model.getNote(unitId, sheetId, row, col);
- if (!oldNote) return;
-
const target = this.getSheetTarget(unitId, sheetId);
if (!target) return;
+ if (row === undefined || col === undefined) return;
+ const model = injector.get(SheetsNoteModel);
+ const oldNote = model.getNote(unitId, sheetId, { row, col });
+ if (!oldNote) return;
const { workbook, worksheet } = target;
const cancel = this.fireEvent(this.Event.BeforeSheetNoteDelete, {
@@ -232,14 +232,15 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.Event.BeforeSheetNoteUpdate,
() => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetUpdateNoteCommand.id) {
- const model = injector.get(SheetsNoteModel);
const { unitId, sheetId, row, col, note } = command.params as IUpdateNoteMutationParams;
- const oldNote = model.getNote(unitId, sheetId, row, col);
- if (!oldNote) return;
+
const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
+ if (!target) return;
+
+ const model = injector.get(SheetsNoteModel);
+ const oldNote = model.getNote(unitId, sheetId, { row, col });
+ if (!oldNote) return;
+
const { workbook, worksheet } = target;
const cancel = this.fireEvent(this.Event.BeforeSheetNoteUpdate, {
workbook,
@@ -262,20 +263,29 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.Event.BeforeSheetNoteShow,
() => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetToggleNotePopupCommand.id) {
- const model = injector.get(SheetsNoteModel);
- const { unitId, sheetId, row, col } = command.params as IUpdateNoteMutationParams;
- const oldNote = model.getNote(unitId, sheetId, row, col);
- if (oldNote?.show) return;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
+ const target = getSheetCommandTarget(injector.get(IUniverInstanceService));
+ if (!target) return;
+
+ const { unitId, subUnitId } = target;
+ const workbook = this.getUniverSheet(unitId);
+ if (!workbook) return;
+ const worksheet = workbook.getSheetBySheetId(subUnitId);
+ if (!worksheet) return;
+
+ const sheetsSelectionsService = injector.get(SheetsSelectionsService);
+ const selection = sheetsSelectionsService.getCurrentLastSelection();
+ if (!selection?.primary) return;
+
+ const sheetsNoteModel = injector.get(SheetsNoteModel);
+ const { actualColumn, actualRow } = selection.primary;
+ const note = sheetsNoteModel.getNote(unitId, subUnitId, { row: actualRow, col: actualColumn });
+ if (!note || note.show) return;
+
const cancel = this.fireEvent(this.Event.BeforeSheetNoteShow, {
workbook,
worksheet,
- row,
- col,
+ row: actualRow,
+ col: actualColumn,
});
if (cancel) {
throw new CanceledError();
@@ -290,20 +300,29 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.Event.BeforeSheetNoteHide,
() => commandService.beforeCommandExecuted((command) => {
if (command.id === SheetToggleNotePopupCommand.id) {
- const model = injector.get(SheetsNoteModel);
- const { unitId, sheetId, row, col } = command.params as IUpdateNoteMutationParams;
- const oldNote = model.getNote(unitId, sheetId, row, col);
- if (!oldNote?.show) return;
- const target = this.getSheetTarget(unitId, sheetId);
- if (!target) {
- return;
- }
- const { workbook, worksheet } = target;
+ const target = getSheetCommandTarget(injector.get(IUniverInstanceService));
+ if (!target) return;
+
+ const { unitId, subUnitId } = target;
+ const workbook = this.getUniverSheet(unitId);
+ if (!workbook) return;
+ const worksheet = workbook.getSheetBySheetId(subUnitId);
+ if (!worksheet) return;
+
+ const sheetsSelectionsService = injector.get(SheetsSelectionsService);
+ const selection = sheetsSelectionsService.getCurrentLastSelection();
+ if (!selection?.primary) return;
+
+ const sheetsNoteModel = injector.get(SheetsNoteModel);
+ const { actualColumn, actualRow } = selection.primary;
+ const note = sheetsNoteModel.getNote(unitId, subUnitId, { row: actualRow, col: actualColumn });
+ if (!note || !note.show) return;
+
const cancel = this.fireEvent(this.Event.BeforeSheetNoteHide, {
workbook,
worksheet,
- row,
- col,
+ row: actualRow,
+ col: actualColumn,
});
if (cancel) {
throw new CanceledError();
diff --git a/packages/sheets-note/src/models/sheets-note.model.ts b/packages/sheets-note/src/models/sheets-note.model.ts
index 0c91f66280..c081e06071 100644
--- a/packages/sheets-note/src/models/sheets-note.model.ts
+++ b/packages/sheets-note/src/models/sheets-note.model.ts
@@ -123,17 +123,17 @@ export class SheetsNoteModel extends Disposable {
}
updateNote(unitId: string, subUnitId: string, row: number, col: number, note: Partial<ISheetNote>, silent?: boolean): void {
- const _note = { ...note, row, col } as ISheetNote;
- if (!note.id) {
- _note.id = generateRandomId(6);
- }
-
+ const oldNote = this._getNoteByParams(unitId, subUnitId, { noteId: note?.id, row, col });
const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
- const noteId = _note.id;
- const oldNote = this._getNoteById(unitId, subUnitId, noteId);
- subUnitMap.set(noteId, _note);
+ const newNote = {
+ ...note,
+ id: oldNote?.id || note.id || generateRandomId(6),
+ row,
+ col,
+ } as ISheetNote;
+ subUnitMap.set(newNote.id, newNote);
- this._change$.next({ unitId, subUnitId, oldNote, type: 'update', newNote: _note, silent });
+ this._change$.next({ unitId, subUnitId, oldNote, type: 'update', newNote, silent });
}
removeNote(unitId: string, subUnitId: string, params: { noteId?: string; row?: number; col?: number; silent?: boolean }): void {
@@ -180,8 +180,8 @@ export class SheetsNoteModel extends Disposable {
this._change$.next({ unitId, subUnitId, oldNote, type: 'ref', newNote, silent });
}
- getNote(unitId: string, subUnitId: string, row: number, col: number): Nullable<ISheetNote> {
- return this._getNoteByPosition(unitId, subUnitId, row, col);
+ getNote(unitId: string, subUnitId: string, params: { noteId?: string; row?: number; col?: number }): Nullable<ISheetNote> {
+ return this._getNoteByParams(unitId, subUnitId, params);
}
getNotes() {