CKEditor: Customized HTML on inserting an image

后端 未结 4 1610
借酒劲吻你
借酒劲吻你 2020-12-05 20:24

I\'m using CKEditor 3.5 to provide WYSYWYG editing in a website. When inserting an image you can provide width and height of the image, which results in HTML like follows:

相关标签:
4条回答
  • 2020-12-05 20:56

    Look at the "output html" sample, you can find there some code that changes the dimensions in images from styles to attributes, so you can adjust it to rewrite the URL.

    0 讨论(0)
  • 2020-12-05 20:59

    Best bet might be to "recreate" the src (and possibly the style) field's behavior. I've do something similar. (but not as complex)

    Start with the original code (from plugins/dialog/image.js) and create setup and commit logic that produces (and parses) the markup you're looking for.

    Then during dialog definition

    1. Delete Originals
    2. Add your "custom" fields

    style field not sure, maybe just leave it in the dialog, but stub out it's commit logic.

    I added my field to the dialog...

    var infoTab = dialogDefinition.getContents( 'info' );
    // Move the ID field from "advanced" to "info" tab.
    infoTab.add( idField_config);
    var idField_config = {
        type : 'text',
        label : 'Name',
        id : 'linkId',
        setup : function( type, element ) {
            //if ( type == IMAGE )
                this.setValue( element.getAttribute( 'id' ) );
        },
        commit : function( type, element ) {
            //if ( type == IMAGE ) {
                if ( this.getValue() || this.isChanged() )
                element.setAttribute( 'id', this.getValue() );
            //}
        }   
    };
    

    Issues I faced.

    1. New fields get added to end of dialog.
    2. Pieces of the original code ( type == IMAGE ) isn't valid (Love to know why but felt comfortable it was safe to comment for my usage)

    You might face problems with the markup rules undoing your hard work, but "output html" sample" suggestion should help you weed through that issue.

    0 讨论(0)
  • 2020-12-05 21:17

    I kind of had the same problem, I needed to remove those attributes from the generated HTML for the image, so what I did was to override the onOK method of the uploader and insert the image element manually using the CKEditor's API, something like this:

       CKEDITOR.on('dialogDefinition', function(ev) {
            // Take the dialog name and its definition from the event data
            var dialogName = ev.data.name;
            var dialogDefinition = ev.data.definition;
            var editor = ev.editor;
            if (dialogName == 'image') {
               dialogDefinition.onOk = function(e) {
                  var imageSrcUrl = e.sender.originalElement.$.src;
                  var imgHtml = CKEDITOR.dom.element.createFromHtml("<img src=" + imageSrcUrl + " alt='' align='right'/>");
                  editor.insertElement(imgHtml);
               };
            }
      }
    

    This has worked for us so far.

    0 讨论(0)
  • 2020-12-05 21:18

    I don't have enough points to comment on that previous answer. but in respect to your error: CKEDITOR.currentInstance returns undefined.

    That is strange because CKEDITOR is global, but you shouldn't have to resort to that.

    Within the OK function invocation, you have access to "editor", you shouldn't have to get the instance.

    just a suggestion.

    0 讨论(0)
提交回复
热议问题