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.

refactor(ui): adjust layout & refactor toolbar (#769)

白熱e790cdff91d6
diff --git a/packages/ui/src/views/App.tsx b/packages/ui/src/views/App.tsx
index ab73bf8360..fa23686dd0 100644
--- a/packages/ui/src/views/App.tsx
+++ b/packages/ui/src/views/App.tsx
@@ -25,7 +25,6 @@ import type { IWorkbenchOptions } from '../controllers/ui/ui.controller';
import { IMessageService } from '../services/message/message.service';
import styles from './app.module.less';
import { ComponentContainer } from './components/ComponentContainer';
-import { MenuBar } from './components/doc-bars/MenuBar';
import { Toolbar } from './components/doc-bars/Toolbar';
import { Sidebar } from './components/sidebar/Sidebar';
import { globalComponents } from './parts';
@@ -98,39 +97,41 @@ export function App(props: IUniverAppProps) {
<ConfigProvider locale={locale} mountContainer={portalContainer}>
<div className={styles.appLayout}>
{/* header */}
- {props.toolbar && <MenuBar headerMenuComponents={headerMenuComponents} />}
+ {props.toolbar && (
+ <header className={styles.appContainerHeader}>
+ <Toolbar headerMenuComponents={headerMenuComponents} />
+ </header>
+ )}
{/* content */}
<section className={styles.appContainer}>
- <header className={styles.appContainerHeader}>{props.toolbar && <Toolbar />}</header>
-
- <div className={styles.appContainerMain}>
- <div className={styles.appContainerWrapper}>
- <section className={styles.appContainerContent}>
- <header>
- <ComponentContainer components={headerComponents} />
- </header>
-
- <section
- className={styles.appContainerCanvas}
- ref={contentRef}
- data-range-selector
- onContextMenu={(e) => e.preventDefault()}
- >
- <ComponentContainer components={contentComponents} />
- </section>
+ <div className={styles.appContainerWrapper}>
+ <section className={styles.appContainerContent}>
+ <header>
+ <ComponentContainer components={headerComponents} />
+ </header>
+
+ <section
+ className={styles.appContainerCanvas}
+ ref={contentRef}
+ data-range-selector
+ onContextMenu={(e) => e.preventDefault()}
+ >
+ <ComponentContainer components={contentComponents} />
</section>
+ </section>
- <aside className={styles.appContainerSidebar}>
- <Sidebar />
- </aside>
- </div>
+ <aside className={styles.appContainerSidebar}>
+ <Sidebar />
+ </aside>
+ </div>
- {/* footer */}
+ {/* footer */}
+ {props.footer && (
<footer className={styles.appFooter}>
<ComponentContainer components={footerComponents} />
</footer>
- </div>
+ )}
</section>
</div>
diff --git a/packages/ui/src/views/app.module.less b/packages/ui/src/views/app.module.less
index 6cbe85b47f..58dba43ff5 100644
--- a/packages/ui/src/views/app.module.less
+++ b/packages/ui/src/views/app.module.less
@@ -21,12 +21,6 @@
width: 100%;
}
- &-main {
- display: flex;
- flex: 1;
- flex-direction: column;
- }
-
&-wrapper {
display: grid;
grid-template-columns: 1fr auto;
diff --git a/packages/ui/src/views/components/doc-bars/MenuBar.tsx b/packages/ui/src/views/components/doc-bars/MenuBar.tsx
deleted file mode 100644
index 168089ef8e..0000000000
--- a/packages/ui/src/views/components/doc-bars/MenuBar.tsx
+++ /dev/null
@@ -1,87 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * 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 { LocaleService } from '@univerjs/core';
-import { useDependency } from '@wendellhu/redi/react-bindings';
-import clsx from 'clsx';
-import type { ComponentType } from 'react';
-import React, { useEffect, useState } from 'react';
-
-import { useObservable } from '../../../components/hooks/observable';
-import { MenuPosition } from '../../../services/menu/menu';
-import { IMenuService } from '../../../services/menu/menu.service';
-import { ComponentContainer } from '../ComponentContainer';
-import type { IMenuGroup } from './hooks/menu';
-import { position$, positions, setPosition } from './hooks/menu';
-import styles from './index.module.less';
-
-export interface IMenuBarProps {
- headerMenuComponents?: Set<() => ComponentType>;
-}
-
-export function MenuBar(props: IMenuBarProps) {
- const { headerMenuComponents } = props;
- const localeService = useDependency(LocaleService);
- const menuService = useDependency(IMenuService);
-
- const [group, setGroup] = useState<IMenuGroup[]>([]);
-
- useEffect(() => {
- const listener = menuService.menuChanged$.subscribe(() => {
- const group: IMenuGroup[] = [];
- for (const position of positions) {
- const menuItems = menuService.getMenuItems(position);
-
- if (menuItems.length) {
- group.push({
- name: position,
- menuItems,
- });
- }
- }
-
- setGroup(group);
- });
-
- return () => {
- listener.unsubscribe();
- };
- }, []);
-
- const position = useObservable(position$, MenuPosition.TOOLBAR_START, true);
-
- return (
- <header className={styles.headerbar}>
- <div className={styles.menubar}>
- {group.length > 1 &&
- group.map((item, index) => (
- <a
- key={index}
- className={clsx(styles.menubarItem, {
- [styles.menubarItemActive]: item.name === position,
- })}
- onClick={() => setPosition(item.name)}
- >
- {localeService.t(item.name)}
- </a>
- ))}
- </div>
- <div className={styles.headerMenu}>
- <ComponentContainer components={headerMenuComponents} />
- </div>
- </header>
- );
-}
diff --git a/packages/ui/src/views/components/doc-bars/Toolbar.tsx b/packages/ui/src/views/components/doc-bars/Toolbar.tsx
index 2f27ce5b8a..94a358bebf 100644
--- a/packages/ui/src/views/components/doc-bars/Toolbar.tsx
+++ b/packages/ui/src/views/components/doc-bars/Toolbar.tsx
@@ -14,23 +14,45 @@
* limitations under the License.
*/
+import { LocaleService } from '@univerjs/core';
import { Dropdown, Tooltip } from '@univerjs/design';
import { MoreFunctionSingle } from '@univerjs/icons';
import { useDependency } from '@wendellhu/redi/react-bindings';
+import clsx from 'clsx';
+import type { ComponentType } from 'react';
import React, { useEffect, useMemo, useRef, useState } from 'react';
-import { useObservable } from '../../../components/hooks/observable';
import type { IDisplayMenuItem, IMenuItem } from '../../../services/menu/menu';
import { MenuGroup, MenuPosition } from '../../../services/menu/menu';
import { IMenuService } from '../../../services/menu/menu.service';
+import { ComponentContainer } from '../ComponentContainer';
import { ToolbarButton } from './Button/ToolbarButton';
-import type { IMenuGroup } from './hooks/menu';
-import { position$, positions } from './hooks/menu';
import styles from './index.module.less';
import { ToolbarItem } from './ToolbarItem';
-export function Toolbar() {
+interface IMenuGroup {
+ name: MenuPosition;
+ menuItems: Array<IDisplayMenuItem<IMenuItem>>;
+}
+
+export const positions = [
+ MenuPosition.TOOLBAR_START,
+ MenuPosition.TOOLBAR_INSERT,
+ MenuPosition.TOOLBAR_FORMULAS,
+ MenuPosition.TOOLBAR_DATA,
+ MenuPosition.TOOLBAR_VIEW,
+ MenuPosition.TOOLBAR_OTHERS,
+];
+
+export interface IToolbarProps {
+ headerMenuComponents?: Set<() => ComponentType>;
+}
+
+export function Toolbar(props: IToolbarProps) {
+ const { headerMenuComponents } = props;
+ const localeService = useDependency(LocaleService);
const menuService = useDependency(IMenuService);
+ const [position, setPosition] = useState<MenuPosition>(MenuPosition.TOOLBAR_START);
const toolbarRef = useRef<HTMLDivElement>(null);
const toolbarContainerRef = useRef<HTMLDivElement>(null);
@@ -47,8 +69,6 @@ export function Toolbar() {
const [group, setGroup] = useState<IMenuGroup[]>([]);
const [collapsedId, setCollapsedId] = useState<string[]>([]);
- const position = useObservable(position$, MenuPosition.TOOLBAR_START, true);
-
useEffect(() => {
const listener = menuService.menuChanged$.subscribe(() => {
const group: IMenuGroup[] = [];
@@ -138,6 +158,26 @@ export function Toolbar() {
return (
<>
+ <header className={styles.headerbar}>
+ <div className={styles.menubar}>
+ {group.length > 1 &&
+ group.map((item, index) => (
+ <a
+ key={index}
+ className={clsx(styles.menubarItem, {
+ [styles.menubarItemActive]: item.name === position,
+ })}
+ onClick={() => setPosition(item.name)}
+ >
+ {localeService.t(item.name)}
+ </a>
+ ))}
+ </div>
+ <div className={styles.headerMenu}>
+ <ComponentContainer components={headerMenuComponents} />
+ </div>
+ </header>
+
<div ref={toolbarRef} className={styles.toolbar}>
<div className={styles.toolbarContainer}>
{Object.entries(toolbarGroups).map(
diff --git a/packages/ui/src/views/components/doc-bars/hooks/menu.ts b/packages/ui/src/views/components/doc-bars/hooks/menu.ts
deleted file mode 100644
index 92694a9c74..0000000000
--- a/packages/ui/src/views/components/doc-bars/hooks/menu.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * 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 { BehaviorSubject } from 'rxjs';
-
-import type { IDisplayMenuItem, IMenuItem } from '../../../../services/menu/menu';
-import { MenuPosition } from '../../../../services/menu/menu';
-
-export interface IMenuGroup {
- name: MenuPosition;
- menuItems: Array<IDisplayMenuItem<IMenuItem>>;
-}
-
-export const positions = [
- MenuPosition.TOOLBAR_START,
- MenuPosition.TOOLBAR_INSERT,
- MenuPosition.TOOLBAR_FORMULAS,
- MenuPosition.TOOLBAR_DATA,
- MenuPosition.TOOLBAR_VIEW,
- MenuPosition.TOOLBAR_OTHERS,
-];
-
-const positionSubject = new BehaviorSubject(MenuPosition.TOOLBAR_START);
-
-export const position$ = positionSubject.asObservable();
-
-export function setPosition(position: MenuPosition) {
- positionSubject.next(position);
-}