How to create undo/redo buttons in Quill JS (react-quill)?

浪子不回头ぞ 提交于 2020-01-15 04:41:13

问题


QuillJS doesn't come with default undo/redo buttons. I'm trying to add them to the toolbar. Quill has a folder with the undo/redo icons saved. In the node_modules, there's also undo() and redo() functions. I'm kind of new to coding and don't know how to access these things and make them work. I'm using React. Here's my code so far:

import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
import 'react-quill/dist/quill.bubble.css';

class QuillTextEditor extends Component {
    constructor(props) {
        super(props);

        this.modules = {
            toolbar: [
              [{ 'header': [false, 1, 2, 3] }],
              [{ 'align': [] }],
              ['bold', 'italic', 'underline',],
              [{'list': 'ordered'}, {'list': 'bullet'}],
              [{ 'indent': '-1'}, { 'indent': '+1' }],
              [{ 'script': 'super' }, 'strike'],
              [{ 'color': [] }, { 'background': [] }], 
              ['link', 'image'],
            ]
        };

        this.formats = [
            'header',
            'align',
            'bold', 'italic', 'underline', 
            'list', 'bullet',
            'indent', 'indent',
            'script', 'strike',
            'color', 'background',
            'link', 'image',
        ];
    }

    render() {
        return (
          <div>
            <ReactQuill 
                theme="snow"  
                modules={this.modules}
                formats={this.formats} 
                value={''}/>
            </div>
          </div>
        );
    }

}

export default QuillTextEditor;

Does anyone know exactly what code I would need to write and where in order to add undo/redo icons to the toolbar that are connected to the undo/redo functions build into Quill? I've been trying for days and can't figure it out.


回答1:


Does anyone know exactly what code I would need to write and where in order to add undo/redo icons to the toolbar that are connected to the undo/redo functions build into Quill?

Hi. Unfortunately I still don't know how to connect buttons to native Quill functions. But you can do something else that can give you the desired result.

Take a look at this. Search for items 020, 021 and 026.

You can add a new button, and set it to call the following code:

quill.history.undo();

History Module

If you have additional questions, please leave a comment. As soon as I can, I will answer you.




回答2:


@Loa thanks for all your help. I had to mix together code from a lot of different posts, but your links started the process of finding all the posts.

Here's how to make undo/redo work for react-quill:

In the ReactQuill component in the render(), add:

    <ReactQuill 
        ...
        ref={(el) => {this.reactQuillRef = el}}
        .../>   

In the constructor, add:

        var icons = Quill.import("ui/icons");
        icons["undo"] = 'UNDO';
        icons["redo"] = 'REDO';

        this.modules = {
            history: {
                delay: 1000,
                maxStack: 100,
                userOnly: false
              },
            toolbar: {
                container: [
                ['undo'],
                ['redo'],
                ...
                ],
                handlers: {
                'undo' : this.myUndo,
                'redo' : this.myRedo,
                }
            }

In the function builder area (don't know the name for it), add these functions:

    myUndo = () => {
        let myEditor = this.reactQuillRef.getEditor();
        return myEditor.history.undo();
    }

    myRedo = () => {
        let myEditor = this.reactQuillRef.getEditor();
        return myEditor.history.redo();
    }

That will get the undo/redo functions working. In the editor, the undo/redo buttons don't have the icons yet; I haven't figured out how to add the icons yet; they just have the words 'UNDO' and 'REDO'. But they work.

The next thing for me to figure out will be how to add the undo/redo icons. If anyone knows how to do this, please let me know. Thanks!



来源:https://stackoverflow.com/questions/59555447/how-to-create-undo-redo-buttons-in-quill-js-react-quill

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!