Skip to content

Commit

Permalink
Merge branch dev into refactor/cell-editor-1125
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 committed Dec 3, 2024
2 parents b700251 + 22facae commit a84f0f7
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class ConditionalFormattingViewModel extends Disposable {
*/
public markRuleDirty = (() => {
const rxItem = new Subject<{ unitId: string; subUnitId: string; cfId: string; isNeedResetPreComputingCache: boolean }>();
rxItem.pipe(bufferTime(100), filter((e) => !!e.length), map((list) => {
this.disposeWithMe(rxItem.pipe(bufferTime(100), filter((e) => !!e.length), map((list) => {
const set = new Set();
const result: typeof list = [];
list.forEach((item) => {
Expand All @@ -152,7 +152,7 @@ export class ConditionalFormattingViewModel extends Disposable {
list.forEach((item) => {
this._markRuleDirtyAtOnce(item.unitId, item.subUnitId, item.cfId, item.isNeedResetPreComputingCache);
});
});
}));
return (unitId: string, subUnitId: string, cfId: string, isNeedResetPreComputingCache: boolean = true) => {
rxItem.next({ unitId, subUnitId, cfId, isNeedResetPreComputingCache });
};
Expand Down
14 changes: 10 additions & 4 deletions packages/sheets-formula-ui/src/controllers/prompt.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
DISABLE_NORMAL_SELECTIONS,
getPrimaryForRange,
IRefSelectionsService,
SelectionMoveType,
setEndForRange,
SheetsSelectionsService,
} from '@univerjs/sheets';
Expand Down Expand Up @@ -906,7 +907,7 @@ export class PromptController extends Disposable {

const bodyList = this._getFormulaAndCellEditorBody(unitIds).filter((b) => !!b);

this._refSelectionsService.clear();
// this._refSelectionsService.clear();

if (sequenceNodes == null || sequenceNodes.length === 0) {
this._existsSequenceNode = false;
Expand Down Expand Up @@ -1071,9 +1072,8 @@ export class PromptController extends Disposable {
// if (lastRange) {
// selectionWithStyle.push(lastRange);
// }

if (selectionWithStyle.length) {
this._refSelectionsService.addSelections(unitId, currSheetId, selectionWithStyle);
this._refSelectionsService.setSelections(unitId, currSheetId, selectionWithStyle, SelectionMoveType.ONLY_SET);
}
}

Expand Down Expand Up @@ -1405,6 +1405,12 @@ export class PromptController extends Disposable {
return;
}

const { skeleton } = this._getCurrentUnitIdAndSheetId();
const unitId = skeleton?.worksheet.getUnitId();
const sheetId = skeleton?.worksheet.getSheetId();
currentSelection.range.sheetId = sheetId;
currentSelection.range.unitId = unitId;

const refString = this._generateRefString(currentSelection);
this._formulaPromptService.setSequenceNodes(insertNodes);
this._formulaPromptService.insertSequenceRef(this._previousInsertRefStringIndex, refString);
Expand Down Expand Up @@ -1830,7 +1836,7 @@ export class PromptController extends Disposable {
const selectionData = this._sheetsSelectionsService.getCurrentLastSelection();
if (selectionData != null) {
const selectionDataNew = Tools.deepClone(selectionData);
this._refSelectionsService.addSelections([selectionDataNew]);
this._refSelectionsService.setSelections([selectionDataNew]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
return control;
}

private _initSelectionChangeListener(): void {
// used for refresh selection when focus in formula editor.
// TODO @lumixraku The response of the formula selection to keyboard events is different from that of a regular selection; I believe they should be consistent.
// unlike normal selection, there is no need to listen selectionService.selectionMoveEnd$ for keyboard moving event. prompt@highlightFormula would refresh selection.
this.disposeWithMe(this._refSelectionsService.selectionSet$.subscribe((selectionsWithStyles) => {
this._reset();
const skeleton = this._skeleton;
if (!skeleton) return;
// The selections' style would be colorful here. PromptController would change the color of selections later.
this.resetSelectionsByModelData(selectionsWithStyles || []);
}));
}

/**
* Update selectionModel in this._workbookSelections by user action in spreadsheet area.
*/
Expand Down Expand Up @@ -205,22 +218,6 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
);
}

private _initSelectionChangeListener(): void {
// selectionMoveEnd$ beforeSelectionMoveEnd$ was triggered when pointerup after dragging to change selection area.
// Changing the selection area through the 8 control points of the ref selection will not trigger this subscriber.

// beforeSelectionMoveEnd$ & selectionMoveEnd$ would triggered when change skeleton(change sheet).
this.disposeWithMe(this._workbookSelections.selectionSet$.subscribe((selectionsWithStyles) => {
this._reset();
const skeleton = this._skeleton;
if (!skeleton) return;
// The selections' style would be colorful here. PromptController would change the color of selections later.
for (const selectionWithStyle of selectionsWithStyles) {
this._addSelectionControlByModelData(selectionWithStyle);
}
}));
}

private _initSkeletonChangeListener(): void {
// changing sheet is not the only way cause currentSkeleton$ emit, a lot of cmds will emit currentSkeleton$
// COMMAND_LISTENER_SKELETON_CHANGE ---> currentSkeleton$.next
Expand Down Expand Up @@ -266,7 +263,7 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
* @param viewport
* @param scrollTimerType
*/
// eslint-disable-next-line max-lines-per-function, complexity
// eslint-disable-next-line complexity
protected _onPointerDown(
evt: IPointerEvent | IMouseEvent,
_zIndex = 0,
Expand Down
9 changes: 5 additions & 4 deletions packages/sheets-ui/src/commands/commands/auto-fill.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { IAccessor, ICommand, IRange } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import type { ISetRangeValuesMutationParams, ISetSelectionsOperationParams } from '@univerjs/sheets';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, sequenceExecute } from '@univerjs/core';
import { generateNullCellValue, getSheetCommandTarget, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation, SheetInterceptorService } from '@univerjs/sheets';

Expand Down Expand Up @@ -66,7 +66,7 @@ export const AutoClearContentCommand: ICommand = {
clearMutationParams
);
const { startColumn, startRow } = selectionRange;
commandService.executeCommand(SetSelectionsOperation.id, {
const param: ISetSelectionsOperationParams = {
selections: [
{
primary: {
Expand All @@ -76,7 +76,7 @@ export const AutoClearContentCommand: ICommand = {
endRow: startRow,
actualRow: startRow,
actualColumn: startColumn,
isMerge: false,
isMerged: false,
isMergedMainCell: false,
},
range: {
Expand All @@ -86,7 +86,8 @@ export const AutoClearContentCommand: ICommand = {
],
unitId,
subUnitId,
});
};
commandService.executeCommand(SetSelectionsOperation.id, param);

const result = commandService.syncExecuteCommand(SetRangeValuesMutation.id, clearMutationParams);
if (result) {
Expand Down
21 changes: 11 additions & 10 deletions packages/sheets-ui/src/commands/commands/set-selection.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
*/

import type { Direction, ICommand, IRange } from '@univerjs/core';
import type {
ISetSelectionsOperationParams } from '@univerjs/sheets';
import { CommandType, ICommandService, IUniverInstanceService, RANGE_TYPE, Rectangle, Tools } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';

import { IRenderManagerService } from '@univerjs/engine-render';
import {
expandToContinuousRange,
getCellAtRowCol,
getSelectionsService,
getSheetCommandTarget,
SelectionMoveType,
SetSelectionsOperation,
} from '@univerjs/sheets';
import { KeyCode } from '@univerjs/ui';
Expand Down Expand Up @@ -64,7 +67,7 @@ export interface IMoveSelectionEnterAndTabCommandParams {
}

/**
* Move selection (Mainly by keyboard arrow keys, Tab and Enter key see MoveSelectionEnterAndTabCommand)
* Move selection (Mainly by keyboard arrow keys, For Tab and Enter key, check @MoveSelectionEnterAndTabCommand)
*/
export const MoveSelectionCommand: ICommand<IMoveSelectionCommandParams> = {
id: 'sheet.command.move-selection',
Expand Down Expand Up @@ -122,17 +125,12 @@ export const MoveSelectionCommand: ICommand<IMoveSelectionCommandParams> = {
},
},
];

const rs = accessor.get(ICommandService).syncExecuteCommand(SetSelectionsOperation.id, {
const rs = accessor.get(ICommandService).executeCommand(SetSelectionsOperation.id, {
unitId: workbook.getUnitId(),
subUnitId: worksheet.getSheetId(),
selections,
extra,
});

const renderManagerService = accessor.get(IRenderManagerService);
const selectionService = renderManagerService.getRenderById(unitId)?.with(ISheetSelectionRenderService);
selectionService?.refreshSelectionMoveEnd();
type: SelectionMoveType.MOVE_END,
} as ISetSelectionsOperationParams);
return rs;
},
};
Expand Down Expand Up @@ -274,6 +272,7 @@ export const MoveSelectionEnterAndTabCommand: ICommand<IMoveSelectionEnterAndTab
const rs = accessor.get(ICommandService).syncExecuteCommand(SetSelectionsOperation.id, {
unitId,
subUnitId: sheetId,
type: SelectionMoveType.MOVE_END,
selections: [resultRange],
extra,
});
Expand Down Expand Up @@ -323,6 +322,7 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
worksheet
)
: shrinkToNextCell(startRange, direction, worksheet);
destRange.rangeType = selection.range.rangeType;

if (Rectangle.equals(destRange, startRange)) {
return false;
Expand All @@ -331,6 +331,7 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
return accessor.get(ICommandService).syncExecuteCommand(SetSelectionsOperation.id, {
unitId,
subUnitId,
type: SelectionMoveType.ONLY_SET,
selections: [
{
range: destRange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class SheetsDefinedNameController extends Disposable {
this.disposeWithMe(merge(
this._selectionManagerService.selectionMoveStart$,
this._selectionManagerService.selectionMoving$,
this._selectionManagerService.selectionMoveEnd$
this._selectionManagerService.selectionMoveEnd$,
this._selectionManagerService.selectionSet$
)
.pipe(filter((params) => !!params))
.subscribe((params) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,3 @@ export function getViewPermissionDisable$(accessor: IAccessor) {
})
);
}

5 changes: 3 additions & 2 deletions packages/sheets-ui/src/controllers/utils/selections-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import type { IAccessor, Nullable, Workbook } from '@univerjs/core';
import { IUniverInstanceService, RANGE_TYPE, Rectangle, UniverInstanceType } from '@univerjs/core';
import type { ISelectionWithStyle } from '@univerjs/sheets';
import { IUniverInstanceService, RANGE_TYPE, Rectangle, UniverInstanceType } from '@univerjs/core';
import { MERGE_CELL_INTERCEPTOR_CHECK, MergeCellController, RangeProtectionRuleModel, SheetsSelectionsService } from '@univerjs/sheets';
import { combineLatest, map, of, switchMap } from 'rxjs';

Expand Down Expand Up @@ -45,7 +45,8 @@ export function getSheetSelectionsDisabled$(accessor: IAccessor) {
}

const subUnitRuleRange = rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId)
.map((rule) => rule.ranges).flat();
.map((rule) => rule.ranges)
.flat();

if (selection.length < 2) {
const range = selection[0].range;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,21 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
// #endregion

/**
* Mainly emit by pointerup (pointerup is handled in _onPointerdown)
* Mainly emit by pointerup in spreadsheet. (pointerup is handled in _onPointerdown)
*/
protected readonly _selectionMoveEnd$ = new BehaviorSubject<ISelectionWithCoord[]>([]);
/**
* Pointerup in spreadsheet
*/
readonly selectionMoveEnd$ = this._selectionMoveEnd$.asObservable();
/**
* Mainly emit by pointermove in spreadsheet
*/
protected readonly _selectionMoving$ = new Subject<ISelectionWithCoord[]>();
readonly selectionMoving$ = this._selectionMoving$.asObservable();

/**
* Mainly emit by pointerdown
* Pointermove in spreadsheet
*/
readonly selectionMoving$ = this._selectionMoving$.asObservable();
protected readonly _selectionMoveStart$ = new Subject<ISelectionWithCoord[]>();
readonly selectionMoveStart$ = this._selectionMoveStart$.asObservable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
const sheetObject = this._getSheetObject();

this._initEventListeners(sheetObject);
this._initSelectionChangeListener();
this._initSelectionModelChangeListener();
this._initThemeChangeListener();
this._initSkeletonChangeListener();
this._initUserActionSyncListener();
Expand Down Expand Up @@ -144,13 +144,21 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
return this._contextService.getContextValue(DISABLE_NORMAL_SELECTIONS);
}

private _initSelectionChangeListener(): void {
// normal selection: after dragging selection(move end)
this.disposeWithMe(merge(this._workbookSelections.selectionMoveEnd$, this._workbookSelections.selectionSet$).subscribe((selectionWithStyleList) => {
/**
* Response for selection model changing.
*/
private _initSelectionModelChangeListener(): void {
this.disposeWithMe(merge(
this._workbookSelections.selectionMoveEnd$, // triggered by keyboard(e.g. arrow key tab enter)
this._workbookSelections.selectionSet$ // shift + arrow key
).subscribe((selectionWithStyleList) => {
this.resetSelectionsByModelData(selectionWithStyleList);
}));
}

/**
* Handle events in spreadsheet. (e.g. drag and move to make a selection)
*/
private _initUserActionSyncListener(): void {
this.disposeWithMe(this.selectionMoveStart$.subscribe((params) => this._updateSelections(params, SelectionMoveType.MOVE_START)));
this.disposeWithMe(this.selectionMoving$.subscribe((params) => this._updateSelections(params, SelectionMoveType.MOVING)));
Expand All @@ -164,7 +172,9 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
this._reset();
} else {
this._renderDisposable = toDisposable(
this.selectionMoveEnd$.subscribe((params) => this._updateSelections(params, SelectionMoveType.MOVE_END))
this.selectionMoveEnd$.subscribe((params) => {
this._updateSelections(params, SelectionMoveType.MOVE_END);
})
);
}
}));
Expand All @@ -184,13 +194,15 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
return;
}

const selectionWithStyles = selectionDataWithStyleList.map((selectionDataWithStyle) =>
convertSelectionDataToRange(selectionDataWithStyle)
);

this._commandService.executeCommand(SetSelectionsOperation.id, {
unitId,
subUnitId: sheetId,
type,
selections: selectionDataWithStyleList.map((selectionDataWithStyle) =>
convertSelectionDataToRange(selectionDataWithStyle)
),
selections: selectionWithStyles,
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export function FormulaBar() {
return merge(
worksheetProtectionRuleModel.ruleChange$,
rangeProtectionRuleModel.ruleChange$,
selectionManager.selectionMoveEnd$
selectionManager.selectionMoveEnd$,
selectionManager.selectionSet$
).pipe(
switchMap(() => {
const unitId = workbook.getUnitId();
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets/src/basics/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export interface ISelectionWithStyle extends ISelection {
* if primary is not defined, means not keep current primary cell.
*/
primary: Nullable<ISelectionCell>;
style: Nullable<Partial<ISelectionStyle>>;
style?: Nullable<Partial<ISelectionStyle>>;
}

export interface ISheetRangeLocation {
Expand Down
Loading

0 comments on commit a84f0f7

Please sign in to comment.