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.

5.4 KB
Raw Blame
import type { Plugin, PluginCtor } from '@univerjs/core';
import { LocaleType } from '@univerjs/core';
import { UniverDocsMobileUIPlugin } from '@univerjs/docs-ui';
import { UniverSheetsConditionalFormattingMobileUIPlugin, UniverSheetsConditionalFormattingPreset } from '@univerjs/preset-sheets-conditional-formatting';
import {
    UniverSheetsCorePreset,
    UniverSheetsFormulaMobileUIPlugin,
    UniverSheetsMobileUIPlugin,
    UniverSheetsNumfmtMobileUIPlugin,
} from '@univerjs/preset-sheets-core';
import { UniverSheetsDataValidationMobileUIPlugin, UniverSheetsDataValidationPreset } from '@univerjs/preset-sheets-data-validation';
import { UniverDrawingMobileUIPlugin, UniverSheetsDrawingMobileUIPlugin, UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing';
import { UniverSheetsFilterMobileUIPlugin, UniverSheetsFilterPreset } from '@univerjs/preset-sheets-filter';
import { UniverFindReplaceMobileUIPlugin, UniverSheetsFindReplaceMobileUIPlugin, UniverSheetsFindReplacePreset } from '@univerjs/preset-sheets-find-replace';
import { UniverSheetsHyperLinkMobileUIPlugin, UniverSheetsHyperLinkPreset } from '@univerjs/preset-sheets-hyper-link';
import { UniverSheetsNotePreset } from '@univerjs/preset-sheets-note';
import { UniverSheetsSortMobileUIPlugin, UniverSheetsSortPreset } from '@univerjs/preset-sheets-sort';
import { UniverSheetsTableMobileUIPlugin, UniverSheetsTablePreset } from '@univerjs/preset-sheets-table';
import { UniverSheetsThreadCommentMobileUIPlugin, UniverSheetsThreadCommentPreset, UniverThreadCommentMobileUIPlugin } from '@univerjs/preset-sheets-thread-comment';
import { createUniver } from '@univerjs/presets';
import { UniverMobileUIPlugin, UniverUIPlugin } from '@univerjs/ui';
import { loadSheetLocale } from 'virtual:univer-examples-sheet-locale';
import type { MountExample } from '../mount-example';
import type { IWorkbenchMountOptions } from '../workbench-settings';
import { applyWorkbenchUIChrome, createMountedUniver, replacePresetPlugins } from '../mount-example';
import { getEffectiveWorkbenchRegion } from '../workbench-settings';
import { applyWorkbookZoom } from './apply-workbook-zoom';
import { createSheetFixture } from './create-sheet-fixture';
import './global.css';

const UNIVER_LOCALES = {
    [LocaleType.ZH_CN]: LocaleType.ZH_CN,
    [LocaleType.EN_US]: LocaleType.EN_US,
    [LocaleType.AR_SA]: LocaleType.AR_SA,
    [LocaleType.FR_FR]: LocaleType.FR_FR,
    [LocaleType.RU_RU]: LocaleType.RU_RU,
    [LocaleType.ZH_TW]: LocaleType.ZH_TW,
    [LocaleType.ZH_HK]: LocaleType.ZH_HK,
    [LocaleType.VI_VN]: LocaleType.VI_VN,
    [LocaleType.FA_IR]: LocaleType.FA_IR,
    [LocaleType.JA_JP]: LocaleType.JA_JP,
    [LocaleType.KO_KR]: LocaleType.KO_KR,
    [LocaleType.ES_ES]: LocaleType.ES_ES,
    [LocaleType.CA_ES]: LocaleType.CA_ES,
    [LocaleType.SK_SK]: LocaleType.SK_SK,
    [LocaleType.PT_BR]: LocaleType.PT_BR,
    [LocaleType.DE_DE]: LocaleType.DE_DE,
    [LocaleType.IT_IT]: LocaleType.IT_IT,
    [LocaleType.ID_ID]: LocaleType.ID_ID,
    [LocaleType.PL_PL]: LocaleType.PL_PL,
} satisfies Record<IWorkbenchMountOptions['locale'], LocaleType>;

const MOBILE_PLUGINS = new Map<string, PluginCtor<Plugin>>([
    UniverDocsMobileUIPlugin,
    UniverSheetsMobileUIPlugin,
    UniverSheetsFormulaMobileUIPlugin,
    UniverSheetsNumfmtMobileUIPlugin,
    UniverDrawingMobileUIPlugin,
    UniverSheetsDrawingMobileUIPlugin,
    UniverSheetsConditionalFormattingMobileUIPlugin,
    UniverSheetsFilterMobileUIPlugin,
    UniverSheetsHyperLinkMobileUIPlugin,
    UniverSheetsDataValidationMobileUIPlugin,
    UniverFindReplaceMobileUIPlugin,
    UniverSheetsFindReplaceMobileUIPlugin,
    UniverSheetsSortMobileUIPlugin,
    UniverSheetsTableMobileUIPlugin,
    UniverThreadCommentMobileUIPlugin,
    UniverSheetsThreadCommentMobileUIPlugin,
].map((plugin) => [plugin.pluginName, plugin]));
MOBILE_PLUGINS.set(UniverUIPlugin.pluginName, UniverMobileUIPlugin);

export const mount: MountExample = async (host, options) => {
    const { darkMode, direction, locale, ribbonType, theme, zoomRatio } = options;
    const univerLocale = UNIVER_LOCALES[locale];
    const localePack = await loadSheetLocale(locale);
    const presets = [
        UniverSheetsCorePreset({ container: host, ribbonType }),
        UniverSheetsDrawingPreset(),
        UniverSheetsConditionalFormattingPreset(),
        UniverSheetsFilterPreset(),
        UniverSheetsHyperLinkPreset(),
        UniverSheetsDataValidationPreset(),
        UniverSheetsFindReplacePreset(),
        UniverSheetsNotePreset(),
        UniverSheetsSortPreset(),
        UniverSheetsTablePreset(),
        UniverSheetsThreadCommentPreset(),
    ];
    const { univer, univerAPI } = createUniver({
        locale: univerLocale,
        region: UNIVER_LOCALES[getEffectiveWorkbenchRegion(options)],
        locales: {
            [univerLocale]: localePack,
        },
        theme,
        darkMode,
        direction,
        presets: options.device === 'mobile' ? replacePresetPlugins(presets, MOBILE_PLUGINS) : presets,
    });

    try {
        applyWorkbenchUIChrome(univerAPI, options.uiChrome);
        univerAPI.createWorkbook(createSheetFixture(univerLocale, direction, zoomRatio));
        return createMountedUniver(
            host,
            univer,
            univerAPI,
            options,
            loadSheetLocale,
            (nextOptions) => applyWorkbookZoom(univerAPI, nextOptions)
        );
    } catch (error) {
        univer.dispose();
        host.replaceChildren();
        throw error;
    }
};