import './color-panel.scss'; import {designComponent, getComponentCls, iMouseEvent, reactive, useModel} from "@peryl/react-compose"; import {ColorSvPanel} from "./sub/ColorSvPanel"; import {ColorHueSlider} from "./sub/ColorHueSlider"; import {ColorAlphaSlider} from "./sub/ColorAlphaSlider"; import {PlainColor} from "./utils/PlainColor"; import {useColorPanelEditor} from "./useColorPanelEditor"; import {useColorPanelHistory} from "./useColorPanelHistory"; import {ColorPanelPublicPropOptions} from "./utils/color-picker.utils"; import {Icon} from "../Icon"; import {$message} from "../$message"; import {i18n} from "../i18n"; export const ColorPanel = designComponent({ name: 'color-panel', props: { ...ColorPanelPublicPropOptions }, emits: { onUpdateModelValue: (val: any) => true, onDblclickSvPanel: (e: iMouseEvent) => true, }, inheritPropsType: HTMLDivElement, setup({ props, event: { emit } }) { const model = useModel(() => props.modelValue, emit.onUpdateModelValue, { onChange: (value) => { const prev = state.colorState; const colorState = utils.getState(value, prev); if (colorState !== prev) { state.colorState = colorState; } }, }); const utils = { getState: (value: string | null | undefined, prev?: iColorState): iColorState => { if (!prev) { return { color: new PlainColor(value || '#FFFFFF'), value }; } else { if (prev.value == value) { return prev; } else { return { color: new PlainColor(value || '#FFFFFF'), value }; } } }, }; const state = reactive({ colorState: utils.getState(model.value) }); const handler = { svChange: ({ value, saturation }: { value: number, saturation: number }) => { const color = state.colorState.color.useHsva({ v: value, s: saturation }); state.colorState = { color, value: color.value }; model.value = state.colorState.value!; }, hueChange: (hue: number) => { const color = state.colorState.color.useHsva({ h: hue }); state.colorState = { color, value: color.value }; model.value = state.colorState.value!; }, alphaChange: (alpha: number) => { const color = state.colorState.color.useHsva({ a: alpha / 100 }); state.colorState = { color, value: color.value }; model.value = state.colorState.value!; }, dropColor: () => { if (!('EyeDropper' in window)) { $message.error(i18n.$it('colorPicker.notSupportDropper').d('您的浏览器不支持颜色拾取器,推荐使用谷歌浏览器')); return; } const close = $message(i18n.$it('colorPicker.exitDropperMode').d('按 ESC 键关闭取色吸管'), { time: null }); const eyeDropper = new (window as any).EyeDropper(); eyeDropper.open().then((result: { sRGBHex: string }) => { state.colorState = utils.getState(result.sRGBHex); model.value = state.colorState.value!; close(); }).catch(() => { close(); }); }, }; const editor = useColorPanelEditor({ props, getColor: () => state.colorState.color, onChange: color => { state.colorState = { color, value: color.value }; model.value = color.value; } }); const history = useColorPanelHistory({ onChange: color => { state.colorState = { color, value: color.value }; model.value = color.value; } }); return { render: () => (
{editor.renderContent.value()}
{history.renderContent.value()}
) }; }, }); interface iColorState { color: PlainColor, value: string | null | undefined }