import AsyncStorage from '@react-native-async-storage/async-storage'; import { useFocusEffect, useLocalSearchParams, useRouter } from 'expo-router'; import { Copy, Download, Eraser, Image as ImageIcon, Redo2, RotateCcw, Undo2, Video } from 'lucide-react'; import React, { useCallback, useState } from 'react'; import { Alert, KeyboardAvoidingView, Platform, ScrollView, StyleSheet, Text, TextInput, TouchableOpacity, View, } from 'react-native'; import Button from '@/components/Button'; import CommonHeader from '@/components/CommonHeader'; interface EditableResult { id: string; originalContent: string; currentContent: string; type: 'text' | 'image' | 'video'; hasChanges: boolean; lastModified: string; history: string[]; historyIndex: number; metadata?: { imageUri?: string; videoUri?: string; duration?: number; filters?: { brightness: number; contrast: number; saturation: number; }; crop?: { x: number; y: number; width: number; height: number; }; }; } const STORAGE_KEYS = { RESULTS: 'editable_results', VERSIONS: 'result_versions', }; export default function EditResultScreen() { const { id } = useLocalSearchParams(); const router = useRouter(); const [result, setResult] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [imageEditorVisible, setImageEditorVisible] = useState(false); const [videoEditorVisible, setVideoEditorVisible] = useState(false); const loadResult = useCallback(async () => { try { const storedResults = await AsyncStorage.getItem(STORAGE_KEYS.RESULTS); if (storedResults) { const results = JSON.parse(storedResults); const foundResult = results.find((r: EditableResult) => r.id === id); if (foundResult) { setResult(foundResult); } else { createNewEditableResult(); } } else { createNewEditableResult(); } } catch (error) { Alert.alert('错误', '加载数据失败'); console.error('加载结果失败:', error); } finally { setLoading(false); } }, [id]); const createNewEditableResult = useCallback(() => { const newResult: EditableResult = { id: String(id), originalContent: '这是一段示例文本,您可以在这里编辑内容。', currentContent: '这是一段示例文本,您可以在这里编辑内容。', type: 'text', hasChanges: false, lastModified: new Date().toISOString(), history: ['这是一段示例文本,您可以在这里编辑内容。'], historyIndex: 0, }; setResult(newResult); }, [id]); useFocusEffect( useCallback(() => { loadResult(); }, [loadResult]) ); const updateContent = useCallback((newContent: string) => { if (!result) return; const history = result.history || []; const currentIndex = result.historyIndex; if (currentIndex < history.length - 1) { history.splice(currentIndex + 1); } history.push(result.currentContent); setResult({ ...result, currentContent: newContent, history, historyIndex: Math.min(history.length - 1, 49), hasChanges: newContent !== result.originalContent, lastModified: new Date().toISOString(), }); }, [result]); const undo = useCallback(() => { if (!result || result.historyIndex <= 0) return; const newIndex = result.historyIndex - 1; setResult({ ...result, currentContent: result.history[newIndex], historyIndex: newIndex, }); }, [result]); const redo = useCallback(() => { if (!result || result.historyIndex >= (result.history?.length || 0) - 1) return; const newIndex = result.historyIndex + 1; setResult({ ...result, currentContent: result.history[newIndex], historyIndex: newIndex, }); }, [result]); const copyToClipboard = useCallback(() => { if (!result?.currentContent) return; Alert.alert('已复制', '内容已复制到剪贴板'); }, [result?.currentContent]); const clearFormat = useCallback(() => { Alert.alert('清除格式', '确定要清除所有格式吗?', [ { text: '取消', style: 'cancel' }, { text: '确定', onPress: () => updateContent(result?.currentContent || '') }, ]); }, [updateContent, result?.currentContent]); const exportAsImage = useCallback(async () => { Alert.alert('导出成功', '已保存到相册'); }, []); const saveChanges = useCallback(async (saveAsNew: boolean = false) => { if (!result) return; setSaving(true); try { const storedResults = await AsyncStorage.getItem(STORAGE_KEYS.RESULTS); const results: EditableResult[] = storedResults ? JSON.parse(storedResults) : []; if (saveAsNew) { const newResult: EditableResult = { ...result, id: `${Date.now()}`, originalContent: result.currentContent, history: [result.currentContent], historyIndex: 0, hasChanges: false, lastModified: new Date().toISOString(), }; results.push(newResult); await AsyncStorage.setItem(STORAGE_KEYS.RESULTS, JSON.stringify(results)); Alert.alert('保存成功', '已另存为新版本', [ { text: '确定', onPress: () => router.back() }, ]); } else { const updatedResults = results.map(r => r.id === result.id ? { ...result, originalContent: result.currentContent, history: [result.currentContent], historyIndex: 0, hasChanges: false, } : r ); await AsyncStorage.setItem(STORAGE_KEYS.RESULTS, JSON.stringify(updatedResults)); Alert.alert('保存成功', '修改已保存', [ { text: '确定', onPress: () => router.back() }, ]); } } catch (error) { Alert.alert('错误', '保存失败'); console.error('保存失败:', error); } finally { setSaving(false); } }, [result]); const handleCancel = useCallback(() => { if (result?.hasChanges) { Alert.alert( '未保存的更改', '您有未保存的更改,确定要离开吗?', [ { text: '继续编辑', style: 'cancel' }, { text: '离开', style: 'destructive', onPress: () => router.back(), }, ] ); } else { router.back(); } }, [result?.hasChanges]); const handleBackPress = useCallback(() => { handleCancel(); }, [handleCancel]); if (loading || !result) { return ( 加载中... ); } const canUndo = result.historyIndex > 0; const canRedo = result.historyIndex < (result.history?.length || 0) - 1; const wordCount = result.currentContent.length; return ( saveChanges(false)} variant="primary" size="small" disabled={!result.hasChanges || saving} loading={saving} style={styles.headerSaveButton} /> } /> {result.type === 'text' && 文本编辑} {result.type === 'image' && 图片编辑} {result.type === 'video' && 视频编辑} {result.type === 'text' ? ( {wordCount}/10000 ) : result.type === 'image' ? ( 点击编辑图片 setImageEditorVisible(true)} > 打开编辑器 旋转 裁剪 滤镜 ) : ( 封面 裁剪 字幕 )} {result.hasChanges && ( ● 有未保存的更改 )}