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(base-sheets): 1. add CellInputExtension for formula bar cell input box 2. rename CellExtension to CellEdit, compare to CellInput

Dushusirfa2746058708
diff --git a/packages/base-sheets/src/Basics/Interfaces/Cell.ts b/packages/base-sheets/src/Basics/Interfaces/Cell.ts
new file mode 100644
index 0000000000..84c2a1f916
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Interfaces/Cell.ts
@@ -0,0 +1,5 @@
+export interface ICell {
+ row: number;
+ column: number;
+ value: string;
+}
diff --git a/packages/base-sheets/src/Basics/Interfaces/index.ts b/packages/base-sheets/src/Basics/Interfaces/index.ts
index 33d2981547..70fdc652fd 100644
--- a/packages/base-sheets/src/Basics/Interfaces/index.ts
+++ b/packages/base-sheets/src/Basics/Interfaces/index.ts
@@ -1,4 +1,5 @@
// @index('./*.ts', f => `export * from '${f.path}'`)
+export * from './Cell';
export * from './SelectionConfig';
export * from './SpreadsheetConfig';
// @endindex
diff --git a/packages/base-sheets/src/Basics/Observer.ts b/packages/base-sheets/src/Basics/Observer.ts
index 6ca64648e4..e2c9d28d85 100644
--- a/packages/base-sheets/src/Basics/Observer.ts
+++ b/packages/base-sheets/src/Basics/Observer.ts
@@ -1,10 +1,12 @@
import { IKeyboardEvent } from '@univer/base-render';
import { Observable, Plugin } from '@univer/core';
+import { FormulaBar } from '../View/UI/FormulaBar/FormulaBar';
import { SelectionControl } from '../Controller/Selection/SelectionController';
import { SheetContainer } from '../View/UI/SheetContainer';
import { ToolBar } from '../View/UI/ToolBar/ToolBar';
import { LineColor } from '../View/UI/Common/Line/LineColor';
import { ModalGroup } from '../View/UI/ModalGroup/ModalGroup';
+import { RichText } from '../View/UI/RichText/RichText';
interface ISelectionControlFillConfig {}
@@ -40,6 +42,10 @@ export type SheetPluginObserve = {
onModalGroupDidMountObservable: Observable<ModalGroup>;
+ onRichTextDidMountObservable: Observable<RichText>;
+
+ onFormulaBarDidMountObservable: Observable<FormulaBar>;
+
onLineColorDidMountObservable: Observable<LineColor>;
onSheetContainerDidMountObservable: Observable<SheetContainer>;
@@ -74,6 +80,8 @@ export function uninstall(plugin: Plugin) {
plugin.deleteObserve('onToolBarDidMountObservable');
plugin.deleteObserve('onModalGroupDidMountObservable');
+ plugin.deleteObserve('onRichTextDidMountObservable');
+ plugin.deleteObserve('onFormulaBarDidMountObservable');
plugin.deleteObserve('onChangeCurrentSheetObserver');
plugin.deleteObserve('onSheetContainerDidMountObservable');
@@ -109,6 +117,8 @@ export function install(plugin: Plugin) {
plugin.pushToObserve('onToolBarDidMountObservable');
plugin.pushToObserve('onModalGroupDidMountObservable');
+ plugin.pushToObserve('onRichTextDidMountObservable');
+ plugin.pushToObserve('onFormulaBarDidMountObservable');
plugin.pushToObserve('onLineColorDidMountObservable');
plugin.pushToObserve('onSheetBarDidMountObservable');
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionFactory.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionFactory.ts
similarity index 60%
rename from packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionFactory.ts
rename to packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionFactory.ts
index 36f693f061..3c8e4c3c83 100644
--- a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionFactory.ts
+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionFactory.ts
@@ -1,15 +1,10 @@
import { Plugin } from '@univer/core';
-
-export interface ICell {
- row: number;
- column: number;
- value: string;
-}
+import { ICell } from '../../Interfaces';
/**
* Modify cell value
*/
-export class BaseCellExtension {
+export class BaseCellEditExtension {
constructor(protected _cell: ICell, protected _value: string) {}
getCell(): ICell {
@@ -28,9 +23,9 @@ export class BaseCellExtension {
}
/**
- * Determine whether to intercept and create BaseCellExtension
+ * Determine whether to intercept and create BaseCellEditExtension
*/
-export class BaseCellExtensionFactory<T extends Plugin = Plugin> {
+export class BaseCellEditExtensionFactory<T extends Plugin = Plugin> {
constructor(protected _plugin: T) {}
get zIndex() {
@@ -42,8 +37,8 @@ export class BaseCellExtensionFactory<T extends Plugin = Plugin> {
* @param value
* @returns
*/
- create(cell: ICell, value: string): BaseCellExtension {
- return new BaseCellExtension(cell, value);
+ create(cell: ICell, value: string): BaseCellEditExtension {
+ return new BaseCellEditExtension(cell, value);
}
/**
@@ -53,7 +48,7 @@ export class BaseCellExtensionFactory<T extends Plugin = Plugin> {
* @param value
* @returns
*/
- check(cell: ICell): false | BaseCellExtension {
+ check(cell: ICell): false | BaseCellEditExtension {
return false;
}
}
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionManager.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionManager.ts
new file mode 100644
index 0000000000..06307b12f9
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionManager.ts
@@ -0,0 +1,55 @@
+import { Nullable } from '@univer/core';
+import { ICell } from '../../Interfaces';
+import { BaseCellEditExtension, BaseCellEditExtensionFactory } from './CellEditExtensionFactory';
+import { CellEditExtensionRegister } from './CellEditExtensionRegister';
+
+export class CellEditExtensionManager {
+ private _cellEditExtensionFactoryList: BaseCellEditExtensionFactory[];
+
+ /**
+ * inject cell position
+ * @param row
+ * @param column
+ * @param value
+ */
+ handle(cell: ICell): Nullable<ICell> {
+ // get the sorted list
+ // get the dynamically added list
+ this._cellEditExtensionFactoryList = CellEditExtensionManager.register.cellEditExtensionFactoryList;
+ const extension = this._checkExtension(cell);
+ if (extension) {
+ extension.execute();
+ return extension.getCell();
+ }
+ }
+
+ /**
+ * Execute when the action is matched
+ * @param command
+ * @returns
+ */
+ private _checkExtension(cell: ICell): BaseCellEditExtension | false {
+ let extension: BaseCellEditExtension | false = false;
+ if (!this._cellEditExtensionFactoryList) return false;
+
+ for (let index = 0; index < this._cellEditExtensionFactoryList.length; index++) {
+ const extensionFactory = this._cellEditExtensionFactoryList[index];
+ extension = extensionFactory.check(cell);
+ if (extension !== false) {
+ break;
+ }
+ }
+ return extension;
+ }
+
+ static register: CellEditExtensionRegister;
+
+ static create(): CellEditExtensionRegister {
+ if (!this.register) {
+ this.register = new CellEditExtensionRegister();
+ this.register.initialize();
+ }
+
+ return this.register;
+ }
+}
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionRegister.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionRegister.ts
new file mode 100644
index 0000000000..05fbe47488
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellEditExtensionRegister.ts
@@ -0,0 +1,29 @@
+import { sortRules } from '@univer/core';
+import { BaseCellEditExtensionFactory } from './CellEditExtensionFactory';
+import { REGISTRY_CELL_EDIT_FACTORY } from './RegistryFactory';
+
+export class CellEditExtensionRegister {
+ private _cellEditExtensionFactoryList: BaseCellEditExtensionFactory[] = [];
+
+ get cellEditExtensionFactoryList(): BaseCellEditExtensionFactory[] {
+ return this._cellEditExtensionFactoryList;
+ }
+
+ initialize() {
+ this._initExtensions();
+ }
+
+ add(...extensionFactoryList: BaseCellEditExtensionFactory[]) {
+ this._cellEditExtensionFactoryList.push(...extensionFactoryList);
+ this._cellEditExtensionFactoryList.sort(sortRules);
+ }
+
+ delete(extensionFactory: BaseCellEditExtensionFactory) {
+ const index = this._cellEditExtensionFactoryList.indexOf(extensionFactory);
+ this._cellEditExtensionFactoryList.splice(index, 1);
+ }
+
+ private _initExtensions() {
+ this._cellEditExtensionFactoryList.push(...REGISTRY_CELL_EDIT_FACTORY.getData().sort(sortRules));
+ }
+}
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionManager.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionManager.ts
deleted file mode 100644
index 1aedb323fc..0000000000
--- a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionManager.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import { Nullable } from '@univer/core';
-import { BaseCellExtension, BaseCellExtensionFactory, ICell } from './CellExtensionFactory';
-import { CellExtensionRegister } from './CellExtensionRegister';
-
-export class CellExtensionManager {
- private _cellExtensionFactoryList: BaseCellExtensionFactory[];
-
- /**
- * inject cell position
- * @param row
- * @param column
- * @param value
- */
- handle(cell: ICell): Nullable<ICell> {
- // get the sorted list
- // get the dynamically added list
- this._cellExtensionFactoryList = CellExtensionManager.register.cellExtensionFactoryList;
- const extension = this._checkExtension(cell);
- if (extension) {
- extension.execute();
- return extension.getCell();
- }
- }
-
- /**
- * Execute when the action is matched
- * @param command
- * @returns
- */
- private _checkExtension(cell: ICell): BaseCellExtension | false {
- let extension: BaseCellExtension | false = false;
- if (!this._cellExtensionFactoryList) return false;
-
- for (let index = 0; index < this._cellExtensionFactoryList.length; index++) {
- const extensionFactory = this._cellExtensionFactoryList[index];
- extension = extensionFactory.check(cell);
- if (extension !== false) {
- break;
- }
- }
- return extension;
- }
-
- static register: CellExtensionRegister;
-
- static create(): CellExtensionRegister {
- if (!this.register) {
- this.register = new CellExtensionRegister();
- this.register.initialize();
- }
-
- return this.register;
- }
-}
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionRegister.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionRegister.ts
deleted file mode 100644
index 1809fe6da1..0000000000
--- a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionRegister.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { sortRules } from '@univer/core';
-import { BaseCellExtensionFactory } from './CellExtensionFactory';
-import { REGISTRY_CELL_FACTORY } from './RegistryFactory';
-
-export class CellExtensionRegister {
- private _cellExtensionFactoryList: BaseCellExtensionFactory[] = [];
-
- get cellExtensionFactoryList(): BaseCellExtensionFactory[] {
- return this._cellExtensionFactoryList;
- }
-
- initialize() {
- this._initExtensions();
- }
-
- add(...extensionFactoryList: BaseCellExtensionFactory[]) {
- this._cellExtensionFactoryList.push(...extensionFactoryList);
- this._cellExtensionFactoryList.sort(sortRules);
- }
-
- delete(extensionFactory: BaseCellExtensionFactory) {
- const index = this._cellExtensionFactoryList.indexOf(extensionFactory);
- this._cellExtensionFactoryList.splice(index, 1);
- }
-
- private _initExtensions() {
- this._cellExtensionFactoryList.push(...REGISTRY_CELL_FACTORY.getData().sort(sortRules));
- }
-}
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/RegistryFactory.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/RegistryFactory.ts
index 43f35dd30f..2b0cb7e4d2 100644
--- a/packages/base-sheets/src/Basics/Register/CellEditRegister/RegistryFactory.ts
+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/RegistryFactory.ts
@@ -20,4 +20,4 @@ class Registry {
}
}
-export const REGISTRY_CELL_FACTORY = Registry.create();
+export const REGISTRY_CELL_EDIT_FACTORY = Registry.create();
diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/index.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/index.ts
index 9c5d819843..d2797baf16 100644
--- a/packages/base-sheets/src/Basics/Register/CellEditRegister/index.ts
+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/index.ts
@@ -1,6 +1,6 @@
// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)
-export * from './CellExtensionFactory';
-export * from './CellExtensionManager';
-export * from './CellExtensionRegister';
+export * from './CellEditExtensionFactory';
+export * from './CellEditExtensionManager';
+export * from './CellEditExtensionRegister';
// @endindex
diff --git a/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionFactory.ts b/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionFactory.ts
new file mode 100644
index 0000000000..85da1e6f09
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionFactory.ts
@@ -0,0 +1,54 @@
+import { Plugin } from '@univer/core';
+import { ICell } from '../../Interfaces';
+
+/**
+ * Modify cell value
+ */
+export class BaseCellInputExtension {
+ constructor(protected _cell: ICell, protected _value: string) {}
+
+ getCell(): ICell {
+ return this._cell;
+ }
+
+ /**
+ * Modify the value
+ */
+ setValue(value: string) {}
+
+ /**
+ * Execute the core logic after the check is successful
+ */
+ execute() {}
+}
+
+/**
+ * Determine whether to intercept and create BaseCellEditExtension
+ */
+export class BaseCellInputExtensionFactory<T extends Plugin = Plugin> {
+ constructor(protected _plugin: T) {}
+
+ get zIndex() {
+ return 0;
+ }
+
+ /**
+ * Generate FormulaBar Extension
+ * @param value
+ * @returns
+ */
+ create(cell: ICell, value: string): BaseCellInputExtension {
+ return new BaseCellInputExtension(cell, value);
+ }
+
+ /**
+ * Check if this cell needs to be intercepted currently
+ * @param row
+ * @param column
+ * @param value
+ * @returns
+ */
+ check(cell: ICell): false | BaseCellInputExtension {
+ return false;
+ }
+}
diff --git a/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionManager.ts b/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionManager.ts
new file mode 100644
index 0000000000..7cd827a230
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionManager.ts
@@ -0,0 +1,55 @@
+import { Nullable } from '@univer/core';
+import { ICell } from '../../Interfaces';
+import { BaseCellInputExtension, BaseCellInputExtensionFactory } from './CellInputExtensionFactory';
+import { CellInputExtensionRegister } from './CellInputExtensionRegister';
+
+export class CellInputExtensionManager {
+ private _cellInputExtensionFactoryList: BaseCellInputExtensionFactory[];
+
+ /**
+ * inject cell position
+ * @param row
+ * @param column
+ * @param value
+ */
+ handle(cell: ICell): Nullable<ICell> {
+ // get the sorted list
+ // get the dynamically added list
+ this._cellInputExtensionFactoryList = CellInputExtensionManager.register.cellInputExtensionFactoryList;
+ const extension = this._checkExtension(cell);
+ if (extension) {
+ extension.execute();
+ return extension.getCell();
+ }
+ }
+
+ /**
+ * Execute when the action is matched
+ * @param command
+ * @returns
+ */
+ private _checkExtension(cell: ICell): BaseCellInputExtension | false {
+ let extension: BaseCellInputExtension | false = false;
+ if (!this._cellInputExtensionFactoryList) return false;
+
+ for (let index = 0; index < this._cellInputExtensionFactoryList.length; index++) {
+ const extensionFactory = this._cellInputExtensionFactoryList[index];
+ extension = extensionFactory.check(cell);
+ if (extension !== false) {
+ break;
+ }
+ }
+ return extension;
+ }
+
+ static register: CellInputExtensionRegister;
+
+ static create(): CellInputExtensionRegister {
+ if (!this.register) {
+ this.register = new CellInputExtensionRegister();
+ this.register.initialize();
+ }
+
+ return this.register;
+ }
+}
diff --git a/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionRegister.ts b/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionRegister.ts
new file mode 100644
index 0000000000..96bb68ae7d
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellInputRegister/CellInputExtensionRegister.ts
@@ -0,0 +1,29 @@
+import { sortRules } from '@univer/core';
+import { BaseCellInputExtensionFactory } from './CellInputExtensionFactory';
+import { REGISTRY_CELL_INPUT_FACTORY } from './RegistryFactory';
+
+export class CellInputExtensionRegister {
+ private _cellInputExtensionFactoryList: BaseCellInputExtensionFactory[] = [];
+
+ get cellInputExtensionFactoryList(): BaseCellInputExtensionFactory[] {
+ return this._cellInputExtensionFactoryList;
+ }
+
+ initialize() {
+ this._initExtensions();
+ }
+
+ add(...extensionFactoryList: BaseCellInputExtensionFactory[]) {
+ this._cellInputExtensionFactoryList.push(...extensionFactoryList);
+ this._cellInputExtensionFactoryList.sort(sortRules);
+ }
+
+ delete(extensionFactory: BaseCellInputExtensionFactory) {
+ const index = this._cellInputExtensionFactoryList.indexOf(extensionFactory);
+ this._cellInputExtensionFactoryList.splice(index, 1);
+ }
+
+ private _initExtensions() {
+ this._cellInputExtensionFactoryList.push(...REGISTRY_CELL_INPUT_FACTORY.getData().sort(sortRules));
+ }
+}
diff --git a/packages/base-sheets/src/Basics/Register/CellInputRegister/RegistryFactory.ts b/packages/base-sheets/src/Basics/Register/CellInputRegister/RegistryFactory.ts
new file mode 100644
index 0000000000..deac37e549
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellInputRegister/RegistryFactory.ts
@@ -0,0 +1,23 @@
+// import { Registry } from '../Basics';
+class Registry {
+ private _data: any[] = [];
+
+ add(dataInstance: any) {
+ this._data.push(dataInstance);
+ }
+
+ delete(dataInstance: any) {
+ const index = this._data.indexOf(dataInstance);
+ this._data.splice(index, 1);
+ }
+
+ getData() {
+ return this._data;
+ }
+
+ static create() {
+ return new Registry();
+ }
+}
+
+export const REGISTRY_CELL_INPUT_FACTORY = Registry.create();
diff --git a/packages/base-sheets/src/Basics/Register/CellInputRegister/index.ts b/packages/base-sheets/src/Basics/Register/CellInputRegister/index.ts
new file mode 100644
index 0000000000..af38dd6328
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Register/CellInputRegister/index.ts
@@ -0,0 +1,6 @@
+// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)
+
+export * from './CellInputExtensionFactory';
+export * from './CellInputExtensionManager';
+export * from './CellInputExtensionRegister';
+// @endindex
diff --git a/packages/base-sheets/src/Basics/Register/SelectionRegister/index.ts b/packages/base-sheets/src/Basics/Register/SelectionRegister/index.ts
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/packages/base-sheets/src/Basics/Register/index.ts b/packages/base-sheets/src/Basics/Register/index.ts
index 79d29672ba..7efbfd9304 100644
--- a/packages/base-sheets/src/Basics/Register/index.ts
+++ b/packages/base-sheets/src/Basics/Register/index.ts
@@ -1,4 +1,4 @@
// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)
export * from './CellEditRegister/index';
-// export * from './SelectionRegister/index';
+export * from './CellInputRegister/index';
// @endindex
diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.ts
index ec9eed1a3d..46ece4f90b 100644
--- a/packages/base-sheets/src/Controller/CellEditorController.ts
+++ b/packages/base-sheets/src/Controller/CellEditorController.ts
@@ -1,27 +1,23 @@
-import { getRefElement, handleDomToJson, handleJsonToDom, IMainProps, ISlotElement, handleStyleToString, handleStringToStyle } from '@univer/base-component';
+import { getRefElement, handleDomToJson, handleJsonToDom, handleStyleToString, handleStringToStyle, $$ } from '@univer/base-component';
import { Direction, IDocumentData, IRangeData, IStyleData, Nullable, ICellData, Tools, isKeyPrintable, PLUGIN_NAMES } from '@univer/core';
import { DeviceType, IKeyboardEvent, IMouseEvent, IPointerEvent } from '@univer/base-render';
-import { RichText } from '@univer/style-universheet';
import { SheetPlugin } from '../SheetPlugin';
import { SheetContainer } from '../View/UI/SheetContainer';
import { CANVAS_VIEW_KEY } from '../View/Render/BaseView';
-import { CellExtensionManager } from '../Basics/Register/CellEditRegister';
+import { CellEditExtensionManager } from '../Basics/Register/CellEditRegister';
+import { RichText } from '../View/UI/RichText/RichText';
const CELL_EDIT_HIDDEN_TOP = -10000;
/**
* Cell Editor
- *
- * TODO:
- *
- * 2. p存储的富文本字符数少于50的情况下,存一份到v,便于vlookup公式计算
*/
export class CellEditorController {
private _plugin: SheetPlugin;
private _sheetContainer: SheetContainer;
- private _cellExtensionManager: CellExtensionManager;
+ private _cellEditExtensionManager: CellEditExtensionManager;
richTextEle: HTMLElement;
@@ -38,46 +34,35 @@ export class CellEditorController {
constructor(plugin: SheetPlugin) {
this._plugin = plugin;
- this.initRegisterComponent();
-
this._initialize();
}
private _initialize() {
this._plugin.getObserver('onSheetContainerDidMountObservable')?.add((sheetContainer: SheetContainer) => {
this._sheetContainer = sheetContainer;
- const mainItem: IMainProps = {
- name: 'cellEditor',
- type: ISlotElement.JSX_STRING,
- content: 'RichText',
- show: false,
- };
-
- // this._plugin.addMain(mainItem).then(() => {
- // const cellEditor = this._sheetContainer.refMap.cellEditor;
- // this.richTextEle = getRefElement(cellEditor);
- // this.richTextEditEle = $$('div', this.richTextEle);
- // this.richText = cellEditor.current as RichText;
-
- // // focus
- // this._plugin.showMainByName('cellEditor', true).then(() => {
- // this.richTextEditEle.focus();
- // this.richTextEditEle.tabIndex = 1;
-
- // this.hideEditContainer();
- // });
-
- // // init event
- // this._handleKeyboardAction();
-
- // // // set key down hooks
- // // this.richText.hooks.set('onKeyDown', (event) => {
- // // let kCode = event.keyCode;
- // // if (kCode === KeyCode.ENTER) {
- // // this.exitEditMode();
- // // }
- // // });
- // });
+ // const mainItem: IMainProps = {
+ // name: 'cellEditor',
+ // type: ISlotElement.JSX_STRING,
+ // content: 'RichText',
+ // show: false,
+ // };
+
+ // init RichText component
+ this.initRegisterComponent();
+ this._plugin.getObserver('onRichTextDidMountObservable')?.add((cellEditor) => {
+ this.richTextEle = getRefElement(cellEditor.container);
+ this.richTextEditEle = $$('div', this.richTextEle);
+ this.richText = cellEditor;
+
+ // focus
+ this.richTextEditEle.focus();
+ this.richTextEditEle.tabIndex = 1;
+
+ this.hideEditContainer();
+
+ // init event
+ this._handleKeyboardAction();
+ });
});
this._plugin.context
@@ -204,7 +189,7 @@ export class CellEditorController {
this._plugin.getObserver('onSpreadsheetKeyCompositionUpdateObservable')?.add((evt: IKeyboardEvent) => {});
this._plugin.getObserver('onSpreadsheetKeyCompositionEndObservable')?.add((evt: IKeyboardEvent) => {});
- this._cellExtensionManager = new CellExtensionManager();
+ this._cellEditExtensionManager = new CellEditExtensionManager();
}
/**
@@ -364,7 +349,7 @@ export class CellEditorController {
let cellValue = this.getSelectionValue();
// Intercept, the formula needs to modify the value of the edit state
- const cell = this._cellExtensionManager.handle({
+ const cell = this._cellEditExtensionManager.handle({
row: currentCell.row,
column: currentCell.column,
value: cellValue,
diff --git a/packages/base-sheets/src/Controller/FormulaBarController.ts b/packages/base-sheets/src/Controller/FormulaBarController.ts
new file mode 100644
index 0000000000..88abff57e0
--- /dev/null
+++ b/packages/base-sheets/src/Controller/FormulaBarController.ts
@@ -0,0 +1,75 @@
+import { PLUGIN_NAMES } from '@univer/core';
+import { CellInputExtensionManager } from '../Basics/Register/CellInputRegister';
+import { SheetPlugin } from '../SheetPlugin';
+import { FormulaBar } from '../View/UI/FormulaBar';
+import { SelectionControl } from './Selection';
+
+export class FormulaBarController {
+ private _formulaBar: FormulaBar;
+
+ private _plugin: SheetPlugin;
+
+ private _cellInputExtensionManager: CellInputExtensionManager;
+
+ constructor(plugin: SheetPlugin) {
+ this._plugin = plugin;
+
+ this._initialize();
+ }
+
+ private _initialize() {
+ const context = this._plugin.context;
+ const manager = context.getObserverManager();
+
+ manager.requiredObserver<FormulaBar>('onFormulaBarDidMountObservable', PLUGIN_NAMES.SPREADSHEET).add((component) => {
+ this._formulaBar = component;
+ });
+
+ this._plugin.getObserver('onChangeSelectionObserver')?.add((selectionControl: SelectionControl) => {
+ const currentCell = selectionControl.model.currentCell;
+
+ if (currentCell) {
+ let currentRangeData;
+
+ if (currentCell.isMerged) {
+ const mergeInfo = currentCell.mergeInfo;
+
+ currentRangeData = {
+ startRow: mergeInfo.startRow,
+ endRow: mergeInfo.endRow,
+ startColumn: mergeInfo.startColumn,
+ endColumn: mergeInfo.endColumn,
+ };
+ } else {
+ const { row, column } = currentCell;
+ currentRangeData = {
+ startRow: row,
+ endRow: row,
+ startColumn: column,
+ endColumn: column,
+ };
+ }
+
+ const cellData = this._plugin.getWorkbook().getActiveSheet().getRange(currentRangeData).getObjectValue({ isIncludeStyle: true });
+
+ if (cellData) {
+ let cellValue = cellData.m || `${cellData.v}` || '';
+
+ // If the cell has a formula, it needs to be intercepted
+ const cell = this._cellInputExtensionManager.handle({
+ row: currentRangeData.startRow,
+ column: currentRangeData.startColumn,
+ value: cellValue,
+ });
+
+ if (cell) {
+ cellValue = cell.value;
+ }
+ this._formulaBar.setFormulaContent(cellValue);
+ }
+ }
+ });
+
+ this._cellInputExtensionManager = new CellInputExtensionManager();
+ }
+}
diff --git a/packages/base-sheets/src/SheetPlugin.tsx b/packages/base-sheets/src/SheetPlugin.tsx
index 27ec74d9f4..1a5b20056d 100644
--- a/packages/base-sheets/src/SheetPlugin.tsx
+++ b/packages/base-sheets/src/SheetPlugin.tsx
@@ -21,6 +21,7 @@ import {
import { IToolBarItemProps } from './Model/ToolBarModel';
import { ModalGroupController } from './Controller/ModalGroupController';
import { ISheetPluginConfig, DEFAULT_SPREADSHEET_PLUGIN_DATA } from './Basics';
+import { FormulaBarController } from './Controller/FormulaBarController';
/**
* The main sheet base, construct the sheet container and layout, mount the rendering engine
@@ -61,6 +62,8 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
private _infoBarControl: InfoBarController;
+ private _formulaBarController: FormulaBarController;
+
private _sheetBarControl: SheetBarControl;
private _cellEditorControl: CellEditorController;
@@ -147,6 +150,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
this._rightMenuControl = new RightMenuController(this);
this._toolBarControl = new ToolBarController(this);
this._infoBarControl = new InfoBarController(this);
+ this._formulaBarController = new FormulaBarController(this);
this._sheetBarControl = new SheetBarControl(this);
this._cellEditorControl = new CellEditorController(this);
this._antLineController = new AntLineControl(this);
diff --git a/packages/base-sheets/src/View/UI/FormulaBar/FormulaBar.tsx b/packages/base-sheets/src/View/UI/FormulaBar/FormulaBar.tsx
index 6db533d216..9d4f47c5c8 100644
--- a/packages/base-sheets/src/View/UI/FormulaBar/FormulaBar.tsx
+++ b/packages/base-sheets/src/View/UI/FormulaBar/FormulaBar.tsx
@@ -1,10 +1,11 @@
-import { BaseComponentProps, BaseComponentRender, BaseComponentSheet, Component, debounce, ISelectButton } from '@univer/base-component';
+import { BaseComponentProps, BaseComponentRender, BaseComponentSheet, Component, debounce } from '@univer/base-component';
import { Select } from '../Common/Select/Select';
import styles from './index.module.less';
type FormulaState = {
data: Record<string, any>;
spanClass: string;
+ formulaContent: string;
};
export interface BaseFormulaBarProps extends BaseComponentProps {}
@@ -12,6 +13,8 @@ export interface BaseFormulaBarProps extends BaseComponentProps {}
export class FormulaBar extends Component<BaseFormulaBarProps, FormulaState> {
Render: BaseComponentRender;
+ // formulaContent = createRef<HTMLDivElement>();
+
initialize(props?: BaseFormulaBarProps) {
const component = this._context.getPluginManager().getPluginByName<BaseComponentSheet>('ComponentSheet')!;
this.Render = component.getComponentRender();
@@ -20,12 +23,10 @@ export class FormulaBar extends Component<BaseFormulaBarProps, FormulaState> {
this.state = {
data: {
- selectType: ISelectButton.INPUT,
- needChange: true,
- icon: <NextIcon />,
children: [],
},
spanClass: styles.formulaGrey,
+ formulaContent: '',
};
this.onkeyUp = debounce(this.onkeyUp, 300);
@@ -39,6 +40,16 @@ export class FormulaBar extends Component<BaseFormulaBarProps, FormulaState> {
this.onkeyUp(e.target);
};
+ setFormulaContent(value: string) {
+ this.setState({
+ formulaContent: value,
+ });
+ }
+
+ componentDidMount() {
+ this._context.getObserverManager().getObserver<FormulaBar>('onFormulaBarDidMountObservable')?.notifyObservers(this);
+ }
+
render(props: BaseFormulaBarProps, state: FormulaState) {
const { data } = state;
@@ -62,7 +73,9 @@ export class FormulaBar extends Component<BaseFormulaBarProps, FormulaState> {
</span>
</div>
<div className={styles.formulaInput}>
- <div autoFocus contentEditable={true} className={styles.formulaContent} onKeyUp={(e) => this.printChange(e)}></div>
+ <div autoFocus contentEditable={true} className={styles.formulaContent} onKeyUp={(e) => this.printChange(e)}>
+ {state.formulaContent}
+ </div>
</div>
</div>
</div>
diff --git a/packages/base-sheets/src/View/UI/RichText/RichText.tsx b/packages/base-sheets/src/View/UI/RichText/RichText.tsx
index 0ee3ef12b9..e0f04d5047 100644
--- a/packages/base-sheets/src/View/UI/RichText/RichText.tsx
+++ b/packages/base-sheets/src/View/UI/RichText/RichText.tsx
@@ -48,6 +48,8 @@ export class RichText extends Component<BaseRichTextProps, IState> {
// private _fontSizeObserv: Nullable<WorkBookObserver<number>>;
+ container = createRef<HTMLDivElement>();
+
ref = createRef<HTMLDivElement>();
cellTextStyle: CellTextStyle;
@@ -76,6 +78,14 @@ export class RichText extends Component<BaseRichTextProps, IState> {
return this.cellInputHandler.getInputValue();
}
+ show() {
+ this.container.current!.style.display = 'block';
+ }
+
+ hide() {
+ this.container.current!.style.display = 'none';
+ }
+
// TODO 键盘事件
onKeyDown(event: KeyboardEvent) {
let ctrlKey = event.ctrlKey;
@@ -248,6 +258,8 @@ export class RichText extends Component<BaseRichTextProps, IState> {
componentDidMount() {
this.cellTextStyle = new CellTextStyle(this.ref.current!);
this.cellInputHandler = new CellInputHandler(this.ref.current!);
+
+ this._context.getObserverManager().getObserver<RichText>('onRichTextDidMountObservable')?.notifyObservers(this);
}
/**
@@ -260,7 +272,7 @@ export class RichText extends Component<BaseRichTextProps, IState> {
render(props: BaseRichTextProps, state: IState) {
const { style, className, onClick, text } = props;
return (
- <div className={`${styles.richTextEditorContainer} ${className}`} style={style}>
+ <div className={`${styles.richTextEditorContainer} ${className}`} style={style} ref={this.container}>
<div
ref={this.ref}
className={styles.richTextEditor}
diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellEditExtension.ts
similarity index 65%
rename from packages/sheets-plugin-formula/src/Basic/Register/FormulaCellExtension.ts
rename to packages/sheets-plugin-formula/src/Basic/Register/FormulaCellEditExtension.ts
index 0b75cd88b5..d286b62d14 100644
--- a/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellExtension.ts
+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellEditExtension.ts
@@ -1,8 +1,8 @@
-import { BaseCellExtension, BaseCellExtensionFactory, ICell } from '@univer/base-sheets';
+import { BaseCellEditExtension, BaseCellEditExtensionFactory, ICell } from '@univer/base-sheets';
import { IFormulaData } from '@univer/base-formula-engine';
import { FormulaPlugin } from '../../FormulaPlugin';
-export class FormulaCellExtension extends BaseCellExtension {
+export class FormulaCellEditExtension extends BaseCellEditExtension {
setValue(value: string) {
this._cell.value = value;
}
@@ -12,13 +12,13 @@ export class FormulaCellExtension extends BaseCellExtension {
}
}
-export class FormulaCellExtensionFactory extends BaseCellExtensionFactory<FormulaPlugin> {
+export class FormulaCellEditExtensionFactory extends BaseCellEditExtensionFactory<FormulaPlugin> {
get zIndex(): number {
return 0;
}
- create(cell: ICell, value: string): FormulaCellExtension {
- return new FormulaCellExtension(cell, value);
+ create(cell: ICell, value: string): FormulaCellEditExtension {
+ return new FormulaCellEditExtension(cell, value);
}
check(cell: ICell) {
@@ -36,13 +36,6 @@ export class FormulaCellExtensionFactory extends BaseCellExtensionFactory<Formul
for (let i = 0, len = sheetIds.length; i < len; i++) {
const sheetId = sheetIds[i];
const cellData = sheetData[sheetId];
- console.info('cellData', cellData);
- // cellData.forEach((row, rowArray) => {
- // rowArray.forEach((column, mainCell) => {
- // console.log('main----', row, column, mainCell);
- // });
- // });
-
Object.keys(cellData).forEach((cellRow) => {
const rowArray = cellData[cellRow];
rowArray.forEach((cellColumn: number, value: IFormulaData) => {
diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellInputExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellInputExtension.ts
new file mode 100644
index 0000000000..02ee77e71b
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellInputExtension.ts
@@ -0,0 +1,57 @@
+import { BaseCellInputExtension, BaseCellInputExtensionFactory, ICell } from '@univer/base-sheets';
+import { IFormulaData } from '@univer/base-formula-engine';
+import { FormulaPlugin } from '../../FormulaPlugin';
+
+export class FormulaCellInputExtension extends BaseCellInputExtension {
+ setValue(value: string) {
+ this._cell.value = value;
+ }
+
+ execute() {
+ this.setValue(this._value);
+ }
+}
+
+export class FormulaCellInputExtensionFactory extends BaseCellInputExtensionFactory<FormulaPlugin> {
+ get zIndex(): number {
+ return 0;
+ }
+
+ create(cell: ICell, value: string): FormulaCellInputExtension {
+ return new FormulaCellInputExtension(cell, value);
+ }
+
+ check(cell: ICell) {
+ const { row, column } = cell;
+
+ const formulaData = this._plugin.getFormulaController().getDataModel().getFormulaData();
+
+ let formula;
+
+ Object.keys(formulaData).forEach((unitId) => {
+ const sheetData = formulaData[unitId];
+
+ const sheetIds = Object.keys(sheetData);
+
+ for (let i = 0, len = sheetIds.length; i < len; i++) {
+ const sheetId = sheetIds[i];
+ const cellData = sheetData[sheetId];
+ Object.keys(cellData).forEach((cellRow) => {
+ const rowArray = cellData[cellRow];
+ rowArray.forEach((cellColumn: number, value: IFormulaData) => {
+ if (Number(cellRow) === row && cellColumn === column) {
+ formula = value.formula;
+ return false;
+ }
+ });
+ });
+ }
+ });
+
+ if (!formula) {
+ return false;
+ }
+
+ return this.create(cell, formula);
+ }
+}
diff --git a/packages/sheets-plugin-formula/src/FormulaPlugin.tsx b/packages/sheets-plugin-formula/src/FormulaPlugin.tsx
index d668191ee6..289076b866 100644
--- a/packages/sheets-plugin-formula/src/FormulaPlugin.tsx
+++ b/packages/sheets-plugin-formula/src/FormulaPlugin.tsx
@@ -1,5 +1,5 @@
import { SheetContext, IOCContainer, UniverSheet, Plugin, PLUGIN_NAMES } from '@univer/core';
-import { CellExtensionManager, SheetPlugin } from '@univer/base-sheets';
+import { CellEditExtensionManager, CellInputExtensionManager, SheetPlugin } from '@univer/base-sheets';
import { FormulaEnginePlugin } from '@univer/base-formula-engine';
import { FormulaButton } from './UI/FormulaButton';
import { zh, en } from './Locale';
@@ -8,7 +8,8 @@ import { IConfig, IFormulaConfig } from './Basic/IFormula';
import { FORMULA_PLUGIN_NAME } from './Basic/PLUGIN_NAME';
import { FormulaController } from './Controller/FormulaController';
import { firstLoader } from './Controller/FirstLoader';
-import { FormulaCellExtensionFactory } from './Basic/Register/FormulaCellExtension';
+import { FormulaCellEditExtensionFactory } from './Basic/Register/FormulaCellEditExtension';
+import { FormulaCellInputExtensionFactory } from './Basic/Register/FormulaCellInputExtension';
export class FormulaPlugin extends Plugin<any, SheetContext> {
private _formulaController: FormulaController;
@@ -71,9 +72,13 @@ export class FormulaPlugin extends Plugin<any, SheetContext> {
onDestroy(): void {}
registerExtension() {
- const register = CellExtensionManager.create();
+ const cellEditRegister = CellEditExtensionManager.create();
- register.add(new FormulaCellExtensionFactory(this));
+ cellEditRegister.add(new FormulaCellEditExtensionFactory(this));
+
+ const cellInputRegister = CellInputExtensionManager.create();
+
+ cellInputRegister.add(new FormulaCellInputExtensionFactory(this));
}
getFormulaEngine() {