Skip to content

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.

4.1 KB
Raw Blame
/**
 * 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 { IUniverSheetsFilterUIConfig } from './config/config';
import {
    DependentOn,
    IConfigService,
    Inject,
    Injector,
    merge,
    Optional,
    Plugin,
    registerDependencies,
    touchDependencies,
    UniverInstanceType,
} from '@univerjs/core';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { IRPCChannelService, toModule } from '@univerjs/rpc';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
import pkg from '../package.json';
import { defaultPluginConfig, SHEETS_FILTER_UI_PLUGIN_CONFIG_KEY } from './config/config';
import { ComponentsController } from './controllers/components.controller';
import { SheetsFilterPermissionController } from './controllers/sheets-filter-permission.controller';
import { SheetsFilterUIDesktopController } from './controllers/ui.controller';
import { ISheetsFilterPanelService, SheetsFilterPanelService } from './services/sheets-filter-panel.service';
import {
    ISheetsGenerateFilterValuesService,
    SHEETS_GENERATE_FILTER_VALUES_SERVICE_NAME,
} from './worker/generate-filter-values.service';

/**
 * The plugin for the desktop version of the sheets filter UI. Its type is {@link UniverInstanceType.UNIVER_SHEET}.
 */
@DependentOn(
    UniverRenderEnginePlugin,
    UniverSheetsPlugin,
    UniverSheetsFilterPlugin,
    UniverSheetsUIPlugin
)
export class UniverSheetsFilterUIPlugin extends Plugin {
    static override type = UniverInstanceType.UNIVER_SHEET;
    static override pluginName = 'SHEET_FILTER_UI_PLUGIN';
    static override packageName = pkg.name;
    static override version = pkg.version;

    constructor(
        private readonly _config: Partial<IUniverSheetsFilterUIConfig> = defaultPluginConfig,
        @Inject(Injector) protected readonly _injector: Injector,
        @IConfigService private readonly _configService: IConfigService,
        @Optional(IRPCChannelService) private readonly _rpcChannelService?: IRPCChannelService
    ) {
        super();

        // Manage the plugin configuration.
        const { menu, ...rest } = merge(
            {},
            defaultPluginConfig,
            this._config
        );
        if (menu) {
            this._configService.setConfig('menu', menu, { merge: true });
        }
        this._configService.setConfig(SHEETS_FILTER_UI_PLUGIN_CONFIG_KEY, rest);
    }

    override onStarting(): void {
        this._injector.add([ComponentsController]);
        this._injector.get(ComponentsController);
        registerDependencies(this._injector, [
            [ISheetsFilterPanelService, { useClass: SheetsFilterPanelService }],
            [SheetsFilterPermissionController],
            [SheetsFilterUIDesktopController],
        ]);

        if (this._config.useRemoteFilterValuesGenerator && this._rpcChannelService) {
            this._injector.add([ISheetsGenerateFilterValuesService, {
                useFactory: (): ISheetsGenerateFilterValuesService =>
                    toModule<ISheetsGenerateFilterValuesService>(
                        this._rpcChannelService!.requestChannel(SHEETS_GENERATE_FILTER_VALUES_SERVICE_NAME)
                    ),
            }]);
        }
    }

    override onReady(): void {
        touchDependencies(this._injector, [
            [SheetsFilterPermissionController],
        ]);
    }

    override onRendered(): void {
        touchDependencies(this._injector, [
            [SheetsFilterUIDesktopController],
        ]);
    }
}