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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(resources): fix some issues with plugin functions when remove and copy worksheets (#6038)
wpxp123456145272b82527
diff --git a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.tsindex 93fa0f7646..dd81c215db 100644--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts@@ -16,6 +16,7 @@import type { IMutationInfo, IRange, Workbook } from '@univerjs/core';import type {+ ICopySheetCommandParams,IInsertColMutationParams,IMoveColumnsMutationParams,IMoveRangeMutationParams,@@ -25,11 +26,13 @@ import type {IReorderRangeMutationParams,ISetRangeValuesMutationParams,} from '@univerjs/sheets';+import type { IAddConditionalRuleMutationParams } from '../commands/mutations/add-conditional-rule.mutation';import type { IDeleteConditionalRuleMutationParams } from '../commands/mutations/delete-conditional-rule.mutation';import type { IConditionFormattingRule, IHighlightCell, IRuleModelJson } from '../models/type';import type { IDataBarCellData, IDataBarRenderParams, IIconSetCellData, IIconSetRenderParams } from '../render/type';import { Disposable, ICommandService, Inject, Injector, IResourceManagerService, isInternalEditorID, IUniverInstanceService, merge, ObjectMatrix, Rectangle, UniverInstanceType } from '@univerjs/core';import {+ CopySheetCommand,InsertColMutation,InsertRowMutation,MoveColsMutation,@@ -43,6 +46,7 @@ import {SheetInterceptorService,} from '@univerjs/sheets';import { CFRuleType, SHEET_CONDITIONAL_FORMATTING_PLUGIN } from '../base/const';+import { AddConditionalRuleMutation, AddConditionalRuleMutationUndoFactory } from '../commands/mutations/add-conditional-rule.mutation';import { DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory } from '../commands/mutations/delete-conditional-rule.mutation';import { ConditionalFormattingRuleModel } from '../models/conditional-formatting-rule-model';import { ConditionalFormattingViewModel } from '../models/conditional-formatting-view-model';@@ -191,6 +195,36 @@ export class ConditionalFormattingService extends Disposable {redos,undos,};+ } else if (commandInfo.id === CopySheetCommand.id) {+ const params = commandInfo.params as ICopySheetCommandParams & { targetSubUnitId: string };+ const { unitId, subUnitId, targetSubUnitId } = params;++ if (!unitId || !subUnitId || !targetSubUnitId) {+ return { redos: [], undos: [] };+ }++ const ruleList = this._conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId);+ if (!ruleList) {+ return { redos: [], undos: [] };+ }++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ ruleList.forEach((item) => {+ const params: IAddConditionalRuleMutationParams = {+ unitId,+ subUnitId: targetSubUnitId,+ rule: {+ ...item,+ cfId: this._conditionalFormattingRuleModel.createCfId(unitId, targetSubUnitId),+ },+ };+ redos.push({ id: AddConditionalRuleMutation.id, params });+ undos.push(AddConditionalRuleMutationUndoFactory(this._injector, params));+ });++ return { redos, undos };}return { redos: [], undos: [] };},diff --git a/packages/sheets-data-validation/src/controllers/dv-sheet.controller.ts b/packages/sheets-data-validation/src/controllers/dv-sheet.controller.tsindex adbeacf793..0a0f6986c6 100644--- a/packages/sheets-data-validation/src/controllers/dv-sheet.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv-sheet.controller.ts@@ -16,10 +16,10 @@import type { Workbook } from '@univerjs/core';import type { IAddDataValidationMutationParams, IRemoveDataValidationMutationParams } from '@univerjs/data-validation';-import type { IRemoveSheetCommandParams } from '@univerjs/sheets';-import { Disposable, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';+import type { ICopySheetCommandParams, IRemoveSheetCommandParams } from '@univerjs/sheets';+import { Disposable, generateRandomId, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';import { AddDataValidationMutation, RemoveDataValidationMutation } from '@univerjs/data-validation';-import { RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';+import { CopySheetCommand, RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';import { SheetDataValidationModel } from '../models/sheet-data-validation-model';export class SheetDataValidationSheetController extends Disposable {@@ -32,9 +32,11 @@ export class SheetDataValidationSheetController extends Disposable {this._initSheetChange();}+ // eslint-disable-next-line max-lines-per-functionprivate _initSheetChange() {this.disposeWithMe(this._sheetInterceptorService.interceptCommand({+ // eslint-disable-next-line max-lines-per-functiongetMutations: (commandInfo) => {if (commandInfo.id === RemoveSheetCommand.id) {const params = commandInfo.params as IRemoveSheetCommandParams;@@ -79,6 +81,49 @@ export class SheetDataValidationSheetController extends Disposable {params: undoParams,}],};+ } else if (commandInfo.id === CopySheetCommand.id) {+ const params = commandInfo.params as ICopySheetCommandParams & { targetSubUnitId: string };+ const { unitId, subUnitId, targetSubUnitId } = params;++ if (!unitId || !subUnitId || !targetSubUnitId) {+ return { redos: [], undos: [] };+ }++ const rules = this._sheetDataValidationModel.getRules(unitId, subUnitId);++ if (rules.length === 0) {+ return { redos: [], undos: [] };+ }++ const newRules = rules.map((rule) => {+ const newRule = { ...rule, uid: generateRandomId(6) }; // Clear uid to let the system generate a new one+ return newRule;+ });++ return {+ redos: [+ {+ id: AddDataValidationMutation.id,+ params: {+ unitId,+ subUnitId: targetSubUnitId,+ rule: newRules,+ source: 'patched',+ },+ },+ ],+ undos: [+ {+ id: RemoveDataValidationMutation.id,+ params: {+ unitId,+ subUnitId: targetSubUnitId,+ ruleId: newRules.map((r) => r.uid),+ source: 'patched',+ },+ },+ ],+ };}return { redos: [], undos: [] };},diff --git a/packages/sheets-drawing/package.json b/packages/sheets-drawing/package.jsonindex f2fa9ddcc7..a1b61059c8 100644--- a/packages/sheets-drawing/package.json+++ b/packages/sheets-drawing/package.json@@ -57,11 +57,11 @@},"dependencies": {"@univerjs/core": "workspace:*",- "@univerjs/drawing": "workspace:*"+ "@univerjs/drawing": "workspace:*",+ "@univerjs/sheets": "workspace:*"},"devDependencies": {"@univerjs-infra/shared": "workspace:*",- "@univerjs/sheets": "workspace:*","typescript": "^5.9.3","vite": "^7.1.10","vitest": "^3.2.4"diff --git a/packages/sheets-drawing/src/controllers/sheet-drawing.controller.ts b/packages/sheets-drawing/src/controllers/sheet-drawing.controller.tsindex c3cc328247..dec3e31669 100644--- a/packages/sheets-drawing/src/controllers/sheet-drawing.controller.ts+++ b/packages/sheets-drawing/src/controllers/sheet-drawing.controller.ts@@ -14,17 +14,22 @@* limitations under the License.*/-import type { IDrawingSubunitMap } from '@univerjs/drawing';+import type { Workbook } from '@univerjs/core';+import type { IDrawingJsonUndo1, IDrawingSubunitMap } from '@univerjs/drawing';+import type { ICopySheetCommandParams, IRemoveSheetCommandParams } from '@univerjs/sheets';import type { ISheetDrawing } from '../services/sheet-drawing.service';-import { Disposable, ICommandService, IResourceManagerService, UniverInstanceType } from '@univerjs/core';+import { Disposable, generateRandomId, ICommandService, Inject, IResourceManagerService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';import { IDrawingManagerService } from '@univerjs/drawing';-import { SetDrawingApplyMutation } from '../commands/mutations/set-drawing-apply.mutation';+import { CopySheetCommand, RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';+import { DrawingApplyType, SetDrawingApplyMutation } from '../commands/mutations/set-drawing-apply.mutation';import { ISheetDrawingService } from '../services/sheet-drawing.service';export const SHEET_DRAWING_PLUGIN = 'SHEET_DRAWING_PLUGIN';export class SheetsDrawingLoadController extends Disposable {constructor(+ @Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,+ @Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@ISheetDrawingService private readonly _sheetDrawingService: ISheetDrawingService,@IDrawingManagerService private readonly _drawingManagerService: IDrawingManagerService,@@ -33,6 +38,7 @@ export class SheetsDrawingLoadController extends Disposable {super();this._initSnapshot();+ this._initSheetChange();this.disposeWithMe(this._commandService.registerCommand(SetDrawingApplyMutation));}@@ -76,4 +82,112 @@ export class SheetsDrawingLoadController extends Disposable {}));}++ // eslint-disable-next-line max-lines-per-function+ private _initSheetChange() {+ this.disposeWithMe(+ this._sheetInterceptorService.interceptCommand({+ // eslint-disable-next-line max-lines-per-function+ getMutations: (commandInfo) => {+ if (commandInfo.id === RemoveSheetCommand.id) {+ const params = commandInfo.params as IRemoveSheetCommandParams;+ const unitId = params.unitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId();+ const subUnitId = params.subUnitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId();++ if (!unitId || !subUnitId) {+ return { redos: [], undos: [] };+ }++ const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);+ const drawings = Object.values(drawingData);++ if (drawings.length === 0) {+ return { redos: [], undos: [] };+ }++ const jsonOp = this._sheetDrawingService.getBatchRemoveOp(drawings) as IDrawingJsonUndo1;+ const { unitId: jsonOpUnitId, subUnitId: jsonOpSubUnitId, undo, redo, objects } = jsonOp;++ return {+ redos: [+ {+ id: SetDrawingApplyMutation.id,+ params: {+ op: redo,+ unitId: jsonOpUnitId,+ subUnitId: jsonOpSubUnitId,+ objects,+ type: DrawingApplyType.REMOVE,+ },+ },+ ],+ undos: [+ {+ id: SetDrawingApplyMutation.id,+ params: {+ op: undo,+ unitId: jsonOpUnitId,+ subUnitId: jsonOpSubUnitId,+ objects,+ type: DrawingApplyType.INSERT,+ },+ },+ ],+ };+ } else if (commandInfo.id === CopySheetCommand.id) {+ const params = commandInfo.params as ICopySheetCommandParams & { targetSubUnitId: string };+ const { unitId, subUnitId, targetSubUnitId } = params;++ if (!unitId || !subUnitId || !targetSubUnitId) {+ return { redos: [], undos: [] };+ }++ const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);+ const drawings = Object.values(drawingData).map((drawing) => {+ return {+ ...drawing,+ subUnitId: targetSubUnitId,+ drawingId: generateRandomId(6),+ };+ });++ if (drawings.length === 0) {+ return { redos: [], undos: [] };+ }++ const jsonOp = this._sheetDrawingService.getBatchAddOp(drawings) as IDrawingJsonUndo1;+ const { unitId: jsonOpUnitId, subUnitId: jsonOpSubUnitId, undo, redo, objects } = jsonOp;++ return {+ redos: [+ {+ id: SetDrawingApplyMutation.id,+ params: {+ op: redo,+ unitId: jsonOpUnitId,+ subUnitId: jsonOpSubUnitId,+ objects,+ type: DrawingApplyType.INSERT,+ },+ },+ ],+ undos: [+ {+ id: SetDrawingApplyMutation.id,+ params: {+ op: undo,+ unitId: jsonOpUnitId,+ subUnitId: jsonOpSubUnitId,+ objects,+ type: DrawingApplyType.REMOVE,+ },+ },+ ],+ };+ }+ return { redos: [], undos: [] };+ },+ })+ );+ }}diff --git a/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts b/packages/sheets-note/src/controllers/sheets-note-resource.controller.tsindex d2179a692a..ba2cf5bfd1 100644--- a/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts+++ b/packages/sheets-note/src/controllers/sheets-note-resource.controller.ts@@ -14,8 +14,12 @@* limitations under the License.*/+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 { CopySheetCommand, RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';+import { RemoveNoteMutation, UpdateNoteMutation } from '../commands/mutations/note.mutation';import { PLUGIN_NAME } from '../const';import { SheetsNoteModel } from '../models/sheets-note.model';@@ -31,10 +35,12 @@ export class SheetsNoteResourceController extends Disposable {constructor(@IResourceManagerService private readonly _resourceManagerService: IResourceManagerService,@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,+ @Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,@Inject(SheetsNoteModel) private readonly _sheetsNoteModel: SheetsNoteModel) {super();this._initSnapshot();+ this._initSheetChange();}private _initSnapshot() {@@ -101,4 +107,98 @@ export class SheetsNoteResourceController extends Disposable {}));}++ // eslint-disable-next-line max-lines-per-function+ private _initSheetChange() {+ this.disposeWithMe(+ this._sheetInterceptorService.interceptCommand({+ // eslint-disable-next-line max-lines-per-function+ getMutations: (commandInfo) => {+ if (commandInfo.id === RemoveSheetCommand.id) {+ const params = commandInfo.params as IRemoveSheetCommandParams;+ const unitId = params.unitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId();+ const subUnitId = params.subUnitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId();++ if (!unitId || !subUnitId) {+ return { redos: [], undos: [] };+ }++ const noteMatrix = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);++ if (!noteMatrix) {+ return { redos: [], undos: [] };+ }++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ noteMatrix.forValue((row, col, note) => {+ redos.push({+ id: RemoveNoteMutation.id,+ params: {+ unitId,+ sheetId: subUnitId,+ row,+ col,+ },+ });+ undos.push({+ id: UpdateNoteMutation.id,+ params: {+ unitId,+ sheetId: subUnitId,+ row,+ col,+ note,+ },+ });+ });++ return { redos, undos };+ } else if (commandInfo.id === CopySheetCommand.id) {+ const params = commandInfo.params as ICopySheetCommandParams & { targetSubUnitId: string };+ const { unitId, subUnitId, targetSubUnitId } = params;++ if (!unitId || !subUnitId || !targetSubUnitId) {+ return { redos: [], undos: [] };+ }++ const noteMatrix = this._sheetsNoteModel.getSheetNotes(unitId, subUnitId);++ if (!noteMatrix) {+ return { redos: [], undos: [] };+ }++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ noteMatrix.forValue((row, col, note) => {+ redos.push({+ id: UpdateNoteMutation.id,+ params: {+ unitId,+ sheetId: targetSubUnitId,+ row,+ col,+ note,+ },+ });+ undos.push({+ id: RemoveNoteMutation.id,+ params: {+ unitId,+ sheetId: targetSubUnitId,+ row,+ col,+ },+ });+ });++ return { redos, undos };+ }+ return { redos: [], undos: [] };+ },+ })+ );+ }}diff --git a/packages/sheets-table/src/controllers/sheets-table.controller.ts b/packages/sheets-table/src/controllers/sheets-table.controller.tsindex 5b5628833a..0b830491db 100644--- a/packages/sheets-table/src/controllers/sheets-table.controller.ts+++ b/packages/sheets-table/src/controllers/sheets-table.controller.ts@@ -14,23 +14,27 @@* limitations under the License.*/-import type { IRange } from '@univerjs/core';-+import type { IMutationInfo, IRange, Workbook } from '@univerjs/core';+import type { ICopySheetCommandParams, IRemoveSheetCommandParams } from '@univerjs/sheets';import type { ITableResource } from '../types/type';-import { Disposable, Inject, InterceptorEffectEnum, IResourceManagerService, Rectangle, RTree, UniverInstanceType } from '@univerjs/core';-import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';+import { Disposable, generateRandomId, Inject, InterceptorEffectEnum, IResourceManagerService, IUniverInstanceService, Rectangle, RTree, UniverInstanceType } from '@univerjs/core';+import { CopySheetCommand, INTERCEPTOR_POINT, RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';+import { AddSheetTableMutation } from '../commands/mutations/add-sheet-table.mutation';+import { DeleteSheetTableMutation } from '../commands/mutations/delete-sheet-table.mutation';import { PLUGIN_NAME } from '../const';import { TableManager } from '../model/table-manager';export class SheetsTableController extends Disposable {private _tableRangeRTree = new Map<string, RTree>();constructor(+ @Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,@Inject(TableManager) private _tableManager: TableManager,@Inject(IResourceManagerService) private _resourceManagerService: IResourceManagerService) {super();this._initSnapshot();+ this._initSheetChange();this.registerTableChangeEvent();this.registerTableHeaderInterceptor();}@@ -178,6 +182,116 @@ export class SheetsTableController extends Disposable {}));}+ // eslint-disable-next-line max-lines-per-function+ private _initSheetChange() {+ this.disposeWithMe(+ this._sheetInterceptorService.interceptCommand({+ // eslint-disable-next-line max-lines-per-function+ getMutations: (commandInfo) => {+ if (commandInfo.id === RemoveSheetCommand.id) {+ const params = commandInfo.params as IRemoveSheetCommandParams;+ const unitId = params.unitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId();+ const subUnitId = params.subUnitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId();++ if (!unitId || !subUnitId) {+ return { redos: [], undos: [] };+ }++ const tables = this._tableManager.getTablesBySubunitId(unitId, subUnitId);++ if (tables.length === 0) {+ return { redos: [], undos: [] };+ }++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ tables.forEach((table) => {+ const tableJson = table.toJSON();++ redos.push({+ id: DeleteSheetTableMutation.id,+ params: {+ unitId,+ subUnitId,+ tableId: tableJson.id,+ },+ });+ undos.push({+ id: AddSheetTableMutation.id,+ params: {+ unitId,+ subUnitId,+ name: tableJson.name,+ range: tableJson.range,+ tableId: tableJson.id,+ options: {+ ...tableJson.options,+ columns: tableJson.columns,+ filters: tableJson.filters.tableColumnFilterList,+ },+ },+ });+ });++ return { redos, undos };+ } else if (commandInfo.id === CopySheetCommand.id) {+ const params = commandInfo.params as ICopySheetCommandParams & { targetSubUnitId: string };+ const { unitId, subUnitId, targetSubUnitId } = params;++ if (!unitId || !subUnitId || !targetSubUnitId) {+ return { redos: [], undos: [] };+ }++ const tables = this._tableManager.getTablesBySubunitId(unitId, subUnitId);++ if (tables.length === 0) {+ return { redos: [], undos: [] };+ }++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ tables.forEach((table) => {+ const tableJson = table.toJSON();+ const tableId = generateRandomId();++ redos.push({+ id: AddSheetTableMutation.id,+ params: {+ unitId,+ subUnitId: targetSubUnitId,+ name: tableJson.name,+ range: {+ ...tableJson.range,+ sheetId: targetSubUnitId,+ },+ tableId,+ options: {+ ...tableJson.options,+ columns: tableJson.columns,+ filters: tableJson.filters.tableColumnFilterList,+ },+ },+ });+ undos.push({+ id: DeleteSheetTableMutation.id,+ params: {+ unitId,+ subUnitId: targetSubUnitId,+ tableId,+ },+ });+ });++ return { redos, undos };+ }+ return { redos: [], undos: [] };+ },+ })+ );+ }+override dispose() {super.dispose();this._tableRangeRTree.clear();diff --git a/packages/sheets-table/src/model/table-manager.ts b/packages/sheets-table/src/model/table-manager.tsindex 364bb07d31..27a5f5f816 100644--- a/packages/sheets-table/src/model/table-manager.ts+++ b/packages/sheets-table/src/model/table-manager.ts@@ -103,6 +103,16 @@ export class TableManager extends Disposable {tableStyleId: options?.tableStyleId,});+ if (options?.filters) {+ const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId)?.getSheetBySheetId(subUnitId);+ table.getTableFilters().doFilter(worksheet!, range);+ this._tableFilterChanged$.next({+ unitId,+ subUnitId,+ tableId: id,+ });+ }+return id;}diff --git a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-remove.controller.ts b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-remove.controller.tsdeleted file mode 100644index 98e6922a8b..0000000000--- a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment-remove.controller.ts+++ /dev/null@@ -1,84 +0,0 @@-/**- * 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 { Workbook } from '@univerjs/core';-import type { IRemoveSheetCommandParams } from '@univerjs/sheets';-import type { IDeleteCommentMutationParams } from '@univerjs/thread-comment';-import { Disposable, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';-import { RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';-import { AddCommentMutation, DeleteCommentMutation, IThreadCommentDataSourceService, ThreadCommentModel } from '@univerjs/thread-comment';--export class ThreadCommentRemoveSheetsController extends Disposable {- constructor(- @Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,- @IUniverInstanceService private _univerInstanceService: IUniverInstanceService,- @Inject(ThreadCommentModel) private _threadCommentModel: ThreadCommentModel,- @IThreadCommentDataSourceService private _threadCommentDataSourceService: IThreadCommentDataSourceService- ) {- super();- this._initSheetChange();- }-- private _initSheetChange() {- this.disposeWithMe(- this._sheetInterceptorService.interceptCommand({- getMutations: (commandInfo) => {- if (commandInfo.id === RemoveSheetCommand.id) {- const params = commandInfo.params as IRemoveSheetCommandParams;- const workbook = params.unitId ? this._univerInstanceService.getUnit<Workbook>(params.unitId) : this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);- if (!workbook) {- return { redos: [], undos: [] };- }- const unitId = workbook.getUnitId();- const subUnitId = params.subUnitId || workbook.getActiveSheet()?.getSheetId();- if (!subUnitId) {- return { redos: [], undos: [] };- }- const commentMap = this._threadCommentModel.ensureMap(unitId, subUnitId);-- const comments = Array.from(commentMap.values()).filter((comment) => !comment.parentId);- const ids = comments.map((comment) => comment.id);- const shouldSync = this._threadCommentDataSourceService.syncUpdateMutationToColla;-- const redos = ids.map((id) => ({- id: DeleteCommentMutation.id,- params: {- unitId,- subUnitId,- commentId: id,- } as IDeleteCommentMutationParams,- }));-- const undos = comments.map(({ children, ...comment }) => ({- id: AddCommentMutation.id,- params: {- unitId,- subUnitId,- comment: {- ...comment,- children: shouldSync ? children : undefined,- },- sync: !shouldSync,- },- }));- return { redos, undos };- }- return { redos: [], undos: [] };- },- })- );- }-}diff --git a/packages/sheets-thread-comment-ui/src/plugin.ts b/packages/sheets-thread-comment-ui/src/plugin.tsindex 7c4df35eaf..676c2709dc 100644--- a/packages/sheets-thread-comment-ui/src/plugin.ts+++ b/packages/sheets-thread-comment-ui/src/plugin.ts@@ -25,7 +25,6 @@ import { SheetsThreadCommentRenderController } from './controllers/render-controimport { SheetsThreadCommentCopyPasteController } from './controllers/sheets-thread-comment-copy-paste.controller';import { SheetsThreadCommentHoverController } from './controllers/sheets-thread-comment-hover.controller';import { SheetsThreadCommentPopupController } from './controllers/sheets-thread-comment-popup.controller';-import { ThreadCommentRemoveSheetsController } from './controllers/sheets-thread-comment-remove.controller';import { SheetsThreadCommentController } from './controllers/sheets-thread-comment.controller';import { SheetsThreadCommentPopupService } from './services/sheets-thread-comment-popup.service';import { SHEETS_THREAD_COMMENT } from './types/const';@@ -61,7 +60,6 @@ export class UniverSheetsThreadCommentUIPlugin extends Plugin {[SheetsThreadCommentRenderController],[SheetsThreadCommentCopyPasteController],[SheetsThreadCommentHoverController],- [ThreadCommentRemoveSheetsController],[SheetsThreadCommentPopupController],[SheetsThreadCommentPopupService],] as Dependency[]).forEach((dep) => {@@ -77,7 +75,6 @@ export class UniverSheetsThreadCommentUIPlugin extends Plugin {override onReady(): void {this._injector.get(SheetsThreadCommentRenderController);- this._injector.get(ThreadCommentRemoveSheetsController);}override onRendered(): void {diff --git a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-resource.controller.ts b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-resource.controller.tsnew file mode 100644index 0000000000..b7bf29a588--- /dev/null+++ b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment-resource.controller.ts@@ -0,0 +1,132 @@+/**+ * 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 { IMutationInfo, Workbook } from '@univerjs/core';+import type { ICopySheetCommandParams, IRemoveSheetCommandParams } from '@univerjs/sheets';+import { Disposable, generateRandomId, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';+import { CopySheetCommand, RemoveSheetCommand, SheetInterceptorService } from '@univerjs/sheets';+import { AddCommentMutation, DeleteCommentMutation, IThreadCommentDataSourceService, ThreadCommentModel } from '@univerjs/thread-comment';++export class SheetsThreadCommentResourceController extends Disposable {+ constructor(+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,+ @Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,+ @Inject(ThreadCommentModel) private _threadCommentModel: ThreadCommentModel,+ @IThreadCommentDataSourceService private _threadCommentDataSourceService: IThreadCommentDataSourceService+ ) {+ super();+ this._initSheetChange();+ }++ // eslint-disable-next-line max-lines-per-function+ private _initSheetChange() {+ this.disposeWithMe(+ this._sheetInterceptorService.interceptCommand({+ // eslint-disable-next-line max-lines-per-function+ getMutations: (commandInfo) => {+ if (commandInfo.id === RemoveSheetCommand.id) {+ const params = commandInfo.params as IRemoveSheetCommandParams;+ const unitId = params.unitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId();+ const subUnitId = params.subUnitId || this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId();++ if (!unitId || !subUnitId) {+ return { redos: [], undos: [] };+ }++ const commentMap = this._threadCommentModel.ensureMap(unitId, subUnitId);+ const comments = Array.from(commentMap.values()).filter((comment) => !comment.parentId);+ const shouldSync = this._threadCommentDataSourceService.syncUpdateMutationToColla;++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ comments.forEach(({ children, ...comment }) => {+ redos.push({+ id: DeleteCommentMutation.id,+ params: {+ unitId,+ subUnitId,+ commentId: comment.id,+ },+ });+ undos.push({+ id: AddCommentMutation.id,+ params: {+ unitId,+ subUnitId,+ comment: {+ ...comment,+ children: shouldSync ? children : undefined,+ },+ sync: !shouldSync,+ },+ });+ });++ return { redos, undos };+ } else if (commandInfo.id === CopySheetCommand.id) {+ const params = commandInfo.params as ICopySheetCommandParams & { targetSubUnitId: string };+ const { unitId, subUnitId, targetSubUnitId } = params;++ if (!unitId || !subUnitId || !targetSubUnitId) {+ return { redos: [], undos: [] };+ }++ const commentMap = this._threadCommentModel.ensureMap(unitId, subUnitId);+ const comments = Array.from(commentMap.values()).map((comment) => {+ return {+ ...comment,+ subUnitId: targetSubUnitId,+ id: generateRandomId(),+ threadId: generateRandomId(),+ };+ }).filter((comment) => !comment.parentId);+ const shouldSync = this._threadCommentDataSourceService.syncUpdateMutationToColla;++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ comments.forEach(({ children, ...comment }) => {+ redos.push({+ id: AddCommentMutation.id,+ params: {+ unitId,+ subUnitId: targetSubUnitId,+ comment: {+ ...comment,+ children: shouldSync ? children : undefined,+ },+ sync: !shouldSync,+ },+ });+ undos.push({+ id: DeleteCommentMutation.id,+ params: {+ unitId,+ subUnitId: targetSubUnitId,+ commentId: comment.id,+ },+ });+ });++ return { redos, undos };+ }+ return { redos: [], undos: [] };+ },+ })+ );+ }+}diff --git a/packages/sheets-thread-comment/src/plugin.ts b/packages/sheets-thread-comment/src/plugin.tsindex 23dc1fde60..58510a7d88 100644--- a/packages/sheets-thread-comment/src/plugin.ts+++ b/packages/sheets-thread-comment/src/plugin.ts@@ -16,10 +16,11 @@import type { Dependency } from '@univerjs/core';import type { IUniverSheetsThreadCommentConfig } from './controllers/config.schema';-import { DependentOn, ICommandService, Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';+import { DependentOn, ICommandService, Inject, Injector, Plugin, touchDependencies, UniverInstanceType } from '@univerjs/core';import { UniverThreadCommentPlugin } from '@univerjs/thread-comment';import { defaultPluginConfig } from './controllers/config.schema';import { SheetsThreadCommentRefRangeController } from './controllers/sheets-thread-comment-ref-range.controller';+import { SheetsThreadCommentResourceController } from './controllers/sheets-thread-comment-resource.controller';import { SheetsThreadCommentModel } from './models/sheets-thread-comment.model';import { SHEET_THREAD_COMMENT_BASE } from './types/const';@@ -40,10 +41,14 @@ export class UniverSheetsThreadCommentPlugin extends Plugin {([[SheetsThreadCommentModel],[SheetsThreadCommentRefRangeController],+ [SheetsThreadCommentResourceController],] as Dependency[]).forEach((dep) => {this._injector.add(dep);});- this._injector.get(SheetsThreadCommentRefRangeController);+ touchDependencies(this._injector, [+ [SheetsThreadCommentRefRangeController],+ [SheetsThreadCommentResourceController],+ ]);}}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 4dff7dbb88..8764a73dcb 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -2209,13 +2209,13 @@ importers:'@univerjs/drawing':specifier: workspace:*version: link:../drawing+ '@univerjs/sheets':+ specifier: workspace:*+ version: link:../sheetsdevDependencies:'@univerjs-infra/shared':specifier: workspace:*version: link:../../common/shared- '@univerjs/sheets':- specifier: workspace:*- version: link:../sheetstypescript:specifier: ^5.9.3version: 5.9.3