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.

7.0 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 { Workbook } from '@univerjs/core';
import type {
    IFormulaHighlightCell,
    IHighlightCell,
} from '@univerjs/sheets-conditional-formatting';
import type { IFormulaEditorRef } from '@univerjs/sheets-formula-ui';
import type { LocaleKey } from '../../../locale/types';
import type { IStyleEditorProps } from './type';
import { IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
import { borderClassName, clsx } from '@univerjs/design';
import { LexerTreeBuilder, operatorToken } from '@univerjs/engine-formula';
import { CFRuleType, CFSubRuleType } from '@univerjs/sheets-conditional-formatting';
import { FormulaEditor } from '@univerjs/sheets-formula-ui';
import { useDependency, useSidebarClick } from '@univerjs/ui';
import { useEffect, useRef, useState } from 'react';
import { ConditionalFormattingRulePreview } from '../../ConditionalFormattingRulePreview';
import { ConditionalStyleEditor } from '../../ConditionalStyleEditor';
import { previewClassName } from './styles';

export const FormulaStyleEditor = (props: IStyleEditorProps) => {
    const { onChange, interceptorManager, ColorPickerComponent, FormulaEditorComponent = FormulaEditor } = props;
    const localeService = useDependency(LocaleService);
    const lexerTreeBuilder = useDependency(LexerTreeBuilder);
    const univerInstanceService = useDependency(IUniverInstanceService);
    const workbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
    const worksheet = workbook.getActiveSheet();

    const rule = props.rule?.type === CFRuleType.highlightCell ? props.rule : undefined;

    const divEleRef = useRef<HTMLDivElement>(null);
    const [isFocusFormulaEditor, setIsFocusFormulaEditor] = useState(false);
    const formulaEditorRef = useRef<IFormulaEditorRef>(null);
    const [style, setStyle] = useState<IHighlightCell['style']>({});
    const [formula, setFormula] = useState(() => {
        if (rule?.subType === CFSubRuleType.formula) {
            return rule.value;
        }
        return '=';
    });
    const [formulaError, setFormulaError] = useState<string | undefined>(undefined);

    const getResult = (config: {
        style: IHighlightCell['style'];
        formula: string;
    }): IFormulaHighlightCell => {
        return {
            style: config.style,
            value: formula,
            type: CFRuleType.highlightCell,
            subType: CFSubRuleType.formula,
        };
    };

    useEffect(() => {
        const dispose = interceptorManager.intercept(interceptorManager.getInterceptPoints().submit, {
            handler() {
                return getResult({ style, formula });
            },
        });
        return dispose as () => void;
    }, [style, formula, interceptorManager]);

    useEffect(() => {
        const dispose = interceptorManager.intercept(interceptorManager.getInterceptPoints().beforeSubmit, {
            handler: (v, _c, next) => {
                const isFormulaValid = formula.length > 1
                    && formula.startsWith(operatorToken.EQUALS)
                    && lexerTreeBuilder.checkIfAddBracket(formula) === 0;

                if (formulaError || !isFormulaValid) {
                    setFormulaError(localeService.t<LocaleKey>('sheets-conditional-formatting-ui.errorMessage.formulaError'));
                    return false;
                }
                return next(v);
            },
        });
        return dispose as () => void;
    }, [
        formulaError,
        formula,
        interceptorManager,
        lexerTreeBuilder,
        localeService,
    ]);

    const _onChange = (config: {
        formula: string;
        style: IHighlightCell['style'];
    }) => {
        onChange(getResult(config));
    };

    useSidebarClick((e: MouseEvent) => {
        const isOutSide = formulaEditorRef.current?.isClickOutSide(e);
        isOutSide && setIsFocusFormulaEditor(false);
    });

    return (
        <div ref={divEleRef}>
            <div
                className={`
                  univer-mt-4 univer-text-sm univer-text-gray-600
                  dark:!univer-text-gray-200
                `}
            >
                {localeService.t<LocaleKey>('sheets-conditional-formatting-ui.panel.styleRule')}
            </div>
            <div className="univer-mt-3">
                <FormulaEditorComponent
                    ref={formulaEditorRef}
                    className={clsx(`
                      univer-box-border univer-h-8 univer-w-full univer-cursor-pointer univer-items-center
                      univer-rounded-lg univer-bg-gray-0 univer-pt-2 univer-transition-colors
                      hover:univer-border-primary-600
                      dark:!univer-bg-gray-700 dark:!univer-text-gray-0
                      [&>div:first-child]:univer-px-2.5
                      [&>div]:univer-h-5 [&>div]:univer-ring-transparent
                    `, borderClassName)}
                    errorText={formulaError}
                    isFocus={isFocusFormulaEditor}
                    initValue={formula as any}
                    unitId={workbook.getUnitId()}
                    subUnitId={worksheet?.getSheetId()}
                    onFocus={() => { setIsFocusFormulaEditor(true); }}
                    onChange={(formula) => {
                        setFormula(formula);
                        _onChange({ style, formula });
                    }}
                    onVerify={(result, formula) => {
                        if (!result || formula.length === 1) {
                            setFormulaError(localeService.t<LocaleKey>('sheets-conditional-formatting-ui.errorMessage.formulaError'));
                        } else {
                            setFormulaError(undefined);
                        }
                    }}
                />
            </div>

            <div className={previewClassName}>
                <ConditionalFormattingRulePreview
                    rule={getResult({
                        style,
                        formula,
                    })}
                />
            </div>
            <ConditionalStyleEditor
                ColorPickerComponent={ColorPickerComponent}
                style={rule?.style}
                className="univer-mt-3"
                onChange={(v) => {
                    setStyle(v);
                    _onChange({ style: v, formula });
                }}
            />
        </div>
    );
};