regular-layout 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var b={type:"split-panel",orientation:"horizontal",sizes:[],children:[]};var _=Object.freeze({CUSTOM_EVENT_NAME_PREFIX:"regular-layout",CHILD_ATTRIBUTE_NAME:"name",MIN_DRAG_DISTANCE:10,SHOULD_ROUND:!1,OVERLAY_CLASSNAME:"overlay",MINIMUM_REDISTRIBUTION_SIZE_THRESHOLD:.15,SPLIT_EDGE_TOLERANCE:.25,SPLIT_ROOT_EDGE_TOLERANCE:.01,GRID_TRACK_COLLAPSE_TOLERANCE:.001,OVERLAY_DEFAULT:"absolute",GRID_DIVIDER_SIZE:6,GRID_DIVIDER_CHECK_TARGET:!0});function H(r,e,t){return(e.length===0||Math.abs(t-e[e.length-1])>r.GRID_TRACK_COLLAPSE_TOLERANCE)&&e.push(t),e}function V(r,e){return e.sort((n,o)=>n-o).reduce(H.bind(void 0,r),[])}function R(r,e,t,n,o){if(r.type==="child-panel")return[t,n];let i=[t,n];if(r.orientation===e){let s=t,a=n-t;for(let c=0;c<r.children.length;c++){let d=r.sizes[c],h=s+d*a;i.push(...R(r.children[c],e,s,h,o)),s=h}}else for(let s of r.children)i.push(...R(s,e,t,n,o));return V(o,i)}function C(r,e,t){let n=e.findIndex(o=>Math.abs(o-t)<r.GRID_TRACK_COLLAPSE_TOLERANCE);return n===-1?0:n}function M(r,e,t,n,o,i,s,a){if(r.type==="child-panel"){let m=r.selected??0;return[{child:r.tabs[m],colStart:C(a,e,n),colEnd:C(a,e,o),rowStart:C(a,t,i),rowEnd:C(a,t,s)}]}let{children:c,sizes:d,orientation:h}=r,l=h==="horizontal",u=l?n:i,p=l?o-n:s-i,f=[];for(let m=0;m<c.length;m++){let E=u+d[m]*p;l?f.push(...M(c[m],e,t,u,E,i,s,a)):f.push(...M(c[m],e,t,n,o,u,E,a)),u=E}return f}var x=(r,e)=>`:host ::slotted(*){display:none}:host{display:grid;grid-template-rows:${r};grid-template-columns:${e}}`,A=(r,e,t,n)=>`:host ::slotted([${r.CHILD_ATTRIBUTE_NAME}="${e}"]){display:flex;grid-column:${n};grid-row:${t}}`;function v(r,e,t=_){if(r.type==="child-panel"){let l=r.selected??0;return[x("100%","100%"),A(t,r.tabs[l],"1","1")].join(`
2
- `)}let n=l=>l.slice(0,-1).map((p,f)=>l[f+1]-p).map(p=>`${t.SHOULD_ROUND?Math.round(p*100):p*100}fr`).join(" "),o=R(r,"horizontal",0,1,t),i=n(o),s=R(r,"vertical",0,1,t),a=n(s),c=(l,u)=>u-l===1?`${l+1}`:`${l+1} / ${u+1}`,d=M(r,o,s,0,1,0,1,t),h=[x(a,i)];for(let l of d){let u=c(l.colStart,l.colEnd),p=c(l.rowStart,l.rowEnd);h.push(A(t,l.child,p,u)),l.child===e?.[1]&&(h.push(A(t,e[0],p,u)),h.push(`:host ::slotted([${t.CHILD_ATTRIBUTE_NAME}=${e[0]}]){z-index:1}`))}return h.join(`
3
- `)}var F={row_start:0,row_end:1,col_start:0,col_end:1};function y(r,e,t,n=null){return U(r,e,t,n)}function U(r,e,t,n,o=null,i=structuredClone(F),s=[]){if(r<0||e<0||r>1||e>1)return null;if(t.type==="child-panel"){let f=t.selected??0,m=i.col_end-i.col_start,E=i.row_end-i.row_start;return{type:"layout-path",layout:t,slot:t.tabs[f],path:s,view_window:i,is_edge:!1,column:r,row:e,column_offset:(r-i.col_start)/m,row_offset:(e-i.row_start)/E,orientation:o||"horizontal"}}let a=t.orientation==="vertical",c=a?e:r,d=a?"row_start":"col_start",h=a?"row_end":"col_end",l=a?n?.rect?.height:n?.rect?.width,u=i[d],p=i[h]-i[d];for(let f=0;f<t.children.length;f++){let m=u+p*t.sizes[f];if(n&&l){let E=n.size/l;if(Math.abs(c-m)<E)return{path:[...s,f],type:t.orientation,view_window:{...i,[d]:u,[h]:m}}}if(c>=u&&c<m)return U(r,e,t.children[f],n,t.orientation,{...i,[d]:u,[h]:m},[...s,f]);u=m}return null}function T(r,e){if(r.type==="child-panel"){if(r.tabs.includes(e)){let s=r.tabs.filter(a=>a!==e);return s.length===0?structuredClone(b):{type:"child-panel",tabs:s}}return structuredClone(r)}let t=structuredClone(r),n=t.children.findIndex(s=>s.type==="child-panel"?s.tabs.includes(e):!1);if(n!==-1){let s=t.children[n];if(s.tabs.length===1){let a=t.children.filter((d,h)=>h!==n),c=$(t.sizes,n);if(a.length===1)return a[0];t.children=a,t.sizes=c}else s.tabs.splice(s.tabs.indexOf(e),1),s.selected&&s.selected>=s.tabs.length&&s.selected--;return t}let o=!1,i=t.children.map(s=>{if(s.type==="split-panel"){let a=T(s,e);return a!==s&&(o=!0),a}return s});return o&&(t.children=i),t}function $(r,e){let t=[],n=r[e],o=0;for(let i=0;i<r.length;i++)i!==e&&(o+=r[i]);for(let i=0;i<r.length;i++)if(i!==e){let s=r[i]/o;t.push(r[i]+n*s)}return t}function L(r,e,t,n){let o=l=>({type:"child-panel",tabs:[l]});if(t.length===0){if(r.type==="child-panel")return{type:"child-panel",tabs:[e,...r.tabs]};if(n)return{type:"split-panel",orientation:n,children:[o(e),r],sizes:[.5,.5]};{let l=[...r.children,o(e)],u=[...r.sizes,1/(l.length-1)];return{...r,children:l,sizes:P(u)}}}let[i,...s]=t;if(n&&s.length===0){if(r.type==="split-panel"&&r.orientation===n){let u=[...r.children];u.splice(i,0,o(e));let p=[...r.sizes];return p.splice(i,0,1/(u.length-1)),{...r,children:u,sizes:P(p)}}let l=i===0?[o(e),r]:[r,o(e)];return{type:"split-panel",orientation:n,children:l,sizes:[.5,.5]}}if(r.type==="child-panel"){if(s.length===0&&n===void 0&&i>=0&&i<=r.tabs.length){let u=[...r.tabs];return u.splice(i,0,e),{...r,tabs:u}}return L({type:"split-panel",orientation:n||"horizontal",children:[r],sizes:[1]},e,t,n)}if(s.length===0||i===r.children.length){if(n&&r.children[i]){let p={type:"split-panel",orientation:n,children:[o(e),r.children[i]],sizes:[.5,.5]},f=[...r.children];return f[i]=p,{...r,children:f,sizes:P(r.sizes)}}let l=[...r.children];l.splice(i,0,o(e));let u=[...r.sizes];return u.splice(i,0,1/(l.length-1)),{...r,children:l,sizes:P(u)}}let a=r.children[i],c=a.type==="child-panel"&&s.length>0&&n!==void 0?r.orientation==="horizontal"?"vertical":"horizontal":n,d=L(a,e,s,c),h=[...r.children];return h[i]=d,{...r,children:h}}function P(r){let e=r.reduce((t,n)=>t+n,0);return r.map(t=>t/e)}function I(r,e,t,n=_){let o=structuredClone(r),i=o,s={horizontal:t||0,vertical:t||0};for(let a=0;a<e.length-1;a++)i.type==="split-panel"&&(s[i.orientation]/=i.sizes[e[a]],i=i.children[e[a]]);if(i.type==="split-panel")if(t===void 0)i.sizes=i.sizes.map(a=>1/i.sizes.length);else{let a=s[i.orientation],c=e[e.length-1];c<i.sizes.length-1&&(i.sizes=Y(n,i.sizes,c,a))}return o}function Y(r,e,t,n){let o=[...e],i=0;for(let a=0;a<=t;a++)i+=e[a];let s=0;for(let a=t+1;a<e.length;a++)s+=e[a];n=Math.sign(n)*Math.min(Math.abs(n),(1-r.MINIMUM_REDISTRIBUTION_SIZE_THRESHOLD)*(n>0?i:s));for(let a=0;a<=t;a++){let c=e[a]/i;o[a]=e[a]-n*c}for(let a=t+1;a<e.length;a++){let c=e[a]/s;o[a]=e[a]+n*c}return o}function G(r,e,t,n,o=_){if(!n)return`:host ::slotted([${o.CHILD_ATTRIBUTE_NAME}="${r}"]){display:none;}`;let{view_window:{row_start:i,row_end:s,col_start:a,col_end:c}}=n,d=e.height-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom),h=e.width-parseFloat(t.paddingLeft)-parseFloat(t.paddingRight),l=i*d+parseFloat(t.paddingTop),u=a*h+parseFloat(t.paddingLeft),p=(s-i)*d,f=(c-a)*h,m=`display:flex;position:absolute!important;z-index:1;top:${l}px;left:${u}px;height:${p}px;width:${f}px;`;return`::slotted([${o.CHILD_ATTRIBUTE_NAME}="${r}"]){${m}}`}function z(r,e,t,n,o,i,s=_){if(r<s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,n,o,[0],!0,"horizontal");if(r>1-s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,n,o,o.path.length>0?o.path:[1],!1,"horizontal");if(e<s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,n,o,[0],!0,"vertical");if(e>1-s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,n,o,o.path.length>0?o.path:[1],!1,"vertical");let a=o.column_offset<s.SPLIT_EDGE_TOLERANCE||o.column_offset>1-s.SPLIT_EDGE_TOLERANCE,c=o.row_offset<s.SPLIT_EDGE_TOLERANCE||o.row_offset>1-s.SPLIT_EDGE_TOLERANCE;if(a&&c){let d=Math.abs(o.column_offset-.5),h=Math.abs(o.row_offset-.5),l=(i?.width||1)*(o.view_window.col_end-o.view_window.col_start),u=(i?.height||1)*(o.view_window.row_end-o.view_window.row_start),p=l/2-d*l<u/2-h*u;return w(t,n,o,p?o.column_offset<s.SPLIT_EDGE_TOLERANCE:o.row_offset<s.SPLIT_EDGE_TOLERANCE,p?"horizontal":"vertical")}return a?w(t,n,o,o.column_offset<s.SPLIT_EDGE_TOLERANCE,"horizontal"):c?w(t,n,o,o.row_offset<s.SPLIT_EDGE_TOLERANCE,"vertical"):{...o,path:[...o.path,0]}}function D(r,e,t,n,o,i){return w(r,e,{...t,path:n,orientation:i},o,i)}function w(r,e,t,n,o){let i;if(t.orientation===o)if(t.path.length===0)i=[n?0:1];else{let c=t.path[t.path.length-1];i=[...t.path.slice(0,-1),n?c:c+1]}else i=[...t.path,n?0:1];let s=L(r,e,i,o),a=k(s,i);return{...t,path:i,slot:t.slot,is_edge:!0,orientation:o,view_window:a}}function k(r,e){let t={row_start:0,row_end:1,col_start:0,col_end:1},n=r;for(let o of e){if(n.type==="child-panel")break;let i=Math.min(o,n.children.length-1),s=n.orientation==="vertical",a=s?"row_start":"col_start",c=s?"row_end":"col_end",d=t[c]-t[a],h=n.sizes.slice(0,i).reduce((l,u)=>l+u*d,t[a]);t={...t,[a]:h,[c]:h+d*n.sizes[i]},n=n.children[i]}return t}function N(r){if(r.type==="child-panel")return r.selected=r.selected||0,r;let e=[],t=[];for(let n=0;n<r.children.length;n++){let o=r.children[n],i=r.sizes[n],s=N(o);if(s.type==="split-panel"&&s.orientation===r.orientation)for(let a=0;a<s.children.length;a++)e.push(s.children[a]),t.push(s.sizes[a]*i);else e.push(s),t.push(i)}return e.length===1?e[0]:{type:"split-panel",orientation:r.orientation,children:e,sizes:t}}var g=class extends HTMLElement{s;n;c;u;h;a;d;t;constructor(){super(),this.t=_,this.n=structuredClone(b),this.s=this.attachShadow({mode:"open"}),this.s.innerHTML="<slot></slot>",this.c=new CSSStyleSheet,this.u=new CSSStyleSheet,this.a=!1,this.s.adoptedStyleSheets=[this.c,this.u]}connectedCallback(){this.addEventListener("dblclick",this.onDblClick),this.addEventListener("pointerdown",this.onPointerDown),this.addEventListener("pointerup",this.onPointerUp),this.addEventListener("pointermove",this.onPointerMove)}disconnectedCallback(){this.removeEventListener("dblclick",this.onDblClick),this.removeEventListener("pointerdown",this.onPointerDown),this.removeEventListener("pointerup",this.onPointerUp),this.removeEventListener("pointermove",this.onPointerMove)}calculateIntersect=e=>{let[t,n,o]=this.relativeCoordinates(e,!1);return y(t,n,this.n)};setOverlayState=(e,{slot:t},n=this.t.OVERLAY_CLASSNAME,o=this.t.OVERLAY_DEFAULT)=>{let i=T(this.n,t),s=`:scope > [${this.t.CHILD_ATTRIBUTE_NAME}="${t}"]`,a=this.querySelector(s);a&&a.classList.add(n);let[c,d,h,l]=this.relativeCoordinates(e,!0),u=y(c,d,i);if(u&&(u=z(c,d,i,t,u,h,this.t)),o==="grid"&&u){let m=[t,u?.slot],E=v(i,m,this.t);this.c.replaceSync(E)}else if(o==="absolute"){let m=v(i,void 0,this.t),E=G(t,h,l,u,this.t);this.c.replaceSync([m,E].join(`
4
- `))}let p=`${this.t.CUSTOM_EVENT_NAME_PREFIX}-before-update`,f=new CustomEvent(p,{detail:i});this.dispatchEvent(f)};clearOverlayState=(e,{slot:t,layout:n},o=this.t.OVERLAY_CLASSNAME)=>{let i=this.n;i=T(i,t);let s=`:scope > [${this.t.CHILD_ATTRIBUTE_NAME}="${t}"]`,a=this.querySelector(s);if(a&&a.classList.remove(o),e===null){this.restore(n);return}let[c,d,h]=this.relativeCoordinates(e,!1),l=y(c,d,i);if(l&&(l=z(c,d,i,t,l,h,this.t)),l){let u=l?.is_edge?l.orientation:void 0,p=L(i,t,l.path,u);this.restore(p)}else this.restore(n)};insertPanel=(e,t=[],n)=>{let o;typeof n=="boolean"&&n?o="horizontal":typeof n=="string"&&(o=n),this.restore(L(this.n,e,t,o))};removePanel=e=>{this.restore(T(this.n,e))};getPanel=(e,t=this.n)=>{if(t.type==="child-panel")return t.tabs.includes(e)?t:null;for(let n of t.children){let o=this.getPanel(e,n);if(o)return o}return null};clear=()=>{this.restore(b)};restore=(e,t=!1)=>{this.n=t?e:N(e);let n=v(this.n,void 0,this.t);this.c.replaceSync(n);let o=`${this.t.CUSTOM_EVENT_NAME_PREFIX}-update`,i=new CustomEvent(o,{detail:this.n});this.dispatchEvent(i)};save=()=>structuredClone(this.n);restorePhysics(e){this.t=Object.freeze({...this.t,...e})}savePhysics(){return this.t}relativeCoordinates=(e,t=!0)=>{(t||!this.d)&&(this.d={box:this.getBoundingClientRect(),style:getComputedStyle(this)});let n=this.d.box,o=this.d.style,i=(e.clientX-n.left-parseFloat(o.paddingLeft))/(n.width-parseFloat(o.paddingLeft)-parseFloat(o.paddingRight)),s=(e.clientY-n.top-parseFloat(o.paddingTop))/(n.height-parseFloat(o.paddingTop)-parseFloat(o.paddingBottom));return[i,s,n,o]};diffCoordinates=(e,t)=>{let[n,o,i]=this.relativeCoordinates(e,!1),s=(n-t.column)*i.width,a=(o-t.row)*i.height;return Math.sqrt(s**2+a**2)};onDblClick=e=>{let[t,n,o]=this.relativeCoordinates(e,!1),i=y(t,n,this.n,{rect:o,size:this.t.GRID_DIVIDER_SIZE});if(i?.type==="horizontal"||i?.type==="vertical"){let s=I(this.n,i.path,void 0);this.restore(s,!0)}};onPointerDown=e=>{if(!this.t.GRID_DIVIDER_CHECK_TARGET||e.target===this){let[t,n,o]=this.relativeCoordinates(e),i=this.t.GRID_DIVIDER_SIZE,s=y(t,n,this.n,{rect:o,size:i});s&&s.type!=="layout-path"&&(this.h=[s,t,n],this.setPointerCapture(e.pointerId),e.preventDefault())}};onPointerMove=e=>{if(this.h){let[s,a]=this.relativeCoordinates(e,!1),[{path:c,type:d},h,l]=this.h,u=d==="horizontal"?h-s:l-a,p=I(this.n,c,u);this.c.replaceSync(v(p,void 0,this.t))}if(this.t.GRID_DIVIDER_CHECK_TARGET&&e.target!==this){this.a&&(this.a=!1,this.u.replaceSync(""));return}let[t,n,o]=this.relativeCoordinates(e,!1),i=y(t,n,this.n,{rect:o,size:this.t.GRID_DIVIDER_SIZE});i?.type==="vertical"?(this.u.replaceSync(":host{cursor:row-resize"),this.a=!0):i?.type==="horizontal"?(this.u.replaceSync(":host{cursor:col-resize"),this.a=!0):this.a&&(this.a=!1,this.u.replaceSync(""))};onPointerUp=e=>{if(this.h){this.releasePointerCapture(e.pointerId);let[t,n]=this.relativeCoordinates(e,!1),[{path:o,type:i},s,a]=this.h,c=i==="horizontal"?s-t:a-n,d=I(this.n,o,c);this.restore(d,!0),this.h=void 0}}};var B=`
1
+ var b={type:"split-layout",orientation:"horizontal",sizes:[],children:[]};var E=Object.freeze({CUSTOM_EVENT_NAME_PREFIX:"regular-layout",CHILD_ATTRIBUTE_NAME:"name",MIN_DRAG_DISTANCE:10,SHOULD_ROUND:!1,OVERLAY_CLASSNAME:"overlay",MINIMUM_REDISTRIBUTION_SIZE_THRESHOLD:.15,SPLIT_EDGE_TOLERANCE:.25,SPLIT_ROOT_EDGE_TOLERANCE:.01,GRID_TRACK_COLLAPSE_TOLERANCE:.001,OVERLAY_DEFAULT:"absolute",GRID_DIVIDER_SIZE:6,GRID_DIVIDER_CHECK_TARGET:!0});function F(r,e,t){return(e.length===0||Math.abs(t-e[e.length-1])>r.GRID_TRACK_COLLAPSE_TOLERANCE)&&e.push(t),e}function $(r,e){return e.sort((o,i)=>o-i).reduce(F.bind(void 0,r),[])}function C(r,e,t,o,i){if(r.type==="tab-layout")return[t,o];let n=[t,o];if(r.orientation===e){let s=t,a=o-t;for(let u=0;u<r.children.length;u++){let d=r.sizes[u],h=s+d*a;n.push(...C(r.children[u],e,s,h,i)),s=h}}else for(let s of r.children)n.push(...C(s,e,t,o,i));return $(i,n)}function S(r,e,t){let o=e.findIndex(i=>Math.abs(i-t)<r.GRID_TRACK_COLLAPSE_TOLERANCE);return o===-1?0:o}function M(r,e,t,o,i,n,s,a){if(r.type==="tab-layout"){let p=r.selected??0;return[{child:r.tabs[p],colStart:S(a,e,o),colEnd:S(a,e,i),rowStart:S(a,t,n),rowEnd:S(a,t,s)}]}let{children:u,sizes:d,orientation:h}=r,l=h==="horizontal",c=l?o:n,y=l?i-o:s-n,f=[];for(let p=0;p<u.length;p++){let m=c+d[p]*y;l?f.push(...M(u[p],e,t,c,m,n,s,a)):f.push(...M(u[p],e,t,o,i,c,m,a)),c=m}return f}var x=(r,e)=>`:host ::slotted(*){display:none}:host{display:grid;grid-template-rows:${r};grid-template-columns:${e}}`,A=(r,e,t,o)=>`:host ::slotted([${r.CHILD_ATTRIBUTE_NAME}="${e}"]){display:flex;grid-column:${o};grid-row:${t}}`;function v(r,e,t=E){if(r.type==="tab-layout"){let l=r.selected??0;return[x("100%","100%"),A(t,r.tabs[l],"1","1")].join(`
2
+ `)}let o=l=>l.slice(0,-1).map((y,f)=>l[f+1]-y).map(y=>`${t.SHOULD_ROUND?Math.round(y*100):y*100}fr`).join(" "),i=C(r,"horizontal",0,1,t),n=o(i),s=C(r,"vertical",0,1,t),a=o(s),u=(l,c)=>c-l===1?`${l+1}`:`${l+1} / ${c+1}`,d=M(r,i,s,0,1,0,1,t),h=[x(a,n)];for(let l of d){let c=u(l.colStart,l.colEnd),y=u(l.rowStart,l.rowEnd);h.push(A(t,l.child,y,c)),l.child===e?.[1]&&(h.push(A(t,e[0],y,c)),h.push(`:host ::slotted([${t.CHILD_ATTRIBUTE_NAME}=${e[0]}]){z-index:1}`))}return h.join(`
3
+ `)}var Y={row_start:0,row_end:1,col_start:0,col_end:1};function _(r,e,t,o=null){return U(r,e,t,o)}function U(r,e,t,o,i=null,n=structuredClone(Y),s=[]){if(r<0||e<0||r>1||e>1)return null;if(t.type==="tab-layout"){let f=t.selected??0,p=n.col_end-n.col_start,m=n.row_end-n.row_start;return{type:"layout-path",layout:t,slot:t.tabs[f],path:s,view_window:n,is_edge:!1,column:r,row:e,column_offset:(r-n.col_start)/p,row_offset:(e-n.row_start)/m,orientation:i||"horizontal"}}let a=t.orientation==="vertical",u=a?e:r,d=a?"row_start":"col_start",h=a?"row_end":"col_end",l=a?o?.rect?.height:o?.rect?.width,c=n[d],y=n[h]-n[d];for(let f=0;f<t.children.length;f++){let p=c+y*t.sizes[f];if(o&&l){let m=o.size/l;if(Math.abs(u-p)<m)return{path:[...s,f],type:t.orientation,view_window:{...n,[d]:c,[h]:p}}}if(u>=c&&u<p)return U(r,e,t.children[f],o,t.orientation,{...n,[d]:c,[h]:p},[...s,f]);c=p}return null}function T(r,e){if(r.type==="tab-layout"){if(r.tabs.includes(e)){let s=r.tabs.filter(a=>a!==e);return s.length===0?structuredClone(b):{type:"tab-layout",tabs:s}}return structuredClone(r)}let t=structuredClone(r),o=t.children.findIndex(s=>s.type==="tab-layout"?s.tabs.includes(e):!1);if(o!==-1){let s=t.children[o];if(s.tabs.length===1){let a=t.children.filter((d,h)=>h!==o),u=k(t.sizes,o);if(a.length===1)return a[0];t.children=a,t.sizes=u}else s.tabs.splice(s.tabs.indexOf(e),1),s.selected&&s.selected>=s.tabs.length&&s.selected--;return t}let i=!1,n=t.children.map(s=>{if(s.type==="split-layout"){let a=T(s,e);return a!==s&&(i=!0),a}return s});return i&&(t.children=n),t}function k(r,e){let t=[],o=r[e],i=0;for(let n=0;n<r.length;n++)n!==e&&(i+=r[n]);for(let n=0;n<r.length;n++)if(n!==e){let s=r[n]/i;t.push(r[n]+o*s)}return t}function L(r,e,t,o){let i=l=>({type:"tab-layout",tabs:[l]});if(t.length===0){if(r.type==="tab-layout")return{type:"tab-layout",tabs:[e,...r.tabs]};if(o)return{type:"split-layout",orientation:o,children:[i(e),r],sizes:[.5,.5]};{let l=[...r.children,i(e)],c=[...r.sizes,1/(l.length-1)];return{...r,children:l,sizes:R(c)}}}let[n,...s]=t;if(o&&s.length===0){if(r.type==="split-layout"&&r.orientation===o){let c=[...r.children];c.splice(n,0,i(e));let y=[...r.sizes];return y.splice(n,0,1/(c.length-1)),{...r,children:c,sizes:R(y)}}let l=n===0?[i(e),r]:[r,i(e)];return{type:"split-layout",orientation:o,children:l,sizes:[.5,.5]}}if(r.type==="tab-layout"){if(s.length===0&&o===void 0&&n>=0&&n<=r.tabs.length){let c=[...r.tabs];return c.splice(n,0,e),{...r,tabs:c}}return L({type:"split-layout",orientation:o||"horizontal",children:[r],sizes:[1]},e,t,o)}if(s.length===0||n===r.children.length){if(o&&r.children[n]){let y={type:"split-layout",orientation:o,children:[i(e),r.children[n]],sizes:[.5,.5]},f=[...r.children];return f[n]=y,{...r,children:f,sizes:R(r.sizes)}}let l=[...r.children];l.splice(n,0,i(e));let c=[...r.sizes];return c.splice(n,0,1/(l.length-1)),{...r,children:l,sizes:R(c)}}let a=r.children[n],u=a.type==="tab-layout"&&s.length>0&&o!==void 0?r.orientation==="horizontal"?"vertical":"horizontal":o,d=L(a,e,s,u),h=[...r.children];return h[n]=d,{...r,children:h}}function R(r){let e=r.reduce((t,o)=>t+o,0);return r.map(t=>t/e)}function I(r,e,t,o=E){let i=structuredClone(r),n=i,s={horizontal:t||0,vertical:t||0};for(let a=0;a<e.length-1;a++)n.type==="split-layout"&&(s[n.orientation]/=n.sizes[e[a]],n=n.children[e[a]]);if(n.type==="split-layout")if(t===void 0)n.sizes=n.sizes.map(a=>1/n.sizes.length);else{let a=s[n.orientation],u=e[e.length-1];u<n.sizes.length-1&&(n.sizes=B(o,n.sizes,u,a))}return i}function B(r,e,t,o){let i=[...e],n=0;for(let a=0;a<=t;a++)n+=e[a];let s=0;for(let a=t+1;a<e.length;a++)s+=e[a];o=Math.sign(o)*Math.min(Math.abs(o),(1-r.MINIMUM_REDISTRIBUTION_SIZE_THRESHOLD)*(o>0?n:s));for(let a=0;a<=t;a++){let u=e[a]/n;i[a]=e[a]-o*u}for(let a=t+1;a<e.length;a++){let u=e[a]/s;i[a]=e[a]+o*u}return i}function G(r,e,t,o,i=E){if(!o)return`:host ::slotted([${i.CHILD_ATTRIBUTE_NAME}="${r}"]){display:none;}`;let{view_window:{row_start:n,row_end:s,col_start:a,col_end:u}}=o,d=e.height-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom),h=e.width-parseFloat(t.paddingLeft)-parseFloat(t.paddingRight),l=n*d+parseFloat(t.paddingTop),c=a*h+parseFloat(t.paddingLeft),y=(s-n)*d,f=(u-a)*h,p=`display:flex;position:absolute!important;z-index:1;top:${l}px;left:${c}px;height:${y}px;width:${f}px;`;return`::slotted([${i.CHILD_ATTRIBUTE_NAME}="${r}"]){${p}}`}function z(r,e,t,o,i,n,s=E){if(r<s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,o,i,[0],!0,"horizontal");if(r>1-s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,o,i,i.path.length>0?i.path:[1],!1,"horizontal");if(e<s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,o,i,[0],!0,"vertical");if(e>1-s.SPLIT_ROOT_EDGE_TOLERANCE)return D(t,o,i,i.path.length>0?i.path:[1],!1,"vertical");let a=i.column_offset<s.SPLIT_EDGE_TOLERANCE||i.column_offset>1-s.SPLIT_EDGE_TOLERANCE,u=i.row_offset<s.SPLIT_EDGE_TOLERANCE||i.row_offset>1-s.SPLIT_EDGE_TOLERANCE;if(a&&u){let d=Math.abs(i.column_offset-.5),h=Math.abs(i.row_offset-.5),l=(n?.width||1)*(i.view_window.col_end-i.view_window.col_start),c=(n?.height||1)*(i.view_window.row_end-i.view_window.row_start),y=l/2-d*l<c/2-h*c;return w(t,o,i,y?i.column_offset<s.SPLIT_EDGE_TOLERANCE:i.row_offset<s.SPLIT_EDGE_TOLERANCE,y?"horizontal":"vertical")}return a?w(t,o,i,i.column_offset<s.SPLIT_EDGE_TOLERANCE,"horizontal"):u?w(t,o,i,i.row_offset<s.SPLIT_EDGE_TOLERANCE,"vertical"):{...i,path:[...i.path,0]}}function D(r,e,t,o,i,n){return w(r,e,{...t,path:o,orientation:n},i,n)}function w(r,e,t,o,i){let n;if(t.orientation===i)if(t.path.length===0)n=[o?0:1];else{let u=t.path[t.path.length-1];n=[...t.path.slice(0,-1),o?u:u+1]}else n=[...t.path,o?0:1];let s=L(r,e,n,i),a=W(s,n);return{...t,path:n,slot:t.slot,is_edge:!0,orientation:i,view_window:a}}function W(r,e){let t={row_start:0,row_end:1,col_start:0,col_end:1},o=r;for(let i of e){if(o.type==="tab-layout")break;let n=Math.min(i,o.children.length-1),s=o.orientation==="vertical",a=s?"row_start":"col_start",u=s?"row_end":"col_end",d=t[u]-t[a],h=o.sizes.slice(0,n).reduce((l,c)=>l+c*d,t[a]);t={...t,[a]:h,[u]:h+d*o.sizes[n]},o=o.children[n]}return t}function N(r){if(r.type==="tab-layout")return r.selected=r.selected||0,r;let e=[],t=[];for(let o=0;o<r.children.length;o++){let i=r.children[o],n=r.sizes[o],s=N(i);if(s.type==="split-layout"&&s.orientation===r.orientation)for(let a=0;a<s.children.length;a++)e.push(s.children[a]),t.push(s.sizes[a]*n);else e.push(s),t.push(n)}return e.length===1?e[0]:{type:"split-layout",orientation:r.orientation,children:e,sizes:t}}function H(r,e){return V(r,e,[])}function V(r,e,t){if(e.type==="tab-layout")return e.tabs.includes(r)?t:null;for(let o=0;o<e.children.length;o++){let i=V(r,e.children[o],[...t,o]);if(i)return i}return null}var g=class extends HTMLElement{s;o;u;c;h;a;d;t;constructor(){super(),this.t=E,this.o=structuredClone(b),this.s=this.attachShadow({mode:"open"}),this.s.innerHTML="<slot></slot>",this.u=new CSSStyleSheet,this.c=new CSSStyleSheet,this.a=!1,this.s.adoptedStyleSheets=[this.u,this.c]}connectedCallback(){this.addEventListener("dblclick",this.onDblClick),this.addEventListener("pointerdown",this.onPointerDown),this.addEventListener("pointerup",this.onPointerUp),this.addEventListener("pointermove",this.onPointerMove)}disconnectedCallback(){this.removeEventListener("dblclick",this.onDblClick),this.removeEventListener("pointerdown",this.onPointerDown),this.removeEventListener("pointerup",this.onPointerUp),this.removeEventListener("pointermove",this.onPointerMove)}calculateIntersect=e=>{let[t,o,i]=this.relativeCoordinates(e,!1);return _(t,o,this.o)};calculatePath=e=>H(e,this.o);setOverlayState=(e,{slot:t},o=this.t.OVERLAY_CLASSNAME,i=this.t.OVERLAY_DEFAULT)=>{let n=T(this.o,t),s=`:scope > [${this.t.CHILD_ATTRIBUTE_NAME}="${t}"]`,a=this.querySelector(s);a&&a.classList.add(o);let[u,d,h,l]=this.relativeCoordinates(e,!0),c=_(u,d,n);if(c&&(c=z(u,d,n,t,c,h,this.t)),i==="grid"&&c){let p=[t,c?.slot],m=v(n,p,this.t);this.u.replaceSync(m)}else if(i==="absolute"){let p=v(n,void 0,this.t),m=G(t,h,l,c,this.t);this.u.replaceSync([p,m].join(`
4
+ `))}let y=`${this.t.CUSTOM_EVENT_NAME_PREFIX}-before-update`,f=new CustomEvent(y,{detail:n});this.dispatchEvent(f)};clearOverlayState=(e,{slot:t,layout:o},i=this.t.OVERLAY_CLASSNAME)=>{let n=this.o;n=T(n,t);let s=`:scope > [${this.t.CHILD_ATTRIBUTE_NAME}="${t}"]`,a=this.querySelector(s);if(a&&a.classList.remove(i),e===null){this.restore(o);return}let[u,d,h]=this.relativeCoordinates(e,!1),l=_(u,d,n);if(l&&(l=z(u,d,n,t,l,h,this.t)),l){let c=l?.is_edge?l.orientation:void 0,y=L(n,t,l.path,c);this.restore(y)}else this.restore(o)};insertPanel=(e,t=[],o)=>{let i;typeof o=="boolean"&&o?i="horizontal":typeof o=="string"&&(i=o),this.restore(L(this.o,e,t,i))};removePanel=e=>{this.restore(T(this.o,e))};getPanel=(e,t=this.o)=>{if(t.type==="tab-layout")return t.tabs.includes(e)?t:null;for(let o of t.children){let i=this.getPanel(e,o);if(i)return i}return null};clear=()=>{this.restore(b)};restore=(e,t=!1)=>{this.o=t?e:N(e);let o=v(this.o,void 0,this.t);this.u.replaceSync(o);let i=`${this.t.CUSTOM_EVENT_NAME_PREFIX}-update`,n=new CustomEvent(i,{detail:this.o});this.dispatchEvent(n)};save=()=>structuredClone(this.o);restorePhysics(e){this.t=Object.freeze({...this.t,...e})}savePhysics(){return this.t}relativeCoordinates=(e,t=!0)=>{(t||!this.d)&&(this.d={box:this.getBoundingClientRect(),style:getComputedStyle(this)});let o=this.d.box,i=this.d.style,n=(e.clientX-o.left-parseFloat(i.paddingLeft))/(o.width-parseFloat(i.paddingLeft)-parseFloat(i.paddingRight)),s=(e.clientY-o.top-parseFloat(i.paddingTop))/(o.height-parseFloat(i.paddingTop)-parseFloat(i.paddingBottom));return[n,s,o,i]};diffCoordinates=(e,t)=>{let[o,i,n]=this.relativeCoordinates(e,!1),s=(o-t.column)*n.width,a=(i-t.row)*n.height;return Math.sqrt(s**2+a**2)};onDblClick=e=>{let[t,o,i]=this.relativeCoordinates(e,!1),n=_(t,o,this.o,{rect:i,size:this.t.GRID_DIVIDER_SIZE});if(n?.type==="horizontal"||n?.type==="vertical"){let s=I(this.o,n.path,void 0);this.restore(s,!0)}};onPointerDown=e=>{if(!this.t.GRID_DIVIDER_CHECK_TARGET||e.target===this){let[t,o,i]=this.relativeCoordinates(e),n=this.t.GRID_DIVIDER_SIZE,s=_(t,o,this.o,{rect:i,size:n});s&&s.type!=="layout-path"&&(this.h=[s,t,o],this.setPointerCapture(e.pointerId),e.preventDefault())}};onPointerMove=e=>{if(this.h){let[s,a]=this.relativeCoordinates(e,!1),[{path:u,type:d},h,l]=this.h,c=d==="horizontal"?h-s:l-a,y=I(this.o,u,c);this.u.replaceSync(v(y,void 0,this.t))}if(this.t.GRID_DIVIDER_CHECK_TARGET&&e.target!==this){this.a&&(this.a=!1,this.c.replaceSync(""));return}let[t,o,i]=this.relativeCoordinates(e,!1),n=_(t,o,this.o,{rect:i,size:this.t.GRID_DIVIDER_SIZE});n?.type==="vertical"?(this.c.replaceSync(":host{cursor:row-resize"),this.a=!0):n?.type==="horizontal"?(this.c.replaceSync(":host{cursor:col-resize"),this.a=!0):this.a&&(this.a=!1,this.c.replaceSync(""))};onPointerUp=e=>{if(this.h){this.releasePointerCapture(e.pointerId);let[t,o]=this.relativeCoordinates(e,!1),[{path:i,type:n},s,a]=this.h,u=n==="horizontal"?s-t:a-o,d=I(this.o,i,u);this.restore(d,!0),this.h=void 0}}};var K=`
5
5
  :host{box-sizing:border-box;flex-direction:column}
6
6
  :host::part(titlebar){display:flex;height:24px;user-select:none;overflow:hidden}
7
7
  :host::part(container){flex:1 1 auto}
@@ -9,8 +9,8 @@ var b={type:"split-panel",orientation:"horizontal",sizes:[],children:[]};var _=O
9
9
  :host::part(close){align-self:stretch}
10
10
  :host::slotted{flex:1 1 auto;}
11
11
  :host regular-layout-tab{width:0px;}
12
- `,W=`
12
+ `,Z=`
13
13
  <div part="titlebar"></div>
14
14
  <slot part="container"></slot>
15
- `,S=class extends HTMLElement{s;p;e;o;i=null;f=new WeakMap;connectedCallback(){this.p??=new CSSStyleSheet,this.p.replaceSync(B),this.s??=this.attachShadow({mode:"open"}),this.s.adoptedStyleSheets=[this.p],this.s.innerHTML=W,this.e=this.parentElement,this.o=this.s.children[0],this.o.addEventListener("pointerdown",this.onPointerDown),this.addEventListener("pointermove",this.onPointerMove),this.addEventListener("pointerup",this.onPointerUp),this.addEventListener("pointercancel",this.onPointerCancel),this.addEventListener("lostpointercapture",this.onPointerLost),this.e.addEventListener("regular-layout-update",this.drawTabs),this.e.addEventListener("regular-layout-before-update",this.drawTabs)}disconnectedCallback(){this.o.removeEventListener("pointerdown",this.onPointerDown),this.removeEventListener("pointermove",this.onPointerMove),this.removeEventListener("pointerup",this.onPointerUp),this.removeEventListener("pointercancel",this.onPointerUp),this.removeEventListener("lostpointercapture",this.onPointerLost),this.e.removeEventListener("regular-layout-update",this.drawTabs),this.e.removeEventListener("regular-layout-before-update",this.drawTabs)}onPointerDown=e=>{if(e.target.part.contains("tab")){let n=this.e.calculateIntersect(e);n?(this.i={path:n},this.setPointerCapture(e.pointerId),e.preventDefault()):this.i=null}};onPointerMove=e=>{if(this.i){let t=this.e.savePhysics();if(!this.i.moved&&this.e.diffCoordinates(e,this.i.path)<=t.MIN_DRAG_DISTANCE)return;this.i.moved=!0,this.e.setOverlayState(e,this.i.path)}};onPointerUp=e=>{this.i?.moved&&this.e.clearOverlayState(e,this.i.path)};onPointerCancel=e=>{this.i?.moved&&this.e.clearOverlayState(null,this.i.path)};onPointerLost=e=>{this.releasePointerCapture(e.pointerId),this.i=null};drawTabs=e=>{let t=this.e.savePhysics().CHILD_ATTRIBUTE_NAME,n=this.getAttribute(t);if(!n)return;let o=e.detail,i=this.e.getPanel(n,o);i||(i={type:"child-panel",tabs:[n],selected:0});for(let a=0;a<i.tabs.length;a++)if(a>=this.o.children.length){let c=document.createElement("regular-layout-tab");c.populate(this.e,i,a),this.o.appendChild(c),this.f.set(c,a)}else this.o.children[a].populate(this.e,i,a);let s=i.tabs.length;for(let a=this.o.children.length-1;a>=s;a--)this.o.removeChild(this.o.children[a])}};var O=class extends HTMLElement{e;r;l;populate=(e,t,n)=>{if(this.r){if(n===t.selected!=(n===this.r?.selected)||this.r?.tabs[n]!==t.tabs[n]){let s=t.selected===n,a=t.tabs[n];this.children[0].textContent=a,s?(this.children[1].part.add("active-close"),this.part.add("active-tab")):(this.children[1].part.remove("active-close"),this.part.remove("active-tab"))}}else{let o=t.tabs[n],i=t.selected===n,s=i?"active-close close":"close";this.innerHTML=`<div part="title"></div><button part="${s}"></button>`,i?this.part.add("tab","active-tab"):this.part.add("tab"),this.addEventListener("pointerdown",this.onTabClick),this.children[0].textContent=o,this.children[1].addEventListener("pointerdown",this.onTabClose)}this.r=t,this.e=e,this.l=n};onTabClose=e=>{this.r!==void 0&&this.l!==void 0&&this.e?.removePanel(this.r.tabs[this.l])};onTabClick=e=>{if(this.r!==void 0&&this.l!==void 0&&this.l!==this.r.selected){let t=this.e?.save(),n=this.e?.getPanel(this.r.tabs[this.l],t);n&&t&&(n.selected=this.l,this.e?.restore(t))}}};customElements.define("regular-layout",g);customElements.define("regular-layout-frame",S);customElements.define("regular-layout-tab",O);export{g as RegularLayout,S as RegularLayoutFrame};
15
+ `,P=class extends HTMLElement{s;y;e;r;n=null;f=new WeakMap;connectedCallback(){this.y??=new CSSStyleSheet,this.y.replaceSync(K),this.s??=this.attachShadow({mode:"open"}),this.s.adoptedStyleSheets=[this.y],this.s.innerHTML=Z,this.e=this.parentElement,this.r=this.s.children[0],this.r.addEventListener("pointerdown",this.onPointerDown),this.addEventListener("pointermove",this.onPointerMove),this.addEventListener("pointerup",this.onPointerUp),this.addEventListener("pointercancel",this.onPointerCancel),this.addEventListener("lostpointercapture",this.onPointerLost),this.e.addEventListener("regular-layout-update",this.drawTabs),this.e.addEventListener("regular-layout-before-update",this.drawTabs)}disconnectedCallback(){this.r.removeEventListener("pointerdown",this.onPointerDown),this.removeEventListener("pointermove",this.onPointerMove),this.removeEventListener("pointerup",this.onPointerUp),this.removeEventListener("pointercancel",this.onPointerUp),this.removeEventListener("lostpointercapture",this.onPointerLost),this.e.removeEventListener("regular-layout-update",this.drawTabs),this.e.removeEventListener("regular-layout-before-update",this.drawTabs)}onPointerDown=e=>{if(e.target.part.contains("tab")){let o=this.e.calculateIntersect(e);o?(this.n={path:o},this.setPointerCapture(e.pointerId),e.preventDefault()):this.n=null}};onPointerMove=e=>{if(this.n){let t=this.e.savePhysics();if(!this.n.moved&&this.e.diffCoordinates(e,this.n.path)<=t.MIN_DRAG_DISTANCE)return;this.n.moved=!0,this.e.setOverlayState(e,this.n.path)}};onPointerUp=e=>{this.n?.moved&&this.e.clearOverlayState(e,this.n.path)};onPointerCancel=e=>{this.n?.moved&&this.e.clearOverlayState(null,this.n.path)};onPointerLost=e=>{this.releasePointerCapture(e.pointerId),this.n=null};drawTabs=e=>{let t=this.e.savePhysics().CHILD_ATTRIBUTE_NAME,o=this.getAttribute(t);if(!o)return;let i=e.detail,n=this.e.getPanel(o,i);n||(n={type:"tab-layout",tabs:[o],selected:0});for(let a=0;a<n.tabs.length;a++)if(a>=this.r.children.length){let u=document.createElement("regular-layout-tab");u.populate(this.e,n,a),this.r.appendChild(u),this.f.set(u,a)}else this.r.children[a].populate(this.e,n,a);let s=n.tabs.length;for(let a=this.r.children.length-1;a>=s;a--)this.r.removeChild(this.r.children[a])}};var O=class extends HTMLElement{e;i;l;populate=(e,t,o)=>{if(this.i){if(o===t.selected!=(o===this.i?.selected)||this.i?.tabs[o]!==t.tabs[o]){let s=t.selected===o,a=t.tabs[o];this.children[0].textContent=a,s?(this.children[1].part.add("active-close"),this.part.add("active-tab")):(this.children[1].part.remove("active-close"),this.part.remove("active-tab"))}}else{let i=t.tabs[o],n=t.selected===o,s=n?"active-close close":"close";this.innerHTML=`<div part="title"></div><button part="${s}"></button>`,n?this.part.add("tab","active-tab"):this.part.add("tab"),this.addEventListener("pointerdown",this.onTabClick),this.children[0].textContent=i,this.children[1].addEventListener("pointerdown",this.onTabClose)}this.i=t,this.e=e,this.l=o};onTabClose=e=>{this.i!==void 0&&this.l!==void 0&&this.e?.removePanel(this.i.tabs[this.l])};onTabClick=e=>{if(this.i!==void 0&&this.l!==void 0&&this.l!==this.i.selected){let t=this.e?.save(),o=this.e?.getPanel(this.i.tabs[this.l],t);o&&t&&(o.selected=this.l,this.e?.restore(t))}}};customElements.define("regular-layout",g);customElements.define("regular-layout-frame",P);customElements.define("regular-layout-tab",O);export{g as RegularLayout,P as RegularLayoutFrame};
16
16
  //# sourceMappingURL=index.js.map