-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitty.min.js
1 lines (1 loc) · 5.78 KB
/
bitty.min.js
1
let bitty=window.bitty={instances:[],config:{flashTime:250,flashColor:"black",flashBackground:"white",value:"// itty bitty ide"},events:{},rules:{},keyManager(e){let r={bitty:e,combos:[],process(e){for(var t of r.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 n=e.split("+"),i={key:n[n.length-1]};if(1<n.length)for(let e=0;e<n.length-1;e++)i[n[e].toLowerCase()]=!0;i.fnc=t,r.combos.push(i)}};return e.subscribe("keydown",r.process.bind(r)),r},undoManager(n){var e={history:[],rules:{},undoIdx:0,save(e){this.undoIdx<this.history.length-1&&(this.history=this.history.slice(0,this.undoIdx+1),this.undoIdx++);var t=n.el.innerHTML;for(t===this.history[this.undoIdx]&&1!==e||(this.history.push(t),this.undoIdx=this.history.length-1);50<this.history.length;)this.history.shift()},undo(){var e;0<this.undoIdx&&(e=this.history[this.undoIdx-1],n.el.innerHTML=e,this.undoIdx--)},redo(){var e;this.history[this.undoIdx+1]&&(e=this.history[this.undoIdx+1],n.el.innerHTML=e,this.undoIdx++)}};return n.subscribe("keydown",e.save.bind(e)),n.subscribe("paste",e.save.bind(e)),e},create(e={}){let t=null;t=void 0===e.el?document.querySelector('[contenteditable="true"]'):e.el;var n=Object.create(bitty),e=(n.el=t,Object.defineProperty(n,"value",{set(e){e=this.process(e,!0),e=this.divide(e);this.el.innerHTML=e},get(){return this.el.innerText}}),Object.assign({},bitty.config,e)),i=e.value;return delete e.value,Object.assign(n,e,{events:{}}),void 0!==i&&(n.value=i),n.editor(n,t),n.publish("init",n),n.undoManager=bitty.undoManager(n),n.keyManager=bitty.keyManager(n),bitty.publish("new",n),t.focus(),bitty.instances.push(n),n},divide(e){return e.split("\n").map(e=>" "===e||""===e?" ":e).map(e=>`<div>${e}</div>`).join("")},focus(){this.el.focus()},process(e,t=!1){var n=this.el,i=Object.keys(this.rules),r=this.rules;if(t)for(var o of i)e=e.replace(r[o],`<span class=bitty-${o}>$1</span>`);else for(var s of n.children){e=s.innerText;for(var l of i)e=e.replace(r[l],`<span class=bitty-${l}>$1</span>`);s.innerHTML=e.split("\n").join("<br/>")}return e},subscribe(e,t){var n=this.events;void 0===n[e]&&(n[e]=[]),n[e].push(t)},unsubscribe(e,t){var n=this.events;void 0!==n[e]&&(n=n[e]).splice(n.indexOf(t),1)},publish(e,t){var n=this.events;void 0!==n[e]&&n[e].forEach(e=>e(t))},runBlock(){var e;let t=window.getSelection().anchorNode.parentElement,n=[];for(;"div"!==t.localName;)t=t.parentElement;let i=t.style.background,r=t.style.color;for(;null!==t.previousSibling&&"\n"!==t.previousSibling.innerText&&"div"===t.previousSibling.localName;)t=t.previousSibling;for(n.push(t);null!==t.nextSibling&&"\n"!==t.nextSibling.innerText&&"div"===t.nextSibling.localName;)t=t.nextSibling,n.push(t);n.forEach(e=>{e.style&&(e.style.background=bitty.config.flashBackground,e.style.color=bitty.config.flashColor)}),setTimeout(e=>{n.forEach(e=>{e.style&&(e.style.background=i,e.style.color=r)})},this.config.flashTime),e=n.map(e=>e.innerText).join("\n"),this.publish("run",e)},runSelection(){var i=window.getSelection();let r=i.toString();if(""===r){let e=i.anchorNode.parentElement;for(;"div"!==e.localName;)e=e.parentElement;r=e.innerText;let t=e.style.background,n=e.style.color;e.style.background=bitty.config.flashBackground,e.style.color=bitty.config.flashColor,setTimeout(()=>{e.style.background=t,e.style.color=n},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",r)},editor(e,o,i=" "){let s=e,l=()=>{var e=window.getSelection().getRangeAt(0),t=e.cloneRange();return t.selectNodeContents(o),t.setEnd(e.endContainer,e.endOffset),t.toString().length},a=(e,t=o)=>{for(var n of t.childNodes)if(n.nodeType==Node.TEXT_NODE){var i,r;if(n.length>=e)return i=document.createRange(),r=window.getSelection(),i.setStart(n,e),i.collapse(!0),r.removeAllRanges(),r.addRange(i),-1;e-=n.length}else if((e=a(e,n))<0)return e;return e};new MutationObserver(e=>{e.forEach(e=>{e.addedNodes.length?s.publish("nodes added",e.addedNodes):s.publish("nodes removed",e.removedNodes)})}).observe(o,{childList:!0});o.addEventListener("paste",e=>{var t=(e.clipboardData||window.clipboardData).getData("text/plain");if(1!==t.split("\n").length&&window.getSelection().rangeCount){let i=l();var n=t.split("\n").length-1,t=(i+=t.length-n,""===e.target.innerText||" "===e.target.innerText||"\n"===e.target.innerText||"<br>"===e.target.innerText);e.target!==s.el&&t&&e.target.remove(),setTimeout(()=>{s.el.innerHTML=s.divide(s.value),s.process(),setTimeout(()=>{for(var e of Array.from(s.el.childNodes))if(3!==e.nodeType){var t=e.querySelectorAll("div");if(1<t.length)for(var n of t)s.el.insertBefore(n,e)}a(i)},0)},0),s.publish("paste",e)}}),o.addEventListener("keydown",e=>{var t,n;9===e.keyCode?(t=l()+i.length,(n=window.getSelection().getRangeAt(0)).deleteContents(),n.insertNode(document.createTextNode(i)),a(t),e.preventDefault()):8===e.keyCode&&setTimeout(e=>{if(1===s.el.childNodes.length&&"br"===s.el.firstChild.localName){var t=document.createElement("div");t.innerHTML=" ",t.className=s.el.firstChild.className,s.el.firstChild.remove(),s.el.appendChild(t),a(0)}else{var n,i,r;for(n of Array.from(s.el.childNodes))"br"===n.localName&&(i=document.createElement("div"),r=l(),i.innerHTML=" ",n.replaceWith(i),a(r+1))}},10),"z"===e.key&&(e.ctrlKey||e.metaKey)?(e.shiftKey?s.undoManager.redo():s.undoManager.undo(),e.preventDefault()):"y"===e.key&&(e.ctrlKey||e.metaKey)?(s.undoManager.redo(),e.preventDefault()):s.publish("keydown",e)}),o.addEventListener("keyup",e=>{if(!e.ctrlKey&&48<=e.keyCode||32===e.keyCode){var t=l();s.process(),a(t)}else switch(e.keyCode){case 8:break;case 13:e.ctrlKey?s.runSelection():e.altKey&&s.runBlock()}s.publish("keyup",e)}),o.addEventListener("click",e=>s.publish("click",e))}};