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
feat(docs-ui): add FloatToolbar component and its stories (#4835)
白熱04772114fac2
diff --git a/packages/docs-ui/src/components/float-toolbar/FloatToolbar.stories.tsx b/packages/docs-ui/src/components/float-toolbar/FloatToolbar.stories.tsxnew file mode 100644index 0000000000..c238e2cbac--- /dev/null+++ b/packages/docs-ui/src/components/float-toolbar/FloatToolbar.stories.tsx@@ -0,0 +1,119 @@+/**+ * 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 { Meta } from '@storybook/react';++import type { Dependency } from '@univerjs/core';+import { LocaleService, LocaleType, ThemeService } from '@univerjs/core';+import { ColorPicker, defaultTheme } from '@univerjs/design';+import { DocSelectionManagerService } from '@univerjs/docs';+import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';+import { ComponentManager, DesktopLayoutService, FontFamily, FontFamilyItem, FontSize, ILayoutService, IMenuManagerService, MenuManagerService, RediContext } from '@univerjs/ui';+import { useContext, useEffect, useState } from 'react';+import {+ SetInlineFormatBoldCommand,+ SetInlineFormatFontFamilyCommand,+ SetInlineFormatFontSizeCommand,+ SetInlineFormatItalicCommand,+ SetInlineFormatStrikethroughCommand,+ SetInlineFormatSubscriptCommand,+ SetInlineFormatSuperscriptCommand,+ SetInlineFormatTextBackgroundColorCommand,+ SetInlineFormatTextColorCommand,+ SetInlineFormatUnderlineCommand,+} from '../../commands/commands/inline-format.command';+import { menuSchema } from '../../controllers/menu.schema';+import enUS from '../../locale/en-US';+import ruRU from '../../locale/ru-RU';+import zhCN from '../../locale/zh-CN';+import { COLOR_PICKER_COMPONENT } from '../color-picker';+import { FONT_FAMILY_COMPONENT, FONT_FAMILY_ITEM_COMPONENT } from '../font-family';+import { FONT_SIZE_COMPONENT } from '../font-size';+import { FloatToolbar } from './FloatToolbar';++const meta: Meta<typeof FloatToolbar> = {+ title: 'FloatToolbar',+ component: FloatToolbar,+ parameters: {+ layout: 'centered',+ },+ tags: ['autodocs'],+};++export default meta;++export const Playground = {+ render() {+ const { injector } = useContext(RediContext);++ const [inject] = useState(() => {+ const deps: Dependency[] = [+ [ComponentManager],++ [ILayoutService, { useClass: DesktopLayoutService }],+ [IMenuManagerService, { useClass: MenuManagerService }],+ [DocSelectionManagerService],+ [IRenderManagerService, { useClass: RenderManagerService }],+ ];++ injector?.get(LocaleService).load({+ [LocaleType.EN_US]: enUS,+ [LocaleType.ZH_CN]: zhCN,+ [LocaleType.RU_RU]: ruRU,+ });++ deps.forEach((dependency) => injector?.add(dependency));++ return injector;+ });++ useEffect(() => {+ if (!inject) return;+ const themeService = inject.get(ThemeService);+ themeService.setTheme(defaultTheme);++ const componentManager = inject.get(ComponentManager);+ componentManager.register(COLOR_PICKER_COMPONENT, ColorPicker);+ componentManager.register(FONT_FAMILY_COMPONENT, FontFamily);+ componentManager.register(FONT_FAMILY_ITEM_COMPONENT, FontFamilyItem);+ componentManager.register(FONT_SIZE_COMPONENT, FontSize);++ const menuManagerService = inject.get(IMenuManagerService);+ menuManagerService.mergeMenu(menuSchema);+ }, [inject]);++ return (+ <RediContext.Provider value={{ injector: inject }}>+ <div>+ <FloatToolbar+ avaliableMenus={[+ SetInlineFormatFontFamilyCommand.id,+ SetInlineFormatFontSizeCommand.id,+ SetInlineFormatBoldCommand.id,+ SetInlineFormatItalicCommand.id,+ SetInlineFormatUnderlineCommand.id,+ SetInlineFormatStrikethroughCommand.id,+ SetInlineFormatSubscriptCommand.id,+ SetInlineFormatSuperscriptCommand.id,+ SetInlineFormatTextColorCommand.id,+ SetInlineFormatTextBackgroundColorCommand.id,+ ]}+ />+ </div>+ </RediContext.Provider>+ );+ },+};diff --git a/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsx b/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsxnew file mode 100644index 0000000000..6f8e0ac66c--- /dev/null+++ b/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsx@@ -0,0 +1,87 @@+/**+ * 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 { IMenuSchema } from '@univerjs/ui';+import { IMenuManagerService, MenuManagerPosition, ToolbarItem, useDependency } from '@univerjs/ui';+import { useEffect, useState } from 'react';++interface IFloatToolbarProps {+ avaliableMenus?: string[];+ position?: {+ top?: number;+ right?: number;+ bottom?: number;+ left?: number;+ };+}++export function FloatToolbar(props: IFloatToolbarProps) {+ const { avaliableMenus = [], position = { top: 0, left: 0 } } = props;++ const { top, right, bottom, left } = position;++ const menuManagerService = useDependency(IMenuManagerService);++ const [menus, setMenus] = useState<IMenuSchema[]>([]);++ // subscribe to menu changes+ useEffect(() => {+ function getRibbon(): void {+ const flatMenus = menuManagerService.getFlatMenuByPositionKey(MenuManagerPosition.RIBBON);++ const menus: IMenuSchema[] = [];+ for (const key of avaliableMenus) {+ const item = flatMenus.find((item) => item.key === key);+ if (item) {+ // TODO: should not delete the hidden$ property, it just a hack to make the menu visible in stories+ delete item.item?.hidden$;++ menus.push(item);+ }+ }+ setMenus(menus);+ }+ getRibbon();++ const subscription = menuManagerService.menuChanged$.subscribe(getRibbon);++ return () => {+ subscription.unsubscribe();+ };+ }, [menuManagerService]);++ return (+ <div+ className={`+ univer-fixed univer-box-border univer-flex univer-rounded-xl univer-border univer-border-solid+ univer-border-gray-200 univer-bg-white univer-p-2+ univer-shadow-[0_1px_6px_-2px_rgba(30,40,77,0.08),0_2px_6px_-1px_rgba(30,40,77,0.10)]+ `}+ style={{+ top: `${top}px`,+ right: `${right}px`,+ bottom: `${bottom}px`,+ left: `${left}px`,+ }}+ >+ {menus.map((groupItem) => groupItem.item && (+ <div key={groupItem.key} className="univer-flex univer-flex-nowrap univer-gap-2 univer-px-2">+ <ToolbarItem key={groupItem.key} {...groupItem.item} />+ </div>+ ))}+ </div>+ );+}diff --git a/packages/docs-ui/src/controllers/menu/menu.ts b/packages/docs-ui/src/controllers/menu/menu.tsindex acb9c065ab..c467faf76b 100644--- a/packages/docs-ui/src/controllers/menu/menu.ts+++ b/packages/docs-ui/src/controllers/menu/menu.ts@@ -15,9 +15,7 @@*/import type { DocumentDataModel, IAccessor, PresetListType } from '@univerjs/core';-import type {- IRichTextEditingMutationParams,-} from '@univerjs/docs';+import type { IRichTextEditingMutationParams } from '@univerjs/docs';import type { IMenuButtonItem, IMenuItem, IMenuSelectorItem } from '@univerjs/ui';import type { Subscription } from 'rxjs';import {diff --git a/packages/sheets-numfmt-ui/src/views/components/stories/Panel.stories.tsx b/packages/sheets-numfmt-ui/src/views/components/stories/Panel.stories.tsxindex 621d3ef7c3..24656cbf7c 100644--- a/packages/sheets-numfmt-ui/src/views/components/stories/Panel.stories.tsx+++ b/packages/sheets-numfmt-ui/src/views/components/stories/Panel.stories.tsx@@ -19,7 +19,7 @@ import type { Dependency } from '@univerjs/core';import type { ISheetNumfmtPanelProps } from '../index';import { LocaleService, LocaleType } from '@univerjs/core';import { RediContext } from '@univerjs/ui';-import React, { useContext, useState } from 'react';+import { useContext, useState } from 'react';import { UserHabitController } from '../../../controllers/user-habit.controller';import enUS from '../../../locale/en-US';import ruRU from '../../../locale/ru-RU';@@ -36,7 +36,6 @@ const meta: Meta = {export default meta;export const Test: StoryObj<ISheetNumfmtPanelProps> = {-render() {const { injector } = useContext(RediContext);diff --git a/packages/ui/src/services/menu/menu-manager.service.ts b/packages/ui/src/services/menu/menu-manager.service.tsindex 33ab7ede29..f24b7d92ff 100644--- a/packages/ui/src/services/menu/menu-manager.service.ts+++ b/packages/ui/src/services/menu/menu-manager.service.ts@@ -39,6 +39,8 @@ export interface IMenuManagerService {appendRootMenu(source: MenuSchemaType): void;getMenuByPositionKey(position: string): IMenuSchema[];++ getFlatMenuByPositionKey(position: string): IMenuSchema[];}export type MenuSchemaType = {@@ -277,4 +279,24 @@ export class MenuManagerService extends Disposable implements IMenuManagerServicreturn findKey(this._menu);}++ /**+ * Get flat menu schema by position key+ * @param key+ * @returns Flat menu schema array or empty array if not found+ */+ getFlatMenuByPositionKey(key: string): IMenuSchema[] {+ const menu = this.getMenuByPositionKey(key);++ function flatMenuItems(items: IMenuSchema[]): IMenuSchema[] {+ return items.reduce((acc, item) => {+ if (item.children) {+ return [...acc, item, ...flatMenuItems(item.children)];+ }+ return [...acc, item];+ }, [] as IMenuSchema[]);+ }++ return flatMenuItems(menu);+ }}