0

有人可以帮我找出代码中的问题。我创建了一个自定义图像上传选项,但由于某种原因,当调用 quillImageCallback 函数时,变量“quillReact”变为空。我正在使用反应钩子。使用 API 时图像已正确上传,并且后端也返回了正确的响应。

let quillReact: ReactQuill | null = null;

const updateIssueInfo = (value: string, delta: any, source: any, editor: any) => {
        setIssueManagementInEdit({
            ...issueManagementInEdit,
            description: value
        });
    };

const quillImageCallback = () => {
        console.log(issueManagement);
        const input = document.createElement("input");
        input.setAttribute("type","file");
        input.setAttribute("accept", "image/*");
        input.click();
        input.onchange = async () => {
            const file: File | null = input.files ? input.files[0] : null;
            if(file){
                uploadImage(file).then(async (fileName: any) => {
                    const newFileName:string = await fileName.text();
                    console.log(quillReact);
                    let quill: any | null = quillReact?.getEditor();
                    console.log(quill);
                    const range : any | null = quill?.getSelection(true);
                    quill?.insertEmbed(range.index, 'image', `http://localhost:8080/uploads/${newFileName}`);
                });
            }
        }
    };

    const module =  React.useMemo(() => { return {
        toolbar: {
            container: [
                ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                ['blockquote', 'code-block'],
            
                [{ 'header': 1 }, { 'header': 2 }],               // custom button values
                [{ 'list': 'ordered'}, { 'list': 'bullet' }],
                [{ '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 from theme
                [{ 'font': [] }],
                [{ 'align': [] }],
            
                ['clean', 'image']                                    // remove formatting button     
            ],
            handlers: {
                image: quillImageCallback
            }  
        },
        clipboard: {
            // toggle to add extra line breaks when pasting HTML:
            matchVisual: false,
        }   
    }},[]);

<ReactQuill
      value={issueManagementInEdit.description ? issueManagementInEdit.description : ""}
      onChange={updateIssueInfo}
      modules={module}
      ref={(el: ReactQuill) => {
            quillReact = el;
      } }
      style={{height: "250px"}}
      id="description"
      key="description"
/>   

谢谢你。

4

1 回答 1

1

我建议你试试useRef

const quillRef = React.useRef(null);
<ReactQuill ... ref={quillRef} />

然后在您的回调中访问编辑器:

const quill = quillRef.current.getEditor();
于 2020-09-13T20:04:36.610 回答