【问题标题】:ckeditor 5 custom dropdown renders emptyckeditor 5自定义下拉菜单呈现为空
【发布时间】:2021-03-26 22:51:36
【问题描述】:

我遵循了 ckeditor 5 的指南: https://ckeditor.com/docs/ckeditor5/latest/api/module_ui_dropdown_dropdownview-DropdownView.html 我还发现了这个话题: Register click listener on ckeditor5 dropdown items 我关注了他们两个,但下拉列表显示为空: How it looks

我希望下一个代码将创建包含 2 个元素的下拉菜单:英语和西班牙语。

有我插件的代码:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import {createDropdown, addListToDropdown} from "@ckeditor/ckeditor5-ui/src/dropdown/utils";
import Collection from "@ckeditor/ckeditor5-utils/src/collection";
import Model from "@ckeditor/ckeditor5-engine/src/model/model";
import SplitButtonView from "@ckeditor/ckeditor5-ui/src/dropdown/button/splitbuttonview";

export default class Preset extends Plugin {

    static get pluginName() {
        return 'Preset';
    }

    init() {
        const editor = this.editor;

        editor.ui.componentFactory.add('Preset', locale => {
            const dropdownView = createDropdown(locale);
            dropdownView.buttonView.set({
                label: 'Translate',
                withText: true,
            });

            const items = new Collection();
            items.add( {
                type: 'button',
                model: new Model({
                    id: 'en',
                    withText: true,
                    label: 'English',
                })
            } );
            items.add( {
                type: 'button',
                model: new Model({
                    id: 'es',
                    withText: true,
                    label: 'Spanish'
                })
            } );

            addListToDropdown(dropdownView, items);

            dropdownView.on('execute', (eventInfo) => {
                const { id, label } = eventInfo.source;

                if ( id === 'en' ) {
                    console.log('Object (en):', label);
                } else if ( id === 'es' ) {
                    console.log('Object (es):', label);
                }
            });

            return dropdownView;
        });
    }
}

有ckeditor的代码(如何注册我的插件):

/**
 * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

// The editor creator to use.
import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import EssentialsPlugin from '@ckeditor/ckeditor5-essentials/src/essentials';
import UploadAdapter from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
import EasyImage from '@ckeditor/ckeditor5-easy-image/src/easyimage';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
import Indent from '@ckeditor/ckeditor5-indent/src/indent';
import Link from '@ckeditor/ckeditor5-link/src/link';
import List from '@ckeditor/ckeditor5-list/src/list';
import MediaEmbed from '@ckeditor/ckeditor5-media-embed/src/mediaembed';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import PasteFromOffice from '@ckeditor/ckeditor5-paste-from-office/src/pastefromoffice';
import Table from '@ckeditor/ckeditor5-table/src/table';
import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
import TextTransformation from '@ckeditor/ckeditor5-typing/src/texttransformation';
import Preset from "./preset";

export default class ClassicEditor extends ClassicEditorBase {}

// Plugins to include in the build.
ClassicEditor.builtinPlugins = [
    EssentialsPlugin,
    UploadAdapter,
    Autoformat,
    Bold,
    Italic,
    BlockQuote,
    CKFinder,
    EasyImage,
    Heading,
    Image,
    ImageCaption,
    ImageStyle,
    ImageToolbar,
    ImageUpload,
    Indent,
    Link,
    List,
    MediaEmbed,
    Paragraph,
    PasteFromOffice,
    Table,
    TableToolbar,
    TextTransformation,
    Preset
];

// Editor configuration.
ClassicEditor.defaultConfig = {
    toolbar: {
        items: [
            'heading',
            '|',
            'bold',
            'italic',
            'link',
            'bulletedList',
            'numberedList',
            '|',
            'indent',
            'outdent',
            '|',
            'imageUpload',
            'blockQuote',
            'insertTable',
            'mediaEmbed',
            'undo',
            'redo',
            'Preset'
        ]
    },
    image: {
        toolbar: [
            'imageStyle:full',
            'imageStyle:side',
            '|',
            'imageTextAlternative'
        ]
    },
    table: {
        contentToolbar: [
            'tableColumn',
            'tableRow',
            'mergeTableCells'
        ]
    },
    // This value must be kept in sync with the language defined in webpack.config.js.
    language: 'en'
};

我怎么称呼它(它发生在其他项目中):

setCKEditors = function(){
    var textareas = $('.ckeditor, .ckeditor-collection textarea');

    for(var i=0; i < textareas.length; i++) {
        if(textareas[i].style.display === '') {
            ClassicEditor
                .create(textareas[i], {
                    link: {
                        decorators: {
                            openInNewTab: {
                                mode: 'manual',
                                label: 'Open in a new tab',
                                defaultValue: true,         // This option will be selected by default.
                                attributes: {
                                    target: '_blank',
                                    class: 'text-link'
                                }
                            }
                        }
                    }
                })
                .catch(error => {
                    console.error(error);
                });
        }
    }
};

$(function() {
    console.log('E');
    setCKEditors();
    $('.sonata-collection-add').click(function(){
        setTimeout(function(){
            setCKEditors();
        }, 500);
    });

    $('.dropdown a.sonata-toggle-filter').each(function() {
        if ($(this).attr('sonata-filter') == 'false') {
            return;
        }
        if( ~$(this).first().attr('filter-target').indexOf('issued_to_students') ) {
            if ($(this).find('i').hasClass('fa-square-o')) {
                $(this).click();
            }
        }
    });
});

我做错了什么?也许我应该提供一些 CSS 或其他选项?

【问题讨论】:

    标签: javascript jquery css ckeditor ckeditor5


    【解决方案1】:

    我不知道你是否还有这个问题,但我遇到了同样的问题,这是因为我没有为 Model 的课程使用良好的导入。好的导入是:

    import Model from '@ckeditor/ckeditor5-ui/src/model';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2021-10-22
      • 1970-01-01
      • 2020-05-11
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多