Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sheet): formula show and add selection move #676

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/shared/array-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function orderSearchArray(arr: number[], pos: number) {
cur_pre = arr[i_ed - 1];
}

if (pos >= cur_pre && pos < cur) {
if (pos >= cur_pre && pos <= cur) {
cur_index = i_ed;
break;
}
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/sheets/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IKeyType, Nullable } from '../shared';
import { Tools } from '../shared';
import type { ICellData, IStyleData } from '../types/interfaces';
import type { ICellDataForSheetInterceptor, IStyleData } from '../types/interfaces';

/**
* Styles in a workbook, cells locate styles based on style IDs
Expand Down Expand Up @@ -88,14 +88,23 @@ export class Styles {
return this._styles;
}

getStyleByCell(cell: Nullable<ICellData>): Nullable<IStyleData> {
getStyleByCell(cell: Nullable<ICellDataForSheetInterceptor>): Nullable<IStyleData> {
let style;
if (cell && Tools.isObject(cell.s)) {
style = cell.s as IStyleData;
} else {
style = cell?.s && this.get(cell.s);
}

const interceptStyle = cell?.interceptorStyle;

if (interceptStyle) {
return {
...style,
...interceptStyle,
} as IStyleData;
}

return style as IStyleData;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/sheets/view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { IDisposable } from '@wendellhu/redi';
import { remove } from '../common/array';
import type { Nullable } from '../common/type-utils';
import { Disposable, toDisposable } from '../shared/lifecycle';
import type { ICellData } from '../types/interfaces/i-cell-data';
import type { ICellDataForSheetInterceptor } from '../types/interfaces/i-cell-data';

export interface ICellContentInterceptor {
getCell(row: number, col: number): Nullable<ICellData>;
getCell(row: number, col: number): Nullable<ICellDataForSheetInterceptor>;
}

export interface IRowFilteredInterceptor {}
Expand All @@ -21,7 +21,7 @@ export interface ISheetViewModel {
registerRowVisibleInterceptor(interceptor: IRowVisibleInterceptor): IDisposable;
registerColVisibleInterceptor(interceptor: IColVisibleInterceptor): IDisposable;

getCell(row: number, col: number): Nullable<ICellData>;
getCell(row: number, col: number): Nullable<ICellDataForSheetInterceptor>;
}

/**
Expand All @@ -42,7 +42,7 @@ export class SheetViewModel extends Disposable implements ISheetViewModel {
this._colVisibleInterceptors.length = 0;
}

getCell(row: number, col: number): Nullable<ICellData> {
getCell(row: number, col: number): Nullable<ICellDataForSheetInterceptor> {
for (const interceptor of this._cellContentInterceptors) {
const result = interceptor.getCell(row, col);
if (typeof result !== 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createRowColIter } from '../shared/row-col-iter';
import { DEFAULT_WORKSHEET } from '../types/const';
import type { SheetTypes } from '../types/enum';
import { BooleanNumber } from '../types/enum';
import type { ICellData, IFreeze, IRange, IWorksheetData } from '../types/interfaces';
import type { ICellData, ICellDataForSheetInterceptor, IFreeze, IRange, IWorksheetData } from '../types/interfaces';
import { ColumnManager } from './column-manager';
import { Range } from './range';
import { RowManager } from './row-manager';
Expand Down Expand Up @@ -165,7 +165,7 @@ export class Worksheet {
return null;
}

getCell(row: number, col: number): Nullable<ICellData> {
getCell(row: number, col: number): Nullable<ICellDataForSheetInterceptor> {
if (row < 0 || col < 0) {
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/interfaces/i-cell-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface ICellData {
si?: Nullable<string>; // formula id
}

export interface ICellDataForSheetInterceptor extends ICellData {
interceptorStyle?: Nullable<IStyleData>;
isInArrayFormulaRange?: Nullable<boolean>;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isICellData(value: any): value is ICellData {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

Expand Down Expand Up @@ -101,7 +101,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
univerInstanceService.focusUniverInstance('test');

const logService = get(ILogService);
logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
logService.setLogLevel(LogLevel.SILENT); // change this to `true` to debug tests via logs

return {
univer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

Expand Down Expand Up @@ -73,7 +73,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
univerInstanceService.focusUniverInstance('test');

const logService = get(ILogService);
logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
logService.setLogLevel(LogLevel.SILENT); // change this to `true` to debug tests via logs

return {
univer,
Expand Down
39 changes: 32 additions & 7 deletions packages/engine-formula/src/models/formula-data.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ICellData, IRange, ObjectMatrixPrimitiveType } from '@univerjs/core';
import type { ICellData, IRange, Nullable, ObjectMatrixPrimitiveType } from '@univerjs/core';
import {
Disposable,
ICommandService,
Expand All @@ -21,12 +21,6 @@ import type {
} from '../basics/common';
import { FormulaEngineService } from '../services/formula-engine.service';

export interface IFormulaConfig {
notExecuteFormula?: boolean;

formulaData: IFormulaData;
}

export class FormulaDataModel extends Disposable {
private _formulaData: IFormulaData = {};

Expand Down Expand Up @@ -344,6 +338,37 @@ export class FormulaDataModel extends Disposable {
}
});
}

getFormulaItemBySId(sId: string, sheetId: string, unitId: string): Nullable<IFormulaDataItem> {
const formulaData = this._formulaData;
if (formulaData[unitId] == null) {
return null;
}
const workbookFormulaData = formulaData[unitId];

if (workbookFormulaData[sheetId] == null) {
return null;
}

const cellMatrix = new ObjectMatrix(workbookFormulaData[sheetId]);

let formulaDataItem: Nullable<IFormulaDataItem> = null;

cellMatrix.forValue((row, column, item) => {
const { f, si } = item;

if (si === sId && f.length > 0) {
formulaDataItem = item;
return false;
}
});

return formulaDataItem;
}

getFormulaDataItem(row: number, column: number, sheetId: string, unitId: string) {
return this._formulaData?.[unitId]?.[sheetId]?.[row]?.[column];
}
}

export function initSheetFormulaData(
Expand Down
17 changes: 11 additions & 6 deletions packages/engine-render/src/basics/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,10 @@ export function getCellPositionByIndex(
const startColumn = column - 1;

const startY = rowHeightAccumulation[startRow] || 0;
const endY = rowHeightAccumulation[row];
const endY = rowHeightAccumulation[row] || rowHeightAccumulation[rowHeightAccumulation.length - 1];

const startX = columnWidthAccumulation[startColumn] || 0;
const endX = columnWidthAccumulation[column];
const endX = columnWidthAccumulation[column] || columnWidthAccumulation[columnWidthAccumulation.length - 1];

return {
startY,
Expand Down Expand Up @@ -503,12 +503,17 @@ export function getCellByIndex(
startX: 0,
endX: 0,
};

const rowAccumulationCount = rowHeightAccumulation.length - 1;

const columnAccumulationCount = columnWidthAccumulation.length - 1;

if (isMerged && startRow !== -1 && startColumn !== -1) {
const mergeStartY = rowHeightAccumulation[startRow - 1] || 0;
const mergeEndY = rowHeightAccumulation[endRow];
const mergeEndY = rowHeightAccumulation[endRow] || rowHeightAccumulation[rowAccumulationCount];

const mergeStartX = columnWidthAccumulation[startColumn - 1] || 0;
const mergeEndX = columnWidthAccumulation[endColumn];
const mergeEndX = columnWidthAccumulation[endColumn] || columnWidthAccumulation[columnAccumulationCount];
mergeInfo = {
...mergeInfo,
startY: mergeStartY,
Expand All @@ -517,8 +522,8 @@ export function getCellByIndex(
endX: mergeEndX,
};
} else if (!isMerged && endRow !== -1 && endColumn !== -1) {
const mergeEndY = rowHeightAccumulation[endRow] || 0;
const mergeEndX = columnWidthAccumulation[endColumn] || 0;
const mergeEndY = rowHeightAccumulation[endRow] || rowHeightAccumulation[rowAccumulationCount];
const mergeEndX = columnWidthAccumulation[endColumn] || columnWidthAccumulation[columnAccumulationCount];

mergeInfo = {
...mergeInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,16 @@ export class BorderAuxiliary extends SheetExtension {
const { startRow, endRow, startColumn, endColumn } = dataCache;

const startY = fixLineWidthByScale(rowHeightAccumulation[startRow - 1] || 0, scale);
const endY = fixLineWidthByScale(rowHeightAccumulation[endRow], scale);
const endY = fixLineWidthByScale(
rowHeightAccumulation[endRow] || rowHeightAccumulation[rowHeightAccumulation.length - 1],
scale
);

const startX = fixLineWidthByScale(columnWidthAccumulation[startColumn - 1] || 0, scale);
const endX = fixLineWidthByScale(columnWidthAccumulation[endColumn], scale);
const endX = fixLineWidthByScale(
columnWidthAccumulation[endColumn] || columnWidthAccumulation[columnWidthAccumulation.length - 1],
scale
);

ctx.clearRect(startX, startY, endX - startX, endY - startY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,16 @@ export class Font extends SheetExtension {
columnWidthAccumulation: number[]
) {
const startY = fixLineWidthByScale(rowHeightAccumulation[startRow - 1] || 0, scale);
const endY = fixLineWidthByScale(rowHeightAccumulation[endRow], scale);
const endY = fixLineWidthByScale(
rowHeightAccumulation[endRow] || rowHeightAccumulation[rowHeightAccumulation.length - 1],
scale
);

const startX = fixLineWidthByScale(columnWidthAccumulation[startColumn - 1] || 0, scale);
const endX = fixLineWidthByScale(columnWidthAccumulation[endColumn], scale);
const endX = fixLineWidthByScale(
columnWidthAccumulation[endColumn] || columnWidthAccumulation[columnWidthAccumulation.length - 1],
scale
);

ctx.rect(startX, startY, endX - startX, endY - startY);
}
Expand Down
23 changes: 7 additions & 16 deletions packages/engine-render/src/scene.input-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import type { Viewport } from './viewport';

export class InputManager {
/** The distance in pixel that you have to move to prevent some events */
static DragMovementThreshold = 10; // in pixels
static DragMovementThreshold = 2; // in pixels

/** Time in milliseconds to wait to raise long press events if button is still pressed */
static LongPressDelay = 500; // in milliseconds

/** Time in milliseconds with two consecutive clicks will be considered as a double or triple click */
static DoubleOrTripleClickDelay = 600; // in milliseconds
static DoubleOrTripleClickDelay = 500; // in milliseconds

/** If you need to check double click without raising a single click at first click, enable this flag */
static ExclusiveDoubleClickMode = false;
Expand Down Expand Up @@ -330,28 +330,18 @@ export class InputManager {
private _prePointerDoubleOrTripleClick(evt: IPointerEvent) {
const { clientX, clientY } = evt;

if (this._doubleClickOccurred === 0) {
this._startingPosition.x = clientX;
this._startingPosition.y = clientY;
}

this._doubleClickOccurred += 1;

this._delayedTimeout = setTimeout(() => {
this._resetDoubleClickParam();
}, InputManager.DoubleOrTripleClickDelay);

if (this._doubleClickOccurred < 2) {
return;
}

const isMoveThreshold = this._isPointerSwiping(clientX, clientY);

if (isMoveThreshold) {
this._resetDoubleClickParam();
return;
}

this._doubleClickOccurred += 1;

if (this._doubleClickOccurred === 2) {
this._scene?.pick(Vector2.FromArray([evt.offsetX, evt.offsetY]))?.triggerDblclick(evt);

Expand All @@ -370,12 +360,13 @@ export class InputManager {

this._resetDoubleClickParam();
}

this._startingPosition.x = clientX;
this._startingPosition.y = clientY;
}

private _resetDoubleClickParam() {
this._doubleClickOccurred = 0;
this._startingPosition.x = Infinity;
this._startingPosition.y = Infinity;
clearTimeout(this._delayedTimeout);
}
}
Loading
Loading