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.

fix(docs-ui): inline format float box postion issue (#4857)

Ran Luof1cbe91b7344
diff --git a/packages/docs-ui/src/controllers/float-menu.controller.ts b/packages/docs-ui/src/controllers/float-menu.controller.ts
index 0511dcf96c..277d20d2dc 100644
--- a/packages/docs-ui/src/controllers/float-menu.controller.ts
+++ b/packages/docs-ui/src/controllers/float-menu.controller.ts
@@ -15,7 +15,8 @@
*/
import type { DocumentDataModel, IDisposable, ITextRangeParam, Nullable } from '@univerjs/core';
-import { Disposable, Inject, isInternalEditorID, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';
+import type { INodePosition, ITextRangeWithStyle } from '@univerjs/engine-render';
+import { deepCompare, Disposable, Inject, isInternalEditorID, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
import { ComponentManager } from '@univerjs/ui';
import { FloatToolbar } from '../components/float-toolbar/FloatToolbar';
@@ -23,6 +24,16 @@ import { DocCanvasPopManagerService } from '../services/doc-popup-manager.servic
const FLOAT_MENU_COMPONENT_KEY = 'univer.doc.float-menu';
+function isInSameLine(startNodePosition: Nullable<INodePosition>, endNodePosition: Nullable<INodePosition>) {
+ if (startNodePosition == null || endNodePosition == null) {
+ return false;
+ }
+ const { glyph: _startGlyph, ...startRest } = startNodePosition;
+ const { glyph: _endGlyph, ...endRest } = endNodePosition;
+
+ return deepCompare(startRest, endRest);
+}
+
export class FloatMenuController extends Disposable {
private _floatMenu: Nullable<IDisposable> = null;
@@ -44,40 +55,39 @@ export class FloatMenuController extends Disposable {
private _initSelectionChange() {
this.disposeWithMe(this._docSelectionManagerService.textSelection$.subscribe((selection) => {
const { unitId, textRanges } = selection;
+ this._hideFloatMenu();
+
if (isInternalEditorID(unitId)) {
- this.hideFloatMenu();
return;
}
if ((textRanges.length > 0) && textRanges.some((range) => !range.collapsed)) {
- this.hideFloatMenu();
- this.showFloatMenu(unitId, textRanges.find((range) => !range.collapsed)!);
- } else {
- this.hideFloatMenu();
+ this._showFloatMenu(unitId, textRanges.find((range) => !range.collapsed)!);
}
}));
}
- hideFloatMenu() {
+ private _hideFloatMenu() {
this._floatMenu?.dispose();
this._floatMenu = null;
}
- showFloatMenu(unitId: string, range: ITextRangeParam) {
+ private _showFloatMenu(unitId: string, range: ITextRangeParam) {
const documentDataModel = this._univerInstanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);
if (!documentDataModel || documentDataModel.getDisabled()) {
return;
}
+
this._floatMenu = this._docCanvasPopManagerService.attachPopupToRange(
range,
{
componentKey: FLOAT_MENU_COMPONENT_KEY,
- direction: range.direction === 'backward' ? 'top-center' : 'bottom-center',
+ direction: range.direction === 'backward' || isInSameLine((range as ITextRangeWithStyle).startNodePosition, (range as ITextRangeWithStyle).endNodePosition) ? 'top-center' : 'bottom-center',
offset: [0, 4],
},
unitId
);
- return toDisposable(() => this.hideFloatMenu());
+ return toDisposable(() => this._hideFloatMenu());
}
}
diff --git a/packages/docs-ui/src/services/doc-popup-manager.service.ts b/packages/docs-ui/src/services/doc-popup-manager.service.ts
index d551dc6daf..3769906206 100644
--- a/packages/docs-ui/src/services/doc-popup-manager.service.ts
+++ b/packages/docs-ui/src/services/doc-popup-manager.service.ts
@@ -288,8 +288,10 @@ export class DocCanvasPopManagerService extends Disposable {
anchorRect$: position$,
excludeRects: bounds,
excludeRects$: bounds$,
- direction: (['top', 'bottom', 'horizontal'].some((i) => direction.includes(i))) ?
- bounds.length > 1 ? multipleDirection ?? direction : direction
+ direction: (['top', 'bottom', 'horizontal'].some((i) => direction.includes(i)))
+ ? bounds.length > 1
+ ? multipleDirection ?? direction
+ : direction
: 'bottom',
canvasElement: currentRender.engine.getCanvasElement(),
});
diff --git a/packages/ui/src/services/popup/canvas-popup.service.ts b/packages/ui/src/services/popup/canvas-popup.service.ts
index 9b20ccd758..9e049ee5b7 100644
--- a/packages/ui/src/services/popup/canvas-popup.service.ts
+++ b/packages/ui/src/services/popup/canvas-popup.service.ts
@@ -93,6 +93,7 @@ export class CanvasPopupService extends Disposable implements ICanvasPopupServic
},
});
this._update();
+
return id;
}
diff --git a/packages/ui/src/views/components/popup/CanvasPopup.tsx b/packages/ui/src/views/components/popup/CanvasPopup.tsx
index 00262ad0cc..2a96d027e9 100644
--- a/packages/ui/src/views/components/popup/CanvasPopup.tsx
+++ b/packages/ui/src/views/components/popup/CanvasPopup.tsx
@@ -74,6 +74,7 @@ const SingleCanvasPopup = ({ popup, children }: ISingleCanvasPopupProps) => {
return () => anchorRectSub.unsubscribe();
}, [canvasElement, hideOnInvisible, anchorRect$, hiddenRects$]);
+
if ((hidden && hiddenType === 'destroy')) {
return null;
}
@@ -104,6 +105,7 @@ export function CanvasPopup() {
return popups.map((item) => {
const [key, popup] = item;
const Component = componentManager.get(popup.componentKey);
+
return (
<SingleCanvasPopup
key={key}
diff --git a/packages/ui/src/views/components/popup/RectPopup.tsx b/packages/ui/src/views/components/popup/RectPopup.tsx
index cfdf66a7d2..4888924c6a 100644
--- a/packages/ui/src/views/components/popup/RectPopup.tsx
+++ b/packages/ui/src/views/components/popup/RectPopup.tsx
@@ -121,29 +121,50 @@ function RectPopup(props: IRectPopupProps) {
const excludeRectsRef = excludeRects;
const anchorRectRef = useRef<IAbsolutePosition | undefined>(undefined);
+ function updatePosition(position: IAbsolutePosition) {
+ requestAnimationFrame(() => {
+ if (!nodeRef.current) return;
+
+ const { clientWidth, clientHeight } = nodeRef.current;
+ const innerWidth = window.innerWidth;
+ const innerHeight = window.innerHeight;
+
+ positionRef.current = calcPopupPosition(
+ {
+ position,
+ width: clientWidth,
+ height: clientHeight,
+ containerWidth: innerWidth,
+ containerHeight: innerHeight,
+ direction,
+ }
+ );
+
+ nodeRef.current.style.top = `${positionRef.current.top}px`;
+ nodeRef.current.style.left = `${positionRef.current.left}px`;
+ });
+ }
+
+ useEffect(() => {
+ let observer: ResizeObserver | null;
+ if (nodeRef.current) {
+ observer = new ResizeObserver(() => {
+ if (!anchorRectRef.current) return;
+ updatePosition(anchorRectRef.current);
+ });
+
+ observer.observe(nodeRef.current);
+ }
+
+ return () => {
+ observer?.disconnect();
+ };
+ }, [nodeRef.current]);
+
useEffect(() => {
const anchorRectSub = anchorRect$.subscribe((anchorRect) => {
anchorRectRef.current = anchorRect;
- requestAnimationFrame(() => {
- if (!nodeRef.current) return;
-
- const { clientWidth, clientHeight } = nodeRef.current;
- const innerWidth = window.innerWidth;
- const innerHeight = window.innerHeight;
-
- positionRef.current = calcPopupPosition(
- {
- position: anchorRect,
- width: clientWidth,
- height: clientHeight,
- containerWidth: innerWidth,
- containerHeight: innerHeight,
- direction,
- }
- );
- nodeRef.current.style.top = `${positionRef.current.top}px`;
- nodeRef.current.style.left = `${positionRef.current.left}px`;
- });
+ updatePosition(anchorRect);
});
return () => anchorRectSub.unsubscribe();