mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-27 19:47:42 +01:00
e05fa407bf
Built from https://develop.svn.wordpress.org/trunk@27632 git-svn-id: http://core.svn.wordpress.org/trunk@27475 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 line
4.9 KiB
JavaScript
1 line
4.9 KiB
JavaScript
tinymce.PluginManager.add("wpview",function(a){function b(a){for(;a&&"BODY"!==a.nodeName;){if(c(a))return a;a=a.parentNode}}function c(a){return a&&/\bwpview-wrap\b/.test(a.className)}function d(){return a.dom.create("p",{"data-wpview-pad":1},tinymce.Env.ie&&tinymce.Env.ie<11?"":'<br data-mce-bogus="1" />')}function e(c){return c=b("string"==typeof c?a.dom.get(c):c),c?window.decodeURIComponent(a.dom.getAttrib(c,"data-wpview-text")||""):""}function f(c,d){return c=b("string"==typeof c?a.dom.get(c):c),c?(a.dom.setAttrib(c,"data-wpview-text",window.encodeURIComponent(d||"")),!0):!1}function g(a){a.stopPropagation()}function h(b){var c,d=a.dom;b!==k&&(i(),k=b,d.addClass(b,"selected"),c=d.create("div",{"class":"wpview-clipboard",contenteditable:"true"},e(b)),b.appendChild(c),a.dom.bind(c,"beforedeactivate focusin focusout",g),a.dom.bind(k,"beforedeactivate focusin focusout",g),a.getBody().focus(),a.selection.select(c,!0))}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",g),c.removeClass(k,"selected")),k=null}function j(b,d){for(var e=a.getBody(),f="previous"===d?"previousSibling":"nextSibling";b&&b.parentNode!==e;){if(b[f])return!1;b=b.parentNode}return c(b[f])?(h(b[f]),!0):!1}var k,l=tinymce.util.VK,m=tinymce.dom.TreeWalker,n=!1;if("undefined"!=typeof wp&&wp.mce)return a.on("BeforeAddUndo",function(a){k&&!n&&a.preventDefault()}),a.on("BeforeSetContent",function(a){a.content&&(a.content=wp.mce.views.toViews(a.content))}),a.on("SetContent",function(b){var e,f;"raw"!==b.format&&wp.mce.views.render(),(b.load||!b.set)&&(e=a.getBody(),c(e.lastChild)&&(f=d(),e.appendChild(f),a.selection.setCursorLocation(f,0)))}),a.on("click",function(b){var e,f,g,h,j,k=a.getBody(),l=a.getDoc(),m=l.documentElement.scrollTop||k.scrollTop||0;"HTML"!==b.target.nodeName||b.metaKey||b.ctrlKey||(g=k.firstChild,h=k.lastChild,e=b.clientX,f=b.clientY,c(g)&&(e<g.offsetLeft&&f<g.offsetHeight-m||f<g.offsetTop)?(j=d(),k.insertBefore(j,g)):c(h)&&(e>h.offsetLeft+h.offsetWidth||m+f-(h.offsetTop+h.offsetHeight)>0)&&(j=d(),k.appendChild(j)),j&&(i(),a.getBody().focus(),a.selection.setCursorLocation(j,0)))}),a.on("init",function(){var d=a.selection;a.on("BeforeSetContent",function(){var e,f,g=b(d.getNode());g&&(!g.nextSibling||c(g.nextSibling)?(f=a.getDoc().createTextNode(""),a.dom.insertAfter(f,g)):(e=new m(g.nextSibling,g.nextSibling),f=e.next()),d.select(f),d.collapse(!0))}),a.on("SetContent",function(a){if(a.context){var b=d.getNode();b.innerHTML&&(b.innerHTML=wp.mce.views.toViews(b.innerHTML))}}),a.dom.bind(a.getBody(),"mousedown mouseup click",function(c){var d=b(c.target);return d?(c.stopPropagation(),"click"!==c.type||c.metaKey||c.ctrlKey||(a.dom.hasClass(c.target,"edit")?wp.mce.views.edit(d):a.dom.hasClass(c.target,"remove")&&a.dom.remove(d)),h(d),!1):void("mousedown"===c.type&&i())})}),a.on("PreProcess",function(b){var c=a.dom;tinymce.each(c.select("p[data-wpview-pad]",b.node),function(a){c.isEmpty(a)?c.remove(a):c.setAttrib(a,"data-wpview-pad",null)}),tinymce.each(c.select("div[data-wpview-text]",b.node),function(a){"textContent"in a?a.textContent="":a.innerText="",c.replace(c.create("p",null,window.decodeURIComponent(c.getAttrib(a,"data-wpview-text"))),a)})}),a.on("keydown",function(e){var f,g,j=e.keyCode,m=a.getBody();if(k){if(e.metaKey||e.ctrlKey||j>=112&&123>=j)return void((e.metaKey||e.ctrlKey)&&88===j&&(n=k));if(f=b(a.selection.getNode()),f!==k)return void i();j===l.LEFT||j===l.UP?(i(),c(f.previousSibling)?h(f.previousSibling):f.previousSibling?(a.selection.select(f.previousSibling,!0),a.selection.collapse()):(g=d(),m.insertBefore(g,m.firstChild),a.selection.setCursorLocation(m.firstChild,0))):j===l.RIGHT||j===l.DOWN?(i(),c(f.nextSibling)?h(f.nextSibling):f.nextSibling?a.selection.setCursorLocation(f.nextSibling,0):(g=d(),m.appendChild(g),a.selection.setCursorLocation(m.lastChild,0))):(j===l.DELETE||j===l.BACKSPACE)&&a.dom.remove(k),e.preventDefault()}}),a.on("keydown",function(b){var c,d,e=b.keyCode,f=a.dom,g=a.selection.getRng(),h=g.startContainer,i=a.getBody();if(h&&h!==i&&!b.metaKey&&!b.ctrlKey)if(e===l.UP||e===l.LEFT){if(e===l.LEFT&&(!g.collapsed||0!==g.startOffset))return;if(!(c=f.getParent(h,f.isBlock)))return;j(c,"previous")&&b.preventDefault()}else if(e===l.DOWN||e===l.RIGHT){if(!(c=f.getParent(h,f.isBlock)))return;if(e===l.RIGHT){if(d=g.endContainer,!g.collapsed||0===g.startOffset&&d.length||d.nextSibling||3===d.nodeType&&g.startOffset!==d.length)return;for(;d&&d!==c&&d!==i;){if(d.nextSibling)return;d=d.parentNode}}j(c,"next")&&b.preventDefault()}}),a.on("keyup",function(b){var e,f,g=b.keyCode,h=a.getBody();n&&(a.dom.remove(n),n=!1),(g===l.DELETE||g===l.BACKSPACE)&&(c(h.lastChild)&&(e=d(),h.appendChild(e),2===h.childNodes.length&&a.selection.setCursorLocation(e,0)),f=a.selection.getRng(),h.firstChild===f.startContainer&&f.collapsed===!0&&c(f.startContainer.nextSibling)&&0===f.startOffset&&a.dom.remove(f.startContainer))}),{getViewText:e,setViewText:f}}); |