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.

feat(ui): add ui facade menu position (#4546)

Wenzhao Hu9685cb30a556
diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts
index 2c1ebb099d..0d575a7748 100644
--- a/examples/src/sheets/main.ts
+++ b/examples/src/sheets/main.ts
@@ -174,3 +174,4 @@ declare global {
createNewInstance?: typeof createNewInstance;
}
}
+
diff --git a/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx b/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx
index b9f343db5c..e2585b099b 100644
--- a/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx
+++ b/packages/sheets-ui/src/views/sheet-container/SheetContainer.tsx
@@ -32,9 +32,10 @@ export function RenderSheetFooter() {
const menuManagerService = useDependency(IMenuManagerService);
const workbook = useActiveWorkbook();
+ if (!workbook) return null;
+
const footerMenus = menuManagerService.getMenuByPositionKey(ContextMenuPosition.FOOTER_MENU);
- if (!workbook) return null;
return (
<section className={styles.sheetContainer} data-range-selector>
<SheetBar />
diff --git a/packages/ui/src/facade/f-menu-builder.ts b/packages/ui/src/facade/f-menu-builder.ts
index 03d03a9b89..7fb3e76338 100644
--- a/packages/ui/src/facade/f-menu-builder.ts
+++ b/packages/ui/src/facade/f-menu-builder.ts
@@ -72,13 +72,65 @@ export interface IFacadeSubmenuItem {
type FAllMenu = FMenu | FSubmenu;
+/**
+ * @ignore
+ */
+abstract class FMenuBase extends FBase {
+ protected abstract readonly _menuManagerService: IMenuManagerService;
+ abstract __getSchema(): { [key: string]: MenuSchemaType };
+
+ /**
+ * Append the menu to any menu position on Univer UI.
+ * @param {string | string[]} path - Some predefined path to append the menu. The paths can be an array,
+ * or an array joined by `|` separator. Since lots of submenus reuse the same name,
+ * you may need to specify their parent menus as well.
+ *
+ * @example
+ * ```typescript
+ * // This menu item will appear on every `contextMenu.others` section.
+ * univerAPI.createMenu({
+ * id: 'custom-menu-id-1',
+ * title: 'Custom Menu 1',
+ * action: () => {},
+ * }).appendTo('contextMenu.others');
+ *
+ * // This menu item will only appear on the `contextMenu.others` section on the main area.
+ * univerAPI.createMenu({
+ * id: 'custom-menu-id-2',
+ * title: 'Custom Menu 2',
+ * action: () => { console.log(123); },
+ * }).appendTo(['contextMenu.mainArea', 'contextMenu.others']);
+ * ```
+ */
+ appendTo(path: string | string[]): void {
+ const paths = typeof path === 'string' ? path.split('|') : path;
+ const len = paths.length;
+
+ // eslint-disable-next-line ts/no-explicit-any
+ const menuConfig: Record<string, any> = {};
+ let obj = menuConfig;
+
+ const schema = this.__getSchema();
+ paths.forEach((p, index) => {
+ if (index === len - 1) {
+ obj[p] = schema;
+ } else {
+ obj[p] = {};
+ }
+ obj = obj[p];
+ });
+
+ this._menuManagerService.mergeMenu(menuConfig);
+ }
+}
+
/**
* This is a build for adding a menu to Univer. Please notice that until the `appendTo` method is called,
* the menu item is not added to the UI. Please note that this menu cannot have submenus. If you want to
* have submenus, please use `FSubmenu`.
* @hideconstructor
*/
-export class FMenu extends FBase {
+export class FMenu extends FMenuBase {
static RibbonStartGroup = RibbonStartGroup;
static RibbonPosition = RibbonPosition;
static MenuManagerPosition = MenuManagerPosition;
@@ -133,23 +185,12 @@ export class FMenu extends FBase {
return { [this._item.id]: this._buildingSchema };
}
-
- /**
- * Append the menu to any menu position on Univer UI.
- * @param path Some predefined path to append the menu.
- */
- appendTo(path: string): void {
- const schema = this.__getSchema();
- this._menuManagerService.mergeMenu({
- [path]: schema,
- });
- }
}
/**
* @hideconstructor
*/
-export class FSubmenu extends FBase {
+export class FSubmenu extends FMenuBase {
private _menuByGroups: FAllMenu[][] = [];
private _submenus: FAllMenu[] = [];
@@ -183,8 +224,8 @@ export class FSubmenu extends FBase {
/**
* Add a menu to the submenu. It can be a {@link FMenu} or a {@link FSubmenu}.
- * @param submenu Menu to add to the submenu.
- * @returns The FSubmenu itself for chaining calls.
+ * @param {FMenu | FSubmenu} submenu - Menu to add to the submenu.
+ * @returns {FSubmenu} The FSubmenu itself for chaining calls.
*/
addSubmenu(submenu: FMenu | FSubmenu): this {
this._submenus.push(submenu);
@@ -193,7 +234,7 @@ export class FSubmenu extends FBase {
/**
* Add a separator to the submenu.
- * @returns The FSubmenu itself for chaining calls.
+ * @returns {FSubmenu} The FSubmenu itself for chaining calls.
*/
addSeparator(): this {
this._menuByGroups.push(this._submenus);
@@ -201,17 +242,6 @@ export class FSubmenu extends FBase {
return this;
}
- /**
- * Append the menu to any menu position on Univer UI.
- * @param path Some predefined path to append the menu.
- */
- appendTo(path: string): void {
- const schema = this.__getSchema();
- this._menuManagerService.mergeMenu({
- [path]: schema,
- });
- }
-
/** @ignore */
__getSchema(): { [key: string]: MenuSchemaType } {
const schema: { [key: string]: MenuSchemaType } = {};