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.

refactor(note): refactor the SheetsNoteModel data structure and make it compatible with the old data structure (#6561)

wpxp1234569ba20ee4658c
diff --git a/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts b/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts
index 4765c6d86f..0568173342 100644
--- a/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts
+++ b/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts
@@ -341,6 +341,8 @@ export class DocDrawingUpdateRenderController extends Disposable implements IRen
// Update drawings edit status and opacity. You can not edit header footer images when you are editing body. and vice verse.
private _updateDrawingsEditStatus() {
+ if (!this._context) return;
+
const { unit: docDataModel, scene, unitId } = this._context;
const viewModel = this._renderManagerSrv
.getRenderById(unitId)
diff --git a/packages/sheets-note-ui/src/controllers/sheets-note-attachment.controller.ts b/packages/sheets-note-ui/src/controllers/sheets-note-attachment.controller.ts
index aa12b0b9ca..27599ef755 100644
--- a/packages/sheets-note-ui/src/controllers/sheets-note-attachment.controller.ts
+++ b/packages/sheets-note-ui/src/controllers/sheets-note-attachment.controller.ts
@@ -96,35 +96,39 @@ export class SheetsNoteAttachmentController extends Disposable {
}
};
- this._sheetsNoteModel.getSheetNotes(targetUnitId, targetSheetId)?.forValue((row, col, note) => {
- handleNote(targetUnitId, targetSheetId, row, col, note);
+ this._sheetsNoteModel.getSheetNotes(targetUnitId, targetSheetId)?.forEach((note) => {
+ handleNote(targetUnitId, targetSheetId, note.row, note.col, note);
});
return this._sheetsNoteModel.change$.subscribe((change) => {
- if (change.unitId !== targetUnitId || change.sheetId !== targetSheetId) {
+ if (change.unitId !== targetUnitId || change.subUnitId !== targetSheetId) {
return;
}
switch (change.type) {
case 'ref': {
- const { unitId, sheetId, row, col, newPosition, note } = change;
+ const { unitId, subUnitId, oldNote, newNote } = change;
+ if (!newNote.show) return;
+
const matrix = this._noteMatrix;
- if (!note.show) return;
+ const { row: oldRow, col: oldCol } = oldNote!;
+ const { row: newRow, col: newCol } = newNote;
- const disposable = matrix.getValue(row, col);
+ const disposable = matrix.getValue(oldRow, oldCol);
if (disposable) {
disposable.dispose();
- matrix.realDeleteValue(row, col);
+ matrix.realDeleteValue(oldRow, oldCol);
}
- const newDisposable = this._showPopup(unitId, sheetId, newPosition.row, newPosition.col);
+ const newDisposable = this._showPopup(unitId, subUnitId, newRow, newCol);
if (newDisposable) {
- matrix.setValue(newPosition.row, newPosition.col, newDisposable);
+ matrix.setValue(newRow, newCol, newDisposable);
}
break;
}
-
case 'update': {
- const { unitId, sheetId, row, col, note } = change;
- handleNote(unitId, sheetId, row, col, note);
+ const { unitId, subUnitId, oldNote, newNote } = change;
+ const row = newNote ? newNote.row : oldNote!.row;
+ const col = newNote ? newNote.col : oldNote!.col;
+ handleNote(unitId, subUnitId, row, col, newNote);
break;
}
default:
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 1062252725..0d7a2e84f5 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
@@ -121,14 +121,16 @@ export class SheetsNotePopupController extends Disposable {
const { startRow, endRow, startColumn, endColumn } = actualCell.mergeInfo;
if (startRow !== endRow || startColumn !== endColumn) {
const sheetNotes = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);
- sheetNotes?.forValue((r, c, candidate) => {
- if (r >= startRow && r <= endRow && c >= startColumn && c <= endColumn) {
- note = candidate;
- targetRow = r;
- targetCol = c;
- return false;
+ if (sheetNotes) {
+ for (const [_id, _note] of sheetNotes) {
+ if (_note.row >= startRow && _note.row <= endRow && _note.col >= startColumn && _note.col <= endColumn) {
+ note = _note;
+ targetRow = _note.row;
+ targetCol = _note.col;
+ break;
+ }
}
- });
+ }
}
}
if (note?.show) return;
diff --git a/packages/sheets-note-ui/src/views/note.tsx b/packages/sheets-note-ui/src/views/note.tsx
index dd61a0015d..d476ef6bd0 100644
--- a/packages/sheets-note-ui/src/views/note.tsx
+++ b/packages/sheets-note-ui/src/views/note.tsx
@@ -47,7 +47,7 @@ export const SheetsNote = (props: { popup: IPopup<{ location: INotePopupLocation
const textareaRef = useRef<HTMLTextAreaElement>(null);
const currentRender = renderManagerService.getRenderById(activePopup.unitId)!;
- const [note, setNote] = useState<ISheetNote | null>(null);
+ const [note, setNote] = useState<Partial<ISheetNote> | null>(null);
useEffect(() => {
const { unitId, subUnitId, row, col } = activePopup;
@@ -76,7 +76,7 @@ export const SheetsNote = (props: { popup: IPopup<{ location: INotePopupLocation
}, [activePopup]);
const commandService = useDependency(ICommandService);
- const updateNote = useDebounceFn((newNote: ISheetNote) => {
+ const updateNote = useDebounceFn((newNote: Partial<ISheetNote>) => {
if (!activePopup) return;
if (newNote.note) {
diff --git a/packages/sheets-note/src/commands/mutations/note.mutation.ts b/packages/sheets-note/src/commands/mutations/note.mutation.ts
index 3915b38605..ac5c70eed5 100644
--- a/packages/sheets-note/src/commands/mutations/note.mutation.ts
+++ b/packages/sheets-note/src/commands/mutations/note.mutation.ts
@@ -42,8 +42,9 @@ export const UpdateNoteMutation: IMutation<IUpdateNoteMutationParams> = {
export interface IRemoveNoteMutationParams {
unitId: string;
sheetId: string;
- row: number;
- col: number;
+ noteId?: string;
+ row?: number;
+ col?: number;
silent?: boolean;
}
@@ -51,9 +52,9 @@ export const RemoveNoteMutation: IMutation<IRemoveNoteMutationParams> = {
id: 'sheet.mutation.remove-note',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: IRemoveNoteMutationParams) => {
- const { unitId, sheetId, row, col, silent } = params;
+ const { unitId, sheetId, noteId, row, col, silent } = params;
const sheetsNoteModel = accessor.get(SheetsNoteModel);
- sheetsNoteModel.removeNote(unitId, sheetId, row, col, silent);
+ sheetsNoteModel.removeNote(unitId, sheetId, { noteId, row, col, silent });
return true;
},
};
@@ -61,8 +62,9 @@ export const RemoveNoteMutation: IMutation<IRemoveNoteMutationParams> = {
export interface IToggleNotePopupMutationParams {
unitId: string;
sheetId: string;
- row: number;
- col: number;
+ noteId?: string;
+ row?: number;
+ col?: number;
silent?: boolean;
}
@@ -70,9 +72,9 @@ export const ToggleNotePopupMutation: IMutation<IToggleNotePopupMutationParams>
id: 'sheet.mutation.toggle-note-popup',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: IToggleNotePopupMutationParams) => {
- const { unitId, sheetId, row, col, silent } = params;
+ const { unitId, sheetId, noteId, row, col, silent } = params;
const sheetsNoteModel = accessor.get(SheetsNoteModel);
- sheetsNoteModel.toggleNotePopup(unitId, sheetId, row, col, silent);
+ sheetsNoteModel.toggleNotePopup(unitId, sheetId, { noteId, row, col, silent });
return true;
},
};
@@ -80,8 +82,9 @@ export const ToggleNotePopupMutation: IMutation<IToggleNotePopupMutationParams>
export interface IUpdateNotePositionMutationParams {
unitId: string;
sheetId: string;
- row: number;
- col: number;
+ noteId?: string;
+ row?: number;
+ col?: number;
newPosition: {
row: number;
col: number;
@@ -93,9 +96,9 @@ export const UpdateNotePositionMutation: IMutation<IUpdateNotePositionMutationPa
id: 'sheet.mutation.update-note-position',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: IUpdateNotePositionMutationParams) => {
- const { unitId, sheetId, row, col, newPosition, silent } = params;
+ const { unitId, sheetId, noteId, row, col, newPosition, silent } = params;
const sheetsNoteModel = accessor.get(SheetsNoteModel);
- sheetsNoteModel.updateNotePosition(unitId, sheetId, row, col, newPosition.row, newPosition.col, silent);
+ sheetsNoteModel.updateNotePosition(unitId, sheetId, { noteId, row, col, newRow: newPosition.row, newCol: newPosition.col, silent });
return true;
},
};
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
new file mode 100644
index 0000000000..cf3cbbea64
--- /dev/null
+++ b/packages/sheets-note/src/controllers/__tests__/ref-range.controller.spec.ts
@@ -0,0 +1,253 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { Dependency, IWorkbookData, Workbook } from '@univerjs/core';
+import type { IInsertColCommandParams, IInsertRowCommandParams, IRemoveColByRangeCommandParams, IRemoveRowByRangeCommandParams } from '@univerjs/sheets';
+import { Direction, ICommandService, ILogService, Inject, Injector, IUndoRedoService, IUniverInstanceService, LocaleType, LogLevel, Plugin, touchDependencies, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
+import {
+ InsertColByRangeCommand,
+ InsertColMutation,
+ InsertRowByRangeCommand,
+ InsertRowMutation,
+ RefRangeService,
+ RemoveColByRangeCommand,
+ RemoveColMutation,
+ RemoveRowByRangeCommand,
+ RemoveRowMutation,
+ SetRangeValuesMutation,
+ SetSelectionsOperation,
+ SheetInterceptorService,
+ SheetsSelectionsService,
+} from '@univerjs/sheets';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { SheetsNoteModel } from '../../models/sheets-note.model';
+import { SheetsNoteRefRangeController } from '../sheets-note-ref-range.controller';
+import { SheetsNoteResourceController } from '../sheets-note-resource.controller';
+import { SheetsNoteController } from '../sheets.note.controller';
+
+const unitId = 'test';
+const subUnitId = 'sheet1';
+const TEST_WORKBOOK_DATA: IWorkbookData = {
+ id: unitId,
+ sheetOrder: [subUnitId],
+ name: '',
+ appVersion: '0.15.4',
+ locale: LocaleType.EN_US,
+ styles: {},
+ sheets: {
+ [subUnitId]: {
+ id: subUnitId,
+ name: 'Sheet1',
+ rowCount: 10,
+ columnCount: 10,
+ cellData: {},
+ },
+ },
+ resources: [
+ {
+ name: 'SHEET_NOTE_PLUGIN',
+ data: '{"sheet1":{"1":{"1":{"width":160,"height":72,"note":"1","row":1,"col":1,"id":"cwvCsB"}},"2":{"2":{"width":160,"height":72,"note":"2","row":2,"col":2,"id":"URAqOO"}},"3":{"3":{"width":160,"height":72,"note":"3","row":3,"col":3,"id":"tCk4Qd"}},"4":{"4":{"width":160,"height":72,"note":"4","row":4,"col":4,"id":"nIasaz"}}}}',
+ },
+ ],
+};
+
+export function createRefRangeTestBed() {
+ const univer = new Univer();
+ const injector = univer.__getInjector();
+ const get = injector.get.bind(injector);
+
+ class TestPlugin extends Plugin {
+ static override pluginName = 'test-plugin';
+ static override type = UniverInstanceType.UNIVER_SHEET;
+
+ constructor(
+ _config: undefined,
+ @Inject(Injector) override readonly _injector: Injector
+ ) {
+ super();
+ }
+
+ override onStarting(): void {
+ this._injector.get(IUndoRedoService);
+
+ ([
+ [SheetInterceptorService],
+ [RefRangeService],
+ [SheetsSelectionsService],
+ [SheetsNoteModel],
+ [SheetsNoteController],
+ [SheetsNoteResourceController],
+ [SheetsNoteRefRangeController],
+ ] as Dependency[]).forEach((d) => this._injector.add(d));
+
+ touchDependencies(this._injector, [
+ [SheetsNoteModel],
+ [SheetsNoteController],
+ [SheetsNoteResourceController],
+ ]);
+ }
+
+ override onReady(): void {
+ touchDependencies(this._injector, [
+ [SheetsNoteRefRangeController],
+ ]);
+ }
+ }
+
+ univer.registerPlugin(TestPlugin);
+ const sheet = univer.createUnit<IWorkbookData, Workbook>(UniverInstanceType.UNIVER_SHEET, TEST_WORKBOOK_DATA);
+
+ const univerInstanceService = get(IUniverInstanceService);
+ univerInstanceService.focusUnit('test');
+
+ const logService = get(ILogService);
+ logService.setLogLevel(LogLevel.SILENT);
+
+ return {
+ univer,
+ get,
+ sheet,
+ };
+}
+
+describe('test note ref range controller', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let sheetsNoteModel: SheetsNoteModel;
+
+ beforeEach(() => {
+ const testBed = createRefRangeTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(InsertRowByRangeCommand);
+ commandService.registerCommand(InsertColByRangeCommand);
+ commandService.registerCommand(RemoveRowByRangeCommand);
+ commandService.registerCommand(RemoveColByRangeCommand);
+ commandService.registerCommand(InsertRowMutation);
+ commandService.registerCommand(InsertColMutation);
+ commandService.registerCommand(RemoveRowMutation);
+ commandService.registerCommand(RemoveColMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
+ commandService.registerCommand(SetSelectionsOperation);
+
+ sheetsNoteModel = get(SheetsNoteModel);
+ });
+
+ afterEach(() => univer.dispose());
+
+ describe('test note ref range change', () => {
+ it('test D4 cell note when insert row', async () => {
+ const D4CellNote = {
+ width: 160,
+ height: 72,
+ note: '3',
+ row: 3,
+ col: 3,
+ id: 'tCk4Qd',
+ };
+
+ const params: IInsertRowCommandParams = {
+ unitId,
+ subUnitId,
+ range: { startRow: 3, endRow: 3, startColumn: 0, endColumn: 9 },
+ 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(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 4, 3)).toBeUndefined();
+ });
+
+ it('test D4 cell note when insert column', async () => {
+ const D4CellNote = {
+ width: 160,
+ height: 72,
+ note: '3',
+ row: 3,
+ col: 3,
+ id: 'tCk4Qd',
+ };
+
+ const params: IInsertColCommandParams = {
+ unitId,
+ subUnitId,
+ range: { startRow: 0, endRow: 9, startColumn: 3, endColumn: 3 },
+ 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(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 4)).toBeUndefined();
+ });
+
+ it('test D4 cell note when remove row', async () => {
+ const D4CellNote = {
+ width: 160,
+ height: 72,
+ note: '3',
+ row: 3,
+ col: 3,
+ id: 'tCk4Qd',
+ };
+
+ const params: IRemoveRowByRangeCommandParams = {
+ unitId,
+ subUnitId,
+ 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(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 2, 3)).toBeUndefined();
+ });
+
+ it('test D4 cell note when remove column', async () => {
+ const D4CellNote = {
+ width: 160,
+ height: 72,
+ note: '3',
+ row: 3,
+ col: 3,
+ id: 'tCk4Qd',
+ };
+
+ const params: IRemoveColByRangeCommandParams = {
+ unitId,
+ subUnitId,
+ 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(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 3)).toStrictEqual(D4CellNote);
+ expect(sheetsNoteModel.getNote(unitId, subUnitId, 3, 2)).toBeUndefined();
+ });
+ });
+});
diff --git a/packages/sheets-note/src/controllers/sheets-note-ref-range.controller.ts b/packages/sheets-note/src/controllers/sheets-note-ref-range.controller.ts
index aecaba4f34..d779609986 100644
--- a/packages/sheets-note/src/controllers/sheets-note-ref-range.controller.ts
+++ b/packages/sheets-note/src/controllers/sheets-note-ref-range.controller.ts
@@ -49,8 +49,7 @@ export class SheetsNoteRefRangeController extends Disposable {
params: {
unitId,
sheetId: subUnitId,
- row,
- col,
+ noteId: note.id,
},
}],
undos: [{
@@ -72,8 +71,7 @@ export class SheetsNoteRefRangeController extends Disposable {
params: {
unitId,
sheetId: subUnitId,
- row,
- col,
+ noteId: note.id,
newPosition: {
row: resultRange.startRow,
col: resultRange.startColumn,
@@ -86,8 +84,7 @@ export class SheetsNoteRefRangeController extends Disposable {
params: {
unitId,
sheetId: subUnitId,
- row: resultRange.startRow,
- col: resultRange.startColumn,
+ noteId: note.id,
newPosition: {
row,
col,
@@ -156,13 +153,10 @@ export class SheetsNoteRefRangeController extends Disposable {
private _initData() {
const unitNotes = this._sheetsNoteModel.getNotes();
for (const [unitId, unitNote] of unitNotes) {
- for (const [sheetId, matrix] of unitNote) {
- matrix.forValue((row: number, col: number, value: ISheetNote) => {
- if (value) {
- this._register(unitId, sheetId, value, row, col);
- this._watch(unitId, sheetId, value, row, col);
- }
- return true;
+ for (const [subUnitId, notes] of unitNote) {
+ notes.forEach((note) => {
+ this._register(unitId, subUnitId, note, note.row, note.col);
+ this._watch(unitId, subUnitId, note, note.row, note.col);
});
}
}
@@ -173,29 +167,34 @@ export class SheetsNoteRefRangeController extends Disposable {
this._sheetsNoteModel.change$.subscribe((option) => {
switch (option.type) {
case 'update': {
- const { unitId, sheetId, row, col, note } = option;
- const id = this._getIdWithUnitId(unitId, sheetId, row, col);
- if (note) {
+ const { unitId, subUnitId, oldNote, newNote } = option;
+ const row = newNote ? newNote.row : oldNote!.row;
+ const col = newNote ? newNote.col : oldNote!.col;
+ const id = this._getIdWithUnitId(unitId, subUnitId, row, col);
+ if (newNote) {
if (!this._disposableMap.has(id)) {
- this._register(unitId, sheetId, note, row, col);
- this._watch(unitId, sheetId, note, row, col);
+ this._register(unitId, subUnitId, newNote, row, col);
+ this._watch(unitId, subUnitId, newNote, row, col);
}
} else {
- this._unregister(unitId, sheetId, row, col);
- this._unwatch(unitId, sheetId, row, col);
+ this._unregister(unitId, subUnitId, row, col);
+ this._unwatch(unitId, subUnitId, row, col);
}
break;
}
case 'ref': {
- const { unitId, sheetId, row, col, newPosition, note, silent } = option;
- this._unregister(unitId, sheetId, row, col);
+ const { unitId, subUnitId, oldNote, newNote, silent } = option;
+ const { row: oldRow, col: oldCol } = oldNote!;
+ const { row: newRow, col: newCol } = newNote;
+
+ this._unregister(unitId, subUnitId, oldRow, oldCol);
if (!silent) {
- this._unwatch(unitId, sheetId, row, col);
- this._watch(unitId, sheetId, note, newPosition.row, newPosition.col);
+ this._unwatch(unitId, subUnitId, oldRow, oldCol);
+ this._watch(unitId, subUnitId, newNote, newRow, newCol);
}
- this._register(unitId, sheetId, note, newPosition.row, newPosition.col);
+ this._register(unitId, subUnitId, newNote, newRow, newCol);
break;
}
}
diff --git a/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts b/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts
index ba2cf5bfd1..e3cca5f28f 100644
--- a/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts
+++ b/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts
@@ -17,7 +17,7 @@
import type { IMutationInfo, Workbook } from '@univerjs/core';
import type { ICopySheetCommandParams, IRemoveSheetCommandParams } from '@univerjs/sheets';
import type { ISheetNote } from '../models/sheets-note.model';
-import { Disposable, Inject, IResourceManagerService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { Disposable, generateRandomId, Inject, IResourceManagerService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { CopySheetCommand, RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';
import { RemoveNoteMutation, UpdateNoteMutation } from '../commands/mutations/note.mutation';
import { PLUGIN_NAME } from '../const';
@@ -51,16 +51,20 @@ export class SheetsNoteResourceController extends Disposable {
}
const result: INoteData = {};
- unitMap.forEach((matrix, sheetId) => {
+
+ unitMap.forEach((subUnitMap, subUnitId) => {
const sheetNotes: INoteData[string] = {};
- matrix.forValue((row, col, note) => {
+
+ subUnitMap.forEach((note) => {
+ const { row, col } = note;
if (!sheetNotes[row]) {
sheetNotes[row] = {};
}
sheetNotes[row][col] = note;
});
+
if (Object.keys(sheetNotes).length > 0) {
- result[sheetId] = sheetNotes;
+ result[subUnitId] = sheetNotes;
}
});
@@ -123,23 +127,24 @@ export class SheetsNoteResourceController extends Disposable {
return { redos: [], undos: [] };
}
- const noteMatrix = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);
+ const sheetNotes = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);
- if (!noteMatrix) {
+ if (!sheetNotes) {
return { redos: [], undos: [] };
}
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
- noteMatrix.forValue((row, col, note) => {
+ sheetNotes.forEach((note) => {
redos.push({
id: RemoveNoteMutation.id,
params: {
unitId,
sheetId: subUnitId,
- row,
- col,
+ noteId: note.id,
+ row: note.row,
+ col: note.col,
},
});
undos.push({
@@ -147,8 +152,8 @@ export class SheetsNoteResourceController extends Disposable {
params: {
unitId,
sheetId: subUnitId,
- row,
- col,
+ row: note.row,
+ col: note.col,
note,
},
});
@@ -163,24 +168,26 @@ export class SheetsNoteResourceController extends Disposable {
return { redos: [], undos: [] };
}
- const noteMatrix = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);
+ const sheetNotes = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);
- if (!noteMatrix) {
+ if (!sheetNotes) {
return { redos: [], undos: [] };
}
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
- noteMatrix.forValue((row, col, note) => {
+ sheetNotes.forEach((note) => {
+ const newNote = { ...note, id: generateRandomId(6) }; // Generate new ID for the copied note
+
redos.push({
id: UpdateNoteMutation.id,
params: {
unitId,
sheetId: targetSubUnitId,
- row,
- col,
- note,
+ row: newNote.row,
+ col: newNote.col,
+ note: newNote,
},
});
undos.push({
@@ -188,8 +195,9 @@ export class SheetsNoteResourceController extends Disposable {
params: {
unitId,
sheetId: targetSubUnitId,
- row,
- col,
+ noteId: newNote.id,
+ row: newNote.row,
+ col: newNote.col,
},
});
});
diff --git a/packages/sheets-note/src/facade/f-univer.ts b/packages/sheets-note/src/facade/f-univer.ts
index e64894b5e4..937c508825 100644
--- a/packages/sheets-note/src/facade/f-univer.ts
+++ b/packages/sheets-note/src/facade/f-univer.ts
@@ -38,9 +38,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
() => {
const model = injector.get(SheetsNoteModel);
return model.change$.subscribe((change) => {
- if (change.type === 'update' && !change.oldNote && change.note) {
- const { unitId, sheetId, row, col, note } = change;
- const target = this.getSheetTarget(unitId, sheetId);
+ if (change.type === 'update' && !change.oldNote && change.newNote) {
+ const { unitId, subUnitId, newNote } = change;
+ const target = this.getSheetTarget(unitId, subUnitId);
if (!target) {
return;
}
@@ -49,9 +49,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.fireEvent(this.Event.SheetNoteAdd, {
workbook,
worksheet,
- row,
- col,
- note,
+ row: newNote.row,
+ col: newNote.col,
+ note: newNote,
});
}
});
@@ -65,9 +65,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
() => {
const model = injector.get(SheetsNoteModel);
return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && !change.note) {
- const { unitId, sheetId, row, col, oldNote } = change;
- const target = this.getSheetTarget(unitId, sheetId);
+ if (change.type === 'update' && change.oldNote && !change.newNote) {
+ const { unitId, subUnitId, oldNote } = change;
+ const target = this.getSheetTarget(unitId, subUnitId);
if (!target) {
return;
}
@@ -76,8 +76,8 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.fireEvent(this.Event.SheetNoteDelete, {
workbook,
worksheet,
- row,
- col,
+ row: oldNote.row,
+ col: oldNote.col,
oldNote,
});
}
@@ -92,9 +92,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
() => {
const model = injector.get(SheetsNoteModel);
return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && change.note) {
- const { unitId, sheetId, row, col, note, oldNote } = change;
- const target = this.getSheetTarget(unitId, sheetId);
+ if (change.type === 'update' && change.oldNote && change.newNote) {
+ const { unitId, subUnitId, oldNote, newNote } = change;
+ const target = this.getSheetTarget(unitId, subUnitId);
if (!target) {
return;
}
@@ -103,9 +103,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.fireEvent(this.Event.SheetNoteUpdate, {
workbook,
worksheet,
- row,
- col,
- note,
+ row: newNote.row,
+ col: newNote.col,
+ note: newNote,
oldNote,
});
}
@@ -120,9 +120,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
() => {
const model = injector.get(SheetsNoteModel);
return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && change.note && !change.oldNote.show && change.note.show) {
- const { unitId, sheetId, row, col } = change;
- const target = this.getSheetTarget(unitId, sheetId);
+ if (change.type === 'update' && change.oldNote && change.newNote && !change.oldNote.show && change.newNote.show) {
+ const { unitId, subUnitId, newNote } = change;
+ const target = this.getSheetTarget(unitId, subUnitId);
if (!target) {
return;
}
@@ -131,8 +131,8 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.fireEvent(this.Event.SheetNoteShow, {
workbook,
worksheet,
- row,
- col,
+ row: newNote.row,
+ col: newNote.col,
});
}
});
@@ -146,9 +146,9 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
() => {
const model = injector.get(SheetsNoteModel);
return model.change$.subscribe((change) => {
- if (change.type === 'update' && change.oldNote && change.note && change.oldNote.show && !change.note.show) {
- const { unitId, sheetId, row, col } = change;
- const target = this.getSheetTarget(unitId, sheetId);
+ if (change.type === 'update' && change.oldNote && change.newNote && change.oldNote.show && !change.newNote.show) {
+ const { unitId, subUnitId, newNote } = change;
+ const target = this.getSheetTarget(unitId, subUnitId);
if (!target) {
return;
}
@@ -157,8 +157,8 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
this.fireEvent(this.Event.SheetNoteHide, {
workbook,
worksheet,
- row,
- col,
+ row: newNote.row,
+ col: newNote.col,
});
}
});
@@ -202,12 +202,14 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
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 (!target) return;
+
const { workbook, worksheet } = target;
const cancel = this.fireEvent(this.Event.BeforeSheetNoteDelete, {
workbook,
@@ -216,6 +218,7 @@ export class FUniverSheetNoteMixin extends FUniver implements IFUniverSheetNoteM
col,
oldNote,
});
+
if (cancel) {
throw new CanceledError();
}
diff --git a/packages/sheets-note/src/facade/f-worksheet.ts b/packages/sheets-note/src/facade/f-worksheet.ts
index 3a6640e836..1c3db23dde 100644
--- a/packages/sheets-note/src/facade/f-worksheet.ts
+++ b/packages/sheets-note/src/facade/f-worksheet.ts
@@ -18,18 +18,13 @@ import type { ISheetNote } from '@univerjs/sheets-note';
import { SheetsNoteModel } from '@univerjs/sheets-note';
import { FWorksheet } from '@univerjs/sheets/facade';
-export interface ISheetNoteInfo extends ISheetNote {
- row: number;
- col: number;
-}
-
/**
* @ignore
*/
export interface IFSheetsNoteWorksheet {
/**
* Get all annotations in the worksheet
- * @returns {ISheetNoteInfo[]} An array of all annotations in the worksheet
+ * @returns {ISheetNote[]} An array of all annotations in the worksheet
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
@@ -43,24 +38,17 @@ export interface IFSheetsNoteWorksheet {
* });
* ```
*/
- getNotes(): ISheetNoteInfo[];
+ getNotes(): ISheetNote[];
}
export class FSheetsNoteWorksheet extends FWorksheet implements IFSheetsNoteWorksheet {
- override getNotes(): ISheetNoteInfo[] {
+ override getNotes(): ISheetNote[] {
const model = this._injector.get(SheetsNoteModel);
const notes = model.getSheetNotes(this.getWorkbook().getUnitId(), this.getSheetId());
- const arr: ISheetNoteInfo[] = [];
-
- notes?.forValue((row, col, note) => {
- arr.push({
- ...note,
- row,
- col,
- });
- });
-
- return arr;
+ if (!notes) {
+ return [];
+ }
+ return Array.from(notes.values()).map((note) => ({ ...note }));
}
}
diff --git a/packages/sheets-note/src/models/sheets-note.model.ts b/packages/sheets-note/src/models/sheets-note.model.ts
index ff054e567a..0c91f66280 100644
--- a/packages/sheets-note/src/models/sheets-note.model.ts
+++ b/packages/sheets-note/src/models/sheets-note.model.ts
@@ -16,10 +16,13 @@
import type { Nullable } from '@univerjs/core';
import type { ISheetLocationBase } from '@univerjs/sheets';
-import { Disposable, ObjectMatrix } from '@univerjs/core';
+import { Disposable, generateRandomId } from '@univerjs/core';
import { filter, map, Subject } from 'rxjs';
export interface ISheetNote {
+ id: string;
+ row: number;
+ col: number;
width: number;
height: number;
note: string;
@@ -28,144 +31,176 @@ export interface ISheetNote {
export type ISheetNoteChange = {
unitId: string;
- sheetId: string;
- row: number;
- col: number;
+ subUnitId: string;
+ oldNote: Nullable<ISheetNote>;
silent?: boolean;
} & ({
type: 'update';
- note: Nullable<ISheetNote>;
- oldNote: Nullable<ISheetNote>;
+ newNote: Nullable<ISheetNote>;
} | {
type: 'ref';
- newPosition: {
- row: number;
- col: number;
- };
- note: ISheetNote;
+ newNote: ISheetNote;
});
export class SheetsNoteModel extends Disposable {
- private _noteMatrix = new Map<string, Map<string, ObjectMatrix<ISheetNote>>>();
+ private _notesMap: Map<string, Map<string, Map<string, ISheetNote>>> = new Map();
private readonly _change$ = new Subject<ISheetNoteChange>();
readonly change$ = this._change$.asObservable();
- private _ensureNoteMatrix(unitId: string, sheetId: string): ObjectMatrix<ISheetNote> {
- let unitMap = this._noteMatrix.get(unitId);
+ private _ensureNotesMap(unitId: string, subUnitId: string): Map<string, ISheetNote> {
+ let unitMap = this._notesMap.get(unitId);
if (!unitMap) {
unitMap = new Map();
- this._noteMatrix.set(unitId, unitMap);
+ this._notesMap.set(unitId, unitMap);
+ }
+
+ let subUnitMap = unitMap.get(subUnitId);
+ if (!subUnitMap) {
+ subUnitMap = new Map();
+ unitMap.set(subUnitId, subUnitMap);
+ }
+
+ return subUnitMap;
+ }
+
+ private _getNoteByPosition(unitId: string, subUnitId: string, row: number, col: number): Nullable<ISheetNote> {
+ const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
+
+ for (const [_, note] of subUnitMap) {
+ if (note.row === row && note.col === col) {
+ return note;
+ }
+ }
+ }
+
+ private _getNoteById(unitId: string, subUnitId: string, id: string): Nullable<ISheetNote> {
+ const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
+ return subUnitMap.get(id);
+ }
+
+ private _getNoteByParams(unitId: string, subUnitId: string, params: { noteId?: string; row?: number; col?: number }): Nullable<ISheetNote> {
+ const { noteId, row, col } = params;
+
+ if (noteId) {
+ return this._getNoteById(unitId, subUnitId, noteId);
}
- let matrix = unitMap.get(sheetId);
- if (!matrix) {
- matrix = new ObjectMatrix<ISheetNote>();
- unitMap.set(sheetId, matrix);
+ if (row !== undefined && col !== undefined) {
+ return this._getNoteByPosition(unitId, subUnitId, row, col);
}
- return matrix;
+ return null;
}
- getSheetShowNotes$(unitId: string, sheetId: string) {
+ getSheetShowNotes$(unitId: string, subUnitId: string) {
return this._change$.pipe(
- filter(({ unitId: u, sheetId: s }) => u === unitId && s === sheetId),
+ filter(({ unitId: u, subUnitId: s }) => u === unitId && s === subUnitId),
map(() => {
- const matrix = this._ensureNoteMatrix(unitId, sheetId);
+ const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
const notes: { loc: ISheetLocationBase; note: ISheetNote }[] = [];
- matrix.forValue((row, col, note) => {
+ for (const [_, note] of subUnitMap) {
if (note.show) {
- notes.push({ loc: { row, col, unitId, subUnitId: sheetId }, note });
+ notes.push({ loc: { row: note.row, col: note.col, unitId, subUnitId }, note });
}
- });
+ }
return notes;
})
);
}
- getCellNoteChange$(unitId: string, sheetId: string, row: number, col: number) {
+ getCellNoteChange$(unitId: string, subUnitId: string, row: number, col: number) {
return this._change$.pipe(
- filter(({ unitId: u, sheetId: s, row: r, col: c }) => u === unitId && s === sheetId && r === row && c === col),
- map(({ note }) => note)
+ filter(({ unitId: u, subUnitId: s, oldNote }) => {
+ if (u !== unitId || s !== subUnitId || !oldNote) {
+ return false;
+ }
+ return oldNote.row === row && oldNote.col === col;
+ }),
+ map((newNote) => newNote)
);
}
- updateNote(unitId: string, sheetId: string, row: number, col: number, note: ISheetNote, silent?: boolean): void {
- const matrix = this._ensureNoteMatrix(unitId, sheetId);
- const oldNote = matrix.getValue(row, col);
- matrix.setValue(row, col, note);
- this._change$.next({ unitId, sheetId, row, col, type: 'update', note, oldNote, silent });
+ 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 subUnitMap = this._ensureNotesMap(unitId, subUnitId);
+ const noteId = _note.id;
+ const oldNote = this._getNoteById(unitId, subUnitId, noteId);
+ subUnitMap.set(noteId, _note);
+
+ this._change$.next({ unitId, subUnitId, oldNote, type: 'update', newNote: _note, silent });
}
- removeNote(unitId: string, sheetId: string, row: number, col: number, silent?: boolean): void {
- const matrix = this._ensureNoteMatrix(unitId, sheetId);
- const oldNote = matrix.getValue(row, col);
- matrix.realDeleteValue(row, col);
- this._change$.next({ unitId, sheetId, row, col, type: 'update', note: null, oldNote, silent });
+ removeNote(unitId: string, subUnitId: string, params: { noteId?: string; row?: number; col?: number; silent?: boolean }): void {
+ const { noteId, row, col, silent } = params;
+ const oldNote = this._getNoteByParams(unitId, subUnitId, { noteId, row, col });
+
+ if (!oldNote) {
+ return;
+ }
+
+ const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
+ subUnitMap.delete(oldNote.id);
+
+ this._change$.next({ unitId, subUnitId, oldNote, type: 'update', newNote: null, silent });
}
- toggleNotePopup(unitId: string, sheetId: string, row: number, col: number, silent?: boolean): void {
- const matrix = this._ensureNoteMatrix(unitId, sheetId);
- const note = matrix.getValue(row, col);
- if (note) {
- note.show = !note.show;
- const newNote = { ...note, show: note.show };
- matrix.setValue(row, col, newNote);
- this._change$.next({
- unitId,
- sheetId,
- row,
- col,
- type: 'update',
- note: newNote,
- oldNote: note,
- silent,
- });
+ toggleNotePopup(unitId: string, subUnitId: string, params: { noteId?: string; row?: number; col?: number; silent?: boolean }): void {
+ const { noteId, row, col, silent } = params;
+ const oldNote = this._getNoteByParams(unitId, subUnitId, { noteId, row, col });
+
+ if (!oldNote) {
+ return;
}
+
+ const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
+ const newNote = { ...oldNote, show: !oldNote.show };
+ subUnitMap.set(newNote.id, newNote);
+
+ this._change$.next({ unitId, subUnitId, oldNote, type: 'update', newNote, silent });
}
- updateNotePosition(unitId: string, sheetId: string, row: number, col: number, newRow: number, newCol: number, silent?: boolean): void {
- const matrix = this._ensureNoteMatrix(unitId, sheetId);
- const note = matrix.getValue(row, col);
- if (note) {
- matrix.realDeleteValue(row, col);
- matrix.setValue(newRow, newCol, note);
- this._change$.next({
- unitId,
- sheetId,
- row,
- col,
- type: 'ref',
- newPosition: { row: newRow, col: newCol },
- note,
- silent,
- });
+ updateNotePosition(unitId: string, subUnitId: string, params: { noteId?: string; row?: number; col?: number; newRow: number; newCol: number; silent?: boolean }): void {
+ const { noteId, row, col, newRow, newCol, silent } = params;
+ const oldNote = this._getNoteByParams(unitId, subUnitId, { noteId, row, col });
+
+ if (!oldNote) {
+ return;
}
+
+ const subUnitMap = this._ensureNotesMap(unitId, subUnitId);
+ const newNote = { ...oldNote, row: newRow, col: newCol };
+ subUnitMap.set(newNote.id, newNote);
+
+ this._change$.next({ unitId, subUnitId, oldNote, type: 'ref', newNote, silent });
}
- getNote(unitId: string, sheetId: string, row: number, col: number): Nullable<ISheetNote> {
- const matrix = this._ensureNoteMatrix(unitId, sheetId);
- return matrix.getValue(row, col);
+ getNote(unitId: string, subUnitId: string, row: number, col: number): Nullable<ISheetNote> {
+ return this._getNoteByPosition(unitId, subUnitId, row, col);
}
- getUnitNotes(unitId: string): Map<string, ObjectMatrix<ISheetNote>> | undefined {
- return this._noteMatrix.get(unitId);
+ getNotes() {
+ return this._notesMap;
}
- getSheetNotes(unitId: string, sheetId: string): ObjectMatrix<ISheetNote> | undefined {
- const unitMap = this._noteMatrix.get(unitId);
+ getUnitNotes(unitId: string): Map<string, Map<string, ISheetNote>> | undefined {
+ return this._notesMap.get(unitId);
+ }
+
+ getSheetNotes(unitId: string, subUnitId: string): Map<string, ISheetNote> | undefined {
+ const unitMap = this._notesMap.get(unitId);
if (!unitMap) {
return undefined;
}
- return unitMap.get(sheetId);
- }
-
- getNotes() {
- return this._noteMatrix;
+ return unitMap.get(subUnitId);
}
deleteUnitNotes(unitId: string): void {
- this._noteMatrix.delete(unitId);
+ this._notesMap.delete(unitId);
}
}