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.

8.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 { describe, expect, it, vi } from 'vitest';
import { ColumnHeaderLayout } from '../column-header-layout';

function createCtx() {
    return {
        save: vi.fn(),
        restore: vi.fn(),
        beginPath: vi.fn(),
        moveTo: vi.fn(),
        lineTo: vi.fn(),
        moveToByPrecision: vi.fn(),
        lineToByPrecision: vi.fn(),
        stroke: vi.fn(),
        clip: vi.fn(),
        rectByPrecision: vi.fn(),
        fillRectByPrecision: vi.fn(),
        setLineWidthByPrecision: vi.fn(),
        translateWithPrecisionRatio: vi.fn(),
        fillText: vi.fn(),
        fillStyle: '',
        strokeStyle: '',
        textAlign: 'center',
        textBaseline: 'middle',
        font: '',
    } as any;
}

describe('column header layout extension', () => {
    it('supports config getters and style setter', () => {
        const layout = new ColumnHeaderLayout({
            columnsCfg: {
                0: 'A-Col',
            },
            headerStyle: {
                fontColor: '#135',
            },
        });

        layout.configHeaderColumn(
            {
                columnsCfg: {
                    1: { text: 'B-Col', textAlign: 'left' },
                },
                headerStyle: {
                    backgroundColor: '#eee',
                },
            },
            'sheet-1'
        );

        const columnsCfg = layout.getColumnsCfg('sheet-1');
        expect(columnsCfg[0]).toBe('A-Col');
        expect((columnsCfg[1] as any).text).toBe('B-Col');

        const headerStyle = layout.getHeaderStyle('sheet-1');
        expect(headerStyle.fontColor).toBe('#135');
        expect(headerStyle.backgroundColor).toBe('#eee');

        const [cfg, specStyle] = layout.getCfgOfCurrentColumn(columnsCfg, headerStyle, 0);
        expect(cfg.text).toBe('A-Col');
        expect(specStyle).toBe(false);

        const ctx = createCtx();
        layout.setStyleToCtx(ctx, { textAlign: 'right', textBaseline: 'alphabetic', fontColor: '#222', borderColor: '#333', fontSize: 12 });
        expect(ctx.textAlign).toBe('right');
        expect(ctx.textBaseline).toBe('alphabetic');
        expect(ctx.fillStyle).toBe('#222');
        expect(ctx.strokeStyle).toBe('#333');
        expect(ctx.font).toContain('12px');
    });

    it('draws column headers and handles early returns/hidden columns', () => {
        const layout = new ColumnHeaderLayout({
            columnsCfg: {
                0: { text: 'X', textAlign: 'right', backgroundColor: '#f0f0f0' },
            },
        });
        const ctx = createCtx();

        layout.draw(ctx, { scaleX: 1, scaleY: 1 } as any, {
            rowColumnSegment: { startRow: 0, endRow: 0, startColumn: 0, endColumn: 1 },
            columnHeaderHeight: 0,
        } as any);
        expect(ctx.fillRectByPrecision).not.toHaveBeenCalled();

        const skeleton = {
            rowColumnSegment: { startRow: 0, endRow: 0, startColumn: 0, endColumn: 2 },
            columnHeaderHeight: 22,
            rowHeightAccumulation: [20],
            columnWidthAccumulation: [40, 40, 90],
            columnTotalWidth: 90,
            rowTotalHeight: 20,
            worksheet: {
                getSheetId: vi.fn(() => 'sheet-main'),
            },
        } as any;

        layout.draw(ctx, { scaleX: 1, scaleY: 1 } as any, skeleton);

        expect(ctx.fillRectByPrecision).toHaveBeenCalled();
        expect(ctx.translateWithPrecisionRatio).toHaveBeenCalled();
        expect(ctx.stroke).toHaveBeenCalled();
        expect(ctx.fillText).toHaveBeenCalled();
    });

    it('omits column labels that cannot occupy four screen pixels', () => {
        const layout = new ColumnHeaderLayout();
        const ctx = createCtx();
        const compressedColumnCount = 228;
        const skeleton = {
            rowColumnSegment: { startRow: 0, endRow: 0, startColumn: 0, endColumn: compressedColumnCount },
            columnHeaderHeight: 20,
            rowHeightAccumulation: [20],
            columnWidthAccumulation: Array.from(
                { length: compressedColumnCount + 1 },
                (_, column) => column < compressedColumnCount ? (column + 1) * 2 : compressedColumnCount * 2 + 20
            ),
            columnTotalWidth: compressedColumnCount * 2 + 20,
            rowTotalHeight: 20,
            worksheet: {
                getSheetId: vi.fn(() => 'sheet-main'),
            },
        } as any;

        layout.draw(ctx, { scaleX: 1, scaleY: 1 } as any, skeleton);

        expect(ctx.fillText).toHaveBeenCalledOnce();
        expect(ctx.fillText).toHaveBeenCalledWith('HU', expect.any(Number), expect.any(Number));
        expect(ctx.stroke).toHaveBeenCalledTimes(compressedColumnCount + 2);

        ctx.fillText.mockClear();
        layout.draw(ctx, { scaleX: 2, scaleY: 1 } as any, skeleton);
        expect(ctx.fillText).toHaveBeenCalledTimes(compressedColumnCount + 1);
    });

    it('clips default column labels to their header cells', () => {
        const layout = new ColumnHeaderLayout({
            columnsCfg: {
                0: 'AA',
                1: 'AB',
            },
        });
        const ctx = createCtx();
        const skeleton = {
            rowColumnSegment: { startRow: 0, endRow: 0, startColumn: 0, endColumn: 1 },
            columnHeaderHeight: 20,
            rowHeightAccumulation: [20],
            columnWidthAccumulation: [8, 16],
            columnTotalWidth: 16,
            rowTotalHeight: 20,
            worksheet: {
                getSheetId: vi.fn(() => 'sheet-main'),
            },
        } as any;

        layout.draw(ctx, { scaleX: 1, scaleY: 1 } as any, skeleton);

        expect(ctx.fillText).toHaveBeenCalledTimes(2);
        expect(ctx.rectByPrecision).toHaveBeenNthCalledWith(1, 0, 0, 8, 20);
        expect(ctx.rectByPrecision).toHaveBeenNthCalledWith(2, 8, 0, 8, 20);
        expect(ctx.clip).toHaveBeenCalledTimes(2);
    });

    it('uses gapConfig default colors when drawing column gaps', () => {
        const layout = new ColumnHeaderLayout();
        const ctx = createCtx();
        const fillStyles: string[] = [];
        const strokeStyles: string[] = [];

        ctx.fillRectByPrecision = vi.fn(() => {
            fillStyles.push(ctx.fillStyle);
        });
        ctx.stroke = vi.fn(() => {
            strokeStyles.push(ctx.strokeStyle);
        });

        const skeleton = {
            rowColumnSegment: { startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 },
            columnHeaderHeight: 20,
            rowHeightAccumulation: [20],
            columnWidthAccumulation: [60],
            columnTotalWidth: 60,
            rowTotalHeight: 20,
            gapConfig: {
                defaultBackgroundColor: 'rgba(1, 2, 3, 0.08)',
                defaultStripeColor: 'rgba(4, 5, 6, 0.25)',
                colGaps: {
                    0: { size: 6 },
                },
            },
            worksheet: {
                getSheetId: vi.fn(() => 'sheet-main'),
            },
        } as any;

        layout.draw(ctx, { scaleX: 1, scaleY: 1 } as any, skeleton);

        expect(fillStyles).toContain('rgba(1, 2, 3, 0.08)');
        expect(strokeStyles).toContain('rgba(4, 5, 6, 0.25)');
    });

    it('uses gap item custom colors before default colors in column gaps', () => {
        const layout = new ColumnHeaderLayout();
        const ctx = createCtx();
        const fillStyles: string[] = [];
        const strokeStyles: string[] = [];

        ctx.fillRectByPrecision = vi.fn(() => {
            fillStyles.push(ctx.fillStyle);
        });
        ctx.stroke = vi.fn(() => {
            strokeStyles.push(ctx.strokeStyle);
        });

        const skeleton = {
            rowColumnSegment: { startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 },
            columnHeaderHeight: 20,
            rowHeightAccumulation: [20],
            columnWidthAccumulation: [60],
            columnTotalWidth: 60,
            rowTotalHeight: 20,
            gapConfig: {
                defaultBackgroundColor: 'rgba(1, 2, 3, 0.08)',
                defaultStripeColor: 'rgba(4, 5, 6, 0.25)',
                colGaps: {
                    0: { size: 6, color: '#112233', stripeColor: '#334455' },
                },
            },
            worksheet: {
                getSheetId: vi.fn(() => 'sheet-main'),
            },
        } as any;

        layout.draw(ctx, { scaleX: 1, scaleY: 1 } as any, skeleton);

        expect(fillStyles).toContain('#112233');
        expect(strokeStyles).toContain('#334455');
    });
});