fix(ui): constrain editor tools and add screen color sampling
This commit is contained in:
@@ -3,6 +3,10 @@ import { VerticalRangeBand, type RangeBandControlProps } from "./VerticalRangeBa
|
||||
export type { RangeBandControlProps } from "./VerticalRangeBand.js";
|
||||
import { cn } from "./cn.js";
|
||||
import { Dropdown } from "./Dropdown.js";
|
||||
import { Button } from "./Button.js";
|
||||
import { Icon } from "./Icon.js";
|
||||
import { Description } from "./Description.js";
|
||||
import { screenColorPicker } from "./screenColorPicker.js";
|
||||
|
||||
export interface RangeControlProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "value" | "onChange"> {
|
||||
label: string;
|
||||
@@ -277,7 +281,27 @@ function hsvToHex({ h, s, v }: Hsv): string {
|
||||
|
||||
export function ColorField({ value, onChange, label = "Цвет", className, disabled = false, onBlur, onKeyDown, ...props }: ColorFieldProps) {
|
||||
const [draft, setDraft] = useState(value);
|
||||
const [paletteOpen, setPaletteOpen] = useState(false), [picking, setPicking] = useState(false), [pickError, setPickError] = useState(false);
|
||||
const nativePicker = useRef<HTMLInputElement>(null), sampling = useRef<AbortController | null>(null);
|
||||
const screenPicker = screenColorPicker();
|
||||
useEffect(() => () => { sampling.current?.abort(); }, []);
|
||||
useEffect(() => { if (disabled || !paletteOpen) { sampling.current?.abort(); sampling.current = null; setPicking(false); } }, [disabled, paletteOpen]);
|
||||
useEffect(() => setDraft(value), [value]);
|
||||
const sampleScreen = async () => {
|
||||
if (disabled || sampling.current) return;
|
||||
setPickError(false);
|
||||
if (!screenPicker) { nativePicker.current?.click(); return; }
|
||||
const controller = new AbortController(); sampling.current = controller; setPicking(true);
|
||||
try {
|
||||
// open() executes synchronously in this trusted click before the first await.
|
||||
const result = await new screenPicker().open({ signal: controller.signal });
|
||||
if (!controller.signal.aborted && parseHexColor(result.sRGBHex)) { setDraft(result.sRGBHex.toLowerCase()); onChange(result.sRGBHex.toLowerCase()); }
|
||||
} catch (error) {
|
||||
if (!controller.signal.aborted && !(error instanceof Error && error.name === 'AbortError')) setPickError(true);
|
||||
} finally {
|
||||
if (!controller.signal.aborted) { setPicking(false); sampling.current = null; }
|
||||
}
|
||||
};
|
||||
const validColor = hexToHsv(value);
|
||||
const [hsv, setHsv] = useState<Hsv>(() => validColor ?? { h: 330, s: 82, v: 100 });
|
||||
const style = { "--nodedc-color-value": validColor ? hsvToHex(validColor) : hsvToHex(hsv) } as CSSProperties;
|
||||
@@ -302,6 +326,7 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
|
||||
<div className={cn("nodedc-color-field", className)} style={style}>
|
||||
<Dropdown
|
||||
className="nodedc-color-field__dropdown"
|
||||
onOpenChange={setPaletteOpen}
|
||||
placement="bottom-start"
|
||||
minWidth={296}
|
||||
width={296}
|
||||
@@ -354,6 +379,10 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
|
||||
<span>Оттенок</span>
|
||||
<input type="range" min={0} max={360} value={Math.round(hsv.h)} aria-label={`${label}: оттенок`} disabled={disabled} onChange={(event) => commit({ ...hsv, h: Number(event.target.value) })} />
|
||||
</label>
|
||||
<Button width="full" icon={<Icon name="pipette"/>} disabled={disabled} loading={picking} onClick={() => void sampleScreen()} title={screenPicker ? 'Выбрать цвет в любой точке экрана; Escape — отменить' : 'В этом браузере выбор цвета с экрана доступен через системную палитру, если она поддерживает пипетку'}>
|
||||
{screenPicker ? 'Цвет с экрана' : 'Системная палитра'}
|
||||
</Button>
|
||||
{pickError && <Description role="status">Не удалось включить пипетку. Повторите попытку.</Description>}
|
||||
<div className="nodedc-color-palette__presets" aria-label="Быстрые цвета">
|
||||
{palettePresets.map((preset) => (
|
||||
<button
|
||||
@@ -371,6 +400,7 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
|
||||
</div>
|
||||
</div>
|
||||
</Dropdown>
|
||||
<input ref={nativePicker} type="color" className="nodedc-color-field__native-picker" tabIndex={-1} aria-hidden="true" disabled={disabled} value={validColor ? hsvToHex(validColor) : '#000000'} onChange={event => { setDraft(event.target.value); onChange(event.target.value); }}/>
|
||||
<input
|
||||
className="nodedc-color-field__text"
|
||||
{...props}
|
||||
|
||||
Reference in New Issue
Block a user