# all files / src/js/base/module/ Handle.js

54.1% Statements 33/61
42.86% Branches 6/14
71.43% Functions 10/14
54.1% Lines 33/61

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    1441×
    1×

    1×
    92×
    91×
    91×
    91×
    91×
    91×

    91×

    1×

    61×

    4×

    2×

    92×
    91×

    91×

    91×

    1×
    2×

    1×
    64×
    1×

    63×
    63×

    63×

    63×

    63×

    63×

    1×
    67×
    67×

    1×
     import $ from 'jquery';
    import dom from '../core/dom';

    export default class Handle {
      constructor(context) {
        this.context = context;
        this.$document = $(document);
        this.$editingArea = context.layoutInfo.editingArea;
        this.options = context.options;
        this.lang = this.options.langInfo;

        this.events = {
          'summernote.mousedown': (we, e) => {
            Iif (this.update(e.target)) {
              e.preventDefault();
            }
          },
          'summernote.keyup summernote.scroll summernote.change summernote.dialog.shown': () => {
            this.update();
          },
          'summernote.disable': () => {
            this.hide();
          },
          'summernote.codeview.toggled': () => {
            this.update();
          }
        };
      }

      initialize() {
        this.$handle = $([
          '<div class="note-handle">',
          '<div class="note-control-selection">',
          '<div class="note-control-selection-bg"></div>',
          '<div class="note-control-holder note-control-nw"></div>',
          '<div class="note-control-holder note-control-ne"></div>',
          '<div class="note-control-holder note-control-sw"></div>',
          '<div class="',
          (this.options.disableResizeImage ? 'note-control-holder' : 'note-control-sizing'),
          ' note-control-se"></div>',
          (this.options.disableResizeImage ? '' : '<div class="note-control-selection-info"></div>'),
          '</div>',
          '</div>'
        ].join('')).prependTo(this.$editingArea);

        this.$handle.on('mousedown', (event) => {
          if (dom.isControlSizing(event.target)) {
            event.preventDefault();
            event.stopPropagation();

            const $target = this.$handle.find('.note-control-selection').data('target');
            const posStart = $target.offset();
            const scrollTop = this.$document.scrollTop();

            const onMouseMove = (event) => {
              this.context.invoke('editor.resizeTo', {
                x: event.clientX - posStart.left,
                y: event.clientY - (posStart.top - scrollTop)
              }, $target, !event.shiftKey);

              this.update($target[0]);
            };

            this.$document
              .on('mousemove', onMouseMove)
              .one('mouseup', (e) => {
                e.preventDefault();
                this.$document.off('mousemove', onMouseMove);
                this.context.invoke('editor.afterCommand');
              });

            if (!$target.data('ratio')) { // original ratio.
              $target.data('ratio', $target.height() / $target.width());
            }
          }
        });

        // Listen for scrolling on the handle overlay.
        this.$handle.on('wheel', (e) => {
          e.preventDefault();
          this.update();
        });
      }

      destroy() {
        this.$handle.remove();
      }

      update(target) {
        if (this.context.isDisabled()) {
          return false;
        }

        const isImage = dom.isImg(target);
        const $selection = this.$handle.find('.note-control-selection');

        this.context.invoke('imagePopover.update', target);

        Iif (isImage) {
          const $image = $(target);
          const position = $image.position();
          const pos = {
            left: position.left + parseInt($image.css('marginLeft'), 10),
            top: position.top + parseInt($image.css('marginTop'), 10)
          };

          // exclude margin
          const imageSize = {
            w: $image.outerWidth(false),
            h: $image.outerHeight(false)
          };

          $selection.css({
            display: 'block',
            left: pos.left,
            top: pos.top,
            width: imageSize.w,
            height: imageSize.h
          }).data('target', $image); // save current image element.

          const origImageObj = new Image();
          origImageObj.src = $image.attr('src');

          const sizingText = imageSize.w + 'x' + imageSize.h + ' (' + this.lang.image.original + ': ' + origImageObj.width + 'x' + origImageObj.height + ')';
          $selection.find('.note-control-selection-info').text(sizingText);
          this.context.invoke('editor.saveTarget', target);
        } else {
          this.hide();
        }

        return isImage;
      }

      /**
       * hide
       *
       * @param {jQuery} $handle
       */
      hide() {
        this.context.invoke('editor.clearTarget');
        this.$handle.children().hide();
      }
    }