Skip to content

Commit

Permalink
chore: more comments2
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Jun 3, 2024
1 parent 2ccbaea commit 2b70466
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 194 deletions.
6 changes: 4 additions & 2 deletions packages/engine-render/src/basics/scroll-xy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* limitations under the License.
*/

import type { Viewport } from '../viewport';

export function getCurrentScrollXY(scrollTimer: any) {
const scene = scrollTimer.getScene();
const viewport = scrollTimer.getViewportByCoord(scene);
const viewport = scrollTimer.getViewportByCoord(scene) as Viewport;
const scrollX = 0;
const scrollY = 0;
if (!viewport) {
Expand All @@ -25,7 +27,7 @@ export function getCurrentScrollXY(scrollTimer: any) {
scrollY,
};
}
const actualScroll = viewport.getActualScroll(viewport.scrollX, viewport.scrollY);
const actualScroll = viewport.transScroll2ViewportScrollValue(viewport.scrollX, viewport.scrollY);
return {
scrollX: actualScroll.x,
scrollY: actualScroll.y,
Expand Down
141 changes: 64 additions & 77 deletions packages/engine-render/src/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export interface IScrollObserverParam {
/**
* scrollX for viewport
*/
actualScrollX?: number;
actualScrollY?: number;
viewportScrollX?: number;
viewportScrollY?: number;
limitX?: number;
limitY?: number;
isTrigger?: boolean;
Expand All @@ -94,19 +94,19 @@ export class Viewport {
*/
scrollX: number = 0;
scrollY: number = 0;
_preScrollX: number = 0;
_preScrollY: number = 0;
private _preScrollX: number = 0;
private _preScrollY: number = 0;

/**
* The actual scroll offset equals the distance from the content area position to the top, and there is a conversion relationship with scrollX and scrollY
* use getActualScroll to get scrolling value for spreadsheet.
* The viewport scroll offset equals the distance from the content area position to the top, and there is a conversion relationship with scrollX and scrollY
* use transScroll2ViewportScrollValue to get scrolling value for spreadsheet.
*/
viewportScrollX: number = 0;
viewportScrollY: number = 0;
preViewportScrollX: number = 0;
preViewportScrollY: number = 0;
_deltaViewportScrollX: number = 0;
_deltaViewportScrollY: number = 0;
private _viewportScrollX: number = 0;
private _viewportScrollY: number = 0;
private _preViewportScrollX: number = 0;
private _preViewportScrollY: number = 0;
private _deltaViewportScrollX: number = 0;
private _deltaViewportScrollY: number = 0;

onMouseWheelObserver = new Observable<IWheelEvent>();

Expand Down Expand Up @@ -252,9 +252,6 @@ export class Viewport {
this._mainCanvasResizeHandler();
});
this._mainCanvasResizeHandler();
if (this.viewportKey === 'viewMain') {
console.log('viewMain init');
}
}

initCacheCanvas(props?: IViewProps) {
Expand Down Expand Up @@ -377,6 +374,22 @@ export class Viewport {
return this._active;
}

set viewportScrollX(val: number) {
this._viewportScrollX = val;
}

get viewportScrollX() {
return this._viewportScrollX;
}

set viewportScrollY(val: number) {
this._viewportScrollY = val;
}

get viewportScrollY() {
return this._viewportScrollY;
}

private set top(num: number) {
this._topOrigin = num;
this._top = toPx(num, this._scene?.getParent()?.height);
Expand Down Expand Up @@ -488,30 +501,27 @@ export class Viewport {
}

/**
* scroll to scrollbar position, absolute,
* only viewMain would call scrollTo, other views did not call scroll
* see scroll.render-controller
* set scrollXY and viewportScrollXY, and update scrollInfo without notify listeners of scrollInfo$
* mainly call by scroll.render-controller and viewport.resize ...
* only viewMain would call scrollTo, other views did not call scroll, see scroll.render-controller
* @param pos
*
* when scrolling:
*
* scroll.render-controller@_scrollManagerService.scrollInfo$.subscribe --> scrollTo
*
* mainly call by scroll.render-controller and viewport.resize ...
* when change skelenton:
* _currentSkeletonBefore$ ---> scroll.render-controller@_updateSceneSize --> setSearchParam --> scene@_setTransForm ---> viewport.resetCanvasSizeAndUpdateScrollBar ---> scrollTo ---> _scroll
* --> onScrollAfterObserver.notifyObservers --> scroll.render-controller@onScrollAfterObserver ---> setScrollInfoToCurrSheetWithoutNotify ---> sms._setScrollInfo
*
* after change skelenton
* exec sequence
* _currentSkeleton$ ---> selection.render-controller ---> formula@_autoScroll ---> viewport.resize
* _currentSkeleton$ ---> selection.render-controller ---> formula@_autoScroll ---> viewport.resize ---> get scrollXY by viewportScrollXY ---> scrollTo
* _currentSkeleton$ ---> selection.render-controller ---> setCurrentSelection ---> formula@_autoScroll ---> scrollTo
* _currentSkeleton$ ---> freeze.render-controller@_refreshFreeze --> viewport.resize ---> scrollTo ---> _scroll (XXXX)
* _currentSkeleton$ ---> scroll.render-controller@updateSceneSize --> setSearchParam --> _setTransForm ---> viewport.resetCanvasSizeAndScrollbar ---> scrollTo ---> _scroll
* --> onScrollAfterObserver.notifyObservers --> scroll.render-controller@observer ---> setScrollInfoWithoutNotify
*
* render-controller@addOrReplaceNoRefresh ---> _setScrollInfo
* _currentSkeleton$ ---> scrollManagerService@setCurrentScroll --> scrollTo
* _currentSkeleton$ ---> scroll.render-controller@updateSceneSize --> viewport.resetCanvasSizeAndScrollbar ---> scrollTo
* _currentSkeleton$ ---> freeze.render-controller@_refreshFreeze --> viewport.resize ---> scrollTo ---> _scroll
* Debug
* window.scene.getViewports()[0].scrollTo({x: 14.2, y: 1.8}, true)
* @param pos
*/
scrollTo(pos: IScrollBarPosition, isTrigger = true) {
return this._scrollToScrollbarPos(SCROLL_TYPE.scrollTo, pos, isTrigger);
scrollTo(pos: IScrollBarPosition) {
return this._scrollToScrollbarPos(SCROLL_TYPE.scrollTo, pos);
}

/**
Expand All @@ -532,8 +542,8 @@ export class Viewport {
scrollY: this.scrollY,
x,
y,
actualScrollX: this.viewportScrollX,
actualScrollY: this.viewportScrollY,
viewportScrollX: this.viewportScrollX,
viewportScrollY: this.viewportScrollY,
limitX: this._scrollBar?.limitX,
limitY: this._scrollBar?.limitY,
isTrigger,
Expand Down Expand Up @@ -655,7 +665,7 @@ export class Viewport {
// this._deltaScrollY = this.scrollY - this._preScrollY;
this._preScrollX = this.scrollX;
this._preScrollY = this.scrollY;
const { scrollX, scrollY, actualScrollX, actualScrollY } = current;
const { scrollX, scrollY, viewportScrollX, viewportScrollY } = current;
if (scrollX !== undefined) {
this.scrollX = scrollX;
}
Expand All @@ -664,16 +674,16 @@ export class Viewport {
this.scrollY = scrollY;
}

if (actualScrollX !== undefined) {
this.preViewportScrollX = this.viewportScrollX;
this.viewportScrollX = actualScrollX;
this._deltaViewportScrollX = actualScrollX - this.preViewportScrollX;
if (viewportScrollX !== undefined) {
this._preViewportScrollX = this.viewportScrollX;
this.viewportScrollX = viewportScrollX;
this._deltaViewportScrollX = viewportScrollX - this._preViewportScrollX;
}

if (actualScrollY !== undefined) {
this.preViewportScrollY = this.viewportScrollY;
this.viewportScrollY = actualScrollY;
this._deltaViewportScrollY = actualScrollY - this.preViewportScrollY;
if (viewportScrollY !== undefined) {
this._preViewportScrollY = this.viewportScrollY;
this.viewportScrollY = viewportScrollY;
this._deltaViewportScrollY = viewportScrollY - this._preViewportScrollY;
}
return this;
}
Expand Down Expand Up @@ -1133,8 +1143,6 @@ export class Viewport {
* resize canvas & use viewportScrollXY to scrollTo
*/
private _resizeCacheCanvas() {
const viewportScrollX = this.viewportScrollX;
const viewportScrollY = this.viewportScrollY;
const { width, height } = this._getViewPortSize();

const scaleX = this.scene.scaleX;
Expand All @@ -1145,21 +1153,6 @@ export class Viewport {
this.cacheBound = this._viewBound;
this.preCacheBound = null;

// const contentWidth = (this._scene.width - this._paddingEndX) * this._scene.scaleX;
// const contentHeight = (this._scene.height - this._paddingEndY) * this._scene.scaleY;
// // @ts-expect-error
// if (this.viewportKey === 'sheetViewMain') {
// console.log('cotnentHeight', contentHeight, window.sms?._searchParamForScroll);
// }

// if (this._scrollBar) {
// this._scrollBar.resize(width, height, contentWidth, contentHeight);
// const { x, y } = this.transViewportScroll2ScrollValue(viewportScrollX, viewportScrollY);
// this.scrollTo({
// x,
// y,
// });
// }
this.markForceDirty(true);
}

Expand All @@ -1169,10 +1162,6 @@ export class Viewport {
const { width, height } = this._getViewPortSize();
const contentWidth = (this._scene.width - this._paddingEndX) * this._scene.scaleX;
const contentHeight = (this._scene.height - this._paddingEndY) * this._scene.scaleY;
// @ts-expect-error
if (this.viewportKey === 'sheetViewMain') {
console.log('cotnentHeight', contentHeight, window.sms?._searchParamForScroll);
}

if (this._scrollBar) {
this._scrollBar.resize(width, height, contentWidth, contentHeight);
Expand Down Expand Up @@ -1256,8 +1245,7 @@ export class Viewport {
y: number;
},
x?: number,
y?: number,
isTrigger = true
y?: number
) {
clearTimeout(this._scrollStopNum);
this._scrollStopNum = setTimeout(() => {
Expand All @@ -1267,11 +1255,10 @@ export class Viewport {
scrollY: this.scrollY,
x,
y,
actualScrollX: scroll.x,
actualScrollY: scroll.y,
viewportScrollX: scroll.x,
viewportScrollY: scroll.y,
limitX: this._scrollBar?.limitX,
limitY: this._scrollBar?.limitY,
isTrigger,
});
}, 2);
}
Expand All @@ -1286,7 +1273,7 @@ export class Viewport {
* @param scrollBarPos viewMain 滚动条的位置
* @param isTrigger
*/
private _scrollToScrollbarPos(scrollType: SCROLL_TYPE, scrollBarPos: IScrollBarPosition, isTrigger = true) {
private _scrollToScrollbarPos(scrollType: SCROLL_TYPE, scrollBarPos: IScrollBarPosition) {
const { x, y } = scrollBarPos;
if (this._scrollBar == null) {
return;
Expand Down Expand Up @@ -1317,15 +1304,14 @@ export class Viewport {
}

const limited = this.limitedScroll(); // 限制滚动范围
isTrigger && this.onScrollBeforeObserver.notifyObservers({
this.onScrollBeforeObserver.notifyObservers({
viewport: this,
scrollX: this.scrollX,
scrollY: this.scrollY,
x,
y,
limitX: this._scrollBar?.limitX,
limitY: this._scrollBar?.limitY,
isTrigger,
});

if (this._scrollBar) {
Expand All @@ -1337,19 +1323,21 @@ export class Viewport {
this.viewportScrollY = vpScroll.y;

// scroll.render-controller@onScrollAfterObserver ---> setScrollInfo but no notify
isTrigger && this.onScrollAfterObserver.notifyObservers({
// calc startRow & offset by viewportScrollXY, then update scrollInfo
// other viewports, rowHeader & colHeader depend on this notify
this.onScrollAfterObserver.notifyObservers({
viewport: this,
scrollX: this.scrollX,
scrollY: this.scrollY,
x,
y,
actualScrollX: vpScroll.x,
actualScrollY: vpScroll.y,
viewportScrollX: vpScroll.x,
viewportScrollY: vpScroll.y,
limitX: this._scrollBar?.limitX,
limitY: this._scrollBar?.limitY,
});

this._triggerScrollStop(vpScroll, x, y, isTrigger);
this._triggerScrollStop(vpScroll, x, y);

return limited;
}
Expand Down Expand Up @@ -1395,7 +1383,6 @@ export class Viewport {
: 0b00;

const shouldCacheUpdate = nearEdge | viewBoundOutCacheArea;
// console.log(`shouldCacheUpdate${shouldCacheUpdate}`, `${this.viewportKey}:`, this.preCacheBound, this.cacheBound, this.viewBound, this._preCacheVisibleBound);
return shouldCacheUpdate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const SetSelectionFrozenCommand: ICommand<ISetSelectionFrozenCommandParam
const currentSelection = selections[selections?.length - 1];
const { range } = currentSelection;
const scrollManagerService = accessor.get(ScrollManagerService);
const { sheetViewStartRow = 0, sheetViewStartColumn = 0 } = scrollManagerService.getCurrentScroll() || {};
const { sheetViewStartRow = 0, sheetViewStartColumn = 0 } = scrollManagerService.getCurrentScrollInfo() || {};
let startRow;
let startColumn;
let ySplit;
Expand Down
11 changes: 4 additions & 7 deletions packages/sheets-ui/src/commands/commands/set-scroll.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ISetScrollRelativeCommandParams {
export const SetScrollRelativeCommand: ICommand<ISetScrollRelativeCommandParams> = {
id: 'sheet.command.set-scroll-relative',
type: CommandType.COMMAND,
// offsetXY derived from mouse wheel event
handler: async (accessor, params = { offsetX: 0, offsetY: 0 }) => {
const commandService = accessor.get(ICommandService);
const scrollManagerService = accessor.get(ScrollManagerService);
Expand All @@ -45,26 +46,22 @@ export const SetScrollRelativeCommand: ICommand<ISetScrollRelativeCommandParams>

const { unitId, subUnitId, worksheet } = target;
const { xSplit, ySplit } = worksheet.getConfig().freeze;
const currentScroll = scrollManagerService.getCurrentScroll();
const currentScroll = scrollManagerService.getCurrentScrollInfo();
const { offsetX = 0, offsetY = 0 } = params || {};
const {
sheetViewStartRow = 0,
sheetViewStartColumn = 0,
offsetX: currentOffsetX = 0,
offsetY: currentOffsetY = 0,
scrollLeft,
scrollTop,
} = currentScroll || {};

return commandService.executeCommand(SetScrollOperation.id, {
unitId,
sheetId: subUnitId,
sheetViewStartRow: sheetViewStartRow + ySplit,
sheetViewStartColumn: sheetViewStartColumn + xSplit,
offsetX: currentOffsetX + offsetX,
offsetX: currentOffsetX + offsetX, // currentOffsetX, offsetX 0, -179, offsetX may be negative or over max
offsetY: currentOffsetY + offsetY,
scrollLeft,
scrollTop,
});
},
};
Expand Down Expand Up @@ -95,7 +92,7 @@ export const ScrollCommand: ICommand<IScrollCommandParams> = {
if (!target) return false;

const { workbook, worksheet } = target;
const currentScroll = scrollManagerService.getCurrentScroll();
const currentScroll = scrollManagerService.getCurrentScrollInfo();

if (!worksheet) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ export const SetScrollOperation: IOperation<IScrollManagerInsertParam> = {
const worksheet = workbook!.getSheetBySheetId(params!.sheetId);
const { xSplit, ySplit } = worksheet!.getConfig().freeze;

if (!worksheet) return false;
const config = worksheet.getConfig();
config.scrollLeft = params.scrollLeft || 0;
config.scrollTop = params.scrollTop || 0;

scrollManagerService.setScrollInfo({
...params,
sheetViewStartRow: params.sheetViewStartRow - ySplit,
Expand Down
Loading

0 comments on commit 2b70466

Please sign in to comment.