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.

fix(Selection): init selection by config

Dushusir43563b9a49cd
diff --git a/.github/contributing-zh.md b/.github/contributing-zh.md
index 5db0bfa0c9..0475f74e7c 100644
--- a/.github/contributing-zh.md
+++ b/.github/contributing-zh.md
@@ -333,7 +333,7 @@ pnpm install
### 插件目录
-一个标准的插件目录结构如下所示
+一个标准的插件目录结构 `src` 目录如下所示
```sh
│ index.ts # 插件出口
@@ -345,16 +345,15 @@ pnpm install
│ index.ts # 出口
│ SpreadsheetController.ts # 具体的数据更新类(实际名称会根据插件名称替换)
│
-├─Basic # 基础工具函数、常量、接口
+├─Basics # 基础工具函数、常量、接口(可选)
│ ├─Const # 常量
│ │ index.ts # 出口
│ │
│ ├─Enum # 枚举
│ │ index.ts # 出口
│ │
-│ │
│ └─Shared # 公共方法
-│ index.ts # 出口
+│ index.ts # 出口
│
├─Locale # 国际化
│ en.ts # 英文国际化翻译文件
@@ -362,8 +361,15 @@ pnpm install
│ zh.ts # 中文国际化翻译文件
│
├─Model # 数据 CRDT
-│
-├─Apply # 通用的一些公共的外部服务连接,比如权限、其他插件的能力对接
+│ │
+│ ├─Action # 由Command触发(可选)
+│ │ index.ts # 出口
+│ │
+│ ├─Apply # 修改数据(可选)
+│ │ index.ts # 出口
+│ │
+│ └─SpreadsheetModel # 各个Controller对应的数据模型
+│ index.ts # 出口
│
├─Types # 声明文件
│ index.d.ts # 具体的声明文件
@@ -568,9 +574,14 @@ import styles from './index.module.less';
const s = `${style.AlternatingColorsSideSetting}`;
```
+### 测试数据
+
+1. 如果是单元测试需要使用的数据,直接放在 `src` 同级的 `test` 目录下
+2. 如果是插件的初始化的入参数据,可以直接写在 `main.tsx` 中,或者自己建立一个新的文件夹,如 `src/Data` 文件夹,文件名推荐使用 `DEFAULT_[NAME]_DATA` 的格式,如 `DEFAULT_WORKBOOK_DATA`
+
### 不使用 preact 构造插件 UI
-#### 处理国际化
+### 处理国际化
## 测试
@@ -1278,10 +1289,3 @@ npm run build:customd
```
将每个插件单独打包出模块,内部自动使用`import()`动态载入插件,提升首屏加载速度,用法上和上面几乎一致,区别是需要将打包后所有文件复制到工程目录即可,其他配置一样。
-
-## 已知问题
-
-1. 遇上科学计数法 会卡死
- 输入 =1.2974966045650646E8
- 有时候输入后系统崩溃
- 有时候是输入后在操作其他单元格系统崩溃
diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.ts
index b58cf67bc6..59cc6cbc98 100644
--- a/packages/base-sheets/src/Controller/CellEditorController.ts
+++ b/packages/base-sheets/src/Controller/CellEditorController.ts
@@ -14,8 +14,10 @@ const CELL_EDIT_HIDDEN_TOP = -10000;
* TODO:
*
* 1. 注册机制,type='prefix | suffix | regex',
- * type='prefix' value='=',注册为公式
- * type='prefix' value=''' 注册为单元格文本格式设置
+ * type='prefix' pattern='=',注册为公式
+ * type='prefix' pattern='$' 注册为货币
+ * type='suffix' pattern='%' 注册为百分比
+ * type='regex' pattern='/*-*-/g' 注册为日期
*
* 2. p存储的富文本字符数少于50的情况下,存一份到v,便于vlookup公式计算
*/
@@ -92,6 +94,8 @@ export class CellEditorController {
.getObserver('onSheetRenderDidMountObservable', 'core')
?.add((e) => {
const main = this._plugin.getMainComponent();
+
+ // TODO 应该接受selection obs,便于外部注册拦截
main.onDblclickObserver.add((evt: IPointerEvent | IMouseEvent) => {
this.enterEditMode();
});
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts b/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts
index c30de0f193..0a13a99042 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts
@@ -50,6 +50,18 @@ export class SelectionControlDragAndDrop {
});
}
+ remove() {
+ const { leftControl, rightControl, topControl, bottomControl } = this._control;
+ leftControl.onPointerEnterObserver.clear();
+ leftControl.onPointerLeaveObserver.clear();
+ rightControl.onPointerEnterObserver.clear();
+ rightControl.onPointerLeaveObserver.clear();
+ topControl.onPointerEnterObserver.clear();
+ topControl.onPointerLeaveObserver.clear();
+ bottomControl.onPointerEnterObserver.clear();
+ bottomControl.onPointerLeaveObserver.clear();
+ }
+
static create(control: SelectionControl) {
return new SelectionControlDragAndDrop(control);
}
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionControlFill.ts b/packages/base-sheets/src/Controller/Selection/SelectionControlFill.ts
index 8406f28dfd..7ee470d87c 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionControlFill.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionControlFill.ts
@@ -20,6 +20,12 @@ export class SelectionControlFill {
});
}
+ remove() {
+ const { fillControl } = this._control;
+ fillControl.onPointerEnterObserver.clear();
+ fillControl.onPointerLeaveObserver.clear();
+ }
+
static create(control: SelectionControl) {
return new SelectionControlFill(control);
}
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionController.ts b/packages/base-sheets/src/Controller/Selection/SelectionController.ts
index 4e99c6822e..e55ff764c5 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionController.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionController.ts
@@ -5,14 +5,22 @@ import { SelectionControlDragAndDrop } from './SelectionControlDragDrop';
import { SelectionControlFill } from './SelectionControlFill';
import { SelectionManager } from './SelectionManager';
-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 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,
@@ -69,45 +77,45 @@ export class SelectionControl {
this._leftControl = new Rect(SELECTION_MANAGER_KEY.left + zIndex, {
top: 0,
left: -DEFAULT_SELECTION_CONFIG.strokeWidth / 2,
- width: DEFAULT_SELECTION_CONFIG.strokeWidth as number,
- fill: DEFAULT_SELECTION_CONFIG.strokeColor as string,
+ 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 as number,
- fill: DEFAULT_SELECTION_CONFIG.strokeColor as string,
+ 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 as number,
- fill: DEFAULT_SELECTION_CONFIG.strokeColor as string,
+ 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 as number,
- fill: DEFAULT_SELECTION_CONFIG.strokeColor as string,
+ 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 as string,
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
zIndex: zIndex - 1,
evented: false,
});
this._backgroundControlBottom = new Rect(SELECTION_MANAGER_KEY.backgroundBottom + zIndex, {
- fill: DEFAULT_SELECTION_CONFIG.backgroundColor as string,
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
zIndex: zIndex - 1,
evented: false,
});
this._backgroundControlMiddleLeft = new Rect(SELECTION_MANAGER_KEY.backgroundMiddleLeft + zIndex, {
- fill: DEFAULT_SELECTION_CONFIG.backgroundColor as string,
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
zIndex: zIndex - 1,
evented: false,
});
this._backgroundControlMiddleRight = new Rect(SELECTION_MANAGER_KEY.backgroundMiddleRight + zIndex, {
- fill: DEFAULT_SELECTION_CONFIG.backgroundColor as string,
+ fill: DEFAULT_SELECTION_CONFIG.backgroundColor,
zIndex: zIndex - 1,
evented: false,
});
@@ -115,9 +123,9 @@ export class SelectionControl {
this._fillControl = new Rect(SELECTION_MANAGER_KEY.fill + zIndex, {
width: fillSideLength,
height: fillSideLength,
- fill: DEFAULT_SELECTION_CONFIG.strokeColor as string,
- strokeWidth: DEFAULT_SELECTION_CONFIG.fillStrokeLength as number,
- stroke: DEFAULT_SELECTION_CONFIG.fillStrokeColor as string,
+ fill: DEFAULT_SELECTION_CONFIG.strokeColor,
+ strokeWidth: DEFAULT_SELECTION_CONFIG.fillStrokeLength,
+ stroke: DEFAULT_SELECTION_CONFIG.fillStrokeColor,
zIndex: zIndex + 1,
});
this._selectionShape = new Group(
@@ -160,8 +168,12 @@ export class SelectionControl {
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._updateDragAndFill();
+
this.selectionShape.visible = true;
this.selectionShape.translate(startX, startY);
@@ -186,6 +198,8 @@ export class SelectionControl {
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;
}
@@ -242,11 +256,30 @@ export class SelectionControl {
this._backgroundControlBottom.transformByState(middleBottomConfig);
}
+ /**
+ * Updated DragAndDrop and fill controls, inactive selections need to hide them
+ */
+ private _updateDragAndFill() {
+ const highlightSelection = this._selectionModel.highlightToSelection();
+
+ if (!highlightSelection) {
+ this._selectionDragAndDrop.remove();
+
+ this._fillControl.resize(0, 0);
+ this._selectionControlFill.remove();
+ }
+ }
+
update(newSelectionRange: ISelection, highlight: Nullable<ICellInfo>) {
this._selectionModel.setValue(newSelectionRange, highlight);
this._updateControl();
}
+ clearHighlight() {
+ this._selectionModel.clearCurrentCell();
+ this._updateControl();
+ }
+
dispose() {
this._leftControl?.dispose();
this._rightControl?.dispose();
@@ -296,6 +329,14 @@ export class SelectionControl {
return this._backgroundControlBottom;
}
+ get backgroundControlMiddleLeft() {
+ return this._backgroundControlMiddleLeft;
+ }
+
+ get backgroundControlMiddleRight() {
+ return this._backgroundControlMiddleRight;
+ }
+
get selectionShape() {
return this._selectionShape;
}
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
index f5c8731e24..4be29c992c 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
@@ -1,10 +1,14 @@
import { IMouseEvent, IPointerEvent, Rect, Spreadsheet, SpreadsheetColumnTitle, SpreadsheetRowTitle } from '@univer/base-render';
-import { Nullable, Observer, Plugin, Worksheet, ISelection, makeCellToSelection, IRangeData, RangeList, Range } from '@univer/core';
+import { Nullable, Observer, Worksheet, ISelection, makeCellToSelection, IRangeData, RangeList, Range, IRangeCellData } from '@univer/core';
import { SelectionModel } from '../../Model/SelectionModel';
+import { ISelectionsConfig, SpreadsheetPlugin } from '../../SpreadsheetPlugin';
import { SheetView } from '../../View/Render/Views/SheetView';
import { ScrollTimer } from '../ScrollTimer';
import { SelectionControl, SELECTION_TYPE } from './SelectionController';
+/**
+ * TODO 注册selection拦截,可能在有公式ArrayObject时,fx公式栏显示不同
+ */
export class SelectionManager {
private _mainComponent: Spreadsheet;
@@ -20,7 +24,7 @@ export class SelectionManager {
private _selectionControls = new Map<string, SelectionControl[]>(); // sheetID:Controls
- private _plugin: Plugin;
+ private _plugin: SpreadsheetPlugin;
private _startSelectionRange: ISelection;
@@ -78,12 +82,12 @@ export class SelectionManager {
}
/**
- * TODO Not necessarily curCellRange
+ * add a selection
* @param selectionRange
* @param curCellRange
* @returns
*/
- addControlToCurrentByRangeData(selectionRange: IRangeData, curCellRange: IRangeData) {
+ addControlToCurrentByRangeData(selectionRange: IRangeData, curCellRange: Nullable<IRangeCellData>) {
const currentControls = this.getCurrentControls();
if (!currentControls) {
return;
@@ -91,7 +95,11 @@ export class SelectionManager {
const main = this._mainComponent;
const control = SelectionControl.create(this, currentControls.length);
- const cellInfo = main.getCellByIndex(curCellRange.startRow, curCellRange.startColumn);
+
+ let cellInfo = null;
+ if (curCellRange) {
+ cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);
+ }
const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = selectionRange;
const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);
@@ -116,7 +124,7 @@ export class SelectionManager {
recreateControlsByRangeData() {}
constructor(private _sheetView: SheetView) {
- this._plugin = this._sheetView.getPlugin();
+ this._plugin = this._sheetView.getPlugin() as SpreadsheetPlugin;
this._initialize();
}
@@ -139,6 +147,8 @@ export class SelectionManager {
if (worksheetId) {
this._selectionControls.set(worksheetId, []);
}
+
+ this._initControls(this._plugin.config.selections);
}
private _mainEventInitial() {
@@ -173,21 +183,26 @@ export class SelectionManager {
let curControls = this.getCurrentControls();
- console.log('selection', curControls, startSelectionRange);
+ // console.log('selection', curControls, startSelectionRange);
if (!curControls) {
return false;
}
for (let control of curControls) {
+ // right click
if (evt.button === 2 && control.model.isInclude(startSelectionRange, SELECTION_TYPE.NORMAL)) {
selectionControl = control;
return;
}
+ // Click to an existing selection
if (control.model.isEqual(startSelectionRange, SELECTION_TYPE.NORMAL)) {
selectionControl = control;
break;
}
+
+ // There can only be one highlighted cell, so clear the highlighted cell of the existing selection
+ control.clearHighlight();
}
if (!selectionControl) {
@@ -284,7 +299,7 @@ export class SelectionManager {
selectionControl && selectionControl.update(newSelectionRange);
if (oldStartColumn !== finalStartColumn || oldStartRow !== finalStartRow || oldEndColumn !== finalEndColumn || oldEndRow !== finalEndRow) {
- this._plugin.getObserver('onChangeSelectionObserver')?.notifyObservers(selectionControl);
+ selectionControl && this._plugin.getObserver('onChangeSelectionObserver')?.notifyObservers(selectionControl);
}
}
@@ -309,6 +324,51 @@ export class SelectionManager {
});
}
+ /**
+ * Initialize selection based on user configuration
+ * @param selections
+ */
+ private _initControls(selections: ISelectionsConfig) {
+ Object.keys(selections).forEach((worksheetId) => {
+ const selectionsList = selections[worksheetId];
+ const currentControls: SelectionControl[] = [];
+
+ selectionsList.forEach((selectionConfig) => {
+ const selectionRange = selectionConfig.selection;
+ const curCellRange = selectionConfig.cell;
+ const main = this._mainComponent;
+
+ const control = SelectionControl.create(this, currentControls.length);
+
+ let cellInfo = null;
+ if (curCellRange) {
+ cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);
+ }
+
+ const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = selectionRange;
+ const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);
+ const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);
+
+ control.update(
+ {
+ startColumn: finalStartColumn,
+ startRow: finalStartRow,
+ endColumn: finalEndColumn,
+ endRow: finalEndRow,
+ startY: startCell?.startY || 0,
+ endY: endCell?.endY || 0,
+ startX: startCell?.startX || 0,
+ endX: endCell?.endX || 0,
+ },
+ cellInfo
+ );
+ currentControls.push(control);
+ });
+
+ this._selectionControls.set(worksheetId, currentControls);
+ });
+ }
+
/**
* 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.
@@ -333,21 +393,38 @@ export class SelectionManager {
/**
* 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<Range> {
const controls = this.getCurrentControls();
- const model = controls && controls[controls.length - 1].model;
- return (
- model &&
- this._worksheet?.getRange({
- startRow: model.startRow,
- startColumn: model.startColumn,
- endRow: model.endRow,
- endColumn: model.endColumn,
- })
- );
+ if (controls && controls.length > 0) {
+ const control = controls?.find((control: SelectionControl) => {
+ if (control.getCurrentCellInfo()) {
+ return control;
+ }
+ return null;
+ });
+ if (control) {
+ const model = control.model;
+ const rangeData = {
+ startRow: model.startRow,
+ startColumn: model.startColumn,
+ endRow: model.endRow,
+ endColumn: model.endColumn,
+ };
+ return this._worksheet?.getRange(rangeData);
+ }
+ }
+ // const model = controls && controls[controls.length - 1].model;
+ // return (
+ // model &&
+ // this._worksheet?.getRange({
+ // startRow: model.startRow,
+ // startColumn: model.startColumn,
+ // endRow: model.endRow,
+ // endColumn: model.endColumn,
+ // })
+ // );
// if (controls && controls.length > 0) {
// const selections = controls?.map((control: SelectionControl) => {
@@ -369,6 +446,16 @@ export class SelectionManager {
*/
getActiveSelection(): Nullable<SelectionControl> {
const controls = this.getCurrentControls();
- return controls && controls[controls.length - 1];
+ if (controls && controls.length > 0) {
+ const control = controls?.find((control: SelectionControl) => {
+ if (control.getCurrentCellInfo()) {
+ return control;
+ }
+ return null;
+ });
+ if (control) {
+ return control;
+ }
+ }
}
}
diff --git a/packages/base-sheets/src/Controller/SheetContainerController.ts b/packages/base-sheets/src/Controller/SheetContainerController.ts
index 90780040f5..2becc8a274 100644
--- a/packages/base-sheets/src/Controller/SheetContainerController.ts
+++ b/packages/base-sheets/src/Controller/SheetContainerController.ts
@@ -9,7 +9,7 @@ export class SheetContainerController {
private _sheetContainerContent: HTMLElement;
- private __debounceTimeout: number;
+ // private __debounceTimeout: number;
constructor(plugin: SpreadsheetPlugin) {
this._plugin = plugin;
@@ -20,10 +20,12 @@ export class SheetContainerController {
protected _initialize() {
// Monitor all command changes and automatically trigger the refresh of the canvas
CommandManager.getActionObservers().add((actionEvent) => {
- window.clearTimeout(this.__debounceTimeout);
- this.__debounceTimeout = window.setTimeout(() => {
- this._plugin.getMainComponent().makeDirty(true);
- }, 10);
+ //In order not to affect the refresh experience and remove the delay, multiple refreshes are acceptable if there is no performance problem
+
+ // window.clearTimeout(this.__debounceTimeout);
+ // this.__debounceTimeout = window.setTimeout(() => {
+ this._plugin.getMainComponent().makeDirty(true);
+ // }, 10);
});
}
}
diff --git a/packages/base-sheets/src/Controller/ToolBarController.ts b/packages/base-sheets/src/Controller/ToolBarController.ts
index 37718e5ac7..74bfff9d29 100644
--- a/packages/base-sheets/src/Controller/ToolBarController.ts
+++ b/packages/base-sheets/src/Controller/ToolBarController.ts
@@ -40,9 +40,9 @@ export class ToolBarController {
this.Render = component.getComponentRender();
const config =
- this._plugin.layout === 'auto'
+ this._plugin.config.layout === 'auto'
? Tools.deepClone(defaultLayout.toolBarConfig)
- : Tools.deepMerge(defaultLayout.toolBarConfig, (this._plugin.layout as ILayout).toolBarConfig);
+ : Tools.deepMerge(defaultLayout.toolBarConfig, (this._plugin.config.layout as ILayout).toolBarConfig);
const toolList: IToolBarItemProps[] = [
{
diff --git a/packages/base-sheets/src/Model/SelectionModel.ts b/packages/base-sheets/src/Model/SelectionModel.ts
index c059402e75..c6897bce06 100644
--- a/packages/base-sheets/src/Model/SelectionModel.ts
+++ b/packages/base-sheets/src/Model/SelectionModel.ts
@@ -133,4 +133,8 @@ export class SelectionModel implements ISelection {
this._currentCell = currentCell;
}
}
+
+ clearCurrentCell() {
+ this._currentCell = null;
+ }
}
diff --git a/packages/base-sheets/src/SpreadsheetPlugin.tsx b/packages/base-sheets/src/SpreadsheetPlugin.tsx
index 8d86bdf627..71439961e3 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, IWorkbookConfig, Plugin, PLUGIN_NAMES } from '@univer/core';
+import { AsyncFunction, Attribute, Context, IOCAttribute, IOCContainer, IRangeCellData, IRangeData, 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, ILayout, ISpreadsheetPluginConfigBase, SheetContainer } from './View/UI/SheetContainer';
+import { BaseSheetContainerConfig, ISpreadsheetPluginConfigBase, SheetContainer } from './View/UI/SheetContainer';
import {
RightMenuController,
ToolBarController,
@@ -20,11 +20,41 @@ import {
SheetContainerController,
} from './Controller';
+export interface ISelectionConfig {
+ selection: IRangeData;
+ cell?: IRangeCellData;
+}
+
+export interface ISelectionsConfig {
+ [worksheetId: string]: ISelectionConfig[];
+}
+
export interface ISpreadsheetPluginConfig extends ISpreadsheetPluginConfigBase {
- container?: HTMLElement | string;
- // selection?: ISelection[]
+ 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,
+ },
+ cell: {
+ row: 13,
+ column: 1,
+ },
+ },
+ ],
+ },
+};
+
/**
* The main sheet base, construct the sheet container and layout, mount the rendering engine
*/
@@ -32,7 +62,7 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
@Attribute()
private pros: IOCAttribute;
- layout: string | ILayout;
+ private _config: ISpreadsheetPluginConfig;
context: Context;
@@ -52,8 +82,6 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
private _showMainByNameFunc: Function;
- private _sheetContainer: HTMLElement;
-
private _canvasEngine: Engine;
private _canvasView: CanvasView;
@@ -76,26 +104,10 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
private _sheetContainerController: SheetContainerController;
- constructor(props: ISpreadsheetPluginConfig = {}) {
+ constructor(props: Partial<ISpreadsheetPluginConfig> = {}) {
super(PLUGIN_NAMES.SPREADSHEET);
- const { container = 'universheet', layout = 'auto' } = props;
- this.layout = layout;
-
- if (typeof container === 'string') {
- const containerDOM = document.getElementById(container);
- if (containerDOM == null) {
- this._sheetContainer = document.createElement('div');
- this._sheetContainer.id = container;
- } else {
- this._sheetContainer = containerDOM;
- }
- } else if (isElement(container)) {
- this._sheetContainer = container;
- } else {
- this._sheetContainer = document.createElement('div');
- this._sheetContainer.id = 'universheet';
- }
+ this._config = Tools.commonExtend(DEFAULT_SPREADSHEET_PLUGIN_DATA, props);
}
register(engineInstance: Engine) {
@@ -117,7 +129,6 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
this._initializeRender(context);
});
- const locale = context.getLocale().options.currentLocale;
/**
* load more Locale object
*/
@@ -128,10 +139,12 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
const configure: IWorkbookConfig = this.pros.getValue();
+ const sheetContainer = this._initContainer(this._config.container);
+
const config: BaseSheetContainerConfig = {
skin: configure.skin || 'default',
- layout: this.layout,
- container: this._sheetContainer,
+ layout: this._config.layout,
+ container: sheetContainer,
context,
getSplitLeftRef: (ref) => {
this._splitLeftRef = ref;
@@ -167,7 +180,7 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
this._sheetContainerController = new SheetContainerController(this);
// render sheet container
- render(<SheetContainer config={config} />, this._sheetContainer!);
+ render(<SheetContainer config={config} />, sheetContainer);
const formulaEngine = this.getPluginByName<FormulaPlugin>('formula')?.getFormulaEngine();
// =(sum(max(B1:C10,10)*5-100,((1+1)*2+5)/2,10)+count(B1:C10,10*5-100))*5-100
@@ -178,6 +191,36 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
// formulaEngine?.calculate(`=lambda(x,y, x*y*x)(sum(1,(1+2)*3),2)`);
}
+ /**
+ * Convert id to DOM
+ * @param container
+ * @returns
+ */
+ private _initContainer(container: HTMLElement | string) {
+ let sheetContainer = null;
+ if (typeof container === 'string') {
+ const containerDOM = document.getElementById(container);
+ if (containerDOM == null) {
+ sheetContainer = document.createElement('div');
+ sheetContainer.id = container;
+ } else {
+ sheetContainer = containerDOM;
+ }
+ } else if (isElement(container)) {
+ sheetContainer = container;
+ } else {
+ sheetContainer = document.createElement('div');
+ sheetContainer.id = 'universheet';
+ }
+
+ return sheetContainer;
+ }
+
+ /**
+ * Initialize all selections
+ */
+ private _initSelection() {}
+
onMounted(ctx: Context): void {
install(this);
@@ -200,6 +243,10 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {
// this._initializeRender(ctx);
}
+ get config() {
+ return this._config;
+ }
+
private _initializeRender(context: Context) {
const engine = this.getPluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER)?.getEngine()!;
diff --git a/packages/base-sheets/src/View/UI/SheetContainer/SheetContainer.tsx b/packages/base-sheets/src/View/UI/SheetContainer/SheetContainer.tsx
index be70381395..4caa6d3d2f 100644
--- a/packages/base-sheets/src/View/UI/SheetContainer/SheetContainer.tsx
+++ b/packages/base-sheets/src/View/UI/SheetContainer/SheetContainer.tsx
@@ -82,7 +82,7 @@ export interface ILayout {
}
export interface ISpreadsheetPluginConfigBase {
- layout?: string | ILayout;
+ layout: string | ILayout;
}
export interface BaseSheetContainerConfig extends BaseComponentProps, ISpreadsheetPluginConfigBase {
diff --git a/packages/core/src/Server/ServerBase.ts b/packages/core/src/Server/ServerBase.ts
index d3965225f9..32ab37df4f 100644
--- a/packages/core/src/Server/ServerBase.ts
+++ b/packages/core/src/Server/ServerBase.ts
@@ -28,7 +28,7 @@ export enum ServerReceiveType {
* Basics class for Server
*/
export abstract class ServerBase {
- static isMessageReponseReceive(
+ static isMessageResponseReceive(
receive: IOServerReceive<unknown>
): receive is IOServerReceive<IOServerMessage> {
return receive.type === ServerReceiveType.MESSAGE_RESPONSE;
diff --git a/packages/core/src/Server/ServerSocket.ts b/packages/core/src/Server/ServerSocket.ts
index 935d48bfc4..ea64734f0f 100644
--- a/packages/core/src/Server/ServerSocket.ts
+++ b/packages/core/src/Server/ServerSocket.ts
@@ -81,7 +81,7 @@ export class ServerSocket extends ServerBase {
* @returns
*/
private checkupReceiveMessage(receive: IOServerReceive<unknown>) {
- if (ServerSocket.isMessageReponseReceive(receive)) {
+ if (ServerSocket.isMessageResponseReceive(receive)) {
if (this.getTopMessage().version === receive.message.version) {
this.popTopMessage();
this.globalSendResolve();