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(base-sheets): set column width

tonytonychopper123eda110281795
diff --git a/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts b/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts
index fecf6f1c26..6a3f73c9b5 100644
--- a/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts
+++ b/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts
@@ -1,4 +1,6 @@
-import { IMouseEvent, IPointerEvent } from '@univer/base-render';
+import { IMouseEvent, IPointerEvent, Rect } from '@univer/base-render';
+import { Nullable } from '@univer/core';
+import { DragLineDirection } from './DragLineController';
import { SelectionManager } from './SelectionManager';
export class ColumnTitleController {
@@ -10,22 +12,126 @@ export class ColumnTitleController {
private _startOffsetY: number = 0;
+ private _index: number = 0;
+
+ private _currentWidth: number = 0;
+
+ private _highlightItem: Rect;
+
constructor(manager: SelectionManager) {
this._manager = manager;
this._leftTopWidth = this._manager.getSheetView().getSpreadsheetLeftTopPlaceholder().getState().width;
+ // 创建高亮item
+ this._highlightItem = new Rect('HighLightColumnTitle', {
+ width: 0,
+ height: this._manager.getSheetView().getSpreadsheetSkeleton().columnTitleHeight,
+ left: 0,
+ fill: 'rgb(220,220,220,0.5)',
+ });
+
+ this._highlightItem.hide();
+
+ this._highlightItem.evented = false;
+
+ const scene = this._manager.getScene();
+
+ scene.addObject(this._highlightItem, 3);
}
pointerDown(e: IPointerEvent | IMouseEvent) {
- // this._manager.getDragLineControl().show();
const main = this._manager.getMainComponent();
const { offsetX: evtOffsetX, offsetY: evtOffsetY } = e;
this._startOffsetX = evtOffsetX;
this._startOffsetY = evtOffsetY;
const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);
- const clientX = e.clientX - scrollXY.x - this._leftTopWidth;
- const columnWidthAccumulation = main.getSkeleton()?.columnWidthAccumulation;
- for (let i = 0; i < columnWidthAccumulation?.length; i++) {}
+ const contentRef = this._manager.getPlugin().getSheetContainerControl().getContentRef();
+ const clientX = e.clientX - scrollXY.x - this._leftTopWidth - contentRef.current!.getBoundingClientRect().left;
+ const columnWidthAccumulation = main.getSkeleton()?.columnWidthAccumulation ?? [];
+
+ for (let i = 0; i < columnWidthAccumulation?.length; i++) {
+ if (columnWidthAccumulation[i] >= clientX) {
+ this._index = i;
+ break;
+ }
+ }
+ // 当前列宽度
+ this._currentWidth = columnWidthAccumulation[0];
+ if (this._index) {
+ this._currentWidth = columnWidthAccumulation[this._index] - columnWidthAccumulation[this._index - 1];
+ }
+ // 显示拖动线
+ if (columnWidthAccumulation[this._index] - clientX <= 5) {
+ const end = columnWidthAccumulation[this._index] + this._leftTopWidth;
+ const start = (this._index ? columnWidthAccumulation[this._index - 1] : 0) + this._leftTopWidth;
+
+ this._manager.getDragLineControl().create({
+ direction: DragLineDirection.VERTICAL,
+ end,
+ start,
+ dragUp: this.setColumnWidth.bind(this),
+ });
+ this._manager.getDragLineControl().dragDown(e);
+ }
+ // 高亮当前列
+ this.highlightColumn();
+ }
+
+ setColumnWidth(width: Nullable<number>) {
+ const plugin = this._manager.getPlugin();
+ const sheet = plugin.getContext().getWorkBook().getActiveSheet();
+ if (width === null) {
+ sheet.setColumnWidth(this._index, 1, 5);
+ } else {
+ sheet.setColumnWidth(this._index, 1, width! + this._currentWidth);
+ }
+ this.highlightColumn();
+ }
+
+ highlightColumn() {
+ this._manager.clearSelectionControls();
+ const sheet = this._manager.getPlugin().getWorkbook().getActiveSheet();
+ this._manager.addControlToCurrentByRangeData({
+ startRow: 0,
+ startColumn: this._index,
+ endColumn: this._index,
+ endRow: sheet.getRowCount() - 1,
+ });
}
- highlightColumn() {}
+ highlightColumnTitle(e: IPointerEvent | IMouseEvent) {
+ const main = this._manager.getMainComponent();
+ const { offsetX: evtOffsetX, offsetY: evtOffsetY } = e;
+ this._startOffsetX = evtOffsetX;
+ this._startOffsetY = evtOffsetY;
+ const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);
+ const contentRef = this._manager.getPlugin().getSheetContainerControl().getContentRef();
+ const clientX = e.clientX - scrollXY.x - this._leftTopWidth - contentRef.current!.getBoundingClientRect().left;
+ const columnWidthAccumulation = main.getSkeleton()?.columnWidthAccumulation ?? [];
+
+ for (let i = 0; i < columnWidthAccumulation?.length; i++) {
+ if (columnWidthAccumulation[i] >= clientX) {
+ this._index = i;
+ break;
+ }
+ }
+
+ // 当前列宽度
+ this._currentWidth = columnWidthAccumulation[0];
+ let left = this._leftTopWidth;
+ if (this._index) {
+ this._currentWidth = columnWidthAccumulation[this._index] - columnWidthAccumulation[this._index - 1];
+ left = this._leftTopWidth + columnWidthAccumulation[this._index - 1];
+ }
+
+ this._highlightItem.transformByState({
+ width: this._currentWidth,
+ left,
+ });
+
+ this._highlightItem.show();
+ }
+
+ unHighlightColumnTitle() {
+ this._highlightItem.hide();
+ }
}
diff --git a/packages/base-sheets/src/Controller/Selection/DragLineController.ts b/packages/base-sheets/src/Controller/Selection/DragLineController.ts
index e2ac555413..f003e2d623 100644
--- a/packages/base-sheets/src/Controller/Selection/DragLineController.ts
+++ b/packages/base-sheets/src/Controller/Selection/DragLineController.ts
@@ -14,9 +14,22 @@ const DEFAULT_DRAG_LINE_CONFIG = {
fillStrokeWidth: 5,
};
+export enum DragLineDirection {
+ VERTICAL,
+ HORIZONTAL,
+}
+
interface State {
- left: number;
- clientX: number;
+ left?: number;
+ top?: number;
+ distance: number;
+}
+
+interface Option {
+ direction: DragLineDirection;
+ end: number; // 当前列/行尾距离屏幕距离
+ start: number; //当前列/行首距离屏幕距离
+ dragUp: (distance: Nullable<number>) => void;
}
export class DragLineController {
@@ -34,44 +47,71 @@ export class DragLineController {
private _state: State;
+ private _direction: DragLineDirection;
+
+ private _end: number; // 初始线距离屏幕的距离
+
+ private _start: number;
+
+ private _dragUp: (distance: Nullable<number>) => void;
+
constructor(manager: SelectionManager) {
this._manager = manager;
+ }
- this._dragLineControlMain = new Rect(DRAG_LINE_KEY.lineMain, {
- width: DEFAULT_DRAG_LINE_CONFIG.fillStrokeWidth,
- height: 20,
- fill: DEFAULT_DRAG_LINE_CONFIG.strokeColor,
- zIndex: 2,
- });
-
- this._dragLineControlContent = new Rect(DRAG_LINE_KEY.lineContent, {
- width: DEFAULT_DRAG_LINE_CONFIG.strokeWidth,
- height: 1000,
- fill: DEFAULT_DRAG_LINE_CONFIG.strokeColor,
- zIndex: 1,
- });
+ create(option: Option) {
+ const { direction, end, dragUp, start } = option;
+ this._direction = direction;
+ this._end = end;
+ this._start = start;
+ this._dragUp = dragUp;
+
+ const skeleton = this._manager.getSheetView().getSpreadsheetColumnTitle().getSkeleton();
+ if (direction) {
+ const width = skeleton?.rowTitleWidth;
+ const contentWidth = skeleton?.columnTotalWidth;
+ this._dragLineControlMain = new Rect(DRAG_LINE_KEY.lineMain, {
+ width,
+ height: 5,
+ top: this._end - 5,
+ fill: DEFAULT_DRAG_LINE_CONFIG.strokeColor,
+ });
+
+ this._dragLineControlContent = new Rect(DRAG_LINE_KEY.lineContent, {
+ width: contentWidth,
+ height: 2,
+ top: this._end - 2,
+ left: width,
+ fill: DEFAULT_DRAG_LINE_CONFIG.strokeColor,
+ });
+ } else {
+ const height = skeleton?.columnTitleHeight;
+ const contentHeight = skeleton?.rowTotalHeight;
+ this._dragLineControlMain = new Rect(DRAG_LINE_KEY.lineMain, {
+ width: DEFAULT_DRAG_LINE_CONFIG.fillStrokeWidth,
+ height,
+ left: this._end - 5,
+ fill: DEFAULT_DRAG_LINE_CONFIG.strokeColor,
+ });
+
+ this._dragLineControlContent = new Rect(DRAG_LINE_KEY.lineContent, {
+ width: DEFAULT_DRAG_LINE_CONFIG.strokeWidth,
+ height: contentHeight,
+ top: height,
+ left: this._end - 2,
+ fill: DEFAULT_DRAG_LINE_CONFIG.strokeColor,
+ });
+ }
this._dragLine = new Group(DRAG_LINE_KEY.line, this._dragLineControlMain, this._dragLineControlContent);
- // this._dragLine.hide();
+ this._dragLine.hide();
this._dragLine.evented = false;
const scene = this._manager.getScene();
scene.addObject(this._dragLine, 3);
-
- this._dragLine.onPointerDownObserver.add((e: IPointerEvent | IMouseEvent) => {
- this.dragDown(e);
- });
- }
-
- show() {
- this._dragLine.show();
- }
-
- hide() {
- this._dragLine.hide();
}
dragDown(e: IPointerEvent | IMouseEvent) {
@@ -80,16 +120,17 @@ export class DragLineController {
scene.disableEvent();
this._state = {
- left: this._dragLine.getState().left,
- clientX: e.clientX,
+ distance: this._direction ? e.clientY : e.clientX,
};
+ this._dragLine.show();
+
this._moveObserver = scene.onPointerMoveObserver.add((e: IPointerEvent | IMouseEvent) => {
this.dragMoving(e);
});
this._upObserver = scene.onPointerUpObserver.add((e: IPointerEvent | IMouseEvent) => {
- this.dragUp();
+ this.dragUp(e);
scene.onPointerMoveObserver.remove(this._moveObserver);
scene.onPointerUpObserver.remove(this._upObserver);
scene.enableEvent();
@@ -97,10 +138,39 @@ export class DragLineController {
}
dragMoving(e: IPointerEvent | IMouseEvent) {
- this._dragLine.transformByState({
- left: this._state.left + e.clientX - this._state.clientX,
- });
+ if (this._direction) {
+ if (e.clientY < this._state.distance && this._state.distance - e.clientY >= this._end - this._start - 5) {
+ //留一丝空隙
+ return;
+ }
+ this._dragLine.transformByState({
+ top: e.clientY - this._state.distance,
+ });
+ } else {
+ if (e.clientX < this._state.distance && this._state.distance - e.clientX >= this._end - this._start - 5) {
+ //留一丝空隙
+ return;
+ }
+ this._dragLine.transformByState({
+ left: e.clientX - this._state.distance,
+ });
+ }
}
- dragUp() {}
+ dragUp(e: IPointerEvent | IMouseEvent) {
+ let distance;
+ if (this._direction) {
+ distance = e.clientY - this._state.distance;
+ if (e.clientY < this._state.distance && this._state.distance - e.clientY >= this._end - this._start - 5) {
+ distance = null;
+ }
+ } else {
+ distance = e.clientX - this._state.distance;
+ if (e.clientX < this._state.distance && this._state.distance - e.clientX >= this._end - this._start - 5) {
+ distance = null;
+ }
+ }
+ this._dragUp(distance);
+ this._dragLine.dispose();
+ }
}
diff --git a/packages/base-sheets/src/Controller/Selection/RowTitleController.ts b/packages/base-sheets/src/Controller/Selection/RowTitleController.ts
new file mode 100644
index 0000000000..cbcdc730d4
--- /dev/null
+++ b/packages/base-sheets/src/Controller/Selection/RowTitleController.ts
@@ -0,0 +1,138 @@
+import { IMouseEvent, IPointerEvent, Rect } from '@univer/base-render';
+import { Nullable } from '@univer/core';
+import { DragLineDirection } from './DragLineController';
+import { SelectionManager } from './SelectionManager';
+
+export class RowTitleController {
+ private _manager: SelectionManager;
+
+ private _leftTopHeight: number;
+
+ private _startOffsetX: number = 0;
+
+ private _startOffsetY: number = 0;
+
+ private _index: number = 0;
+
+ private _currentHeight: number = 0;
+
+ private _highlightItem: Rect;
+
+ constructor(manager: SelectionManager) {
+ this._manager = manager;
+ this._leftTopHeight = this._manager.getSheetView().getSpreadsheetLeftTopPlaceholder().getState().height;
+
+ // 创建高亮item
+ this._highlightItem = new Rect('HighLightRowTitle', {
+ width: this._manager.getSheetView().getSpreadsheetSkeleton().rowTitleWidth,
+ height: 0,
+ top: 0,
+ fill: 'rgb(220,220,220,0.5)',
+ });
+
+ this._highlightItem.hide();
+
+ this._highlightItem.evented = false;
+
+ const scene = this._manager.getScene();
+
+ scene.addObject(this._highlightItem, 3);
+ }
+
+ pointerDown(e: IPointerEvent | IMouseEvent) {
+ const main = this._manager.getMainComponent();
+ const { offsetX: evtOffsetX, offsetY: evtOffsetY } = e;
+ this._startOffsetX = evtOffsetX;
+ this._startOffsetY = evtOffsetY;
+ const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);
+ const contentRef = this._manager.getPlugin().getSheetContainerControl().getContentRef();
+ const clientY = e.clientY + scrollXY.y - this._leftTopHeight - contentRef.current!.getBoundingClientRect().top;
+ const rowHeightAccumulation = main.getSkeleton()?.rowHeightAccumulation ?? [];
+
+ for (let i = 0; i < rowHeightAccumulation?.length; i++) {
+ if (rowHeightAccumulation[i] >= clientY) {
+ this._index = i;
+ break;
+ }
+ }
+ // 当前列宽度
+ this._currentHeight = rowHeightAccumulation[0];
+ if (this._index) {
+ this._currentHeight = rowHeightAccumulation[this._index] - rowHeightAccumulation[this._index - 1];
+ }
+
+ // 显示拖动线
+ if (rowHeightAccumulation[this._index] - clientY <= 5) {
+ const end = rowHeightAccumulation[this._index] + this._leftTopHeight;
+ const start = (this._index ? rowHeightAccumulation[this._index - 1] : 0) + this._leftTopHeight;
+
+ this._manager.getDragLineControl().create({
+ direction: DragLineDirection.HORIZONTAL,
+ end,
+ start,
+ dragUp: this.setRowHeight.bind(this),
+ });
+ this._manager.getDragLineControl().dragDown(e);
+ }
+ // 高亮当前行
+ this.highlightRow();
+ }
+
+ setRowHeight(height: Nullable<number>) {
+ const plugin = this._manager.getPlugin();
+ const sheet = plugin.getContext().getWorkBook().getActiveSheet();
+ if (height === null) {
+ sheet.setRowHeights(this._index, 1, 5);
+ } else {
+ sheet.setRowHeights(this._index, 1, height! + this._currentHeight);
+ }
+ this.highlightRow();
+ }
+
+ highlightRow() {
+ this._manager.clearSelectionControls();
+ const sheet = this._manager.getPlugin().getWorkbook().getActiveSheet();
+ this._manager.addControlToCurrentByRangeData({
+ startRow: this._index,
+ startColumn: 0,
+ endColumn: sheet.getColumnCount() - 1,
+ endRow: this._index,
+ });
+ }
+
+ highlightRowTitle(e: IPointerEvent | IMouseEvent) {
+ const main = this._manager.getMainComponent();
+ const { offsetX: evtOffsetX, offsetY: evtOffsetY } = e;
+ this._startOffsetX = evtOffsetX;
+ this._startOffsetY = evtOffsetY;
+ const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);
+ const contentRef = this._manager.getPlugin().getSheetContainerControl().getContentRef();
+ const clientY = e.clientY + scrollXY.y - this._leftTopHeight - contentRef.current!.getBoundingClientRect().top;
+ const rowHeightAccumulation = main.getSkeleton()?.rowHeightAccumulation ?? [];
+
+ for (let i = 0; i < rowHeightAccumulation?.length; i++) {
+ if (rowHeightAccumulation[i] >= clientY) {
+ this._index = i;
+ break;
+ }
+ }
+ // 当前行高
+ this._currentHeight = rowHeightAccumulation[0];
+ let top = this._leftTopHeight;
+ if (this._index) {
+ this._currentHeight = rowHeightAccumulation[this._index] - rowHeightAccumulation[this._index - 1];
+ top = this._leftTopHeight + rowHeightAccumulation[this._index - 1];
+ }
+
+ this._highlightItem.transformByState({
+ height: this._currentHeight,
+ top,
+ });
+
+ this._highlightItem.show();
+ }
+
+ unHighlightRowTitle() {
+ this._highlightItem.hide();
+ }
+}
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
index 9b6332d97d..2e48cefb55 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
@@ -7,6 +7,7 @@ import { SheetPlugin } from '../../SheetPlugin';
import { SheetView } from '../../View/Render/Views/SheetView';
import { ColumnTitleController } from './ColumnTitleController';
import { DragLineController } from './DragLineController';
+import { RowTitleController } from './RowTitleController';
import { SelectionControl, SELECTION_TYPE } from './SelectionController';
/**
@@ -43,6 +44,8 @@ export class SelectionManager {
private _columnTitleControl: ColumnTitleController;
+ private _rowTitleControl: RowTitleController;
+
private _dragLineControl: DragLineController;
hasSelection: boolean = false;
@@ -414,6 +417,8 @@ export class SelectionManager {
this._dragLineControl = new DragLineController(this);
this._columnTitleControl = new ColumnTitleController(this);
+
+ this._rowTitleControl = new RowTitleController(this);
}
private _mainEventInitial() {
@@ -624,7 +629,16 @@ export class SelectionManager {
private _rowEventInitial() {
const row = this._rowComponent;
row.onPointerDownObserver.add((evt: IPointerEvent | IMouseEvent) => {
- console.log('rowTitle_moveObserver', evt);
+ this._rowTitleControl.pointerDown(evt);
+ });
+ row.onPointerEnterObserver.add((evt: IPointerEvent | IMouseEvent) => {
+ this._rowTitleControl.highlightRowTitle(evt);
+ });
+ row.onPointerMoveObserver.add((evt: IPointerEvent | IMouseEvent) => {
+ this._rowTitleControl.highlightRowTitle(evt);
+ });
+ row.onPointerLeaveObserver.add(() => {
+ this._rowTitleControl.unHighlightRowTitle();
});
}
@@ -632,7 +646,15 @@ export class SelectionManager {
const column = this._columnComponent;
column.onPointerDownObserver.add((evt: IPointerEvent | IMouseEvent) => {
this._columnTitleControl.pointerDown(evt);
- console.log('columnTitle_moveObserver', evt);
+ });
+ column.onPointerEnterObserver.add((evt: IPointerEvent | IMouseEvent) => {
+ this._columnTitleControl.highlightColumnTitle(evt);
+ });
+ column.onPointerMoveObserver.add((evt: IPointerEvent | IMouseEvent) => {
+ this._columnTitleControl.highlightColumnTitle(evt);
+ });
+ column.onPointerLeaveObserver.add(() => {
+ this._columnTitleControl.unHighlightColumnTitle();
});
}
diff --git a/packages/base-sheets/src/Controller/ToolBarController.ts b/packages/base-sheets/src/Controller/ToolBarController.ts
index c71c06a831..9d198cd860 100644
--- a/packages/base-sheets/src/Controller/ToolBarController.ts
+++ b/packages/base-sheets/src/Controller/ToolBarController.ts
@@ -211,6 +211,7 @@ export class ToolBarController {
name: 'fontSize',
show: toolbarConfig.fontSize,
onClick: (fontSize: number) => {
+ console.dir(fontSize);
this._plugin.getObserver('onAfterChangeFontSizeObservable')?.notifyObservers(fontSize);
},
onKeyUp: (fontSize: number) => {
diff --git a/packages/style-universheet/src/Components/Menu/Menu.tsx b/packages/style-universheet/src/Components/Menu/Menu.tsx
index a2270cc98e..17b0660019 100644
--- a/packages/style-universheet/src/Components/Menu/Menu.tsx
+++ b/packages/style-universheet/src/Components/Menu/Menu.tsx
@@ -17,12 +17,9 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {
handleClick = (e: MouseEvent, item: BaseMenuItem, index: number) => {
const { deep = 0 } = this.props;
- // e.stopPropagation();
- if (item.onClick) {
- item.onClick.call(null, e, item.value, index, deep);
- } else if (this.props.onClick) {
- this.props.onClick.call(null, e, item.value, index, deep);
- }
+
+ item.onClick?.call(null, e, item.value, index, deep);
+ this.props.onClick?.call(null, e, item.value, index, deep);
this.showMenu(false);
};