Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

10.9 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * 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.
 */

import type { IScale } from '@univerjs/core';
import type { UniverRenderingContext } from '../../../context';
import type { IARowCfg, IARowCfgObj, IHeaderStyleCfg, IRowStyleCfg } from '../interfaces';
import type { SpreadsheetSkeleton } from '../sheet.render-skeleton';
import { DEFAULT_FONTFACE_PLANE, FIX_ONE_PIXEL_BLUR_OFFSET, MIDDLE_CELL_POS_MAGIC_NUMBER } from '../../../basics/const';
import { getColor } from '../../../basics/tools';
import { SheetRowHeaderExtensionRegistry } from '../../extension';
import { SheetExtension } from './sheet-extension';

const UNIQUE_KEY = 'DefaultRowHeaderLayoutExtension';
const MIN_TEXT_RENDER_HEIGHT_IN_SCREEN_PX = 4;

export interface IRowsHeaderCfgParam {
    headerStyle?: Partial<IRowStyleCfg>;
    rowsCfg?: Record<number, IARowCfg>;
}

const DEFAULT_ROW_STYLE = {
    fontSize: 13,
    fontFamily: DEFAULT_FONTFACE_PLANE,
    fontColor: 'gray.900',
    backgroundColor: 'gray.50',
    borderColor: 'gray.200',
    textAlign: 'center',
    textBaseline: 'middle',
} as const;

const DEFAULT_PRINTING_ROW_STYLE = {
    ...DEFAULT_ROW_STYLE,
    fontColor: '#000000',
    backgroundColor: getColor([248, 249, 250]),
    borderColor: getColor([217, 217, 217]),
} as const;

export class RowHeaderLayout extends SheetExtension {
    override readonly viewportCacheReusable = this.constructor === RowHeaderLayout;

    override uKey = UNIQUE_KEY;
    override Z_INDEX = 10;
    // The workbook rows configuration and header style
    rowsCfg: Record<number, IARowCfg> = {};
    headerStyle: Partial<IRowStyleCfg> = {};
    // Map of row configurations and header styles for each worksheet, where the key is worksheet id
    rowsCfgOfWorksheet: Map<string, Record<number, IARowCfg>> = new Map();
    headerStyleOfWorksheet: Map<string, Partial<IRowStyleCfg>> = new Map();

    constructor(cfg?: IRowsHeaderCfgParam) {
        super();
        if (cfg) {
            this.configHeaderRow(cfg);
        }
    }

    configHeaderRow(cfg: IRowsHeaderCfgParam, sheetId?: string) {
        if (sheetId) {
            this.rowsCfgOfWorksheet.set(sheetId, cfg.rowsCfg ?? {});
            this.headerStyleOfWorksheet.set(sheetId, cfg.headerStyle ?? {});
        } else {
            this.rowsCfg = cfg.rowsCfg ?? {};
            this.headerStyle = cfg.headerStyle ?? {};
        }
    }

    getRowsCfg(sheetId: string): Record<number, IARowCfg> {
        const rowsCfg = this.rowsCfgOfWorksheet.get(sheetId) ?? {};
        return { ...this.rowsCfg, ...rowsCfg };
    }

    getHeaderStyle(sheetId: string, isPrinting = false): IRowStyleCfg {
        const headerStyle = this.headerStyleOfWorksheet.get(sheetId) ?? {};
        const defaultStyle = isPrinting ? DEFAULT_PRINTING_ROW_STYLE : DEFAULT_ROW_STYLE;
        return { ...defaultStyle, ...this.headerStyle, ...headerStyle };
    }

    getCfgOfCurrentRow(rowsCfg: Record<number, IARowCfg>, headerStyle: IHeaderStyleCfg, rowIndex: number) {
        let mergeWithSpecCfg;
        let curRowSpecCfg;

        if (rowsCfg[rowIndex]) {
            if (typeof rowsCfg[rowIndex] == 'string') {
                rowsCfg[rowIndex] = { text: rowsCfg[rowIndex] } as IARowCfgObj;
            }
            curRowSpecCfg = rowsCfg[rowIndex] as IRowStyleCfg & { text: string };
            mergeWithSpecCfg = { ...headerStyle, ...curRowSpecCfg };
        } else {
            mergeWithSpecCfg = { ...headerStyle, text: `${rowIndex + 1}` };
        }
        const specStyle = Object.keys(curRowSpecCfg || {}).length > 1; // if cfg have more keys than 'text', means there would be special style config for this row.
        return [mergeWithSpecCfg, specStyle] as [IARowCfgObj, boolean];
    }

    setStyleToCtx(ctx: UniverRenderingContext, rowStyle: Partial<IHeaderStyleCfg>) {
        if (rowStyle.textAlign) ctx.textAlign = rowStyle.textAlign;
        if (rowStyle.textBaseline) ctx.textBaseline = rowStyle.textBaseline;
        if (rowStyle.fontColor) ctx.fillStyle = rowStyle.fontColor;
        if (rowStyle.borderColor) ctx.strokeStyle = rowStyle.borderColor;
        if (rowStyle.fontSize) ctx.font = `${rowStyle.fontSize}px ${DEFAULT_FONTFACE_PLANE}`;
    }

    // eslint-disable-next-line max-lines-per-function
    override draw(ctx: UniverRenderingContext, parentScale: IScale, spreadsheetSkeleton: SpreadsheetSkeleton) {
        const { rowColumnSegment, rowHeaderWidth = 0 } = spreadsheetSkeleton;
        const { startRow, endRow } = rowColumnSegment;

        if (!spreadsheetSkeleton || rowHeaderWidth === 0) {
            return;
        }

        const { rowHeightAccumulation, columnTotalWidth, columnWidthAccumulation, rowTotalHeight, worksheet } =
            spreadsheetSkeleton;

        if (
            !rowHeightAccumulation ||
            !columnWidthAccumulation ||
            columnTotalWidth === undefined ||
            rowTotalHeight === undefined
        ) {
            return;
        }

        const rowsCfg = this.getRowsCfg(worksheet.getSheetId());
        const headerStyle = this.getHeaderStyle(worksheet.getSheetId(), ctx.__mode === 'printing');

        const scale = this._getScale(parentScale);
        this.setStyleToCtx(ctx, headerStyle);

        // background
        ctx.save();
        ctx.fillStyle = headerStyle.backgroundColor;
        ctx.fillRectByPrecision(0, 0, rowHeaderWidth, rowTotalHeight);
        ctx.restore();

        ctx.setLineWidthByPrecision(1);
        ctx.translateWithPrecisionRatio(FIX_ONE_PIXEL_BLUR_OFFSET, FIX_ONE_PIXEL_BLUR_OFFSET);

        let preRowPosition = 0;
        const rowHeightAccumulationLength = rowHeightAccumulation.length;
        const rowGaps = spreadsheetSkeleton.gapConfig?.rowGaps;
        for (let r = startRow - 1; r <= endRow; r++) {
            if (r < 0 || r > rowHeightAccumulationLength - 1) {
                continue;
            }
            const rowEndPosition = rowHeightAccumulation[r];
            if (preRowPosition === rowEndPosition) {
                continue; // Skip hidden rows
            }

            // Draw gap area in row header if a gap is configured before this row
            const gapSize = rowGaps?.[r]?.size ?? 0;
            if (gapSize > 0) {
                const gapItem = rowGaps![r];
                const gapTop = preRowPosition;
                const gapBottom = preRowPosition + gapSize;
                // defaultBackgroundColor and defaultStripeColor are guaranteed by SheetSkeleton._fillDefaultGapThemeColors
                const { defaultBackgroundColor = '', defaultStripeColor = '' } = spreadsheetSkeleton.gapConfig || {};
                const defaultBg = defaultBackgroundColor;
                const defaultStripe = defaultStripeColor;

                // Fill gap background
                ctx.save();
                ctx.fillStyle = gapItem.color ?? defaultBg;
                ctx.fillRectByPrecision(0, gapTop, rowHeaderWidth, gapSize);
                ctx.restore();

                // Draw diagonal stripes
                ctx.save();
                ctx.beginPath();
                ctx.rectByPrecision(0, gapTop, rowHeaderWidth, gapSize);
                ctx.clip();
                ctx.strokeStyle = gapItem.stripeColor ?? defaultStripe;
                ctx.lineWidth = 1;
                ctx.beginPath();
                const spacing = 6;
                for (let d = -gapSize; d < rowHeaderWidth; d += spacing) {
                    ctx.moveTo(d, gapBottom);
                    ctx.lineTo(d + gapSize, gapTop);
                }
                ctx.stroke();
                ctx.restore();

                preRowPosition = gapBottom;
            }

            // After gap, the row itself may still be hidden (height = 0)
            if (preRowPosition >= rowEndPosition) {
                preRowPosition = rowEndPosition;
                continue;
            }

            const cellBound = {
                left: 0,
                top: preRowPosition,
                right: rowHeaderWidth,
                bottom: rowEndPosition,
                width: rowHeaderWidth,
                height: rowEndPosition - preRowPosition,
            };
            const [curRowCfg, specStyle] = this.getCfgOfCurrentRow(rowsCfg, headerStyle, r);

            // background
            if (specStyle && curRowCfg.backgroundColor) {
                ctx.save();
                ctx.fillStyle = curRowCfg.backgroundColor;
                ctx.fillRectByPrecision(cellBound.left, cellBound.top, cellBound.width, cellBound.height);
                ctx.restore();
            }

            // horizontal line border
            ctx.beginPath();
            ctx.moveToByPrecision(cellBound.left, cellBound.bottom);
            ctx.lineToByPrecision(cellBound.right, cellBound.bottom);
            ctx.stroke();

            if (cellBound.height * Math.abs(parentScale.scaleY ?? 1) < MIN_TEXT_RENDER_HEIGHT_IN_SCREEN_PX) {
                preRowPosition = rowEndPosition;
                continue;
            }

            // row header text
            const textX = (() => {
                switch (curRowCfg.textAlign) {
                    case 'center':
                        return cellBound.left + (cellBound.right - cellBound.left) / 2;
                    case 'right':
                        return cellBound.right - MIDDLE_CELL_POS_MAGIC_NUMBER;
                    case 'left':
                        return cellBound.left + MIDDLE_CELL_POS_MAGIC_NUMBER;
                    default: // center
                        return cellBound.left + (cellBound.right - cellBound.left) / 2;
                }
            })();
            const middleYCellRect = preRowPosition + (rowEndPosition - preRowPosition) / 2 + MIDDLE_CELL_POS_MAGIC_NUMBER; // Magic number 1, because the vertical alignment appears to be off by 1 pixel

            ctx.save();
            ctx.beginPath();
            if (specStyle) {
                this.setStyleToCtx(ctx, curRowCfg);
            }

            ctx.rectByPrecision(cellBound.left, cellBound.top, cellBound.width, cellBound.height);
            ctx.clip();
            ctx.fillText(curRowCfg.text, textX, middleYCellRect);
            ctx.restore();

            preRowPosition = rowEndPosition;
        }

        // border right line
        const rowHeaderWidthFix = rowHeaderWidth - 0.5 / scale;
        ctx.beginPath();
        ctx.moveToByPrecision(rowHeaderWidthFix, 0);
        ctx.lineToByPrecision(rowHeaderWidthFix, rowTotalHeight);
        ctx.stroke();
    }
}

SheetRowHeaderExtensionRegistry.add(new RowHeaderLayout());