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

1 line
13 KiB
JavaScript

tinymce.PluginManager.add("wpeditimage",function(a){function b(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(/width=['"]([0-9]*)['"] ?/),g&&(c=c.replace(g[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]:"",f=f&&f[1]?f[1]:"alignnone",!g&&i&&(g=i.match(/width=['"]([0-9]*)['"]/)),g&&g[1]&&(g=g[1]),g&&h?(j=parseInt(g,10),a.getParam("wpeditimage_html5_captions")||(j+=10),'<div class="mceTemp"><dl id="'+e+'" class="wp-caption '+f+'" style="width: '+j+'px"><dt class="wp-caption-dt">'+i+'</dt><dd class="wp-caption-dd">'+h+"</dd></dl></div>"):d})}function c(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(/<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi,function(a,b,c,d){var e,f,g;return g=c.match(/width="([0-9]*)"/),g=g&&g[1]?g[1]:"",g&&d?(e=b.match(/id="([^"]*)"/),e=e&&e[1]?e[1]:"",f=b.match(/class="([^"]*)"/),f=f&&f[1]?f[1]:"",f=f.match(/align[a-z]+/)||"alignnone",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="'+f+'" width="'+g+'"]'+c+" "+d+"[/caption]"):c}),0!==c.indexOf("[caption")&&(c=b.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi,"<p>$1</p>$2")),c)})}function d(b){var c,d,e,f,g,h,i,j,k=a.dom,l=/^\d+$/;return e={attachment_id:!1,size:"custom",caption:"",align:"none",extraClasses:"",link:!1,linkUrl:"",linkClassName:"",linkTargetBlank:!1,linkRel:"",title:""},e.url=k.getAttrib(b,"src"),e.alt=k.getAttrib(b,"alt"),e.title=k.getAttrib(b,"title"),i=k.getAttrib(b,"width"),j=k.getAttrib(b,"height"),(!l.test(i)||parseInt(i,10)<1)&&(i=b.naturalWidth||b.width),(!l.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=k.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",""))}),g=k.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=k.getAttrib(h,"href"),e.linkTargetBlank="_blank"===k.getAttrib(h,"target")?!0:!1,e.linkRel=k.getAttrib(h,"rel"),e.linkClassName=h.className),e}function e(a){return a&&!(!a.textContent&&!a.innerText)}function f(b){return!b||-1===b.indexOf("<")&&-1===b.indexOf(">")?b:(m||(m=new tinymce.html.Serializer({},a.schema)),m.serialize(a.parser.parse(b,{forced_root_block:!1})))}function g(b,c){var d,g,h,i,k,l,m,n,o,p,q,r,s,t,u,v=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)),t=c.width,u=c.height,"custom"===c.size&&(t=c.customWidth,u=c.customHeight),r={src:c.url,width:t||null,height:u||null,alt:c.alt,title:c.title||null,"class":d.join(" ")||null},v.setAttribs(b,r),s={href:c.linkUrl,rel:c.linkRel||null,target:c.linkTargetBlank?"_blank":null,"class":c.linkClassName||null},b.parentNode&&"A"===b.parentNode.nodeName&&!e(b.parentNode)?c.linkUrl?v.setAttribs(b.parentNode,s):v.remove(b.parentNode,!0):c.linkUrl&&((m=v.getParent(b,"a"))&&v.insertAfter(b,m),m=v.create("a",s),b.parentNode.insertBefore(m,b),m.appendChild(b)),n=a.dom.getParent(b,".mceTemp"),h=b.parentNode&&"A"===b.parentNode.nodeName&&!e(b.parentNode)?b.parentNode:b,c.caption?(c.caption=f(c.caption),q=c.attachment_id?"attachment_"+c.attachment_id:null,g="wp-caption align"+(c.align||"none"),a.getParam("wpeditimage_html5_captions")||(t=parseInt(t,10),t+=10),n?(p=v.select("dl.wp-caption",n),p.length&&v.setAttribs(p,{id:q,"class":g,style:"width: "+t+"px"}),o=v.select(".wp-caption-dd",n),o.length&&v.setHTML(o[0],c.caption)):(q=q?'id="'+q+'" ':"",i="<dl "+q+'class="'+g+'" style="width: '+t+'px"><dt class="wp-caption-dt">'+v.getOuterHTML(h)+'</dt><dd class="wp-caption-dd">'+c.caption+"</dd></dl>",(k=v.getParent(h,"p"))?(l=v.create("div",{"class":"mceTemp"},i),k.parentNode.insertBefore(l,k),v.remove(h),v.isEmpty(k)&&v.remove(k)):v.setOuterHTML(h,'<div class="mceTemp">'+i+"</div>"))):n&&(k=v.create("p"),n.parentNode.insertBefore(k,n),k.appendChild(h),v.remove(n)),wp.media.events&&wp.media.events.trigger("editor:image-update",{editor:a,metadata:c,image:b}),a.nodeChanged(),j(b)}function h(b){var c,e,f;return"undefined"!=typeof wp&&wp.media?(f=d(b),wp.media.events.trigger("editor:image-edit",{editor:a,metadata:f,image:b}),c=wp.media({frame:"image",state:"image-details",metadata:f}),wp.media.events.trigger("editor:frame-create",{frame:c}),e=function(d){a.focus(),a.undoManager.transact(function(){g(b,d)}),c.detach()},c.state("image-details").on("update",e),c.state("replace-image").on("replace",e),c.on("close",function(){a.focus(),c.detach()}),void c.open()):void a.execCommand("mceImage")}function i(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.nodeChanged(),a.dom.remove(c)):a.dom.remove(b),k()}function j(b){var c,d,e,f,g=a.dom;k(),b&&"IMG"===b.nodeName&&!l(b)&&(g.setAttrib(b,"data-wp-imgselect",1),c=g.getRect(b),d='<div class="dashicons dashicons-edit edit" data-mce-bogus="1"></div><div class="dashicons dashicons-no-alt remove" data-mce-bogus="1"></div>',e=g.create("div",{id:"wp-image-toolbar","data-mce-bogus":"1",contenteditable:!1},d),f=a.rtl?c.x+c.w-82:c.x,a.getBody().appendChild(e),g.setStyles(e,{top:c.y,left:f}),n=!0)}function k(){var b=a.dom.get("wp-image-toolbar");b&&a.dom.remove(b),a.dom.setAttrib(a.dom.select("img[data-wp-imgselect]"),"data-wp-imgselect",null),n=!1}function l(b){var c=a.dom;return c.hasClass(b,"mceItem")||c.getAttrib(b,"data-mce-placeholder")||c.getAttrib(b,"data-mce-object")?!0:!1}var m,n=!1;return 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,g,h,i,j=c.imgData.data,k=c.imgData.node,l=c.imgData.caption,m="",n="",o="";return j.id="__wp-temp-img-id",c.imgData.cancel=!0,j.style||(j.style=null),j.src?(l&&(l=l.replace(/\r\n|\r/g,"\n").replace(/<\/?[a-zA-Z0-9]+( [^<>]+)?>/g,function(a){return a.replace(/[\r\n\t]+/," ")}),l=l.replace(/(<br[^>]*>)\s*\n\s*/g,"$1").replace(/\s*\n\s*/g,"<br />"),l=f(l)),k?(i=k.id||null,b.setAttribs(k,j),d=b.getParent(k,"dl.wp-caption"),l?d?(e=b.select("dd.wp-caption-dd",d)[0])&&(e.innerHTML=l):(k.className&&(m=k.className.match(/wp-image-([0-9]+)/),n=k.className.match(/align(left|right|center|none)/)),n?(n=n[0],k.className=k.className.replace(/align(left|right|center|none)/g,"")):n="alignnone",n=' class="wp-caption '+n+'"',m&&(m=' id="attachment_'+m[1]+'"'),o=j.width||k.clientWidth,o&&(o=parseInt(o,10),a.getParam("wpeditimage_html5_captions")||(o+=10),o=' style="width: '+o+'px"'),k.parentNode&&"A"===k.parentNode.nodeName?(h=b.getOuterHTML(k.parentNode),g=k.parentNode):(h=b.getOuterHTML(k),g=k),h="<dl "+m+n+o+'><dt class="wp-caption-dt">'+h+'</dt><dd class="wp-caption-dd">'+l+"</dd></dl>",(e=b.getParent(k,"p"))?(d=b.create("div",{"class":"mceTemp"},h),b.insertAfter(d,e),a.selection.select(d),a.nodeChanged(),b.remove(g),b.isEmpty(e)&&b.remove(e)):a.selection.setContent('<div class="mceTemp">'+h+"</div>")):d&&(h=b.getOuterHTML("A"===k.parentNode.nodeName?k.parentNode:k),e=b.create("p",{},h),b.insertAfter(e,d.parentNode),a.selection.select(e),a.nodeChanged(),b.remove(d.parentNode))):(h=b.createHTML("img",j),l?(g=a.selection.getNode(),j.width&&(o=parseInt(j.width,10),a.getParam("wpeditimage_html5_captions")||(o+=10),o=' style="width: '+o+'px"'),h='<dl class="wp-caption alignnone"'+o+'><dt class="wp-caption-dt">'+h+'</dt><dd class="wp-caption-dd">'+l+"</dd></dl>",e="P"===g.nodeName?g:b.getParent(g,"p"),e&&"P"===e.nodeName?(d=b.create("div",{"class":"mceTemp"},h),e.parentNode.insertBefore(d,e),a.selection.select(d),a.nodeChanged(),b.isEmpty(e)&&b.remove(e)):a.selection.setContent('<div class="mceTemp">'+h+"</div>")):a.selection.setContent(h)),k=b.get("__wp-temp-img-id"),b.setAttrib(k,"id",i),void(c.imgData.node=k)):void(k&&(b.remove((d=b.getParent(k,"div.mceTemp"))?d:"A"===k.parentNode.nodeName?k.parentNode:k),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(),k()}),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("click",function(c){"IMG"===c.target.nodeName&&b.getAttrib(c.target,"data-wp-imgselect")&&b.getParent(c.target,"dl.wp-caption")&&a.getBody().focus()}))}),a.on("ObjectResized",function(b){var c,d,e=b.target,f=a.dom;"IMG"===e.nodeName&&(e.className=e.className.replace(/\bsize-[^ ]+/,""),(c=f.getParent(e,".wp-caption"))&&(d=b.width||f.getAttrib(e,"width"),d&&(d=parseInt(d,10),a.getParam("wpeditimage_html5_captions")||(d+=10),f.setStyle(c,"width",d+"px"))),j(e))}),a.on("BeforeExecCommand",function(b){var c,d,e,f,g=b.command,h=a.dom;if("mceInsertContent"===g){if((c=h.getParent(a.selection.getNode(),"div.mceTemp"))&&(d=h.create("p"),h.insertAfter(d,c),a.selection.setCursorLocation(d,0),a.nodeChanged(),tinymce.Env.ie>8))return setTimeout(function(){a.selection.setCursorLocation(d,0),a.selection.setContent(b.value)},500),!1}else if("JustifyLeft"===g||"JustifyRight"===g||"JustifyCenter"===g){if(c=a.selection.getNode(),f=g.substr(7).toLowerCase(),f="align"+f,k(),e=h.is(c,"dl.wp-caption")?c:h.getParent(c,"dl.wp-caption"))return h.hasClass(e,f)?(h.removeClass(e,f),h.addClass(e,"alignnone")):(e.className=e.className.replace(/align[^ ]+/g,""),h.addClass(e,f)),!1;"IMG"===c.nodeName&&(h.hasClass(c,f)?h.addClass(c,"alignnone"):h.removeClass(c,"alignnone"))}}),a.on("keydown",function(b){var c,d,e,f,g=a.selection,h=b.keyCode,j=a.dom;if(h===tinymce.util.VK.ENTER)c=g.getNode(),d=j.getParent(c,"div.mceTemp"),d&&(j.events.cancel(b),tinymce.each(j.select("dt, dd",d),function(a){j.isEmpty(a)&&j.remove(a)}),f=tinymce.Env.ie&&tinymce.Env.ie<11?"":'<br data-mce-bogus="1" />',e=j.create("p",null,f),"DD"===c.nodeName?j.insertAfter(e,d):d.parentNode.insertBefore(e,d),a.nodeChanged(),g.setCursorLocation(e,0));else if(h===tinymce.util.VK.DELETE||h===tinymce.util.VK.BACKSPACE){if(c=g.getNode(),"DIV"===c.nodeName&&j.hasClass(c,"mceTemp")?d=c:("IMG"===c.nodeName||"DT"===c.nodeName||"A"===c.nodeName)&&(d=j.getParent(c,"div.mceTemp")),d)return j.events.cancel(b),i(c),!1;k()}if(n){if(b.ctrlKey||b.metaKey||b.altKey||48>h&&h>90||h>186)return;k()}}),a.on("mousedown",function(b){a.dom.getParent(b.target,"#wp-image-toolbar")?tinymce.Env.ie&&b.preventDefault():"IMG"!==b.target.nodeName&&k()}),a.on("mouseup",function(b){var c,d=b.target,e=a.dom;b.button&&b.button>1||("DIV"===d.nodeName&&e.getParent(d,"#wp-image-toolbar")?(c=e.select("img[data-wp-imgselect]")[0],c&&(a.selection.select(c),e.hasClass(d,"remove")?i(c):e.hasClass(d,"edit")&&h(c))):"IMG"!==d.nodeName||a.dom.getAttrib(d,"data-wp-imgselect")||l(d)?"IMG"!==d.nodeName&&k():j(d))}),a.on("cut",function(){k()}),a.wpSetImgCaption=function(a){return b(a)},a.wpGetImgCaption=function(a){return c(a)},a.on("BeforeSetContent",function(b){b.content=a.wpSetImgCaption(b.content)}),a.on("PostProcess",function(b){b.get&&(b.content=a.wpGetImgCaption(b.content),b.content=b.content.replace(/ data-wp-imgselect="1"/g,""))}),{_do_shcode:b,_get_shcode:c}});