186 lines
5.1 KiB
Markdown
186 lines
5.1 KiB
Markdown
# Editor Fixes Summary
|
|
|
|
## Masalah yang Diperbaiki
|
|
|
|
### 1. **Data dari setValue Tidak Tampil**
|
|
- **Masalah**: Ketika menggunakan `setValue` dari 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
|
|
```javascript
|
|
const [isEditorReady, setIsEditorReady] = useState(false);
|
|
const [currentContent, setCurrentContent] = useState(props.initialData || "");
|
|
```
|
|
|
|
#### Improved useEffect Structure
|
|
1. **Editor Initialization**: Handle editor ready state
|
|
2. **Content Updates**: Watch for initialData changes
|
|
3. **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
|
|
```javascript
|
|
// 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
|
|
```javascript
|
|
// 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
|
|
1. **Set Value**: Test basic setValue functionality
|
|
2. **Set Empty**: Test empty content handling
|
|
3. **Set HTML**: Test rich HTML content
|
|
4. **Real-time Typing**: Test onChange functionality
|
|
5. **Form Submission**: Test complete form workflow
|
|
|
|
## Cara Penggunaan
|
|
|
|
### Untuk CustomEditor
|
|
```javascript
|
|
<Controller
|
|
control={control}
|
|
name="description"
|
|
render={({ field }) => (
|
|
<CustomEditor onChange={field.onChange} initialData={field.value} />
|
|
)}
|
|
/>
|
|
```
|
|
|
|
### Untuk FormEditor
|
|
```javascript
|
|
<Controller
|
|
control={control}
|
|
name="description"
|
|
render={({ field }) => (
|
|
<FormEditor onChange={field.onChange} initialData={field.value} />
|
|
)}
|
|
/>
|
|
```
|
|
|
|
### SetValue Usage
|
|
```javascript
|
|
// 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
|
|
1. Check if editor is ready (`isEditorReady`)
|
|
2. Verify setValue timing
|
|
3. Check initialData prop value
|
|
4. Use EditorTest untuk debugging
|
|
|
|
### Cursor Masih Melompat
|
|
1. Ensure no excessive event prevention
|
|
2. Check for conflicting event handlers
|
|
3. Verify TinyMCE configuration
|
|
4. Test with simplified content
|
|
|
|
### Performance Issues
|
|
1. Avoid unnecessary re-renders
|
|
2. Use useCallback for event handlers
|
|
3. Optimize useEffect dependencies
|
|
4. Monitor component lifecycle
|
|
|
|
## Migration Guide
|
|
|
|
### Dari CustomEditor Lama
|
|
1. Update import path
|
|
2. Verify prop names (onChange, initialData)
|
|
3. Test setValue functionality
|
|
4. Monitor for any breaking changes
|
|
|
|
### Ke FormEditor
|
|
1. Replace CustomEditor with FormEditor
|
|
2. Update any custom configurations
|
|
3. Test all form scenarios
|
|
4. Verify data persistence
|
|
|
|
## Future Improvements
|
|
|
|
1. **Auto-save functionality**
|
|
2. **Better error handling**
|
|
3. **Performance optimizations**
|
|
4. **Enhanced mobile support**
|
|
5. **Accessibility improvements** |