import React, { useCallback, useEffect } from 'react'; import { View, SafeAreaView, KeyboardAvoidingView, Platform, ScrollView } from 'react-native'; import { Stack, useLocalSearchParams } from 'expo-router'; import { useTheme } from '~/utils/theme/theme'; import { useDocumentEditor } from '~/hooks/useDocumentEditor'; import { DocumentContent } from './DocumentContent'; import { DocumentToolbar, KeyboardShortcutsInfo } from './DocumentToolbar'; import { DocumentTagsEditor } from './DocumentTagsEditor'; import { DocumentHeader } from './DocumentHeader'; import { VariantCreator } from '~/components/variants/VariantCreator'; import { BottomLLMToolbar } from '~/components/ai/BottomLLMToolbar'; import { Text } from '~/components/ui/Text'; import { Skeleton } from '~/components/ui/Skeleton'; import { EDITOR_CONFIG } from '~/config/editorConfig'; export interface DocumentEditorProps { spaceId: string; documentId: string; } /** * Optimierter Dokumenten-Editor mit separaten Komponenten und Custom Hooks * Ersetzt die ursprüngliche 1.322-Zeilen-Komponente */ export const DocumentEditor: React.FC = ({ spaceId, documentId }) => { const { isDark } = useTheme(); const params = useLocalSearchParams(); const initialMode = (params.mode as 'edit' | 'preview') || 'edit'; const { state, dispatch, saveDocument, toggleMode, updateContent, updateTitle, updateTags, autoSave, navigateToNextDocument, navigateToSpace, isNewDocument, canSave, } = useDocumentEditor({ spaceId, documentId, initialMode, }); // Keyboard Shortcuts (nur für Web) useEffect(() => { if (Platform.OS !== 'web') return; const handleKeyPress = (e: KeyboardEvent) => { if (e.ctrlKey || e.metaKey) { switch (e.key) { case 's': e.preventDefault(); if (canSave) { saveDocument(); } break; case 'p': e.preventDefault(); toggleMode(); break; case 'k': e.preventDefault(); // Focus auf Content-Eingabe setzen dispatch({ type: 'SET_MODE', payload: 'edit' }); break; case 'n': e.preventDefault(); navigateToSpace(); break; } } }; document.addEventListener('keydown', handleKeyPress); return () => document.removeEventListener('keydown', handleKeyPress); }, [canSave, saveDocument, toggleMode, dispatch, navigateToSpace]); // Handlers const handleToggleMode = useCallback(() => { toggleMode(); }, [toggleMode]); const handleSave = useCallback(() => { saveDocument(); }, [saveDocument]); const handleShowTags = useCallback(() => { dispatch({ type: 'SET_SHOW_TAGS_EDITOR', payload: !state.showTagsEditor }); }, [dispatch, state.showTagsEditor]); const handleShowVariantCreator = useCallback(() => { dispatch({ type: 'SET_SHOW_VARIANT_CREATOR', payload: !state.showVariantCreator }); }, [dispatch, state.showVariantCreator]); const handleTagsUpdate = useCallback( (tags: string[]) => { updateTags(tags); }, [updateTags] ); const handleContentChange = useCallback( (content: string) => { updateContent(content); }, [updateContent] ); const handleTitleChange = useCallback( (title: string) => { updateTitle(title); }, [updateTitle] ); const handleGenerateText = useCallback( (text: string) => { // Füge generierten Text am Ende des aktuellen Inhalts hinzu const newContent = state.content + '\\n\\n---\\n\\n' + text; updateContent(newContent); }, [state.content, updateContent] ); const handleCreateVariant = useCallback( (variant: any) => { // Hier würde die Varianten-Erstellung implementiert console.log('Create variant:', variant); dispatch({ type: 'SET_SHOW_VARIANT_CREATOR', payload: false }); }, [dispatch] ); // Loading Screen if (state.loading) { return ( ); } // Error Screen if (state.error) { return ( {state.error} Bitte versuche es später erneut oder kontaktiere den Support. ); } return ( {/* Keyboard Shortcuts Info (nur Web) */} {/* Document Header */} {/* Tags Editor (wenn sichtbar) */} {state.showTagsEditor && ( )} {/* Main Content Area */} {/* Bottom Toolbar */} {/* AI Toolbar (nur im Edit-Modus) */} {state.mode === 'edit' && ( { // Handle the generated text based on mode if (mode === 'replace') { updateContent(text); } else { updateContent(state.content + text); } }} isGenerating={state.isGeneratingText} setIsGenerating={(isGenerating) => { dispatch({ type: 'SET_IS_GENERATING_TEXT', payload: isGenerating }); }} documentId={documentId} /> )} {/* Variant Creator Modal */} {state.showVariantCreator && ( dispatch({ type: 'SET_SHOW_VARIANT_CREATOR', payload: false })} documentContent={state.content} documentTitle={state.title} documentId={documentId} spaceId={spaceId} onVariantCreated={(newDocumentId) => { console.log('Variant created:', newDocumentId); dispatch({ type: 'SET_SHOW_VARIANT_CREATOR', payload: false }); }} /> )} ); }; export default DocumentEditor;