-
Notifications
You must be signed in to change notification settings - Fork 928
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 in nextjs and typescript custom image handler issue? #849
Comments
|
The ref type is not defined in quill 's props. |
Had to manually add the ImageUploader files and modules locally into my project folder |
Ugly workaround with nextjs but worked for me: export default function Editor({ control, formValue }: EditorProps) {
const [value, setValue] = useState<string>("");
function imageHandler() {
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.click();
input.onchange = async () => {
if (!input.files) return
const cursorPosition = this.quill.selection.cursor.selection.lastRange.index;
// upload your file here
const link = 'https://i.imgur.com/m8wOp65_d.webp?maxwidth=300&shape=thumb&fidelity=high';
this.quill.editor.insertEmbed(cursorPosition, 'image', link);
};
};
const modules = useMemo(
() => ({
toolbar: {
container: TOOLBAR_OPTIONS,
handlers: {
image: imageHandler,
},
},
}),
[],
);
return (
<Controller
control={control}
name={formValue}
defaultValue={null}
render={(
{
field: { onChange, },
fieldState: { error },
}
) => (
<div>
<ReactQuill
className={error ? "!border-error" : ''}
theme="snow"
placeholder="Start writing..."
modules={modules}
value={value}
onChange={(content: string) => {
setValue(content);
onChange(content)
}}
/>
{error && <p className="text-error">{error.message}</p>}
</div>
)}
/>
);
} |
I based my solution on @eungwang1 opened PR. Here it is: const Editor = () => {
const [value, setValue] = useState("<p style={{color:'#bdbdbd'}}>Start writing...</p>");
const quillRef: React.LegacyRef<ReactQuill> = useRef(null);
function handler() {
console.log(value);
}
const imageHandler = useCallback(() => {
// Create an input element of type 'file'
const input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/*");
input.click();
// When a file is selected
input.onchange = () => {
if (!input.files) return;
const file = input.files[0];
const reader = new FileReader();
// Read the selected file as a data URL
reader.onload = () => {
const imageUrl = reader.result;
if (!quillRef?.current) return;
const quillEditor = quillRef.current.getEditor();
// Get the current selection range and insert the image at that index
const range = quillEditor.getSelection(true);
quillEditor.insertEmbed(range.index, "image", imageUrl, "user");
};
reader.readAsDataURL(file);
};
}, []);
const modules = useMemo(
() => ({
toolbar: {
container: [
[{ header: [2, 3, 4, false] }],
["bold", "italic", "underline", "blockquote"],
[{ color: [] }],
[
{ list: "ordered" },
{ list: "bullet" },
{ indent: "-1" },
{ indent: "+1" },
],
["link", "image"],
["clean"],
],
handlers: {
image: imageHandler,
},
},
clipboard: {
matchVisual: true,
},
}),
[imageHandler]
);
const formats = [
"header",
"bold",
"italic",
"underline",
"strike",
"blockquote",
"list",
"bullet",
"indent",
"link",
"image",
"color",
"clean",
];
return (
<Flex>
<QuillEditor
ref={quillRef}
className={styles.editor}
theme="bubble"
value={value}
defaultValue={"<p>Start writing...</p>"}
formats={formats}
modules={modules}
onChange={(value, _, __, editor) => console.log(value, editor.getText())}
/>
</Flex>
);
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am try to use
react-quill
in my typescript nextjs project. Here I am finding typing and ref issue that I can't solve. Please help me. Here is code example-I am try to use react-quill in my typescript nextjs project. Here I am finding typing and ref issue that I can't solve. Please help me.
Here is code example-
import React, { useState, useRef } from 'react';
import dynamic from 'next/dynamic';
import { Container } from "@mui/material";
const ReactQuill = dynamic(import('react-quill'), {
ssr: false,
loading: () =>
Loading ...
,})
import 'react-quill/dist/quill.snow.css';
const Editor = () => {
const [value, setValue] = useState('');
const quillRef = useRef();
};
export default Editor;
enter image description here
Here is CodeSandBox-
https://codesandbox.io/s/still-hill-xkb1pj
Can any one give me a proper typescript solutions.
The text was updated successfully, but these errors were encountered: