import {computed, reactive, watch} from "@peryl/react-compose"; import {PlainColor} from "./utils/PlainColor"; import {iColorPublicPropsType} from "./utils/color-picker.utils"; export function useColorPanelEditor( { getColor, onChange, props, }: { getColor: () => PlainColor, onChange: (color: PlainColor) => void, props: iColorPublicPropsType, } ) { const state = reactive({ editValue: (() => { const { r, g, b, a, hex } = getColor(); return { r, g, b, a, hex }; })() }); watch(() => getColor(), color => { const { r, g, b, a, hex } = color; state.editValue = { r, g, b, a, hex }; }); const handler = { onHexChange: (e: any) => { let hex = e.target.value; state.editValue.hex = hex; if (hex.length < 6) { return; } if (hex.indexOf('#') === -1) { hex = '#' + hex; } let color: PlainColor; try { color = new PlainColor(hex); } catch (e) { color = new PlainColor('#000000'); } onChange(color); }, onRChange: (e: any) => { state.editValue.r = e.target.value || 0; if (e.target.value.charAt(e.target.value.length - 1) === '.') { return; } onChange(getColor().useRgba({ r: state.editValue.r })); }, onGChange: (e: any) => { state.editValue.g = e.target.value || 0; if (e.target.value.charAt(e.target.value.length - 1) === '.') { return; } onChange(getColor().useRgba({ g: state.editValue.g })); }, onBChange: (e: any) => { state.editValue.b = e.target.value || 0; if (e.target.value.charAt(e.target.value.length - 1) === '.') { return; } onChange(getColor().useRgba({ b: state.editValue.b })); }, onAlphaChange: (e: any) => { let val = e.target.value; state.editValue.a = val; if (!val.trim()) { return; } if (val.charAt(val.length - 1) === '.') { return; } onChange(getColor().useRgba({ a: val })); }, onInputFocus: (e: any) => { const el = e.target as HTMLInputElement; const length = el.value.length; el.setSelectionRange(0, length, 'backward'); }, }; const renderContent = computed(() => { const { r, g, b, a, hex } = state.editValue; return () => (