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
chore(core): ActionExtension handle actionDataList
Dushusir9d37b9e534fa
diff --git a/.github/contributing-zh.md b/.github/contributing-zh.mdindex a58958e779..a1b4698bc8 100644--- a/.github/contributing-zh.md+++ b/.github/contributing-zh.md@@ -961,6 +961,14 @@ export class AlternatingColorsPlugin extends Plugin {Tips:核心是否要包装插件 API,要看核心是否提供了 Action,不允许核心调用插件 API,如 Find 插件提供 createTextFinder,核心不提供+8. Action 扩展++当用户输入 `100%`,我们需要自动将单元格格式改为`百分比`,当用户输入`=SUM(2)`时候需要计算公式,这种情况下,我们需要使用到数字格式和公式插件的能力来转换这些数据。所以需要在 Command 执行前做个 Action 拦截的动作,拦截 ActionData 传入到数字格式 Action 扩展和公式 Action 扩展,做完计算之后,直接修改这个 ActionData,并且增加自己的数字格式 Action 和公式 Action,来实现这两种计算。++在编辑单元格的时候,我们是不知道用户是否安装了数字格式插件或者公式插件的,所以我们还做了一个 Action 扩展注册机制,只有按照规范注册了的扩展,才会在 Action 拦截的时候使用,其中,扩展管理的地方负责检验 Action 数据是否打中扩展,打中的扩展就会执行。++ActionOperation 有一个 removeExtension 可以移除 action 拦截。一般情况下,Action 都会走拦截,但是在初始化(比如公式)或者已经拦截的 ActionExtension 里触发的 Action(比如 FormulaActionExtension),这两种情况是不需要拦截的,所以要做过滤。+## 组件开发### 技术原理diff --git a/packages/base-sheets/src/Basics/Register/NamedRangeActionExtension.ts b/packages/base-sheets/src/Basics/Register/NamedRangeActionExtension.tsnew file mode 100644index 0000000000..3c01bb3225--- /dev/null+++ b/packages/base-sheets/src/Basics/Register/NamedRangeActionExtension.ts@@ -0,0 +1,60 @@+import {+ ActionOperation,+ ACTION_NAMES,+ BaseActionExtension,+ BaseActionExtensionFactory,+ ISetNamedRangeActionData,+ ISheetActionData,+ Tools,+ IInsertRowActionData,+} from '@univer/core';+import { SheetPlugin } from '../../SheetPlugin';++/**+ * TODO insertColumn/insertRange/insertRange/deleteRange+ */+export class NamedRangeActionExtension extends BaseActionExtension<SheetPlugin> {+ execute() {+ const actionDataList = this.actionDataList;++ actionDataList.forEach((actionData) => {+ if (actionData.operation != null && !ActionOperation.hasExtension(actionData)) {+ return false;+ }++ if (actionData.actionName === ACTION_NAMES.INSERT_ROW_ACTION) {+ const { sheetId, rowCount, rowIndex } = actionData as IInsertRowActionData;+ const namedRanges = Tools.deepClone(this._plugin.getWorkbook().getConfig().namedRanges);++ for (let i = 0; i < namedRanges.length; i++) {+ const namedRangeData = namedRanges[i].range.rangeData;+ if (rowIndex > namedRangeData.endRow) {+ continue;+ } else if (rowIndex >= namedRangeData.startRow && rowIndex <= namedRangeData.endRow) {+ namedRangeData.endRow += rowCount;+ } else {+ namedRangeData.startRow += rowCount;+ namedRangeData.endRow += rowCount;+ }++ const actionData: ISetNamedRangeActionData = {+ actionName: ACTION_NAMES.SET_NAMED_RANGE_ACTION,+ namedRange: namedRanges[i],+ sheetId,+ };+ this.push(actionData);+ }+ }+ });+ }+}++export class NamedRangeActionExtensionFactory extends BaseActionExtensionFactory<SheetPlugin> {+ get zIndex(): number {+ return 3;+ }++ create(actionDataList: ISheetActionData[]): BaseActionExtension<SheetPlugin> {+ return new NamedRangeActionExtension(actionDataList, this._plugin);+ }+}diff --git a/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.ts b/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.tsdeleted file mode 100644index 4d575750d6..0000000000--- a/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.ts+++ /dev/null@@ -1,62 +0,0 @@-import { ACTION_NAMES, BaseActionExtension, BaseActionExtensionFactory, Command, IInsertRowActionData, ISetNamedRangeActionData, ISheetActionData, Tools } from '@univer/core';-import { SheetPlugin } from '../../SheetPlugin';--/**- * TODO insertColumn/insertRange/insertRange/deleteRange- */-export class NamedRangeInsertRowActionExtension extends BaseActionExtension<IInsertRowActionData, SheetPlugin> {- execute() {- const unitId = this._plugin.getContext().getWorkBook().getUnitId();- const sheetId = this.actionData.sheetId;- const commandManager = this._plugin.getContext().getCommandManager();-- const workbook = this._plugin.getContext().getWorkBook();-- const namedRanges = Tools.deepClone(this._plugin.getWorkbook().getConfig().namedRanges);-- const { rowCount, rowIndex } = this.actionData;-- const actionDatas = [];- for (let i = 0; i < namedRanges.length; i++) {- const namedRangeData = namedRanges[i].range.rangeData;- if (rowIndex > namedRangeData.endRow) {- continue;- } else if (rowIndex >= namedRangeData.startRow && rowIndex <= namedRangeData.endRow) {- namedRangeData.endRow += rowCount;- } else {- namedRangeData.startRow += rowCount;- namedRangeData.endRow += rowCount;- }-- const actionData: ISetNamedRangeActionData = {- actionName: ACTION_NAMES.SET_NAMED_RANGE_ACTION,- namedRange: namedRanges[i],- sheetId,- };- actionDatas.push(actionData);- }-- // Execute action- const command = new Command(- {- WorkBookUnit: workbook,- },- ...actionDatas- );- commandManager.invoke(command);- }-}--export class NamedRangeInsertRowActionExtensionFactory extends BaseActionExtensionFactory<IInsertRowActionData, SheetPlugin> {- get zIndex(): number {- return 1;- }-- get actionName(): ACTION_NAMES {- return ACTION_NAMES.INSERT_ROW_ACTION;- }-- create(actionData: IInsertRowActionData, actionDataList: ISheetActionData[]): BaseActionExtension<IInsertRowActionData> {- return new NamedRangeInsertRowActionExtension(actionData, actionDataList, this._plugin);- }-}diff --git a/packages/base-sheets/src/SheetPlugin.tsx b/packages/base-sheets/src/SheetPlugin.tsxindex da53aa9e79..72a99f40bf 100644--- a/packages/base-sheets/src/SheetPlugin.tsx+++ b/packages/base-sheets/src/SheetPlugin.tsx@@ -35,7 +35,7 @@ import { IToolBarItemProps } from './Model/ToolBarModel';import { ModalGroupController } from './Controller/ModalGroupController';import { ISheetPluginConfig, DEFAULT_SPREADSHEET_PLUGIN_DATA } from './Basics';import { FormulaBarController } from './Controller/FormulaBarController';-import { NamedRangeInsertRowActionExtensionFactory } from './Basics/Register/NamedRangeInsertRowActionExtension';+import { NamedRangeActionExtensionFactory } from './Basics/Register/NamedRangeActionExtension';/*** The main sheet base, construct the sheet container and layout, mount the rendering engine@@ -88,7 +88,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {private _componentList: Map<string, any>;- private _namedRangeActionExtensionFactory: NamedRangeInsertRowActionExtensionFactory;+ private _namedRangeActionExtensionFactory: NamedRangeActionExtensionFactory;constructor(config: Partial<ISheetPluginConfig> = {}) {super(PLUGIN_NAMES.SPREADSHEET);@@ -253,7 +253,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {registerExtension() {const actionRegister = this.context.getCommandManager().getActionExtensionManager().getRegister();- this._namedRangeActionExtensionFactory = new NamedRangeInsertRowActionExtensionFactory(this);+ this._namedRangeActionExtensionFactory = new NamedRangeActionExtensionFactory(this);actionRegister.add(this._namedRangeActionExtensionFactory);}diff --git a/packages/core/src/Command/ActionExtensionFactory.ts b/packages/core/src/Command/ActionExtensionFactory.tsindex 2551f3066d..3684f79977 100644--- a/packages/core/src/Command/ActionExtensionFactory.ts+++ b/packages/core/src/Command/ActionExtensionFactory.ts@@ -1,24 +1,11 @@-import { ACTION_NAMES } from '../Const';import { IActionData } from './ActionBase';import { Plugin } from '../Plugin';-import { ActionOperation } from './ActionOperation';/*** Manipulate the list of actions in a command*/-export class BaseActionExtension<- T extends IActionData = IActionData,- P extends Plugin = Plugin-> {- constructor(- protected actionData: T,- protected actionDataList: IActionData[],- protected _plugin: P- ) {}-- getActionData(): T {- return this.actionData;- }+export class BaseActionExtension<T extends Plugin = Plugin> {+ constructor(protected actionDataList: IActionData[], protected _plugin: T) {}getActionDataList(): IActionData[] {return this.actionDataList;@@ -46,48 +33,27 @@ export class BaseActionExtension</*** Determine whether to intercept and create BaseActionExtension*/-export class BaseActionExtensionFactory<- T extends IActionData,- P extends Plugin = Plugin-> {- constructor(protected _plugin: P) {}+export class BaseActionExtensionFactory<T extends Plugin = Plugin> {+ constructor(protected _plugin: T) {}get zIndex() {return 0;}- get actionName(): ACTION_NAMES {- return ACTION_NAMES.CLEAR_RANGE_ACTION;- }-/*** Generate Action Extension- * @param actionData* @param actionDataList* @returns*/- create(actionData: T, actionDataList: IActionData[]): BaseActionExtension<T> {- return new BaseActionExtension(actionData, actionDataList, this._plugin);+ create(actionDataList: IActionData[]): BaseActionExtension<T> {+ return new BaseActionExtension(actionDataList, this._plugin);}/**- * Check if this action needs to be intercepted currently- * @param actionData- * @param actionDataList+ * Intercept actionDataList* @returns*/- check(- actionData: T,- actionDataList: IActionData[]- ): false | BaseActionExtension<T> {- if (- actionData.actionName !== this.actionName ||- (actionData.operation != null &&- !ActionOperation.hasExtension(actionData))- ) {- return false;- }-- return this.create(actionData, actionDataList);+ check(actionDataList: IActionData[]): false | BaseActionExtension<T> {+ return this.create(actionDataList);}}diff --git a/packages/core/src/Command/ActionExtensionManager.ts b/packages/core/src/Command/ActionExtensionManager.tsindex 62fe63f8c5..48c851a20b 100644--- a/packages/core/src/Command/ActionExtensionManager.ts+++ b/packages/core/src/Command/ActionExtensionManager.ts@@ -3,9 +3,7 @@ import { BaseActionExtensionFactory } from './ActionExtensionFactory';import { ActionExtensionRegister } from './ActionExtensionRegister';export class ActionExtensionManager {- private _actionExtensionFactoryList: Array<- BaseActionExtensionFactory<IActionData>- >;+ private _actionExtensionFactoryList: BaseActionExtensionFactory[];// 挂载到实例上private _register: ActionExtensionRegister;@@ -30,6 +28,9 @@ export class ActionExtensionManager {// get the sorted list// get the dynamically added listthis._actionExtensionFactoryList = actionExtensionFactoryList;++ if (actions.length === 0) return;+this._checkExtension(actions);}@@ -41,14 +42,12 @@ export class ActionExtensionManager {private _checkExtension(actions: IActionData[]) {if (!this._actionExtensionFactoryList) return false;- actions.forEach((action) => {- this._actionExtensionFactoryList.forEach((actionExtensionFactory) => {- const extension = actionExtensionFactory.check(action, actions);- // Both formula and formatting need to execute- if (extension !== false) {- extension.execute();- }- });+ this._actionExtensionFactoryList.forEach((actionExtensionFactory) => {+ const extension = actionExtensionFactory.check(actions);+ // Both formula and formatting need to execute+ if (extension !== false) {+ extension.execute();+ }});}}diff --git a/packages/core/src/Command/ActionExtensionRegister.ts b/packages/core/src/Command/ActionExtensionRegister.tsindex c3f9e69d4c..41c8f4d815 100644--- a/packages/core/src/Command/ActionExtensionRegister.ts+++ b/packages/core/src/Command/ActionExtensionRegister.ts@@ -2,16 +2,10 @@ import { sortRules } from '../Shared';import { BaseActionExtensionFactory } from './ActionExtensionFactory';import { REGISTRY_ACTION_FACTORY } from './RegistryFactory';-import { IActionData } from './ActionBase';-export class ActionExtensionRegister {- private _actionExtensionFactoryList: Array<- BaseActionExtensionFactory<IActionData>- > = [];+ private _actionExtensionFactoryList: BaseActionExtensionFactory[] = [];- get actionExtensionFactoryList(): Array<- BaseActionExtensionFactory<IActionData>- > {+ get actionExtensionFactoryList(): BaseActionExtensionFactory[] {return this._actionExtensionFactoryList;}@@ -19,12 +13,12 @@ export class ActionExtensionRegister {this._initExtensions();}- add(...extensionFactoryList: Array<BaseActionExtensionFactory<IActionData>>) {+ add(...extensionFactoryList: BaseActionExtensionFactory[]) {this._actionExtensionFactoryList.push(...extensionFactoryList);this._actionExtensionFactoryList.sort(sortRules);}- delete(extensionFactory: BaseActionExtensionFactory<IActionData>) {+ delete(extensionFactory: BaseActionExtensionFactory) {const index = this._actionExtensionFactoryList.indexOf(extensionFactory);this._actionExtensionFactoryList.splice(index, 1);}diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.tsindex f44eabe194..7e911f957e 100644--- a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts@@ -14,52 +14,67 @@ import {import { FormulaPlugin } from '../../FormulaPlugin';import { ACTION_NAMES as PLUGIN_ACTION_NAMES } from '../Enum';-export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataActionData, FormulaPlugin> {+export class FormulaActionExtension extends BaseActionExtension<FormulaPlugin> {execute() {const engine = this._plugin.getFormulaEngine();const formulaController = this._plugin.getFormulaController();const formulaDataModel = formulaController.getDataModel();+ // The purpose of deep copy is to trigger the modification of formulaData through Command instead of reference relationshipconst formulaData = Tools.deepClone(formulaDataModel.getFormulaData());const unitId = this._plugin.getContext().getWorkBook().getUnitId();- const sheetId = this.actionData.sheetId;- const commandManager = this._plugin.getContext().getCommandManager();+ const actionDataList = this.actionDataList as ISetRangeDataActionData[];- const workBook = this._plugin.getContext().getWorkBook();- let cellValue = this.actionData.cellValue;+ const unitRange: IUnitRange[] = [];- if (Tools.isEmptyObject(formulaData)) return;+ let isCalculate = false;- const cellData = new ObjectMatrix(formulaData[unitId][sheetId]);+ // handle each action+ actionDataList.forEach((actionData) => {+ if (actionData.operation != null && !ActionOperation.hasExtension(actionData)) {+ return false;+ }++ // Filter out Actions that contain formulas, inject setRangeDataAction+ if (actionData.actionName === ACTION_NAMES.SET_RANGE_DATA_ACTION) {+ const { sheetId, cellValue } = actionData;++ if (Tools.isEmptyObject(formulaData)) return;++ const cellData = new ObjectMatrix(formulaData[unitId][sheetId]);++ if (cellValue == null) {+ return;+ }- // cellValue 在公式更新后,还是会触发,并且为空,这里要看看- if (cellValue == null) {- return;- }+ const rangeMatrix = new ObjectMatrix(cellValue);- const rangeMatrix = new ObjectMatrix(cellValue);+ // update formula string,Any modification to cellData will be linked to formulaData+ rangeMatrix.forValue((r, c, cell) => {+ const formulaString = cell.m;+ if (isFormulaString(formulaString)) {+ isCalculate = true;+ cellData.setValue(r, c, {+ formula: formulaString,+ row: r,+ column: c,+ sheetId,+ });+ }+ });- // update formula string- rangeMatrix.forValue((r, c, cell) => {- const formulaString = cell.m;- if (isFormulaString(formulaString)) {- cellData.setValue(r, c, {- formula: formulaString,- row: r,- column: c,+ unitRange.push({+ unitId,sheetId,+ rangeData: rangeMatrix.getDataRange(),});}});- const unitRange: IUnitRange[] = [- {- unitId,- sheetId,- rangeData: rangeMatrix.getDataRange(),- },- ];+ if (unitRange.length === 0 || !isCalculate) return;+ // Add command after calculating the formulaengine.execute(unitId, formulaData, formulaController.toInterpreterCalculateProps(), false, unitRange).then((data) => {+ // TODO arrayFormulaDataconst { sheetData, arrayFormulaData } = data;if (!sheetData) {@@ -102,26 +117,23 @@ export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataActconst setFormulaDataAction = {actionName: PLUGIN_ACTION_NAMES.SET_FORMULA_RANGE_DATA_ACTION,- sheetId: this.actionData.sheetId,+ sheetId: actionDataList[0].sheetId, // Any sheetId can be passed in, it has no practical effectformulaData,};-+ const workBook = this._plugin.getContext().getWorkBook();+ const commandManager = this._plugin.getContext().getCommandManager();const command = new Command({ WorkBookUnit: workBook }, ...actionDatas, setFormulaDataAction);commandManager.invoke(command);});}}-export class FormulaActionExtensionFactory extends BaseActionExtensionFactory<ISetRangeDataActionData, FormulaPlugin> {+export class FormulaActionExtensionFactory extends BaseActionExtensionFactory<FormulaPlugin> {get zIndex(): number {return 1;}- get actionName(): ACTION_NAMES {- return ACTION_NAMES.SET_RANGE_DATA_ACTION;- }-- create(actionData: ISetRangeDataActionData, actionDataList: ISheetActionData[]): BaseActionExtension<ISetRangeDataActionData> {- return new FormulaActionExtension(actionData, actionDataList, this._plugin);+ create(actionDataList: ISheetActionData[]): BaseActionExtension<FormulaPlugin> {+ return new FormulaActionExtension(actionDataList, this._plugin);}}diff --git a/packages/sheets-plugin-numfmt/src/Basic/Register/NumfmtActionExtension.ts b/packages/sheets-plugin-numfmt/src/Basic/Register/NumfmtActionExtension.tsindex 49b927179a..d17fac39ea 100644--- a/packages/sheets-plugin-numfmt/src/Basic/Register/NumfmtActionExtension.ts+++ b/packages/sheets-plugin-numfmt/src/Basic/Register/NumfmtActionExtension.ts@@ -1,63 +1,47 @@-import { ACTION_NAMES, BaseActionExtension, BaseActionExtensionFactory, ISetRangeDataActionData, ObjectMatrix, ISheetActionData, ObjectMatrixPrimitiveType } from '@univer/core';+import { BaseActionExtension, BaseActionExtensionFactory, ISetRangeDataActionData, ObjectMatrix, ISheetActionData, ACTION_NAMES, ActionOperation } 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 '../Enum';import { NumfmtPlugin } from '../../NumfmtPlugin';-export class NumfmtActionExtension extends BaseActionExtension<ISetRangeDataActionData, NumfmtPlugin> {- // constructor(protected actionData: ISetRangeDataActionData) {- // super(actionData);- // }-+export class NumfmtActionExtension extends BaseActionExtension<NumfmtPlugin> {execute() {- // TODOconst numfmtMatrix = new ObjectMatrix<string>();- 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(0, 0, typed.z);+ const actionDataList = this.actionDataList as ISetRangeDataActionData[];+ actionDataList.forEach((actionData) => {+ if (actionData.operation != null && !ActionOperation.hasExtension(actionData)) {+ return false;}- }+ if (actionData.actionName === ACTION_NAMES.SET_RANGE_DATA_ACTION) {+ let { cellValue, sheetId } = actionData;+ const rangeMatrix = new ObjectMatrix(cellValue);++ 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);+ }+ });- const setNumfmtRangeDataAction = {- actionName: PLUGIN_ACTION_NAMES.SET_NUMFMT_RANGE_DATA_ACTION,- sheetId: this.actionData.sheetId,- cellValue: numfmtMatrix.toJSON(),- };- this.push(setNumfmtRangeDataAction);+ const setNumfmtRangeDataAction = {+ actionName: PLUGIN_ACTION_NAMES.SET_NUMFMT_RANGE_DATA_ACTION,+ sheetId,+ cellValue: numfmtMatrix.toJSON(),+ };+ this.push(setNumfmtRangeDataAction);+ }+ });}}-export class NumfmtActionExtensionFactory extends BaseActionExtensionFactory<ISetRangeDataActionData, NumfmtPlugin> {+export class NumfmtActionExtensionFactory extends BaseActionExtensionFactory<NumfmtPlugin> {get zIndex(): number {return 2;}- get actionName(): ACTION_NAMES {- return ACTION_NAMES.SET_RANGE_DATA_ACTION;- }-- create(actionData: ISetRangeDataActionData, actionDataList: ISheetActionData[]): BaseActionExtension<ISetRangeDataActionData> {- return new NumfmtActionExtension(actionData, actionDataList, this._plugin);+ create(actionDataList: ISheetActionData[]): BaseActionExtension<NumfmtPlugin> {+ return new NumfmtActionExtension(actionDataList, this._plugin);}}