x-render / widgets /RichText /src /index.js
AbdulElahGwaith's picture
Upload folder using huggingface_hub
c9d0005 verified
Raw
History Blame Contribute Delete
1.23 kB
import BraftEditor from 'braft-editor';
import 'braft-editor/dist/index.css';
import React, { useEffect, useState } from 'react';
const { createEditorState } = BraftEditor;
const RichTextEditor = ({ name, onChange, value, ...rest }) => {
const [editor, set] = useState(null);
useEffect(() => {
if (value !== undefined) {
set(createEditorState(value));
}
}, [value]);
const handleChange = editor => {
set(editor);
// const htmlContent = editor.toHTML();
// onChange(name, htmlContent);
};
const onSave = () => {
// Pressing ctrl + s when the editor has focus will execute this method
// Before the editor content is submitted to the server, you can directly call editorState.toHTML () to get the HTML content
const htmlContent = editor.toHTML();
if (name) {
onChange(name, htmlContent);
} else {
onChange(htmlContent);
}
};
return (
<div style={{ border: '1px solid rgba(0,0,0,0.2)' }}>
<BraftEditor
contentStyle={{ minHeight: 100, height: 'auto', maxHeight: 500 }}
value={editor}
onChange={handleChange}
onSave={onSave}
onBlur={onSave}
/>
</div>
);
};
export default RichTextEditor;