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(formula): array form line canvas extension

Dushusir6ceeab5e26e1
diff --git a/packages/base-render/src/Layer.ts b/packages/base-render/src/Layer.ts
index 5cb2278b08..ee79a7cab2 100644
--- a/packages/base-render/src/Layer.ts
+++ b/packages/base-render/src/Layer.ts
@@ -97,6 +97,25 @@ export class Layer {
return this;
}
+ removeObjects(objects: BaseObject[] | string[]) {
+ const allObjects = this.getObjects();
+ const allObjectsLength = allObjects.length;
+
+ for (let i = allObjectsLength; i >= 0; i--) {
+ let o: BaseObject | string = allObjects[i];
+ const objectsLength = objects.length;
+ for (let j = 0; j < objectsLength; j++) {
+ const object = objects[j];
+ o = object instanceof BaseObject ? o : (o as BaseObject).oKey;
+ if (o === object) {
+ allObjects.splice(i, 1);
+ objects.splice(j, 1);
+ break;
+ }
+ }
+ }
+ }
+
clear() {
this._objects = [];
}
diff --git a/packages/base-render/src/Scene.ts b/packages/base-render/src/Scene.ts
index da6993c1b1..c073416308 100644
--- a/packages/base-render/src/Scene.ts
+++ b/packages/base-render/src/Scene.ts
@@ -418,6 +418,17 @@ export class Scene {
return this;
}
+ removeObjects(objects?: BaseObject[] | string[]) {
+ if (objects == null) {
+ return;
+ }
+ const layers = this.getLayers();
+ for (let layer of layers) {
+ layer.removeObjects(objects);
+ }
+ return this;
+ }
+
// addBackObjects(...argument: BaseObject[]) {
// argument.forEach((o: BaseObject) => {
// this.addObjectBack(o);
diff --git a/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts b/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts
index 5b25cfe4a5..c82db4c74b 100644
--- a/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts
+++ b/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts
@@ -18,14 +18,14 @@ import { EventState, Worksheet } from '@univer/core';
import { SheetPlugin } from '../../../SheetPlugin';
import { BaseView, CanvasViewRegistry } from '../BaseView';
-export enum SHEET_VIEW_KEY {
+enum SHEET_VIEW_KEY {
MAIN = '__SpreadsheetRender__Test',
ROW = '__SpreadsheetRowTitle__Test',
COLUMN = '__SpreadsheetColumnTitle__Test',
LEFT_TOP = '__SpreadsheetLeftTopPlaceholder__Test',
}
-export enum CANVAS_VIEW_KEY {
+enum CANVAS_VIEW_KEY {
MAIN_SCENE = 'mainScene_Test',
VIEW_MAIN = 'viewMain_Test',
VIEW_TOP = 'viewTop_Test',
diff --git a/packages/base-sheets/src/View/Render/index.ts b/packages/base-sheets/src/View/Render/index.ts
index 9089079cee..5bc2a7586f 100644
--- a/packages/base-sheets/src/View/Render/index.ts
+++ b/packages/base-sheets/src/View/Render/index.ts
@@ -1,2 +1,3 @@
export * from './BaseView';
export * from './CanvasView';
+export * from './Views';
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index ea228a6c5a..49d1d2e40d 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -3,3 +3,4 @@ export * from './Model';
export * from './Controller';
export * from './Basics';
export * from './View/UI';
+export * from './View/Render';
diff --git a/packages/sheets-plugin-formula/src/Controller/ArrayFormLineController.ts b/packages/sheets-plugin-formula/src/Controller/ArrayFormLineController.ts
new file mode 100644
index 0000000000..82be5a050a
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/Controller/ArrayFormLineController.ts
@@ -0,0 +1,255 @@
+import { PLUGIN_NAMES, Workbook } from '@univer/core';
+import { Path, Scene } from '@univer/base-render';
+import { SheetPlugin, SheetView } from '@univer/base-sheets';
+import { ArrayFormLine, ArrayFormLineModel, IArrayFormLineRange } from '../Model/ArrayFormLineModel';
+import { FormulaPlugin } from '../FormulaPlugin';
+
+enum ARRAY_FORM_LINE_MANAGER_KEY {
+ top = '__ArrayFormLineTopControl__',
+ bottom = '__ArrayFormLineBottomControl__',
+ left = '__ArrayFormLineLeftControl__',
+ right = '__ArrayFormLineRightControl__',
+}
+
+/**
+ * Ant Line Controller
+ */
+export class ArrayFormLineControl {
+ private _arrayFormLineModelList: ArrayFormLineModel[];
+
+ private _sheetPlugin: SheetPlugin;
+
+ private _activeSheetId: string;
+
+ /**
+ * Remove ArrayFormLineModel By SheetId
+ * @param sheetId
+ */
+ private _removeArrayFormLineModelBySheetId(sheetId: string): void {
+ const index = this._arrayFormLineModelList.findIndex((model) => model.getSheetId() !== sheetId);
+ if (index > -1) {
+ this._arrayFormLineModelList.splice(index, 1);
+ }
+ }
+
+ /**
+ * Find ArrayFormLineModel By SheetId
+ * @param sheetId
+ * @returns
+ */
+ private _findArrayFormLineModelBySheetId(sheetId: string, defaultValue: ArrayFormLineModel): ArrayFormLineModel {
+ return this._arrayFormLineModelList.find((model) => model.getSheetId() === sheetId) ?? defaultValue;
+ }
+
+ /**
+ * Save Or Update ArrayFormLineModel By SheetId
+ * @param model
+ * @returns
+ */
+ private _saveOrUpdateArrayFormLineModel(model: ArrayFormLineModel): void {
+ for (let i = 0; i < this._arrayFormLineModelList.length; i++) {
+ const itemModel = this._arrayFormLineModelList[i];
+ if (itemModel.getSheetId() === model.getSheetId()) {
+ this._arrayFormLineModelList[i] = model;
+ return;
+ }
+ }
+ this._arrayFormLineModelList.push(model);
+ }
+
+ /**
+ * Create A Sheet ArrayFormLine Path
+ * @param sheetId
+ * @param range
+ */
+ private _createArrayFormLinePathBySheetIdAndRange(sheetId: string, range: IArrayFormLineRange): Path[] {
+ let workbook = this.getWorkBook();
+ let worksheet = workbook.getSheetBySheetId(sheetId);
+
+ if (worksheet == null) {
+ throw new Error(`not found sheet from id: ${sheetId}`);
+ }
+
+ let rowTitleWidth = worksheet.getConfig().rowTitle.width;
+ let columnTitleHeight = worksheet.getConfig().columnTitle.height;
+
+ let rowManager = worksheet.getRowManager();
+ let columnManager = worksheet.getColumnManager();
+
+ let totalHeight = 0;
+ let totalWidth = 0;
+ for (let i = range.startRow; i < range.endRow; i++) {
+ totalHeight += rowManager.getRowHeight(i);
+ }
+ for (let i = range.startColumn; i < range.endColumn; i++) {
+ totalWidth += columnManager.getColumnWidth(i);
+ }
+
+ let offsetLeft = 0;
+ let offsetTop = 0;
+ for (let i = 0; i <= range.startRow - 1; i++) {
+ offsetTop += rowManager.getRowHeight(i);
+ }
+ for (let i = 0; i <= range.startColumn - 1; i++) {
+ offsetLeft += columnManager.getColumnWidth(i);
+ }
+
+ return [
+ new Path(ARRAY_FORM_LINE_MANAGER_KEY.left, {
+ stroke: 'yellow',
+ strokeWidth: 1,
+ left: offsetLeft + rowTitleWidth,
+ top: offsetTop + columnTitleHeight,
+ height: totalHeight,
+ width: 1,
+ shadowOffsetX: 10,
+ shadowOffsetY: 10,
+ shadowColor: 'LightSkyBlue',
+ shadowBlur: 5,
+ }),
+ new Path(ARRAY_FORM_LINE_MANAGER_KEY.top, {
+ stroke: 'green',
+ strokeWidth: 1,
+ left: offsetLeft + rowTitleWidth,
+ top: offsetTop + columnTitleHeight + totalHeight,
+ height: 1,
+ width: totalWidth,
+ shadowOffsetX: 10,
+ shadowOffsetY: 10,
+ shadowColor: 'LightSkyBlue',
+ shadowBlur: 5,
+ }),
+ new Path(ARRAY_FORM_LINE_MANAGER_KEY.right, {
+ stroke: 'yellow',
+ strokeWidth: 1,
+ left: offsetLeft + rowTitleWidth + totalWidth,
+ top: offsetTop + columnTitleHeight,
+ height: totalHeight,
+ width: 1,
+ shadowOffsetX: 10,
+ shadowOffsetY: 10,
+ shadowColor: 'LightSkyBlue',
+ shadowBlur: 5,
+ }),
+ new Path(ARRAY_FORM_LINE_MANAGER_KEY.bottom, {
+ stroke: 'green',
+ strokeWidth: 1,
+ left: offsetLeft + rowTitleWidth,
+ top: offsetTop + columnTitleHeight + totalHeight,
+ height: 1,
+ width: totalWidth,
+ shadowOffsetX: 10,
+ shadowOffsetY: 10,
+ shadowColor: 'LightSkyBlue',
+ shadowBlur: 5,
+ }),
+ ];
+ }
+
+ /**
+ * Delete ArrayFormLine Canvas UI
+ */
+ private _deleteSceneAllArrayFormLinePath(): void {
+ for (let i = 0; i < this._arrayFormLineModelList.length; i++) {
+ let arrayFormLineModel = this._arrayFormLineModelList[i];
+ let arrayFormLineList = arrayFormLineModel.getArrayFormLineList();
+ for (let j = 0; j < arrayFormLineList.length; j++) {
+ let arrayFormLine = arrayFormLineList[j];
+ let antPath = arrayFormLine.getPath();
+ if (antPath) {
+ this.getSheetViewScene().removeObjects(antPath);
+ }
+ }
+ }
+ }
+
+ /**
+ * Make Update ArrayFormLine Canvas UI
+ */
+ private _makeUpdateSceneArrayFormLinePath(): void {
+ this._deleteSceneAllArrayFormLinePath();
+ for (let i = 0; i < this._arrayFormLineModelList.length; i++) {
+ let arrayFormLineModel = this._arrayFormLineModelList[i];
+ let arrayFormLineList = arrayFormLineModel.getArrayFormLineList();
+ if (arrayFormLineModel.getSheetId() !== this._activeSheetId) {
+ continue;
+ }
+ for (let j = 0; j < arrayFormLineList.length; j++) {
+ let arrayFormLine = arrayFormLineList[j];
+ let antPath = this._createArrayFormLinePathBySheetIdAndRange(arrayFormLineModel.getSheetId(), arrayFormLine.getRange());
+ arrayFormLine.setPath(antPath);
+ console.log(antPath);
+ this.getSheetViewScene().addObjects(antPath);
+ }
+ }
+ }
+
+ /**
+ * Create ArrayFormLineController
+ * @param plugin
+ */
+ constructor(private _plugin: FormulaPlugin) {
+ this._sheetPlugin = this._plugin.getContext().getPluginManager().getPluginByName<SheetPlugin>(PLUGIN_NAMES.SPREADSHEET)!;
+
+ this._arrayFormLineModelList = [];
+ this._sheetPlugin
+ .getContext()
+ .getContextObserver('onAfterChangeActiveSheetObservable')
+ .add(() => {
+ this._activeSheetId = this._sheetPlugin.getWorkbook().getActiveSheet().getSheetId();
+ this._makeUpdateSceneArrayFormLinePath();
+ });
+ this._sheetPlugin
+ .getContext()
+ .getContextObserver('onSheetRenderDidMountObservable')
+ .add(() => {
+ this._activeSheetId = this._sheetPlugin.getWorkbook().getActiveSheet().getSheetId();
+ this._makeUpdateSceneArrayFormLinePath();
+ });
+ }
+
+ /**
+ * Remove ArrayFormLine By SheetId
+ * @param sheetId
+ */
+ removeSheetArrayFormLine(sheetId: string = this._activeSheetId): void {
+ this._removeArrayFormLineModelBySheetId(sheetId);
+ this._makeUpdateSceneArrayFormLinePath();
+ }
+
+ /**
+ * Add New ArrayFormLine By SheetId
+ * @param sheetId
+ * @param range
+ */
+ addArrayFormLineToSheet(range: IArrayFormLineRange, sheetId: string = this._activeSheetId): void {
+ const arrayFormLineModel = this._findArrayFormLineModelBySheetId(sheetId, new ArrayFormLineModel(sheetId));
+ arrayFormLineModel.addArrayFormLine(new ArrayFormLine(range));
+ this._saveOrUpdateArrayFormLineModel(arrayFormLineModel);
+ this._makeUpdateSceneArrayFormLinePath();
+ }
+
+ /**
+ * Return SheetView
+ * @returns
+ */
+ getSheetView(): SheetView {
+ return this._sheetPlugin.getCanvasView().getSheetView();
+ }
+
+ /**
+ * Return WorkBook
+ * @returns Workbook
+ */
+ getWorkBook(): Workbook {
+ return this._sheetPlugin.getWorkbook();
+ }
+
+ /**
+ * Return SheetView Scene
+ * @returns
+ */
+ getSheetViewScene(): Scene {
+ return this._sheetPlugin.getCanvasView().getSheetView().getScene();
+ }
+}
diff --git a/packages/sheets-plugin-formula/src/FormulaPlugin.tsx b/packages/sheets-plugin-formula/src/FormulaPlugin.tsx
index 5db4d5711f..dd690164c6 100644
--- a/packages/sheets-plugin-formula/src/FormulaPlugin.tsx
+++ b/packages/sheets-plugin-formula/src/FormulaPlugin.tsx
@@ -13,6 +13,7 @@ import { FormulaActionExtensionFactory } from './Basic/Register';
import { FormulaPluginObserve, install } from './Basic/Observer';
import { SearchFormulaController } from './Controller/SearchFormulaModalController';
import { FormulaPromptController } from './Controller/FormulaPromptController';
+import { ArrayFormLineControl } from './Controller/ArrayFormLineController';
export class FormulaPlugin extends Plugin<FormulaPluginObserve, SheetContext> {
private _formulaController: FormulaController;
@@ -21,6 +22,8 @@ export class FormulaPlugin extends Plugin<FormulaPluginObserve, SheetContext> {
private _formulaPromptController: FormulaPromptController;
+ private _arrayFormLineControl: ArrayFormLineControl;
+
protected _formulaActionExtensionFactory: FormulaActionExtensionFactory;
constructor(private _config?: IFormulaConfig) {
@@ -62,6 +65,18 @@ export class FormulaPlugin extends Plugin<FormulaPluginObserve, SheetContext> {
});
this.registerExtension();
+
+ this._arrayFormLineControl = new ArrayFormLineControl(this);
+
+ this._arrayFormLineControl.addArrayFormLineToSheet(
+ {
+ startRow: 1,
+ endRow: 3,
+ startColumn: 1,
+ endColumn: 3,
+ },
+ 'sheet-0004'
+ );
}
onMapping(IOC: IOCContainer): void {}
diff --git a/packages/sheets-plugin-formula/src/Model/ArrayFormLineModel.ts b/packages/sheets-plugin-formula/src/Model/ArrayFormLineModel.ts
new file mode 100644
index 0000000000..c832bed057
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/Model/ArrayFormLineModel.ts
@@ -0,0 +1,69 @@
+import { Path } from '@univer/base-render';
+import { IRangeData, Nullable } from '@univer/core';
+
+export interface IArrayFormLineRange extends IRangeData {
+ id?: string;
+}
+
+export class ArrayFormLine {
+ private _range: IArrayFormLineRange;
+
+ private _rect: Nullable<Path[]>;
+
+ constructor(range: IArrayFormLineRange) {
+ this._range = range;
+ }
+
+ getRange(): IArrayFormLineRange {
+ return this._range;
+ }
+
+ getPath(): Nullable<Path[]> {
+ return this._rect;
+ }
+
+ setPath(rect: Path[]): void {
+ this._rect = rect;
+ }
+
+ setRange(range: IArrayFormLineRange): void {
+ this._range = range;
+ }
+}
+
+export class ArrayFormLineModel {
+ private _sheetId: string;
+
+ private _arrayFormLineList: ArrayFormLine[];
+
+ constructor(sheetId: string) {
+ this._sheetId = sheetId;
+ this._arrayFormLineList = [];
+ }
+
+ deleteArrayFormLineById(id: string): void {
+ const index = this._arrayFormLineList.findIndex((arrayFormLine) => arrayFormLine.getRange().id === id);
+ if (index > -1) {
+ this._arrayFormLineList.splice(index, 1);
+ }
+ }
+
+ addArrayFormLine(arrayFormLine: ArrayFormLine): void {
+ this._arrayFormLineList.push(arrayFormLine);
+ }
+
+ getSheetId(): string {
+ return this._sheetId;
+ }
+
+ getArrayFormLineList(): ArrayFormLine[] {
+ return this._arrayFormLineList;
+ }
+
+ getArrayFormLineById(id: string): Nullable<ArrayFormLine> {
+ const index = this._arrayFormLineList.findIndex((arrayFormLine) => arrayFormLine.getRange().id === id);
+ if (index > -1) {
+ return this._arrayFormLineList[index];
+ }
+ }
+}