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

chore(refactor): init merge cell service #1018

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore(refactor): init merge cell service
  • Loading branch information
Gggpound committed Jan 22, 2024
commit 6a597520f06f10faf9108b31ce86bc90dbdf18d1
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[] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要把这个 API 去掉?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个属于 model ,业务使用应该统一归属到 service .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前渲染层那边没有接入 di,所以这个 api 没办法完全去掉.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个属于 model ,业务使用应该统一归属到 service .

为什么获取合并单元格算作所谓的“业务使用”,获取单元格内容不算?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得拿单元格内容也应该走 service 拦一层的...要弱化 worksheet 这个概念, 本身 worksheet 算 snapshot。
对外提供 api 又是走的 facade(基于 service 进行的 api 组装),

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 @@ -486,6 +486,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 @@ -563,7 +573,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 @@ -104,9 +104,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
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 @@ -54,17 +54,13 @@ 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,
getPrimaryForRange,
MergeCellService,
NORMAL_SELECTION_PLUGIN_NAME,
SelectionManagerService,
} from '@univerjs/sheets';
Expand Down Expand Up @@ -157,7 +153,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 @@ -1384,7 +1381,7 @@ export class PromptController extends Disposable {
return;
}

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

this._formulaInputService.enableLockedSelectionChange();

Expand All @@ -1396,7 +1393,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 @@ -602,15 +604,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 @@ -819,17 +821,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
49 changes: 23 additions & 26 deletions packages/sheets-ui/src/controllers/clipboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
getAddMergeMutationRangeByType,
MergeCellService,
MoveRangeCommand,
MoveRangeMutation,
NORMAL_SELECTION_PLUGIN_NAME,
Expand Down Expand Up @@ -432,6 +433,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 @@ -467,35 +469,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 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 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
Loading