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.8 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 { IBorderData, Nullable } from '@univerjs/core';
import type { IBorderInfo } from '@univerjs/sheets';
import type { LocaleKey } from '../../locale/types';
import type { IBorderPanelProps } from './interface';
import { LocaleService } from '@univerjs/core';
import { clsx, ColorPicker, ConfigContext, Dropdown, Separator } from '@univerjs/design';
import { MoreDownIcon, PaintBucketDoubleIcon } from '@univerjs/icons';
import { SheetsSelectionsService } from '@univerjs/sheets';
import { IconManager, useDependency } from '@univerjs/ui';
import { useContext } from 'react';
import { BorderLine } from './border-line/BorderLine';
import { BORDER_LINE_CHILDREN, BORDER_SIZE_CHILDREN } from './interface';

export interface IResponsiveBorderPanelProps extends IBorderPanelProps {
    ColorPickerComponent?: typeof ColorPicker;
    DropdownComponent?: typeof Dropdown;
}

export function BorderPanel(props: IResponsiveBorderPanelProps) {
    const localeService = useDependency(LocaleService);
    const iconManager = useDependency(IconManager);
    const selectionManagerService = useDependency(SheetsSelectionsService);
    const { direction } = useContext(ConfigContext);

    const { isAllValuesSame, value: currentValue } = selectionManagerService.getCellStylesProperty('bd');
    const color = isAllValuesSame ? getBorderColor(currentValue as Nullable<IBorderData>) : undefined;

    const { onChange, value, ColorPickerComponent = ColorPicker, DropdownComponent = Dropdown } = props;

    function getBorderColor(borderData: Nullable<IBorderData>): string | undefined {
        if (!borderData) {
            return;
        }
        for (const key in borderData) {
            const border = borderData[key as keyof IBorderData];
            if (border?.cl?.rgb) {
                return border.cl.rgb;
            }
        }
    }

    function handleClick(v: string | number, type: keyof IBorderInfo) {
        onChange?.({
            ...value,
            [type]: v,
        });
    }

    function renderIcon(icon: string) {
        const Icon = iconManager.get(icon);

        return Icon && <Icon className="univer-fill-primary-600" />;
    }

    return (
        <section dir={direction} className="univer-box-border univer-grid univer-gap-2 univer-p-1.5">
            <div
                className={`
                  univer-box-border univer-grid univer-grid-cols-5 univer-gap-2 univer-text-gray-600
                  dark:!univer-text-gray-200
                `}
            >
                {BORDER_LINE_CHILDREN.map((item) => (
                    <a
                        key={item.value}
                        className={clsx(`
                          univer-flex univer-size-6 univer-cursor-pointer univer-items-center univer-justify-center
                          univer-justify-self-center univer-rounded
                          hover:univer-bg-gray-100
                          dark:hover:!univer-bg-gray-700
                        `, {
                            'univer-bg-gray-200 dark:!univer-bg-gray-600': value.type === item.value,
                        })}
                        onClick={(e) => {
                            e.stopPropagation();
                            handleClick(item.value, 'type');
                        }}
                    >
                        {renderIcon(item.icon)}
                    </a>
                ))}
            </div>

            <Separator />

            <div className="univer-flex univer-items-center univer-gap-2">
                <div>
                    <DropdownComponent
                        overlay={(
                            <div className="univer-rounded-lg univer-p-4">
                                <ColorPickerComponent value={color} onChange={(value) => handleClick(value, 'color')} />
                            </div>
                        )}
                    >
                        <button
                            className={`
                              univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-rounded
                              univer-border-none univer-bg-transparent univer-p-1 univer-text-gray-900
                              hover:univer-bg-gray-100
                              dark:hover:!univer-bg-gray-700
                            `}
                            type="button"
                        >
                            <PaintBucketDoubleIcon
                                className={`
                                  univer-fill-primary-600
                                  dark:!univer-text-gray-0
                                `}
                                extend={{ colorChannel1: value?.color ?? '' }}
                            />
                            <MoreDownIcon className="dark:!univer-text-gray-0" />
                        </button>
                    </DropdownComponent>
                </div>

                <div>
                    <DropdownComponent
                        overlay={(
                            <section className="univer-rounded-lg univer-p-1.5">
                                <ul className="univer-m-0 univer-grid univer-list-none univer-gap-1 univer-p-0">
                                    {BORDER_SIZE_CHILDREN.map((item) => (
                                        <li key={item.value}>
                                            <button
                                                type="button"
                                                aria-label={localeService.t<LocaleKey>(item.label)}
                                                aria-pressed={item.value === value.style}
                                                className={clsx(`
                                                  univer-flex univer-w-full univer-cursor-pointer univer-items-center
                                                  univer-justify-center univer-rounded univer-border-none
                                                  univer-bg-transparent univer-px-1 univer-py-3
                                                  hover:univer-bg-gray-100
                                                  dark:hover:!univer-bg-gray-700
                                                `, {
                                                    'univer-bg-gray-200 dark:!univer-bg-gray-600': item.value === value.style,
                                                })}
                                                onClick={() => handleClick(item.value, 'style')}
                                            >
                                                <BorderLine
                                                    className={`
                                                      univer-fill-gray-900
                                                      dark:!univer-fill-gray-0
                                                    `}
                                                    type={item.value}
                                                />
                                            </button>
                                        </li>
                                    ))}
                                </ul>
                            </section>
                        )}
                    >
                        <button
                            className={`
                              univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-rounded
                              univer-border-none univer-bg-transparent univer-p-1 univer-text-gray-900
                              hover:univer-bg-gray-100
                              dark:hover:!univer-bg-gray-700
                            `}
                            type="button"
                        >
                            <BorderLine
                                className={`
                                  univer-fill-gray-900
                                  dark:!univer-fill-gray-0
                                `}
                                type={value.style}
                            />
                            <MoreDownIcon className="dark:!univer-text-gray-0" />
                        </button>
                    </DropdownComponent>
                </div>
            </div>
        </section>
    );
}