代码之家  ›  专栏  ›  技术社区  ›  Dirk J. Faber

在CKEditor5中插入自定义元素

  •  0
  • Dirk J. Faber  · 技术社区  · 6 年前

    span 元素来显示工具提示。其思想是显示一个工具提示,其中有一个(英尺)注释,而元素本身将显示一个递增的数字。在CKEditor4中,我用以下方法制作了这样的东西:

    CKEDITOR.dialog.add( 'footnoteDialog', function( editor ) {
        return {
            title: 'Footnote Properties',
            minWidth: 400,
            minHeight: 100,
            contents: [
                {
                    id: 'tab-basic',
                    label: 'Basic Settings',
                    elements: [
                        {
                            type: 'text',
                            id: 'content',
                            label: 'Content of footnote',
                            validate: CKEDITOR.dialog.validate.notEmpty( "Footnote field cannot be empty." )
                        }
                    ]
                }
            ],
            onOk: function() {
                var dialog = this;
    
                var footnote = editor.document.createElement( 'span' );
                footnote.setAttribute('class', 'footnote');
                footnote.setAttribute('data-toggle', 'tooltip');
                footnote.setAttribute( 'title', dialog.getValueOf( 'tab-basic', 'content' ) );
                footnote.setText('[FN]');
    
                editor.insertElement( footnote );
            }
        };
    });
    

    [FN] 将以递增的数字进行变换。

    现在我尝试在CKEditor5中创建这个插件,但是没有成功。我遇到了两个问题。首先,我无法在文本中插入元素。第二,当我想使用属性 data-toggle 这不起作用是因为 -

    import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
    import pilcrowIcon from '@ckeditor/ckeditor5-core/theme/icons/pilcrow.svg';
    import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
    
    export default class Footnote extends Plugin {
        init() {
            const editor = this.editor;
    
            editor.ui.componentFactory.add( 'footnote', locale => {
                const view = new ButtonView( locale );
    
                view.set( {
                    label: 'Insert footnote',
                    icon: pilcrowIcon,
                    tooltip: true
                } );
    
                view.on( 'execute', () => {
                    const source = prompt( 'Footnote' );
    
                    editor.model.schema.register( 'span', { allowAttributes: ['class', 'data-toggle', 'title'] } );
    
                    editor.model.change( writer => {
    
                    const footnoteElement = writer.createElement( 'span', {
                        class: 'footnote',
                        // data-toggle: 'tooltip',
                        title: source
                    });
    
                    editor.model.insertContent( footnoteElement, editor.model.document.selection );
                } );
            } );
    
                return view;
            } );
        }
    }
    

    跨度 data-toggle="tooltip"

    0 回复  |  直到 6 年前
    推荐文章