Skip to main content

lib/assets/phoenix_ex_ratatui/main.js

var g={black:"#000000",red:"#cc0000",green:"#4e9a06",yellow:"#c4a000",blue:"#3465a4",magenta:"#75507b",cyan:"#06989a",gray:"#d3d7cf",dark_gray:"#555753",light_red:"#ef2929",light_green:"#8ae234",light_yellow:"#fce94f",light_blue:"#729fcf",light_magenta:"#ad7fa8",light_cyan:"#34e2e2",white:"#eeeeec"},b=g.white,w=g.black;function p(e){if(e==null||e==="reset")return null;if(typeof e=="string")return g[e]??null;if(Array.isArray(e)){if(e[0]==="rgb")return`rgb(${e[1]}, ${e[2]}, ${e[3]})`;if(e[0]==="indexed")return m(e[1])}return null}function m(e){if(e<16)return g[["black","red","green","yellow","blue","magenta","cyan","gray","dark_gray","light_red","light_green","light_yellow","light_blue","light_magenta","light_cyan","white"][e]];if(e<232){let i=e-16,o=Math.floor(i/36),n=Math.floor(i%36/6),h=i%6,s=a=>a===0?0:55+a*40;return`rgb(${s(o)}, ${s(n)}, ${s(h)})`}let t=(e-232)*10+8;return`rgb(${t}, ${t}, ${t})`}var y="phx-ex-ratatui-cell-style";function E(){if(document.getElementById(y))return;let e=document.createElement("style");e.id=y,e.textContent=".pxr-row{display:flex;line-height:1}.pxr-cell{display:inline-block;width:var(--pxr-cw);height:var(--pxr-ch);text-align:center}",document.head.appendChild(e)}function x(e,t,i){let o=p(e)||"",n=p(t)||"",h=!1,s=!1,a=!1,r=!1,d=!1,c=!1;for(let u=0;u<i.length;u++){let f=i[u];f==="bold"?h=!0:f==="italic"?s=!0:f==="dim"?a=!0:f==="underlined"?r=!0:f==="crossed_out"?d=!0:f==="reversed"&&(c=!0)}if(c){let u=n||w,f=o||b;o=u,n=f}let l="";return o&&(l="color:"+o),n&&(l+=(l?";":"")+"background-color:"+n),h&&(l+=(l?";":"")+"font-weight:bold"),s&&(l+=(l?";":"")+"font-style:italic"),a&&(l+=(l?";":"")+"opacity:0.6"),r&&d?l+=(l?";":"")+"text-decoration:underline line-through":r?l+=(l?";":"")+"text-decoration:underline":d&&(l+=(l?";":"")+"text-decoration:line-through"),{fg:o,bg:n,css:l}}var _={ArrowUp:"up",ArrowDown:"down",ArrowLeft:"left",ArrowRight:"right",Enter:"enter",Escape:"esc",Backspace:"backspace",Tab:"tab",Delete:"delete",Insert:"insert",Home:"home",End:"end",PageUp:"page_up",PageDown:"page_down"};function C(e){return _[e.key]?_[e.key]:/^F\d+$/.test(e.key)?e.key.toLowerCase():e.key}function k(e){let t=[];return e.ctrlKey&&t.push("ctrl"),e.shiftKey&&t.push("shift"),e.altKey&&t.push("alt"),e.metaKey&&t.push("meta"),t}var v={mounted(){this.cells=[],this.charWidth=0,this.charHeight=0,this.el.tabIndex<0&&(this.el.tabIndex=0),this.el.style.fontFamily||(this.el.style.fontFamily="ui-monospace, monospace"),this.el.style.whiteSpace||(this.el.style.whiteSpace="pre"),this.el.style.lineHeight||(this.el.style.lineHeight="1"),this.el.style.overflow||(this.el.style.overflow="hidden"),E(),this.el.dataset.phxExRatatuiAutofocus==="true"&&this.el.focus({preventScroll:!0}),this.measureChar(),this.reportSize(),this.handleEvent("phx_ex_ratatui:render",e=>this.applyDiff(e)),this.resizeObserver=new ResizeObserver(()=>this.reportSize()),this.resizeObserver.observe(this.el),this.keydownListener=e=>this.onKeydown(e),this.el.addEventListener("keydown",this.keydownListener)},destroyed(){this.resizeObserver&&this.resizeObserver.disconnect(),this.keydownListener&&this.el.removeEventListener("keydown",this.keydownListener)},measureChar(){let e=document.createElement("span");e.style.cssText="visibility:hidden;position:absolute;font:inherit;white-space:pre",e.textContent="M",this.el.appendChild(e);let t=e.getBoundingClientRect();this.charWidth=t.width||8,this.charHeight=t.height||16,e.remove(),this.el.style.setProperty("--pxr-cw",this.charWidth+"px"),this.el.style.setProperty("--pxr-ch",this.charHeight+"px")},reportSize(){if(!this.charWidth||!this.charHeight)return;let e=this.el.getBoundingClientRect(),t=Math.floor(e.width/this.charWidth),i=Math.floor(e.height/this.charHeight);(t<1||i<1)&&(t=80,i=24),this.pushEventTo(this.el,"phx_ex_ratatui:resize",{cols:t,rows:i})},applyDiff({width:e,height:t,ops:i}){(this.cells.length!==t||this.cells[0]&&this.cells[0].length!==e)&&this.buildGrid(e,t);for(let n=0;n<i.length;n++){let[h,s,a,r,d,c,l]=i[n];this.setCell(h,s,a,r,d,c,l)}},buildGrid(e,t){this.el.replaceChildren(),this.cells=[];for(let i=0;i<t;i++){let o=document.createElement("div");o.className="pxr-row";let n=new Array(e);for(let h=0;h<e;h++){let s=document.createElement("span");s.className="pxr-cell",s.textContent=" ",s._sym=" ",s._fg="",s._bg="",s._mods="",o.appendChild(s),n[h]=s}this.cells.push(n),this.el.appendChild(o)}},setCell(e,t,i,o,n,h,s){let a=this.cells[e];if(!a)return;let r=a[t];if(!r||s)return;let d=h.length?h.join(","):"",c=x(o,n,h);r._sym===i&&r._fg===c.fg&&r._bg===c.bg&&r._mods===d||(r._sym=i,r._fg=c.fg,r._bg=c.bg,r._mods=d,r.textContent!==i&&(r.textContent=i),r.style.cssText=c.css)},onKeydown(e){e.preventDefault(),this.pushEventTo(this.el,"phx_ex_ratatui:input",{kind:"key",code:C(e),modifiers:k(e),press_kind:"press"})}},A=v,L={colorToCss:p,indexedColor:m,buildStyle:x,keyToCode:C,modifiersFor:k,NAMED_COLORS:g,DEFAULT_FG:b,DEFAULT_BG:w};export{v as PhoenixExRatatuiHook,L as __test__,A as default};