添加自定义工具栏按钮后,反应羽毛笔样式丢失

时间:2018-10-09 14:12:56

标签: javascript reactjs

我需要在react quill HTML编辑器内添加一个自定义工具栏按钮。

我已经按照教程https://github.com/zenoamaro/react-quill#custom-toolbar进行操作,但是无法使其正常工作。代码如下。

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import ReactQuill from 'react-quill'; // ES6

import './style.scss';

class DocumentForm extends Component {

  constructor(props) {
    super(props);

    this.state = {
      file: null,
      documentDetails: {},
      text: ''
    };
    this.handleEditorChange = this.handleEditorChange.bind(this);
    this.modules =  {
      toolbar: [
        [{ 'header': [1, 2, false] }],
        ['bold', 'italic', 'underline','strike', 'blockquote'],
        [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}],
        ['link', 'image'],
        ['clean']
      ],
    }

    this.formats = [
      'header',
      'bold', 'italic', 'underline', 'strike', 'blockquote',
      'list', 'bullet', 'indent',
      'link', 'image'
    ]
  }

  handleEditorChange(value) {
    this.setDocumentDetails('html_content', value)
  }

  render() {
    let selectedDocument = this.state.documentDetails;
    return (
             <div>
                 <div>
                      <div className="form-group">
                        <label>File Content</label>
                        <Editor placeholder={'Write something or insert a star ★'}/>,
                      </div>
                  </div>
             </div>
    );
  }
}

export default  DocumentForm


/*
 * Custom "star" icon for the toolbar using an Octicon
 * https://octicons.github.io
 */
const CustomButton = () => <span className="octicon octicon-star" />

/*
 * Event handler to be attached using Quill toolbar module
 * http://quilljs.com/docs/modules/toolbar/
 */
function insertStar () {
  const cursorPosition = this.quill.getSelection().index
  this.quill.insertText(cursorPosition, "★")
  this.quill.setSelection(cursorPosition + 1)
}

/*
 * Custom toolbar component including insertStar button and dropdowns
 */
const CustomToolbar = () => (
  <div id="toolbar">
    <select className="ql-header" defaultValue={""} onChange={e => e.persist()}>
      <option value="1"></option>
      <option value="2"></option>
      <option selected></option>
    </select>
    <button className="ql-bold"></button>
    <button className="ql-italic"></button>
    <select className="ql-color">
      <option value="red"></option>
      <option value="green"></option>
      <option value="blue"></option>
      <option value="orange"></option>
      <option value="violet"></option>
      <option value="#d0d1d2"></option>
      <option selected></option>
    </select>
    <button className="ql-insertStar">
      <CustomButton />
    </button>
  </div>
)

/*
 * Editor component with custom toolbar and content containers
 */
class Editor extends React.Component {
  constructor (props) {
    super(props)
    this.state = { editorHtml: '' }
    this.handleChange = this.handleChange.bind(this)
  }

  handleChange (html) {
    this.setState({ editorHtml: html });
  }

  render() {
    return (
      <div className="text-editor">
        <CustomToolbar />
        <ReactQuill
          onChange={this.handleChange}
          placeholder={this.props.placeholder}
          modules={Editor.modules}
        />
      </div>
    )
  }
}

/*
 * Quill modules to attach to editor
 * See http://quilljs.com/docs/modules/ for complete options
 */
Editor.modules = {
  toolbar: {
    container: "#toolbar",
    handlers: {
      "insertStar": insertStar,
    }
  }
}

/*
 * Quill editor formats
 * See http://quilljs.com/docs/formats/
 */
Editor.formats = [
  'header', 'font', 'size',
  'bold', 'italic', 'underline', 'strike', 'blockquote',
  'list', 'bullet', 'indent',
  'link', 'image', 'color',
]

与本教程不同,有2个工具栏 显示了自定义工具栏,但是按钮没有样式。 此外,按钮处理程序(insertStar)无法正常工作。

该页面的屏幕截图如下。 enter image description here

关于如何解决此问题的任何想法

3 个答案:

答案 0 :(得分:0)

根据quill.js guide,您需要像下面这样导入CSS

@import "~quill/dist/quill.core.css"

答案 1 :(得分:0)

在Reactquill文档https://www.npmjs.com/package/react-quill#import-the-stylesheet中,样式表必须导入为:

require('react-quill/dist/quill.snow.css'); // CommonJS
import 'react-quill/dist/quill.snow.css'; // ES6

答案 2 :(得分:0)

导入“ react-quill / dist / quill.snow.css”