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

1 line
2.9 KiB
JavaScript

!function(u,g){u.Env.ie&&u.Env.ie<9||u.PluginManager.add("wptextpattern",function(d){var s,t=u.util.VK,e=[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],i=[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"},{regExp:/^(-){3,}$/,element:"hr"}],c=[{start:"`",end:"`",format:"code"}],l=[];function n(){var e,a,t,n,r=d.selection.getRng(),o=r.startContainer,i=r.startOffset;o&&3===o.nodeType&&o.data.length&&i&&-1!==u.inArray(l,o.data.charAt(i-1))&&(e=function(e){for(var t,n=c.length;n--;)if(a=c[n],-1!==(t=e.data.indexOf(a.end)))return t}(o),r=o.data.lastIndexOf(a.end),e!==r&&-1!==r&&(r-e<=a.start.length||-1===o.data.slice(e+a.start.length,r).indexOf(a.start.slice(0,1))&&(t=d.formatter.get(a.format))&&t[0].inline&&(d.undoManager.add(),d.undoManager.transact(function(){o.insertData(i,"\u200b"),o=o.splitText(e),n=o.splitText(i-e),o.deleteData(0,a.start.length),o.deleteData(o.data.length-a.end.length,a.end.length),d.formatter.apply(a.format,{},o),d.selection.setCursorLocation(n,1)}),g(function(){s="space",d.once("selectionchange",function(){var e;n&&-1!==(e=n.data.indexOf("\u200b"))&&n.deleteData(e,e+1)})}))))}function f(e){var t,n=d.dom.getParent(e,"p");if(n){for(;(t=n.firstChild)&&3!==t.nodeType;)n=t;if(t)return t=!t.data?t.nextSibling&&3===t.nextSibling.nodeType?t.nextSibling:null:t}}function a(){var n,a,r=d.selection.getRng(),o=r.startContainer;o&&f(o)===o&&(n=o.parentNode,a=o.data,u.each(e,function(e){var t=a.match(e.regExp);if(t&&r.startOffset===t[0].length)return d.undoManager.add(),d.undoManager.transact(function(){o.deleteData(0,t[0].length),n.innerHTML||n.appendChild(document.createElement("br")),d.selection.setCursorLocation(n),d.execCommand(e.cmd)}),g(function(){s="space"}),!1}))}u.each(c,function(e){u.each((e.start+e.end).split(""),function(e){-1===u.inArray(l,e)&&l.push(e)})}),d.on("selectionchange",function(){s=null}),d.on("keydown",function(e){(s&&27===e.keyCode||"space"===s&&e.keyCode===t.BACKSPACE)&&(d.undoManager.undo(),e.preventDefault(),e.stopImmediatePropagation()),e.keyCode!==t.ENTER||t.modifierPressed(e)||function(){var e,t,n,a=d.selection.getRng().startContainer,r=f(a),o=i.length;if(r){for(e=r.data;o--;)if(i[o].start){if(0===e.indexOf(i[o].start)){t=i[o];break}}else if(i[o].regExp&&i[o].regExp.test(e)){t=i[o];break}t&&(r===a&&u.trim(e)===t.start||d.once("keyup",function(){d.undoManager.add(),d.undoManager.transact(function(){var e;t.format?(d.formatter.apply(t.format,{},r),r.replaceData(0,r.data.length,(e=r.data.slice(t.start.length))?e.replace(/^\s+/,""):"")):t.element&&(n=r.parentNode&&r.parentNode.parentNode)&&n.replaceChild(document.createElement(t.element),r.parentNode)}),g(function(){s="enter"})}))}}(),e.keyCode!==t.SPACEBAR||e.ctrlKey||e.metaKey||e.altKey?47<e.keyCode&&!(91<=e.keyCode&&e.keyCode<=93)&&g(n):g(a)},!0)})}(window.tinymce,window.setTimeout);