let bitty=window.bitty={instances:[],baseFontSize:12,uid:0,getUID(){return this.uid++},config:{flashTime:250,flashColor:"black",flashBackground:"white",value:"// itty bitty ide"},events:{},events:{paste(t){var i=(t.clipboardData||window.clipboardData).getData("text/plain");if(1!==i.split("\n").length&&window.getSelection().rangeCount){let e=this.caret();var n=i.split("\n").length-3,i=(e+=i.length-n,""===t.target.innerText||" "===t.target.innerText||"\n"===t.target.innerText||"<br>"===t.target.innerText);t.target!==this.el&&i&&t.target.remove(),setTimeout(()=>{this.process(),setTimeout(()=>{this.noDivsInDivs(),this.setCaret(e)},0)},0),this.publish("paste",t)}},keydown(e){1!==e.key.length&&"Tab"!==e.key&&"Enter"!==e.key||(this.lastKeyDownCode=e.keyCode);var t,i;9===e.keyCode?(t=this.caret()+"\t".length,(i=window.getSelection().getRangeAt(0)).deleteContents(),i.insertNode(document.createTextNode("\t")),this.setCaret(t),e.preventDefault()):8===e.keyCode?this.checkForEmpty():13===e.keyCode&&(e.ctrlKey?(e.preventDefault(),this.runSelection()):e.altKey&&(e.preventDefault(),this.runBlock())),"z"===e.key&&(e.ctrlKey||e.metaKey)?setTimeout(()=>this.process(),50):"y"===e.key&&(e.ctrlKey||e.metaKey)||this.publish("keydown",e)},keyup(e){var t,i;(13!==this.lastKeyDownCode&&!e.ctrlKey&&48<=e.keyCode||32===e.keyCode)&&(t=this.caret(),this.process(),i=window.getSelection(),this.setCaret(t,i.anchorNode.parentNode,i)),this.publish("keyup",e)}},rules:{},keyManager(e){let s={bitty:e,combos:[],process(e){for(var t of s.combos)if(e.key===t.key&&(!t.ctrl||e.ctrlKey)&&(!t.shift||e.shiftKey)&&(!t.alt||e.altKey)&&(!t.meta||e.metaKey))return void t.fnc(e)},register(e,t){var i=e.split("+"),n={key:i[i.length-1]};if(1<i.length)for(let e=0;e<i.length-1;e++)n[i[e].toLowerCase()]=!0;n.fnc=t,s.combos.push(n)}};return e.subscribe("keydown",s.process.bind(s)),s},create(e={}){let t=null;t=void 0===e.el?document.querySelector('[contenteditable="true"]'):e.el;var i=Object.create(bitty),e=(i.el=t,Object.defineProperty(i,"value",{set(e){this.el.innerHTML=this.divide(e),this.process(e,!0)},get(){return this.el.innerText}}),Object.assign({},bitty.config,e)),n=e.value;return delete e.value,Object.assign(i,e,{events:{},timeout:null}),void 0!==n&&(i.value=n),i.editor(i,t),i.publish("init",i),i.keyManager=bitty.keyManager(i),bitty.publish("new",i),t.focus(),bitty.instances.push(i),i},divide(e){return e.split("\n").map(e=>""===e?"\n":e).map(e=>`<div>${e}</div>`).join("")},focus(){this.el.focus()},changeFontSize(e){this.baseFontSize+=e,this.el.style.fontSize=this.baseFontSize+"px"},process(e){if("function"==typeof Highlight){var t,i=this.el,n=this.rules,s=Object.keys(n),r=Array.from(i.childNodes).map(e=>"\n"!==e.innerText?e.innerText:" ").join("\n");for(t of s){for(var l=new Highlight;null!==(match=n[t].exec(r));)this.markRange(match.index,n[t].lastIndex,t,l);CSS.highlights.set(t,l)}return r}},markRange(e,t,i,n){let s=0,r=!1;for(var l of this.el.childNodes){var o=l.innerText.length;if(s+o>=e){if(s+o>=t){var a=new Range,h=r?0:e-s,d=t-s;a.setStart(l.firstChild,h),a.setEnd(l.firstChild,d),n.add(a),r=!1;break}h=new Range,d=r?0:e-s,a=o;h.setStart(l.firstChild,d),h.setEnd(l.firstChild,a),n.add(h),r=!0}s+=l.innerText.length+1}},subscribe(e,t){var i=this.events;void 0===i[e]&&(i[e]=[]),i[e].push(t)},unsubscribe(e,t){var i=this.events;void 0!==i[e]&&(i=i[e]).splice(i.indexOf(t),1)},publish(e,t){var i=this.events;void 0!==i[e]&&i[e].forEach(e=>e(t))},runBlock(){let e=window.getSelection().anchorNode.parentElement,t=[];for(;"div"!==e.localName;)e=e.parentElement;for(e.style.background,e.style.color;null!==e.previousSibling&&"\n"!==e.previousSibling.innerText&&"div"===e.previousSibling.localName;)e=e.previousSibling;for(t.push(e);null!==e.nextSibling&&"\n"!==e.nextSibling.innerText&&"div"===e.nextSibling.localName;)e=e.nextSibling,t.push(e);let i=document.styleSheets[0],n="flash"+this.getUID();var s=`.${n} { 
      background: ${this.flashBackground} !important;
      color: ${this.flashColor} !important;
    }`;let r=i.insertRule(s);t.forEach(e=>e.classList.add(n)),setTimeout(e=>{t.forEach(e=>e.classList.remove(n)),i.removeRule(r)},this.config.flashTime),s=t.map(e=>e.innerText).join("\n"),this.publish("run",s)},runSelection(){var n=window.getSelection();let s=n.toString(),r=document.styleSheets[0];if(""===s){let e=n.anchorNode.parentElement;for(;"div"!==e.localName;)e=e.parentElement;s=e.innerText;let t="flash"+this.getUID();e.classList.add(t);n=`.${t} { 
        background: ${this.flashBackground} !important;
        color: ${this.flashColor} !important;
      }`;let i=r.insertRule(n);setTimeout(()=>{r.removeRule(i),e.classList.remove(t)},this.config.flashTime)}else{let e=document.styleSheets[0],t=e.insertRule("::selection{ color:black !important; background:white !important}");setTimeout(()=>e.removeRule(t),250)}this.publish("run",s)},caret(){var e=window.getSelection().getRangeAt(0),t=e.cloneRange();return t.selectNodeContents(this.el),t.setEnd(e.endContainer,e.endOffset),t.toString().length},setCaret(e,t=this.el,i=null){for(var n of t.childNodes)if(3===n.nodeType){var s,r;if(n.length>=e)return s=document.createRange(),r=i||window.getSelection(),s.setStart(n,e),s.collapse(!0),r.removeAllRanges(),r.addRange(s),-1;e-=n.length}else if((e=this.setCaret(e,n,i))<0)break;return e},checkForEmpty(){setTimeout(e=>{var t;1===this.el.childNodes.length&&"br"===this.el.firstChild.localName?((t=document.createElement("div")).innerHTML="&nbsp;",t.className=this.el.firstChild.className,this.el.firstChild.remove(),this.el.appendChild(t),this.setCaret(0)):this.el.normalize()},5)},noDivsInDivs(){for(var e of Array.from(this.el.childNodes))if(3!==e.nodeType){var t=e.querySelectorAll("div");if(1<t.length)for(var i of t)this.el.insertBefore(i,e)}},editor(t,e,i=0){for(var n in t.lastKeyDownCode=0,new MutationObserver(e=>{e.forEach(e=>{e.addedNodes.length?t.publish("nodes added",e.addedNodes):t.publish("nodes removed",e.removedNodes)})}).observe(e,{childList:!0}),e.addEventListener("click",e=>t.publish("click",e)),window.bitty.events)e.addEventListener(n,window.bitty.events[n].bind(t))}};