【问题标题】:CKEditor image2 change initial sizeCKEditor image2 改变初始大小
【发布时间】:2018-12-07 21:59:25
【问题描述】:

使用 image2 插件上传图片时,高度和宽度最初设置为图片的尺寸。我们的一些用户正在上传大量图像,然后单击“确定”将其插入页面,而无需先选择合理的尺寸。图片填满了整个编辑器,他们看不到自己在做什么。

如何将初始大小设置为 300 像素宽?

我们正在使用带有 image2 插件的 CKEditor 4.11.1。

我可以通过破解 plugins/image2/dialog/image2.js 并将其添加到第 148 行附近的 onChangeSrc() 来实现:

 // Limit initial size
 if (width > editor.config.image_initial_width) {
   height = Math.round( editor.config.image_initial_width * ( height / width ) )
   width = editor.config.image_initial_width;
 }
 if (height > editor.config.image_initial_height) {
   width = Math.round( editor.config.image_initial_height * ( width / height) );
   height = editor.config.image_initial_height;
 }

然后定义 config.image_initial_height=300 和 config.image_initial_width=300。

但是我怎样才能在不进行黑客攻击的情况下实现这一目标呢?

【问题讨论】:

    标签: ckeditor


    【解决方案1】:

    这对我有用。

    用我们自己的替换 image2 onChange 事件,但保留一个引用并调用它。

    需要在config中定义两个变量:

        config.ckeditor_image2_initial_width = 300;
        config.ckeditor_image2_initial_height = 300;
    
    jQuery(document).ready(function() {
      if (typeof CKEDITOR !== 'undefined') {
        CKEDITOR.on('dialogDefinition', function(e) {
          if (e.data.name == 'image2') {
            var infoTab = e.data.definition.getContents('info');
            var src_field = infoTab.elements[0].children[0].children[0];
            var widthfield = infoTab.elements[2].children[0];
            var height_field = infoTab.elements[2].children[1];
            var src_was_changed = 0;
    
            // Add a change function to the height field.
            height_field.onChange = heightChanged;
    
            // We need to add a change event to the src field but it already has
            // one from image2 plugin. Replace it with our own but keep a reference
            // and call it with CKEditor tools.
            // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools.html#method-override
            src_field.onChange = CKEDITOR.tools.override(src_field.onChange, function(original) {
              return function() {
                // Call the original image2 onChangeSrc() function.
                original.call(this);
                var dialog = this.getDialog();
                var widget_image_src = 0;
                if (e.editor.widgets.selected.length) {
                  widget_image_src = e.editor.widgets.selected[0].data.src;
                }
                // Fire only when image src is set and has actually changed.
                if (this.getValue() && (this.getValue() !== widget_image_src)) {
                  var initial_width = e.editor.config.ckeditor_image2_initial_width;
                  var initial_height = e.editor.config.ckeditor_image2_initial_height;
                  if (typeof initial_width !== 'undefined' || typeof initial_height !== 'undefined') {
                    // Set a flag to be used in heightChanged().
                    src_was_changed = 1;
                  }
                }
              }
            });
    
            // Change event for the image height field.
            function heightChanged() {
              if (src_was_changed) {
                src_was_changed = 0;
                var dialog = this.getDialog();
                var initial_width = e.editor.config.ckeditor_image2_initial_width;
                var initial_height = e.editor.config.ckeditor_image2_initial_height;
                var width_field = dialog.getContentElement('info', 'width');
                var height_field = dialog.getContentElement('info', 'height');
                var new_width = orig_width = width_field.getValue();
                var new_height = orig_height = height_field.getValue();
                if (new_height > initial_height) {
                  new_height = initial_height;
                  new_width = Math.round(orig_width * (initial_height / orig_height));
                }
                if (new_width > initial_width) {
                  new_width = initial_width;
                  new_height = Math.round(orig_height * (initial_width / orig_width));
                }
                width_field.setValue(new_width);
                height_field.setValue(new_height);
              }
            }
          }
        });
      }
    });
    

    【讨论】:

    • 您好,上面的代码在本地服务器上运行良好。但是当我将我的项目部署到 pythonanywhere 时,new_heightnew_width 是空的。因此,最后,width_fieldheight_field 具有原始值。知道为什么吗?谢谢。顺便说一句,我正在使用 image2。
    • 我已经更新了不使用 setTimeout() 位的答案。相反,它对高度字段使用更改功能。也许这会有所帮助...?
    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多