0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-06 23:00:08 -05:00
Squire/build/squire.js

2 lines
37 KiB
JavaScript
Raw Normal View History

!function(e,t){"use strict";function n(e,t,n){this.root=this.currentNode=e,this.nodeType=t,this.filter=n}function o(e,t){for(var n=e.length;n--;)if(!t(e[n]))return!1;return!0}function r(e,t,n){if(e.nodeName!==t)return!1;for(var o in n)if(e.getAttribute(o)!==n[o])return!1;return!0}function i(e,t){return e.nodeType===t.nodeType&&e.nodeName===t.nodeName&&e.className===t.className&&(!e.style&&!t.style||e.style.cssText===t.style.cssText)}function a(e){return e.nodeType===L&&!!ne[e.nodeName]}function s(e){return te.test(e.nodeName)}function d(e){return e.nodeType===L&&!s(e)&&o(e.childNodes,s)}function l(e){return e.nodeType===L&&!s(e)&&!d(e)}function c(e){var t=e.ownerDocument,o=new n(t.body,R,d,!1);return o.currentNode=e,o}function f(e){return c(e).previousNode()}function h(e){return c(e).nextNode()}function u(e,t,n){do if(r(e,t,n))return e;while(e=e.parentNode);return null}function p(e){var t,n,o,r,i,a=e.parentNode;return a&&e.nodeType===L?(t=p(a),t+=(t?">":"")+e.nodeName,(n=e.id)&&(t+="#"+n),(o=e.className.trim())&&(r=o.split(/\s\s*/),r.sort(),t+=".",t+=r.join(".")),(i=e.dir)&&(t+="[dir="+i+"]")):t=a?p(a):"",t}function g(e){var t=e.nodeType;return t===L?e.childNodes.length:e.length||0}function m(e){var t=e.parentNode;return t&&t.removeChild(e),e}function v(e,t){var n=e.parentNode;n&&n.replaceChild(t,e)}function N(e){for(var t=e.ownerDocument.createDocumentFragment(),n=e.childNodes,o=n?n.length:0;o--;)t.appendChild(e.firstChild);return t}function C(e,n,o,r){var i,a,s,d,l=e.createElement(n);if(o instanceof Array&&(r=o,o=null),o)for(i in o)a=o[i],a!==t&&l.setAttribute(i,o[i]);if(r)for(s=0,d=r.length;d>s;s+=1)l.appendChild(r[s]);return l}function _(e){var t,n,o=e.ownerDocument,r=e;if("BODY"===e.nodeName&&((n=e.firstChild)&&"BR"!==n.nodeName||(t=k(o).createDefaultBlock(),n?e.replaceChild(t,n):e.appendChild(t),e=t,t=null)),s(e)){for(n=e.firstChild;$&&n&&n.nodeType===x&&!n.data;)e.removeChild(n),n=e.firstChild;n||($?(t=o.createTextNode(F),k(o)._didAddZWS()):t=o.createTextNode(""))}else if(G){for(;e.nodeType!==x&&!a(e);){if(n=e.firstChild,!n){t=o.createTextNode("");break}e=n}e.nodeType===x?/^ +$/.test(e.data)&&(e.data=""):a(e)&&e.parentNode.insertBefore(o.createTextNode(""),e)}else if(!e.querySelector("BR"))for(t=C(o,"BR");(n=e.lastElementChild)&&!s(n);)e=n;return t&&e.appendChild(t),r}function S(e){var t,n,o,r,i=e.childNodes,a=e.ownerDocument,d=null,c=k(a)._config;for(t=0,n=i.length;n>t;t+=1)o=i[t],r="BR"===o.nodeName,!r&&s(o)?(d||(d=C(a,c.blockTag,c.blockAttributes)),d.appendChild(o),t-=1,n-=1):(r||d)&&(d||(d=C(a,c.blockTag,c.blockAttributes)),_(d),r?e.replaceChild(d,o):(e.insertBefore(d,o),t+=1,n+=1),d=null),l(o)&&S(o);return d&&e.appendChild(_(d)),e}function y(e,t,n){var o,r,i,a=e.nodeType;if(a===x&&e!==n)return y(e.parentNode,e.splitText(t),n);if(a===L){if("number"==typeof t&&(t=t<e.childNodes.length?e.childNodes[t]:null),e===n)return t;for(o=e.parentNode,r=e.cloneNode(!1);t;)i=t.nextSibling,r.appendChild(t),t=i;return"OL"===e.nodeName&&u(e,"BLOCKQUOTE")&&(r.start=(+e.start||1)+e.childNodes.length-1),_(e),_(r),(i=e.nextSibling)?o.insertBefore(r,i):o.appendChild(r),y(o,r,n)}return t}function T(e,t){if(e.nodeType===L)for(var n,o,r,a=e.childNodes,d=a.length,l=[];d--;)if(n=a[d],o=d&&a[d-1],d&&s(n)&&i(n,o)&&!ne[n.nodeName])t.startContainer===n&&(t.startContainer=o,t.startOffset+=g(o)),t.endContainer===n&&(t.endContainer=o,t.endOffset+=g(o)),t.startContainer===e&&(t.startOffset>d?t.startOffset-=1:t.startOffset===d&&(t.startContainer=o,t.startOffset=g(o))),t.endContainer===e&&(t.endOffset>d?t.endOffset-=1:t.endOffset===d&&(t.endContainer=o,t.endOffset=g(o))),m(n),n.nodeType===x?o.appendData(n.data):l.push(N(n));else if(n.nodeType===L){for(r=l.length;r--;)n.appendChild(l.pop());T(n,t)}}function b(e,t,n){for(var o,r,i,a=t;1===a.parentNode.childNodes.length;)a=a.parentNode;m(a),r=e.childNodes.length,o=e.lastChild,o&&"BR"===o.nodeName&&(e.removeChild(o),r-=1),i={startContainer:e,startOffset:r,endContainer:e,endOffset:r},e.appendChild(N(t)),T(e,i),n.setStart(i.startContainer,i.startOffset),n.collapse(!0),q&&(o=e.lastChild)&&"BR"===o.node
var t=e.keyCode,n=Ye[t],o="",r=this.getSelection();n||(n=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(n)||(n="")),q&&46===e.which&&(n="."),t>111&&124>t&&(n="f"+(t-111)),"backspace"!==n&&"delete"!==n&&(e.altKey&&(o+="alt-"),e.ctrlKey&&(o+="ctrl-"),e.metaKey&&(o+="meta-")),e.shiftKey&&(o+="shift-"),n=o+n,this._keyHandlers[n]?this._keyHandlers[n](this,e,r):1!==n.length||r.collapsed||(this._recordUndoState(r),this._getRangeAndRemoveBookmark(r),se(r),this._ensureBottomLine(),this.setSelection(r),this._updatePath(r,!0))},Te.setKeyHandler=function(e,t){return this._keyHandlers[e]=t,this},Te._getHTML=function(){return this._body.innerHTML},Te._setHTML=function(e){var t=this._body;t.innerHTML=e;do _(t);while(t=h(t));this._ignoreChange=!0},Te.getHTML=function(e){var t,n,o,r,i,a=[];if(e&&(i=this.getSelection())&&this._saveRangeToBookmark(i),G)for(t=this._body;t=h(t);)t.textContent||t.querySelector("BR")||(n=this.createElement("BR"),t.appendChild(n),a.push(n));if(o=this._getHTML().replace(/\u200B/g,""),G)for(r=a.length;r--;)m(a[r]);return i&&this._getRangeAndRemoveBookmark(i),o},Te.setHTML=function(e){var t,n=this._doc.createDocumentFragment(),o=this.createElement("DIV");o.innerHTML=e,n.appendChild(N(o)),Qe(n,!0),je(n),S(n);for(var r=n;r=h(r);)_(r);this._ignoreChange=!0;for(var i=this._body;t=i.lastChild;)i.removeChild(t);i.appendChild(n),_(i),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;var a=this._getRangeAndRemoveBookmark()||this._createRange(i.firstChild,0);return this._recordUndoState(a),this._getRangeAndRemoveBookmark(a),j?this._lastSelection=a:this.setSelection(a),this._updatePath(a,!0),this},Te.insertElement=function(e,t){if(t||(t=this.getSelection()),t.collapse(!0),s(e))ie(t,e),t.setStartAfter(e);else{for(var n,o,r=this._body,i=he(t)||r;i!==r&&!i.nextSibling;)i=i.parentNode;i!==r&&(n=i.parentNode,o=y(n,i.nextSibling,r)),o?(r.insertBefore(e,o),t.setStart(o,0),t.setStart(o,0),ce(t)):(r.appendChild(e),r.appendChild(this.createDefaultBlock()),t.setStart(e,0),t.setEnd(e,0)),this.focus(),this.setSelection(t),this._updatePath(t)}return this},Te.insertImage=function(e,t){var n=this.createElement("IMG",B({src:e},t));return this.insertElement(n),n},Te.insertHTML=function(e){var t=this.getSelection(),n=this._doc.createDocumentFragment(),o=this.createElement("DIV");o.innerHTML=e,n.appendChild(N(o)),this._recordUndoState(t),this._getRangeAndRemoveBookmark(t);try{n.normalize(),Me(n),Qe(n,!0),je(n),Ve(n),S(n);for(var r=n;r=h(r);)_(r);de(t,n),Y||this._docWasChanged(),t.collapse(!1),this._ensureBottomLine(),this.setSelection(t),this._updatePath(t,!0)}catch(i){this.didError(i)}return this};var Xe=function(e,t,n){return function(){return this[e](t,n),this.focus()}};Te.addStyles=function(e){if(e){var t=this._doc.documentElement.firstChild,n=this.createElement("STYLE",{type:"text/css"});n.styleSheet?(t.appendChild(n),n.styleSheet.cssText=e):(n.appendChild(this._doc.createTextNode(e)),t.appendChild(n))}return this},Te.bold=Xe("changeFormat",{tag:"B"}),Te.italic=Xe("changeFormat",{tag:"I"}),Te.underline=Xe("changeFormat",{tag:"U"}),Te.strikethrough=Xe("changeFormat",{tag:"S"}),Te.subscript=Xe("changeFormat",{tag:"SUB"},{tag:"SUP"}),Te.superscript=Xe("changeFormat",{tag:"SUP"},{tag:"SUB"}),Te.removeBold=Xe("changeFormat",null,{tag:"B"}),Te.removeItalic=Xe("changeFormat",null,{tag:"I"}),Te.removeUnderline=Xe("changeFormat",null,{tag:"U"}),Te.removeStrikethrough=Xe("changeFormat",null,{tag:"S"}),Te.removeSubscript=Xe("changeFormat",null,{tag:"SUB"}),Te.removeSuperscript=Xe("changeFormat",null,{tag:"SUP"}),Te.makeLink=function(e,t){var n=this.getSelection();if(n.collapsed){var o=e.indexOf(":")+1;if(o)for(;"/"===e[o];)o+=1;ie(n,this._doc.createTextNode(e.slice(o)))}return t||(t={}),t.href=e,this.changeFormat({tag:"A",attributes:t},{tag:"A"},n),this.focus()},Te.removeLink=function(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0),this.focus()},Te.setFontFace=function(e){return this.changeFormat({tag:"SPAN",attributes:{"class":"font",style:"font-family: "+e+", sans-serif;"}},{ta