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(sheet): Selection in sceneViewer

DR-Univer5e359ce13088
diff --git a/packages/base-formula-engine/src/Interpreter/Interpreter.ts b/packages/base-formula-engine/src/Interpreter/Interpreter.ts
index dae6226534..a2c62a515c 100644
--- a/packages/base-formula-engine/src/Interpreter/Interpreter.ts
+++ b/packages/base-formula-engine/src/Interpreter/Interpreter.ts
@@ -190,6 +190,14 @@ export class Interpreter {
return this._unitArrayFormulaData[unitId];
}
+ getUnitData() {
+ return this._runtimeData;
+ }
+
+ getUnitArrayFormula() {
+ return this._unitArrayFormulaData;
+ }
+
setProps(interpreterDatasetConfig: IInterpreterDatasetConfig) {
this._interpreterDatasetConfig = interpreterDatasetConfig;
}
diff --git a/packages/base-render/src/Component/Sheets/Selection/SelectionControl.ts b/packages/base-render/src/Component/Sheets/Selection/SelectionControl.ts
new file mode 100644
index 0000000000..9bade377f2
--- /dev/null
+++ b/packages/base-render/src/Component/Sheets/Selection/SelectionControl.ts
@@ -0,0 +1,385 @@
+import { Nullable, ISelection, ICellInfo } from '@univer/core';
+import { SelectionModel } from './SelectionModel';
+import { Rect } from '../../../Shape/Rect';
+import { Group } from '../../../Group';
+import { Scene } from '../../../Scene';
+
+// export enum DEFAULT_SELECTION_CONFIG {
+// strokeColor = 'rgb(1,136,251)',
+// backgroundColor = 'rgba(1,136,251, 0.1)',
+// strokeWidth = 2,
+// fillSideLength = 6,
+// fillStrokeLength = 1,
+// fillStrokeColor = 'rgb(255,255,255)',
+// }
+export const DEFAULT_SELECTION_CONFIG = {
+ strokeColor: 'rgb(1,136,251)',
+ backgroundColor: 'rgba(1,136,251, 0.1)',
+ strokeWidth: 2,
+ fillSideLength: 6,
+ fillStrokeLength: 1,
+ fillStrokeColor: 'rgb(255,255,255)',
+};
+
+export enum SELECTION_TYPE {
+ NORMAL,
+ ROW,
+ COLUMN,
+}
+
+enum SELECTION_MANAGER_KEY {
+ Selection = '__SpreadsheetSelectionShape__',
+ top = '__SpreadsheetSelectionTopControl__',
+ bottom = '__SpreadsheetSelectionBottomControl__',
+ left = '__SpreadsheetSelectionShapeLeftControl__',
+ right = '__SpreadsheetSelectionShapeRightControl__',
+ backgroundTop = '__SpreadsheetSelectionBackgroundControlTop__',
+ backgroundMiddleLeft = '__SpreadsheetSelectionBackgroundControlMiddleLeft__',
+ backgroundMiddleRight = '__SpreadsheetSelectionBackgroundControlMiddleRight__',
+ backgroundBottom = '__SpreadsheetSelectionBackgroundControlBottom__',
+ fill = '__SpreadsheetSelectionFillControl__',
+ lineMain = '__SpreadsheetDragLineMainControl__',
+ lineContent = '__SpreadsheetDragLineContentControl__',
+ line = '__SpreadsheetDragLineControl__',
+}
+
+/**
+ * The main selection canvas component
+ */
+export class SelectionControl {
+ private _leftControl: Rect;
+
+ private _rightControl: Rect;
+
+ private _topControl: Rect;
+
+ private _bottomControl: Rect;
+
+ private _backgroundControlTop: Rect;
+
+ private _backgroundControlBottom: Rect;
+
+ private _backgroundControlMiddleLeft: Rect;
+
+ private _backgroundControlMiddleRight: Rect;
+
+ private _fillControl: Rect;
+
+ private _selectionShape: Group;
+
+ private _selectionModel: SelectionModel;
+
+ constructor(private _scene: Scene, private _zIndex: number) {
+ this._initialize();
+ }
+
+ private _initialize() {
+ this._selectionModel = new SelectionModel(SELECTION_TYPE.NORMAL);
+ const zIndex = this._zIndex;
+ this._leftControl = new Rect(SELECTION_MANAGER_KEY.left + zIndex, {
+ top: 0,
+ left: -DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
+ width: DEFAULT_SELECTION_CONFIG.strokeWidth,
+ fill: DEFAULT_SELECTION_CONFIG.strokeColor,
+ zIndex,
+ });
+ this._rightControl = new Rect(SELECTION_MANAGER_KEY.right + zIndex, {
+ width: DEFAULT_SELECTION_CONFIG.strokeWidth,
+ fill: DEFAULT_SELECTION_CONFIG.strokeColor,
+ zIndex,
+ });
+ this._topControl = new Rect(SELECTION_MANAGER_KEY.top + zIndex, {
+ top: -DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
+ left: -DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
+ height: DEFAULT_SELECTION_CONFIG.strokeWidth,
+ fill: DEFAULT_SELECTION_CONFIG.strokeColor,
+ zIndex,
+ });
+ this._bottomControl = new Rect(SELECTION_MANAGER_KEY.bottom + zIndex, {
+ height: DEFAULT_SELECTION_CONFIG.strokeWidth,
+ fill: DEFAULT_SELECTION_CONFIG.strokeColor,
+ left: -DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
+ zIndex,
+ });
+ this._backgroundControlTop = new Rect(SELECTION_MANAGER_KEY.backgroundTop + zIndex, {
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
+ zIndex: zIndex - 1,
+ evented: false,
+ });
+ this._backgroundControlBottom = new Rect(SELECTION_MANAGER_KEY.backgroundBottom + zIndex, {
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
+ zIndex: zIndex - 1,
+ evented: false,
+ });
+ this._backgroundControlMiddleLeft = new Rect(SELECTION_MANAGER_KEY.backgroundMiddleLeft + zIndex, {
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
+ zIndex: zIndex - 1,
+ evented: false,
+ });
+ this._backgroundControlMiddleRight = new Rect(SELECTION_MANAGER_KEY.backgroundMiddleRight + zIndex, {
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
+ zIndex: zIndex - 1,
+ evented: false,
+ });
+ const fillSideLength = DEFAULT_SELECTION_CONFIG.fillSideLength - DEFAULT_SELECTION_CONFIG.fillStrokeLength;
+ this._fillControl = new Rect(SELECTION_MANAGER_KEY.fill + zIndex, {
+ width: fillSideLength,
+ height: fillSideLength,
+ fill: DEFAULT_SELECTION_CONFIG.strokeColor,
+ strokeWidth: DEFAULT_SELECTION_CONFIG.fillStrokeLength,
+ stroke: DEFAULT_SELECTION_CONFIG.fillStrokeColor,
+ zIndex: zIndex + 1,
+ });
+ this._selectionShape = new Group(
+ SELECTION_MANAGER_KEY.Selection + zIndex,
+ this._fillControl,
+ this._leftControl,
+ this._rightControl,
+ this._topControl,
+ this._bottomControl,
+ this._backgroundControlTop,
+ this._backgroundControlMiddleLeft,
+ this._backgroundControlMiddleRight,
+ this._backgroundControlBottom
+ );
+
+ this._selectionShape.hide();
+
+ this._selectionShape.evented = false;
+
+ this._selectionShape.zIndex = zIndex;
+
+ const scene = this.getScene();
+ scene.addObject(this._selectionShape);
+ }
+
+ get zIndex() {
+ return this._zIndex;
+ }
+
+ /**
+ * just handle the view
+ *
+ * inner update
+ */
+ _updateControl() {
+ const { startX, startY, endX, endY } = this._selectionModel;
+
+ this.leftControl.resize(undefined, endY - startY);
+ this.rightControl.transformByState({
+ height: endY - startY,
+ left: endX - startX - DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
+ });
+ this.topControl.resize(endX - startX + DEFAULT_SELECTION_CONFIG.strokeWidth);
+ this.bottomControl.transformByState({
+ width: endX - startX + DEFAULT_SELECTION_CONFIG.strokeWidth,
+ top: endY - startY - DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
+ });
+
+ this.fillControl.translate(endX - startX - DEFAULT_SELECTION_CONFIG.fillSideLength / 2, endY - startY - DEFAULT_SELECTION_CONFIG.fillSideLength / 2);
+
+ this._updateBackgroundControl();
+
+ this.selectionShape.show();
+ this.selectionShape.translate(startX, startY);
+
+ this.selectionShape.makeDirty(true);
+ }
+
+ private _updateBackgroundControl() {
+ const {
+ startColumn,
+ startRow,
+ endColumn,
+ endRow,
+
+ startX,
+ startY,
+ endX,
+ endY,
+ } = this._selectionModel;
+
+ const highlightSelection = this._selectionModel.highlightToSelection();
+
+ if (!highlightSelection) {
+ this._backgroundControlTop.resize(endX - startX, endY - startY);
+ this._backgroundControlBottom.resize(0, 0);
+ this._backgroundControlMiddleLeft.resize(0, 0);
+ this._backgroundControlMiddleRight.resize(0, 0);
+ return;
+ }
+
+ const { startX: h_startX, startY: h_startY, endX: h_endX, endY: h_endY } = highlightSelection;
+
+ const strokeOffset = DEFAULT_SELECTION_CONFIG.strokeWidth / 2;
+
+ const topConfig = {
+ left: -strokeOffset,
+ top: -strokeOffset,
+ width: endX - startX + strokeOffset * 2,
+ height: h_startY - startY + strokeOffset,
+ };
+ if (topConfig.height < 0) {
+ topConfig.width = 0;
+ topConfig.height = 0;
+ }
+ this._backgroundControlTop.transformByState(topConfig);
+
+ const middleLeftConfig = {
+ left: -strokeOffset,
+ top: h_startY - startY,
+ width: h_startX - startX + strokeOffset,
+ height: h_endY - h_startY,
+ };
+ if (middleLeftConfig.width < 0) {
+ middleLeftConfig.width = 0;
+ middleLeftConfig.height = 0;
+ }
+ this._backgroundControlMiddleLeft.transformByState(middleLeftConfig);
+
+ const middleRightConfig = {
+ left: h_endX - startX - strokeOffset,
+ top: h_startY - startY,
+ width: endX - h_endX + strokeOffset * 2,
+ height: h_endY - h_startY,
+ };
+ if (middleRightConfig.width < 0) {
+ middleRightConfig.width = 0;
+ middleRightConfig.height = 0;
+ }
+ this._backgroundControlMiddleRight.transformByState(middleRightConfig);
+
+ const middleBottomConfig = {
+ left: -strokeOffset,
+ top: h_endY - startY,
+ width: endX - startX + strokeOffset * 2,
+ height: endY - h_endY + strokeOffset,
+ };
+ if (middleBottomConfig.height < 0) {
+ middleBottomConfig.width = 0;
+ middleBottomConfig.height = 0;
+ }
+ this._backgroundControlBottom.transformByState(middleBottomConfig);
+ }
+
+ update(newSelectionRange: ISelection, highlight: Nullable<ICellInfo>) {
+ this._selectionModel.setValue(newSelectionRange, highlight);
+ this._updateControl();
+ }
+
+ clearHighlight() {
+ this._selectionModel.clearCurrentCell();
+ this._updateControl();
+ }
+
+ getScene() {
+ return this._scene;
+ }
+
+ dispose() {
+ this._leftControl?.dispose();
+ this._rightControl?.dispose();
+ this._topControl?.dispose();
+ this._bottomControl?.dispose();
+ this._backgroundControlTop?.dispose();
+ this._backgroundControlMiddleLeft?.dispose();
+ this._backgroundControlMiddleRight?.dispose();
+ this._backgroundControlBottom?.dispose();
+ this._fillControl?.dispose();
+ this._selectionShape?.dispose();
+ }
+
+ get leftControl() {
+ return this._leftControl;
+ }
+
+ get rightControl() {
+ return this._rightControl;
+ }
+
+ get topControl() {
+ return this._topControl;
+ }
+
+ get bottomControl() {
+ return this._bottomControl;
+ }
+
+ get fillControl() {
+ return this._fillControl;
+ }
+
+ get backgroundControlTop() {
+ return this._backgroundControlTop;
+ }
+
+ get backgroundControlBottom() {
+ return this._backgroundControlBottom;
+ }
+
+ get backgroundControlMiddleLeft() {
+ return this._backgroundControlMiddleLeft;
+ }
+
+ get backgroundControlMiddleRight() {
+ return this._backgroundControlMiddleRight;
+ }
+
+ get selectionShape() {
+ return this._selectionShape;
+ }
+
+ get model() {
+ return this._selectionModel;
+ }
+
+ /**
+ * Get the cell information of the current selection, considering the case of merging cells
+ */
+ getCurrentCellInfo(): Nullable<ISelection> {
+ const currentCell = this.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,
+ startX: mergeInfo.startX,
+ endX: mergeInfo.endX,
+ startY: mergeInfo.startY,
+ endY: mergeInfo.endY,
+ };
+ } else {
+ const { row, column, startX, endX, startY, endY } = currentCell;
+ currentRangeData = {
+ startRow: row,
+ endRow: row,
+ startColumn: column,
+ endColumn: column,
+ startX,
+ endX,
+ startY,
+ endY,
+ };
+ }
+
+ return currentRangeData;
+ }
+ }
+
+ static create(scene: Scene, zIndex: number) {
+ return new this(scene, zIndex);
+ }
+
+ static fromJson(scene: Scene, zIndex: number, newSelectionRange: ISelection) {
+ const control = SelectionControl.create(scene, zIndex);
+ control.update(newSelectionRange);
+ return control;
+ }
+}
diff --git a/packages/base-render/src/Component/Sheets/Selection/SelectionManager.ts b/packages/base-render/src/Component/Sheets/Selection/SelectionManager.ts
new file mode 100644
index 0000000000..a0bc1fee2d
--- /dev/null
+++ b/packages/base-render/src/Component/Sheets/Selection/SelectionManager.ts
@@ -0,0 +1,418 @@
+import { Nullable, Observer, ISelection, makeCellToSelection, IRangeData, IRangeCellData } from '@univer/core';
+
+import { SelectionControl } from './SelectionControl';
+import { ScrollTimer } from '../../../ScrollTimer';
+import { IMouseEvent, IPointerEvent } from '../../../Basics/IEvents';
+import { Spreadsheet } from '../Spreadsheet';
+import { SelectionModel } from './SelectionModel';
+import { Vector2 } from '../../../Basics/Vector2';
+import { Scene } from '../../../Scene';
+
+/**
+ * TODO 注册selection拦截,可能在有公式ArrayObject时,fx公式栏显示不同
+ *
+ * SelectionManager 维护model数据list,action也是修改这一层数据,obs监听到数据变动后,自动刷新(control仍然可以持有数据)
+ */
+export class SelectionManager {
+ private _moveObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
+
+ private _upObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
+
+ private _selectionControls: SelectionControl[] = []; // sheetID:Controls
+
+ private _startSelectionRange: ISelection;
+
+ private _startOffsetX: number = 0;
+
+ private _startOffsetY: number = 0;
+
+ private _scrollTimer: ScrollTimer;
+
+ private _cancelDownObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
+
+ private _cancelUpObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
+
+ hasSelection: boolean = false;
+
+ constructor(private _spreadsheet: Spreadsheet) {
+ this._mainEventInitial();
+ }
+
+ /**
+ * add a selection
+ * @param selectionRange
+ * @param curCellRange
+ * @returns
+ */
+ addControlToCurrentByRangeData(selectionRange: IRangeData, curCellRange: Nullable<IRangeCellData>) {
+ const currentControls = this.getCurrentControls();
+ if (!currentControls) {
+ return;
+ }
+ const main = this._spreadsheet;
+
+ const control = SelectionControl.create(this.getScene(), currentControls.length);
+
+ let cellInfo = null;
+ if (curCellRange) {
+ cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);
+ }
+
+ const { startRow, startColumn, endRow, endColumn } = selectionRange;
+ const startCell = main.getNoMergeCellPositionByIndex(startRow, startColumn);
+ const endCell = main.getNoMergeCellPositionByIndex(endRow, endColumn);
+
+ // update control
+ control.update(
+ {
+ startColumn,
+ startRow,
+ endColumn,
+ endRow,
+ startY: startCell?.startY || 0,
+ endY: endCell?.endY || 0,
+ startX: startCell?.startX || 0,
+ endX: endCell?.endX || 0,
+ },
+ cellInfo
+ );
+
+ currentControls.push(control);
+ }
+
+ getSpreadsheet() {
+ return this._spreadsheet;
+ }
+
+ getMaxIndex() {
+ return this._spreadsheet.zIndex + 1;
+ }
+
+ getScene() {
+ return this._spreadsheet.getScene();
+ }
+
+ getSkeleton() {
+ return this._spreadsheet.getSkeleton();
+ }
+
+ getRowAndColumnCount() {
+ const skeleton = this.getSkeleton();
+ return {
+ rowCount: skeleton?.getRowCount() || 1000,
+ columnCount: skeleton?.getColumnCount() || 50,
+ };
+ }
+
+ getCurrentControls() {
+ return this._selectionControls;
+ }
+
+ getCurrentControl() {
+ const controls = this.getCurrentControls();
+ if (controls && controls.length > 0) {
+ for (const control of controls) {
+ const currentCell = control.model.currentCell;
+ if (currentCell) {
+ return control;
+ }
+ }
+ }
+ }
+
+ clearSelectionControls() {
+ let curControls = this.getCurrentControls();
+
+ if (curControls.length > 0) {
+ for (let control of curControls) {
+ control.dispose();
+ }
+
+ curControls.length = 0; // clear currentSelectionControls
+ }
+ }
+
+ /**
+ * Returns the list of active ranges in the active sheet or null if there are no active ranges.
+ * If there is a single range selected, this behaves as a getActiveRange() call.
+ *
+ * @returns
+ */
+ getActiveRangeList(): Nullable<IRangeData[]> {
+ const controls = this.getCurrentControls();
+ if (controls && controls.length > 0) {
+ const selections = controls?.map((control: SelectionControl) => {
+ const model: SelectionModel = control.model;
+ return {
+ startRow: model.startRow,
+ startColumn: model.startColumn,
+ endRow: model.endRow,
+ endColumn: model.endColumn,
+ };
+ });
+ return selections;
+ }
+ }
+
+ /**
+ * Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.
+ * TODO: 默认最后一个选区为当前激活选区,或者当前激活单元格所在选区为激活选区
+ * @returns
+ */
+ getActiveRange(): Nullable<IRangeData> {
+ const controls = this.getCurrentControls();
+ const model = controls && controls[controls.length - 1].model;
+ return (
+ model && {
+ startRow: model.startRow,
+ startColumn: model.startColumn,
+ endRow: model.endRow,
+ endColumn: model.endColumn,
+ }
+ );
+ }
+
+ /**
+ * get active selection control
+ * @returns
+ */
+ getActiveSelection(): Nullable<SelectionControl> {
+ const controls = this.getCurrentControls();
+ return controls && controls[controls.length - 1];
+ }
+
+ dispose() {
+ this.clearSelectionControls();
+ this._moveObserver = null;
+ this._upObserver = null;
+ }
+
+ /**
+ * When mousedown and mouseup need to go to the coordination and undo stack, when mousemove does not need to go to the coordination and undo stack
+ * @param moveEvt
+ * @param selectionControl
+ * @returns
+ */
+ private _moving(moveOffsetX: number, moveOffsetY: number, selectionControl: Nullable<SelectionControl>) {
+ // console.log('moving');
+ const main = this._spreadsheet;
+ // const { offsetX: moveOffsetX, offsetY: moveOffsetY } = moveEvt;
+ const { startRow, startColumn, endRow, endColumn } = this._startSelectionRange;
+
+ // const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);
+ // const scene = this.getScene() as Scene;
+
+ // const coord = scene.getRelativeCoord(Vector2.FromArray([moveOffsetX, moveOffsetY]));
+ // const { x: newMoveOffsetX, y: newMoveOffsetY } = coord;
+
+ const scrollXY = main.getScrollXYByRelativeCoords(Vector2.FromArray([this._startOffsetX, this._startOffsetY]));
+
+ const moveCellInfo = main.calculateCellIndexByPosition(moveOffsetX, moveOffsetY, scrollXY);
+ const moveActualSelection = makeCellToSelection(moveCellInfo);
+
+ if (!moveActualSelection) {
+ return false;
+ }
+ const { startRow: moveStartRow, startColumn: moveStartColumn, endColumn: moveEndColumn, endRow: moveEndRow } = moveActualSelection;
+
+ const newStartRow = Math.min(moveStartRow, startRow);
+ const newStartColumn = Math.min(moveStartColumn, startColumn);
+ const newEndRow = Math.max(moveEndRow, endRow);
+ const newEndColumn = Math.max(moveEndColumn, endColumn);
+ const newBounding = main.getSelectionBounding(newStartRow, newStartColumn, newEndRow, newEndColumn);
+
+ if (!newBounding) {
+ return false;
+ }
+ const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = newBounding;
+
+ const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);
+ const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);
+
+ const newSelectionRange: ISelection = {
+ startColumn: finalStartColumn,
+ startRow: finalStartRow,
+ endColumn: finalEndColumn,
+ endRow: finalEndRow,
+ startY: startCell?.startY || 0,
+ endY: endCell?.endY || 0,
+ startX: startCell?.startX || 0,
+ endX: endCell?.endX || 0,
+ };
+ // Only notify when the selection changes
+ const {
+ startRow: oldStartRow,
+ endRow: oldEndRow,
+ startColumn: oldStartColumn,
+ endColumn: oldEndColumn,
+ } = selectionControl?.model || { startRow: -1, endRow: -1, startColumn: -1, endColumn: -1 };
+
+ if (oldStartColumn !== finalStartColumn || oldStartRow !== finalStartRow || oldEndColumn !== finalEndColumn || oldEndRow !== finalEndRow) {
+ selectionControl && selectionControl.update(newSelectionRange);
+ }
+ }
+
+ private _endSelection(scene: Scene) {
+ scene.onPointerMoveObserver.remove(this._moveObserver);
+ scene.onPointerUpObserver.remove(this._upObserver);
+ scene.enableEvent();
+
+ this._scrollTimer?.stopScroll();
+
+ const mainScene = scene.getEngine()?.activeScene;
+ mainScene?.onPointerDownObserver.remove(this._cancelDownObserver);
+ mainScene?.onPointerUpObserver.remove(this._cancelUpObserver);
+ }
+
+ private _addCancelObserver() {
+ const scene = this.getScene() as Scene;
+ const mainScene = scene.getEngine()?.activeScene;
+ if (mainScene == null || mainScene === scene) {
+ return;
+ }
+ mainScene.onPointerDownObserver.remove(this._cancelDownObserver);
+ mainScene.onPointerUpObserver.remove(this._cancelUpObserver);
+ this._cancelDownObserver = mainScene.onPointerDownObserver.add((moveEvt: IPointerEvent | IMouseEvent) => {
+ this._endSelection(scene);
+ });
+
+ this._cancelUpObserver = mainScene.onPointerUpObserver.add((moveEvt: IPointerEvent | IMouseEvent) => {
+ this._endSelection(scene);
+ });
+ }
+
+ private _mainEventInitial() {
+ const main = this._spreadsheet;
+ main.onPointerDownObserver.add((evt: IPointerEvent | IMouseEvent, state) => {
+ const { offsetX: evtOffsetX, offsetY: evtOffsetY } = evt;
+
+ const scene = this.getScene() as Scene;
+
+ const relativeCoords = scene.getRelativeCoord(Vector2.FromArray([evtOffsetX, evtOffsetY]));
+
+ const { x: newEvtOffsetX, y: newEvtOffsetY } = relativeCoords;
+
+ this._startOffsetX = newEvtOffsetX;
+ this._startOffsetY = newEvtOffsetY;
+
+ const scrollXY = main.getScrollXYByRelativeCoords(relativeCoords);
+
+ const cellInfo = main.calculateCellIndexByPosition(newEvtOffsetX, newEvtOffsetY, scrollXY);
+ const actualSelection = makeCellToSelection(cellInfo);
+ if (!actualSelection) {
+ return false;
+ }
+
+ const { startRow, startColumn, endColumn, endRow, startY, endY, startX, endX } = actualSelection;
+
+ const startSelectionRange = {
+ startColumn,
+ startRow,
+ endColumn,
+ endRow,
+ startY,
+ endY,
+ startX,
+ endX,
+ };
+
+ this._startSelectionRange = startSelectionRange;
+
+ let selectionControl: Nullable<SelectionControl> = this.getCurrentControl();
+
+ let curControls = this.getCurrentControls();
+
+ if (!curControls) {
+ return false;
+ }
+
+ for (let control of curControls) {
+ // right click
+ if (evt.button === 2 && control.model.isInclude(startSelectionRange)) {
+ selectionControl = control;
+ return;
+ }
+ // Click to an existing selection
+ if (control.model.isEqual(startSelectionRange)) {
+ selectionControl = control;
+ break;
+ }
+
+ // There can only be one highlighted cell, so clear the highlighted cell of the existing selection
+ if (!evt.shiftKey) {
+ control.clearHighlight();
+ }
+ }
+
+ // In addition to pressing the ctrl or shift key, we must clear the previous selection
+ if (curControls.length > 0 && !evt.ctrlKey && !evt.shiftKey) {
+ for (let control of curControls) {
+ control.dispose();
+ }
+
+ curControls.length = 0; // clear currentSelectionControls
+ }
+
+ const currentCell = selectionControl && selectionControl.model.currentCell;
+
+ if (selectionControl && evt.shiftKey && currentCell) {
+ const { row, column } = currentCell;
+
+ // TODO startCell position calculate error
+ const startCell = main.getNoMergeCellPositionByIndex(row, column);
+ const endCell = main.getNoMergeCellPositionByIndex(endRow, endColumn);
+
+ const newSelectionRange = {
+ startColumn: column,
+ startRow: row,
+ endColumn: startSelectionRange.startColumn,
+ endRow: startSelectionRange.startRow,
+ startY: startCell?.startY || 0,
+ endY: endCell?.endY || 0,
+ startX: startCell?.startX || 0,
+ endX: endCell?.endX || 0,
+ };
+ selectionControl.update(newSelectionRange, currentCell);
+ } else {
+ selectionControl = SelectionControl.create(scene, curControls.length + this.getMaxIndex());
+ selectionControl.update(startSelectionRange, cellInfo);
+ curControls.push(selectionControl);
+ }
+
+ this.hasSelection = true;
+
+ scene.disableEvent();
+
+ this._endSelection(scene);
+
+ const scrollTimer = ScrollTimer.create(this.getScene());
+ scrollTimer.startScroll(newEvtOffsetX, newEvtOffsetY);
+
+ this._scrollTimer = scrollTimer;
+
+ this._addCancelObserver();
+
+ this._moveObserver = scene.onPointerMoveObserver.add((moveEvt: IPointerEvent | IMouseEvent) => {
+ const { offsetX: moveOffsetX, offsetY: moveOffsetY } = moveEvt;
+
+ const { x: newMoveOffsetX, y: newMoveOffsetY } = scene.getRelativeCoord(Vector2.FromArray([moveOffsetX, moveOffsetY]));
+
+ this._moving(newMoveOffsetX, newMoveOffsetY, selectionControl);
+
+ scrollTimer.scrolling(newMoveOffsetX, newMoveOffsetY, () => {
+ this._moving(newMoveOffsetX, newMoveOffsetY, selectionControl);
+ });
+ });
+
+ this._upObserver = scene.onPointerUpObserver.add((upEvt: IPointerEvent | IMouseEvent) => {
+ this._endSelection(scene);
+ });
+
+ state.stopPropagation();
+ });
+ }
+
+ static create(spreadsheet: Spreadsheet) {
+ return new SelectionManager(spreadsheet);
+ }
+}
diff --git a/packages/base-render/src/Component/Sheets/Selection/SelectionModel.ts b/packages/base-render/src/Component/Sheets/Selection/SelectionModel.ts
new file mode 100644
index 0000000000..32bdeb220c
--- /dev/null
+++ b/packages/base-render/src/Component/Sheets/Selection/SelectionModel.ts
@@ -0,0 +1,157 @@
+import { ICellInfo, ISelection, Nullable, makeCellToSelection } from '@univer/core';
+import { SELECTION_TYPE } from './SelectionControl';
+
+export class SelectionModel implements ISelection {
+ private _startColumn: number;
+
+ private _startRow: number;
+
+ private _endColumn: number;
+
+ private _endRow: number;
+
+ private _startX: number;
+
+ private _startY: number;
+
+ private _endX: number;
+
+ private _endY: number;
+
+ private _type: SELECTION_TYPE;
+
+ private _currentCell: Nullable<ICellInfo>;
+
+ constructor(type: SELECTION_TYPE = SELECTION_TYPE.NORMAL) {
+ this._type = type;
+ }
+
+ isEqual(selectionRange: ISelection) {
+ const { startColumn, startRow, endColumn, endRow, type } = this;
+ const { startColumn: newStartColumn, startRow: newStartRow, endColumn: newEndColumn, endRow: newEndRow } = selectionRange;
+ // if (type !== newType) {
+ // return false;
+ // }
+ if (startColumn === newStartColumn && startRow === newStartRow && endColumn === newEndColumn && endRow === newEndRow) {
+ return true;
+ }
+ return false;
+ }
+
+ isInclude(selectionRange: ISelection) {
+ const { startColumn, startRow, endColumn, endRow, type } = this;
+ const { startColumn: newStartColumn, startRow: newStartRow, endColumn: newEndColumn, endRow: newEndRow } = selectionRange;
+
+ // if (type !== newType) {
+ // return false;
+ // }
+
+ if (!(newEndColumn < startColumn || newStartColumn > endColumn || newStartRow > endRow || newEndRow < startRow)) {
+ return true;
+ }
+ return false;
+ }
+
+ get startColumn() {
+ return this._startColumn;
+ }
+
+ get startRow() {
+ return this._startRow;
+ }
+
+ get endColumn() {
+ return this._endColumn;
+ }
+
+ get endRow() {
+ return this._endRow;
+ }
+
+ get startX() {
+ return this._startX;
+ }
+
+ get startY() {
+ return this._startY;
+ }
+
+ get endX() {
+ return this._endX;
+ }
+
+ get endY() {
+ return this._endY;
+ }
+
+ get type() {
+ return this._type;
+ }
+
+ get currentCell() {
+ return this._currentCell;
+ }
+
+ highlightToSelection() {
+ return makeCellToSelection(this._currentCell);
+ }
+
+ getValue() {
+ return {
+ selection: {
+ startColumn: this._startColumn,
+ startRow: this._startRow,
+ endColumn: this._endColumn,
+ endRow: this._endRow,
+
+ startX: this._startX,
+ startY: this._startY,
+ endX: this._endX,
+ endY: this._endY,
+ },
+ cell: this._currentCell,
+ };
+ }
+
+ setValue(newSelectionRange: ISelection, currentCell: Nullable<ICellInfo>) {
+ const {
+ startColumn,
+ startRow,
+ endColumn,
+ endRow,
+
+ startX,
+ startY,
+ endX,
+ endY,
+ } = newSelectionRange;
+
+ this._startColumn = startColumn;
+
+ this._startRow = startRow;
+
+ this._endColumn = endColumn;
+
+ this._endRow = endRow;
+
+ this._startX = startX;
+
+ this._startY = startY;
+
+ this._endX = endX;
+
+ this._endY = endY;
+
+ this.setCurrentCell(currentCell);
+ }
+
+ setCurrentCell(currentCell: Nullable<ICellInfo>) {
+ if (currentCell) {
+ this._currentCell = currentCell;
+ }
+ }
+
+ clearCurrentCell() {
+ this._currentCell = null;
+ }
+}
diff --git a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
index 5355be7c2f..ab1fca5a78 100644
--- a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
+++ b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
@@ -289,6 +289,10 @@ export class SpreadsheetSkeleton extends Skeleton {
return this._columnWidthAccumulation.length;
}
+ getRowCount() {
+ return this._rowHeightAccumulation.length;
+ }
+
get dataMergeCacheAll() {
return this._dataMergeCacheAll;
}
diff --git a/packages/base-render/src/Component/Sheets/Spreadsheet.ts b/packages/base-render/src/Component/Sheets/Spreadsheet.ts
index 0a35b6506c..0fe0bb13d7 100644
--- a/packages/base-render/src/Component/Sheets/Spreadsheet.ts
+++ b/packages/base-render/src/Component/Sheets/Spreadsheet.ts
@@ -21,6 +21,7 @@ import { columnIterator } from '../Docs/Common/Tools';
import { DocumentSkeleton } from '../Docs/DocSkeleton';
import { IDocumentSkeletonColumn } from '../../Basics/IDocumentSkeletonCached';
import { getRotateOffsetAndFarthestHypotenuse, getRotateOrientation } from '../../Basics/Draw';
+import { SelectionManager } from './Selection/SelectionManager';
const OBJECT_KEY = '__SHEET_EXTENSION_FONT_DOCUMENT_INSTANCE__';
@@ -41,6 +42,10 @@ export class Spreadsheet extends SheetComponent {
private _cacheOffsetY = 0;
+ private _hasSelection = false;
+
+ private _selection: SelectionManager;
+
private _documents: Documents = new Documents(OBJECT_KEY, undefined, {
pageMarginLeft: 0,
pageMarginTop: 0,
@@ -248,15 +253,33 @@ export class Spreadsheet extends SheetComponent {
};
}
+ getScrollXYByRelativeCoords(coord: Vector2) {
+ let scene = this.getParent() as Scene;
+ let x = 0;
+ let y = 0;
+ const viewPort = scene.getActiveViewportByRelativeCoord(coord);
+ if (viewPort) {
+ const actualX = viewPort.actualScrollX || 0;
+ const actualY = viewPort.actualScrollY || 0;
+ x += actualX;
+ y += actualY;
+ }
+ return {
+ x,
+ y,
+ };
+ }
+
getAncestorScrollXY(offsetX: number, offsetY: number) {
let parent: any = this.getParent();
+
let x = 0;
let y = 0;
+ let coord = Vector2.FromArray([offsetX, offsetY]);
while (parent) {
if (parent.classType === RENDER_CLASS_TYPE.SCENE) {
- const viewports = (parent as Scene).getViewports();
- const viewPort = viewports.find((vp) => vp.isHit(Vector2.FromArray([offsetX, offsetY])));
- // const viewPort = this._getHasScrollViewport(viewports);
+ const scene = parent as Scene;
+ const viewPort = scene.getActiveViewportByCoord(coord);
if (viewPort) {
const actualX = viewPort.actualScrollX || 0;
const actualY = viewPort.actualScrollY || 0;
@@ -339,6 +362,19 @@ export class Spreadsheet extends SheetComponent {
this.makeDirty(true);
}
+ enableSelection() {
+ if (this._hasSelection) {
+ return;
+ }
+ this._selection = SelectionManager.create(this);
+ this._hasSelection = true;
+ }
+
+ disableSelection() {
+ this._selection?.dispose();
+ this._hasSelection = false;
+ }
+
private _getAncestorSize() {
const parent = this._getAncestorParent();
if (!parent) {
diff --git a/packages/base-render/src/Scene.ts b/packages/base-render/src/Scene.ts
index da6993c1b1..fe25efe933 100644
--- a/packages/base-render/src/Scene.ts
+++ b/packages/base-render/src/Scene.ts
@@ -591,10 +591,59 @@ export class Scene {
return this._transformer;
}
- getActiveViewportByCoord(coord: Vector2) {
+ getActiveViewportByRelativeCoord(coord: Vector2) {
return this._viewports.find((vp) => vp.isHit(coord));
}
+ getActiveViewportByCoord(coord: Vector2) {
+ // let parent: any = this.getParent();
+ // while (parent) {
+ // if (parent.classType === RENDER_CLASS_TYPE.SCENE_VIEWER) {
+ // const sv = parent as SceneViewer;
+ // const transform = sv.transform.clone().invert();
+ // coord = transform.applyPoint(coord);
+ // }
+ // parent = parent?.getParent && parent?.getParent();
+ // }
+
+ coord = this.getRelativeCoord(coord);
+
+ return this.getActiveViewportByRelativeCoord(coord);
+ }
+
+ getRelativeCoord(coord: Vector2) {
+ let parent: any = this.getParent();
+
+ const parentList: any[] = [];
+
+ while (parent) {
+ if (parent.classType === RENDER_CLASS_TYPE.SCENE || parent.classType === RENDER_CLASS_TYPE.SCENE_VIEWER) {
+ parentList.push(parent);
+ }
+ parent = parent?.getParent && parent?.getParent();
+ }
+
+ parentList.reverse();
+
+ for (let parent of parentList) {
+ if (parent.classType === RENDER_CLASS_TYPE.SCENE) {
+ const scene = parent as Scene;
+ const viewPort = scene.getActiveViewportByCoord(coord);
+ if (viewPort) {
+ const actualX = viewPort.actualScrollX || 0;
+ const actualY = viewPort.actualScrollY || 0;
+ coord = coord.addByPoint(actualX, actualY);
+ }
+ } else if (parent.classType === RENDER_CLASS_TYPE.SCENE_VIEWER) {
+ const sv = parent as SceneViewer;
+ const transform = sv.transform.clone().invert();
+ coord = transform.applyPoint(coord);
+ }
+ }
+
+ return coord;
+ }
+
transformToSceneCoord(coord: Vector2) {
const pickedViewport = this.getActiveViewportByCoord(coord);
return pickedViewport?.getRelativeVector(coord);
diff --git a/packages/base-render/src/ScrollTimer.ts b/packages/base-render/src/ScrollTimer.ts
index 8bbef46f3c..4ea96e8563 100644
--- a/packages/base-render/src/ScrollTimer.ts
+++ b/packages/base-render/src/ScrollTimer.ts
@@ -26,7 +26,7 @@ export class ScrollTimer {
constructor(private _scene: Scene, private _padding?: IPaddingData, private _smoothRatioX = 0.05, private _smoothRatioY = 0.05) {
if (!this._padding) {
- this._padding = { t: 20, b: 20, l: 46, r: 20 };
+ this._padding = { t: 20, b: 20, l: 46, r: 60 };
}
}
@@ -114,8 +114,9 @@ export class ScrollTimer {
}
getViewportByCoord(scene?: Scene) {
- const viewports = scene?.getViewports();
- return viewports?.find((vp) => vp.isHit(Vector2.FromArray([this._offsetX, this._offsetY])));
+ // return scene?.getActiveViewportByCoord(Vector2.FromArray([this._offsetX, this._offsetY]));
+
+ return scene?.getActiveViewportByRelativeCoord(Vector2.FromArray([this._offsetX, this._offsetY]));
}
getScene() {
diff --git a/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts b/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts
index 5b25cfe4a5..5b4bf737e5 100644
--- a/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts
+++ b/packages/base-sheets/src/View/Render/Views/SceneViewerTestView.ts
@@ -116,6 +116,8 @@ export class SceneViewerTestView extends BaseView {
height: allHeight,
});
+ spreadsheet.enableSelection();
+
mainScene.addObject(sv);
}
diff --git a/packages/base-sheets/src/main.tsx b/packages/base-sheets/src/main.tsx
index 08650c706d..faa6258fe4 100644
--- a/packages/base-sheets/src/main.tsx
+++ b/packages/base-sheets/src/main.tsx
@@ -35,8 +35,30 @@ const uiDefaultConfigUp = {
},
],
},
+
+ render: {
+ top: 0,
+ left: 0,
+ width: '100%',
+ height: '100%',
+ zIndex: 11,
+ angle: 0,
+ scaleX: 0,
+ scaleY: 0,
+ skewX: 0,
+ skewY: 0,
+ flipX: false,
+ flipY: false,
+ isTransformer: false,
+ forceRender: false,
+ },
};
+// const univer = new Univer();
+// univer.installPlugin(new RenderEngine());
+// univer.installPlugin(new UniverComponentSheet());
+// univer.installPlugin(new FormulaEngine());
+
const univerSheetUp = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA);
univerSheetUp.installPlugin(new RenderEngine());
univerSheetUp.installPlugin(new UniverComponentSheet());