1 line
8.1 KiB
JavaScript
1 line
8.1 KiB
JavaScript
tinymce.PluginManager.add("wpview",function(a){function b(a){return c(a,"wpview-wrap")}function c(a,b){for(;a&&a.parentNode;){if(a.className&&-1!==(" "+a.className+" ").indexOf(" "+b+" "))return a;a=a.parentNode}return!1}function d(a){a.stopPropagation()}function e(b,c){var d=b?"before":"after",e=b?0:1;i(),a.selection.setCursorLocation(a.dom.select(".wpview-selection-"+d,c)[0],e),a.nodeChanged()}function f(b,c,d){var f=a.dom,g=f.create("p");t.ie&&t.ie<11||(g.innerHTML='<br data-mce-bogus="1">'),c?b.parentNode.insertBefore(g,b):f.insertAfter(g,b),i(),c&&d===u.ENTER?e(c,b):a.selection.setCursorLocation(g,0),a.nodeChanged()}function g(b){a.undoManager.transact(function(){f(b),wp.mce.views.remove(a,b)})}function h(b){var c,e=a.dom;b&&(b!==l&&(a.getBody().focus(),i(),l=b,e.setAttrib(b,"data-mce-selected",1),c=e.create("div",{"class":"wpview-clipboard",contenteditable:"true"},wp.mce.views.getText(b)),a.dom.select(".wpview-body",b)[0].appendChild(c),e.bind(c,"beforedeactivate focusin focusout",d),e.bind(l,"beforedeactivate focusin focusout",d),z?a.selection.select(c):a.selection.select(c,!0)),a.nodeChanged(),a.fire("wpview-selected",b))}function i(){var b,c=a.dom;l&&(b=a.dom.select(".wpview-clipboard",l)[0],c.unbind(b),c.remove(b),c.unbind(l,"beforedeactivate focusin focusout click mouseup",d),c.setAttrib(l,"data-mce-selected",null)),l=null}function j(a){return a.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?: |\u00a0)*<\/p><\/div>/g,"$1")}function k(a){return 47>=a&&a!==u.SPACEBAR&&a!==u.ENTER&&a!==u.DELETE&&a!==u.BACKSPACE&&(37>a||a>40)||a>=224||a>=144&&150>=a||a>=91&&93>=a||a>=112&&135>=a}var l,m,n,o,p,q,r,s,t=tinymce.Env,u=tinymce.util.VK,v=tinymce.dom.TreeWalker,w=!1,x=!0,y=function(){return!1},z=/iPad|iPod|iPhone/.test(navigator.userAgent);return"undefined"!=typeof wp&&wp.mce?(a.on("BeforeAddUndo",function(a){a.lastLevel&&j(a.level.content)===j(a.lastLevel.content)&&a.preventDefault()}),a.on("BeforeSetContent",function(b){var c;if(b.content){if(l&&g(l),!b.load&&(c=a.selection.getNode(),c&&c!==a.getBody()&&/^\s*https?:\/\/\S+\s*$/i.test(b.content))){if(c=a.dom.getParent(c,"p"),!c||!/^[\s\uFEFF\u00A0]*$/.test(c.textContent||c.innerText))return;c.innerHTML=""}b.content=wp.mce.views.setMarkers(b.content)}}),a.on("pastePreProcess",function(a){var b=a.content;b&&(b=tinymce.trim(b.replace(/<[^>]+>/g,"")),/^https?:\/\/\S+$/i.test(b)&&(a.content=b))}),a.on("SetContent",function(){wp.mce.views.render()}),a.on("click",function(c){var d,f,g,h=c.clientX,i=c.clientY,j=a.getBody(),k=j.getBoundingClientRect(),l=j.firstChild,m=j.lastChild;l&&m&&(d=l.getBoundingClientRect(),f=m.getBoundingClientRect(),i<d.top&&(g=b(l))?(e(!0,g),c.preventDefault()):i>f.bottom&&(g=b(m))?(e(!1,g),c.preventDefault()):(h<k.left||h>k.right)&&tinymce.each(a.dom.select(".wpview-wrap"),function(a){var b=a.getBoundingClientRect();return i<b.top?!1:i>=b.top&&i<=b.bottom?(h<k.left?(e(!0,a),c.preventDefault()):h>k.right&&(e(!1,a),c.preventDefault()),!1):void 0}))}),a.on("init",function(){var c=!1,d=a.selection,e=window.MutationObserver||window.WebKitMutationObserver;a.on("BeforeSetContent",function(){var c,e,f=b(d.getNode());f&&(!f.nextSibling||b(f.nextSibling)?(e=a.getDoc().createTextNode(""),a.dom.insertAfter(e,f)):(c=new v(f.nextSibling,f.nextSibling),e=c.next()),d.select(e),d.collapse(!0))}),a.dom.bind(a.getDoc(),"touchmove",function(){c=!0}),a.on("mousedown mouseup click touchend",function(a){var d=b(a.target);return x=!1,d?(a.stopImmediatePropagation(),a.preventDefault(),"touchend"===a.type&&c?c=!1:h(d),!1):(("touchend"===a.type||"mousedown"===a.type)&&i(),void("touchend"===a.type&&c&&(c=!1)))},!0),e&&new e(function(){a.fire("wp-body-class-change")}).observe(a.getBody(),{attributes:!0,attributeFilter:["class"]})}),a.on("PreProcess",function(b){tinymce.each(a.dom.select("div[data-wpview-text]",b.node),function(a){a.textContent=a.innerText=" "})}),a.on("PostProcess",function(a){a.content&&(a.content=a.content.replace(/<div [^>]*?data-wpview-text="([^"]*)"[^>]*>[\s\S]*?<\/div>/g,function(a,b){return b?"<p>"+window.decodeURIComponent(b)+"</p>":""}))}),a.on("keydown",function(c){var d,j,m,o,p,q,r,s=c.keyCode,t=a.dom,v=a.selection;if(l){if((c.metaKey||c.ctrlKey)&&s!==u.BACKSPACE&&86!==s||s>=112&&123>=s)return void((c.metaKey||c.ctrlKey)&&88===s&&(w=l));if(j=b(v.getNode()),j!==l)return void i();s===u.LEFT?(e(!0,j),c.preventDefault()):s===u.UP?(j.previousSibling?b(j.previousSibling)?e(!0,j.previousSibling):(i(),v.select(j.previousSibling,!0),v.collapse()):e(!0,j),c.preventDefault()):s===u.RIGHT?(e(!1,j),c.preventDefault()):s===u.DOWN?(j.nextSibling?b(j.nextSibling)?e(!1,j.nextSibling):(i(),v.setCursorLocation(j.nextSibling,0)):e(!1,j),c.preventDefault()):k(s)||(g(l),(s===u.ENTER||s===u.DELETE||s===u.BACKSPACE)&&c.preventDefault())}else{if(c.metaKey||c.ctrlKey||s>=112&&123>=s)return;if(d=v.getNode(),n=d,j=b(d),v.isCollapsed()||(p=v.getRng(),(j=b(p.endContainer))?(q=p.cloneRange(),v.select(j.previousSibling,!0),v.collapse(),r=v.getRng(),q.setEnd(r.endContainer,r.endOffset),v.setRng(q)):(j=b(p.startContainer))&&(q=p.cloneRange(),q.setStart(j.nextSibling,0),v.setRng(q))),!j)return void(c.keyCode===u.BACKSPACE&&(a.dom.isEmpty(d)?(j=b(d.previousSibling))&&(e(!1,j),a.dom.remove(d),c.preventDefault()):(p=v.getRng())&&0===p.startOffset&&0===p.endOffset&&(j=b(d.previousSibling))&&(e(!1,j),c.preventDefault())));if(!(m=t.hasClass(j,"wpview-selection-before"))&&!(o=t.hasClass(j,"wpview-selection-after")))return;if(k(s))return;o&&s===u.UP||m&&s===u.BACKSPACE?(j.previousSibling?b(j.previousSibling)?e(!1,j.previousSibling):t.isEmpty(j.previousSibling)&&s===u.BACKSPACE?t.remove(j.previousSibling):(v.select(j.previousSibling,!0),v.collapse()):e(!0,j),c.preventDefault()):!o||s!==u.DOWN&&s!==u.RIGHT?!m||s!==u.UP&&s!==u.LEFT?m&&s===u.DOWN?(j.nextSibling?b(j.nextSibling)?e(!0,j.nextSibling):v.setCursorLocation(j.nextSibling,0):e(!1,j),c.preventDefault()):o&&s===u.LEFT||m&&s===u.RIGHT?(h(j),c.preventDefault()):o&&s===u.BACKSPACE?(g(j),c.preventDefault()):o?f(j):m&&f(j,!0,s):(j.previousSibling&&(b(j.previousSibling)?e(s===u.UP,j.previousSibling):(v.select(j.previousSibling,!0),v.collapse())),c.preventDefault()):(j.nextSibling&&(b(j.nextSibling)?e(s===u.RIGHT,j.nextSibling):v.setCursorLocation(j.nextSibling,0)),c.preventDefault()),s===u.ENTER&&c.preventDefault()}}),a.on("keyup",function(){w&&(g(w),w=!1)}),a.on("focus",function(){var c;p=!0,a.dom.addClass(a.getBody(),"has-focus"),x&&(c=b(a.getBody().firstChild))&&e(!0,c),x=!1}),a.on("blur",function(){p=!1,a.dom.removeClass(a.getBody(),"has-focus")}),a.on("NodeChange",function(d){var f=a.dom,g=a.dom.select(".wpview-wrap"),h=d.element.className,j=b(d.element),k=n;if(n=!1,clearInterval(m),tinymce.each(g,function(a){a.className&&(a.className=a.className.replace(/ ?\bwpview-(?:selection-before|selection-after|cursor-hide)\b/g,""))}),p&&j)if("wpview-selection-before"!==h&&"wpview-selection-after"!==h||!a.selection.isCollapsed())c(d.element,"wpview-clipboard")||o||(i(),o++,e(!0,j));else{if(o=0,i(),k===j.previousSibling)return void e(!0,j);if(k===j.nextSibling)return void e(!1,j);f.addClass(j,h),m=setInterval(function(){f.hasClass(j,"wpview-cursor-hide")?f.removeClass(j,"wpview-cursor-hide"):f.addClass(j,"wpview-cursor-hide")},500)}}),a.on("BeforeExecCommand",function(){var c,d=a.selection.getNode();d&&((r="wpview-selection-before"===d.className)||"wpview-selection-after"===d.className)&&(c=b(d))&&(f(c,r),q=c)}),a.on("ExecCommand",function(){var b,c;l&&(b=l,i(),h(b)),q&&(c=q[r?"previousSibling":"nextSibling"],c&&"P"===c.nodeName&&a.dom.isEmpty(c)&&(a.dom.remove(c),e(r,q)),q=!1)}),a.on("ResolveName",function(c){a.dom.hasClass(c.target,"wpview-wrap")?(c.name=a.dom.getAttrib(c.target,"data-wpview-type")||"wpview",c.stopPropagation()):b(c.target)&&(c.preventDefault(),c.stopPropagation())}),a.addButton("wp_view_edit",{tooltip:"Edit ",icon:"dashicon dashicons-edit",onclick:function(){l&&wp.mce.views.edit(a,l)}}),a.addButton("wp_view_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){l&&g(l)}}),a.once("preinit",function(){s=a.wp._createToolbar(["wp_view_edit","wp_view_remove"])}),a.on("wptoolbar",function(a){l&&(a.element=l,a.toolbar=s)}),a.wp=a.wp||{},a.wp.getView=b,{getView:b}):{getView:y}}); |