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(sheet): render viewport wrong size #1727

Draft
wants to merge 13 commits into
base: dev
Choose a base branch
from
Prev Previous commit
Next Next commit
test(facade): fix canvas mock
fix: new viewport need more param222

test: mock canvas

fix: rm useless
  • Loading branch information
jikkai authored and lumixraku committed May 24, 2024
commit 4c2d87e3d22e6b93685c8c408c358b82fa585620
906,620 changes: 906,620 additions & 0 deletions examples/src/data/sheets/demo/testdata.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/engine-render/src/basics/vector2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ export interface IViewportInfo {
* 例如冻结从第四列开始, left 就是 4 * column + rowHeaderWidth
*/
viewPortPosition: IBoundRectNoAngle;
viewPortKey: string;
viewportKey: string;
/**
* 后续会通过 number 来表示究竟是什么原因导致的标脏 这里采用二进制数值方便运算
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Background extends SheetExtension {
parentScale: IScale,
spreadsheetSkeleton: SpreadsheetSkeleton,
diffRanges: IRange[],
{ viewRanges, checkOutOfViewBound }: { viewRanges: IRange[]; checkOutOfViewBound: boolean; viewPortKey: string }
{ viewRanges, checkOutOfViewBound }: { viewRanges: IRange[]; checkOutOfViewBound: boolean; viewportKey: string }
) {
const { stylesCache } = spreadsheetSkeleton;
const { background, backgroundPositions } = stylesCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Font extends SheetExtension {
parentScale: IScale,
spreadsheetSkeleton: SpreadsheetSkeleton,
diffRanges: IRange[],
moreBoundsInfo: { viewRanges: IRange[]; checkOutOfViewBound?: boolean; viewPortKey: string }
moreBoundsInfo: { viewRanges: IRange[]; checkOutOfViewBound?: boolean; viewportKey: string }
) {
const { viewRanges = [], checkOutOfViewBound } = moreBoundsInfo;
const { stylesCache, dataMergeCache, overflowCache, worksheet } = spreadsheetSkeleton;
Expand Down
17 changes: 15 additions & 2 deletions packages/engine-render/src/components/sheets/spreadsheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,24 @@ export class Spreadsheet extends SheetComponent {
const { rowHeaderWidth, columnHeaderHeight } = spreadsheetSkeleton;
mainCtx.translateWithPrecision(rowHeaderWidth, columnHeaderHeight);

const { viewPortKey } = viewportInfo;
const { viewportKey: viewPortKey } = viewportInfo;
// scene --> layer, getObjects --> viewport.render(object) --> spreadsheet
// zIndex 0 spreadsheet this.getObjectsByOrder() ---> [spreadsheet]
// zIndex 2 rowHeader & colHeader & freezeBorder this.getObjectsByOrder() ---> [SpreadsheetRowHeader, SpreadsheetColumnHeader, _Rect]
// zIndex 3 selection this.getObjectsByOrder() ---> [group]

// SpreadsheetRowHeader SpreadsheetColumnHeader 并不在 spreadsheet 中处理
if (['viewMain', 'viewMainLeftTop', 'viewMainTop', 'viewMainLeft'].includes(viewPortKey)) {
if (this.sheetContentViewport().includes(viewPortKey)) {
if (viewportInfo && viewportInfo.cacheCanvas) {
this.renderByViewport(mainCtx, viewportInfo, spreadsheetSkeleton);
} else {
this._draw(mainCtx, viewportInfo);
}
} else if (this.sheetHeaderViewport().includes(viewPortKey)) {
// doing nothing, other components(SpreadsheetRowHeader...) will render
} else {
// embed in doc & slide
this._draw(mainCtx, viewportInfo);
}

mainCtx.restore();
Expand Down Expand Up @@ -683,6 +688,14 @@ export class Spreadsheet extends SheetComponent {
}
}

sheetContentViewport() {
return ['viewMain', 'viewMainLeftTop', 'viewMainTop', 'viewMainLeft'];
}

sheetHeaderViewport() {
return ['viewRowTop', 'viewRowBottom', 'viewColumnLeft', 'viewColumnRight', 'viewLeftTop'];
}

private _clearBackground(ctx: UniverRenderingContext, backgroundPositions?: ObjectMatrix<ISelectionCellWithCoord>) {
backgroundPositions?.forValue((row, column, cellInfo) => {
let { startY, endY, startX, endX } = cellInfo;
Expand Down
14 changes: 8 additions & 6 deletions packages/engine-render/src/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ export class Viewport {
initCacheCanvas(props?: IViewProps) {
if (props?.allowCache) {
this._cacheCanvas = new UniverCanvas();
this.bufferEdgeX = props?.bufferEdgeX || 0;
this.bufferEdgeY = props?.bufferEdgeY || 0;
}
this._allowCache = props?.allowCache || true;
this.bufferEdgeX = props?.bufferEdgeX || 0;
this.bufferEdgeY = props?.bufferEdgeY || 0;
this._allowCache = props?.allowCache || false;
}

/**
Expand Down Expand Up @@ -736,7 +736,7 @@ export class Viewport {
bottom: 0,
right: 0,
},
viewPortKey: this.viewportKey,
viewportKey: this.viewportKey,
// isDirty: this.isDirty,
isForceDirty: this.isForceDirty,
allowCache: false,
Expand Down Expand Up @@ -839,7 +839,7 @@ export class Viewport {
diffX,
diffY,
viewPortPosition,
viewPortKey: this.viewportKey,
viewportKey: this.viewportKey,
isDirty: this.isDirty ? 0b10 : 0b00,
isForceDirty: this.isForceDirty,
allowCache: this._allowCache,
Expand Down Expand Up @@ -1464,7 +1464,9 @@ export class Viewport {
*/
private _resizeHandler() {
if (!this._cacheCanvas) return;
const mainCanvas = this.scene.getEngine().getCanvas();
const engine = this.scene.getEngine();
if (!engine) return;
const mainCanvas = engine.getCanvas();
const width = mainCanvas.getWidth();
const height = mainCanvas.getHeight();
this._mainCanvasW = width;
Expand Down
19 changes: 19 additions & 0 deletions packages/facade/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { vi } from 'vitest';

vi.stubGlobal('jest', vi);
4 changes: 2 additions & 2 deletions packages/facade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@
"@univerjs/sheets-ui": "workspace:*",
"@univerjs/ui": "workspace:*",
"@wendellhu/redi": "0.15.2",
"jest-canvas-mock": "^2.5.2",
"jsdom": "^24.0.0",
"rxjs": "^7.8.1",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"vitest": "^1.6.0",
"vitest-canvas-mock": "^0.3.3"
"vitest": "^1.6.0"
}
}
6 changes: 1 addition & 5 deletions packages/facade/src/apis/__tests__/facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { ICellData, IStyleData, Nullable } from '@univerjs/core';
import { ICommandService, IUniverInstanceService } from '@univerjs/core';
import { SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type { RenderComponentType, SheetComponent } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
Expand Down Expand Up @@ -48,10 +48,6 @@ describe('Test FUniver', () => {
) => Nullable<IStyleData>;
let getSheetRenderComponent: (unitId: string, viewKey: SHEET_VIEW_KEY) => Nullable<RenderComponentType>;

beforeEach(() => {
vi.stubGlobal('jest', vi);
});

beforeEach(() => {
const testBed = createFacadeTestBed();
get = testBed.get;
Expand Down
3 changes: 1 addition & 2 deletions packages/facade/src/apis/sheets/__tests__/f-range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Test FRange', () => {
it('Range setValue', () => {
const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();

// A1 sets the number
// // A1 sets the number
const range1 = activeSheet?.getRange(0, 0, 1, 1);
range1?.setValue(1);

Expand Down Expand Up @@ -190,7 +190,6 @@ describe('Test FRange', () => {
activeSheet?.getRange(0, 0)?.setBackgroundColor('red');
const range = activeSheet?.getRange(0, 0);
expect(range?.getCellStyleData()?.bg?.rgb).toBe('red');

activeSheet?.getRange(0, 0, 2, 2)?.setFontWeight('bold');
expect(range?.getCellStyleData()?.bl).toBe(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { ICellData, IStyleData, Nullable, UnitModel, Workbook } from '@univ
import { ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it } from 'vitest';
import { Subject } from 'rxjs';

import type { IDragCellPosition, IHoverCellPosition } from '@univerjs/sheets-ui';
Expand Down Expand Up @@ -77,10 +77,6 @@ describe('Test FSheetHooks', () => {
let sheetHooks: FSheetHooks;
let workbook: Workbook;

beforeEach(() => {
vi.stubGlobal('jest', vi);
});

beforeEach(() => {
// Initialize the subject
hoverCurrentCell$ = new Subject<Nullable<IHoverCellPosition>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { ICellData, IStyleData, Nullable } from '@univerjs/core';
import { ICommandService, IUniverInstanceService } from '@univerjs/core';
import { InsertSheetCommand, InsertSheetMutation, RemoveSheetCommand, RemoveSheetMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetActiveOperation } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it } from 'vitest';

import type { FUniver } from '../../facade';
import { createFacadeTestBed } from '../../__tests__/create-test-bed';
Expand All @@ -40,10 +40,6 @@ describe('Test FWorkbook', () => {
endColumn: number
) => Nullable<IStyleData>;

beforeEach(() => {
vi.stubGlobal('jest', vi);
});

beforeEach(() => {
const testBed = createFacadeTestBed();
get = testBed.get;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { ICellData, IStyleData, Nullable } from '@univerjs/core';
import { ICommandService, IUniverInstanceService } from '@univerjs/core';
import { SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it } from 'vitest';

import type { FUniver } from '../../facade';
import { createFacadeTestBed } from '../../__tests__/create-test-bed';
Expand All @@ -40,10 +40,6 @@ describe('Test FWorksheet', () => {
endColumn: number
) => Nullable<IStyleData>;

beforeEach(() => {
vi.stubGlobal('jest', vi);
});

beforeEach(() => {
const testBed = createFacadeTestBed();
get = testBed.get;
Expand Down
22 changes: 4 additions & 18 deletions packages/facade/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,11 @@ import pkg from './package.json';

export default ({ mode }) => createViteConfig({
test: {
setupFiles: ['./vitest.setup.ts'],
setupFiles: [
'./jest.setup.ts',
'./vitest.setup.ts',
],
environment: 'jsdom',
deps: {
optimizer: {
web: {
include: ['vitest-canvas-mock'],
},
},
},
poolOptions: {
threads: {
singleThread: true,
},
},
environmentOptions: {
jsdom: {
resources: 'usable',
},
},
},
}, {
mode,
Expand Down
20 changes: 19 additions & 1 deletion packages/facade/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,23 @@
* limitations under the License.
*/

import 'vitest-canvas-mock';
import getCanvasWindow from 'jest-canvas-mock/lib/window';
import { vi } from 'vitest';

const apis = [
'Path2D',
'CanvasGradient',
'CanvasPattern',
'CanvasRenderingContext2D',
'DOMMatrix',
'ImageData',
'TextMetrics',
'ImageBitmap',
'createImageBitmap',
] as const;

const canvasWindow = getCanvasWindow({ document: window.document });

apis.forEach((api) => {
vi.stubGlobal(api, canvasWindow[api]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class SpreadsheetAdaptor extends ObjectAdaptor {
bottom: 0,
top: columnHeaderHeightScale,
width: rowHeaderWidthScale,
isRelativeY: true,
isWheelPreventDefaultX: true,
});

Expand Down