Skip to content

Commit

Permalink
feat: add api getSelectedCellRanges
Browse files Browse the repository at this point in the history
  • Loading branch information
fangsmile committed Mar 25, 2024
1 parent 8c3384c commit 7792654
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"changes": [
{
"comment": "feat: add excel options to support fill handle\n\n",
"type": "none",
"type": "minor",
"packageName": "@visactor/vtable"
}
],
Expand Down
6 changes: 5 additions & 1 deletion packages/vtable/examples/interactive/fill-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,15 @@ export function createTable() {
};
const tableInstance = new VTable.ListTable(option);
window.tableInstance = tableInstance;
let startSelectCellRanges;
tableInstance.on('mousedown_fill_handle', arg => {
console.log('mousedown_fill_handle');
startSelectCellRanges = tableInstance.getSelectedCellRanges();
});
tableInstance.on('drag_fill_handle_end', arg => {
console.log('drag_fill_handle_end');
console.log('drag_fill_handle_end', arg);
const values = [[1], [2], [3]];
tableInstance.changeCellValues(startSelectCellRanges[0].end.col, startSelectCellRanges[0].end.row, values);
});
tableInstance.on('dblclick_fill_handle', arg => {
console.log('dblclick_fill_handle');
Expand Down
19 changes: 18 additions & 1 deletion packages/vtable/src/core/BaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ import { HeaderHelper } from '../header-helper/header-helper';
import type { PivotHeaderLayoutMap } from '../layout/pivot-header-layout';
import { TooltipHandler } from '../components/tooltip/TooltipHandler';
import type { CachedDataSource, DataSource } from '../data';
import { AABBBounds, isNumber, isBoolean, isFunction, type ITextSize, isValid, merge } from '@visactor/vutils';
import {
AABBBounds,
isNumber,
isBoolean,
isFunction,
type ITextSize,
isValid,
merge,
cloneDeep
} from '@visactor/vutils';
import { textMeasure } from '../scenegraph/utils/text-measure';
import { getProp } from '../scenegraph/utils/get-prop';
import type {
Expand Down Expand Up @@ -3661,6 +3670,14 @@ export abstract class BaseTable extends EventTarget implements BaseTableAPI {
}
return cellInfoArray;
}
getSelectedCellRanges(): CellRange[] {
const ranges = this.stateManager.select.ranges;
if (!ranges.length) {
return [];
}
return cloneDeep(ranges);
}

/** 计算字体的宽度接口 */
measureText(text: string, font: { fontSize: number; fontWeight: string | number; fontFamily: string }): ITextSize {
return textMeasure.measureText(text, font);
Expand Down
18 changes: 14 additions & 4 deletions packages/vtable/src/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,27 @@ export class EventManager {
let updateCol;
const currentRange = this.table.stateManager.select.ranges[this.table.stateManager.select.ranges.length - 1];
if (isSelectMoving) {
if (!isValid(this.table.stateManager.fillHandle.direcitonRow)) {
if (!isValid(this.table.stateManager.fillHandle.directionRow)) {
if (
Math.abs(this.table.stateManager.fillHandle.startY - eventArgsSet.abstractPos.y) >=
Math.abs(this.table.stateManager.fillHandle.startX - eventArgsSet.abstractPos.x)
) {
this.table.stateManager.fillHandle.direcitonRow = true;
this.table.stateManager.fillHandle.directionRow = true;
if (this.table.stateManager.fillHandle.startY - eventArgsSet.abstractPos.y > 0) {
this.table.stateManager.fillHandle.direction = 'top';
} else {
this.table.stateManager.fillHandle.direction = 'bottom';
}
} else {
this.table.stateManager.fillHandle.direcitonRow = false;
this.table.stateManager.fillHandle.directionRow = false;
if (this.table.stateManager.fillHandle.startX - eventArgsSet.abstractPos.x > 0) {
this.table.stateManager.fillHandle.direction = 'left';
} else {
this.table.stateManager.fillHandle.direction = 'right';
}
}
}
if (this.table.stateManager.fillHandle.direcitonRow) {
if (this.table.stateManager.fillHandle.directionRow) {
updateRow = eventArgs.row;
updateCol = currentRange.end.col;
} else {
Expand Down
8 changes: 5 additions & 3 deletions packages/vtable/src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export class StateManager {
selecting: boolean;
};
fillHandle: {
direcitonRow?: boolean;
direction?: 'top' | 'bottom' | 'left' | 'right';
directionRow?: boolean;
isFilling: boolean;
startX: number;
startY: number;
Expand Down Expand Up @@ -603,8 +604,9 @@ export class StateManager {
this.fillHandle.isFilling = false;
this.fillHandle.startX = undefined;
this.fillHandle.startY = undefined;
this.table.stateManager.fillHandle.direcitonRow = undefined;
this.table.eventManager.isDraging && this.table.fireListeners(TABLE_EVENT_TYPE.DRAG_FILL_HANDLE_END, {});
this.table.stateManager.fillHandle.directionRow = undefined;
this.table.eventManager.isDraging &&
this.table.fireListeners(TABLE_EVENT_TYPE.DRAG_FILL_HANDLE_END, { direction: this.fillHandle.direction });
}
updateResizeCol(xInTable: number, yInTable: number) {
updateResizeColumn(xInTable, yInTable, this);
Expand Down
1 change: 1 addition & 0 deletions packages/vtable/src/ts-types/base-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ export interface BaseTableAPI {
getCopyValue: () => string;

getSelectedCellInfos: () => CellInfo[][];
getSelectedCellRanges: () => CellRange[];
getCellInfo: (col: number, row: number) => Omit<MousePointerCellEvent, 'target'>;

showTooltip: (col: number, row: number, tooltipOptions?: TooltipOptions) => void;
Expand Down
2 changes: 1 addition & 1 deletion packages/vtable/src/ts-types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export interface TableEventHandlersEventArgumentMap {
change_cell_value: { col: number; row: number; rawValue: string | number; changedValue: string | number };

mousedown_fill_handle: {};
drag_fill_handle_end: {};
drag_fill_handle_end: { direction?: 'top' | 'bottom' | 'left' | 'right' };
dblclick_fill_handle: {};
}
export interface DrillMenuEventInfo {
Expand Down

0 comments on commit 7792654

Please sign in to comment.