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.

11.4 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 { Injector } from '@univerjs/core';
import type { ComponentType } from 'react';
import type { IUniverUIConfig } from '../../config/config';
import type { IWorkbenchOptions } from '../../controllers/ui/ui.controller';
import { IConfigService, LifecycleService, LifecycleStages, LocaleService, ThemeService } from '@univerjs/core';
import { borderBottomClassName, clsx, ConfigContext, ConfigProvider, render } from '@univerjs/design';
import { useContext, useEffect, useLayoutEffect, useMemo, useRef } from 'react';
import { createPortal } from 'react-dom';
import { map } from 'rxjs';
import { UI_PLUGIN_CONFIG_KEY } from '../../config/config';
import { BuiltInUIPart } from '../../services/parts/parts.service';
import { ThemeSwitcherService } from '../../services/theme-switcher/theme-switcher.service';
import { IWorkbenchService } from '../../services/workbench/workbench.service';
import { connectInjector, useDependency, useObservable } from '../../utils/di';
import { ComponentContainer, useComponentsOfPart } from '../components/ComponentContainer';
import { DesktopContextMenu } from '../components/context-menu/ContextMenu';
import { Sidebar } from '../components/sidebar/Sidebar';
import { WorkbenchSkeleton } from '../components/workbench-skeleton/WorkbenchSkeleton';
import { useConfigValue } from '../hooks/index';

export interface IUniverWorkbenchProps extends IWorkbenchOptions {
    mountContainer: HTMLElement;

    onRendered?: (containerElement: HTMLElement) => void;
}

export function DesktopWorkbench(props: IUniverWorkbenchProps) {
    const uiConfig = useConfigValue<IUniverUIConfig>(UI_PLUGIN_CONFIG_KEY);
    return <DesktopWorkbenchContent {...props} {...uiConfig} />;
}

export function mountDesktopWorkbench(
    injector: Injector,
    options: IWorkbenchOptions,
    mountContainer: HTMLElement,
    onRendered: (contentElement: HTMLElement) => void
): void {
    const ConnectedApp = connectInjector(DesktopWorkbench, injector) as ComponentType<IUniverWorkbenchProps>;

    render(
        <ConnectedApp
            {...options}
            mountContainer={mountContainer}
            onRendered={onRendered}
        />,
        mountContainer
    );
}

export function DesktopWorkbenchContent(props: IUniverWorkbenchProps) {
    const {
        header = true,
        toolbar = true,
        footer = true,
        headerMenu = true,
        contextMenu = true,
        ribbonType = 'classic',
        mountContainer,
        onRendered,
    } = props;

    const localeService = useDependency(LocaleService);
    const lifecycleService = useDependency(LifecycleService);
    const workbenchService = useDependency(IWorkbenchService);
    const themeService = useDependency(ThemeService);
    const themeSwitcherService = useDependency(ThemeSwitcherService);
    const contentRef = useRef<HTMLDivElement>(null);
    const configService = useDependency(IConfigService);
    const uiConfig = configService.getConfig(UI_PLUGIN_CONFIG_KEY) as IUniverUIConfig;
    const customHeaderComponents = useComponentsOfPart(BuiltInUIPart.CUSTOM_HEADER);
    const footerComponents = useComponentsOfPart(BuiltInUIPart.FOOTER);
    const headerComponents = useComponentsOfPart(BuiltInUIPart.HEADER);
    const headerMenuComponents = useComponentsOfPart(BuiltInUIPart.HEADER_MENU);
    const contentComponents = useComponentsOfPart(BuiltInUIPart.CONTENT);
    const leftSidebarComponents = useComponentsOfPart(BuiltInUIPart.LEFT_SIDEBAR);
    const globalComponents = useComponentsOfPart(BuiltInUIPart.GLOBAL);
    const toolbarComponents = useComponentsOfPart(BuiltInUIPart.TOOLBAR);
    const lifecycleStage = useObservable(lifecycleService.lifecycle$, lifecycleService.stage);
    const externalSkeletonVisible = useObservable(workbenchService.skeletonVisible$, undefined, true);
    const ready = lifecycleStage >= LifecycleStages.Ready;

    const popupRootId = uiConfig?.popupRootId ?? 'univer-popup-portal';

    useLayoutEffect(() => {
        const sub = themeService.currentTheme$.subscribe((theme) => {
            themeSwitcherService.injectThemeToHead(theme);
        });

        return () => {
            sub.unsubscribe();
        };
    }, []);

    const darkMode = useObservable(themeService.darkMode$, themeService.darkMode);
    useLayoutEffect(() => {
        if (darkMode) {
            document.documentElement.classList.add('univer-dark');
        } else {
            document.documentElement.classList.remove('univer-dark');
        }
    }, [darkMode]);

    useEffect(() => {
        if (ready && contentRef.current) {
            onRendered?.(contentRef.current);
        }
    }, [ready, onRendered]);

    const locale = useObservable(
        () => localeService.localeChanged$.pipe(map(() => localeService.getLocales())),
        localeService.getLocales(),
        false,
        [localeService]
    );
    const direction = useObservable(localeService.direction$, localeService.getDirection());

    // Create a portal container for injecting global component themes.
    const portalContainer = useMemo<HTMLElement>(() => document.createElement('div'), []);

    useEffect(() => {
        document.body.appendChild(portalContainer);

        return () => {
            document.body.removeChild(portalContainer);
        };
    }, [mountContainer, portalContainer]);

    useEffect(() => {
        portalContainer.dir = direction;
    }, [direction, portalContainer]);

    return (
        <ConfigProvider locale={locale?.design} direction={direction} mountContainer={portalContainer}>
            <div className="univer-relative univer-h-full univer-min-h-0">
                {/**
                  * IMPORTANT! This `tabIndex` should not be moved. This attribute allows the element to catch
                  * all focusin event merged from its descendants. The DesktopLayoutService would listen to focusin events
                  * bubbled to this element and refocus the input element.
                  */}
                <div
                    data-u-comp="workbench-layout"
                    className={clsx(`
                      univer-flex univer-h-full univer-min-h-0 univer-flex-col univer-bg-gray-0
                      dark:!univer-bg-gray-800
                    `, {
                        'univer-dark': darkMode,
                    })}
                    tabIndex={-1}
                    onBlur={(e) => e.stopPropagation()}
                    onContextMenu={(e) => e.preventDefault()}
                    dir={direction}
                >
                    {/* user header */}
                    <div
                        className={`
                          univer-relative univer-flex univer-min-h-0 univer-flex-col univer-bg-gray-0
                          dark:!univer-bg-gray-800
                        `}
                    >
                        <ComponentContainer key="custom-header" components={customHeaderComponents} />
                    </div>

                    {/* header */}
                    {header && toolbar && (
                        <header
                            data-u-comp="headerbar"
                            className="univer-relative univer-z-10 univer-w-full univer-overflow-hidden"
                        >
                            <ComponentContainer
                                key="toolbar"
                                components={toolbarComponents}
                                sharedProps={{
                                    ribbonType,
                                    headerMenuComponents,
                                    headerMenu,
                                }}
                            />
                        </header>
                    )}

                    {/* content */}
                    <section className="univer-relative univer-flex univer-min-h-0 univer-flex-1 univer-flex-col">
                        <div
                            className={`
                              univer-grid univer-h-full univer-grid-cols-[auto_1fr_auto] univer-grid-rows-[100%]
                              univer-overflow-hidden
                            `}
                        >
                            <aside data-u-comp="left-sidebar" className="univer-h-full">
                                <ComponentContainer key="left-sidebar" components={leftSidebarComponents} />
                            </aside>

                            <section
                                className={clsx(`
                                  univer-relative univer-grid univer-flex-1 univer-grid-rows-[auto_1fr]
                                  univer-overflow-hidden univer-bg-gray-0
                                  dark:!univer-bg-gray-800
                                `, borderBottomClassName)}
                            >
                                <header>
                                    {header && <ComponentContainer key="header" components={headerComponents} />}
                                </header>

                                <section
                                    className={`
                                      univer-relative univer-overflow-hidden
                                      dark:!univer-bg-gray-900
                                    `}
                                    ref={contentRef}
                                    data-range-selector
                                    onContextMenu={(e) => e.preventDefault()}
                                >
                                    <ComponentContainer key="content" components={contentComponents} />
                                </section>

                            </section>

                            <aside data-u-comp="right-sidebar" className="univer-z-[100] univer-h-full">
                                <Sidebar />
                            </aside>
                        </div>

                        {/* footer */}
                        {footer && (
                            <footer>
                                <ComponentContainer key="footer" components={footerComponents} sharedProps={{ contextMenu }} />
                            </footer>
                        )}
                    </section>
                </div>
                {(!ready || externalSkeletonVisible) && (
                    <WorkbenchSkeleton darkMode={darkMode} direction={direction} overlay />
                )}
            </div>

            <div dir={direction}>
                <ComponentContainer key="global" components={globalComponents} />
                {contextMenu && <DesktopContextMenu />}
                <FloatingContainer />
                <div id={popupRootId} />
            </div>
        </ConfigProvider>
    );
}

function FloatingContainer() {
    const { mountContainer } = useContext(ConfigContext);
    const floatingComponents = useComponentsOfPart(BuiltInUIPart.FLOATING);

    return createPortal(<ComponentContainer key="floating" components={floatingComponents} />, mountContainer!);
}