import { Maximize2 } from 'lucide-react'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useI18n } from '../../application/i18n/I18nProvider'; import { STORAGE_KEY_SNIPPET_SCRIPT_EDITOR_HEIGHT } from '@/infrastructure/config/storageKeys.ts'; import { localStorageAdapter } from '@/infrastructure/persistence/localStorageAdapter.ts'; import { cn } from '@/lib/utils.ts'; import { Button } from '../ui/button'; import { CodeTextarea } from '../ui/code-textarea'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '../ui/dialog'; import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip'; const DEFAULT_HEIGHT = 120; const MIN_HEIGHT = 80; const MAX_HEIGHT = 520; function clampHeight(height: number): number { return Math.max(MIN_HEIGHT, Math.min(MAX_HEIGHT, height)); } function readStoredHeight(): number { const stored = localStorageAdapter.readNumber(STORAGE_KEY_SNIPPET_SCRIPT_EDITOR_HEIGHT); if (stored === null) return DEFAULT_HEIGHT; return clampHeight(stored); } const editorFillClass = '[&>div]:h-full [&_textarea]:h-full [&_textarea]:min-h-0 [&_textarea]:overflow-auto'; export interface SnippetScriptEditorProps { value: string; onChange: (value: string) => void; placeholder?: string; id?: string; /** Shown on the same row as the expand button (e.g. "Script *"). */ label?: string; } export const SnippetScriptEditor: React.FC = ({ value, onChange, placeholder, id, label, }) => { const { t } = useI18n(); const [height, setHeight] = useState(readStoredHeight); const [modalOpen, setModalOpen] = useState(false); const dragRef = useRef<{ startY: number; startHeight: number } | null>(null); const heightRef = useRef(height); heightRef.current = height; const handleResizeStart = useCallback((e: React.MouseEvent) => { e.preventDefault(); dragRef.current = { startY: e.clientY, startHeight: heightRef.current }; document.body.style.cursor = 'ns-resize'; document.body.style.userSelect = 'none'; }, []); useEffect(() => { const onMove = (e: MouseEvent) => { if (!dragRef.current) return; const delta = e.clientY - dragRef.current.startY; setHeight(clampHeight(dragRef.current.startHeight + delta)); }; const onUp = () => { if (dragRef.current) { localStorageAdapter.writeNumber( STORAGE_KEY_SNIPPET_SCRIPT_EDITOR_HEIGHT, heightRef.current, ); } dragRef.current = null; document.body.style.cursor = ''; document.body.style.userSelect = ''; }; window.addEventListener('mousemove', onMove); window.addEventListener('mouseup', onUp); return () => { window.removeEventListener('mousemove', onMove); window.removeEventListener('mouseup', onUp); document.body.style.cursor = ''; document.body.style.userSelect = ''; }; }, []); return ( <>
{label ? ( id ? ( ) : (

{label}

) ) : ( )} {t('snippets.scriptEditor.expand')}
onChange(e.target.value)} className="min-h-0" wrapperClassName="h-full" />
{t('snippets.scriptEditor.modalTitle')}
onChange(e.target.value)} className="min-h-0" wrapperClassName="h-full" autoFocus />
); };