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): remove getRefElement (#126)

Dushusirce4fd123d29c
diff --git a/packages/base-ui/src/Utils/util.ts b/packages/base-ui/src/Utils/util.ts
index eb64efeb80..a61b298394 100644
--- a/packages/base-ui/src/Utils/util.ts
+++ b/packages/base-ui/src/Utils/util.ts
@@ -1,4 +1,4 @@
-import { Component, RefObject } from 'react';
+import { RefObject } from 'react';
/**
* check element
@@ -9,11 +9,6 @@ export function isElement(element: any) {
return element instanceof Element || element instanceof HTMLDocument;
}
-export const isDOM =
- typeof HTMLElement === 'object'
- ? (obj: any) => obj instanceof HTMLElement
- : (obj: any) => obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
-
/**
* DOM selector
* @param {String} selector css selector
@@ -198,19 +193,6 @@ export function textTrim(x: string): string {
return x.replace(/^\s+|\s+$/gm, '');
}
-export function getRefElement(ref: RefObject<HTMLElement | Component> | Component) {
- if (isDOM((ref as RefObject<HTMLElement>).current)) {
- return (ref as RefObject<HTMLElement>).current as HTMLElement;
- }
- const refCurrent = (ref as RefObject<Component>).current as Component<{}, {}>;
-
- // TODO: need to fix
- if (refCurrent) {
- return refCurrent?.contentRef.current as HTMLElement;
- }
- return (ref as Component).contentRef.current as HTMLElement;
-}
-
export function printableCharacter(keycode: number) {
const valid =
(keycode > 47 && keycode < 58) || // number keys
diff --git a/packages/sheets-plugin-formula/src/Controller/FormulaPromptController.ts b/packages/sheets-plugin-formula/src/Controller/FormulaPromptController.ts
index cafc6ff004..d9945043d0 100644
--- a/packages/sheets-plugin-formula/src/Controller/FormulaPromptController.ts
+++ b/packages/sheets-plugin-formula/src/Controller/FormulaPromptController.ts
@@ -1,7 +1,8 @@
-import { $$, ComponentManager, SlotManager, getRefElement } from '@univerjs/base-ui';
+import { $$, ComponentManager, SlotManager } from '@univerjs/base-ui';
import { ObserverManager } from '@univerjs/core';
import { SheetContainerUIController } from '@univerjs/ui-plugin-sheets';
import { Inject } from '@wendellhu/redi';
+
import { FORMULA_PLUGIN_NAME, FunList } from '../Basics';
import { HelpFunction, SearchFunction } from '../View/UI/FormulaPrompt';
import { CellInputHandler } from './CellInputHandler';
@@ -125,50 +126,7 @@ export class FormulaPromptController {
kcode === 46 ||
(event.ctrlKey && kcode === 86)
) {
- this.cellInputHandler.searchFunction(this.richTextEditEle);
- const formula = this.cellInputHandler.getFormula();
- const helpFormula = this.cellInputHandler.getHelpFormula();
- const height = parseInt(this.richTextEle.style.minHeight);
- const width = parseInt(this.richTextEle.style.minWidth);
- let left = parseInt(this.richTextEle.style.left);
- let top = parseInt(this.richTextEle.style.top) + height;
- const sheetContainer = getRefElement(this._sheetContainerUIController.getContentRef());
- const screenW = sheetContainer.offsetWidth;
- const screenH = sheetContainer.offsetHeight;
- const position = { left, top };
- const getPosition = (component: SearchFunction | HelpFunction) => {
- const searchFunctionEle = getRefElement(component.getContentRef());
- const rootW = searchFunctionEle.offsetWidth;
- const rootH = searchFunctionEle.offsetHeight;
- const right = screenW - left > rootW;
- const bottom = screenH - top > rootH;
- if (!right) {
- left -= rootW - width;
- }
- if (!bottom) {
- top -= rootH + height;
- }
- return { left, top };
- };
-
- if (formula[0]) {
- this._searchFunction.updateState(true, formula, 0, position, () => {
- const position = getPosition(this._searchFunction);
- this._searchFunction.updateState(true, formula, 0, position);
- });
- this._helpFunction.updateState(false);
- } else if (helpFormula[0]) {
- const functionName = (helpFormula[0] as string).toUpperCase();
- const functionInfo = FunList.find((item: any) => item.n === functionName) || {};
- this._helpFunction.updateState(true, helpFormula[1] as number, functionInfo, position, () => {
- const position = getPosition(this._helpFunction);
- this._helpFunction.updateState(true, helpFormula[1] as number, functionInfo, position);
- });
- this._searchFunction.updateState(false);
- } else {
- this._searchFunction.updateState(false, formula);
- this._helpFunction.updateState(false);
- }
+ this._update();
}
const value = this.cellInputHandler.getInputValue();
if (value.length > 0 && value.substr(0, 1) === '=' && (kcode !== 229 || value.length === 1)) {
@@ -184,4 +142,57 @@ export class FormulaPromptController {
}
});
}
+
+ private _update() {
+ this.cellInputHandler.searchFunction(this.richTextEditEle);
+ const formula = this.cellInputHandler.getFormula();
+ const helpFormula = this.cellInputHandler.getHelpFormula();
+ const height = parseInt(this.richTextEle.style.minHeight);
+ const width = parseInt(this.richTextEle.style.minWidth);
+ let left = parseInt(this.richTextEle.style.left);
+ let top = parseInt(this.richTextEle.style.top) + height;
+ const sheetContainer = this._sheetContainerUIController.getContentRef().current;
+
+ if (!sheetContainer) return;
+
+ const screenW = sheetContainer.offsetWidth;
+ const screenH = sheetContainer.offsetHeight;
+ const position = { left, top };
+ const getPosition = (component: SearchFunction | HelpFunction) => {
+ const searchFunctionEle = component.getContentRef().current;
+
+ if (!searchFunctionEle) return { left, top };
+
+ const rootW = searchFunctionEle.offsetWidth;
+ const rootH = searchFunctionEle.offsetHeight;
+ const right = screenW - left > rootW;
+ const bottom = screenH - top > rootH;
+ if (!right) {
+ left -= rootW - width;
+ }
+ if (!bottom) {
+ top -= rootH + height;
+ }
+ return { left, top };
+ };
+
+ if (formula[0]) {
+ this._searchFunction.updateState(true, formula, 0, position, () => {
+ const position = getPosition(this._searchFunction);
+ this._searchFunction.updateState(true, formula, 0, position);
+ });
+ this._helpFunction.updateState(false);
+ } else if (helpFormula[0]) {
+ const functionName = (helpFormula[0] as string).toUpperCase();
+ const functionInfo = FunList.find((item: any) => item.n === functionName) || {};
+ this._helpFunction.updateState(true, helpFormula[1] as number, functionInfo, position, () => {
+ const position = getPosition(this._helpFunction);
+ this._helpFunction.updateState(true, helpFormula[1] as number, functionInfo, position);
+ });
+ this._searchFunction.updateState(false);
+ } else {
+ this._searchFunction.updateState(false, formula);
+ this._helpFunction.updateState(false);
+ }
+ }
}
diff --git a/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts b/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts
index 37a183b29c..fef8627074 100644
--- a/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts
+++ b/packages/ui-plugin-docs/src/Controller/DocContainerUIController.ts
@@ -1,5 +1,4 @@
import { Engine, IRenderingEngine } from '@univerjs/base-render';
-import { getRefElement } from '@univerjs/base-ui';
import { LocaleService, LocaleType, ObserverManager } from '@univerjs/core';
import { Inject, Injector, SkipSelf } from '@wendellhu/redi';
@@ -32,7 +31,11 @@ export class DocContainerUIController {
getComponent = (ref: DocContainer) => {
this._docContainer = ref;
- const container = getRefElement(ref.getContentRef());
+ const container = ref.getContentRef().current;
+
+ if (!container) {
+ throw new Error('container is not ready');
+ }
const engine = this._renderingEngine;
engine.setContainer(container);
diff --git a/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts b/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts
index 1f47315955..35d4d8f1ef 100644
--- a/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts
@@ -1,6 +1,6 @@
import { Engine, IMouseEvent, IPointerEvent, IRenderingEngine } from '@univerjs/base-render';
import { CANVAS_VIEW_KEY, CanvasView, CellEditorController, ISelectionManager, SelectionManager } from '@univerjs/base-sheets';
-import { $$, CellEditExtensionManager, getRefElement, handleDomToJson, handleStringToStyle, isCtrlPressed, KeyboardManager, setLastCaretPosition } from '@univerjs/base-ui';
+import { $$, CellEditExtensionManager, handleDomToJson, handleStringToStyle, isCtrlPressed, KeyboardManager, setLastCaretPosition } from '@univerjs/base-ui';
import { Direction, handleStyleToString, ICellData, isKeyPrintable, ObserverManager, Tools, UIObserver } from '@univerjs/core';
import { Inject, SkipSelf } from '@wendellhu/redi';
import { RefObject } from 'react';
@@ -130,7 +130,9 @@ export class CellEditorUIController {
this._richTextEle.style.borderWidth = '2px';
const univerContainerContentRef = this._currentRefFetcher();
- const sheetContentRect = getRefElement(univerContainerContentRef).getBoundingClientRect();
+ const sheetContentRect = univerContainerContentRef.current?.getBoundingClientRect();
+
+ if (!sheetContentRect) return;
this._richTextEle.style.maxWidth = `${sheetContentRect.width - startX + scrollX}px`;
this._richTextEle.style.maxHeight = `${sheetContentRect.height - startY + scrollY}px`;
@@ -241,7 +243,9 @@ export class CellEditorUIController {
}
private setRichText() {
- this._richTextEle = getRefElement(this._richText.container);
+ const richText = this._richText.container.current;
+ if (!richText) return;
+ this._richTextEle = richText;
this._richTextEditEle = $$('div', this._richTextEle);
// // focus
diff --git a/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts b/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
index 586404d851..82315ba8bc 100644
--- a/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
@@ -1,4 +1,4 @@
-import { ComponentManager, DragManager, getRefElement, Prompt, SlotManager } from '@univerjs/base-ui';
+import { ComponentManager, DragManager, Prompt, SlotManager } from '@univerjs/base-ui';
import { LocaleService, LocaleType, ObserverManager } from '@univerjs/core';
import { Inject, Injector, Self, SkipSelf } from '@wendellhu/redi';
@@ -171,6 +171,10 @@ export class SheetContainerUIController {
private setSheetContainer() {
// handle drag event
- this._dragManager.handleDragAction(getRefElement(this._sheetContainer));
+ const sheetContainerEle = this._sheetContainer.getContentRef().current;
+ if (!sheetContainerEle) {
+ throw new Error('sheetContainerEle is not ready');
+ }
+ this._dragManager.handleDragAction(sheetContainerEle);
}
}
diff --git a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts b/packages/ui-plugin-sheets/src/SheetUIPlugin.ts
index d7ee4bec36..39f6860c4e 100644
--- a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts
+++ b/packages/ui-plugin-sheets/src/SheetUIPlugin.ts
@@ -5,7 +5,6 @@ import {
DesktopPlatformService,
DesktopShortcutService,
DragManager,
- getRefElement,
IMenuService,
IPlatformService,
IShortcutService,
@@ -70,7 +69,11 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve> {
initRender() {
const engine = this._injector.get(IRenderingEngine);
- const container = getRefElement(this._appUIController.getSheetContainerController().getContentRef());
+ const container = this._appUIController.getSheetContainerController().getContentRef().current;
+
+ if (!container) {
+ throw new Error('container is not ready');
+ }
// mount canvas to DOM container
engine.setContainer(container);
diff --git a/packages/ui-plugin-slides/src/Controller/SlideContainerUIController.ts b/packages/ui-plugin-slides/src/Controller/SlideContainerUIController.ts
index ddfe28422b..422645518a 100644
--- a/packages/ui-plugin-slides/src/Controller/SlideContainerUIController.ts
+++ b/packages/ui-plugin-slides/src/Controller/SlideContainerUIController.ts
@@ -1,6 +1,7 @@
-import { ComponentManager, DragManager, getRefElement } from '@univerjs/base-ui';
+import { ComponentManager, DragManager } from '@univerjs/base-ui';
import { LocaleService, LocaleType, ObserverManager } from '@univerjs/core';
import { Inject, Injector, Self, SkipSelf } from '@wendellhu/redi';
+
import { ISlideUIPluginConfig } from '../Basics';
import { SlideContainer } from '../View';
import { InfoBarUIController } from './InfoBarUIController';
@@ -98,6 +99,10 @@ export class SlideContainerUIController {
private setSlideContainer() {
// handle drag event
- this._dragManager.handleDragAction(getRefElement(this._slideContainer));
+ const slideContainer = this._slideContainer.getContentRef().current;
+ if (!slideContainer) {
+ throw new Error('slideContainer is not ready');
+ }
+ this._dragManager.handleDragAction(slideContainer);
}
}
diff --git a/packages/ui-plugin-slides/src/SlideUIPlugin.ts b/packages/ui-plugin-slides/src/SlideUIPlugin.ts
index 6d1ac170d4..67da88732a 100644
--- a/packages/ui-plugin-slides/src/SlideUIPlugin.ts
+++ b/packages/ui-plugin-slides/src/SlideUIPlugin.ts
@@ -1,14 +1,14 @@
-import { Plugin, Tools, LocaleService, PluginType } from '@univerjs/core';
-import { ComponentManager, getRefElement, DragManager } from '@univerjs/base-ui';
import { IRenderingEngine } from '@univerjs/base-render';
-import { Inject, Injector } from '@wendellhu/redi';
import { CanvasView } from '@univerjs/base-slides';
+import { ComponentManager, DragManager } from '@univerjs/base-ui';
+import { LocaleService, Plugin, PluginType, Tools } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
-import { zh, en } from './Locale';
-import { SLIDE_UI_PLUGIN_NAME } from './Basics/Const/PLUGIN_NAME';
-import { AppUIController } from './Controller/AppUIController';
import { DefaultSlideUIConfig, installObserver, ISlideUIPluginConfig, SlideUIPluginObserve } from './Basics';
+import { SLIDE_UI_PLUGIN_NAME } from './Basics/Const/PLUGIN_NAME';
import { IToolbarItemProps } from './Controller';
+import { AppUIController } from './Controller/AppUIController';
+import { en, zh } from './Locale';
export class SlideUIPlugin extends Plugin<SlideUIPluginObserve> {
static override type = PluginType.Slide;
@@ -55,7 +55,10 @@ export class SlideUIPlugin extends Plugin<SlideUIPluginObserve> {
initRender() {
const engine = this._injector.get(IRenderingEngine);
- const container = getRefElement(this._appUIController.getSlideContainerController().getContentRef());
+ const container = this._appUIController.getSlideContainerController().getContentRef().current;
+ if (!container) {
+ throw new Error('container is not ready');
+ }
// mount canvas to DOM container
engine.setContainer(container);