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
/**
* 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 {
CommandType,
ICommandService,
IConfigService,
ILogService,
Injector,
IUniverInstanceService,
LogLevel,
Univer,
} from '@univerjs/core';
import { FUniver } from '@univerjs/core/facade';
import { MessageType } from '@univerjs/design';
import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { IMenuSchema } from '../../services/menu/menu-manager.service';
import { ComponentManager } from '../../common/component-manager';
import { UI_PLUGIN_CONFIG_KEY } from '../../config/config';
import { CopyCommand, PasteCommand } from '../../services/clipboard/clipboard.command';
import { ContextMenuHostService, IContextMenuHostService } from '../../services/contextmenu/contextmenu-host.service';
import { DesktopDialogService } from '../../services/dialog/desktop-dialog.service';
import { IDialogService } from '../../services/dialog/dialog.service';
import { FontCategory, FontService, IFontService } from '../../services/font.service';
import { IMenuManagerService, MenuManagerService } from '../../services/menu/menu-manager.service';
import { RibbonStartGroup } from '../../services/menu/types';
import { DesktopMessageService } from '../../services/message/desktop-message.service';
import { IMessageService } from '../../services/message/message.service';
import { BuiltInUIPart, IUIPartsService, UIPartsService } from '../../services/parts/parts.service';
import { IPlatformService, PlatformService } from '../../services/platform/platform.service';
import { DesktopRibbonService, IRibbonService } from '../../services/ribbon/ribbon.service';
import { IUIRuntimeScopeService, UIRuntimeScopeService } from '../../services/runtime-scope/ui-runtime-scope.service';
import { KeyCode } from '../../services/shortcut/keycode';
import { IShortcutService, ShortcutService } from '../../services/shortcut/shortcut.service';
import { DesktopSidebarService } from '../../services/sidebar/desktop-sidebar.service';
import { ISidebarService } from '../../services/sidebar/sidebar.service';
import { FShortcut } from '../f-shortcut';
import '../f-enum';
import '../f-univer';
class FakeShortcutService {
static dispatched: KeyboardEvent[] = [];
forceDisable() {
return { dispose: () => {} };
}
dispatch(event: KeyboardEvent) {
FakeShortcutService.dispatched.push(event);
return { id: 'fake.shortcut', binding: event.keyCode };
}
}
class FakeRenderManagerService {
static renderUnit: unknown;
getRenderUnitById(_unitId: string) {
return FakeRenderManagerService.renderUnit;
}
}
class FakeUniverInstanceService {
static workbook: unknown;
getCurrentUnitOfType() {
return FakeUniverInstanceService.workbook;
}
}
describe('ui facade', () => {
let univer: Univer;
let univerAPI: FUniver;
beforeEach(() => {
univer = new Univer();
const injector = univer.__getInjector();
injector.add([ComponentManager]);
injector.add([IContextMenuHostService, { useClass: ContextMenuHostService }]);
injector.add([IDialogService, { useClass: DesktopDialogService }]);
injector.add([IFontService, { useClass: FontService }]);
injector.add([IMenuManagerService, { useClass: MenuManagerService }]);
injector.add([IMessageService, { useClass: DesktopMessageService }]);
injector.add([IPlatformService, { useClass: PlatformService }]);
injector.add([ISidebarService, { useClass: DesktopSidebarService }]);
injector.add([IUIPartsService, { useClass: UIPartsService }]);
injector.add([IUIRuntimeScopeService, { useClass: UIRuntimeScopeService }]);
injector.add([IShortcutService, { useClass: ShortcutService }]);
injector.add([IRenderManagerService, { useClass: RenderManagerService }]);
injector.get(ILogService).setLogLevel(LogLevel.SILENT);
univerAPI = FUniver.newAPI(injector);
});
afterEach(() => {
univer.dispose();
});
it('adds custom menus and executes their user action through command service', async () => {
const injector = univer.__getInjector();
const menuManagerService = injector.get(IMenuManagerService);
const commandService = injector.get(ICommandService);
const actionCalls: string[] = [];
univerAPI.createMenu({
id: 'custom-menu',
title: 'Custom Menu',
action: () => actionCalls.push('clicked'),
order: 1,
}).appendTo('contextMenu.others');
const menuItem = menuManagerService.getFlatMenuByPositionKey('contextMenu').find((item) => item.item?.id === 'custom-menu')?.item;
expect(menuItem?.title).toBe('Custom Menu');
expect(menuItem?.commandId).toBeDefined();
await commandService.executeCommand(menuItem!.commandId!);
expect(actionCalls).toEqual(['clicked']);
});
it('adds grouped submenu entries into menu positions', () => {
const menuManagerService = univer.__getInjector().get(IMenuManagerService);
const first = univerAPI.createMenu({ id: 'first-action', title: 'First', action: 'first.command' });
const second = univerAPI.createMenu({ id: 'second-action', title: 'Second', action: 'second.command' });
univerAPI.createSubmenu({ id: 'custom-submenu', title: 'Actions' })
.addSubmenu(first)
.addSeparator()
.addSubmenu(second)
.appendTo('contextMenu.others');
const menuKeys = menuManagerService.getFlatMenuByPositionKey('contextMenu').map((item) => item.key);
expect(menuKeys).toContain('custom-submenu');
expect(menuKeys).toContain('first-action');
expect(menuKeys).toContain('second-action');
});
it('updates ribbon menu order and grid layout while preserving menu actions and earlier overrides', async () => {
const injector = univer.__getInjector();
injector.add([IRibbonService, { useClass: DesktopRibbonService }]);
const ribbonService = injector.get(IRibbonService);
const actionCalls: string[] = [];
let ribbon: IMenuSchema[] = [];
const subscription = ribbonService.ribbon$.subscribe((value) => {
ribbon = value;
});
univerAPI.updateMenuConfig({
first: { title: 'Renamed', gridLayout: { row: 1, column: 1, width: 80 } },
});
univerAPI.createMenu({
id: 'first',
title: 'First',
action: () => actionCalls.push('clicked'),
order: 0,
gridLayout: { row: 1, column: 1, showLabel: true },
}).appendTo(RibbonStartGroup.FORMAT);
univerAPI.createSubmenu({
id: 'second',
title: 'Second',
order: 1,
gridLayout: { row: 2, column: 1 },
}).addSubmenu(univerAPI.createMenu({ id: 'child', title: 'Child', action: 'child.command' })).appendTo(RibbonStartGroup.FORMAT);
expect(ribbon[0].children?.[0].children?.map((item) => [item.key, item.gridLayout?.row])).toEqual([
['first', 1],
['second', 2],
]);
expect(univerAPI.updateMenuConfig({
first: { order: 2, gridLayout: { row: 2, column: 1 } },
second: { order: 0, gridLayout: { row: 1, column: 1 } },
})).toBe(univerAPI);
const items = ribbon[0].children![0].children!;
expect(items.map((item) => item.key)).toEqual(['second', 'first']);
expect(items[0].gridLayout).toEqual({ row: 1, column: 1 });
expect(items[1].gridLayout).toEqual({ row: 2, column: 1, showLabel: true, width: 80 });
expect(items[1].item?.title).toBe('Renamed');
await injector.get(ICommandService).executeCommand(items[1].item!.commandId!);
expect(actionCalls).toEqual(['clicked']);
subscription.unsubscribe();
});
it('registers UI parts, toggles visibility, and exposes UI enums from the facade', () => {
const component = () => null;
const disposable = univerAPI.registerUIPart(BuiltInUIPart.CUSTOM_HEADER, component);
const uiPartsService = univer.__getInjector().get(IUIPartsService);
expect(univerAPI.Enum.BuiltInUIPart.CUSTOM_HEADER).toBe(BuiltInUIPart.CUSTOM_HEADER);
expect(univerAPI.setUIVisible(BuiltInUIPart.CUSTOM_HEADER, false)).toBe(univerAPI);
expect(univerAPI.isUIVisible(BuiltInUIPart.CUSTOM_HEADER)).toBe(false);
expect(uiPartsService.getComponents(BuiltInUIPart.CUSTOM_HEADER).size).toBe(1);
disposable.dispose();
expect(uiPartsService.getComponents(BuiltInUIPart.CUSTOM_HEADER).size).toBe(0);
});
it('changes the ribbon type without replacing the remaining UI config', () => {
const configService = univer.__getInjector().get(IConfigService);
configService.setConfig(UI_PLUGIN_CONFIG_KEY, {
ribbonType: 'classic',
toolbar: false,
});
expect(univerAPI.setRibbonType('grid')).toBe(univerAPI);
expect(configService.getConfig(UI_PLUGIN_CONFIG_KEY)).toEqual({
ribbonType: 'grid',
toolbar: false,
});
});
it('dispatches shortcut events unless the shortcut facade is disabled', () => {
const shortcutService = univer.__getInjector().get(IShortcutService);
const shortcut = univerAPI.getShortcut();
shortcutService.registerShortcut({
id: 'custom.shortcut',
binding: KeyCode.B,
});
const event = new KeyboardEvent('keydown', { key: 'b', keyCode: KeyCode.B });
expect(shortcut.disableShortcut().dispatchShortcutEvent(event)).toBeUndefined();
expect(shortcut.enableShortcut().dispatchShortcutEvent(event)?.id).toBe('custom.shortcut');
});
it('executes copy and paste commands', async () => {
const commandService = univer.__getInjector().get(ICommandService);
commandService.registerCommand({
id: CopyCommand.id,
type: CommandType.COMMAND,
handler: () => true,
});
commandService.registerCommand({
id: PasteCommand.id,
type: CommandType.COMMAND,
handler: () => true,
});
await expect(univerAPI.copy()).resolves.toBe(true);
await expect(univerAPI.paste()).resolves.toBe(true);
});
it('opens and disposes UI surfaces through facade services', () => {
const injector = univer.__getInjector();
const sidebarService = injector.get(ISidebarService);
const dialogService = injector.get(IDialogService);
const dialogs: string[][] = [];
const subscription = dialogService.getDialogs$().subscribe((items) => dialogs.push(items.map((item) => item.id)));
const sidebarDisposable = univerAPI.openSidebar({ id: 'inspector', width: 320 });
expect(sidebarService.visible).toBe(true);
expect(sidebarService.options.id).toBe('inspector');
sidebarDisposable.dispose();
expect(sidebarService.visible).toBe(false);
const dialogDisposable = univerAPI.openDialog({ id: 'confirm-dialog', title: { label: 'Confirm' } });
expect(dialogs.at(-1)).toEqual(['confirm-dialog']);
dialogDisposable.dispose();
expect(dialogs.at(-1)).toEqual([]);
expect(univerAPI.showMessage({ content: 'Saved', type: MessageType.Success, id: 'saved-message' })).toBe(univerAPI);
subscription.unsubscribe();
});
it('registers components and appends custom fonts through facade APIs', () => {
const injector = univer.__getInjector();
const componentManager = injector.get(ComponentManager);
const fontService = injector.get(IFontService);
const component = () => null;
const disposable = univerAPI.registerComponent('custom-component', component);
univerAPI.addFonts([{ value: 'CustomSans', label: 'Custom Sans', category: FontCategory.SANS_SERIF }]);
expect(univerAPI.getURL().href).toBe(window.location.href);
expect(univerAPI.getComponentManager()).toBe(componentManager);
expect(componentManager.get('custom-component')).toBe(component);
expect(fontService.getFontByValue('CustomSans')).toMatchObject({ label: 'Custom Sans', category: FontCategory.SANS_SERIF });
disposable.dispose();
expect(componentManager.get('custom-component')).toBeUndefined();
});
it.each([undefined, {}, { framework: 'react' }, { override: undefined }])('defaults facade registrations to overrides with options %j', (options) => {
const manager = univer.__getInjector().get(ComponentManager);
const HostPanel = () => null;
const BuiltInPanel = () => null;
const override = univerAPI.registerComponent('panel', HostPanel, options);
manager.register('panel', BuiltInPanel);
expect(manager.get('panel')).toBe(HostPanel);
override.dispose();
expect(manager.get('panel')).toBe(BuiltInPanel);
});
it('honors an explicit false override option in facade registrations', () => {
const manager = univer.__getInjector().get(ComponentManager);
const HostPanel = () => null;
const BuiltInPanel = () => null;
const override = univerAPI.registerComponent('panel', HostPanel);
univerAPI.registerComponent('panel', BuiltInPanel, { override: false });
expect(manager.get('panel')).toBe(HostPanel);
override.dispose();
expect(manager.get('panel')).toBe(BuiltInPanel);
});
it('triggers shortcuts through the active workbook render canvas', () => {
const injector = new Injector();
injector.add([IShortcutService, { useClass: FakeShortcutService as never }]);
injector.add([IRenderManagerService, { useClass: FakeRenderManagerService as never }]);
injector.add([IUniverInstanceService, { useClass: FakeUniverInstanceService as never }]);
const shortcut = injector.createInstance(FShortcut);
const event = new KeyboardEvent('keydown', { key: 'k', keyCode: KeyCode.K });
const canvasEvents: KeyboardEvent[] = [];
FakeShortcutService.dispatched = [];
FakeUniverInstanceService.workbook = undefined;
FakeRenderManagerService.renderUnit = undefined;
expect(shortcut.triggerShortcut(event)).toBeUndefined();
FakeUniverInstanceService.workbook = { getUnitId: () => 'sheet-1' };
expect(shortcut.triggerShortcut(event)).toBeUndefined();
FakeRenderManagerService.renderUnit = {
engine: {
getCanvasElement: () => ({
dispatchEvent: (dispatchedEvent: KeyboardEvent) => canvasEvents.push(dispatchedEvent),
}),
},
};
expect(shortcut.triggerShortcut(event)?.id).toBe('fake.shortcut');
expect(canvasEvents).toEqual([event]);
expect(FakeShortcutService.dispatched).toEqual([event]);
});
});