WordPress/wp-includes/js/tinymce/plugins/wpeditimage/plugin.min.js

1 line
15 KiB
JavaScript

tinymce.PluginManager.add("wpeditimage",function(a){function b(b){return!(!a.dom.getAttrib(b,"data-mce-placeholder")&&!a.dom.getAttrib(b,"data-mce-object"))}function c(){var b,c=[];return p(["wp_img_alignleft","wp_img_aligncenter","wp_img_alignright","wp_img_alignnone","wp_img_edit","wp_img_remove"],function(d){function e(){var b=a.selection;d.settings.stateSelector&&b.selectorChanged(d.settings.stateSelector,function(a){d.active(a)},!0),d.settings.disabledStateSelector&&b.selectorChanged(d.settings.disabledStateSelector,function(a){d.disabled(a)})}var f;"|"===d?b=null:o.has(d)?(d={type:d},n.toolbar_items_size&&(d.size=n.toolbar_items_size),c.push(d),b=null):(b||(b={type:"buttongroup",items:[]},c.push(b)),a.buttons[d]&&(f=d,d=a.buttons[f],"function"==typeof d&&(d=d()),d.type=d.type||"button",n.toolbar_items_size&&(d.size=n.toolbar_items_size),d=o.create(d),b.items.push(d),a.initialized?e():a.on("init",e)))}),{type:"panel",layout:"stack",classes:"toolbar-grp inline-toolbar-grp wp-image-toolbar",ariaRoot:!0,ariaRemember:!0,items:[{type:"toolbar",layout:"flow",items:c}]}}function d(){r||l.hide()}function e(b){return b.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g,function(b,c,d){var e,f,g,h,i,j,k=tinymce.trim;return e=c.match(/id=['"]([^'"]*)['"] ?/),e&&(c=c.replace(e[0],"")),f=c.match(/align=['"]([^'"]*)['"] ?/),f&&(c=c.replace(f[0],"")),g=c.match(/class=['"]([^'"]*)['"] ?/),g&&(c=c.replace(g[0],"")),j=c.match(/width=['"]([0-9]*)['"] ?/),j&&(c=c.replace(j[0],"")),d=k(d),i=d.match(/((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i),i&&i[2]?(h=k(i[2]),i=k(i[1])):(h=k(c).replace(/caption=['"]/,"").replace(/['"]$/,""),i=d),e=e&&e[1]?e[1].replace(/[<>&]+/g,""):"",f=f&&f[1]?f[1]:"alignnone",g=g&&g[1]?" "+g[1].replace(/[<>&]+/g,""):"",!j&&i&&(j=i.match(/width=['"]([0-9]*)['"]/)),j&&j[1]&&(j=j[1]),j&&h?(j=parseInt(j,10),a.getParam("wpeditimage_html5_captions")||(j+=10),'<div class="mceTemp"><dl id="'+e+'" class="wp-caption '+f+g+'" style="width: '+j+'px"><dt class="wp-caption-dt">'+i+'</dt><dd class="wp-caption-dd">'+h+"</dd></dl></div>"):d})}function f(a){return a.replace(/<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g,function(a,b){var c="";return-1===b.indexOf("<img ")?(c=b.match(/<dd [^>]+>([\s\S]+?)<\/dd>/i),c&&c[1]?"<p>"+c[1]+"</p>":""):(c=b.replace(/\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi,function(a,b,c,d){var e,f,g,h;return h=c.match(/width="([0-9]*)"/),h=h&&h[1]?h[1]:"",h&&d?(e=b.match(/id="([^"]*)"/),e=e&&e[1]?e[1]:"",f=b.match(/class="([^"]*)"/),f=f&&f[1]?f[1]:"",g=f.match(/align[a-z]+/i)||"alignnone",f=f.replace(/wp-caption ?|align[a-z]+ ?/gi,""),f&&(f=' class="'+f+'"'),d=d.replace(/\r\n|\r/g,"\n").replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g,function(a){return a.replace(/[\r\n\t]+/," ")}),d=d.replace(/\s*\n\s*/g,"<br />"),'[caption id="'+e+'" align="'+g+'" width="'+h+'"'+f+"]"+c+" "+d+"[/caption]"):c}),-1===c.indexOf("[caption")&&(c=b.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi,"<p>$1</p>$2")),c)})}function g(b){var c,d,e,f,g,h,i,j,k=[],l=a.dom,m=/^\d+$/;return e={attachment_id:!1,size:"custom",caption:"",align:"none",extraClasses:"",link:!1,linkUrl:"",linkClassName:"",linkTargetBlank:!1,linkRel:"",title:""},e.url=l.getAttrib(b,"src"),e.alt=l.getAttrib(b,"alt"),e.title=l.getAttrib(b,"title"),i=l.getAttrib(b,"width"),j=l.getAttrib(b,"height"),(!m.test(i)||parseInt(i,10)<1)&&(i=b.naturalWidth||b.width),(!m.test(j)||parseInt(j,10)<1)&&(j=b.naturalHeight||b.height),e.customWidth=e.width=i,e.customHeight=e.height=j,c=tinymce.explode(b.className," "),d=[],tinymce.each(c,function(a){/^wp-image/.test(a)?e.attachment_id=parseInt(a.replace("wp-image-",""),10):/^align/.test(a)?e.align=a.replace("align",""):/^size/.test(a)?e.size=a.replace("size-",""):d.push(a)}),e.extraClasses=d.join(" "),f=l.getParents(b,".wp-caption"),f.length&&(f=f[0],c=f.className.split(" "),tinymce.each(c,function(a){/^align/.test(a)?e.align=a.replace("align",""):a&&"wp-caption"!==a&&k.push(a)}),e.captionClassName=k.join(" "),g=l.select("dd.wp-caption-dd",f),g.length&&(g=g[0],e.caption=a.serializer.serialize(g).replace(/<br[^>]*>/g,"$&\n").replace(/^<p>/,"").replace(/<\/p>$/,""))),b.parentNode&&"A"===b.parentNode.nodeName&&(h=b.parentNode,e.linkUrl=l.getAttrib(h,"href"),e.linkTargetBlank="_blank"===l.getAttrib(h,"target")?!0:!1,e.linkRel=l.getAttrib(h,"rel"),e.linkClassName=h.className),e}function h(a){return a&&!(!a.textContent&&!a.innerText)}function i(b,c){var d,e,f,g,i,j,k,l,m,n,o,p,q,r,s,t,u=a.dom;d=tinymce.explode(c.extraClasses," "),d||(d=[]),c.caption||d.push("align"+c.align),c.attachment_id&&(d.push("wp-image-"+c.attachment_id),c.size&&"custom"!==c.size&&d.push("size-"+c.size)),r=c.width,s=c.height,"custom"===c.size&&(r=c.customWidth,s=c.customHeight),p={src:c.url,width:r||null,height:s||null,alt:c.alt,title:c.title||null,"class":d.join(" ")||null},u.setAttribs(b,p),q={href:c.linkUrl,rel:c.linkRel||null,target:c.linkTargetBlank?"_blank":null,"class":c.linkClassName||null},b.parentNode&&"A"===b.parentNode.nodeName&&!h(b.parentNode)?c.linkUrl?u.setAttribs(b.parentNode,q):u.remove(b.parentNode,!0):c.linkUrl&&((k=u.getParent(b,"a"))&&u.insertAfter(b,k),k=u.create("a",q),b.parentNode.insertBefore(k,b),k.appendChild(b)),l=a.dom.getParent(b,".mceTemp"),f=b.parentNode&&"A"===b.parentNode.nodeName&&!h(b.parentNode)?b.parentNode:b,c.caption?(o=c.attachment_id?"attachment_"+c.attachment_id:null,t="align"+(c.align||"none"),e="wp-caption "+t,c.captionClassName&&(e+=" "+c.captionClassName.replace(/[<>&]+/g,"")),a.getParam("wpeditimage_html5_captions")||(r=parseInt(r,10),r+=10),l?(n=u.select("dl.wp-caption",l),n.length&&u.setAttribs(n,{id:o,"class":e,style:"width: "+r+"px"}),m=u.select(".wp-caption-dd",l),m.length&&u.setHTML(m[0],c.caption)):(o=o?'id="'+o+'" ':"",g="<dl "+o+'class="'+e+'" style="width: '+r+'px"><dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+c.caption+"</dd></dl>",j=u.create("div",{"class":"mceTemp"},g),(i=u.getParent(f,"p"))?(i.parentNode.insertBefore(j,i),u.isEmpty(i)&&u.remove(i)):f.parentNode.insertBefore(j,f),a.$(j).find("dt.wp-caption-dt").append(f))):l&&(i=u.create("p"),l.parentNode.insertBefore(i,l),i.appendChild(f),u.remove(l)),wp.media.events&&wp.media.events.trigger("editor:image-update",{editor:a,metadata:c,image:b}),a.nodeChanged()}function j(b){var c,d,e;return"undefined"!=typeof wp&&wp.media?(e=g(b),wp.media.events.trigger("editor:image-edit",{editor:a,metadata:e,image:b}),c=wp.media({frame:"image",state:"image-details",metadata:e}),wp.media.events.trigger("editor:frame-create",{frame:c}),d=function(d){a.focus(),a.undoManager.transact(function(){i(b,d)}),c.detach()},c.state("image-details").on("update",d),c.state("replace-image").on("replace",d),c.on("close",function(){a.focus(),c.detach()}),void c.open()):void a.execCommand("mceImage")}function k(b){var c;"DIV"===b.nodeName&&a.dom.hasClass(b,"mceTemp")?c=b:("IMG"===b.nodeName||"DT"===b.nodeName||"A"===b.nodeName)&&(c=a.dom.getParent(b,"div.mceTemp")),c?(a.selection.select(c.nextSibling?c.nextSibling:c.previousSibling?c.previousSibling:c.parentNode),a.selection.collapse(!0),a.dom.remove(c)):a.dom.remove(b),a.nodeChanged(),a.undoManager.add()}var l,m=tinymce.DOM,n=a.settings,o=tinymce.ui.Factory,p=tinymce.each,q=tinymce.Env.iOS,r=!0,s=tinymce.$("#postdivrich");return a.addButton("wp_img_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){k(a.selection.getNode())}}),a.addButton("wp_img_edit",{tooltip:"Edit",icon:"dashicon dashicons-edit",onclick:function(){j(a.selection.getNode())}}),p({alignleft:"Align left",aligncenter:"Align center",alignright:"Align right",alignnone:"Remove alignment"},function(b,c){var d=c.slice(5);a.addButton("wp_img_"+c,{tooltip:b,icon:"dashicon dashicons-align-"+d,cmd:"alignnone"===c?"wpAlignNone":"Justify"+d.slice(0,1).toUpperCase()+d.slice(1),onPostRender:function(){var b=this;a.on("NodeChange",function(d){var e;"IMG"===d.element.nodeName&&(e=a.dom.getParent(d.element,".wp-caption")||d.element,b.active("alignnone"===c?!/\balign(left|center|right)\b/.test(e.className):a.dom.hasClass(e,c)))})}})}),l=o.create(c()).renderTo(document.body).hide(),l.reposition=function(){var b,c,d,e,f=this.getEl(),g=5,h=8,i=window.pageYOffset||document.documentElement.scrollTop,j=tinymce.$("#wpadminbar")[0],k=tinymce.$(".mce-tinymce .mce-toolbar-grp")[0],l=0,n=a.selection.getRng().getBoundingClientRect(),o=(n.left+n.right)/2,p=(n.top+n.bottom)/2,r=n.top,s=z-n.bottom,t=window.innerWidth,u=f.offsetWidth,v=u/2,w=a.getContentAreaContainer().firstChild,x=m.getPos(w),y=w.offsetWidth,z=w.offsetHeight,A=f.offsetHeight,B=A+h+g;return q?b=n.top+x.y+h:r>=B?(e=" mce-arrow-down",b=n.top+x.y-A-h):s>=B?(e=" mce-arrow-up",b=n.bottom+x.y):(b=g,e=p>=B?" mce-arrow-down":" mce-arrow-up"),d=k?m.getPos(k).y+k.clientHeight:x.y,i&&(j&&0===j.getBoundingClientRect().top&&(l=j.clientHeight),i+l>d&&(d=i+l)),b&&d&&d+g>b&&(b=d+g,e=""),c=o-v,c+=x.x,u>=t?(e+=" mce-arrow-full",c=0):0>c&&n.left+u>t||c+u>t&&n.right-u<0?c=(t-u)/2:c<x.x?(e+=" mce-arrow-left",c=n.left+x.x):c+u>y+x.x&&(e+=" mce-arrow-right",c=n.right-u+x.x),q||(f.className=f.className.replace(/ ?mce-arrow-[\w]+/g,""),f.className+=e),m.setStyles(f,{left:c,top:b}),this},q&&a.on("click",function(b){if("IMG"===b.target.nodeName){var c=b.target;window.setTimeout(function(){a.selection.select(c)},200)}else l.hide()}),a.on("nodechange",function(c){var d=q?350:100;return"IMG"!==c.element.nodeName||b(c.element.nodeName)?void l.hide():void setTimeout(function(){var c=a.selection.getNode();"IMG"!==c.nodeName||b(c)?l.hide():l._visible?l.reposition():l.show()},d)}),l.on("show",function(){var a=this;r=!1,setTimeout(function(){a._visible&&(m.addClass(a.getEl(),"mce-inline-toolbar-grp-active"),a.reposition())},100)}),l.on("hide",function(){r=!0,m.removeClass(this.getEl(),"mce-inline-toolbar-grp-active")}),m.bind(window,"resize scroll",function(){!r&&s.hasClass("wp-editor-expand")&&d()}),a.on("init",function(){m.bind(a.getWin(),"resize scroll",d)}),a.on("blur hide",d),a.shortcuts.add("Alt+119","",function(){var a=l.find("toolbar")[0];a&&a.focus(!0)}),a.on("init",function(){var b=a.dom,c=a.getParam("wpeditimage_html5_captions")?"html5-captions":"html4-captions";b.addClass(a.getBody(),c),a.on("wpLoadImageForm",function(b){if(!a.getParam("wpeditimage_disable_captions")){var c={type:"textbox",flex:1,name:"caption",minHeight:60,multiline:!0,scroll:!0,label:"Image caption"};b.data.splice(b.data.length-1,0,c)}}),a.on("wpNewImageRefresh",function(a){var c,d;(c=b.getParent(a.node,"dl.wp-caption"))&&(c.style.width||(d=parseInt(a.node.clientWidth,10)+10,d=d?d+"px":"50%",b.setStyle(c,"width",d)))}),a.on("wpImageFormSubmit",function(c){var d,e,f,g,h,i=c.imgData.data,j=c.imgData.node,k=c.imgData.caption,l="",m="",n="";return i.id="__wp-temp-img-id",c.imgData.cancel=!0,i.style||(i.style=null),i.src?(k&&(k=k.replace(/\r\n|\r/g,"\n").replace(/<\/?[a-zA-Z0-9]+( [^<>]+)?>/g,function(a){return a.replace(/[\r\n\t]+/," ")}),k=k.replace(/(<br[^>]*>)\s*\n\s*/g,"$1").replace(/\s*\n\s*/g,"<br />")),j?(h=j.id||null,b.setAttribs(j,i),d=b.getParent(j,"dl.wp-caption"),k?d?(e=b.select("dd.wp-caption-dd",d)[0])&&(e.innerHTML=k):(j.className&&(l=j.className.match(/wp-image-([0-9]+)/),m=j.className.match(/align(left|right|center|none)/)),m?(m=m[0],j.className=j.className.replace(/align(left|right|center|none)/g,"")):m="alignnone",m=' class="wp-caption '+m+'"',l&&(l=' id="attachment_'+l[1]+'"'),n=i.width||j.clientWidth,n&&(n=parseInt(n,10),a.getParam("wpeditimage_html5_captions")||(n+=10),n=' style="width: '+n+'px"'),f=j.parentNode&&"A"===j.parentNode.nodeName?j.parentNode:j,g="<dl "+l+m+n+'><dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+k+"</dd></dl>",d=b.create("div",{"class":"mceTemp"},g),(e=b.getParent(f,"p"))?(e.parentNode.insertBefore(d,e),b.isEmpty(e)&&b.remove(e)):f.parentNode.insertBefore(d,f),a.$(d).find("dt.wp-caption-dt").append(f)):d&&(g=b.getOuterHTML("A"===j.parentNode.nodeName?j.parentNode:j),e=b.create("p",{},g),b.insertAfter(e,d.parentNode),a.selection.select(e),a.nodeChanged(),b.remove(d.parentNode))):(g=b.createHTML("img",i),k?(f=a.selection.getNode(),i.width&&(n=parseInt(i.width,10),a.getParam("wpeditimage_html5_captions")||(n+=10),n=' style="width: '+n+'px"'),g='<dl class="wp-caption alignnone"'+n+'><dt class="wp-caption-dt">'+g+'</dt><dd class="wp-caption-dd">'+k+"</dd></dl>",e="P"===f.nodeName?f:b.getParent(f,"p"),e&&"P"===e.nodeName?(d=b.create("div",{"class":"mceTemp"},g),e.parentNode.insertBefore(d,e),a.selection.select(d),a.nodeChanged(),b.isEmpty(e)&&b.remove(e)):a.selection.setContent('<div class="mceTemp">'+g+"</div>")):a.selection.setContent(g)),j=b.get("__wp-temp-img-id"),b.setAttrib(j,"id",h),void(c.imgData.node=j)):void(j&&(b.remove((d=b.getParent(j,"div.mceTemp"))?d:"A"===j.parentNode.nodeName?j.parentNode:j),a.nodeChanged()))}),a.on("wpLoadImageData",function(c){var d,e=c.imgData.data,f=c.imgData.node;(d=b.getParent(f,"dl.wp-caption"))&&(d=b.select("dd.wp-caption-dd",d)[0],d&&(e.caption=a.serializer.serialize(d).replace(/<br[^>]*>/g,"$&\n").replace(/^<p>/,"").replace(/<\/p>$/,"")))}),b.bind(a.getDoc(),"dragstart",function(c){var d=a.selection.getNode();"IMG"===d.nodeName&&b.getParent(d,".wp-caption")&&c.preventDefault()}),tinymce.Env.ie&&tinymce.Env.ie>10&&b.bind(a.getBody(),"mscontrolselect",function(c){"IMG"===c.target.nodeName&&b.getParent(c.target,".wp-caption")?a.getBody().focus():"DL"===c.target.nodeName&&b.hasClass(c.target,"wp-caption")&&c.target.focus()})}),a.on("ObjectResized",function(b){var c=b.target;"IMG"===c.nodeName&&a.undoManager.transact(function(){var d,e,f=a.dom;c.className=c.className.replace(/\bsize-[^ ]+/,""),(d=f.getParent(c,".wp-caption"))&&(e=b.width||f.getAttrib(c,"width"),e&&(e=parseInt(e,10),a.getParam("wpeditimage_html5_captions")||(e+=10),f.setStyle(d,"width",e+"px")))})}),a.on("BeforeExecCommand",function(b){var c,d,e,f,g,h=b.command,i=a.dom;if("mceInsertContent"===h)(c=i.getParent(a.selection.getNode(),"div.mceTemp"))&&(d=i.create("p"),i.insertAfter(d,c),a.selection.setCursorLocation(d,0),a.nodeChanged());else if("JustifyLeft"===h||"JustifyRight"===h||"JustifyCenter"===h||"wpAlignNone"===h){if(c=a.selection.getNode(),f="align"+h.slice(7).toLowerCase(),e=a.dom.getParent(c,".wp-caption"),"IMG"!==c.nodeName&&!e)return;c=e||c,g=a.dom.hasClass(c,f)?" alignnone":" "+f,c.className=c.className.replace(/ ?align(left|center|right|none)/g,"")+g,a.nodeChanged(),b.preventDefault(),l&&l.reposition()}}),a.on("keydown",function(b){var c,d,e,f,g=a.selection,h=b.keyCode,i=a.dom,j=tinymce.util.VK;if(h===j.ENTER)c=g.getNode(),d=i.getParent(c,"div.mceTemp"),d&&(i.events.cancel(b),tinymce.each(i.select("dt, dd",d),function(a){i.isEmpty(a)&&i.remove(a)}),f=tinymce.Env.ie&&tinymce.Env.ie<11?"":'<br data-mce-bogus="1" />',e=i.create("p",null,f),"DD"===c.nodeName?i.insertAfter(e,d):d.parentNode.insertBefore(e,d),a.nodeChanged(),g.setCursorLocation(e,0));else if((h===j.DELETE||h===j.BACKSPACE)&&(c=g.getNode(),"DIV"===c.nodeName&&i.hasClass(c,"mceTemp")?d=c:("IMG"===c.nodeName||"DT"===c.nodeName||"A"===c.nodeName)&&(d=i.getParent(c,"div.mceTemp")),d))return i.events.cancel(b),k(c),!1}),tinymce.Env.gecko&&a.on("undo redo",function(){"IMG"===a.selection.getNode().nodeName&&a.selection.collapse()}),a.wpSetImgCaption=function(a){return e(a)},a.wpGetImgCaption=function(a){return f(a)},a.on("BeforeSetContent",function(b){"raw"!==b.format&&(b.content=a.wpSetImgCaption(b.content))}),a.on("PostProcess",function(b){b.get&&(b.content=a.wpGetImgCaption(b.content))}),{_do_shcode:e,_get_shcode:f}});