-
Notifications
You must be signed in to change notification settings - Fork 923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Quill Custom image handler #987
Comments
have you declared the quillRef..? this code below work fine with me. import { useMemo, useRef } from "react";
. . .
const quillRef = useRef(null);
const handleImageInserted = async (file) => {
try {
const compressedImage = await compressImage(file); // compress the image
const imageUrl = await handleFsImageUpload(path, compressedImage); // upload to server
const quill = quillRef.current.getEditor();
const range = quill.getSelection();
quill.insertEmbed(range.index, "image", imageUrl); // add an img tag to quill
} catch (error) {
console.error("Error handling image insertion:", error);
}
};
const openImageFileDialog = () => {
const input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/*");
input.onchange = (event) => {
const file = event.target.files[0];
if (file) {
handleImageInserted(file);
}
};
input.click();
};
. . . |
Hey @tjiptostevens, Error - Error handling image insertion: TypeError: quillRef.current.getEditor is not a function at handleImageInserted Below is the dependencies - |
hhmm... how about this code. this work for me import { useMemo, useRef, useState } from "react";
import dynamic from "next/dynamic";
import "react-quill/dist/quill.snow.css";
const ReactQuill = dynamic(() => import("react-quill"), { ssr: false });
const TextEditor = ({ path = "/" }) => {
const [body, setBody] = useState("");
const quillRef = useRef(null);
const handleImageInserted = async (file) => {
try {
const imageUrl = await handleFsImageUpload(path, file); // image upload function
const quill = quillRef.current.getEditor();
const range = quill.getSelection();
quill.insertEmbed(range.index, "image", imageUrl);
} catch (error) {
console.error("Error handling image insertion:", error);
}
};
const openImageFileDialog = () => {
const input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/*");
input.onchange = (event) => {
const file = event.target.files[0];
if (file) {
handleImageInserted(file);
}
};
input.click();
};
const handleChange = (value) => {
setBody(value);
};
const quillModules = useMemo(
() => ({
toolbar: {
container: [
["bold", "italic", "underline", "strike"],
[{ header: "1" }, { header: "2" }],
[{ list: "ordered" }, { list: "bullet" }],
["link", "image", "youtube"],
],
handlers: {
image: openImageFileDialog,
},
},
}),
[]
);
return (
<div className="form-group __texteditor">
<label htmlFor="category">
Content <b>*</b>
</label>
<ReactQuill
ref={quillRef}
value={body}
onChange={handleChange}
theme="snow"
modules={quillModules}
/>
</div>
);
};
export default TextEditor; |
I am working with react-quill and my image handler is not working below is my code --
The error I'm getting is
TypeError: quillRef.current.getEditor is not a function
It is giving me an error of getEditor
Error - TypeError: quillRef.current.getEditor is not a function
Any help would be much appreciated!
The text was updated successfully, but these errors were encountered: