text: //
  // Copyright (c) 2013-2019 Håkan Edling
  //
  // This software may be modified and distributed under the terms
  // of the MIT license. See the LICENSE file for details.
  //
  // http://github.com/piranhacms/piranha.core
  //
  
  tinymce.PluginManager.add('piranhaimage', function (editor) {
      function showDialog() {
          var win, data, dom = editor.dom, imgElm = editor.selection.getNode();
          var src, width, height;
  
          width = dom.getAttrib(imgElm, 'width');
          height = dom.getAttrib(imgElm, 'height');
          src = dom.getAttrib(imgElm, 'src');
  
          if (src.startsWith('/')) {
              var components = src.split('/');
              var pos = 0;
  
              for (pos = 0; pos < components.length; pos++) {
                  if (components[pos] == 'media') {
                      break;
                  }
                  if (pos == (components.length - 1)) {
                      pos = -1;
                      break;
                  }
              }
  
              if (pos != -1 && components.length > (2 + pos)) {
                  if (components[2 + pos] != 'x') {
                      width = components[2 + pos];
                  }
                  if (components.length > (3 + pos)) {
                      height = components[3 + pos];
                  }
                  src = components.slice(0, 2 + pos).join('/');
              }
          }
  
          if (imgElm.nodeName == "IMG" && !imgElm.getAttribute('data-mce-object')) {
              data = {
                  src: {
                      value: src
                  },
                  alt: dom.getAttrib(imgElm, 'alt'),
                  size: {
                      width: width,
                      height: height
                  }
              };
          } else {
              imgElm = null;
          }
  
          win = editor.windowManager.open({
              title: "Insert/edit image",
              data: data,
              initialData: data,
              body: {
                  type: 'panel',
                  items: [
                      {
                          type: 'urlinput',
                          name: 'src',
                          filetype: 'image',
                          label: 'Source'
                      },
                      { name: 'alt', type: 'input', label: 'Image description' },
                      {
                          type: 'sizeinput',
                          name: 'size',
                          label: 'Dimensions'
                      }
                  ]
              },
              buttons: [
                  {
                    type: 'cancel',
                    name: 'cancel',
                    text: 'Cancel'
                  },
                  {
                    type: 'submit',
                    name: 'save',
                    text: 'Save',
                    primary: true
                  }
              ],
              onSubmit: function (e) {
                  var data = win.getData();
  
                  var attrs = {
                      src: data.src.value,
                      width: data.size.width,
                      height: data.size.height,
                      alt: data.alt
                  };
  
                  if (imgElm) {
                      dom.setAttribs(imgElm, attrs);
                  } else {
                      editor.insertContent(dom.createHTML('img', attrs));
                  }
                  win.close();
              }
          });
      }
  
      editor.ui.registry.addToggleButton('piranhaimage', {
          icon: 'image',
          tooltip: 'Insert/edit image',
          onAction: showDialog,
          onSetup: function (buttonApi) {
              return editor.selection.selectorChangedWithUnbind('img:not([data-mce-object],[data-mce-placeholder]),figure.image', buttonApi.setActive).unbind;
          }
      });
  });