npm install react-quill
React-Quill은 스타일링을 위해 Quill의 CSS를 필요로 합니다. 이 CSS 파일은 패키지 설치 시 함께 제공되므로, 프로젝트에서 직접 임포트해야 합니다.
import 'react-quill/dist/quill.snow.css'; // Snow 테마 CSS
import ReactQuill, { Quill } from 'react-quill';
const [value, setValue] = useState('');
const modules = useMemo(() => ({
toolbar: {
container: [
['bold', 'italic', 'underline', 'strike'], // toggled buttons
['blockquote', 'code-block'], // blocks
[{ 'header': 1 }, { 'header': 2 }], // custom button values
[{ 'list': 'ordered'}, { 'list': 'bullet' }], // lists
[{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript
[{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent
[{ 'direction': 'rtl' }], // text direction
[{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
[{ 'color': [] }, { 'background': [] }], // dropdown with defaults
[{ 'font': [] }],
[{ 'align': [] }],
['image', 'video', 'clean'] // remove formatting button
],
handlers: { image: imageHandler }
},
imageDrop: true,
imageResize: {}
}), []);
const imageHandler = () => {
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.click();
input.addEventListener('change', async () => {
const file = input.files[0];
const formData = new FormData();
formData.append('img', file);
try {
let IMG_URL = await imageUpload(file);
const editor = quillRef.current.getEditor();
const range = editor.getSelection();
editor.insertEmbed(range.index, 'image', `${process.env.REACT_APP_SERVER_URL}${IMG_URL}`);
} catch (error) {
console.error('Image upload failed:', error);
}
});
};