mirror of
https://github.com/WordPress/WordPress.git
synced 2024-09-10 14:48:07 +02:00
Built from https://develop.svn.wordpress.org/trunk@29184 git-svn-id: http://core.svn.wordpress.org/trunk@28968 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
7.2 KiB
JavaScript
1 line
7.2 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(c){return(c=b(c))?window.decodeURIComponent(a.dom.getAttrib(c,"data-wpview-text")||""):""}function e(c,d){return c=b(c),c?(a.dom.setAttrib(c,"data-wpview-text",window.encodeURIComponent(d||"")),!0):!1}function f(a){a.stopPropagation()}function g(b,c){var d=b?"before":"after",e=b?0:1;j(),a.selection.setCursorLocation(a.dom.select(".wpview-selection-"+d,c)[0],e),a.nodeChanged()}function h(b,c,d){var e=a.dom,f=e.create("p");r.ie&&r.ie<11||(f.innerHTML='<br data-mce-bogus="1">'),c?b.parentNode.insertBefore(f,b):e.insertAfter(f,b),j(),c&&d===s.ENTER?g(c,b):a.selection.setCursorLocation(f,0),a.nodeChanged()}function i(b){var c,e=a.dom;b!==l&&(j(),l=b,e.setAttrib(b,"data-mce-selected",1),c=e.create("div",{"class":"wpview-clipboard",contenteditable:"true"},d(b)),a.dom.select(".wpview-body",b)[0].appendChild(c),e.bind(c,"beforedeactivate focusin focusout",f),e.bind(l,"beforedeactivate focusin focusout",f),a.getBody().focus(),a.selection.select(c,!0),a.nodeChanged())}function j(){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",f),c.setAttrib(l,"data-mce-selected",null)),l=null}function k(a){return a.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?: |\u00a0)*<\/p><\/div>/g,"$1")}var l,m,n,o,p,q,r=tinymce.Env,s=tinymce.util.VK,t=tinymce.dom.TreeWalker,u=!1,v=!0;if("undefined"!=typeof wp&&wp.mce)return a.on("BeforeAddUndo",function(a){a.lastLevel&&k(a.level.content)===k(a.lastLevel.content)&&a.preventDefault()}),a.on("BeforeSetContent",function(b){var c;b.content&&(b.initial||wp.mce.views.unbind(a),c=a.selection.getNode(),(!b.content.match(/^\s*(https?:\/\/[^\s"]+)\s*$/i)||"P"===c.nodeName&&c.parentNode===a.getBody()&&a.dom.isEmpty(c))&&(b.content=wp.mce.views.toViews(b.content)))}),a.on("SetContent",function(){wp.mce.views.render()}),a.on("click",function(c){var d,e=c.clientX,f=c.clientY,h=a.getBody(),i=h.getBoundingClientRect(),j=h.firstChild,k=j.getBoundingClientRect(),l=h.lastChild,m=l.getBoundingClientRect();f<k.top&&(d=b(j))?(g(!0,d),c.preventDefault()):f>m.bottom&&(d=b(l))?(g(!1,d),c.preventDefault()):tinymce.each(a.dom.select(".wpview-wrap"),function(a){var b=a.getBoundingClientRect();return f>=b.top&&f<=b.bottom?void(e<i.left?(g(!0,a),c.preventDefault()):e>i.right&&(g(!1,a),c.preventDefault())):void 0})}),a.on("init",function(){var c=a.selection;a.on("BeforeSetContent",function(){var d,e,f=b(c.getNode());f&&(!f.nextSibling||b(f.nextSibling)?(e=a.getDoc().createTextNode(""),a.dom.insertAfter(e,f)):(d=new t(f.nextSibling,f.nextSibling),e=d.next()),c.select(e),c.collapse(!0))}),a.dom.bind(a.getBody().parentNode,"mousedown mouseup click",function(c){var d,e=b(c.target);return v=!1,e?(c.stopPropagation(),r.ie<=10&&j(),i(e),"click"!==c.type||c.metaKey||c.ctrlKey||(a.dom.hasClass(c.target,"edit")?wp.mce.views.edit(e):a.dom.hasClass(c.target,"remove")&&a.dom.remove(e)),!1):(d=r.ie&&r.ie<=8?"mouseup":"mousedown",void(c.type===d&&j()))})}),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){if(!(c.metaKey||c.ctrlKey||m>=112&&123>=m||l)){var d,e,f,j,k,m=c.keyCode,o=a.dom,p=a.selection,q=p.getNode(),r=b(q);n=q,p.isCollapsed()||(f=p.getRng(),(r=b(f.endContainer))?(j=f.cloneRange(),p.select(r.previousSibling,!0),p.collapse(),k=p.getRng(),j.setEnd(k.endContainer,k.endOffset),p.setRng(j)):(r=b(f.startContainer))&&(j=f.cloneRange(),j.setStart(r.nextSibling,0),p.setRng(j))),r&&((d=o.hasClass(r,"wpview-selection-before"))||(e=o.hasClass(r,"wpview-selection-after")))&&(e&&m===s.UP||d&&m===s.BACKSPACE?(r.previousSibling?b(r.previousSibling)?g(!1,r.previousSibling):o.isEmpty(r.previousSibling)&&m===s.BACKSPACE?o.remove(r.previousSibling):(p.select(r.previousSibling,!0),p.collapse()):g(!0,r),c.preventDefault()):!e||m!==s.DOWN&&m!==s.RIGHT?!d||m!==s.UP&&m!==s.LEFT?d&&m===s.DOWN?(r.nextSibling?b(r.nextSibling)?g(!0,r.nextSibling):p.setCursorLocation(r.nextSibling,0):g(!1,r),c.preventDefault()):e&&m===s.LEFT||d&&m===s.RIGHT?(i(r),c.preventDefault(),c.stopImmediatePropagation()):e&&m===s.BACKSPACE?(o.remove(r),c.preventDefault()):e?h(r):d&&h(r,!0,m):(r.previousSibling&&(b(r.previousSibling)?g(m===s.UP,r.previousSibling):(p.select(r.previousSibling,!0),p.collapse())),c.preventDefault()):(r.nextSibling&&(b(r.nextSibling)?g(m===s.RIGHT,r.nextSibling):p.setCursorLocation(r.nextSibling,0)),c.preventDefault()),m===s.ENTER&&c.preventDefault())}}),a.on("keydown",function(c){var d,e=a.dom,f=c.keyCode,i=a.selection;if(l){if(c.metaKey||c.ctrlKey||f>=112&&123>=f)return void(!c.metaKey&&!c.ctrlKey||88!==f&&f!==s.BACKSPACE||(88===f?u=l:a.dom.remove(l)));if(d=b(i.getNode()),d!==l)return void j();f===s.LEFT?g(!0,d):f===s.UP?d.previousSibling?b(d.previousSibling)?g(!0,d.previousSibling):(j(),i.select(d.previousSibling,!0),i.collapse()):g(!0,d):f===s.RIGHT?g(!1,d):f===s.DOWN?d.nextSibling?b(d.nextSibling)?g(!1,d.nextSibling):(j(),i.setCursorLocation(d.nextSibling,0)):g(!1,d):f===s.ENTER?h(d):(f===s.DELETE||f===s.BACKSPACE)&&e.remove(l),c.preventDefault()}}),a.on("keydown",function(c){var d,e,f,h=a.selection;c.keyCode===s.BACKSPACE&&(d=h.getNode(),a.dom.isEmpty(d)?(f=b(d.previousSibling))&&(g(!1,f),a.dom.remove(d),c.preventDefault()):(e=h.getRng())&&0===e.startOffset&&0===e.endOffset&&(f=b(d.previousSibling))&&(g(!1,f),c.preventDefault()))}),a.on("keyup",function(){u&&(a.dom.remove(u),u=!1)}),a.on("focus",function(){var c;p=!0,a.dom.addClass(a.getBody(),"has-focus"),v&&(c=b(a.getBody().firstChild))&&g(!0,c),v=!1}),a.on("blur",function(){p=!1,a.dom.removeClass(a.getBody(),"has-focus")}),a.on("NodeChange",function(d){var e=a.dom,f=a.dom.select(".wpview-wrap"),h=d.element.className,i=b(d.element),k=n;if(n=!1,clearInterval(m),e.removeClass(f,"wpview-selection-before"),e.removeClass(f,"wpview-selection-after"),e.removeClass(f,"wpview-cursor-hide"),p)if(i)if("wpview-selection-before"!==h&&"wpview-selection-after"!==h||!a.selection.isCollapsed())c(d.element,"wpview-clipboard")||o||(j(),o++,g(!0,i));else{if(o=0,j(),k===i.previousSibling)return void g(!0,i);if(k===i.nextSibling)return void g(!1,i);e.addClass(i,h),m=setInterval(function(){e.hasClass(i,"wpview-cursor-hide")?e.removeClass(i,"wpview-cursor-hide"):e.addClass(i,"wpview-cursor-hide")},500)}else j()}),a.on("BeforeExecCommand",function(){var c,d=a.selection.getNode();d&&("wpview-selection-before"===d.className||"wpview-selection-after"===d.className)&&(c=b(d))&&(h(c),q=c)}),a.on("ExecCommand",function(){var b,c;l&&(b=l,j(),i(b)),q&&(c=q.nextSibling,c&&"P"===c.nodeName&&a.dom.isEmpty(c)&&(a.dom.remove(c),g(!1,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())}),{getViewText:d,setViewText:e,getView:b}}); |