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: dbclick to fill range (#553)

YuHongb240b53ce67d
diff --git a/packages/ui-plugin-sheets/src/commands/commands/__tests__/auto-fill.controller.spec.ts b/packages/ui-plugin-sheets/src/commands/commands/__tests__/auto-fill.controller.spec.ts
index 7fd760e00b..9f8a0baebb 100644
--- a/packages/ui-plugin-sheets/src/commands/commands/__tests__/auto-fill.controller.spec.ts
+++ b/packages/ui-plugin-sheets/src/commands/commands/__tests__/auto-fill.controller.spec.ts
@@ -149,6 +149,55 @@ const TEST_WORKBOOK_DATA = {
t: CellValueType.STRING,
},
},
+ // db click to fill
+ '10': {
+ '0': {
+ v: 1,
+ m: '1',
+ t: CellValueType.NUMBER,
+ },
+ '1': {
+ v: 2,
+ m: '2',
+ t: CellValueType.NUMBER,
+ },
+ '2': {
+ v: 3,
+ m: '3',
+ t: CellValueType.NUMBER,
+ },
+ '3': {
+ v: 4,
+ m: '4',
+ t: CellValueType.NUMBER,
+ },
+ },
+ '11': {
+ '0': {
+ v: 2,
+ m: '2',
+ t: CellValueType.NUMBER,
+ },
+ '2': {
+ v: 3,
+ m: '3',
+ t: CellValueType.NUMBER,
+ },
+ },
+ '12': {
+ '0': {
+ v: 3,
+ m: '3',
+ t: CellValueType.NUMBER,
+ },
+ },
+ '13': {
+ '0': {
+ v: 4,
+ m: '4',
+ t: CellValueType.NUMBER,
+ },
+ },
},
},
},
@@ -194,7 +243,7 @@ describe('Test auto fill rules in controller', () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
// test number
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 0,
startColumn: 0,
@@ -211,7 +260,7 @@ describe('Test auto fill rules in controller', () => {
expect(workbook.getSheetBySheetId('sheet1')?.getCell(0, 2)?.v).toBe(3);
expect(workbook.getSheetBySheetId('sheet1')?.getCell(0, 3)?.v).toBe(4);
// test number. descending
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 1,
startColumn: 0,
@@ -235,7 +284,7 @@ describe('Test auto fill rules in controller', () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
// test extend number
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 2,
startColumn: 0,
@@ -259,7 +308,7 @@ describe('Test auto fill rules in controller', () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
// test chinese number
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 3,
startColumn: 0,
@@ -284,7 +333,7 @@ describe('Test auto fill rules in controller', () => {
if (!workbook) throw new Error('This is an error');
// test chinese week
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 4,
startColumn: 0,
@@ -308,7 +357,7 @@ describe('Test auto fill rules in controller', () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
// test loop series
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 5,
startColumn: 0,
@@ -332,7 +381,7 @@ describe('Test auto fill rules in controller', () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
// test other string
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 6,
startColumn: 0,
@@ -356,7 +405,7 @@ describe('Test auto fill rules in controller', () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
// test mixed mode
- (autoFillController as any)._handleFillDrag(
+ (autoFillController as any)._handleFill(
{
startRow: 7,
startColumn: 0,
@@ -390,4 +439,32 @@ describe('Test auto fill rules in controller', () => {
});
});
});
+
+ describe('auto fill range is auto detected', async () => {
+ it('correct situation', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+ // test other string
+ (autoFillController as any)._handleDbClickFill({
+ startRow: 10,
+ startColumn: 1,
+ endRow: 10,
+ endColumn: 1,
+ });
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(11, 1)?.v).toBe(3);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(12, 1)?.v).toBe(4);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(13, 1)?.v).toBe(5);
+
+ // undo redo
+ await commandService.executeCommand(UndoCommand.id);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(11, 1)?.v).toBe(undefined);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(12, 1)?.v).toBe(undefined);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(13, 1)?.v).toBe(undefined);
+
+ await commandService.executeCommand(RedoCommand.id);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(11, 1)?.v).toBe(3);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(12, 1)?.v).toBe(4);
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(13, 1)?.v).toBe(5);
+ });
+ });
});
diff --git a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
index 378346f102..386cd34706 100644
--- a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
@@ -49,7 +49,76 @@ export class AutoFillController extends Disposable {
this._init();
}
- private _handleFillDrag(sourceRange: IRange, destRange: IRange) {
+ private _init() {
+ this._onSelectionControlFillChanged();
+ this._onApplyTypeChanged();
+ [AutoFillCommand, AutoClearContentCommand].forEach((command) => {
+ this.disposeWithMe(this._commandService.registerCommand(command));
+ });
+ }
+
+ private _handleDbClickFill(sourceRange: IRange) {
+ const destRange = this._detectFillRange(sourceRange);
+ // double click only works when dest range is longer than source range
+ if (destRange.endRow <= sourceRange.endRow) {
+ return;
+ }
+ // double click effect is the same as drag effect, but the apply area is automatically calculated (by method '_detectFillRange')
+ this._handleFill(sourceRange, destRange);
+ }
+
+ private _detectFillRange(sourceRange: IRange) {
+ const { startRow, endRow, startColumn, endColumn } = sourceRange;
+ const worksheet = this._univerInstanceService.getCurrentUniverSheetInstance()?.getActiveSheet();
+ if (!worksheet) {
+ return sourceRange;
+ }
+ const matrix = worksheet.getCellMatrix();
+ const maxRow = worksheet.getMaxRows() - 1;
+ const maxColumn = worksheet.getMaxColumns() - 1;
+ let detectEndRow = endRow;
+ // left column first, or consider right column.
+ if (startColumn > 0 && matrix.getValue(startRow, startColumn - 1)?.v != null) {
+ let cur = startRow;
+ while (matrix.getValue(cur, startColumn - 1)?.v != null && cur < maxRow) {
+ cur += 1;
+ }
+ detectEndRow = cur;
+ } else if (endColumn < maxColumn && matrix.getValue(endRow, endColumn + 1)?.v != null) {
+ let cur = startRow;
+ while (matrix.getValue(cur, endColumn + 1)?.v != null && cur < maxRow) {
+ cur++;
+ }
+ detectEndRow = cur;
+ }
+
+ for (let i = endRow + 1; i <= detectEndRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ if (matrix.getValue(i, j)?.v != null) {
+ detectEndRow = i - 1;
+ break;
+ }
+ }
+ }
+
+ return {
+ startColumn,
+ endColumn,
+ startRow,
+ endRow: detectEndRow,
+ };
+ }
+
+ private _handleFill(sourceRange: IRange, destRange: IRange) {
+ // if source range === dest range, do nothing;
+ if (
+ sourceRange.startColumn === destRange.startColumn &&
+ sourceRange.startRow === destRange.startRow &&
+ sourceRange.endColumn === destRange.endColumn &&
+ sourceRange.endRow === destRange.endRow
+ ) {
+ return;
+ }
// situation 1: drag to smaller range, horizontally.
if (destRange.endColumn < sourceRange.endColumn && destRange.endColumn > sourceRange.startColumn) {
this._commandService.executeCommand(AutoClearContentCommand.id, {
@@ -80,14 +149,6 @@ export class AutoFillController extends Disposable {
this._presetAndCacheData(sourceRange, destRange);
}
- private _init() {
- this._onSelectionControlFillChanged();
- this._onApplyTypeChanged();
- [AutoFillCommand, AutoClearContentCommand].forEach((command) => {
- this.disposeWithMe(this._commandService.registerCommand(command));
- });
- }
-
private _onSelectionControlFillChanged() {
const disposableCollection = new DisposableCollection();
this.disposeWithMe(
@@ -114,36 +175,36 @@ export class AutoFillController extends Disposable {
if (filled == null) {
return;
}
- const { startColumn, endColumn, startRow, endRow } = controlSelection.model;
- const {
- startColumn: newStartColumn,
- endColumn: newEndColumn,
- startRow: newStartRow,
- endRow: newEndRow,
- } = filled || {};
- // if no change happened, return
- if (
- startColumn === newStartColumn &&
- endColumn === newEndColumn &&
- startRow === newStartRow &&
- endRow === newEndRow
- ) {
- return;
- }
+ const sourceRange: IRange = {
+ startColumn: controlSelection.model.startColumn,
+ endColumn: controlSelection.model.endColumn,
+ startRow: controlSelection.model.startRow,
+ endRow: controlSelection.model.endRow,
+ };
+ const destRange: IRange = {
+ startColumn: filled.startColumn,
+ endColumn: filled.endColumn,
+ startRow: filled.startRow,
+ endRow: filled.endRow,
+ };
+ this._handleFill(sourceRange, destRange);
+ })
+ )
+ );
+
+ // double click to fill range, range length will align to left or right column.
+ // fill results will be as same as drag operation
+ disposableCollection.add(
+ toDisposable(
+ controlSelection.fillControl.onDblclickObserver.add(() => {
const sourceRange = {
- startColumn,
- endColumn,
- startRow,
- endRow,
- };
- const destRange = {
- startColumn: newStartColumn || startColumn,
- endColumn: newEndColumn || endColumn,
- startRow: newStartRow || startRow,
- endRow: newEndRow || endRow,
+ startColumn: controlSelection.model.startColumn,
+ endColumn: controlSelection.model.endColumn,
+ startRow: controlSelection.model.startRow,
+ endRow: controlSelection.model.endRow,
};
- this._handleFillDrag(sourceRange, destRange);
+ this._handleDbClickFill(sourceRange);
})
)
);