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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(base-sheet): 1. CellEdit support extension 2. FormulaCellExtension
Dushusir4ce1d9e3e86f
diff --git a/packages/base-formula-engine/src/index.ts b/packages/base-formula-engine/src/index.tsindex 67b9ed55e7..d33b3a1a4e 100644--- a/packages/base-formula-engine/src/index.ts+++ b/packages/base-formula-engine/src/index.ts@@ -1,3 +1,4 @@+import { IFormatData } from './../../core/src/Interfaces/ICellData';export { FormulaEnginePlugin } from './FormulaEnginePlugin';-export { type FormulaDataType, type IInterpreterDatasetConfig, type SheetDataType, type UnitDataType, type SheetNameMapType } from './Basics/Common';+export { type FormulaDataType, type IInterpreterDatasetConfig, type SheetDataType, type UnitDataType, type SheetNameMapType, type IFormulaData } from './Basics/Common';diff --git a/packages/base-sheets/src/Basics/Interfaces/SelectionConfig.ts b/packages/base-sheets/src/Basics/Interfaces/SelectionConfig.tsindex bbe5fe5fc1..f2c0d12a77 100644--- a/packages/base-sheets/src/Basics/Interfaces/SelectionConfig.ts+++ b/packages/base-sheets/src/Basics/Interfaces/SelectionConfig.ts@@ -1,10 +1,10 @@import { IRangeCellData, IRangeData } from '@univer/core';-export interface ISelectionValue {+export interface ISelectionConfig {selection: IRangeData;cell?: IRangeCellData;}export interface ISelectionsConfig {- [worksheetId: string]: ISelectionValue[];+ [worksheetId: string]: ISelectionConfig[];}diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionFactory.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionFactory.tsnew file mode 100644index 0000000000..36f693f061--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionFactory.ts@@ -0,0 +1,59 @@+import { Plugin } from '@univer/core';++export interface ICell {+ row: number;+ column: number;+ value: string;+}++/**+ * Modify cell value+ */+export class BaseCellExtension {+ 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 BaseCellExtension+ */+export class BaseCellExtensionFactory<T extends Plugin = Plugin> {+ constructor(protected _plugin: T) {}++ get zIndex() {+ return 0;+ }++ /**+ * Generate Cell Extension+ * @param value+ * @returns+ */+ create(cell: ICell, value: string): BaseCellExtension {+ return new BaseCellExtension(cell, value);+ }++ /**+ * Check if this cell needs to be intercepted currently+ * @param row+ * @param column+ * @param value+ * @returns+ */+ check(cell: ICell): false | BaseCellExtension {+ return false;+ }+}diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionManager.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionManager.tsnew file mode 100644index 0000000000..1aedb323fc--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionManager.ts@@ -0,0 +1,54 @@+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.tsnew file mode 100644index 0000000000..1809fe6da1--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/CellExtensionRegister.ts@@ -0,0 +1,29 @@+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.tsnew file mode 100644index 0000000000..43f35dd30f--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/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_FACTORY = Registry.create();diff --git a/packages/base-sheets/src/Basics/Register/CellEditRegister/index.ts b/packages/base-sheets/src/Basics/Register/CellEditRegister/index.tsnew file mode 100644index 0000000000..9c5d819843--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/CellEditRegister/index.ts@@ -0,0 +1,6 @@+// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)++export * from './CellExtensionFactory';+export * from './CellExtensionManager';+export * from './CellExtensionRegister';+// @endindexdiff --git a/packages/base-sheets/src/Basics/Register/SelectionRegister/index.ts b/packages/base-sheets/src/Basics/Register/SelectionRegister/index.tsnew file mode 100644index 0000000000..e69de29bb2diff --git a/packages/base-sheets/src/Basics/Register/index.ts b/packages/base-sheets/src/Basics/Register/index.tsnew file mode 100644index 0000000000..79d29672ba--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/index.ts@@ -0,0 +1,4 @@+// @index(['./*.ts','./**/index.ts'], f => `export * from '${f.path}'`)+export * from './CellEditRegister/index';+// export * from './SelectionRegister/index';+// @endindexdiff --git a/packages/base-sheets/src/Basics/index.ts b/packages/base-sheets/src/Basics/index.tsindex 009ee64abd..e2d69904ea 100644--- a/packages/base-sheets/src/Basics/index.ts+++ b/packages/base-sheets/src/Basics/index.ts@@ -3,6 +3,7 @@ export * from './Const/index';export * from './Enum/index';export * from './Interfaces/index';export * from './Observer';+export * from './Register';/* eslint-disable import/first */import './RegisterAction';diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.tsindex 498f22498e..1e530c73c2 100644--- a/packages/base-sheets/src/Controller/CellEditorController.ts+++ b/packages/base-sheets/src/Controller/CellEditorController.ts@@ -5,6 +5,7 @@ import { RichText } from '@univer/style-universheet';import { SpreadsheetPlugin } from '../SpreadsheetPlugin';import { SheetContainer } from '../View/UI/SheetContainer';import { CANVAS_VIEW_KEY } from '../View/Render/BaseView';+import { CellExtensionManager } from '../Basics/Register/CellEditRegister';const CELL_EDIT_HIDDEN_TOP = -10000;@@ -20,6 +21,8 @@ export class CellEditorController {private _sheetContainer: SheetContainer;+ private _cellExtensionManager: CellExtensionManager;+richTextEle: HTMLElement;richTextEditEle: HTMLElement;@@ -198,6 +201,8 @@ export class CellEditorController {});this._plugin.getObserver('onSpreadsheetKeyCompositionUpdateObservable')?.add((evt: IKeyboardEvent) => {});this._plugin.getObserver('onSpreadsheetKeyCompositionEndObservable')?.add((evt: IKeyboardEvent) => {});++ this._cellExtensionManager = new CellExtensionManager();}/**@@ -346,7 +351,19 @@ export class CellEditorController {this.richTextEle.style.maxHeight = `${sheetContentRect.height - startY + scrollY}px`;// this._plugin.showMainByName('cellEditor', true).then(() => {- const cellValue = this.getSelectionValue();+ let cellValue = this.getSelectionValue();++ // Intercept, the formula needs to modify the value of the edit state+ const cell = this._cellExtensionManager.handle({+ row: currentCell.row,+ column: currentCell.column,+ value: cellValue,+ });++ if (cell) {+ cellValue = cell.value;+ }+this.richText.setValue(cellValue);const style = this.getSelectionStyle();diff --git a/packages/base-sheets/src/SpreadsheetPlugin.tsx b/packages/base-sheets/src/SpreadsheetPlugin.tsxindex eda77e3fc5..eaf26274ad 100644--- a/packages/base-sheets/src/SpreadsheetPlugin.tsx+++ b/packages/base-sheets/src/SpreadsheetPlugin.tsx@@ -1,6 +1,6 @@import { getRefElement, IMainProps, isElement, ISlotProps, IToolBarItemProps, RefObject, render } from '@univer/base-component';import { Engine, RenderEngine } from '@univer/base-render';-import { AsyncFunction, Attribute, Context, IOCAttribute, IOCContainer, IRangeCellData, IRangeData, IWorkbookConfig, Plugin, PLUGIN_NAMES, Tools } from '@univer/core';+import { AsyncFunction, Attribute, Context, IOCAttribute, IOCContainer, IWorkbookConfig, Plugin, PLUGIN_NAMES, Tools } from '@univer/core';import { FormulaPlugin } from '@univer/sheets-plugin-formula';import { install, SpreadsheetPluginObserve, uninstall } from './Basics/Observer';@@ -8,7 +8,7 @@ import { RightMenuProps } from './Model/RightMenuModel';import { en, zh } from './Locale';import { CANVAS_VIEW_KEY } from './View/Render/BaseView';import { CanvasView } from './View/Render/CanvasView';-import { BaseSheetContainerConfig, ISpreadsheetPluginConfigBase, SheetContainer } from './View/UI/SheetContainer';+import { BaseSheetContainerConfig, SheetContainer } from './View/UI/SheetContainer';import {RightMenuController,ToolBarController,@@ -20,79 +20,7 @@ import {SheetContainerController,} from './Controller';import { ToolBarController1 } from './Controller/ToolBarController1';--export interface ISelectionConfig {- selection: IRangeData;- cell?: IRangeCellData;-}--export interface ISelectionsConfig {- [worksheetId: string]: ISelectionConfig[];-}--export interface ISpreadsheetPluginConfig extends ISpreadsheetPluginConfigBase {- container: HTMLElement | string;- selections: ISelectionsConfig;-}--const DEFAULT_SPREADSHEET_PLUGIN_DATA: ISpreadsheetPluginConfig = {- container: 'universheet',- layout: 'auto',- selections: {- 'sheet-01': [- {- selection: {- startRow: 13,- endRow: 14,- startColumn: 1,- endColumn: 2,- },- },- {- selection: {- startRow: 16,- endRow: 18,- startColumn: 1,- endColumn: 2,- },- cell: {- row: 16,- column: 1,- },- },- ],- 'sheet-02': [- {- selection: {- startRow: 17,- endRow: 20,- startColumn: 1,- endColumn: 2,- },- },- {- selection: {- startRow: 22,- endRow: 23,- startColumn: 1,- endColumn: 2,- },- },- {- selection: {- startRow: 25,- endRow: 27,- startColumn: 4,- endColumn: 6,- },- cell: {- row: 25,- column: 4,- },- },- ],- },-};+import { ISpreadsheetPluginConfig, DEFAULT_SPREADSHEET_PLUGIN_DATA } from './Basics';/*** The main sheet base, construct the sheet container and layout, mount the rendering enginediff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.tsindex 9a272b3866..3fa3d9e65f 100644--- a/packages/base-sheets/src/index.ts+++ b/packages/base-sheets/src/index.ts@@ -1,3 +1,4 @@export * from './SpreadsheetPlugin';export * from './Model';export * from './Controller';+export * from './Basics';diff --git a/packages/core/src/Command/ActionBase.ts b/packages/core/src/Command/ActionBase.tsindex 82ce7b4c32..66a73921c7 100644--- a/packages/core/src/Command/ActionBase.ts+++ b/packages/core/src/Command/ActionBase.ts@@ -17,7 +17,6 @@ export interface IActionData {* Action Operation Type*/export enum ActionOperation {-/*** send obs*/@@ -36,7 +35,7 @@ export enum ActionOperation {/*** default obs*/- DEFAULT_ACTION = 1 | 2 | 3+ DEFAULT_ACTION = 1 | 2 | 3,}/**diff --git a/packages/core/src/Sheets/Apply/SetRangeData.ts b/packages/core/src/Sheets/Apply/SetRangeData.tsindex 4e4875a229..9a6f9b0ec6 100644--- a/packages/core/src/Sheets/Apply/SetRangeData.ts+++ b/packages/core/src/Sheets/Apply/SetRangeData.ts@@ -117,6 +117,8 @@ export function SetRangeData(}if (value.m) {cell.m = value.m;+ } else {+ cell.m = String(cell.v);}if (value.t) {cell.t = value.t;diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellExtension.tsnew file mode 100644index 0000000000..f226367704--- /dev/null+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaCellExtension.ts@@ -0,0 +1,65 @@+import { BaseCellExtension, BaseCellExtensionFactory, ICell } from '@univer/base-sheets';+import { IFormulaData } from '@univer/base-formula-engine';+import { FormulaPlugin } from '../../FormulaPlugin';++export class FormulaCellExtension extends BaseCellExtension {+ setValue(value: string) {+ this._cell.value = value;+ }++ execute() {+ console.info('Find formula!');+ this.setValue(this._value);+ }+}++export class FormulaCellExtensionFactory extends BaseCellExtensionFactory<FormulaPlugin> {+ get zIndex(): number {+ return 0;+ }++ create(cell: ICell, value: string): FormulaCellExtension {+ return new FormulaCellExtension(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];+ 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) => {+ 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/Basic/Register/RegisterExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/RegisterExtension.tsnew file mode 100644index 0000000000..e69de29bb2diff --git a/packages/sheets-plugin-formula/src/Basic/Register/index.ts b/packages/sheets-plugin-formula/src/Basic/Register/index.tsnew file mode 100644index 0000000000..87f7140319--- /dev/null+++ b/packages/sheets-plugin-formula/src/Basic/Register/index.ts@@ -0,0 +1,2 @@+/* eslint-disable import/first */+import './RegisterExtension';diff --git a/packages/sheets-plugin-formula/src/Basic/index.ts b/packages/sheets-plugin-formula/src/Basic/index.tsindex 6c6e3b5d96..17b7b8c43f 100644--- a/packages/sheets-plugin-formula/src/Basic/index.ts+++ b/packages/sheets-plugin-formula/src/Basic/index.ts@@ -1,2 +1,3 @@export * from './Cache';export * from './State';+export * from './PLUGIN_NAME';diff --git a/packages/sheets-plugin-formula/src/Controller/FirstLoader.ts b/packages/sheets-plugin-formula/src/Controller/FirstLoader.tsindex 6de0908f5d..98363f90ef 100644--- a/packages/sheets-plugin-formula/src/Controller/FirstLoader.ts+++ b/packages/sheets-plugin-formula/src/Controller/FirstLoader.ts@@ -32,11 +32,12 @@ export function firstLoader(formulaController: FormulaController) {startRow: row,endRow: row,},- cellValue: {- [row]: {- [column]: mainCell,- },- },+ cellValue: mainCell,+ // cellValue: {+ // [row]: {+ // [column]: mainCell,+ // },+ // },});});});diff --git a/packages/sheets-plugin-formula/src/FormulaPlugin.tsx b/packages/sheets-plugin-formula/src/FormulaPlugin.tsxindex 663494b619..62b41957af 100644--- a/packages/sheets-plugin-formula/src/FormulaPlugin.tsx+++ b/packages/sheets-plugin-formula/src/FormulaPlugin.tsx@@ -1,6 +1,6 @@import { Context, IOCContainer, UniverSheet, Plugin, PLUGIN_NAMES } from '@univer/core';import { IToolBarItemProps, ISlotElement } from '@univer/base-component';-import { SpreadsheetPlugin } from '@univer/base-sheets';+import { CellExtensionManager, SpreadsheetPlugin } from '@univer/base-sheets';import { FormulaEnginePlugin } from '@univer/base-formula-engine';import { FormulaButton } from './UI/FormulaButton';import { zh, en } from './Locale';@@ -9,6 +9,7 @@ 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';export class FormulaPlugin extends Plugin {private _formulaController: FormulaController;@@ -58,6 +59,8 @@ export class FormulaPlugin extends Plugin {label: <FormulaButton config={config} />,};context.getPluginManager().getPluginByName<SpreadsheetPlugin>(PLUGIN_NAMES.SPREADSHEET)?.addButton(item);++ this.registerExtension();}onMapping(IOC: IOCContainer): void {}@@ -68,7 +71,17 @@ export class FormulaPlugin extends Plugin {onDestroy(): void {}+ registerExtension() {+ const register = CellExtensionManager.create();++ register.add(new FormulaCellExtensionFactory(this));+ }+getFormulaEngine() {return this._formulaController.getFormulaEngine();}++ getFormulaController() {+ return this._formulaController;+ }}diff --git a/packages/sheets-plugin-formula/src/index.ts b/packages/sheets-plugin-formula/src/index.tsindex 6099f9f23c..502c79567a 100644--- a/packages/sheets-plugin-formula/src/index.ts+++ b/packages/sheets-plugin-formula/src/index.ts@@ -1,2 +1,3 @@export * from './FormulaPlugin';export * from './Basic/DEFAULT_FORMULA_DATA';+export * from './Basic/PLUGIN_NAME';diff --git a/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts b/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.tsindex 1d7ffa4da8..18537f4d35 100644--- a/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts+++ b/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts@@ -1,5 +1,6 @@-import { ACTION_NAMES, Plugin, BaseActionExtension, BaseActionExtensionFactory, ISetRangeDataActionData, ObjectMatrix, IActionData } from '@univer/core';+import { ACTION_NAMES, Plugin, BaseActionExtension, BaseActionExtensionFactory, ISetRangeDataActionData, ObjectMatrix, IActionData, ObjectMatrixPrimitiveType } from '@univer/core';import { numfmt } from '@univer/base-numfmt-engine';+import { ICellData } from '@univer/core/src/Interfaces/ICellData';import { ACTION_NAMES as PLUGIN_ACTION_NAMES } from './Const';export class NumfmtActionExtension extends BaseActionExtension<ISetRangeDataActionData> {@@ -8,17 +9,34 @@ export class NumfmtActionExtension extends BaseActionExtension<ISetRangeDataActi// }execute() {- const rangeMatrix = new ObjectMatrix(this.actionData.cellValue);const numfmtMatrix = new ObjectMatrix<string>();- rangeMatrix.forValue((r, c, cell) => {+ let cellValue = this.actionData.cellValue;+ // a range+ if (!isNaN(parseInt(Object.keys(cellValue)[0]))) {+ const rangeMatrix = new ObjectMatrix(cellValue as ObjectMatrixPrimitiveType<ICellData>);++ rangeMatrix.forValue((r, c, cell) => {+ const typed = numfmt.parseValue(cell.v) as unknown as { z: string; v: string };+ if (typed) {+ const format = numfmt(typed.z);+ cell.m = format(typed.v);+ cell.v = typed.v || String();+ numfmtMatrix.setValue(r, c, typed.z);+ }+ });+ }+ // a cell+ else {+ let cell: ICellData = cellValue;const typed = numfmt.parseValue(cell.v) as unknown as { z: string; v: string };if (typed) {const format = numfmt(typed.z);cell.m = format(typed.v);cell.v = typed.v || String();- numfmtMatrix.setValue(r, c, typed.z);+ numfmtMatrix.setValue(0, 0, typed.z);}- });+ }+const setNumfmtRangeDataAction = {actionName: PLUGIN_ACTION_NAMES.SET_NUMFMT_RANGE_DATA_ACTION,sheetId: this.actionData.sheetId,