1 line
14 KiB
JavaScript
1 line
14 KiB
JavaScript
tinymce.PluginManager.add("wpeditimage",function(g){var o,t,n,a,e=tinymce.each,l=tinymce.trim,i=tinymce.Env.iOS;function c(e){return!(!g.dom.getAttrib(e,"data-mce-placeholder")&&!g.dom.getAttrib(e,"data-mce-object"))}function r(e){e=g.$(e).parents("[contenteditable]");return e&&"false"===e.attr("contenteditable")}function d(e){return e.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g,function(e,t,n){var a,i,o,c,r,d=t.match(/id=['"]([^'"]*)['"] ?/);return(r=(t=(i=(t=(a=(t=d?t.replace(d[0],""):t).match(/align=['"]([^'"]*)['"] ?/))?t.replace(a[0],""):t).match(/class=['"]([^'"]*)['"] ?/))?t.replace(i[0],""):t).match(/width=['"]([0-9]*)['"] ?/))&&(t=t.replace(r[0],"")),c=(c=(n=l(n)).match(/((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i))&&c[2]?(o=l(c[2]),l(c[1])):(o=l(t).replace(/caption=['"]/,"").replace(/['"]$/,""),n),d=d&&d[1]?d[1].replace(/[<>&]+/g,""):"",a=a&&a[1]?a[1]:"alignnone",i=i&&i[1]?" "+i[1].replace(/[<>&]+/g,""):"",(r=(r=!r&&c?c.match(/width=['"]([0-9]*)['"]/):r)&&r[1]?r[1]:r)&&o?(r=parseInt(r,10),g.getParam("wpeditimage_html5_captions")||(r+=10),'<div class="mceTemp"><dl id="'+d+'" class="wp-caption '+a+i+'" style="width: '+r+'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+o+"</dd></dl></div>"):n})}function s(e){return e.replace(/(?:<div [^>]+mceTemp[^>]+>)?\s*(<dl [^>]+wp-caption[^>]+>[\s\S]+?<\/dl>)\s*(?:<\/div>)?/g,function(e,t){var n="";return-1===t.indexOf("<img ")?(n=t.match(/<dd [^>]+>([\s\S]+?)<\/dd>/i))&&n[1]?"<p>"+n[1]+"</p>":"":-1===(n=t.replace(/\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi,function(e,t,n,a){var i,o,c=n.match(/width="([0-9]*)"/);return c=c&&c[1]?c[1]:"",o=(i=(i=t.match(/class="([^"]*)"/))&&i[1]?i[1]:"").match(/align[a-z]+/i)||"alignnone",c&&a?'[caption id="'+(t=(t=t.match(/id="([^"]*)"/))&&t[1]?t[1]:"")+'" align="'+o+'" width="'+c+'"'+(i=(i=i.replace(/wp-caption ?|align[a-z]+ ?/gi,""))&&' class="'+i+'"')+"]"+n+" "+(a=(a=a.replace(/\r\n|\r/g,"\n").replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g,function(e){return e.replace(/[\r\n\t]+/," ")})).replace(/\s*\n\s*/g,"<br />"))+"[/caption]":"alignnone"!==o[0]?n.replace(/><img/,' class="'+o[0]+'"><img'):n})).indexOf("[caption")?t.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi,"<p>$1</p>$2"):n})}function m(e){return e&&(e.textContent||e.innerText)}function u(e){return!e||-1===e.indexOf("<")&&-1===e.indexOf(">")?e:(t=t||new tinymce.html.Serializer({},g.schema)).serialize(g.parser.parse(e,{forced_root_block:!1}))}function p(e){var t=g.dom.getParent(e,"div.mceTemp");(t=!t&&"IMG"===e.nodeName?g.dom.getParent(e,"a"):t)?(t.nextSibling?g.selection.select(t.nextSibling):t.previousSibling?g.selection.select(t.previousSibling):g.selection.select(t.parentNode),g.selection.collapse(!0),g.dom.remove(t)):g.dom.remove(e),g.nodeChanged(),g.undoManager.add()}return g.addButton("wp_img_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){p(g.selection.getNode())}}),g.addButton("wp_img_edit",{tooltip:"Edit ",icon:"dashicon dashicons-edit",onclick:function(){var p,e,t;p=g.selection.getNode(),"undefined"!=typeof wp&&wp.media?(t=function(e){var t,n,a,i,o=[],c=g.dom,r=/^\d+$/;(n={attachment_id:!1,size:"custom",caption:"",align:"none",extraClasses:"",link:!1,linkUrl:"",linkClassName:"",linkTargetBlank:!1,linkRel:"",title:""}).url=c.getAttrib(e,"src"),n.alt=c.getAttrib(e,"alt"),n.title=c.getAttrib(e,"title"),a=c.getAttrib(e,"width"),i=c.getAttrib(e,"height"),(!r.test(a)||parseInt(a,10)<1)&&(a=e.naturalWidth||e.width);(!r.test(i)||parseInt(i,10)<1)&&(i=e.naturalHeight||e.height);n.customWidth=n.width=a,n.customHeight=n.height=i,a=tinymce.explode(e.className," "),t=[],tinymce.each(a,function(e){/^wp-image/.test(e)?n.attachment_id=parseInt(e.replace("wp-image-",""),10):/^align/.test(e)?n.align=e.replace("align",""):/^size/.test(e)?n.size=e.replace("size-",""):t.push(e)}),n.extraClasses=t.join(" "),(i=c.getParents(e,".wp-caption")).length&&(i=i[0],a=i.className.split(" "),tinymce.each(a,function(e){/^align/.test(e)?n.align=e.replace("align",""):e&&"wp-caption"!==e&&o.push(e)}),n.captionClassName=o.join(" "),(i=c.select("dd.wp-caption-dd",i)).length&&(i=i[0],n.caption=g.serializer.serialize(i).replace(/<br[^>]*>/g,"$&\n").replace(/^<p>/,"").replace(/<\/p>$/,"")));e.parentNode&&"A"===e.parentNode.nodeName&&(e=e.parentNode,n.linkUrl=c.getAttrib(e,"href"),n.linkTargetBlank="_blank"===c.getAttrib(e,"target"),n.linkRel=c.getAttrib(e,"rel"),n.linkClassName=e.className);return n}(p),wp.media.events.trigger("editor:image-edit",{editor:g,metadata:t,image:p}),e=wp.media({frame:"image",state:"image-details",metadata:t}),wp.media.events.trigger("editor:frame-create",{frame:e}),t=function(s){g.focus(),g.undoManager.transact(function(){var e,t,n,a,i,o,c,r,d,l;e=p,t=s,l=g.dom,(r=tinymce.explode(t.extraClasses," "))||(r=[]),t.caption||r.push("align"+t.align),t.attachment_id&&(r.push("wp-image-"+t.attachment_id),t.size&&"custom"!==t.size&&r.push("size-"+t.size)),o=t.width,d=t.height,"custom"===t.size&&(o=t.customWidth,d=t.customHeight),i={src:t.url,width:o||null,height:d||null,alt:t.alt,title:t.title||null,"class":r.join(" ")||null},l.setAttribs(e,i),c={href:t.linkUrl,rel:t.linkRel||null,target:t.linkTargetBlank?"_blank":null,"class":t.linkClassName||null},e.parentNode&&"A"===e.parentNode.nodeName&&!m(e.parentNode)?t.linkUrl?l.setAttribs(e.parentNode,c):l.remove(e.parentNode,!0):t.linkUrl&&((a=l.getParent(e,"a"))&&l.insertAfter(e,a),a=l.create("a",c),e.parentNode.insertBefore(a,e),a.appendChild(e)),d=g.dom.getParent(e,".mceTemp"),r=e.parentNode&&"A"===e.parentNode.nodeName&&!m(e.parentNode)?e.parentNode:e,t.caption?(t.caption=u(t.caption),i=t.attachment_id?"attachment_"+t.attachment_id:null,c="align"+(t.align||"none"),a="wp-caption "+c,t.captionClassName&&(a+=" "+t.captionClassName.replace(/[<>&]+/g,"")),g.getParam("wpeditimage_html5_captions")||(o=parseInt(o,10),o+=10),d?((c=l.select("dl.wp-caption",d)).length&&l.setAttribs(c,{id:i,"class":a,style:"width: "+o+"px"}),(c=l.select(".wp-caption-dd",d)).length&&l.setHTML(c[0],t.caption)):(n="<dl "+(i=i?'id="'+i+'" ':"")+'class="'+a+'" style="width: '+o+'px"><dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+t.caption+"</dd></dl>",o=l.create("div",{"class":"mceTemp"},n),(n=l.getParent(r,"p"))?n.parentNode.insertBefore(o,n):r.parentNode.insertBefore(o,r),g.$(o).find("dt.wp-caption-dt").append(r),n&&l.isEmpty(n)&&l.remove(n))):d&&(n=l.create("p"),d.parentNode.insertBefore(n,d),n.appendChild(r),l.remove(d)),r=g.$(e),l=r.attr("srcset"),d=r.attr("src"),l&&d&&(d=d.replace(/[?#].*/,""),-1===l.indexOf(d)&&r.attr("srcset",null).attr("sizes",null)),wp.media.events&&wp.media.events.trigger("editor:image-update",{editor:g,metadata:t,image:e}),g.nodeChanged()}),e.detach()},e.state("image-details").on("update",t),e.state("replace-image").on("replace",t),e.on("close",function(){g.focus(),e.detach()}),e.open()):g.execCommand("mceImage")}}),e({alignleft:"Align left",aligncenter:"Align center",alignright:"Align right",alignnone:"No alignment"},function(e,n){var t=n.slice(5);g.addButton("wp_img_"+n,{tooltip:e,icon:"dashicon dashicons-align-"+t,cmd:"alignnone"===n?"wpAlignNone":"Justify"+t.slice(0,1).toUpperCase()+t.slice(1),onPostRender:function(){var t=this;g.on("NodeChange",function(e){"IMG"===e.element.nodeName&&(e=g.dom.getParent(e.element,".wp-caption")||e.element,"alignnone"===n?t.active(!/\balign(left|center|right)\b/.test(e.className)):t.active(g.dom.hasClass(e,n)))})}})}),g.once("preinit",function(){g.wp&&g.wp._createToolbar&&(o=g.wp._createToolbar(["wp_img_alignleft","wp_img_aligncenter","wp_img_alignright","wp_img_alignnone","wp_img_edit","wp_img_remove"]))}),g.on("wptoolbar",function(e){"IMG"!==e.element.nodeName||c(e.element)||(e.toolbar=o)}),i&&g.on("init",function(){g.on("touchstart",function(e){"IMG"!==e.target.nodeName||r(e.target)||(n=!0)}),g.dom.bind(g.getDoc(),"touchmove",function(){n=!1}),g.on("touchend",function(e){var t;n&&"IMG"===e.target.nodeName&&!r(e.target)?(t=e.target,n=!1,window.setTimeout(function(){g.selection.select(t),g.nodeChanged()},100)):o&&o.hide()})}),g.on("init",function(){var m=g.dom,e=g.getParam("wpeditimage_html5_captions")?"html5-captions":"html4-captions";m.addClass(g.getBody(),e),g.on("wpLoadImageForm",function(e){g.getParam("wpeditimage_disable_captions")||e.data.splice(e.data.length-1,0,{type:"textbox",flex:1,name:"wpcaption",minHeight:60,multiline:!0,scroll:!0,label:"Image caption"})}),g.on("wpNewImageRefresh",function(e){var t;(t=m.getParent(e.node,"dl.wp-caption"))&&(t.style.width||(e=(e=parseInt(e.node.clientWidth,10)+10)?e+"px":"50%",m.setStyle(t,"width",e)))}),g.on("wpImageFormSubmit",function(e){var t,n,a,i,o,c=e.imgData.data,r=e.imgData.node,d=e.imgData.wpcaption,l="",s="",p="";c.id="__wp-temp-img-id",e.imgData.cancel=!0,c.style||(c.style=null),c.src?(d=d&&u(d=(d=d.replace(/\r\n|\r/g,"\n").replace(/<\/?[a-zA-Z0-9]+( [^<>]+)?>/g,function(e){return e.replace(/[\r\n\t]+/," ")})).replace(/(<br[^>]*>)\s*\n\s*/g,"$1").replace(/\s*\n\s*/g,"<br />")),r?(o=r.id||null,m.setAttribs(r,c),t=m.getParent(r,"dl.wp-caption"),d?t?(n=m.select("dd.wp-caption-dd",t)[0])&&(n.innerHTML=d):(r.className&&(l=r.className.match(/wp-image-([0-9]+)/),s=r.className.match(/align(left|right|center|none)/)),s?(s=s[0],r.className=r.className.replace(/align(left|right|center|none)/g,"")):s="alignnone",s=' class="wp-caption '+s+'"',l=l&&' id="attachment_'+l[1]+'"',(p=c.width||r.clientWidth)&&(p=parseInt(p,10),g.getParam("wpeditimage_html5_captions")||(p+=10),p=' style="width: '+p+'px"'),a=r.parentNode&&"A"===r.parentNode.nodeName?r.parentNode:r,i="<dl "+l+s+p+'><dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+d+"</dd></dl>",t=m.create("div",{"class":"mceTemp"},i),(n=m.getParent(a,"p"))?n.parentNode.insertBefore(t,n):a.parentNode.insertBefore(t,a),g.$(t).find("dt.wp-caption-dt").append(a),n&&m.isEmpty(n)&&m.remove(n)):t&&(i="A"===r.parentNode.nodeName?m.getOuterHTML(r.parentNode):m.getOuterHTML(r),n=m.create("p",{},i),m.insertAfter(n,t.parentNode),g.selection.select(n),g.nodeChanged(),m.remove(t.parentNode))):(i=m.createHTML("img",c),d?(a=g.selection.getNode(),c.width&&(p=parseInt(c.width,10),g.getParam("wpeditimage_html5_captions")||(p+=10),p=' style="width: '+p+'px"'),i='<dl class="wp-caption alignnone"'+p+'><dt class="wp-caption-dt">'+i+'</dt><dd class="wp-caption-dd">'+d+"</dd></dl>",(n="P"===a.nodeName?a:m.getParent(a,"p"))&&"P"===n.nodeName?(t=m.create("div",{"class":"mceTemp"},i),n.parentNode.insertBefore(t,n),g.selection.select(t),g.nodeChanged(),m.isEmpty(n)&&m.remove(n)):g.selection.setContent('<div class="mceTemp">'+i+"</div>")):g.selection.setContent(i)),r=m.get("__wp-temp-img-id"),m.setAttrib(r,"id",o),e.imgData.node=r):r&&((t=m.getParent(r,"div.mceTemp"))?m.remove(t):"A"===r.parentNode.nodeName?m.remove(r.parentNode):m.remove(r),g.nodeChanged())}),g.on("wpLoadImageData",function(e){var t=e.imgData.data,e=e.imgData.node;(e=m.getParent(e,"dl.wp-caption"))&&(e=m.select("dd.wp-caption-dd",e)[0])&&(t.wpcaption=g.serializer.serialize(e).replace(/<br[^>]*>/g,"$&\n").replace(/^<p>/,"").replace(/<\/p>$/,""))}),tinymce.Env.ie&&10<tinymce.Env.ie&&m.bind(g.getBody(),"mscontrolselect",function(e){"IMG"===e.target.nodeName&&m.getParent(e.target,".wp-caption")?g.getBody().focus():"DL"===e.target.nodeName&&m.hasClass(e.target,"wp-caption")&&e.target.focus()})}),g.on("ObjectResized",function(a){var i=a.target;"IMG"===i.nodeName&&g.undoManager.transact(function(){var e,t,n=g.dom;i.className=i.className.replace(/\bsize-[^ ]+/,""),(e=n.getParent(i,".wp-caption"))&&(t=a.width||n.getAttrib(i,"width"))&&(t=parseInt(t,10),g.getParam("wpeditimage_html5_captions")||(t+=10),n.setStyle(e,"width",t+"px"))})}),g.on("BeforeExecCommand",function(e){var t,n,a=e.command,i=g.dom;"mceInsertContent"===a?(t=i.getParent(g.selection.getNode(),"div.mceTemp"))&&(n=i.create("p"),i.insertAfter(n,t),g.selection.setCursorLocation(n,0),g.nodeChanged()):"JustifyLeft"!==a&&"JustifyRight"!==a&&"JustifyCenter"!==a&&"wpAlignNone"!==a||(t=g.selection.getNode(),i="align"+a.slice(7).toLowerCase(),n=g.dom.getParent(t,".wp-caption"),"IMG"!==t.nodeName&&!n||(t=n||t,i=g.dom.hasClass(t,i)?" alignnone":" "+i,t.className=l(t.className.replace(/ ?align(left|center|right|none)/g,"")+i),g.nodeChanged(),e.preventDefault(),o&&o.reposition(),g.fire("ExecCommand",{command:a,ui:e.ui,value:e.value})))}),g.on("keydown",function(e){var t,n,a,i=g.selection,o=e.keyCode,c=g.dom,r=tinymce.util.VK;if(o===r.ENTER)t=i.getNode(),(n=c.getParent(t,"div.mceTemp"))&&(c.events.cancel(e),tinymce.each(c.select("dt, dd",n),function(e){c.isEmpty(e)&&c.remove(e)}),a=tinymce.Env.ie&&tinymce.Env.ie<11?"":'<br data-mce-bogus="1" />',a=c.create("p",null,a),"DD"===t.nodeName?c.insertAfter(a,n):n.parentNode.insertBefore(a,n),g.nodeChanged(),i.setCursorLocation(a,0));else if((o===r.DELETE||o===r.BACKSPACE)&&("DIV"===(t=i.getNode()).nodeName&&c.hasClass(t,"mceTemp")?n=t:"IMG"!==t.nodeName&&"DT"!==t.nodeName&&"A"!==t.nodeName||(n=c.getParent(t,"div.mceTemp")),n))return c.events.cancel(e),p(t),!1}),tinymce.Env.gecko&&g.on("undo redo",function(){"IMG"===g.selection.getNode().nodeName&&g.selection.collapse()}),g.wpSetImgCaption=d,g.wpGetImgCaption=s,g.on("BeforeSetContent",function(e){"raw"!==e.format&&(e.content=g.wpSetImgCaption(e.content))}),g.on("PostProcess",function(e){e.get&&(e.content=g.wpGetImgCaption(e.content))}),g.on("dragstart",function(){var e=g.selection.getNode();"IMG"===e.nodeName&&((a=g.dom.getParent(e,".mceTemp"))||"A"!==e.parentNode.nodeName||m(e.parentNode)||(a=e.parentNode))}),g.on("drop",function(e){var t=g.dom,n=tinymce.dom.RangeUtils.getCaretRangeFromPoint(e.clientX,e.clientY,g.getDoc());n&&t.getParent(n.startContainer,".mceTemp")?e.preventDefault():a&&(e.preventDefault(),g.undoManager.transact(function(){n&&g.selection.setRng(n),g.selection.setNode(a),t.remove(a)})),a=null}),g.wp=g.wp||{},g.wp.isPlaceholder=c,{_do_shcode:d,_get_shcode:s}}); |