WordPress/wp-includes/js/tinymce/plugins/wpview/plugin.min.js
Andrew Ozz 00ff787a86 TinyMCE wpView:
- Improve the fake caret hide/show.
- Improve getView() speed.
- Add callback for showing the proper path when a view is selected. This currently doesn't work, will start working after we update TinyMCE.
props avryl, see #28567, #28595.
Built from https://develop.svn.wordpress.org/trunk@29022


git-svn-id: http://core.svn.wordpress.org/trunk@28810 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2014-07-08 04:15:14 +00:00

1 line
7.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

tinymce.PluginManager.add("wpview",function(a){function b(a){for(;a&&a.parentNode;){if(a.className&&-1!==(" "+a.className+" ").indexOf(" wpview-wrap "))return a;a=a.parentNode}return!1}function c(c){return(c=b(c))?window.decodeURIComponent(a.dom.getAttrib(c,"data-wpview-text")||""):""}function d(c,d){return c=b(c),c?(a.dom.setAttrib(c,"data-wpview-text",window.encodeURIComponent(d||"")),!0):!1}function e(a){a.stopPropagation()}function f(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 g(b,c){var d,e=a.dom;!c&&b.nextSibling&&e.isEmpty(b.nextSibling)&&"P"===b.nextSibling.nodeName?d=b.nextSibling:c&&b.previousSibling&&e.isEmpty(b.previousSibling)&&"P"===b.previousSibling.nodeName?d=b.previousSibling:(d=e.create("p"),p.ie&&p.ie<11||(d.innerHTML='<br data-mce-bogus="1">'),c?b.parentNode.insertBefore(d,b):e.insertAfter(d,b)),i(),a.getBody().focus(),a.selection.setCursorLocation(d,0),a.nodeChanged()}function h(b){var d,f=a.dom;b!==k&&(i(),k=b,f.setAttrib(b,"data-mce-selected",1),d=f.create("div",{"class":"wpview-clipboard",contenteditable:"true"},c(b)),a.dom.select(".wpview-body",b)[0].appendChild(d),f.bind(d,"beforedeactivate focusin focusout",e),f.bind(k,"beforedeactivate focusin focusout",e),tinymce.DOM.addClass(a.getContainer(),"wpview-selected"),a.getBody().focus(),a.selection.select(d,!0),a.nodeChanged())}function i(){var b,c=a.dom;k&&(b=a.dom.select(".wpview-clipboard",k)[0],c.unbind(b),c.remove(b),c.unbind(k,"beforedeactivate focusin focusout click mouseup",e),c.setAttrib(k,"data-mce-selected",null)),k=null}function j(a){return a.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?:&nbsp;|\u00a0)*<\/p><\/div>/g,"$1")}var k,l,m,n,o,p=tinymce.Env,q=tinymce.util.VK,r=tinymce.dom.TreeWalker,s=!1,t=!0;if("undefined"!=typeof wp&&wp.mce)return a.on("BeforeAddUndo",function(a){a.lastLevel&&j(a.level.content)===j(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,g=c.clientY,h=a.getBody(),i=h.getBoundingClientRect(),j=h.firstChild,k=j.getBoundingClientRect(),l=h.lastChild,m=l.getBoundingClientRect();g<k.top&&(d=b(j))?(f(!0,d),c.preventDefault()):g>m.bottom&&(d=b(l))?(f(!1,d),c.preventDefault()):tinymce.each(a.dom.select(".wpview-wrap"),function(a){var b=a.getBoundingClientRect();return g>=b.top&&g<=b.bottom?void(e<i.left?(f(!0,a),c.preventDefault()):e>i.right&&(f(!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 r(f.nextSibling,f.nextSibling),e=d.next()),c.select(e),c.collapse(!0))}),a.on("SetContent",function(a){if(a.context){var b=c.getNode();b.innerHTML&&(b.innerHTML=wp.mce.views.toViews(b.innerHTML))}}),a.dom.bind(a.getBody().parentNode,"mousedown mouseup click",function(c){var d,e=b(c.target);return t=!1,e?(c.stopPropagation(),p.ie<=10&&i(),h(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=p.ie&&p.ie<=8?"mouseup":"mousedown",void(c.type===d&&i()))})}),a.on("PreProcess",function(b){tinymce.each(a.dom.select("div[data-wpview-text]",b.node),function(a){"textContent"in 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||n>=112&&123>=n||k)){var d,e,i,j,l,n=c.keyCode,o=a.dom,p=a.selection,r=p.getNode(),s=b(r);m=r,p.isCollapsed()||(i=p.getRng(),(s=b(i.endContainer))?(j=i.cloneRange(),p.select(s.previousSibling,!0),p.collapse(),l=p.getRng(),j.setEnd(l.endContainer,l.endOffset),p.setRng(j)):(s=b(i.startContainer))&&(j=i.cloneRange(),j.setStart(s.nextSibling,0),p.setRng(j))),s&&((d=o.hasClass(s,"wpview-selection-before"))||(e=o.hasClass(s,"wpview-selection-after")))&&(e&&n===q.UP||d&&n===q.BACKSPACE?(s.previousSibling?b(s.previousSibling)?f(!1,s.previousSibling):o.isEmpty(s.previousSibling)&&n===q.BACKSPACE?o.remove(s.previousSibling):(p.select(s.previousSibling,!0),p.collapse()):f(!0,s),c.preventDefault()):!e||n!==q.DOWN&&n!==q.RIGHT?!d||n!==q.UP&&n!==q.LEFT?d&&n===q.DOWN?(s.nextSibling?b(s.nextSibling)?f(!0,s.nextSibling):p.setCursorLocation(s.nextSibling,0):f(!1,s),c.preventDefault()):e&&n===q.LEFT||d&&n===q.RIGHT?(h(s),c.preventDefault(),c.stopImmediatePropagation()):e&&n===q.BACKSPACE?(o.remove(s),c.preventDefault()):e?g(s):d&&g(s,!0):(s.previousSibling&&(b(s.previousSibling)?f(n===q.UP,s.previousSibling):(p.select(s.previousSibling,!0),p.collapse())),c.preventDefault()):(s.nextSibling&&(b(s.nextSibling)?f(n===q.RIGHT,s.nextSibling):p.setCursorLocation(s.nextSibling,0)),c.preventDefault()),n===q.ENTER&&c.preventDefault())}}),a.on("keydown",function(c){var d,e=a.dom,h=c.keyCode,j=a.selection;if(k){if(c.metaKey||c.ctrlKey||h>=112&&123>=h)return void(!c.metaKey&&!c.ctrlKey||88!==h&&h!==q.BACKSPACE||(88===h?s=k:a.dom.remove(k)));if(d=b(j.getNode()),d!==k)return void i();h===q.LEFT?f(!0,d):h===q.UP?d.previousSibling?b(d.previousSibling)?f(!0,d.previousSibling):(i(),j.select(d.previousSibling,!0),j.collapse()):f(!0,d):h===q.RIGHT?f(!1,d):h===q.DOWN?d.nextSibling?b(d.nextSibling)?f(!1,d.nextSibling):(i(),j.setCursorLocation(d.nextSibling,0)):f(!1,d):h===q.ENTER?g(d):(h===q.DELETE||h===q.BACKSPACE)&&e.remove(k),c.preventDefault()}}),a.on("keydown",function(c){var d,e,g,h=a.selection;c.keyCode===q.BACKSPACE&&(d=h.getNode(),a.dom.isEmpty(d)?(g=b(d.previousSibling))&&(f(!1,g),a.dom.remove(d),c.preventDefault()):(e=h.getRng())&&0===e.startOffset&&0===e.endOffset&&(g=b(d.previousSibling))&&(f(!1,g),c.preventDefault()))}),a.on("keyup",function(){s&&(a.dom.remove(s),s=!1)}),a.on("focus",function(){var c;o=!0,a.dom.addClass(a.getBody(),"has-focus"),t&&(c=b(a.getBody().firstChild))&&f(!0,c),t=!1}),a.on("blur",function(){o=!1,a.dom.removeClass(a.getBody(),"has-focus")}),a.on("nodechange",function(c){var d=a.dom,e=a.dom.select(".wpview-wrap"),g=c.element.className,h=b(c.element),j=m;if(m=!1,clearInterval(l),d.removeClass(e,"wpview-selection-before"),d.removeClass(e,"wpview-selection-after"),d.removeClass(e,"wpview-cursor-hide"),k||tinymce.DOM.removeClass(a.getContainer(),"wpview-selected"),o)if(h)if("wpview-selection-before"===g||"wpview-selection-after"===g&&a.selection.isCollapsed()){if(n=0,i(),tinymce.DOM.addClass(a.getContainer(),"wpview-selected"),j===h.previousSibling)return void f(!0,h);if(j===h.nextSibling)return void f(!1,h);d.addClass(h,g),l=setInterval(function(){d.hasClass(h,"wpview-cursor-hide")?d.removeClass(h,"wpview-cursor-hide"):d.addClass(h,"wpview-cursor-hide")},500)}else"wpview-clipboard"===g||n||(i(),n++,f(!0,h));else i()}),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:c,setViewText:d}});