artabro/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.min.js

1 line
8.8 KiB
JavaScript
Raw Normal View History

2024-08-27 11:35:37 +02:00
(function(){CKEDITOR.plugins.add("pwimage",{requires:"dialog",init:function(editor){var pluginName="pwimage";var allowed="img[alt,id,!src,title,width](align_left,align_center,align_right,hidpi,align-left,align-center,align-right);"+"a[!href];"+"figure{width}(align_left,align_center,align_right,hidpi,align-left,align-center,align-right);"+"figcaption;";var required="img[alt,src]";editor.addCommand(pluginName,{allowedContent:allowed,requiredContent:required,exec:loadIframeImagePicker});editor.ui.addButton("PWImage",{label:editor.lang.common.image,command:pluginName,hidpi:true,icon:CKEDITOR.env.hidpi?this.path+"images/hidpi/pwimage.png":this.path+"images/pwimage.png"});editor.on("doubleclick",function(evt){var element=evt.data.element;if(element.is("img")&&!element.data("cke-realelement")&&!element.isReadOnly()){evt.cancel();editor.commands.pwimage.exec()}});if(editor.addMenuItems){editor.addMenuItems({image:{label:editor.lang.image.menu,command:"pwimage",group:"image"}})}if(editor.contextMenu){editor.contextMenu.addListener(function(element,selection){if(getSelectedImage(editor,element))return{image:CKEDITOR.TRISTATE_OFF}})}}});function getSelectedImage(editor,element){if(!element){var sel=editor.getSelection();element=sel.getSelectedElement()}if(element&&element.is("img")&&!element.data("cke-realelement")&&!element.isReadOnly())return element}function loadIframeImagePicker(editor){var $in=jQuery("#Inputfield_id");var page_id;if($in.length){page_id=$in.val()}else{page_id=jQuery("#"+editor.name).closest(".Inputfield").attr("data-pid")}var edit_page_id=page_id;var file="";var imgClass="";var imgWidth=0;var imgHeight=0;var imgDescription="";var imgLink="";var hidpi=false;var selection=editor.getSelection();var se=selection.getSelectedElement();var node=selection.getStartElement();var $node=jQuery(node);var nodeParent=node.getParent();var nodeGrandparent=nodeParent.getParent();var src=$node.attr("src");var $linkWrapper=null;var $figureWrapper=null;var $figureCaption=null;var nodeParentName=nodeParent.$.nodeName.toUpperCase();var nodeGrandparentName=nodeGrandparent?nodeGrandparent.$.nodeName.toUpperCase():"";var figureNodeSafari=null;var $repeaterItem=jQuery("#"+editor.name).closest(".InputfieldRepeaterItem");if(selection.getType()==CKEDITOR.SELECTION_TEXT){if(nodeParentName=="FIGURE"){figureNodeSafari=nodeParent}else if(nodeGrandparentName=="FIGURE"){figureNodeSafari=nodeGrandparent}if(figureNodeSafari){selection.reset();selection.removeAllRanges();figureNodeSafari.$.style.display="block";selection.selectElement(figureNodeSafari)}}if($repeaterItem.length&&$repeaterItem.find(".InputfieldImage").length){var dataPageAttr=$repeaterItem.attr("data-page");if(typeof dataPageAttr!=="undefined")page_id=parseInt(dataPageAttr)}selection.lock();editor.lockSelection();if(nodeGrandparentName=="FIGURE"){$figureWrapper=jQuery(nodeGrandparent.getOuterHtml());$figureCaption=$figureWrapper.find("figcaption");$figureWrapper.find("img").remove()}else if(nodeParentName=="FIGURE"){$figureWrapper=jQuery(nodeParent.getOuterHtml());$figureCaption=$figureWrapper.find("figcaption");$figureWrapper.find("img").remove()}if(nodeParentName==="A"){$linkWrapper=jQuery(nodeParent.getOuterHtml());$linkWrapper.find("img").remove()}if(src){imgClass=$figureWrapper?$figureWrapper.attr("class"):$node.attr("class");hidpi=imgClass&&imgClass.indexOf("hidpi")>-1;imgWidth=$node.attr("width");imgHeight=$node.attr("height");imgDescription=$node.attr("alt");imgLink=nodeParentName==="A"?nodeParent.$.href:"";var parts=src.split("/");file=parts.pop();parts=parts.reverse();page_id="";for(var n=0;n<parts.length;n++){if(parts[n].match(/^\d+$/)){page_id=parts[n]+page_id}else if(page_id.length){break}}page_id=parseInt(page_id)}var modalUri=ProcessWire.config.urls.admin+"page/image/";var queryString="?id="+page_id+"&edit_page_id="+edit_page_id+"&modal=1";if(file.length)queryString+="&file="+file;if(imgWidth)queryString+="&width="+imgWidth;if(imgHeight)queryString+="&height="+imgHeight;if(imgClass&&imgClass.length)queryString+="&class="+encodeURIComponent(imgClass);queryString+="&hidp