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(formula-ui): replace dropdown with popup (#576)

白熱d85aa282c97d
diff --git a/packages/base-ui/src/views/app.module.less b/packages/base-ui/src/views/app.module.less
index b349e02898..a02cc55ed7 100644
--- a/packages/base-ui/src/views/app.module.less
+++ b/packages/base-ui/src/views/app.module.less
@@ -35,6 +35,8 @@
}
&-header {
+ position: relative;
+ z-index: 10;
width: 100%;
}
@@ -51,4 +53,4 @@
&-footer {
// height: 36px;
}
-}
\ No newline at end of file
+}
diff --git a/packages/base-ui/src/views/components/context-menu/ContextMenu.tsx b/packages/base-ui/src/views/components/context-menu/ContextMenu.tsx
index be44a513cc..248a850fcd 100644
--- a/packages/base-ui/src/views/components/context-menu/ContextMenu.tsx
+++ b/packages/base-ui/src/views/components/context-menu/ContextMenu.tsx
@@ -23,9 +23,11 @@ export function ContextMenu() {
});
document.addEventListener('pointerdown', handleClose);
+ document.addEventListener('wheel', handleClose);
return () => {
document.removeEventListener('pointerdown', handleClose);
+ document.removeEventListener('wheel', handleClose);
disposables.dispose();
};
}, []);
diff --git a/packages/design/src/components/popup/Popup.stories.tsx b/packages/design/src/components/popup/Popup.stories.tsx
index 6031592c6c..84fad2e35f 100644
--- a/packages/design/src/components/popup/Popup.stories.tsx
+++ b/packages/design/src/components/popup/Popup.stories.tsx
@@ -1,5 +1,5 @@
import type { Meta } from '@storybook/react';
-import React from 'react';
+import React, { useState } from 'react';
import { Button } from '../button/Button';
import { Popup } from './Popup';
@@ -17,10 +17,18 @@ export default meta;
export const PopupBasic = {
render() {
+ const [visible, setVisible] = useState(false);
+ const [offset, setOffset] = useState<[number, number]>([0, 0]);
+
+ function handleClick() {
+ setVisible(!visible);
+ setOffset([10, 10]);
+ }
+
return (
<section>
- <Button>Click me</Button>
- <Popup>
+ <Button onClick={handleClick}>Click me</Button>
+ <Popup visible={visible} offset={offset}>
<span>xxxx</span>
</Popup>
</section>
diff --git a/packages/design/src/components/popup/Popup.tsx b/packages/design/src/components/popup/Popup.tsx
index 45c1c17e9b..cd82863713 100644
--- a/packages/design/src/components/popup/Popup.tsx
+++ b/packages/design/src/components/popup/Popup.tsx
@@ -27,6 +27,11 @@ export function Popup(props: IPopupProps) {
const [realoffset, setRealOffset] = useState<[number, number]>(offset);
useEffect(() => {
+ if (!visible) {
+ setRealOffset([-9999, -9999]);
+ return;
+ }
+
// edge avoidance
const [left, top] = offset;
const { clientWidth, clientHeight } = nodeRef.current!;
@@ -37,6 +42,10 @@ export function Popup(props: IPopupProps) {
setRealOffset([x, y]);
}, [offset, visible]);
+ function preventDefault(event: React.MouseEvent) {
+ event.preventDefault();
+ }
+
return (
<CSSTransition
in={visible}
@@ -57,6 +66,7 @@ export function Popup(props: IPopupProps) {
left: realoffset[0],
top: realoffset[1],
}}
+ onContextMenu={preventDefault}
>
{children}
</section>
diff --git a/packages/design/src/components/popup/index.module.less b/packages/design/src/components/popup/index.module.less
index dd7f906084..3a0c31bd4e 100644
--- a/packages/design/src/components/popup/index.module.less
+++ b/packages/design/src/components/popup/index.module.less
@@ -1,5 +1,5 @@
.popup {
- position: absolute;
+ position: fixed;
z-index: 1070;
top: -9999px;
left: -9999px;
diff --git a/packages/design/src/components/tooltip/index.module.less b/packages/design/src/components/tooltip/index.module.less
index bda8cd6acf..2427f674b8 100644
--- a/packages/design/src/components/tooltip/index.module.less
+++ b/packages/design/src/components/tooltip/index.module.less
@@ -4,7 +4,7 @@
.tooltip {
position: absolute;
- z-index: 4;
+ z-index: 10;
display: block;
diff --git a/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/HelpFunction.tsx b/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/HelpFunction.tsx
index 0f3b616418..68ad6a7eec 100644
--- a/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/HelpFunction.tsx
+++ b/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/HelpFunction.tsx
@@ -1,6 +1,6 @@
import type { IFunctionInfo, IFunctionParam } from '@univerjs/base-formula-engine';
import { LocaleService } from '@univerjs/core';
-import { Dropdown } from '@univerjs/design';
+import { Popup } from '@univerjs/design';
import { CloseSingle, DetailsSingle, MoreSingle } from '@univerjs/icons';
import { ICellEditorManagerService } from '@univerjs/ui-plugin-sheets';
import { useDependency } from '@wendellhu/redi/react-bindings';
@@ -15,7 +15,7 @@ export function HelpFunction() {
const [contentVisible, setContentVisible] = useState(true);
const [helpVisible, setHelpVisible] = useState(true);
const [paramIndex, setParamIndex] = useState(0);
- const [offset, setOffset] = useState<number[]>([0, 0]);
+ const [offset, setOffset] = useState<[number, number]>([0, 0]);
const [decoratorPosition, setDecoratorPosition] = useState({ left: 0, top: 0 });
const [functionInfo, setFunctionInfo] = useState<IFunctionInfo | null>(null);
const promptService = useDependency(IFormulaPromptService);
@@ -26,6 +26,8 @@ export function HelpFunction() {
useEffect(() => {
const subscription = promptService.help$.subscribe((params: IHelpFunctionOperationParams) => {
+ const rect = cellEditorManagerService.getRect();
+ if (!rect) return;
const selection = cellEditorManagerService.getState();
if (!selection) return;
@@ -48,8 +50,9 @@ export function HelpFunction() {
repeat: item.repeat,
})),
};
- const { startX = 0, startY = 0, endY = 0 } = selection;
- setOffset([startX, endY]);
+ const { left, top, height } = rect;
+ const { startX = 0, startY = 0 } = selection;
+ setOffset([left, top + height]);
setParamIndex(paramIndex);
setFunctionInfo(localeInfo);
setDecoratorPosition({ left: startX, top: startY });
@@ -68,71 +71,65 @@ export function HelpFunction() {
return (
<>
{helpVisible ? (
- <Dropdown
- visible={visible}
- align={{ offset }}
- overlay={
- functionInfo ? (
- <div className={styles.formulaHelpFunction}>
- <div className={styles.formulaHelpFunctionTitle}>
- <Help
- prefix={functionInfo.functionName}
- value={functionInfo.functionParameter}
- active={paramIndex}
- onClick={handleSwitchActive}
- />
- <div className={styles.formulaHelpFunctionTitleIcons}>
- <div
- className={styles.formulaHelpFunctionTitleIcon}
- style={{ transform: contentVisible ? 'rotateZ(-90deg)' : 'rotateZ(90deg)' }}
- onClick={() => setContentVisible(!contentVisible)}
- >
- <MoreSingle />
- </div>
- <div
- className={styles.formulaHelpFunctionTitleIcon}
- onClick={() => setHelpVisible(!helpVisible)}
- >
- <CloseSingle />
- </div>
+ <Popup visible={visible} offset={offset}>
+ {functionInfo ? (
+ <div className={styles.formulaHelpFunction}>
+ <div className={styles.formulaHelpFunctionTitle}>
+ <Help
+ prefix={functionInfo.functionName}
+ value={functionInfo.functionParameter}
+ active={paramIndex}
+ onClick={handleSwitchActive}
+ />
+ <div className={styles.formulaHelpFunctionTitleIcons}>
+ <div
+ className={styles.formulaHelpFunctionTitleIcon}
+ style={{ transform: contentVisible ? 'rotateZ(-90deg)' : 'rotateZ(90deg)' }}
+ onClick={() => setContentVisible(!contentVisible)}
+ >
+ <MoreSingle />
+ </div>
+ <div
+ className={styles.formulaHelpFunctionTitleIcon}
+ onClick={() => setHelpVisible(!helpVisible)}
+ >
+ <CloseSingle />
</div>
</div>
+ </div>
- <div
- className={styles.formulaHelpFunctionContent}
- style={{ height: contentVisible ? 'unset' : 0 }}
- >
- <div className={styles.formulaHelpFunctionContentInner}>
- <Params
- title={localeService.t('formula.prompt.helpExample')}
- value={`${functionInfo.functionName}(${functionInfo.functionParameter
- .map((item) => item.example)
- .join(',')})`}
- />
- <Params
- title={localeService.t('formula.prompt.helpAbstract')}
- value={functionInfo.description}
- />
- {functionInfo &&
- functionInfo.functionParameter &&
- functionInfo.functionParameter.map((item: IFunctionParam, i: number) => (
- <Params
- key={i}
- className={paramIndex === i ? styles.formulaHelpFunctionActive : ''}
- title={item.name}
- value={`${item.require ? required : optional} ${item.detail}`}
- />
- ))}
- </div>
+ <div
+ className={styles.formulaHelpFunctionContent}
+ style={{ height: contentVisible ? 'unset' : 0 }}
+ >
+ <div className={styles.formulaHelpFunctionContentInner}>
+ <Params
+ title={localeService.t('formula.prompt.helpExample')}
+ value={`${functionInfo.functionName}(${functionInfo.functionParameter
+ .map((item) => item.example)
+ .join(',')})`}
+ />
+ <Params
+ title={localeService.t('formula.prompt.helpAbstract')}
+ value={functionInfo.description}
+ />
+ {functionInfo &&
+ functionInfo.functionParameter &&
+ functionInfo.functionParameter.map((item: IFunctionParam, i: number) => (
+ <Params
+ key={i}
+ className={paramIndex === i ? styles.formulaHelpFunctionActive : ''}
+ title={item.name}
+ value={`${item.require ? required : optional} ${item.detail}`}
+ />
+ ))}
</div>
</div>
- ) : (
- <></>
- )
- }
- >
- <span />
- </Dropdown>
+ </div>
+ ) : (
+ <></>
+ )}
+ </Popup>
) : visible ? (
<div
className={styles.formulaHelpDecorator}
diff --git a/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/index.module.less b/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/index.module.less
index 9cef14ebfb..7a335c8164 100644
--- a/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/index.module.less
+++ b/packages/sheets-plugin-formula-ui/src/views/prompt/help-function/index.module.less
@@ -7,7 +7,6 @@
box-sizing: border-box;
width: @width;
margin: 0;
- padding: var(--padding-xl);
line-height: 20px;
list-style: none;
@@ -23,10 +22,15 @@
align-items: center;
justify-content: space-between;
+ box-sizing: border-box;
+ padding: var(--padding-base) var(--padding-xl);
+
font-size: var(--font-size-xs);
font-weight: 500;
color: rgb(var(--text-color));
+ border-bottom: 1px solid rgb(var(--border-color));
+
&-icons {
display: flex;
}
@@ -38,10 +42,11 @@
align-items: center;
justify-content: center;
+ box-sizing: border-box;
width: fit-content;
height: fit-content;
margin-left: var(--margin-sm);
- padding: var(--padding-xs);
+ padding: var(--padding-base) var(--padding-xs);
color: rgb(var(--text-color-secondary));
@@ -59,12 +64,12 @@
&-content {
overflow-y: auto;
+ box-sizing: border-box;
max-height: @max-height;
+ padding: 0 var(--padding-xl) var(--padding-xl);
&-inner {
margin-top: var(--padding-xl);
- padding: var(--padding-base) 0;
- border-top: 1px solid rgb(var(--border-color));
}
&-params {
diff --git a/packages/sheets-plugin-formula-ui/src/views/prompt/search-function/SearchFunction.tsx b/packages/sheets-plugin-formula-ui/src/views/prompt/search-function/SearchFunction.tsx
index 8a5b091495..6f2c4a4780 100644
--- a/packages/sheets-plugin-formula-ui/src/views/prompt/search-function/SearchFunction.tsx
+++ b/packages/sheets-plugin-formula-ui/src/views/prompt/search-function/SearchFunction.tsx
@@ -1,5 +1,5 @@
import { Direction } from '@univerjs/core';
-import { Dropdown } from '@univerjs/design';
+import { Popup } from '@univerjs/design';
import { ICellEditorManagerService } from '@univerjs/ui-plugin-sheets';
import { useDependency } from '@wendellhu/redi/react-bindings';
import React, { useEffect, useState } from 'react';
@@ -12,7 +12,7 @@ import styles from './index.module.less';
export function SearchFunction() {
const [visible, setVisible] = useState(false);
const [active, setActive] = useState(0);
- const [offset, setOffset] = useState<number[]>([0, 0]);
+ const [offset, setOffset] = useState<[number, number]>([0, 0]);
const [searchList, setSearchList] = useState<ISearchItem[]>([]);
const [searchText, setSearchText] = useState<string>('');
const promptService = useDependency(IFormulaPromptService);
@@ -23,8 +23,8 @@ export function SearchFunction() {
let updatedSearchList: ISearchItem[] = [];
let updatedActive = 0;
const subscribeSearch = promptService.search$.subscribe((params: ISearchFunctionOperationParams) => {
- const selection = cellEditorManagerService.getState();
- if (!selection) return;
+ const rect = cellEditorManagerService.getRect();
+ if (!rect) return;
const { visible, searchText, searchList } = params;
if (!visible) {
@@ -32,12 +32,12 @@ export function SearchFunction() {
return;
}
- const { startX = 0, endY = 0 } = selection;
+ const { left, top, height } = rect;
setSearchText(searchText);
setSearchList(searchList);
updatedSearchList = searchList;
- setOffset([startX, endY]);
+ setOffset([left, top + height]);
setVisible(visible);
setActive(0); // Reset active state
});
@@ -78,34 +78,28 @@ export function SearchFunction() {
}
return (
- <Dropdown
- visible={visible}
- align={{ offset }}
- overlay={
- <ul className={styles.formulaSearchFunction}>
- {searchList.map((item, index) => (
- <li
- key={index}
- className={
- active === index
- ? `${styles.formulaSearchFunctionItem} ${styles.formulaSearchFunctionItemActive}`
- : styles.formulaSearchFunctionItem
- }
- onMouseEnter={() => handleLiMouseEnter(index)}
- onMouseLeave={handleLiMouseLeave}
- onClick={() => promptService.acceptFormulaName(item.name)}
- >
- <span className={styles.formulaSearchFunctionItemName}>
- <span className={styles.formulaSearchFunctionItemNameLight}>{searchText}</span>
- <span>{item.name.slice(searchText.length)}</span>
- </span>
- <span className={styles.formulaSearchFunctionItemDesc}>{item.desc}</span>
- </li>
- ))}
- </ul>
- }
- >
- <span />
- </Dropdown>
+ <Popup visible={visible} offset={offset}>
+ <ul className={styles.formulaSearchFunction}>
+ {searchList.map((item, index) => (
+ <li
+ key={index}
+ className={
+ active === index
+ ? `${styles.formulaSearchFunctionItem} ${styles.formulaSearchFunctionItemActive}`
+ : styles.formulaSearchFunctionItem
+ }
+ onMouseEnter={() => handleLiMouseEnter(index)}
+ onMouseLeave={handleLiMouseLeave}
+ onClick={() => promptService.acceptFormulaName(item.name)}
+ >
+ <span className={styles.formulaSearchFunctionItemName}>
+ <span className={styles.formulaSearchFunctionItemNameLight}>{searchText}</span>
+ <span>{item.name.slice(searchText.length)}</span>
+ </span>
+ <span className={styles.formulaSearchFunctionItemDesc}>{item.desc}</span>
+ </li>
+ ))}
+ </ul>
+ </Popup>
);
}
diff --git a/packages/ui-plugin-sheets/src/controllers/header-menu.controller.ts b/packages/ui-plugin-sheets/src/controllers/header-menu.controller.ts
index 267797e3ee..4894923f60 100644
--- a/packages/ui-plugin-sheets/src/controllers/header-menu.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/header-menu.controller.ts
@@ -242,6 +242,7 @@ export class HeaderMenuController extends Disposable {
sheetObject.spreadsheetColumnHeader.onPointerDownObserver.notifyObservers(evt);
} else {
evt.stopPropagation();
+ evt.preventDefault();
this._contextMenuService.triggerContextMenu(evt, SheetMenuPosition.COL_HEADER_CONTEXT_MENU);
}
});
diff --git a/packages/ui-plugin-sheets/src/services/editor/cell-editor-manager.service.ts b/packages/ui-plugin-sheets/src/services/editor/cell-editor-manager.service.ts
index ffa3ccc7b1..9fab014f6f 100644
--- a/packages/ui-plugin-sheets/src/services/editor/cell-editor-manager.service.ts
+++ b/packages/ui-plugin-sheets/src/services/editor/cell-editor-manager.service.ts
@@ -6,24 +6,40 @@ export interface ICellEditorManagerParam extends Partial<IPosition> {
show: boolean;
}
+export interface ICellEditorBoundingClientRect {
+ left: number;
+ top: number;
+ width: number;
+ height: number;
+}
+
export interface ICellEditorManagerService {
state$: Observable<Nullable<ICellEditorManagerParam>>;
+ rect$: Observable<Nullable<ICellEditorBoundingClientRect>>;
focus$: Observable<boolean>;
dispose(): void;
setState(param: ICellEditorManagerParam): void;
getState(): Readonly<Nullable<ICellEditorManagerParam>>;
+ setRect(param: ICellEditorBoundingClientRect): void;
+ getRect(): Readonly<Nullable<ICellEditorBoundingClientRect>>;
setFocus(param: boolean): void;
}
export class CellEditorManagerService implements ICellEditorManagerService, IDisposable {
private _state: Nullable<ICellEditorManagerParam> = null;
+ private _rect: Nullable<ICellEditorBoundingClientRect> = null;
+
private _focus: boolean = false;
private readonly _state$ = new BehaviorSubject<Nullable<ICellEditorManagerParam>>(null);
readonly state$ = this._state$.asObservable();
+ private readonly _rect$ = new BehaviorSubject<Nullable<ICellEditorBoundingClientRect>>(null);
+
+ readonly rect$ = this._rect$.asObservable();
+
private readonly _focus$ = new BehaviorSubject<boolean>(this._focus);
readonly focus$ = this._focus$.asObservable();
@@ -31,6 +47,8 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDis
dispose(): void {
this._state$.complete();
this._state = null;
+ this._rect$.complete();
+ this._rect = null;
}
setState(param: ICellEditorManagerParam) {
@@ -39,6 +57,15 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDis
this._refresh(param);
}
+ getRect(): Readonly<Nullable<ICellEditorBoundingClientRect>> {
+ return this._rect;
+ }
+
+ setRect(param: ICellEditorBoundingClientRect) {
+ this._rect = param;
+ this._rect$.next(param);
+ }
+
getState(): Readonly<Nullable<ICellEditorManagerParam>> {
return this._state;
}
diff --git a/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx b/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx
index 036f552984..35c87005f8 100644
--- a/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx
+++ b/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx
@@ -71,6 +71,13 @@ export const EditorContainer: React.FC<ICellIEditorProps> = () => {
left: startX - 1.5,
top: startY - 1.5,
});
+
+ // update editor's rect
+ const editor = editorRef.current;
+ if (!editor) return;
+
+ const { left, top, width, height } = editor.getBoundingClientRect();
+ cellEditorManagerService.setRect({ left, top, width, height });
}
});
diff --git a/packages/ui-plugin-sheets/src/views/operate-container/AutoFillPopupMenu.tsx b/packages/ui-plugin-sheets/src/views/operate-container/AutoFillPopupMenu.tsx
index 8ce7fef318..c77c7c837d 100644
--- a/packages/ui-plugin-sheets/src/views/operate-container/AutoFillPopupMenu.tsx
+++ b/packages/ui-plugin-sheets/src/views/operate-container/AutoFillPopupMenu.tsx
@@ -109,6 +109,18 @@ export const AutoFillPopupMenu: React.FC<{}> = () => {
return disposable.dispose;
}, [autoFillService]);
+ useEffect(() => {
+ function handleClose() {
+ setVisible(false);
+ }
+
+ document.addEventListener('wheel', handleClose);
+
+ return () => {
+ document.removeEventListener('wheel', handleClose);
+ };
+ }, [visible]);
+
if (anchor.col < 0 || anchor.row < 0) {
return null;
}
@@ -154,10 +166,8 @@ export const AutoFillPopupMenu: React.FC<{}> = () => {
className={styles.autoFillPopupMenuItem}
>
<span className={styles.autoFillPopupMenuItemIcon}>
- {item.value === selected ? (
+ {item.value === selected && (
<CheckMarkSingle style={{ color: 'rgb(var(--green-700, #409f11))' }} />
- ) : (
- ''
)}
</span>
<span className={styles.autoFillPopupMenuItemTitle}>{localeService.t(item.label)}</span>
diff --git a/packages/ui-plugin-sheets/src/views/operate-container/index.module.less b/packages/ui-plugin-sheets/src/views/operate-container/index.module.less
index 2d0cec28cd..121bc0bac1 100644
--- a/packages/ui-plugin-sheets/src/views/operate-container/index.module.less
+++ b/packages/ui-plugin-sheets/src/views/operate-container/index.module.less
@@ -1,6 +1,6 @@
.operate-container {
position: absolute;
- z-index: 10;
+ z-index: 1;
right: 0;
left: 0;