5.1 KiB
5.1 KiB
Editor Fixes Summary
Masalah yang Diperbaiki
1. Data dari setValue Tidak Tampil
- Masalah: Ketika menggunakan
setValuedari react-hook-form, data tidak muncul di editor - Penyebab: Timing issue antara editor initialization dan data setting
- Solusi: Implementasi state management yang lebih baik dan multiple useEffect untuk handle berbagai timing scenarios
2. Cursor Jumping
- Masalah: Cursor melompat saat mengetik
- Penyebab: Event handling yang tidak tepat dan content manipulation yang berlebihan
- Solusi: Simplifikasi event handling dan removal of problematic event prevention
Perbaikan yang Dilakukan
CustomEditor (components/editor/custom-editor.js)
State Management
const [isEditorReady, setIsEditorReady] = useState(false);
const [currentContent, setCurrentContent] = useState(props.initialData || "");
Improved useEffect Structure
- Editor Initialization: Handle editor ready state
- Content Updates: Watch for initialData changes
- Initial Data Loading: Handle data when editor becomes ready
Key Changes
- Simplified event handling (removed excessive event prevention)
- Better state synchronization between props and internal state
- Multiple timing checks to ensure data is set correctly
FormEditor (components/editor/form-editor.js)
Enhanced Data Handling
// Watch for initialData changes (from setValue)
useEffect(() => {
if (initialData !== editorContent) {
setEditorContent(initialData || "");
// Update editor content if ready
if (editorRef.current && isEditorReady) {
editorRef.current.setContent(initialData || "");
}
}
}, [initialData, editorContent, isEditorReady]);
Features
- Robust initial data handling
- Proper state synchronization
- Better timing management
Image Update Form (components/form/content/image-update-form.tsx)
Improved setValue Timing
// Set form values immediately and then again after a delay to ensure editor is ready
setValue("title", details.title);
setValue("description", details.htmlDescription);
setValue("creatorName", details.creatorName);
// Set again after delay to ensure editor has loaded
setTimeout(() => {
setValue("title", details.title);
setValue("description", details.htmlDescription);
setValue("creatorName", details.creatorName);
}, 500);
Key Changes
- Immediate setValue call for instant feedback
- Delayed setValue call to ensure editor is ready
- Better dependency management in useEffect
Testing
Editor Test Component (components/editor/editor-test.tsx)
- Test both CustomEditor and FormEditor
- Test setValue functionality with different content types
- Real-time form value monitoring
- Multiple test scenarios (empty, HTML, timestamp)
Test Scenarios
- Set Value: Test basic setValue functionality
- Set Empty: Test empty content handling
- Set HTML: Test rich HTML content
- Real-time Typing: Test onChange functionality
- Form Submission: Test complete form workflow
Cara Penggunaan
Untuk CustomEditor
<Controller
control={control}
name="description"
render={({ field }) => (
<CustomEditor onChange={field.onChange} initialData={field.value} />
)}
/>
Untuk FormEditor
<Controller
control={control}
name="description"
render={({ field }) => (
<FormEditor onChange={field.onChange} initialData={field.value} />
)}
/>
SetValue Usage
// Immediate setValue
setValue("description", "New content");
// With delay for editor readiness
setTimeout(() => {
setValue("description", "New content");
}, 500);
Rekomendasi
1. Gunakan FormEditor untuk Form yang Kompleks
- FormEditor lebih robust untuk handling setValue
- Better state management
- More reliable initial data loading
2. Timing Considerations
- Always set form values immediately
- Use setTimeout for additional setValue calls if needed
- Monitor editor ready state
3. Testing
- Use EditorTest component untuk testing
- Test berbagai scenarios sebelum production
- Monitor console untuk debugging
Troubleshooting
Data Tidak Tampil
- Check if editor is ready (
isEditorReady) - Verify setValue timing
- Check initialData prop value
- Use EditorTest untuk debugging
Cursor Masih Melompat
- Ensure no excessive event prevention
- Check for conflicting event handlers
- Verify TinyMCE configuration
- Test with simplified content
Performance Issues
- Avoid unnecessary re-renders
- Use useCallback for event handlers
- Optimize useEffect dependencies
- Monitor component lifecycle
Migration Guide
Dari CustomEditor Lama
- Update import path
- Verify prop names (onChange, initialData)
- Test setValue functionality
- Monitor for any breaking changes
Ke FormEditor
- Replace CustomEditor with FormEditor
- Update any custom configurations
- Test all form scenarios
- Verify data persistence
Future Improvements
- Auto-save functionality
- Better error handling
- Performance optimizations
- Enhanced mobile support
- Accessibility improvements