Skip to content

Commit

Permalink
fix: scroll param rm xy split
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Dec 12, 2024
1 parent 8ea15d0 commit 7dc7942
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 61 deletions.
5 changes: 0 additions & 5 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ export class Worksheet {
this._rowManager = new RowManager(this._snapshot, this._viewModel, rowData);
this._columnManager = new ColumnManager(this._snapshot, columnData);
this._spanModel = new SpanModel(this._snapshot.mergeData);

if (!window.ws) {
window.ws = {};
}
window.ws[this._sheetId] = this;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/engine-render/src/components/sheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

export * from './column-header';
export * from './constants';
export * from './extensions';
export * from './interfaces';
export * from './row-header';
Expand Down
52 changes: 26 additions & 26 deletions packages/engine-render/src/components/sheets/sheet-skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import type {
Worksheet } from '@univerjs/core';
import type { IDocumentSkeletonColumn } from '../../basics/i-document-skeleton-cached';
import type { IBoundRectNoAngle, IPoint, IViewportInfo } from '../../basics/vector2';
import type { Scene } from '../../scene';
import {
addLinkToDocumentModel,
BooleanNumber,
Expand Down Expand Up @@ -273,7 +272,6 @@ export class SpreadsheetSkeleton extends Skeleton {
* @param _injector
*/
constructor(
readonly scene: Scene,
readonly worksheet: Worksheet,
private _styles: Styles,
@Inject(LocaleService) _localeService: LocaleService,
Expand All @@ -289,11 +287,6 @@ export class SpreadsheetSkeleton extends Skeleton {
this._updateLayout();
this._initContextListener();
this._isRowStylePrecedeColumnStyle = this._configService.getConfig(IS_ROW_STYLE_PRECEDE_COLUMN_STYLE) ?? false;

if (!window.sk) {
window.sk = {};
}
window.sk[this.worksheet.getSheetId()] = this;
}

get rowHeightAccumulation(): number[] {
Expand Down Expand Up @@ -453,35 +446,43 @@ export class SpreadsheetSkeleton extends Skeleton {

/**
* Get range in visible area (range in view bounds) and set into this._rowColumnSegment.
* @param bounds
* @param vpInfo
* @returns boolean
*/
updateVisibleRange(bounds?: IViewportInfo): boolean {
updateVisibleRange(vpInfo?: IViewportInfo): boolean {
if (!this._worksheetData || !this._rowHeightAccumulation || !this._columnWidthAccumulation) {
return false;
}

if (bounds) {
const range = this.getRangeByViewport(bounds);
if (vpInfo) {
const range = this.getRangeByViewport(vpInfo);
this._visibleRange = range;
this._visibleRangeMap.set(bounds.viewportKey as SHEET_VIEWPORT_KEY, range);
this._visibleRangeMap.set(vpInfo.viewportKey as SHEET_VIEWPORT_KEY, range);

const cacheRange = this.getCacheRangeByViewport(bounds);
this._cacheRangeMap.set(bounds.viewportKey as SHEET_VIEWPORT_KEY, cacheRange);
const cacheRange = this.getCacheRangeByViewport(vpInfo);
this._cacheRangeMap.set(vpInfo.viewportKey as SHEET_VIEWPORT_KEY, cacheRange);
}

return true;
}

getVisibleRangeByViewport(viewportKey: SHEET_VIEWPORT_KEY) {
return this._visibleRangeMap.get(viewportKey);
}

getVisibleRanges() {
return this._visibleRangeMap;
}

/**
* Set border background and font to this._stylesCache by visible range, which derives from bounds)
* @param bounds viewBounds
* @param vpInfo viewBounds
*/
setStylesCache(bounds?: IViewportInfo): Nullable<SpreadsheetSkeleton> {
setStylesCache(vpInfo?: IViewportInfo): Nullable<SpreadsheetSkeleton> {
if (!this._worksheetData) return;
if (!this._rowHeightAccumulation || !this._columnWidthAccumulation) return;

this.updateVisibleRange(bounds);
this.updateVisibleRange(vpInfo);

const rowColumnSegment = this._visibleRange;
const columnWidthAccumulation = this.columnWidthAccumulation;
Expand Down Expand Up @@ -528,7 +529,6 @@ export class SpreadsheetSkeleton extends Skeleton {
calculate(): Nullable<SpreadsheetSkeleton> {
this._resetCache();
this._updateLayout();
// this.setStylesCache();

return this;
}
Expand Down Expand Up @@ -901,6 +901,14 @@ export class SpreadsheetSkeleton extends Skeleton {
return Math.max(rowHeader.width, widthByComputation);
}

/**
* @deprecated use `getRangeByViewport` instead.
* @param bounds
*/
getRangeByBounding(bounds?: IViewportInfo): IRange {
return this._getRangeByViewBounding(this._rowHeightAccumulation, this._columnWidthAccumulation, bounds?.cacheBound);
}

getRangeByViewport(vpInfo?: IViewportInfo): IRange {
return this._getRangeByViewBounding(this._rowHeightAccumulation, this._columnWidthAccumulation, vpInfo?.viewBound);
}
Expand Down Expand Up @@ -2272,14 +2280,6 @@ export class SpreadsheetSkeleton extends Skeleton {
return this.worksheet.getCellInfoInMergeData(row, column);
}

scrollToRow(row: number, viewportKey: SHEET_VIEWPORT_KEY) {
const distanceY = this._offsetYToRow(row);
const viewport = this.scene.getViewport(viewportKey);
if (viewport) {
viewport.scrollToViewportPos({ viewportScrollX: 0, viewportScrollY: distanceY });
}
}

getDistanceFromTopLeft(row: number, col: number): IPoint {
return {
x: this._offsetXToCol(col),
Expand Down
12 changes: 6 additions & 6 deletions packages/sheets-ui/src/commands/commands/set-scroll.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export const SetScrollRelativeCommand: ICommand<ISetScrollRelativeCommandParams>
unitId,
sheetId: subUnitId,
// why + ySplit? receiver - ySplit in scroll.operation.ts
sheetViewStartRow: sheetViewStartRow + ySplit,
sheetViewStartColumn: sheetViewStartColumn + xSplit,
sheetViewStartRow,
sheetViewStartColumn,
offsetX: currentOffsetX + offsetX, // currentOffsetX + offsetX may be negative or over max
offsetY: currentOffsetY + offsetY,
});
Expand Down Expand Up @@ -113,15 +113,15 @@ export const ScrollCommand: ICommand<IScrollCommandParams> = {
offsetY: currentOffsetY,
} = currentScroll || {};

const { xSplit, ySplit } = worksheet.getConfig().freeze;
// const { xSplit, ySplit } = worksheet.getConfig().freeze;

const commandService = accessor.get(ICommandService);
return commandService.syncExecuteCommand(SetScrollOperation.id, {
unitId: workbook.getUnitId(),
sheetId: worksheet.getSheetId(),
// why + ySplit? receiver in scroll.operation.ts, - ySplit
sheetViewStartRow: sheetViewStartRow ?? (currentRow ?? 0) + ySplit,
sheetViewStartColumn: sheetViewStartColumn ?? (currentColumn ?? 0) + xSplit,
// why + ySplit? receiver in scroll.operation.ts - ySplit again.
sheetViewStartRow: sheetViewStartRow ?? (currentRow ?? 0),
sheetViewStartColumn: sheetViewStartColumn ?? (currentColumn ?? 0),
offsetX: offsetX ?? currentOffsetX,
offsetY: offsetY ?? currentOffsetY,
});
Expand Down
14 changes: 7 additions & 7 deletions packages/sheets-ui/src/commands/operations/scroll.operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { IOperation } from '@univerjs/core';
import type { IScrollStateWithSearchParam } from '../../services/scroll-manager.service';

import { CommandType, IUniverInstanceService } from '@univerjs/core';
import { CommandType } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetScrollManagerService } from '../../services/scroll-manager.service';

Expand All @@ -32,20 +32,20 @@ export const SetScrollOperation: IOperation<IScrollStateWithSearchParam> = {

// freeze is handled by set-scroll.command.ts
const { unitId, sheetId, offsetX, offsetY, sheetViewStartColumn, sheetViewStartRow } = params;
const currentService = accessor.get(IUniverInstanceService);
const renderManagerService = accessor.get(IRenderManagerService);
const workbook = currentService.getUniverSheetInstance(unitId);
const worksheet = workbook!.getSheetBySheetId(sheetId);
// const currentService = accessor.get(IUniverInstanceService);
// const workbook = currentService.getUniverSheetInstance(unitId);
// const worksheet = workbook!.getSheetBySheetId(sheetId);
const scrollManagerService = renderManagerService.getRenderById(unitId)!.with(SheetScrollManagerService);
const { xSplit, ySplit } = worksheet!.getConfig().freeze;
// const { xSplit, ySplit } = worksheet!.getConfig().freeze;

scrollManagerService.setScrollInfoAndEmitEvent({
unitId,
sheetId,
offsetX,
offsetY,
sheetViewStartRow: sheetViewStartRow - ySplit,
sheetViewStartColumn: sheetViewStartColumn - xSplit,
sheetViewStartRow,
sheetViewStartColumn,
});

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ describe('Test EndEditController', () => {
const injector = get(Injector);

const worksheet = workbook.getActiveSheet()!;
// const config = worksheet.getConfig();
const scene = null as any;
spreadsheetSkeleton = new SpreadsheetSkeleton(
scene,
worksheet,
workbook.getStyles(),
localeService,
Expand Down
6 changes: 3 additions & 3 deletions packages/sheets-ui/src/facade/f-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type { IEditorBridgeServiceVisibleParam } from '@univerjs/sheets-ui';
import type { IHoverRichTextInfo, IHoverRichTextPosition } from '../services/hover-manager.service';
import { awaitTime, ICommandService, type IDisposable, ILogService, toDisposable } from '@univerjs/core';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { FWorkbook } from '@univerjs/sheets/facade';
import { HoverManagerService, SetCellEditVisibleOperation } from '@univerjs/sheets-ui';
import { FWorkbook } from '@univerjs/sheets/facade';
import { type IDialogPartMethodOptions, IDialogService, type ISidebarMethodOptions, ISidebarService, KeyCode } from '@univerjs/ui';
import { filter } from 'rxjs';

Expand Down Expand Up @@ -75,7 +75,7 @@ export interface IFWorkbookSheetsUIMixin {
endEditing(save?: boolean): Promise<boolean>;
}

export class FWorokbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheetsUIMixin {
export class FWorkbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheetsUIMixin {
override openSiderbar(params: ISidebarMethodOptions): IDisposable {
this._logDeprecation('openSiderbar');

Expand Down Expand Up @@ -145,7 +145,7 @@ export class FWorokbookSheetsUIMixin extends FWorkbook implements IFWorkbookShee
}
}

FWorkbook.extend(FWorokbookSheetsUIMixin);
FWorkbook.extend(FWorkbookSheetsUIMixin);
declare module '@univerjs/sheets/facade' {
// eslint-disable-next-line ts/naming-convention
interface FWorkbook extends IFWorkbookSheetsUIMixin {}
Expand Down
72 changes: 71 additions & 1 deletion packages/sheets-ui/src/facade/f-worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
* limitations under the License.
*/

import type { IRange, Nullable } from '@univerjs/core';
import type { IScrollState } from '../services/scroll-manager.service';
import { ICommandService } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { IRenderManagerService, SHEET_VIEWPORT_KEY, sheetContentViewportKeys } from '@univerjs/engine-render';
import { SetFrozenCommand } from '@univerjs/sheets';

Check failure on line 21 in packages/sheets-ui/src/facade/f-worksheet.ts

View workflow job for this annotation

GitHub Actions / eslint

'SetFrozenCommand' is defined but never used
import { FWorksheet } from '@univerjs/sheets/facade';
import { ChangeZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
import { SetScrollOperation } from '../commands/operations/scroll.operation';
import { SheetScrollManagerService } from '../services/scroll-manager.service';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';

export interface IFWorksheetSkeletonMixin {
Expand Down Expand Up @@ -86,6 +91,71 @@ export class FWorksheetSkeletonMixin extends FWorksheet implements IFWorksheetSk
override getZoom(): number {
return this._worksheet.getZoomRatio();
}

/**
* Scroll spreadsheet to cell position.
* @param row
* @param column
* @returns void
*/
scrollToCell(row: number, column: number): void {
const unitId = this._workbook.getUnitId();
const sheetId = this._worksheet.getSheetId();
const commandService = this._injector.get(ICommandService);

return commandService.syncExecuteCommand(SetScrollOperation.id, {
unitId,
sheetId,
sheetViewStartRow: row,
sheetViewStartColumn: column,
offsetX: 0,
offsetY: 0,
});
}

/**
* Return visible range.
* @returns IRange
*/
getVisibleRange(): IRange {
const unitId = this._workbook.getUnitId();
const renderManagerService = this._injector.get(IRenderManagerService);
const render = renderManagerService.getRenderById(unitId);
let range: IRange = {
startColumn: 0,
startRow: 0,
endColumn: 0,
endRow: 0,
};
if (!render) return range;
const skm = render.with(SheetSkeletonManagerService);
const sk = skm.getCurrentSkeleton();
if (!sk) return range;
const visibleRangeMap = sk?.getVisibleRanges();
if (!visibleRangeMap) return range;

range = sk.getVisibleRangeByViewport(SHEET_VIEWPORT_KEY.VIEW_MAIN) as IRange;
for (const [k, r] of visibleRangeMap) {
if (sheetContentViewportKeys.indexOf(k) === -1) continue;
range.startColumn = Math.min(range.startColumn, r.startColumn);
range.startRow = Math.min(range.startRow, r.startRow);
range.endColumn = Math.max(range.endColumn, r.endColumn);
range.endRow = Math.max(range.endRow, r.endRow);
}

return range;
}

getScrollState(): Nullable<IScrollState> {
const unitId = this._workbook.getUnitId();
const sheetId = this._worksheet.getSheetId();
const renderManagerService = this._injector.get(IRenderManagerService);
const render = renderManagerService.getRenderById(unitId);
if (!render) return null;
const scm = render.with(SheetScrollManagerService);
return scm.getScrollStateByParam({ unitId, sheetId });
}

Check failure on line 157 in packages/sheets-ui/src/facade/f-worksheet.ts

View workflow job for this annotation

GitHub Actions / eslint

Block must not be padded by blank lines

}

FWorksheet.extend(FWorksheetSkeletonMixin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,8 @@ export class SheetSkeletonManagerService extends Disposable implements IRenderMo
}

private _buildSkeleton(worksheet: Worksheet) {
// const config = worksheet.getConfig();
const scene = this._context.scene;
const spreadsheetSkeleton = this._injector.createInstance(
SpreadsheetSkeleton,
scene,
worksheet,
this._context.unit.getStyles()
);
Expand Down
Loading

0 comments on commit 7dc7942

Please sign in to comment.