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

fix(sheets-ui): auto-fill will copy auto-height strategy from origin region #3287

Merged
merged 3 commits into from
Sep 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import type { RenderManagerService } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';

export const AFFECT_LAYOUT_STYLES = ['ff', 'fs', 'tr', 'tb'];

@OnLifecycle(LifecycleStages.Ready, AutoHeightController)
export class AutoHeightController extends Disposable {
constructor(
Expand Down Expand Up @@ -135,7 +137,6 @@ export class AutoHeightController extends Disposable {

// TODO: @jocs, All styles that affect the size of the cell,
// I don't know if the enumeration is complete, to be added in the future.
const AFFECT_LAYOUT_STYLES = ['ff', 'fs', 'tr', 'tb'];

if (!AFFECT_LAYOUT_STYLES.includes(command.params?.style.type)) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type { IAutoFillLocation } from './services/auto-fill/type';
export type { IDiscreteRange } from './controllers/utils/range-tools';
export { virtualizeDiscreteRanges, rangeToDiscreteRange } from './controllers/utils/range-tools';
export { type IHoverCellPosition } from './services/hover-manager.service';
export { AutoHeightController } from './controllers/auto-height.controller';
export { AutoHeightController, AFFECT_LAYOUT_STYLES } from './controllers/auto-height.controller';
export { type IDragCellPosition } from './services/drag-manager.service';
export { SheetMenuPosition } from './controllers/menu/menu';
export { useHighlightRange } from './hooks/useHighlightRange';
Expand Down
96 changes: 79 additions & 17 deletions packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,32 @@
* limitations under the License.
*/

import type { IDisposable, IMutationInfo, Nullable, Workbook } from '@univerjs/core';
import {
createIdentifier,
Direction,
import type { IDisposable, IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';
import { createIdentifier, Direction,
Disposable,
ICommandService,
Inject,
Injector,
IUndoRedoService,
IUniverInstanceService,
LifecycleStages,
ObjectMatrix,
OnLifecycle,
RANGE_TYPE,
Rectangle,
toDisposable,
UniverInstanceType,
} from '@univerjs/core';
import { SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
import type { ISetRangeValuesMutationParams, ISetWorksheetRowAutoHeightMutationParams } from '@univerjs/sheets';
import { SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetRowAutoHeightMutation, SetWorksheetRowAutoHeightMutationFactory, SheetsSelectionsService } from '@univerjs/sheets';
import type { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs';

import type { RenderManagerService } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { discreteRangeToRange } from '../../controllers/utils/range-tools';
import { AFFECT_LAYOUT_STYLES } from '../../controllers/auto-height.controller';
import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';
import {
chnNumberRule,
chnWeek2Rule,
Expand Down Expand Up @@ -127,10 +132,12 @@ export class AutoFillService extends Disposable implements IAutoFillService {

readonly menu$ = this._menu$.asObservable();
constructor(
@ICommandService private _commandService: ICommandService,
@IUndoRedoService private _undoRedoService: IUndoRedoService,
@IRenderManagerService private readonly _renderManagerService: RenderManagerService,
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@Inject(SheetsSelectionsService) private _selectionManagerService: SheetsSelectionsService,
@ICommandService private _commandService: ICommandService,
@IUndoRedoService private _undoRedoService: IUndoRedoService
@Inject(Injector) private readonly _injector: Injector
) {
super();
this._init();
Expand All @@ -150,7 +157,7 @@ export class AutoFillService extends Disposable implements IAutoFillService {
this._isFillingStyle = true;
}

private getOneByPriority(items: ISheetAutoFillHook[]) {
private _getOneByPriority(items: ISheetAutoFillHook[]) {
if (items.length <= 0) {
return [];
}
Expand Down Expand Up @@ -208,10 +215,10 @@ export class AutoFillService extends Disposable implements IAutoFillService {
);
const onlyHooks = enabledHooks.filter((h) => h.type === AutoFillHookType.Only);
if (onlyHooks.length > 0) {
return this.getOneByPriority(onlyHooks);
return this._getOneByPriority(onlyHooks);
}

const defaultHooks = this.getOneByPriority(enabledHooks.filter((h) => h.type === AutoFillHookType.Default));
const defaultHooks = this._getOneByPriority(enabledHooks.filter((h) => h.type === AutoFillHookType.Default));

const appendHooks = enabledHooks.filter((h) => h.type === AutoFillHookType.Append) || [];

Expand Down Expand Up @@ -273,11 +280,9 @@ export class AutoFillService extends Disposable implements IAutoFillService {
}

fillData(triggerUnitId: string, triggerSubUnitId: string) {
const {
source,
target,
unitId = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId(),
subUnitId = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId(),
const { source, target,
unitId = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId(),
subUnitId = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId(),
} = this.autoFillLocation || {};
const direction = this.direction;
if (!source || !source.cols.length || !source.rows.length
Expand Down Expand Up @@ -317,21 +322,78 @@ export class AutoFillService extends Disposable implements IAutoFillService {
}
});
const result = redos.every((m) => this._commandService.syncExecuteCommand(m.id, m.params));

// deal with auto-height
const autoHeightRanges: IRange[] = [];
if (applyType !== APPLY_TYPE.NO_FORMAT) {
redos.forEach((m) => {
if (m.id === SetRangeValuesMutation.id) {
const { cellValue } = m.params as ISetRangeValuesMutationParams;
const matrix = new ObjectMatrix(cellValue);
matrix.forValue((row, col, value) => {
const style = Object.keys(value?.s || {});
if (style.length && AFFECT_LAYOUT_STYLES.some((s) => style.includes(s))) {
autoHeightRanges.push({ startRow: row, endRow: row, startColumn: col, endColumn: col });
}
});
}
});
}
const autoHeightUndoRedos = this._getAutoHeightUndoRedos(triggerUnitId, triggerSubUnitId, autoHeightRanges);
const autoHeightResult = autoHeightUndoRedos.redos.every((m) => this._commandService.syncExecuteCommand(m.id, m.params));
if (autoHeightResult) {
undos.push(...autoHeightUndoRedos.undos);
redos.push(...autoHeightUndoRedos.redos);
}
if (result) {
// add to undo redo services
this._undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: undos,
redoMutations: redos,
});
}
// this._commandService.executeCommand(AutoFillCommand.id);
activeHooks.forEach((hook) => {
hook.onAfterFillData?.({ source, target, unitId, subUnitId }, direction, applyType);
});
this.setShowMenu(true);
return true;
}

private _getAutoHeightUndoRedos(unitId: string, subUnitId: string, ranges: IRange[]) {
const sheetSkeletonService = this._renderManagerService.getRenderById(unitId)!.with<SheetSkeletonManagerService>(SheetSkeletonManagerService);
const skeleton = sheetSkeletonService.getCurrent()?.skeleton;
if (!skeleton) {
return {
redos: [],
undos: [],
};
}
const rowsAutoHeightInfo = skeleton.calculateAutoHeightInRange(ranges);

const redoParams: ISetWorksheetRowAutoHeightMutationParams = {
subUnitId,
unitId,
rowsAutoHeightInfo,
};
const undoParams: ISetWorksheetRowAutoHeightMutationParams = SetWorksheetRowAutoHeightMutationFactory(
this._injector,
redoParams
);
return {
undos: [
{
id: SetWorksheetRowAutoHeightMutation.id,
params: undoParams,
},
],
redos: [
{
id: SetWorksheetRowAutoHeightMutation.id,
params: redoParams,
},
],
};
}
}

export const IAutoFillService = createIdentifier<AutoFillService>('univer.auto-fill-service');
Loading