import React, { useState, useRef, useEffect, useCallback } from 'react'; import { createPortal } from 'react-dom'; import { useDraggablePanel } from '../hooks/useDraggablePanel'; import type { HLineStyle } from '../utils/indicatorRegistry'; import { parsePlotColor, formatPlotColor, plotColorCss, LINE_WIDTH_OPTIONS, LINE_STYLE_OPTIONS, LINE_STYLE_LABELS, } from '../utils/plotColorUtils'; import ColorPickerPanel from './ColorPickerPanel'; export interface PlotLineStyleValue { color: string; lineWidth?: number; lineStyle?: HLineStyle; } interface PlotLineStylePickerProps { value: PlotLineStyleValue; disabled?: boolean; /** 팝업 제목 (예: MA3) */ title?: string; /** false면 색상만 (히스토그램 등) */ showLineControls?: boolean; onChange: (patch: Partial) => void; } interface PlotLineStylePopupProps { title?: string; showLineControls: boolean; hex6: string; alpha: number; lineWidth: number; lineStyle: HLineStyle; onChange: (patch: Partial) => void; onClose: () => void; patchColor: (hex: string, alphaPct: number) => void; } const PlotLineStylePopup: React.FC = ({ title, showLineControls, hex6, alpha, lineWidth, lineStyle, onChange, onClose, patchColor, }) => { const { panelRef, dragging, onHeaderPointerDown, headerTouchStyle, panelStyle, headerCursor, } = useDraggablePanel({ centerOnMount: true }); return (
e.stopPropagation()} >
{title ? `${title} · ` : ''}색상 · 선 스타일
patchColor(h, alpha)} />
불투명성
patchColor(hex6, Number(e.target.value))} /> {alpha}%
{showLineControls && ( <>
두께
{LINE_WIDTH_OPTIONS.map(w => ( ))}
라인 스타일
{LINE_STYLE_OPTIONS.map(s => ( ))}
)}
); }; const LineStyleIcon: React.FC<{ style: HLineStyle }> = ({ style }) => ( ); const PlotLineStylePicker: React.FC = ({ value, disabled = false, title, showLineControls = true, onChange, }) => { const [open, setOpen] = useState(false); const swatchRef = useRef(null); const { hex6, alpha } = parsePlotColor(value.color); const lineWidth = value.lineWidth ?? 1; const lineStyle = value.lineStyle ?? 'solid'; const close = useCallback(() => setOpen(false), []); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') close(); }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, [open, close]); const patchColor = useCallback((hex: string, alphaPct: number) => { onChange({ color: formatPlotColor(hex, alphaPct) }); }, [onChange]); const popup = open ? ( ) : null; return (
{showLineControls && ( <> )} {typeof document !== 'undefined' && popup ? createPortal(popup, document.body) : null}
); }; export default PlotLineStylePicker;