羽毛笔编辑器未在React中呈现

时间:2020-09-12 14:10:31

标签: javascript reactjs typescript ionic-framework quill

我正在开发一个离子反应应用程序。我想要富文本编辑器,它将在Firestore上保存数据。我已经启动了如下编辑器,并将此组件添加到主页中。但是,尽管可以正常工作,但无法正确渲染。

您还可以提供/建议我更好的解决方案或其他RTF编辑器。我想要富文本编辑器(就像有问题的Stackoverflow文本编辑器一样),并且数据应该保存在firestore中。我已附上屏幕截图和代码。

Dashboard screenshot

QuillEditor.js

import React from 'react';

import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';

import Quill from 'quill/core';
import Toolbar from 'quill/modules/toolbar';
import Snow from 'quill/themes/snow'; //snow works, but need to import and register formats, and replace icons...

import Bold from 'quill/formats/bold';
import Italic from 'quill/formats/italic';
import Header from 'quill/formats/header';
import Underline from 'quill/formats/underline';
import Link from 'quill/formats/link';
import List, { ListItem } from 'quill/formats/list';

import Icons from 'quill/ui/icons'; 

export default class QuillEditor extends React.Component {

  componentDidMount() {

    Quill.register({
      'modules/toolbar': Toolbar,
      'themes/snow': Snow,
      'formats/bold': Bold,
      'formats/italic': Italic,
      'formats/header': Header,
      'formats/underline': Underline,
      'formats/link': Link,
      'formats/list': List,
      'formats/list/item': ListItem,
      'ui/icons': Icons
    });

    var icons = Quill.import('ui/icons');
    icons['bold'] = '<i class="fa fa-bold" aria-hidden="true"></i>';
    icons['italic'] = '<i class="fa fa-italic" aria-hidden="true"></i>';
    icons['underline'] = '<i class="fa fa-underline" aria-hidden="true"></i>';
    icons['link'] = '<i class="fa fa-link" aria-hidden="true"></i>';
    icons['clean'] = '<i class="fa fa-eraser" aria-hidden="true"></i>'; 

    var quill = new Quill('#editor', {
      theme: 'snow', //this needs to come after the above, which registers Snow...
      placeholder: "Write something awesome..."
    });
  } //componentDidMount

  render() {
    return (
      <div><meta charset="utf-8" />
          <div id="QuillEditor-container">
            {/* <!-- Create the editor container --> */}
            <div id="editor">
              <p>Hello World!</p>
              <p>Some initial <strong>bold</strong> text</p>
              <p></p>
            </div>
          </div>
      </div>
    )
  }
}

2 个答案:

答案 0 :(得分:1)

仅在reacjs中使用标准鹅毛笔js代码也会遇到相同的问题。

此外,您也可以尝试以下方法进行初始化:

  componentDidUpdate() {
    console.log(this.editor);
    if(this.editor==null){
      console.log("Here");
      this.editor = new Quill('#editor', {
        theme: 'snow'
    });
    }
}

但是我一直使编辑器为空。...

答案 1 :(得分:0)

这里是react quill的完整代码,上传图片到你自己的s3云存储,并在react quill上指定tab和空格问题:

import ReactQuill from "react-quill";
import "react-quill/dist/quill.snow.css";
import 'react-quill/dist/quill.bubble.css';
            
const quillContainerData = [
  ["bold", "italic", "underline", "strike", "blockquote"],
  [
    { list: "ordered" },
    { list: "bullet" },
    { indent: "-4" },
    { indent: "+4" }
  ],
  ["link", "image", "video"],
  ["clean"],
];
            
const quillFormats = [
  "header",
  "bold",
  "italic",
  "underline",
  "strike",
  "blockquote",
  "list",
  "bullet",
  "indent",
  "link",
  "image",
  "video",
];

handleChangeQuillStandart = (textQuillStandart) => {
  this.setState({ textQuillStandart });
};
    
// quill editor image upload functionality
imageHandler() {
    const input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');
    input.click();
    input.onchange = async () => {
        const file = input.files[0];
        let formData = new FormData();
        formData.append('uri', file);
        const range = this.quill.getSelection(true);
        this.quill.insertEmbed(range.index, 'image', `${window.location.origin}/images/loaders/placeholder.gif`);
        this.quill.setSelection(range.index + 1);
        //fetch api call for upload the image
        const data = await fetch(apiPath + '/api/upload'
        , {
            method: 'POST',
            headers: {
                Authorization: `Bearer ${localStorage.getItem('feathers-jwt')}`,
            },
            body: formData
        })
        const res = await data.json()
        this.quill.deleteText(range.index, 1);
        this.quill.insertEmbed(range.index, 'image', res.url);
    };
}   
     
    
<ReactQuill  theme="snow"
    className="mb-4"
    value={this.state.textQuillStandart}
    onChange={(v) => this.handleChangeQuillStandart(v)}
    modules={{
       toolbar: {
         container: quillContainerData,
         handlers: {
           image: this.imageHandler
         }
       }
    }}
    formats={quillFormats}
    preserveWhitespace={true} 
/>

查看页面这是代码

<div className="task-des" dangerouslySetInnerHTML={{
    __html: handbook.introduction
}}></div>

并使用此 css 解决空格和制表符问题

.task-des{
  white-space: break-spaces;
  tab-size: 4;
}