【问题标题】:How to create undo/redo buttons in Quill JS (react-quill)?如何在 Quill JS (react-quill) 中创建撤消/重做按钮?
【发布时间】:2020-04-20 15:39:48
【问题描述】:

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;

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

【问题讨论】:

    标签: reactjs quill


    【解决方案1】:

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

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

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

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

    在构造函数中,添加:

            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,
                    }
                }
    

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

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

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

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

    【讨论】:

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

    不确定这将如何或不会与 React 集成,但我能够在 quill repo 上使用 this issue 获取 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,
    ...
    

    【讨论】:

    • 这对我有用,只要确保你从 Quill 模块导入默认值.... import Quill from 'quill';令人惊讶的是他们有重做/撤消的图标,但他们没有在 ui/icons 中定义它们
    【解决方案3】:

    有谁知道我需要写什么代码以及在哪里 为了将撤消/重做图标添加到连接到 Quill 中内置了撤消/重做功能?

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

    看看this。 搜索项目 020、021 和 026。

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

    quill.history.undo();
    

    History Module

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

    【讨论】:

    • 我有“UNDO/REDO”按钮出现,但该功能不起作用。这是我的新代码:``` class QuillTextEditor extends Component { ... this.modules = { history: { delay: 1000, maxStack: 100, userOnly: false }, toolbar: [ ['undo'], ['redo' ], ... ], 处理程序:{ 'undo' : quill.history.undo(), 'redo' : quill.history.redo() } }; ``` 我收到一条错误消息,提示“quill”未定义。你知道如何解决这个问题吗?非常感谢您的帮助。
    • 当您没有引用任何内容时会出现此消息。在这种情况下,quill 变量没有启动。 Here's 如何启动这个变量。只有一个问题:您使用的是reactjs,不幸的是我不知道如何使用这项技术。根据您对该工具的了解,您需要了解如何获取 new Quill(...) 的参考。
    • @PageCOW 看看this 或this 是否可以帮助您。如果这对您没有帮助,请在 stackoverflow 上搜索:quill reactjs。
    【解决方案4】:

    我只是想为使用 react-quill 的开发人员添加一些东西。在这一行:

     var icons = Quill.import("ui/icons");
    

    您没有获得 Quill 参考。所以你可以使用上面的代码来代替:

     var icons = ReactQuill.Quill.import("ui/icons");
    

    这解决了 ReactQuill 的问题。

    【讨论】:

    • 我可以像这样获得 Quill 参考。 import { Quill } from 'react-quill'我正在使用 react-quill@2.0.0-beta.2
    • 是的,基本上就是解构导入。
    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    相关资源
    最近更新 更多