Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix resource manager get unrelated data #2989

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { IWorkbookData } from '../../types/interfaces';
import type { IResourceHook } from '../resource-manager/type';
import { IResourceManagerService } from '../resource-manager/type';
import { IUniverInstanceService } from '../instance/instance.service';
import { Disposable, toDisposable } from '../../shared/lifecycle';
import { Disposable } from '../../shared/lifecycle';
import { UniverInstanceType } from '../../common/unit';
import type { DocumentDataModel } from '../../docs';
import type { IResourceLoaderService } from './type';
Expand Down Expand Up @@ -84,33 +84,29 @@ export class ResourceLoaderService extends Disposable implements IResourceLoader
}));

this.disposeWithMe(
toDisposable(
this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._resourceManagerService.loadResources(workbook.getUnitId(), workbook.getSnapshot().resources);
})
)
this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._resourceManagerService.loadResources(workbook.getUnitId(), workbook.getSnapshot().resources);
})
);

this.disposeWithMe(
toDisposable(
this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._resourceManagerService.unloadResources(workbook.getUnitId());
})
)
this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
this._resourceManagerService.unloadResources(workbook.getUnitId());
})
);
}

saveWorkbook: (workbook: Workbook) => IWorkbookData = (workbook) => {
const unitId = workbook.getUnitId();
const resources = this._resourceManagerService.getResources(unitId) || [];
const resources = this._resourceManagerService.getResources(unitId, UniverInstanceType.UNIVER_SHEET);
const snapshot = workbook.getSnapshot();
snapshot.resources = resources;
return snapshot;
};

saveDoc(doc: DocumentDataModel) {
const unitId = doc.getUnitId();
const resources = this._resourceManagerService.getResourcesByType(unitId, UniverInstanceType.UNIVER_DOC) || [];
const resources = this._resourceManagerService.getResources(unitId, UniverInstanceType.UNIVER_DOC);
const snapshot = doc.getSnapshot();
snapshot.resources = resources;
return snapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Subject } from 'rxjs';
import { Disposable, toDisposable } from '../../shared/lifecycle';
import type { IWorkbookData } from '../../types/interfaces/i-workbook-data';
import type { Resources } from '../../types/interfaces/i-workbook-data';
import type { UniverInstanceType } from '../../common/unit';
import type { IResourceHook, IResourceManagerService, IResourceName } from './type';

Expand All @@ -31,7 +31,13 @@ export class ResourceManagerService extends Disposable implements IResourceManag
return list;
}

public getResources(unitId: string) {
public getResources(unitId: string): Resources;
public getResources(unitId: string, type: UniverInstanceType): Resources;
public getResources(unitId: string, type?: UniverInstanceType): Resources {
if (type) {
return this.getResourcesByType(unitId, type);
}

const resourceHooks = this.getAllResourceHooks();
const resources = resourceHooks.map((resourceHook) => {
const data = resourceHook.toJson(unitId);
Expand Down Expand Up @@ -69,7 +75,7 @@ export class ResourceManagerService extends Disposable implements IResourceManag
this._resourceMap.delete(pluginName);
}

public loadResources(unitId: string, resources: IWorkbookData['resources']) {
public loadResources(unitId: string, resources?: Resources) {
this.getAllResourceHooks().forEach((hook) => {
const data = resources?.find((resource) => resource.name === hook.pluginName)?.data;
if (data) {
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/services/resource-manager/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Observable } from 'rxjs';
import type { UniverInstanceType } from '@univerjs/core';
import type { IDisposable } from '../../common/di';
import { createIdentifier } from '../../common/di';
import type { IWorkbookData } from '../../types/interfaces/i-workbook-data';
import type { Resources } from '../../types/interfaces/i-workbook-data';

type IBusinessName = 'SHEET' | 'DOC';
export type IResourceName = `${IBusinessName}_${string}_PLUGIN`;
Expand All @@ -36,9 +36,14 @@ export interface IResourceManagerService {
registerPluginResource: <T = any>(hook: IResourceHook<T>) => IDisposable;
disposePluginResource: (pluginName: IResourceName) => void;
getAllResourceHooks: () => IResourceHook[];
getResources: (unitId: string) => IWorkbookData['resources'];
getResourcesByType: (unitId: string, type: UniverInstanceType) => IWorkbookData['resources'];
loadResources: (unitId: string, resources: IWorkbookData['resources']) => void;

/**
* @deprecated You should get resource with type specified.
*/
getResources(unitId: string): Resources;
getResources(unitId: string, type: UniverInstanceType): Resources;
getResourcesByType: (unitId: string, type: UniverInstanceType) => Resources;
loadResources: (unitId: string, resources?: Resources) => void;
unloadResources(unitId: string): void;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types/interfaces/i-workbook-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import type { IExtraModelData } from './i-extra-model-data';
import type { IStyleData } from './i-style-data';
import type { IWorksheetData } from './i-worksheet-data';

export type Resources = Array<{ id?: string; name: string; data: string }>;

/**
* Properties of a workbook's configuration
*/
Expand Down Expand Up @@ -53,5 +55,5 @@ export interface IWorkbookData extends IExtraModelData {
sheets: { [sheetId: string]: Partial<IWorksheetData> };

// The type of data depends on how the plug-in is defined
resources?: Array<{ id?: string; name: string; data: string }>;
resources?: Resources;
}
Loading