Skip to content

Commit

Permalink
chore: better code
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Oct 8, 2024
1 parent 3d9af57 commit ce068bb
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 369 deletions.
4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default antfu({
html: true,
},
rules: baseRules,
ignores: [
'examples/**/*.json',
// ...globs
],
}, {
files: ['**/*.ts', '**/*.tsx'],
ignores: [
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-render/src/basics/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export enum DOCUMENT_CONTEXT_CLIP_TYPE {

export const COLOR_BLACK_RGB = 'rgb(0,0,0)';

export enum BORDER_TYPE {
export enum BORDER_LTRB {
TOP = 't',
BOTTOM = 'b',
LEFT = 'l',
Expand Down
26 changes: 13 additions & 13 deletions packages/engine-render/src/basics/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/

import type { IPosition } from '@univerjs/core';
import { BorderStyleTypes } from '@univerjs/core';

import type { UniverRenderingContext } from '../context';
import { BORDER_TYPE, ORIENTATION_TYPE } from './const';

import type { IDocumentSkeletonLine } from './i-document-skeleton-cached';
import { BorderStyleTypes } from '@univerjs/core';
import { BORDER_LTRB, ORIENTATION_TYPE } from './const';
import { createCanvasElement } from './tools';
import { Vector2 } from './vector2';

Expand Down Expand Up @@ -61,43 +61,43 @@ export function getDevicePixelRatio(): number {
* @param lineWidthBuffer Solving the problem of mitered corners in the drawing of borders thicker than 2 pixels, caused by the line segments being centered.
* @param position border draw position
*/
export function drawLineByBorderType(ctx: UniverRenderingContext, type: BORDER_TYPE, lineWidthBuffer: number, position: IPosition) {
export function drawLineByBorderType(ctx: UniverRenderingContext, type: BORDER_LTRB, lineWidthBuffer: number, position: IPosition) {
let drawStartX = 0;
let drawStartY = 0;
let drawEndX = 0;
let drawEndY = 0;
const { startX, startY, endX, endY } = position;
if (type === BORDER_TYPE.TOP) {
if (type === BORDER_LTRB.TOP) {
drawStartX = startX - lineWidthBuffer;
drawStartY = startY;
drawEndX = endX + lineWidthBuffer;
drawEndY = startY;
} else if (type === BORDER_TYPE.BOTTOM) {
} else if (type === BORDER_LTRB.BOTTOM) {
drawStartX = startX - lineWidthBuffer;
drawStartY = endY;
drawEndX = endX - lineWidthBuffer;
drawEndY = endY;
} else if (type === BORDER_TYPE.LEFT) {
} else if (type === BORDER_LTRB.LEFT) {
drawStartX = startX;
drawStartY = startY - lineWidthBuffer;
drawEndX = startX;
drawEndY = endY + lineWidthBuffer;
} else if (type === BORDER_TYPE.RIGHT) {
} else if (type === BORDER_LTRB.RIGHT) {
drawStartX = endX;
drawStartY = startY - lineWidthBuffer;
drawEndX = endX;
drawEndY = endY + lineWidthBuffer;
}

// ctx.clearRect(drawStartX - 1, drawStartY - 1, drawEndX - drawStartX + 2, drawEndY - drawStartY + 2);
ctx.beginPath();
// ctx.beginPath();
ctx.moveToByPrecision(drawStartX, drawStartY);
ctx.lineToByPrecision(drawEndX, drawEndY);
ctx.closePathByEnv();
// ctx.closePathByEnv();
ctx.stroke();
}

export function drawDiagonalLineByBorderType(ctx: UniverRenderingContext, type: BORDER_TYPE, position: IPosition) {
export function drawDiagonalLineByBorderType(ctx: UniverRenderingContext, type: BORDER_LTRB, position: IPosition) {
let drawStartX = 0;
let drawStartY = 0;
let drawEndX = 0;
Expand Down Expand Up @@ -150,7 +150,7 @@ export function drawDiagonalLineByBorderType(ctx: UniverRenderingContext, type:
ctx.stroke();
}

export function clearLineByBorderType(ctx: UniverRenderingContext, type: BORDER_TYPE, position: IPosition) {
export function clearLineByBorderType(ctx: UniverRenderingContext, type: BORDER_LTRB, position: IPosition) {
let drawStartX = 0;
let drawStartY = 0;
let drawEndX = 0;
Expand Down Expand Up @@ -264,7 +264,7 @@ export function getRotateOrientation(angle: number) {
}

// rotate calculate logic https://www.processon.com/view/link/630df928f346fb0714c9c4ec
// eslint-disable-next-line max-lines-per-function
// eslint-disable-next-line max-lines-per-function, complexity
export function getRotateOffsetAndFarthestHypotenuse(
lines: IDocumentSkeletonLine[],
rectWidth: number,
Expand Down
30 changes: 15 additions & 15 deletions packages/engine-render/src/components/docs/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
* limitations under the License.
*/

import { CellValueType, HorizontalAlign, VerticalAlign, WrapStrategy } from '@univerjs/core';
import type { IDocumentRenderConfig, IScale, Nullable } from '@univerjs/core';

import type { IDocumentSkeletonGlyph, IDocumentSkeletonLine, IDocumentSkeletonPage, IDocumentSkeletonTable } from '../../basics/i-document-skeleton-cached';
import type { Transform } from '../../basics/transform';
import type { IBoundRectNoAngle, IViewportInfo } from '../../basics/vector2';
import type { UniverRenderingContext } from '../../context';
import type { Scene } from '../../scene';
import type { ComponentExtension, IExtensionConfig } from '../extension';
import type { IDocumentsConfig, IPageMarginLayout } from './doc-component';
import type { DocumentSkeleton } from './layout/doc-skeleton';
import { CellValueType, HorizontalAlign, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { Subject } from 'rxjs';
import type { IDocumentRenderConfig, IScale, Nullable } from '@univerjs/core';
import { BORDER_TYPE, drawLineByBorderType } from '../../basics';
import { BORDER_LTRB, drawLineByBorderType } from '../../basics';
import { calculateRectRotate, getRotateOffsetAndFarthestHypotenuse } from '../../basics/draw';
import { LineType } from '../../basics/i-document-skeleton-cached';
import { VERTICAL_ROTATE_ANGLE } from '../../basics/text-rotation';
Expand All @@ -28,14 +36,6 @@ import { DocumentsSpanAndLineExtensionRegistry } from '../extension';
import { DocComponent } from './doc-component';
import { DOCS_EXTENSION_TYPE } from './doc-extension';
import { Liquid } from './liquid';
import type { IDocumentSkeletonGlyph, IDocumentSkeletonLine, IDocumentSkeletonPage, IDocumentSkeletonTable } from '../../basics/i-document-skeleton-cached';
import type { Transform } from '../../basics/transform';
import type { IBoundRectNoAngle, IViewportInfo } from '../../basics/vector2';
import type { UniverRenderingContext } from '../../context';
import type { Scene } from '../../scene';
import type { ComponentExtension, IExtensionConfig } from '../extension';
import type { IDocumentsConfig, IPageMarginLayout } from './doc-component';
import type { DocumentSkeleton } from './layout/doc-skeleton';
import './extensions';

export interface IPageRenderConfig {
Expand Down Expand Up @@ -723,28 +723,28 @@ export class Documents extends DocComponent {
x += marginLeft;
y += marginTop;

drawLineByBorderType(ctx, BORDER_TYPE.LEFT, 0, {
drawLineByBorderType(ctx, BORDER_LTRB.LEFT, 0, {
startX: x,
startY: y,
endX: x + pageWidth,
endY: y + pageHeight,
});

drawLineByBorderType(ctx, BORDER_TYPE.TOP, 0, {
drawLineByBorderType(ctx, BORDER_LTRB.TOP, 0, {
startX: x,
startY: y,
endX: x + pageWidth,
endY: y + pageHeight,
});

drawLineByBorderType(ctx, BORDER_TYPE.RIGHT, 0, {
drawLineByBorderType(ctx, BORDER_LTRB.RIGHT, 0, {
startX: x,
startY: y,
endX: x + pageWidth,
endY: y + pageHeight,
});

drawLineByBorderType(ctx, BORDER_TYPE.BOTTOM, 0, {
drawLineByBorderType(ctx, BORDER_LTRB.BOTTOM, 0, {
startX: x,
startY: y,
endX: x + pageWidth,
Expand Down
20 changes: 10 additions & 10 deletions packages/engine-render/src/components/docs/extensions/border.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/

import type { BorderStyleTypes, IBorderData, IBorderStyleData, IScale, Nullable } from '@univerjs/core';
import { getColorStyle } from '@univerjs/core';
import type { IDocumentSkeletonGlyph } from '../../../basics/i-document-skeleton-cached';

import { BORDER_TYPE, COLOR_BLACK_RGB, FIX_ONE_PIXEL_BLUR_OFFSET } from '../../../basics/const';
import type { UniverRenderingContext } from '../../../context';
import { getColorStyle } from '@univerjs/core';
import { BORDER_LTRB, COLOR_BLACK_RGB, FIX_ONE_PIXEL_BLUR_OFFSET } from '../../../basics/const';
import { drawLineByBorderType, getLineWidth, setLineType } from '../../../basics/draw';
import type { IDocumentSkeletonGlyph } from '../../../basics/i-document-skeleton-cached';
import { Vector2 } from '../../../basics/vector2';
import type { UniverRenderingContext } from '../../../context';
import { DocumentsSpanAndLineExtensionRegistry } from '../../extension';
import { docExtension } from '../doc-extension';

Expand Down Expand Up @@ -85,7 +85,7 @@ export class Border extends docExtension {
this._preBorderColor = color;
}

drawLineByBorderType(ctx, type as BORDER_TYPE, (lineWidth - 1) / 2 / precisionScale, {
drawLineByBorderType(ctx, type as BORDER_LTRB, (lineWidth - 1) / 2 / precisionScale, {
startX: spanStartPoint.x,
startY: spanStartPoint.y,
endX: spanStartPoint.x + spanWidth,
Expand All @@ -103,11 +103,11 @@ export class Border extends docExtension {

private _createBorderCache(borderData: IBorderData) {
const { t, b, l, r } = borderData;
const borderCache = new Map<BORDER_TYPE, Nullable<IBorderStyleData>>();
t && borderCache.set(BORDER_TYPE.TOP, t);
b && borderCache.set(BORDER_TYPE.BOTTOM, b);
l && borderCache.set(BORDER_TYPE.LEFT, l);
r && borderCache.set(BORDER_TYPE.RIGHT, r);
const borderCache = new Map<BORDER_LTRB, Nullable<IBorderStyleData>>();
t && borderCache.set(BORDER_LTRB.TOP, t);
b && borderCache.set(BORDER_LTRB.BOTTOM, b);
l && borderCache.set(BORDER_LTRB.LEFT, l);
r && borderCache.set(BORDER_LTRB.RIGHT, r);
return borderCache;
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/engine-render/src/components/sheets/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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://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.
*/

export const BORDER_Z_INDEX = 50;
export const FONT_EXTENSION_Z_INDEX = 45;
export const BG_Z_INDEX = 21;
export const PRINTING_BG_Z_INDEX = 21;

export const EXPAND_SIZE_FOR_RENDER_OVERFLOW = 20;
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,19 @@
* limitations under the License.
*/

import { Range } from '@univerjs/core';
import type { IRange, IScale, ISelectionCellWithMergeInfo, ObjectMatrix } from '@univerjs/core';
import { fixLineWidthByScale, getColor, inViewRanges } from '../../../basics/tools';
import { SpreadsheetExtensionRegistry } from '../../extension';
import { SheetExtension } from './sheet-extension';
import type { UniverRenderingContext } from '../../../context';
import type { IDrawInfo } from '../../extension';
import type { SpreadsheetSkeleton } from '../sheet-skeleton';
import type { Spreadsheet } from '../spreadsheet';
import { Range } from '@univerjs/core';
import { fixLineWidthByScale, getColor, inViewRanges } from '../../../basics/tools';
import { SpreadsheetExtensionRegistry } from '../../extension';
import { BG_Z_INDEX, PRINTING_BG_Z_INDEX } from '../constants';
import { SheetExtension } from './sheet-extension';

const UNIQUE_KEY = 'DefaultBackgroundExtension';

/**
* in prev version background ext is higher than font ext. now turing back lower than font ext.
* font ext z-index is 30.
*/
const DOC_EXTENSION_Z_INDEX = 21;
const PRINTING_Z_INDEX = 21;

interface IRenderBGContext {
ctx: UniverRenderingContext;
spreadsheetSkeleton: SpreadsheetSkeleton;
Expand All @@ -48,12 +42,8 @@ interface IRenderBGContext {
export class Background extends SheetExtension {
override uKey = UNIQUE_KEY;

override Z_INDEX = DOC_EXTENSION_Z_INDEX;

PRINTING_Z_INDEX = PRINTING_Z_INDEX;

override get zIndex() {
return (this.parent as Spreadsheet)?.isPrinting ? this.PRINTING_Z_INDEX : this.Z_INDEX;
return (this.parent as Spreadsheet)?.isPrinting ? PRINTING_BG_Z_INDEX : BG_Z_INDEX;
}

override draw(
Expand All @@ -64,8 +54,8 @@ export class Background extends SheetExtension {
{ viewRanges, checkOutOfViewBound }: IDrawInfo
) {
const { stylesCache, worksheet, rowHeightAccumulation, columnTotalWidth, columnWidthAccumulation, rowTotalHeight } = spreadsheetSkeleton;
const { background: bgMatrixCacheByColor, backgroundPositions } = stylesCache;
if (!worksheet || !bgMatrixCacheByColor) return;
const { bgGroupMatrix } = stylesCache;
if (!worksheet || !bgGroupMatrix) return;
if (
!rowHeightAccumulation ||
!columnWidthAccumulation ||
Expand All @@ -78,7 +68,6 @@ export class Background extends SheetExtension {
const { scaleX, scaleY } = ctx.getScale();
const renderBGContext = {
ctx,
backgroundPositions,
scaleX,
scaleY,
checkOutOfViewBound,
Expand All @@ -87,42 +76,36 @@ export class Background extends SheetExtension {
spreadsheetSkeleton,
} as IRenderBGContext;
const renderBGCore = (rgb: string) => {
const bgColorMatrix = bgMatrixCacheByColor[rgb];
const bgColorMatrix = bgGroupMatrix[rgb];
ctx.fillStyle = rgb || getColor([255, 255, 255])!;
const backgroundPaths = new Path2D();

renderBGContext.backgroundPaths = backgroundPaths;
ctx.beginPath();
// bgColorMatrix.forValue(renderBGByCell);
viewRanges.forEach((range) => {
Range.foreach(range, (row, col) => {
const bgConfig = bgColorMatrix.getValue(row, col);
if (bgConfig) {
this.renderBGByCell(renderBGContext, row, col);
this.renderBGEachCell(renderBGContext, row, col);
}
});
});
ctx.fill(backgroundPaths);
ctx.closePath();
};

Object.keys(bgMatrixCacheByColor).forEach(renderBGCore);
Object.keys(bgGroupMatrix).forEach(renderBGCore);
ctx.restore();
}

renderBGByCell(bgContext: IRenderBGContext, row: number, col: number) {
const { spreadsheetSkeleton, backgroundPositions, backgroundPaths, scaleX, scaleY, viewRanges, diffRanges } = bgContext;
// if (!checkOutOfViewBound && !inViewRanges(viewRanges, row, col)) {
// return true;
// }
renderBGEachCell(bgContext: IRenderBGContext, row: number, col: number) {
const { spreadsheetSkeleton, backgroundPaths, scaleX, scaleY, viewRanges, diffRanges } = bgContext;

const cellInfo = backgroundPositions?.getValue(row, col);
if (cellInfo == null) {
return true;
}
const calcHeader = false;
const cellMergeInfo = spreadsheetSkeleton.getMergedCellInfo(row, col, calcHeader);

let { startY, endY, startX, endX } = cellInfo;
const { isMerged, isMergedMainCell, mergeInfo } = cellInfo;
let { startY, endY, startX, endX } = cellMergeInfo;
const { isMerged, isMergedMainCell, mergeInfo } = cellMergeInfo;
const renderRange = diffRanges && diffRanges.length > 0 ? diffRanges : viewRanges;

// isMerged isMergedMainCell are mutually exclusive. isMerged true then isMergedMainCell false.
Expand All @@ -140,12 +123,6 @@ export class Background extends SheetExtension {
endX = mergeInfo.endX;
}

// in merge range , but not top-left cell.
// if (isMerged) return true;

// const combineWithMergeRanges = mergeTo;
//expandRangeIfIntersects([...mergeTo], [mergeInfo]);

// If curr cell is not in the viewrange (viewport + merged cells), exit early.
if ((!isMerged && !isMergedMainCell) && !inViewRanges(renderRange!, row, col)) {
return true;
Expand Down
Loading

0 comments on commit ce068bb

Please sign in to comment.