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.

chore(ui-plugin-sheets): mount canvas

Dushusir2fd8bb8ecd70
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsx
index ab52c20291..8de65d20d6 100644
--- a/examples/univer-sheet-ts/src/main.tsx
+++ b/examples/univer-sheet-ts/src/main.tsx
@@ -83,7 +83,11 @@ universheet.installPlugin(
// univer.install(new UniverComponentSheet());
// univer.install(new BaseComponentPlugin(univerConfig));
-univer.install(new SheetUIPlugin())
+univer.install(
+ new SheetUIPlugin({
+ container: 'universheet',
+ })
+);
// sheets-plugin-ui univer.install(new SheetsUI)
// const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO);
// const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO1);
diff --git a/packages/base-sheets/src/SheetPlugin.tsx b/packages/base-sheets/src/SheetPlugin.tsx
index 5a83b3a5fc..ccc6e78949 100644
--- a/packages/base-sheets/src/SheetPlugin.tsx
+++ b/packages/base-sheets/src/SheetPlugin.tsx
@@ -1,4 +1,4 @@
-import { Engine } from '@univerjs/base-render';
+import { Engine, RenderEngine } from '@univerjs/base-render';
import { SheetContext, Plugin, PLUGIN_NAMES, UIObserver } from '@univerjs/core';
import { SheetPluginObserve, uninstall } from './Basics/Observer';
@@ -64,25 +64,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
zh,
});
- // const engine = this.getPluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER)?.getEngine()!;
-
- // let container = this._component.getUniverContainerController().getContentRef().current!;
-
- // this._canvasEngine = engine;
-
- // engine.setContainer(container);
- // window.addEventListener('resize', () => {
- // engine.resize();
- // });
-
- // // should be clear
- // setTimeout(() => {
- // engine.resize();
- // }, 0);
-
- // if (this._canvasView == null) {
- // this._canvasView = new CanvasView(engine, this);
- // }
+ this.initCanvasView();
this.registerExtension();
}
@@ -91,6 +73,14 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
return this._config;
}
+ initCanvasView() {
+ const engine = this.getContext().getUniver().getGlobalContext().getPluginManager().getPluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER)?.getEngine()!;
+
+ if (this._canvasView == null) {
+ this._canvasView = new CanvasView(engine, this);
+ }
+ }
+
onMounted(ctx: SheetContext): void {
this.initialize(ctx);
}
diff --git a/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetsPluginConfig.ts b/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts
similarity index 92%
rename from packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetsPluginConfig.ts
rename to packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts
index e24a0fb7b9..7440e3fa15 100644
--- a/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetsPluginConfig.ts
+++ b/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts
@@ -9,7 +9,7 @@ export interface ILayout {
rightMenuConfig?: SheetRightMenuConfig;
}
-export interface ISheetsPluginConfig {
+export interface ISheetUIPluginConfig {
container?: HTMLElement | string;
layout?: ILayout;
context?: Context;
diff --git a/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/index.ts b/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/index.ts
index 77485d17a8..4b230ca858 100644
--- a/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/index.ts
+++ b/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/index.ts
@@ -1,3 +1,3 @@
export * from './ToolBarConfig';
export * from './RightMenuConfig';
-export * from './ISheetsPluginConfig';
+export * from './ISheetUIPluginConfig';
diff --git a/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts b/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts
index 915911a57c..f86875ad77 100644
--- a/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/CellEditorUIController.ts
@@ -1,4 +1,5 @@
-import { getRefElement, CellEditExtensionManager,KeyboardManager, $$ } from '@univerjs/base-ui';
+import { getRefElement, CellEditExtensionManager, KeyboardManager, $$, setLastCaretPosition, handleStringToStyle } from '@univerjs/base-ui';
+import { isKeyPrintable, Tools } from '@univerjs/core';
import { SheetUIPlugin } from '../SheetUIPlugin';
import { RichText } from '../View/RichText';
@@ -24,27 +25,31 @@ export class CellEditorUIController {
private _initialize() {
// this._initRegisterComponent();
// If other plugins are loaded asynchronously, they may be initialized after the rendering layer is loaded, and they will not receive obs listeners.
- this._plugin.context
- .getObserverManager()
- .getObserver('onSheetRenderDidMountObservable', 'core')
- ?.add((e) => {
- const main = this._plugin.getMainComponent();
-
- main.onDblclickObserver.add((evt: IPointerEvent | IMouseEvent) => {
- // Prevent left + right double click
- if (evt.button !== 2) {
- this.enterEditMode();
- }
- });
- main.onPointerDownObserver.add((evt: IPointerEvent | IMouseEvent) => {
- this.exitEditMode();
- evt.preventDefault();
- });
- });
+
+ // this._plugin.context
+ // .getObserverManager()
+ // .getObserver('onSheetRenderDidMountObservable', 'core')
+ // ?.add((e) => {
+ // const main = this._plugin.getMainComponent();
+
+ // main.onDblclickObserver.add((evt: IPointerEvent | IMouseEvent) => {
+ // // Prevent left + right double click
+ // if (evt.button !== 2) {
+ // this.enterEditMode();
+ // }
+ // });
+ // main.onPointerDownObserver.add((evt: IPointerEvent | IMouseEvent) => {
+ // this.exitEditMode();
+ // evt.preventDefault();
+ // });
+ // });
this._cellEditExtensionManager = new CellEditExtensionManager();
this._keyboardManager = new KeyboardManager(this._plugin);
+
+ console.log('cell edit--');
+
// this.richTextEditEle
}
@@ -53,7 +58,7 @@ export class CellEditorUIController {
this.richTextEditEle = $$('div', this.richTextEle);
// // focus
- // this.richTextEditEle.focus();
+ this.richTextEditEle.focus();
// this.focusEditEle();
this.hideEditContainer();
@@ -228,7 +233,7 @@ export class CellEditorUIController {
this.richTextEle.style.minHeight = `${endY - startY}px`;
this.richTextEle.style.borderWidth = '2px';
- const univerContainerContentRef = this._plugin.getSheetContainerUIController().getContentRef()
+ const univerContainerContentRef = this._plugin.getSheetContainerUIController().getContentRef();
const sheetContentRect = getRefElement(univerContainerContentRef).getBoundingClientRect();
this.richTextEle.style.maxWidth = `${sheetContentRect.width - startX + scrollX}px`;
@@ -311,7 +316,6 @@ export class CellEditorUIController {
// 获取Toolbar组件
getComponent = (ref: RichText) => {
this.richText = ref;
- this._initRichText()
-
+ this._initRichText();
};
}
diff --git a/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts b/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts
index 788ec0cd98..4dcfdb4d8e 100644
--- a/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts
@@ -58,12 +58,12 @@ export class RightMenuUIController {
private _initialize() {
setTimeout(() => {
- this._plugin
- .getSheetContainerController()
- .getContentRef()
- .current?.addEventListener('click', (e) => {
- this._rightMenu.handleContextMenu(e as any);
- });
+ // this._plugin
+ // .getSheetContainerController()
+ // .getContentRef()
+ // .current?.addEventListener('click', (e) => {
+ // this._rightMenu.handleContextMenu(e as any);
+ // });
}, 1000);
}
diff --git a/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts b/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
index efb4934567..4ce6c986fb 100644
--- a/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
@@ -1,7 +1,8 @@
import { LocaleType, UIObserver } from '@univerjs/core';
-import { ISheetsPluginConfig } from '../Basics';
+import { ISheetUIPluginConfig } from '../Basics';
import { SheetUIPlugin } from '../SheetUIPlugin';
import { SheetContainer, UI } from '../View';
+import { CellEditorUIController } from './CellEditorUIController';
import { RightMenuUIController } from './RightMenuUIController';
import { ToolBarUIController } from './ToolbarUIController';
@@ -12,7 +13,7 @@ export class SheetContainerUIController {
private _toolbarController: ToolBarUIController;
- // private _cellEditorUIController: CellEditorUIController;
+ private _cellEditorUIController: CellEditorUIController;
// private _formulaBarUIController: FormulaBarUIController;
@@ -24,7 +25,7 @@ export class SheetContainerUIController {
// private _sheetBarController: SheetBarControl;
- private _config: ISheetsPluginConfig;
+ private _config: ISheetUIPluginConfig;
constructor(plugin: SheetUIPlugin) {
this._plugin = plugin;
@@ -33,10 +34,8 @@ export class SheetContainerUIController {
this._initialize();
- console.log('sheet container init!');
-
this._toolbarController = new ToolBarUIController(this._plugin, this._config.layout?.toolBarConfig);
- // this._cellEditorUIController = new CellEditorUIController(this._plugin);
+ this._cellEditorUIController = new CellEditorUIController(this._plugin);
// this._formulaBarUIController = new FormulaBarUIController(this._plugin);
// this._infoBarController = new InfoBarController(this._plugin);
this._rightMenuController = new RightMenuUIController(this._plugin, this._config.layout?.rightMenuConfig);
@@ -54,7 +53,7 @@ export class SheetContainerUIController {
getComponent: this._toolbarController.getComponent,
},
cellEditor: {
- // getComponent: this._cellEditorUIController.getComponent,
+ getComponent: this._cellEditorUIController.getComponent,
},
formulaBar: {
// getComponent: this._formulaBarUIController.getComponent,
diff --git a/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx b/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx
index 57cbedcf8d..2ce5733f43 100644
--- a/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx
+++ b/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx
@@ -1,23 +1,26 @@
-import { Plugin, UniverSheet, Tools } from '@univerjs/core';
+import { Plugin, UniverSheet, Tools, PLUGIN_NAMES } from '@univerjs/core';
import { zh, en } from './Locale';
-import { DefaultSheetUiConfig, installObserver, ISheetsPluginConfig, SheetUIPluginObserve, SHEET_UI_PLUGIN_NAME } from './Basics';
+import { DefaultSheetUiConfig, installObserver, ISheetUIPluginConfig, SheetUIPluginObserve, SHEET_UI_PLUGIN_NAME } from './Basics';
import { Context } from '@univerjs/core/src/Basics/Context';
import { SheetContainerUIController } from './Controller';
-import { ComponentManager } from '@univerjs/base-ui';
+import { ComponentManager, getRefElement } from '@univerjs/base-ui';
+import { Engine, RenderEngine } from '@univerjs/base-render';
export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
private _sheetsController: SheetContainerUIController;
private _componentManager: ComponentManager;
- private _config: ISheetsPluginConfig;
+ private _config: ISheetUIPluginConfig;
- constructor(config?: ISheetsPluginConfig) {
+ private _canvasEngine: Engine;
+
+ constructor(config?: ISheetUIPluginConfig) {
super(SHEET_UI_PLUGIN_NAME);
this._config = Tools.deepMerge({}, DefaultSheetUiConfig, config);
}
- static create(config?: ISheetsPluginConfig) {
+ static create(config?: ISheetUIPluginConfig) {
return new SheetUIPlugin(config);
}
@@ -39,12 +42,33 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
this._componentManager = new ComponentManager();
this._sheetsController = new SheetContainerUIController(this);
+ this.initRender();
}
getConfig() {
return this._config;
}
+ initRender() {
+ const engine = this.getPluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER)?.getEngine()!;
+ let container = getRefElement(this.getSheetContainerController().getContentRef());
+ this._canvasEngine = engine;
+
+ // mount canvas to DOM container
+ engine.setContainer(container);
+
+ window.addEventListener('resize', () => {
+ engine.resize();
+ });
+
+ // should be clear
+ setTimeout(() => {
+ engine.resize();
+ }, 0);
+ }
+
+ initUI() {}
+
onMounted(ctx: Context): void {
this.initialize(ctx);
}
diff --git a/packages/ui-plugin-sheets/src/View/RichText/CellTextStyle.ts b/packages/ui-plugin-sheets/src/View/RichText/CellTextStyle.ts
index 488dbf3d24..5cde98fcfb 100644
--- a/packages/ui-plugin-sheets/src/View/RichText/CellTextStyle.ts
+++ b/packages/ui-plugin-sheets/src/View/RichText/CellTextStyle.ts
@@ -1,5 +1,5 @@
+import { $$, selectTextContent, selectTextContentCross, textTrim } from '@univerjs/base-ui';
import { Color } from '@univerjs/core';
-import { $$, selectTextContent, selectTextContentCross, textTrim } from '../../Utils';
import styles from './index.module.less';
interface IStyleSetting {
diff --git a/packages/ui-plugin-sheets/src/View/RichText/RichText.tsx b/packages/ui-plugin-sheets/src/View/RichText/RichText.tsx
index 815dc79759..7664eb2ac1 100644
--- a/packages/ui-plugin-sheets/src/View/RichText/RichText.tsx
+++ b/packages/ui-plugin-sheets/src/View/RichText/RichText.tsx
@@ -1,6 +1,5 @@
// import { CellInputHandler } from '@univerjs/sheets-plugin-formula/src/Controller/CellInputHandler';
-import { Component, createRef } from '@univerjs/base-ui'
-import { xssDeal } from '../../Utils';
+import { Component, createRef, xssDeal } from '@univerjs/base-ui';
import { CellTextStyle } from './CellTextStyle';
import styles from './index.module.less';
// interface IProps {
diff --git a/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx b/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx
index eb2cdc4add..5b05863e5c 100644
--- a/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx
+++ b/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx
@@ -1,6 +1,5 @@
import style from './index.module.less';
import { ToolBar } from '../ToolBar';
-import { ToolBar } from '../ToolBar';
import { InfoBar } from '../InfoBar';
import { RightMenu } from '../RightMenu';
import { CountBar } from '../CountBar';
@@ -8,10 +7,10 @@ import { SheetBar } from '../SheetBar';
import { FormulaBar } from '../FormulaBar';
import { RichText } from '../RichText';
import { AppContext, BaseComponentProps, Component, Container, Content, createRef, Footer, Header, Layout, Sider } from '@univerjs/base-ui';
-import { ISheetsPluginConfig } from '../../Basics';
+import { ISheetUIPluginConfig } from '../../Basics';
export interface BaseSheetContainerProps extends BaseComponentProps {
- config: ISheetsPluginConfig;
+ config: ISheetUIPluginConfig;
container?: HTMLElement;
changeLocale: (locale: string) => void;
methods?: any;
@@ -165,6 +164,8 @@ export class SheetContainer extends Component<BaseSheetContainerProps, IState> {
<Container ref={this.contentRef} className={style.contentInnerRightContainer}>
{/* {config.rightMenu && <RightMenu {...methods.rightMenu}></RightMenu>}
{config.cellEditor && <RichText {...methods.cellEditor}></RichText>} */}
+
+ {<RichText {...methods.cellEditor}></RichText>}
<div style={{ position: 'fixed', right: '200px', top: '10px', fontSize: '14px' }}>
<span style={{ display: 'inline-block', width: 50, margin: '5px 0 0 5px' }}>语言</span>
<select value={currentLocale} onChange={this.setLocale.bind(this)} style={{ width: 55 }}>