How to set up CKEditor for multiple instances with different heights?

前端 未结 6 467
甜味超标
甜味超标 2021-01-02 01:01

I\'d like to have multiple instances of CKEditor based on the same config settings, but with different heights. I tried setting up config with the default height, s

6条回答
  •  悲&欢浪女
    2021-01-02 01:38

    The easiest way to initialize two editors with custom heights is:

    $('#editor1').ckeditor({ height: 100 });
    $('#editor2').ckeditor({ height: 200 });
    

    or without jQuery:

    CKEDITOR.replace('editor1', { height: 100 });
    CKEDITOR.replace('editor2', { height: 200 });
    

    AFAIK it isn't possible to change editor's height on the fly.

    If these methods weren't working for you, then you were doing sth else wrong.

    Update:

    Answering to your comment - your question in fact wasn't about CKEditor, but rather about sharing one object with only two different properties. So you can try like this:

    var configShared = {
            startupOutlineBlocks:true,
            scayt_autoStartup:true,
            // etc.
        },
        config1 = CKEDITOR.tools.prototypedCopy(configShared),
        config2 = CKEDITOR.tools.prototypedCopy(configShared);
    config1.height = 100;
    config2.height = 200;
    
    CKEDITOR.replace('editor1', config1);
    CKEDITOR.replace('editor2', config2);
    

    CKEDITOR.tools.prototypedCopy is a tool that creates new object with prototype set to the passed one. So they share all properties except of these you override later.

    Update 2:

    This is the update for the "Update" section in the question :).

    There's no quirk in CKEditor's timing or bug or whatsoever - it's pure JavaScript and how BOM/DOM and browsers work plus some practical approach.

    First thing - 90% of BOM/DOM is synchronous, but there are a couple of things that aren't. Because of this entire editor has to have asynchronous nature. That's why it provides so many events.

    Second thing - in JS object are passed by reference and as we want CKEditor to initialize very quickly we should avoid unnecessary tasks. One of these is copying config object (without good reason). So to save some msecs (and because of async plugins loading too) CKEditor extends passed config object only by setting its prototype to object containing default options.

    Summarizing - I know that this may look like a bug, but it's how JS/BOM/DOM libs work. I'm pretty sure that many other libs' async methods are affected by the same issue.

提交回复
热议问题