简体   繁体   English

如何在已创建的编辑器上设置数据?

[英]How to set data on an already created editor?

I want to set data to 3 nos.我想将数据设置为 3 个。 of already created editors which I am unable to do.已经创建的编辑器,我无法做到。

I tried pt.我试过pt。 no.不。 2 to set data which is happening on a newly created editor and not to the existing one. 2 设置正在新创建的编辑器上发生的数据,而不是现有的。

  1. Below is where i am creating my 3 editors:下面是我创建我的 3 个编辑器的地方:
create_editor: function () {
            window.editors = {};
            document.querySelectorAll('.editor').forEach((node, index) => {
                ClassicEditor
                    .create(node, {
                        //removePlugins: [ 'Heading', 'Link' ],
                        toolbar: ['bold', 'italic', 'bulletedList', 'numberedList', 'blockQuote', 'Heading'],
                        //placeholder: 'Type the content here!'
                    })
                    .then(editor => {
                        //console.log(editor);
                        window.editors[index] = editor
                    })
                    .catch(error => {
                        //console.error(error);
                    });
            });
        }
  1. Below is where i am trying to set my data to all my 3 existing editors:下面是我试图将我的数据设置给我所有 3 个现有编辑器的地方:
set_editor: function (elem, data) {
            ClassicEditor
                .create(document.querySelector('#' + elem))
                .then(editor => {
                    editor.setData(data);
                })
        }

The data are stored in a local storage and I want to retrieve them and set them to my already 3 created editors which currently is not happening.数据存储在本地存储中,我想检索它们并将它们设置为我已经创建的 3 个当前没有发生的编辑器。

Your problem here is that you need to get an instance of existing ClassicEditor to edit it.您的问题是您需要获取现有 ClassicEditor 的实例来编辑它。

As provided in this answer , you can store your instances in a global variable (like you are already doing, but instead of index store element.id as key)如本答案中所提供,您可以将实例存储在全局变量中(就像您已经在做的那样,而不是将索引存储element.id作为键)

Also from your comments, it is clear that you are facing race condition between editor create and editor set, so instead of saving the editor in window.editors save the promise, that way you can wait for editor create to finish before setting the value.同样从您的评论中,很明显您正面临编辑器创建和编辑器集之间的竞争条件,因此不要将编辑器保存在window.editors中,而是保存 promise,这样您就可以等待编辑器创建完成,然后再设置值。

create_editor: function () {
    window.editors = {};
    document.querySelectorAll('.editor').forEach(node => {
        window.editors[node.id] =
        ClassicEditor
            .create(node, {
                //removePlugins: [ 'Heading', 'Link' ],
                toolbar: ['bold', 'italic', 'bulletedList', 'numberedList', 'blockQuote', 'Heading'],
                //placeholder: 'Type the content here!'
            })
            .then(editor => {
                //console.log(editor);
            })
            .catch(error => {
                //console.error(error);
            });
    });
}

set_editor: function (elem, data) {
    window.editors[elem].then(editor => editor.setData(data));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM