简体   繁体   English

插件不适用于 Vue 组件中的多个 CKEditor4 实例

[英]Plugins don't work with multiple instances of CKEditor4 in Vue Components

I have created a Vue component for rich text editing using CKEditor 4 for my website.我已经使用 CKEditor 4 为我的网站创建了一个用于富文本编辑的 Vue 组件。 When this component is mounted for the first time, the editor and all the default plugins work fine but when another instance of the same component is mounted dynamically on the same page, the plugins such as Table, Image etc. which involve a popup dialog do not work (I am unable to type in any of the text inputs in the dialogs, such as Table Columns and Rows).当这个组件第一次挂载时,编辑器和所有默认插件都可以正常工作,但是当同一个组件的另一个实例在同一页面上动态挂载时,涉及弹出对话框的 Table、Image 等插件会做不起作用(我无法在对话框中输入任何文本输入,例如表列和行)。 The same issue exists with the official ckeditor4-vue package as well.官方 ckeditor4-vue 包也存在同样的问题。 I tried to resolve the issue by using the general package (zip) instead of the vue specific one but that didn't help.我试图通过使用通用包 (zip) 而不是 vue 特定的包来解决这个问题,但这没有帮助。 Here is my code.这是我的代码。

<template>
    <div>
        <textarea v-bind:id="fieldName" v-bind:name="fieldName" v-model="content"></textarea>
    </div>
</template>

<script>
    import './config';
    import './ckeditor/ckeditor';
    export default {
        props: {
            currentContent: {
                type: String,
                default: null,
            },
            fieldName: {
                type: String,
                default: 'content',
            },
            toolbarType: {
                type: String,
                default: 'Full',
            },
        },
        data: function () {
            return {
                editorConfig: {
                    toolbar_Full: null,
                    toolbar: this.toolbarType,
                },
                editor: null,
                content: null,
                initialized: false,
                siteUrl: siteUrl,
            };
        },
        mounted: function () {
            let vm = this;
            vm.initializeTextarea();
        },
        beforeDestroy: function () {
            let vm = this;
            vm.destroyTextarea();
        },
        methods: {
            initializeTextarea: function () {
                let vm = this;
                var editorConfig = JSON.parse(JSON.stringify(vm.editorConfig));
                vm.editor = CKEDITOR.replace(vm.fieldName, editorConfig);
                console.log("After Initialize: CKEditor Instances: ",CKEDITOR.instances);
            },
            destroyTextarea: function () {
                let vm = this;
                CKEDITOR.instances[vm.fieldName].destroy();
                console.log("After Destroy: CKEditor Instances: ",CKEDITOR.instances);
            },
        },
    };
</script>

To replicate the issue, the component can be imported into another component and a second instance of the same component can be added dynamically using v-if, the plugins mentioned above would not work correctly in the second (dynamically created) instance.为了重现这个问题,可以将该组件导入到另一个组件中,并且可以使用 v-if 动态添加同一组件的第二个实例,上述插件在第二个(动态创建的)实例中将无法正常工作。

EDIT I tried changing the v-if to a v-show to check if it works if both are initialized simultaneously but that didn't help either.编辑我尝试将 v-if 更改为 v-show 以检查它是否在同时初始化时起作用,但这也无济于事。 Even if both editor instances are present on a simple HTML page, the plugins don't work.即使两个编辑器实例都出现在一个简单的 HTML 页面上,这些插件也不起作用。

Vue needs a key to differentiate different instances of the same component. Vue 需要一个键来区分同一组件的不同实例。 The same you will do in a v-for.你会在 v-for 中做同样的事情。 I think you don't need to do that initialize/destroy process but just bind needed properties to the editor and give a key to parent where this component is being injected.我认为您不需要执行初始化/销毁过程,而只需将所需的属性绑定到编辑器,并为注入该组件的父级提供一个键。

It turned out to be a jQuery dialog interfering with CKEditor's dialog.结果是一个 jQuery 对话框干扰了 CKEditor 的对话框。 Once the editor was placed outside the dialog, the plugins work as long as no other jQuery dialog is open on the same window/tab.一旦编辑器被放置在对话框之外,只要没有其他 jQuery 对话框在同一窗口/选项卡上打开,插件就可以工作。

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

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