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
revert(resource): revert resources type (#7045)
wpxp12345612b21a71ad34
diff --git a/packages/core/src/services/__tests__/resources.spec.ts b/packages/core/src/services/__tests__/resources.spec.tsindex 88d864d83e..92cca93f80 100644--- a/packages/core/src/services/__tests__/resources.spec.ts+++ b/packages/core/src/services/__tests__/resources.spec.ts@@ -16,6 +16,7 @@import type { IDocumentData } from '../../types/interfaces';import type { Univer } from '../../univer';+import type { IResources } from '../resource-manager/type';import { BehaviorSubject } from 'rxjs';import { beforeEach, describe, expect, it, vi } from 'vitest';import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY } from '../../common/const';@@ -45,7 +46,7 @@ function createDocData(id: string, resources?: NonNullable<IDocumentData['resourinterface ITestSlideData {id: string;name?: string;- resources?: unknown;+ resources?: IResources;}class MockSlideUnit extends UnitModel<ITestSlideData> {@@ -231,11 +232,11 @@ describe('Test resources service', () => {expect(loads).toEqual([['slide-late-resource', 'late']]);});- it('should load object-shaped slide plugin resources through the unit lifecycle', () => {+ it('should load array-shaped slide plugin resources through the unit lifecycle', () => {const injector = univer.__getInjector();const resourceManagerService = injector.get(IResourceManagerService);const univerInstanceService = injector.get(IUniverInstanceService);- const pluginName = 'SLIDE_OBJECT_PLUGIN' as never;+ const pluginName = 'SLIDE_ARRAY_PLUGIN' as never;const loads: Array<[string, string]> = [];univerInstanceService.registerCtorForType(UniverInstanceType.UNIVER_SLIDE, MockSlideUnit as never);@@ -249,22 +250,20 @@ describe('Test resources service', () => {});univer.createUnit<ITestSlideData, MockSlideUnit>(UniverInstanceType.UNIVER_SLIDE, {- id: 'slide-object-resource',- resources: {- [pluginName]: {- data: '{"kind":"object"}',- },- },+ id: 'slide-array-resource',+ resources: [+ { name: pluginName, data: '{"kind":"array"}' },+ ],});- expect(loads).toEqual([['slide-object-resource', 'object']]);+ expect(loads).toEqual([['slide-array-resource', 'array']]);});- it('should load direct object-shaped slide plugin resources through the unit lifecycle', () => {+ it('should load serialized object slide plugin resources through the unit lifecycle', () => {const injector = univer.__getInjector();const resourceManagerService = injector.get(IResourceManagerService);const univerInstanceService = injector.get(IUniverInstanceService);- const pluginName = 'SLIDE_DIRECT_OBJECT_PLUGIN' as never;+ const pluginName = 'SLIDE_SERIALIZED_OBJECT_PLUGIN' as never;const loads: Array<[string, string]> = [];univerInstanceService.registerCtorForType(UniverInstanceType.UNIVER_SLIDE, MockSlideUnit as never);@@ -278,15 +277,13 @@ describe('Test resources service', () => {});univer.createUnit<ITestSlideData, MockSlideUnit>(UniverInstanceType.UNIVER_SLIDE, {- id: 'slide-direct-object-resource',- resources: {- [pluginName]: {- kind: 'direct-object',- },- },+ id: 'slide-serialized-object-resource',+ resources: [+ { name: pluginName, data: '{"kind":"serialized-object"}' },+ ],});- expect(loads).toEqual([['slide-direct-object-resource', 'direct-object']]);+ expect(loads).toEqual([['slide-serialized-object-resource', 'serialized-object']]);});it('should load empty persisted resource payloads when hooks are registered later', () => {diff --git a/packages/core/src/services/resource-loader/resource-loader.service.ts b/packages/core/src/services/resource-loader/resource-loader.service.tsindex e71859be9e..498a2584c1 100644--- a/packages/core/src/services/resource-loader/resource-loader.service.ts+++ b/packages/core/src/services/resource-loader/resource-loader.service.ts@@ -17,7 +17,7 @@import type { UnitModel } from '../../common/unit';import type { DocumentDataModel } from '../../docs';import type { Workbook } from '../../sheets/workbook';-import type { IResourceHook, IResourceName, IResourceSnapshot } from '../resource-manager/type';+import type { IResourceHook, IResources } from '../resource-manager/type';import type { IResourceLoaderService } from './type';import { isInternalEditorID } from '../../common/const';import { Inject } from '../../common/di';@@ -36,49 +36,24 @@ export class ResourceLoaderService extends Disposable implements IResourceLoaderthis._init();}+ // eslint-disable-next-line max-lines-per-functionprivate _init() {const loadHookResource = (hook: IResourceHook,unitId: string,- resources: unknown,+ resources: IResources = [],errorLabel: string) => {- const data = getResourceData(resources, hook.pluginName);- if (data !== undefined) {+ const plugin = resources.find((r) => r.name === hook.pluginName);+ if (plugin) {try {- const model = hook.parseJson(data);- hook.onLoad(unitId, model);+ const data = hook.parseJson(plugin.data);+ hook.onLoad(unitId, data);} catch (err) {console.error(`Load ${errorLabel}{${unitId}} Resources{${hook.pluginName}} Data Error.`);}}};- const getResourceData = (resources: unknown, pluginName: IResourceName): string | undefined => {- if (Array.isArray(resources)) {- const data = resources.find((resource) => resource.name === pluginName)?.data;- return typeof data === 'string' ? data : undefined;- }-- if (!resources || typeof resources !== 'object') {- return undefined;- }-- const raw = (resources as Record<string, unknown>)[pluginName];- if (typeof raw === 'string') {- return raw;- }-- if (raw && typeof raw === 'object') {- const data = (raw as Record<string, unknown>).data;- if (typeof data === 'string') {- return data;- }-- return JSON.stringify(raw);- }-- return undefined;- };const handleHookAdd = (hook: IResourceHook) => {hook.businesses.forEach((business) => {@@ -92,7 +67,7 @@ export class ResourceLoaderService extends Disposable implements IResourceLoaderbreak;}case UniverInstanceType.UNIVER_SLIDE: {- this._univerInstanceService.getAllUnitsForType<UnitModel<{ resources?: IResourceSnapshot }>>(UniverInstanceType.UNIVER_SLIDE).forEach((slide) => {+ this._univerInstanceService.getAllUnitsForType<UnitModel<{ resources?: IResources }>>(UniverInstanceType.UNIVER_SLIDE).forEach((slide) => {loadHookResource(hook, slide.getUnitId(), slide.getSnapshot().resources, 'Slide');});break;@@ -128,7 +103,7 @@ export class ResourceLoaderService extends Disposable implements IResourceLoader}));this.disposeWithMe(- this._univerInstanceService.getTypeOfUnitAdded$<UnitModel<{ resources?: IResourceSnapshot }>>(UniverInstanceType.UNIVER_SLIDE).subscribe((event) => {+ this._univerInstanceService.getTypeOfUnitAdded$<UnitModel<{ resources?: IResources }>>(UniverInstanceType.UNIVER_SLIDE).subscribe((event) => {const { unit: slide } = event;this._resourceManagerService.loadResources(slide.getUnitId(), slide.getSnapshot().resources);})diff --git a/packages/core/src/services/resource-manager/__tests__/resource-manager.service.spec.ts b/packages/core/src/services/resource-manager/__tests__/resource-manager.service.spec.tsindex 903aec0880..5c71f651b4 100644--- a/packages/core/src/services/resource-manager/__tests__/resource-manager.service.spec.ts+++ b/packages/core/src/services/resource-manager/__tests__/resource-manager.service.spec.ts@@ -135,7 +135,7 @@ describe('ResourceManagerService', () => {expect(errorSpy).toHaveBeenCalled();});- it('should load direct object-shaped plugin resources', () => {+ it('should load array-shaped plugin resources', () => {const loaded: unknown[] = [];service.registerPluginResource({@@ -147,9 +147,9 @@ describe('ResourceManagerService', () => {parseJson: JSON.parse,});- service.loadResources('unit-1', {- SHEET_TEST_PLUGIN: { ok: true },- } as never);+ service.loadResources('unit-1', [+ { name: 'SHEET_TEST_PLUGIN', data: '{"ok":true}' },+ ]);expect(loaded).toEqual([{ ok: true }]);});diff --git a/packages/core/src/services/resource-manager/resource-manager.service.ts b/packages/core/src/services/resource-manager/resource-manager.service.tsindex e92180a957..de6ae46dba 100644--- a/packages/core/src/services/resource-manager/resource-manager.service.ts+++ b/packages/core/src/services/resource-manager/resource-manager.service.ts@@ -15,7 +15,7 @@*/import type { UniverInstanceType } from '../../common/unit';-import type { IResourceHook, IResourceManagerService, IResourceName, IResources, IResourceSnapshot } from './type';+import type { IResourceHook, IResourceManagerService, IResourceName, IResources } from './type';import { Subject } from 'rxjs';import { Disposable, toDisposable } from '../../shared/lifecycle';import { ILogService } from '../log/log.service';@@ -79,9 +79,9 @@ export class ResourceManagerService extends Disposable implements IResourceManagthis._resourceMap.delete(pluginName);}- public loadResources(unitId: string, resources?: IResourceSnapshot) {+ public loadResources(unitId: string, resources?: IResources) {this.getAllResourceHooks().forEach((hook) => {- const data = this._getResourceData(resources, hook.pluginName);+ const data = resources?.find((resource) => resource.name === hook.pluginName)?.data;if (data) {try {const model = hook.parseJson(data);@@ -99,33 +99,6 @@ export class ResourceManagerService extends Disposable implements IResourceManag});}- private _getResourceData(resources: unknown, pluginName: IResourceName): string | undefined {- if (Array.isArray(resources)) {- const data = resources.find((resource) => resource.name === pluginName)?.data;- return typeof data === 'string' ? data : undefined;- }-- if (!resources || typeof resources !== 'object') {- return undefined;- }-- const raw = (resources as Record<string, unknown>)[pluginName];- if (typeof raw === 'string') {- return raw;- }-- if (raw && typeof raw === 'object') {- const data = (raw as Record<string, unknown>).data;- if (typeof data === 'string') {- return data;- }-- return JSON.stringify(raw);- }-- return undefined;- }-override dispose(): void {this._register$.complete();this._resourceMap.clear();diff --git a/packages/core/src/services/resource-manager/type.ts b/packages/core/src/services/resource-manager/type.tsindex 5afa6e12e8..914102c3f9 100644--- a/packages/core/src/services/resource-manager/type.ts+++ b/packages/core/src/services/resource-manager/type.ts@@ -20,7 +20,6 @@ import type { UniverInstanceType } from '../../common/unit';import { createIdentifier } from '../../common/di';export type IResources = Array<{ id?: string; name: string; data: string }>;-export type IResourceSnapshot = IResources | Record<string, unknown>;type IBusinessName = 'SHEET' | 'DOC' | 'SLIDE';export type IResourceName = `${IBusinessName}_${string}_PLUGIN`;@@ -42,7 +41,7 @@ export interface IResourceManagerService {getResources(unitId: string, type: UniverInstanceType): IResources;getResourcesByType: (unitId: string, type: UniverInstanceType) => IResources;- loadResources: (unitId: string, resources?: IResourceSnapshot) => void;+ loadResources: (unitId: string, resources?: IResources) => void;unloadResources(unitId: string, type: UniverInstanceType): void;}diff --git a/packages/core/src/types/interfaces/i-document-data.ts b/packages/core/src/types/interfaces/i-document-data.tsindex ccb06adfde..d96d7d03b2 100644--- a/packages/core/src/types/interfaces/i-document-data.ts+++ b/packages/core/src/types/interfaces/i-document-data.ts@@ -15,6 +15,7 @@*/import type { ImageSourceType } from '../../services/image-io/image-io.service';+import type { IResources } from '../../services/resource-manager/type';import type { ISize } from '../../shared/shape';import type { BooleanNumber, CellValueType, HorizontalAlign, LocaleType, TextDirection, VerticalAlign, WrapStrategy } from '../enum';import type { IDrawingParam } from './i-drawing';@@ -36,7 +37,7 @@ export interface IDocumentData extends IReferenceSource {documentStyle: IDocumentStyle;settings?: IDocumentSettings;// The type of data depends on how the plug-in is defined- resources?: Array<{ id?: string; name: string; data: string }>;+ resources?: IResources;disabled?: boolean;}