首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CKEditor image2更改初始大小

CKEditor image2更改初始大小
EN

Stack Overflow用户
提问于 2018-12-07 21:59:25
回答 1查看 961关注 0票数 1

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

如何将初始尺寸设置为300 to宽?

我们使用的是CKEditor 4.11.1和image2插件。

我能够通过对插件/image2 2/对话框/Image2.js进行黑客攻击,并将其添加到第148行的onChangeSrc()中来实现这一目标:

代码语言:javascript
复制
 // 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。

但是,如果没有黑客,我怎么能做到这一点呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-07 20:02:39

这就是对我有用的东西。

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

需要在配置中定义两个变量:

代码语言:javascript
复制
    config.ckeditor_image2_initial_width = 300;
    config.ckeditor_image2_initial_height = 300;
代码语言:javascript
复制
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);
          }
        }
      }
    });
  }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53677318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档