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
feat(sheet): add debugger snapshot feature (#660)
Gpound.liu78a5e5fde6dd
diff --git a/examples/sheets/src/sheets-plugin-debugger/commands/operations/saveSnapshot.operations.ts b/examples/sheets/src/sheets-plugin-debugger/commands/operations/saveSnapshot.operations.tsnew file mode 100644index 0000000000..29068cbe94--- /dev/null+++ b/examples/sheets/src/sheets-plugin-debugger/commands/operations/saveSnapshot.operations.ts@@ -0,0 +1,53 @@+// eslint-disable-next-line import/no-extraneous-dependencies+import type { ICommand, IStyleData, IWorkbookData } from '@univerjs/core';+import { CommandType, IPersistenceService, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';+import type { IAccessor } from '@wendellhu/redi';++export interface ISetEditableCommandParams {+ value: 'sheet' | 'workbook';+}++const filterStyle = (workbookData: IWorkbookData) => {+ const sheets = workbookData.sheets;+ const cacheStyle: Record<string, IStyleData> = {};+ Object.keys(sheets).forEach((sheetId) => {+ const sheet = sheets[sheetId];+ new ObjectMatrix(sheet.cellData).forValue((_r, _c, value) => {+ const s = value.s;+ if (s && typeof s === 'string') {+ const style = workbookData.styles[s];+ if (style) {+ cacheStyle[s] = style;+ }+ }+ });+ });+ workbookData.styles = cacheStyle;+ return workbookData;+};+export const SaveSnapshotOptions: ICommand = {+ id: 'debugger.operation.saveSnapshot',+ type: CommandType.OPERATION,+ handler: async (accessor: IAccessor, params: ISetEditableCommandParams) => {+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const localPersistenceService = accessor.get(IPersistenceService);+ const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ const worksheet = workbook.getActiveSheet();+ const snapshot = localPersistenceService.saveWorkBook(workbook);++ switch (params.value) {+ case 'sheet': {+ const sheetId = worksheet.getSheetId();+ const sheet = snapshot.sheets[sheetId];+ snapshot.sheets = { [sheetId]: sheet };+ snapshot.sheetOrder = [sheetId];+ break;+ }+ }+ const text = JSON.stringify(filterStyle(snapshot), null, 2);+ console.log(text);+ navigator.clipboard.writeText(text);++ return true;+ },+};diff --git a/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts b/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.tsindex 7f326c3845..026d52df8e 100644--- a/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts+++ b/examples/sheets/src/sheets-plugin-debugger/controllers/debugger.controller.ts@@ -1,4 +1,4 @@-import { Disposable, ICommandService } from '@univerjs/core';+import { Disposable, ICommandService, IPersistenceService, LocalPersistenceService } from '@univerjs/core';import { IMenuService } from '@univerjs/ui';import { Inject, Injector } from '@wendellhu/redi';@@ -7,6 +7,7 @@ import { DialogOperation } from '../commands/operations/dialog.operation';import { LocaleOperation } from '../commands/operations/locale.operation';import { MessageOperation } from '../commands/operations/message.operation';import { NotificationOperation } from '../commands/operations/notification.operation';+import { SaveSnapshotOptions } from '../commands/operations/saveSnapshot.operations';import { SetEditable } from '../commands/operations/set.editable.operation';import { SidebarOperation } from '../commands/operations/sidebar.operation';import { ThemeOperation } from '../commands/operations/theme.operation';@@ -16,6 +17,7 @@ import {LocaleMenuItemFactory,MessageMenuItemFactory,NotificationMenuItemFactory,+ SaveSnapshotSetEditableMenuItemFactory,SetEditableMenuItemFactory,SidebarMenuItemFactory,ThemeMenuItemFactory,@@ -39,7 +41,10 @@ export class DebuggerController extends Disposable {MessageOperation,SidebarOperation,SetEditable,+ SaveSnapshotOptions,].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));++ this._injector.add([IPersistenceService, { useClass: LocalPersistenceService }]);}private _initializeContextMenu() {@@ -52,6 +57,7 @@ export class DebuggerController extends Disposable {ConfirmMenuItemFactory,SidebarMenuItemFactory,SetEditableMenuItemFactory,+ SaveSnapshotSetEditableMenuItemFactory,].forEach((factory) => {this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));});diff --git a/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts b/examples/sheets/src/sheets-plugin-debugger/controllers/menu.tsindex f49fbff556..2991fa09ff 100644--- a/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts+++ b/examples/sheets/src/sheets-plugin-debugger/controllers/menu.ts@@ -9,6 +9,7 @@ import { DialogOperation } from '../commands/operations/dialog.operation';import { LocaleOperation } from '../commands/operations/locale.operation';import { MessageOperation } from '../commands/operations/message.operation';import { NotificationOperation } from '../commands/operations/notification.operation';+import { SaveSnapshotOptions } from '../commands/operations/saveSnapshot.operations';import { SetEditable } from '../commands/operations/set.editable.operation';import { SidebarOperation } from '../commands/operations/sidebar.operation';import { ThemeOperation } from '../commands/operations/theme.operation';@@ -168,3 +169,22 @@ export function SetEditableMenuItemFactory(accessor: IAccessor): IMenuSelectorIt],};}++export function SaveSnapshotSetEditableMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {+ return {+ id: SaveSnapshotOptions.id,+ type: MenuItemType.SELECTOR,+ title: 'debugger.snapshot',+ positions: [MenuPosition.TOOLBAR_OTHERS],+ selections: [+ {+ label: 'saveWorkbook',+ value: 'workbook',+ },+ {+ label: 'saveSheet',+ value: 'sheet',+ },+ ],+ };+}diff --git a/examples/sheets/src/sheets-plugin-debugger/locale/en-US.ts b/examples/sheets/src/sheets-plugin-debugger/locale/en-US.tsindex bd20541881..01830251f0 100644--- a/examples/sheets/src/sheets-plugin-debugger/locale/en-US.ts+++ b/examples/sheets/src/sheets-plugin-debugger/locale/en-US.ts@@ -8,5 +8,6 @@ export default {confirm: 'Confirm',sidebar: 'Sidebar',editable: 'Editable',+ snapshot: 'Snapshot',},};diff --git a/examples/sheets/src/sheets-plugin-debugger/locale/zh-CN.ts b/examples/sheets/src/sheets-plugin-debugger/locale/zh-CN.tsindex 52bfc3f209..e3134f2f2f 100644--- a/examples/sheets/src/sheets-plugin-debugger/locale/zh-CN.ts+++ b/examples/sheets/src/sheets-plugin-debugger/locale/zh-CN.ts@@ -8,5 +8,6 @@ export default {confirm: '确认框',sidebar: '侧边栏',editable: '编辑权限',+ snapshot: '快照',},};diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 8a76edc926..9acfbf7435 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -77,6 +77,7 @@ export * from './shared';// #region sheetexport type { IComposeInterceptors, IInterceptor, InterceptorHandler } from './common/interceptor';export { composeInterceptors, createInterceptorKey, InterceptorManager } from './common/interceptor';+export { IPersistenceService, LocalPersistenceService } from './services/resource-manager/local-persistence.service';export { Range } from './sheets/range';export {deserializeRangeWithSheet,diff --git a/packages/core/src/services/resource-manager/local-persistence.service.ts b/packages/core/src/services/resource-manager/local-persistence.service.tsnew file mode 100644index 0000000000..e83531ec9d--- /dev/null+++ b/packages/core/src/services/resource-manager/local-persistence.service.ts@@ -0,0 +1,61 @@+import { createIdentifier, Inject } from '@wendellhu/redi';++import { Disposable } from '../../shared';+import type { Workbook } from '../../sheets/workbook';+import type { IWorkbookData } from '../../types/interfaces/i-workbook-data';+import { IUniverInstanceService } from '../instance/instance.service';+import { LifecycleStages, OnLifecycle } from '../lifecycle/lifecycle';+import { IResourceManagerService } from './type';++export interface IPersistenceService {+ saveWorkBook: (workbook: Workbook) => IWorkbookData;+}++export const IPersistenceService = createIdentifier<IPersistenceService>('LocalPersistenceService');++@OnLifecycle(LifecycleStages.Ready, LocalPersistenceService)+export class LocalPersistenceService extends Disposable implements IPersistenceService {+ constructor(+ @Inject(IResourceManagerService) private _resourceManagerService: IResourceManagerService,+ @Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService+ ) {+ super();+ this._initWorkBook();+ }++ private _initWorkBook() {+ this._univerInstanceService.sheetAdded$.subscribe((workbook) => this._initWorkbookFromSnapshot(workbook));+ const workbooks = this._univerInstanceService.getAllUniverSheetsInstance();+ workbooks.forEach((workbook) => {+ this._initWorkbookFromSnapshot(workbook);+ });+ }++ private _initWorkbookFromSnapshot(workbook: Workbook) {+ const workbookId = workbook.getUnitId();+ const snapshot = workbook.getSnapshot();+ const resources = this._resourceManagerService.getAllResource(workbookId);+ resources.forEach((resource) => {+ const resourceSnapshot = (snapshot.resources || []).find((item) => item.name === resource.resourceName);+ if (resourceSnapshot) {+ const model = resource.hook.parseJson(resourceSnapshot.data);+ resource.hook.onChange(workbookId, model);+ }+ });+ }++ saveWorkBook(workbook: Workbook) {+ const snapshot = { ...workbook.getSnapshot() };+ const unitId = workbook.getUnitId();+ const resourceHooks = this._resourceManagerService.getAllResource(workbook.getUnitId());+ const resources = resourceHooks.map((resourceHook) => {+ const data = resourceHook.hook.toJson(unitId);+ return {+ name: resourceHook.resourceName,+ data,+ };+ });+ snapshot.resources = resources;+ return snapshot;+ }+}diff --git a/packages/sheets/src/services/numfmt/numfmt.service.ts b/packages/sheets/src/services/numfmt/numfmt.service.tsindex 0cb44bd300..c7039be15b 100644--- a/packages/sheets/src/services/numfmt/numfmt.service.ts+++ b/packages/sheets/src/services/numfmt/numfmt.service.ts@@ -17,7 +17,7 @@ import { Subject } from 'rxjs';import type { FormatType, INumfmtItem, INumfmtService, IRefItem, ISnapshot } from './type';const SHEET_NUMFMT_PLUGIN = 'SHEET_NUMFMT_PLUGIN';-@OnLifecycle(LifecycleStages.Ready, NumfmtService)+@OnLifecycle(LifecycleStages.Starting, NumfmtService)export class NumfmtService extends Disposable implements INumfmtService {/*** Map<unitID ,<sheetId ,ObjectMatrix>>