如何在 Quill JS (react-quill) 中创建撤消/重做按钮?

Pag*_*COW 3 reactjs quill

QuillJS 没有默认的撤销/重做按钮。我正在尝试将它们添加到工具栏。Quill 有一个文件夹,其中保存了撤消/重做图标。在 node_modules 中,还有 undo() 和 redo() 函数。我对编码有点陌生,不知道如何访问这些东西并使它们工作。我正在使用反应。到目前为止,这是我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

有谁知道我需要写什么代码以及在哪里添加撤消/重做图标到工具栏,这些图标连接到 Quill 中内置的撤消/重做功能?我已经尝试了几天,无法弄清楚。

use*_*841 8

不确定这会或不会与 React 集成,但我能够在 quill 存储库上使用此问题获取 svg 图标。

toolbarOptions = [
    ['bold', 'italic', 'underline'],
    ['undo' , 'redo' ],
    [{ 'indent': '-1'}, { 'indent': '+1' }],
    [{ align: '' }, { align: 'center' }, { align: 'right' }, { align: 'justify' }],
    ['image']
];

var icons = Quill.import("ui/icons");
    icons["undo"] = `<svg viewbox="0 0 18 18">
    <polygon class="ql-fill ql-stroke" points="6 10 4 12 2 10 6 10"></polygon>
    <path class="ql-stroke" d="M8.09,13.91A4.6,4.6,0,0,0,9,14,5,5,0,1,0,4,9"></path>
  </svg>`;
    icons["redo"] = `<svg viewbox="0 0 18 18">
    <polygon class="ql-fill ql-stroke" points="12 10 14 12 16 10 12 10"></polygon>
    <path class="ql-stroke" d="M9.91,13.91A4.6,4.6,0,0,1,9,14a5,5,0,1,1,5-5"></path>
  </svg>`;

var quill = new Quill('div#content', {
    modules: {
        toolbar: toolbarOptions,
...
Run Code Online (Sandbox Code Playgroud)

撤消和重做图标


Pag*_*COW 7

@Loa 感谢您的所有帮助。我不得不将许多不同帖子的代码混合在一起,但是您的链接开始了查找所有帖子的过程。

以下是如何使 react-quill 撤消/重做工作:

在 render() 的 ReactQuill 组件中,添加:

    <ReactQuill 
        ...
        ref={(el) => {this.reactQuillRef = el}}
        .../>   
Run Code Online (Sandbox Code Playgroud)

在构造函数中,添加:

        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,
                }
            }
Run Code Online (Sandbox Code Playgroud)

在函数构建器区域(不知道它的名字),添加这些函数:

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

    myRedo = () => {
        let myEditor = this.reactQuillRef.getEditor();
        return myEditor.history.redo();
    }
Run Code Online (Sandbox Code Playgroud)

这将使撤消/重做功能工作。在编辑器中,撤销/重做按钮还没有图标;我还没有想出如何添加图标;他们只有“撤消”和“重做”这两个词。但他们工作。

我接下来要弄清楚的是如何添加撤消/重做图标。如果有人知道如何做到这一点,请告诉我。谢谢!

  • 我很高兴能帮到你。最重要的是,您不仅发现了解决方案,而且决定与社区分享。继续努力吧!分享知识!我今天帮助了你,但没有什么可以阻止我将来需要你或其他任何人的帮助。我希望您能够完成您的项目,祝您好运。:) (4认同)

Loa*_*Loa 2

有谁确切知道我需要编写哪些代码以及在哪里添加撤消/重做图标到工具栏,这些图标连接到 Quill 中内置的撤消/重做功能?

你好。不幸的是,我仍然不知道如何将按钮连接到本机 Quill 功能。但您可以做其他事情来获得想要的结果。

看看这个搜索项目 020、021 和 026

您可以添加一个新按钮,并将其设置为调用以下代码:

quill.history.undo();
Run Code Online (Sandbox Code Playgroud)

历史模块

如果您还有其他问题,请发表评论。我会尽快回复您。