Skip to content

Commit

Permalink
chore(refactor): init merge cell service
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Jan 5, 2024
1 parent a24149b commit ce1d8b5
Show file tree
Hide file tree
Showing 24 changed files with 802 additions and 677 deletions.
15 changes: 13 additions & 2 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export class Worksheet {
return new Worksheet(copy, this._styles);
}

/**
* @deprecated use merge-cell-service
* @return {*} {IRange[]}
* @memberof Worksheet
*/
getMergeData(): IRange[] {
return this._snapshot.mergeData;
}
Expand Down Expand Up @@ -194,12 +199,18 @@ export class Worksheet {
}

/**
* @deprecated @TODO:@gggpound should migrate to the service
* Get cell matrix from a given range and pick out non-first cells of merged cells.
*
* Notice that `ICellData` here is not after copying. In another word, the object matrix here should be
* considered as a slice of the original worksheet data matrix.
* PERF: we could not skip indexes with merged cells, because we have already known the merged cells' range
* @param {number} row
* @param {number} col
* @param {number} endRow
* @param {number} endCol
* @return {*} {(ObjectMatrix<ICellData & { rowSpan?: number; colSpan?: number }>)}
* @memberof Worksheet
*/
// PERF: we could not skip indexes with merged cells, because we have already known the merged cells' range
getMatrixWithMergedCells(
row: number,
col: number,
Expand Down
18 changes: 17 additions & 1 deletion packages/engine-render/src/basics/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,16 @@ export function getCellPositionByIndex(
};
}

/**
* @deprecated @TODO:@gggpound should migrate to the service
* @export
* @param {number} row
* @param {number} column
* @param {number[]} rowHeightAccumulation
* @param {number[]} columnWidthAccumulation
* @param {IRange[]} mergeData
* @return {*} {ISelectionCellWithCoord}
*/
export function getCellByIndex(
row: number,
column: number,
Expand Down Expand Up @@ -560,7 +570,13 @@ export function getCellByIndex(
}

/**
* Determines whether the cell(row, column) is within the range of the merged cells.
* @deprecated use `getMergedCell` instead
* Determines whether the cell(row, column) is within the range of the merged cells
* @export
* @param {number} row
* @param {number} column
* @param {IRange[]} [mergeData]
* @return {*} {ISelectionCell}
*/
export function getCellInfoInMergeData(row: number, column: number, mergeData?: IRange[]): ISelectionCell {
let isMerged = false; // The upper left cell only renders the content
Expand Down
3 changes: 0 additions & 3 deletions packages/engine-render/src/components/sheets/spreadsheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ export class Spreadsheet extends SheetComponent {
}

override draw(ctx: UniverRenderingContext, bounds?: IViewportBound) {
// const { parent = { scaleX: 1, scaleY: 1 } } = this;
// const mergeData = this.getMergeData();
// const showGridlines = this.getShowGridlines() || 1;
const spreadsheetSkeleton = this.getSkeleton();
if (!spreadsheetSkeleton) {
return;
Expand Down
8 changes: 3 additions & 5 deletions packages/sheets-find/src/domain/text-find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import type { ICellData, IGridRange, Nullable, Range, Worksheet } from '@univerjs/core';
import { FormatType, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';
import { SelectionManagerService, transformCellDataToSelectionData } from '@univerjs/sheets';
import { ISelectionRenderService } from '@univerjs/sheets-ui';
import { MergeCellService, SelectionManagerService, transformCellDataToSelectionData } from '@univerjs/sheets';
import { Inject } from '@wendellhu/redi';

import type { FindType } from '../i-data';
Expand Down Expand Up @@ -50,8 +49,7 @@ export class TextFinder {
constructor(
@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,
@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
@ISelectionRenderService
private readonly _selectionRenderService: ISelectionRenderService
@Inject(MergeCellService) private _mergeCellService: MergeCellService
) {}

/**
Expand Down Expand Up @@ -341,7 +339,7 @@ export class TextFinder {
const workbook = this._currentUniverService.getCurrentUniverSheetInstance();
const unitId = workbook.getUnitId();
const sheetId = range.sheetId;
const mergeData = workbook.getActiveSheet().getMergeData();
const mergeData = this._mergeCellService.getMergeData(unitId, sheetId);

const selectionRange = transformCellDataToSelectionData(
range.range.startColumn,
Expand Down
15 changes: 6 additions & 9 deletions packages/sheets-formula/src/controllers/prompt.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,12 @@ import {
normalizeSheetName,
sequenceNodeType,
} from '@univerjs/engine-formula';
import {
DeviceInputEventType,
getCellInfoInMergeData,
IRenderManagerService,
ITextSelectionRenderManager,
} from '@univerjs/engine-render';
import { DeviceInputEventType, IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/engine-render';
import type { ISelectionWithStyle } from '@univerjs/sheets';
import {
convertSelectionDataToRange,
getNormalSelectionStyle,
MergeCellService,
NORMAL_SELECTION_PLUGIN_NAME,
SelectionManagerService,
} from '@univerjs/sheets';
Expand Down Expand Up @@ -155,7 +151,8 @@ export class PromptController extends Disposable {
@Inject(IDescriptionService) private readonly _descriptionService: IDescriptionService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
@IFormulaInputService private readonly _formulaInputService: IFormulaInputService,
@Inject(DocViewModelManagerService) private readonly _docViewModelManagerService: DocViewModelManagerService
@Inject(DocViewModelManagerService) private readonly _docViewModelManagerService: DocViewModelManagerService,
@Inject(MergeCellService) private _mergeCellService: MergeCellService
) {
super();

Expand Down Expand Up @@ -1369,7 +1366,7 @@ export class PromptController extends Disposable {
return;
}

const { unitId, sheetId, skeleton } = this._getCurrentUnitIdAndSheetId();
const { unitId, sheetId } = this._getCurrentUnitIdAndSheetId();

this._formulaInputService.enableLockedSelectionChange();

Expand All @@ -1381,7 +1378,7 @@ export class PromptController extends Disposable {

let { startRow, endRow, startColumn, endColumn } = toRange;

const primary = getCellInfoInMergeData(startRow, startColumn, skeleton?.mergeData);
const primary = this._mergeCellService.getCellInfoInfo(unitId, sheetId, startRow, startColumn);

if (primary) {
const {
Expand Down
35 changes: 16 additions & 19 deletions packages/sheets-ui/src/controllers/auto-fill.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
toDisposable,
Tools,
} from '@univerjs/core';
import { DeviceInputEventType, getCellInfoInMergeData } from '@univerjs/engine-render';
import { DeviceInputEventType } from '@univerjs/engine-render';
import type {
IAddWorksheetMergeMutationParams,
IRemoveWorksheetMergeMutationParams,
Expand All @@ -41,6 +41,7 @@ import {
AddWorksheetMergeMutation,
ClearSelectionContentCommand,
getAddMergeMutationRangeByType,
MergeCellService,
NORMAL_SELECTION_PLUGIN_NAME,
RemoveMergeUndoMutationFactory,
RemoveWorksheetMergeMutation,
Expand Down Expand Up @@ -83,7 +84,8 @@ export class AutoFillController extends Disposable {
@IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService,
@Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,
@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
@Inject(Injector) private readonly _injector: Injector
@Inject(Injector) private readonly _injector: Injector,
@Inject(MergeCellService) private mergeCellService: MergeCellService
) {
super();
this._defaultHook = {
Expand Down Expand Up @@ -599,15 +601,15 @@ export class AutoFillController extends Disposable {
}

private _getMergeApplyData(source: IRange, target: IRange, direction: Direction, csLen: number) {
const mergeData = this._univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getMergeData();
const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
const applyMergeRanges = [];
for (let i = source.startRow; i <= source.endRow; i++) {
for (let j = source.startColumn; j <= source.endColumn; j++) {
const { isMergedMainCell, startRow, startColumn, endRow, endColumn } = getCellInfoInMergeData(
i,
j,
mergeData
);
const { isMergedMainCell, startRow, startColumn, endRow, endColumn } =
this.mergeCellService.getCellInfoInfo(unitId, subUnitId, i, j);
if (isMergedMainCell) {
if (direction === Direction.DOWN) {
let windowStartRow = startRow + csLen;
Expand Down Expand Up @@ -799,17 +801,12 @@ export class AutoFillController extends Disposable {
};
// delete cross merge
const deleteMergeRanges: IRange[] = [];
const mergeData = this._univerInstanceService
.getUniverSheetInstance(unitId)
?.getSheetBySheetId(subUnitId)
?.getMergeData();
if (mergeData) {
mergeData.forEach((merge) => {
if (Rectangle.intersects(merge, target)) {
deleteMergeRanges.push(merge);
}
});
}
const mergeData = this.mergeCellService.getMergeData(unitId, subUnitId);
mergeData.forEach((merge) => {
if (Rectangle.intersects(merge, target)) {
deleteMergeRanges.push(merge);
}
});
const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
unitId,
subUnitId,
Expand Down
51 changes: 24 additions & 27 deletions packages/sheets-ui/src/controllers/clipboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
import {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
MergeCellService,
MoveRangeCommand,
MoveRangeMutation,
NORMAL_SELECTION_PLUGIN_NAME,
Expand Down Expand Up @@ -352,6 +353,7 @@ export function getClearAndSetMergeMutations(
matrix: ObjectMatrix<ICellDataWithSpanInfo>,
accessor: IAccessor
) {
const mergeCellService = accessor.get(MergeCellService);
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
const { unitId, subUnitId, range } = pasteTo;
Expand Down Expand Up @@ -387,35 +389,30 @@ export function getClearAndSetMergeMutations(
// remove current range's all merged Cell
if (hasMerge) {
// get all merged cells
const currentService = accessor.get(IUniverInstanceService) as IUniverInstanceService;
const workbook = currentService.getUniverSheetInstance(unitId);
const worksheet = workbook?.getSheetBySheetId(subUnitId);
if (workbook && worksheet) {
const mergeData = worksheet.getMergeData();
const mergedCellsInRange = mergeData.filter((rect) =>
Rectangle.intersects({ startRow, startColumn, endRow, endColumn }, rect)
);

const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
unitId,
subUnitId,
ranges: mergedCellsInRange,
};
redoMutationsInfo.push({
id: RemoveWorksheetMergeMutation.id,
params: removeMergeMutationParams,
});
const mergeData = mergeCellService.getMergeData(unitId, subUnitId);
const mergedCellsInRange = mergeData.filter((rect) =>
Rectangle.intersects({ startRow, startColumn, endRow, endColumn }, rect)
);

const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
unitId,
subUnitId,
ranges: mergedCellsInRange,
};
redoMutationsInfo.push({
id: RemoveWorksheetMergeMutation.id,
params: removeMergeMutationParams,
});

const undoRemoveMergeMutationParams: IAddWorksheetMergeMutationParams = RemoveMergeUndoMutationFactory(
accessor,
removeMergeMutationParams
);
const undoRemoveMergeMutationParams: IAddWorksheetMergeMutationParams = RemoveMergeUndoMutationFactory(
accessor,
removeMergeMutationParams
);

undoMutationsInfo.push({
id: AddWorksheetMergeMutation.id,
params: undoRemoveMergeMutationParams,
});
}
undoMutationsInfo.push({
id: AddWorksheetMergeMutation.id,
params: undoRemoveMergeMutationParams,
});
}

// set merged cell info
Expand Down
4 changes: 3 additions & 1 deletion packages/sheets-ui/src/controllers/selection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { ISelectionWithCoordAndStyle, ISelectionWithStyle } from '@univerjs
import {
convertSelectionDataToRange,
getNormalSelectionStyle,
MergeCellService,
NORMAL_SELECTION_PLUGIN_NAME,
SelectionManagerService,
SelectionMoveType,
Expand All @@ -57,7 +58,8 @@ export class SelectionController extends Disposable {
@ISelectionRenderService
private readonly _selectionRenderService: ISelectionRenderService,
@Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
@Inject(ThemeService) private readonly _themeService: ThemeService
@Inject(ThemeService) private readonly _themeService: ThemeService,
@Inject(MergeCellService) private _mergeCellService: MergeCellService
) {
super();

Expand Down
Loading

0 comments on commit ce1d8b5

Please sign in to comment.