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.

6.7 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 { IRange, IScale } from '@univerjs/core';
import type { SpreadsheetSkeleton, UniverRenderingContext } from '@univerjs/engine-render';
import { Range } from '@univerjs/core';
import { SheetExtension, SpreadsheetExtensionRegistry } from '@univerjs/engine-render';
import type { IConditionalFormattingRenderRangeResolver, IIconSetCellData } from './type';
import { iconMap, IIconSetType } from '../models/icon-map';
import { ConditionalFormattingRasterCache } from './conditional-formatting-raster-cache';
import { resolveRenderRanges } from './resolve-render-ranges';

export const IconUKey = 'sheet-conditional-rule-icon';
const EXTENSION_Z_INDEX = 35;
export const DEFAULT_WIDTH = 15;
export const DEFAULT_PADDING = 2;

const stringifyRange = (range: IRange) => {
    const { startRow, endRow, startColumn, endColumn } = range;
    return `${startRow}-${endRow}-${startColumn}-${endColumn}`;
};

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

    private _paddingRightAndLeft = DEFAULT_PADDING;

    private _width = DEFAULT_WIDTH;

    private _imageMap: Map<string, HTMLImageElement> = new Map();
    private readonly _rasterCache = new ConditionalFormattingRasterCache();
    private readonly _pendingImages = new Set<HTMLImageElement>();
    private _renderRangeResolver: IConditionalFormattingRenderRangeResolver | null = null;
    override uKey = IconUKey;

    override Z_INDEX = EXTENSION_Z_INDEX;
    _radius = 1;
    constructor() {
        super();
        this._init();
    }

    setRenderRangeResolver(resolver: IConditionalFormattingRenderRangeResolver | null) {
        this._renderRangeResolver = resolver;
    }

    override draw(
        ctx: UniverRenderingContext,
        _parentScale: IScale,
        spreadsheetSkeleton: SpreadsheetSkeleton,
        diffRanges: IRange[]
    ) {
        const { worksheet } = spreadsheetSkeleton;
        if (!worksheet) {
            return false;
        }
        const mergeCellRendered = new Set<string>();
        const sourceRanges = diffRanges?.length ? diffRanges : [spreadsheetSkeleton.rowColumnSegment];
        const renderRanges = resolveRenderRanges(worksheet, sourceRanges, this._renderRangeResolver);
        if (!renderRanges.length) {
            return;
        }
        ctx.save();
        // ctx.globalCompositeOperation = 'destination-over';
        renderRanges.forEach((range) => {
            Range.foreach(range, (row, col) => {
                if (!worksheet.getRowVisible(row) || !worksheet.getColVisible(col)) {
                    return;
                }

                const primaryWithCoord = spreadsheetSkeleton.getCellWithCoordByIndex(row, col, false);
                const { isMerged, isMergedMainCell, mergeInfo } = primaryWithCoord;

                let cellData = worksheet.getCell(row, col) as IIconSetCellData;
                if (isMerged) {
                    cellData = worksheet.getCell(mergeInfo.startRow, mergeInfo.startColumn) as IIconSetCellData;
                }

                if (!cellData?.iconSet) {
                    return;
                }

                const { iconType, iconId } = cellData.iconSet;
                if (iconType === IIconSetType.empty) {
                    return;
                }

                const icon = this._imageMap.get(this._createKey(iconType, iconId));
                if (!icon) {
                    return;
                }

                if (!this.isRenderDiffRangesByCell(mergeInfo, diffRanges)) {
                    return;
                }

                if (isMerged || isMergedMainCell) {
                    const rangeStr = stringifyRange(mergeInfo);
                    if (mergeCellRendered.has(rangeStr)) {
                        return;
                    }

                    mergeCellRendered.add(rangeStr);
                }

                const { startX, endX, startY, endY } = (isMerged || isMergedMainCell) ? mergeInfo : primaryWithCoord;
                const borderWidth = endX - startX;
                const borderHeight = endY - startY;
                if (this._width > borderHeight || this._width > borderWidth + this._paddingRightAndLeft * 2) {
                    return;
                }

                // Highly centered processing
                const y = (borderHeight - this._width) / 2 + startY;
                this._drawIcon(ctx, icon, startX + this._paddingRightAndLeft, y);
            });
        });
        ctx.restore();
    }

    override dispose(): void {
        this._rasterCache.dispose();
        for (const image of this._pendingImages) {
            image.onload = null;
        }
        this._pendingImages.clear();
        this._imageMap.clear();
        this._renderRangeResolver = null;
        super.dispose();
    }

    private _drawIcon(ctx: UniverRenderingContext, image: HTMLImageElement, x: number, y: number): void {
        if (!ctx._context || ctx.__mode === 'printing' || ctx.getSystemType() !== 'Linux') {
            ctx.drawImage(image, x, y, this._width, this._width);
            return;
        }
        this._rasterCache.draw(ctx, x, y, this._width, this._width, image.src, (paint, left, top) => {
            paint.drawImage(image, left, top, this._width, this._width);
        });
    }

    private _init() {
        for (const type in iconMap) {
            const list = iconMap[type as IIconSetType];
            list.forEach((base64, index) => {
                const key = this._createKey(type as IIconSetType, String(index));
                const image = new Image();
                image.onload = () => {
                    image.onload = null;
                    this._pendingImages.delete(image);
                    this._imageMap.set(key, image);
                    this.parent?.makeDirty(true);
                };
                this._pendingImages.add(image);
                image.src = base64;
            });
        }
    }

    private _createKey(iconType: IIconSetType, iconIndex: string) {
        return `${iconType}_${iconIndex}`;
    }
}

SpreadsheetExtensionRegistry.add(ConditionalFormattingIcon);