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(docs-mention): add resource-controller (#2803)
zhangw37a1d58add66
diff --git a/packages/docs-mention/src/controllers/doc-mention-resource.controller.ts b/packages/docs-mention/src/controllers/doc-mention-resource.controller.tsnew file mode 100644index 0000000000..3f3c3eda35--- /dev/null+++ b/packages/docs-mention/src/controllers/doc-mention-resource.controller.ts@@ -0,0 +1,66 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * 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 { DocumentDataModel } from '@univerjs/core';+import { CustomRangeType, Disposable, IResourceManagerService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';+import { Inject } from '@wendellhu/redi';+import { DOC_MENTION_PLUGIN } from '../types/const/const';+import type { IDocMention } from '../types/interfaces/i-mention';+import { DocMentionModel } from '../models/doc-mention.model';++interface IDocMentionJSON {+ mentions: IDocMention[];+}++@OnLifecycle(LifecycleStages.Starting, DocMentionResourceController)+export class DocMentionResourceController extends Disposable {+ constructor(+ @Inject(IResourceManagerService) private readonly _resourceManagerService: IResourceManagerService,+ @Inject(DocMentionModel) private readonly _docMentionModel: DocMentionModel,+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService+ ) {+ super();+ this._init();+ }++ private _init() {+ this._resourceManagerService.registerPluginResource({+ pluginName: DOC_MENTION_PLUGIN,+ businesses: [UniverInstanceType.UNIVER_DOC],+ onLoad: (unitID: string, resource: IDocMentionJSON) => {+ resource.mentions.forEach((mention) => {+ this._docMentionModel.addMention(unitID, mention);+ });+ },+ onUnLoad: (unitID: string) => {+ this._docMentionModel.deleteUnit(unitID);+ },+ toJson: (unitID: string) => {+ const mentions = this._docMentionModel.getUnit(unitID);+ const doc = this._univerInstanceService.getUnit<DocumentDataModel>(unitID, UniverInstanceType.UNIVER_DOC);+ const customRanges = doc?.getBody()?.customRanges;+ const set = new Set(customRanges?.filter((i) => i.rangeType === CustomRangeType.MENTION).map((i) => i.rangeId));++ return JSON.stringify({+ mentions: mentions.filter((mention) => set.has(mention.id)),+ });+ },+ parseJson(bytes: string): IDocMentionJSON {+ return JSON.parse(bytes);+ },+ });+ }+}diff --git a/packages/docs-mention/src/models/doc-mention.model.ts b/packages/docs-mention/src/models/doc-mention.model.tsindex bc2b34e329..d0fb61cce1 100644--- a/packages/docs-mention/src/models/doc-mention.model.ts+++ b/packages/docs-mention/src/models/doc-mention.model.ts@@ -58,4 +58,13 @@ export class DocMentionModel extends Disposable {const map = this._ensureMap(unitId);return map.delete(id);}++ getUnit(unitId: string) {+ const map = this._mentionMaps.get(unitId);+ return Array.from(map?.values() ?? []);+ }++ deleteUnit(unitId: string) {+ this._mentionMaps.delete(unitId);+ }}diff --git a/packages/docs-mention/src/plugin.ts b/packages/docs-mention/src/plugin.tsindex 65e773dd6a..ddb256cf34 100644--- a/packages/docs-mention/src/plugin.ts+++ b/packages/docs-mention/src/plugin.ts@@ -23,6 +23,7 @@ import { DocMentionModel } from './models/doc-mention.model';import { DocMentionController } from './controllers/doc-mention.controller';import { DocMentionCustomRangeController } from './controllers/doc-mention-custom-range.controller';import { DocMentionService } from './services/doc-mention.service';+import { DocMentionResourceController } from './controllers/doc-mention-resource.controller';@DependentOn(UniverDocsPlugin)export class UniverDocsMentionPlugin extends Plugin {@@ -43,6 +44,7 @@ export class UniverDocsMentionPlugin extends Plugin {[DocMentionController],[DocMentionCustomRangeController],+ [DocMentionResourceController],];deps.forEach((dep) => {