regular-layout 0.0.1

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/LICENSE.md ADDED
@@ -0,0 +1,194 @@
1
+ Apache License
2
+ ==============
3
+
4
+ _Version 2.0, January 2004_
5
+ _&lt;<http://www.apache.org/licenses/>&gt;_
6
+
7
+ ### Terms and Conditions for use, reproduction, and distribution
8
+
9
+ #### 1. Definitions
10
+
11
+ “License” shall mean the terms and conditions for use, reproduction, and
12
+ distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ “Licensor” shall mean the copyright owner or entity authorized by the copyright
15
+ owner that is granting the License.
16
+
17
+ “Legal Entity” shall mean the union of the acting entity and all other entities
18
+ that control, are controlled by, or are under common control with that entity.
19
+ For the purposes of this definition, “control” means **(i)** the power, direct or
20
+ indirect, to cause the direction or management of such entity, whether by
21
+ contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or **(iii)** beneficial ownership of such entity.
23
+
24
+ “You” (or “Your”) shall mean an individual or Legal Entity exercising
25
+ permissions granted by this License.
26
+
27
+ “Source” form shall mean the preferred form for making modifications, including
28
+ but not limited to software source code, documentation source, and configuration
29
+ files.
30
+
31
+ “Object” form shall mean any form resulting from mechanical transformation or
32
+ translation of a Source form, including but not limited to compiled object code,
33
+ generated documentation, and conversions to other media types.
34
+
35
+ “Work” shall mean the work of authorship, whether in Source or Object form, made
36
+ available under the License, as indicated by a copyright notice that is included
37
+ in or attached to the work (an example is provided in the Appendix below).
38
+
39
+ “Derivative Works” shall mean any work, whether in Source or Object form, that
40
+ is based on (or derived from) the Work and for which the editorial revisions,
41
+ annotations, elaborations, or other modifications represent, as a whole, an
42
+ original work of authorship. For the purposes of this License, Derivative Works
43
+ shall not include works that remain separable from, or merely link (or bind by
44
+ name) to the interfaces of, the Work and Derivative Works thereof.
45
+
46
+ “Contribution” shall mean any work of authorship, including the original version
47
+ of the Work and any modifications or additions to that Work or Derivative Works
48
+ thereof, that is intentionally submitted to Licensor for inclusion in the Work
49
+ by the copyright owner or by an individual or Legal Entity authorized to submit
50
+ on behalf of the copyright owner. For the purposes of this definition,
51
+ “submitted” means any form of electronic, verbal, or written communication sent
52
+ to the Licensor or its representatives, including but not limited to
53
+ communication on electronic mailing lists, source code control systems, and
54
+ issue tracking systems that are managed by, or on behalf of, the Licensor for
55
+ the purpose of discussing and improving the Work, but excluding communication
56
+ that is conspicuously marked or otherwise designated in writing by the copyright
57
+ owner as “Not a Contribution.”
58
+
59
+ “Contributor” shall mean Licensor and any individual or Legal Entity on behalf
60
+ of whom a Contribution has been received by Licensor and subsequently
61
+ incorporated within the Work.
62
+
63
+ #### 2. Grant of Copyright License
64
+
65
+ Subject to the terms and conditions of this License, each Contributor hereby
66
+ grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
67
+ irrevocable copyright license to reproduce, prepare Derivative Works of,
68
+ publicly display, publicly perform, sublicense, and distribute the Work and such
69
+ Derivative Works in Source or Object form.
70
+
71
+ #### 3. Grant of Patent License
72
+
73
+ Subject to the terms and conditions of this License, each Contributor hereby
74
+ grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
75
+ irrevocable (except as stated in this section) patent license to make, have
76
+ made, use, offer to sell, sell, import, and otherwise transfer the Work, where
77
+ such license applies only to those patent claims licensable by such Contributor
78
+ that are necessarily infringed by their Contribution(s) alone or by combination
79
+ of their Contribution(s) with the Work to which such Contribution(s) was
80
+ submitted. If You institute patent litigation against any entity (including a
81
+ cross-claim or counterclaim in a lawsuit) alleging that the Work or a
82
+ Contribution incorporated within the Work constitutes direct or contributory
83
+ patent infringement, then any patent licenses granted to You under this License
84
+ for that Work shall terminate as of the date such litigation is filed.
85
+
86
+ #### 4. Redistribution
87
+
88
+ You may reproduce and distribute copies of the Work or Derivative Works thereof
89
+ in any medium, with or without modifications, and in Source or Object form,
90
+ provided that You meet the following conditions:
91
+
92
+ * **(a)** You must give any other recipients of the Work or Derivative Works a copy of
93
+ this License; and
94
+ * **(b)** You must cause any modified files to carry prominent notices stating that You
95
+ changed the files; and
96
+ * **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
97
+ all copyright, patent, trademark, and attribution notices from the Source form
98
+ of the Work, excluding those notices that do not pertain to any part of the
99
+ Derivative Works; and
100
+ * **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
101
+ Derivative Works that You distribute must include a readable copy of the
102
+ attribution notices contained within such NOTICE file, excluding those notices
103
+ that do not pertain to any part of the Derivative Works, in at least one of the
104
+ following places: within a NOTICE text file distributed as part of the
105
+ Derivative Works; within the Source form or documentation, if provided along
106
+ with the Derivative Works; or, within a display generated by the Derivative
107
+ Works, if and wherever such third-party notices normally appear. The contents of
108
+ the NOTICE file are for informational purposes only and do not modify the
109
+ License. You may add Your own attribution notices within Derivative Works that
110
+ You distribute, alongside or as an addendum to the NOTICE text from the Work,
111
+ provided that such additional attribution notices cannot be construed as
112
+ modifying the License.
113
+
114
+ You may add Your own copyright statement to Your modifications and may provide
115
+ additional or different license terms and conditions for use, reproduction, or
116
+ distribution of Your modifications, or for any such Derivative Works as a whole,
117
+ provided Your use, reproduction, and distribution of the Work otherwise complies
118
+ with the conditions stated in this License.
119
+
120
+ #### 5. Submission of Contributions
121
+
122
+ Unless You explicitly state otherwise, any Contribution intentionally submitted
123
+ for inclusion in the Work by You to the Licensor shall be under the terms and
124
+ conditions of this License, without any additional terms or conditions.
125
+ Notwithstanding the above, nothing herein shall supersede or modify the terms of
126
+ any separate license agreement you may have executed with Licensor regarding
127
+ such Contributions.
128
+
129
+ #### 6. Trademarks
130
+
131
+ This License does not grant permission to use the trade names, trademarks,
132
+ service marks, or product names of the Licensor, except as required for
133
+ reasonable and customary use in describing the origin of the Work and
134
+ reproducing the content of the NOTICE file.
135
+
136
+ #### 7. Disclaimer of Warranty
137
+
138
+ Unless required by applicable law or agreed to in writing, Licensor provides the
139
+ Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
140
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
141
+ including, without limitation, any warranties or conditions of TITLE,
142
+ NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
143
+ solely responsible for determining the appropriateness of using or
144
+ redistributing the Work and assume any risks associated with Your exercise of
145
+ permissions under this License.
146
+
147
+ #### 8. Limitation of Liability
148
+
149
+ In no event and under no legal theory, whether in tort (including negligence),
150
+ contract, or otherwise, unless required by applicable law (such as deliberate
151
+ and grossly negligent acts) or agreed to in writing, shall any Contributor be
152
+ liable to You for damages, including any direct, indirect, special, incidental,
153
+ or consequential damages of any character arising as a result of this License or
154
+ out of the use or inability to use the Work (including but not limited to
155
+ damages for loss of goodwill, work stoppage, computer failure or malfunction, or
156
+ any and all other commercial damages or losses), even if such Contributor has
157
+ been advised of the possibility of such damages.
158
+
159
+ #### 9. Accepting Warranty or Additional Liability
160
+
161
+ While redistributing the Work or Derivative Works thereof, You may choose to
162
+ offer, and charge a fee for, acceptance of support, warranty, indemnity, or
163
+ other liability obligations and/or rights consistent with this License. However,
164
+ in accepting such obligations, You may act only on Your own behalf and on Your
165
+ sole responsibility, not on behalf of any other Contributor, and only if You
166
+ agree to indemnify, defend, and hold each Contributor harmless for any liability
167
+ incurred by, or claims asserted against, such Contributor by reason of your
168
+ accepting any such warranty or additional liability.
169
+
170
+ _END OF TERMS AND CONDITIONS_
171
+
172
+ ### APPENDIX: How to apply the Apache License to your work
173
+
174
+ To apply the Apache License to your work, attach the following boilerplate
175
+ notice, with the fields enclosed by brackets `[]` replaced with your own
176
+ identifying information. (Don't include the brackets!) The text should be
177
+ enclosed in the appropriate comment syntax for the file format. We also
178
+ recommend that a file or class name and description of purpose be included on
179
+ the same “printed page” as the copyright notice for easier identification within
180
+ third-party archives.
181
+
182
+ Copyright [yyyy] [name of copyright owner]
183
+
184
+ Licensed under the Apache License, Version 2.0 (the "License");
185
+ you may not use this file except in compliance with the License.
186
+ You may obtain a copy of the License at
187
+
188
+ http://www.apache.org/licenses/LICENSE-2.0
189
+
190
+ Unless required by applicable law or agreed to in writing, software
191
+ distributed under the License is distributed on an "AS IS" BASIS,
192
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
193
+ See the License for the specific language governing permissions and
194
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # `<regular-layout>`
2
+
3
+ A library for resizable panel layouts using CSS `grid`.
4
+
5
+ - 7kb, zero dependencies
6
+ - Web Component
7
+ -
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install regular-layout
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Add the `<regular-layout>` custom element to your HTML:
18
+
19
+ ```html
20
+ <regular-layout>
21
+ <div slot="main">Main content</div>
22
+ <div slot="sidebar">Sidebar content</div>
23
+ </regular-layout>
24
+ ```
25
+
26
+ Create and manipulate layouts programmatically:
27
+
28
+ ```javascript
29
+ import "regular-layout/dist/index.js";
30
+
31
+ const layout = document.querySelector('regular-layout');
32
+
33
+ // Add panels
34
+ layout.insertPanel('main');
35
+ layout.insertPanel('sidebar');
36
+
37
+ // Save layout state
38
+ const state = layout.save();
39
+
40
+ // Remove panels (this does not change the DOM, the element is unslotted).
41
+ layout.removePanel('sidebar');
42
+
43
+ // Restore saved state
44
+ layout.restore(state);
45
+ ```
46
+
47
+ Create repositionable panels using `<regular-layout-frame>`:
48
+
49
+ ```html
50
+ <regular-layout>
51
+ <regular-layout-frame slot="main">
52
+ Main content
53
+ </regular-layout-frame>
54
+ </regular-layout>
55
+ ```
@@ -0,0 +1,18 @@
1
+ import type { LayoutPath, LayoutDivider, Layout } from "./layout_config.ts";
2
+ /**
3
+ * Determines which panel or divider is located at a given position in the
4
+ * layout.
5
+ *
6
+ * @param row - Vertical position as a fraction (0-1) of the container
7
+ * height
8
+ * @param column - Horizontal position as a fraction (0-1) of the
9
+ * container width
10
+ * @param layout - The layout tree to search
11
+ * @param check_dividers - Whether `LayoutDivider` intersection should be
12
+ * checked, which oyu may not want for e.g. `drop` actions.
13
+ * @returns The panel path if over a panel, a divider if over a resizable
14
+ * boundary, or null if outside all panels
15
+ */
16
+ export declare function calculate_intersection(column: number, row: number, layout: Layout, check_dividers: false): LayoutPath;
17
+ export declare function calculate_intersection(column: number, row: number, layout: Layout, check_dividers: true): LayoutPath | null | LayoutDivider;
18
+ export declare function calculate_intersection(column: number, row: number, layout: Layout, check_dividers?: boolean): LayoutPath | null | LayoutDivider;
@@ -0,0 +1,2 @@
1
+ import { type Layout, type LayoutPath } from "./layout_config";
2
+ export declare function calculate_split(col: number, row: number, panel: Layout, slot: string, config: LayoutPath): LayoutPath;
@@ -0,0 +1,13 @@
1
+ import type { Layout } from "./layout_config.ts";
2
+ /**
3
+ * Flattens the layout tree by merging parent and child split panels that have
4
+ * the same orientation and type.
5
+ *
6
+ * When a split panel contains a child split panel with the same orientation,
7
+ * they are merged into a single split panel. The sizes array is scaled
8
+ * correctly to maintain proportions.
9
+ *
10
+ * @param layout - The layout tree to flatten
11
+ * @returns A new flattened layout tree (original is not mutated).
12
+ */
13
+ export declare function flatten(layout: Layout): Layout;
@@ -0,0 +1,32 @@
1
+ import { type Layout } from "./layout_config.ts";
2
+ /**
3
+ * Generates CSS Grid styles to render a layout tree.
4
+ * Creates grid-template-rows, grid-template-columns, and positioning rules for
5
+ * all child panels.
6
+ *
7
+ * @param layout - The layout tree to convert to CSS
8
+ * @param round - If true, rounds percentages to whole numbers. Useful for
9
+ * avoiding sub-pixel rendering issues. Defaults to false.
10
+ * @returns CSS string containing :host and ::slotted rules implementing the
11
+ * layout.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const layout = {
16
+ * type: "split-panel",
17
+ * orientation: "horizontal",
18
+ * children: [
19
+ * { type: "child-panel", child: "sidebar" },
20
+ * { type: "child-panel", child: "main" }
21
+ * ],
22
+ * sizes: [0.25, 0.75]
23
+ * };
24
+ *
25
+ * const css = create_css_grid_layout(layout);
26
+ * // Returns CSS like:
27
+ * // :host { display: grid; grid-template-columns: 25% 75%; ... }
28
+ * // :host ::slotted([slot=sidebar]) { grid-column: 1; grid-row: 1; }
29
+ * // :host ::slotted([slot=main]) { grid-column: 2; grid-row: 1; }
30
+ * ```
31
+ */
32
+ export declare function create_css_grid_layout(layout: Layout, round?: boolean, overlay?: [string, string]): string;
@@ -0,0 +1,2 @@
1
+ import type { LayoutPath } from "./layout_config";
2
+ export declare function updateOverlaySheet({ view_window: { row_start, row_end, col_start, col_end }, box, }: LayoutPath<DOMRect>): string;
@@ -0,0 +1,15 @@
1
+ import type { Layout } from "./layout_config.ts";
2
+ /**
3
+ * Inserts a new child panel into the layout tree at a specified location.
4
+ * Creates a split panel if necessary and redistributes sizes equally among all
5
+ * children.
6
+ *
7
+ * @param panel - The root layout tree to insert into
8
+ * @param child - Unique identifier for the new child panel
9
+ * @param path - Array of indices defining where to insert. Empty array inserts
10
+ * at root level.
11
+ * @param orientation - Orientation for newly created split panels. Defaults to
12
+ * "horizontal".
13
+ * @returns A new layout tree with the child inserted (original is not mutated).
14
+ */
15
+ export declare function insert_child(panel: Layout, child: string, path: number[], orientation?: "horizontal" | "vertical"): Layout;
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The percentage of the maximum resize distance that will be clamped.
3
+ *
4
+ */
5
+ export declare const MINIMUM_REDISTRIBUTION_SIZE_THRESHOLD = 0.15;
6
+ /**
7
+ * Threshold from panel edge that is considered a split vs drop action.
8
+ */
9
+ export declare const SPLIT_EDGE_TOLERANCE = 0.15;
10
+ /**
11
+ * Tolerance threshold for considering two grid track positions as identical.
12
+ *
13
+ * When collecting and deduplicating track positions, any positions closer than
14
+ * this value are treated as the same position to avoid redundant grid tracks.
15
+ */
16
+ export declare const GRID_TRACK_COLLAPSE_TOLERANCE = 0.0001;
17
+ /**
18
+ * The representation of a CSS grid, in JSON form.
19
+ */
20
+ export type Layout = SplitLayout | TabLayout;
21
+ /**
22
+ * The orientation (of a `SplitPanel`).
23
+ */
24
+ export type Orientation = "horizontal" | "vertical";
25
+ /**
26
+ * A logical rectange in percent-coordinates (of a (1, 1) square).
27
+ */
28
+ export interface ViewWindow {
29
+ row_start: number;
30
+ row_end: number;
31
+ col_start: number;
32
+ col_end: number;
33
+ }
34
+ /**
35
+ * A split panel that divides space among multiple child layouts
36
+ * .
37
+ * Child panels are arranged either horizontally (side by side) or vertically
38
+ * (stacked), via the `orientation` property `"horizzontal"` and `"vertical"`
39
+ * (respectively).
40
+ */
41
+ export interface SplitLayout {
42
+ type: "split-panel";
43
+ children: Layout[];
44
+ sizes: number[];
45
+ orientation: Orientation;
46
+ }
47
+ /**
48
+ * A leaf panel node that contains a single named child element.
49
+ */
50
+ export interface TabLayout {
51
+ type: "child-panel";
52
+ child: string;
53
+ }
54
+ /**
55
+ * Represents a draggable divider between two panels in the layout.
56
+ *
57
+ * Used for hit detection.
58
+ */
59
+ export interface LayoutDivider {
60
+ path: number[];
61
+ view_window: ViewWindow;
62
+ type: Orientation;
63
+ }
64
+ /**
65
+ * Represents a panel location result from hit detection.
66
+ *
67
+ * Contains both the panel identifier and its grid position in relative units.
68
+ * The generic parameter `T` allows DOM-only properties (e.g. `DOMRect`) to be
69
+ * shared in this cross-platform module.
70
+ */
71
+ export interface LayoutPath<T = undefined> {
72
+ type: "layout-path";
73
+ slot: string;
74
+ path: number[];
75
+ view_window: ViewWindow;
76
+ column_offset: number;
77
+ row_offset: number;
78
+ orientation: Orientation;
79
+ box: T;
80
+ }
81
+ /**
82
+ * Recursively iterates over all child panel names in the layout tree, yielding
83
+ * panel names in depth-first order.
84
+ *
85
+ * @param panel - The layout tree to iterate over
86
+ * @returns Generator yielding child panel names
87
+ */
88
+ export declare function iter_panel_children(panel: Layout): Generator<string>;
89
+ /**
90
+ * An empty `Layout` with no panels.
91
+ */
92
+ export declare const EMPTY_PANEL: Layout;
@@ -0,0 +1,19 @@
1
+ import { type Layout } from "./layout_config.ts";
2
+ /**
3
+ * Adjusts panel sizes during a drag operation on a divider.
4
+ *
5
+ * The `delta` is distributed proportionally among affected panels, maintaining
6
+ * the sum:
7
+ *
8
+ * - Panels before and including the path index shrink by delta.
9
+ * - Panels after the path index grow by delta.
10
+ *
11
+ * @param panel - The root layout tree to modify.
12
+ * @param path - Path to the divider being dragged (identifies which split panel
13
+ * to resize).
14
+ * @param delta - Amount to resize, as a fraction (0-1). Positive values grow
15
+ * panels before the divider, negative values shrink them.
16
+ * @returns A new layout tree with updated sizes (original is not mutated).
17
+ * ```
18
+ */
19
+ export declare function redistribute_panel_sizes(panel: Layout, path: number[], delta: number): Layout;
@@ -0,0 +1,13 @@
1
+ import type { Layout } from "./layout_config.ts";
2
+ /**
3
+ * Removes a child panel from the layout tree by its name.
4
+ *
5
+ * Redistributes the removed panel's space proportionally among remaining
6
+ * siblings. Automatically collapses split panels when only one child remains.
7
+ *
8
+ * @param panel - The root layout tree to remove from.
9
+ * @param child - Name of the child panel to remove.
10
+ * @returns A new layout tree with the child removed (original is not mutated).
11
+ * Returns `EMPTY_PANEL` if the last panel is removed.
12
+ */
13
+ export declare function remove_child(panel: Layout, child: string): Layout;
@@ -0,0 +1,15 @@
1
+ import { RegularLayout } from "./regular-layout.ts";
2
+ import { RegularLayoutFrame } from "./regular-layout-frame.ts";
3
+ declare global {
4
+ interface Document {
5
+ createElement(tagName: "regular-layout", options?: ElementCreationOptions): RegularLayout;
6
+ createElement(tagName: "regular-layout-frame", options?: ElementCreationOptions): RegularLayoutFrame;
7
+ querySelector<E extends Element = Element>(selectors: string): E | null;
8
+ querySelector(selectors: "regular-layout"): RegularLayout | null;
9
+ querySelector(selectors: "regular-layout-frame"): RegularLayoutFrame | null;
10
+ }
11
+ interface CustomElementRegistry {
12
+ get(tagName: "regular-layout"): typeof RegularLayout;
13
+ get(tagName: "regular-layout-frame"): typeof RegularLayoutFrame;
14
+ }
15
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Regular Layout - Resizable Panel Layout System
3
+ *
4
+ * A Web Component library for creating resizable, re-arrangeable layouts using
5
+ * CSS `grid`.
6
+ *
7
+ * ## Basic Usage
8
+ *
9
+ * ```html
10
+ * <regular-layout>
11
+ * <regular-layout-frame slot="sidebar">Sidebar content</regular-layout-frame>
12
+ * <regular-layout-frame slot="main">Main content</regular-layout-frame>
13
+ * </regular-layout>
14
+ * ```
15
+ *
16
+ * ```typescript
17
+ * import { RegularLayout } from 'regular-layout';
18
+ *
19
+ * const layout = document.querySelector('regular-layout');
20
+ *
21
+ * // Insert panels into the grid layout.
22
+ * layout.insertPanel('sidebar', [0]);
23
+ * layout.insertPanel('main', [1]);
24
+ *
25
+ * // Remove a panel (DOM child remains connected, but not slotted).
26
+ * layout.removePanel('sidebar');
27
+ *
28
+ * // Save current layout state.
29
+ * const state = layout.save();
30
+ * localStorage.setItem('layout', JSON.stringify(state));
31
+ *
32
+ * // Restore layout later.
33
+ * const savedState = JSON.parse(localStorage.getItem('layout'));
34
+ * layout.restore(savedState);
35
+ * ```
36
+ *
37
+ * ## Core Components
38
+ *
39
+ * - {@link RegularLayout}: The main `<regular-layout>` custom element.
40
+ * - {@link RegularLayoutFrame}: Optional frame component with titlebar support.
41
+ *
42
+ * ## Type Definitions
43
+ *
44
+ * - {@link Layout}: The layout tree structure for panel configuration.
45
+ * - {@link LayoutPath}: Information about a panel's position and viewport.
46
+ * - {@link LayoutDivider}: Information about a resizable divider.
47
+ *
48
+ * @packageDocumentation
49
+ */
50
+ export type { LayoutPath, Layout, LayoutDivider, } from "./common/layout_config.ts";
51
+ export { RegularLayout } from "./regular-layout.ts";
52
+ export { RegularLayoutFrame } from "./regular-layout-frame.ts";
53
+ import "./extensions.ts";
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ function*C(o){if(o.type==="split-panel")for(let t of o.children)yield*C(t);else yield o.child}var P={type:"split-panel",orientation:"horizontal",sizes:[],children:[]};function f(o,t){if(o.type==="child-panel")return structuredClone(P);let e=structuredClone(o),s=e.children.findIndex(i=>i.type==="child-panel"?i.child===t:!1);if(s!==-1){let i=e.children.filter((h,u)=>u!==s),l=$(e.sizes,s);return i.length===1?i[0]:(e.children=i,e.sizes=l,e)}let r=!1,n=e.children.map(i=>{if(i.type==="split-panel"){let l=f(i,t);return l!==i&&(r=!0),l}return i});return r&&(e.children=n),e}function $(o,t){let e=[],s=o[t],r=0;for(let n=0;n<o.length;n++)n!==t&&(r+=o[n]);for(let n=0;n<o.length;n++)if(n!==t){let i=o[n]/r;e.push(o[n]+s*i)}return e}function T(o){if(o.length===0)return[];let t=o.sort((s,r)=>s-r),e=[t[0]];for(let s=1;s<t.length;s++)Math.abs(t[s]-e[e.length-1])>1e-4&&e.push(t[s]);return e}function E(o,t,e,s){if(o.type==="child-panel")return[e,s];if(o.orientation===t){let r=[e,s],n=e;for(let i=0;i<o.children.length;i++){let l=o.sizes[i],h=E(o.children[i],t,n,n+l*(s-e));r.push(...h),n=n+l*(s-e)}return T(r)}else{let r=[e,s];for(let n of o.children){let i=E(n,t,e,s);r.push(...i)}return T(r)}}function v(o,t){for(let e=0;e<o.length;e++)if(Math.abs(o[e]-t)<1e-4)return e;throw new Error(`Position ${t} not found in ${o}`)}function z(o,t,e,s,r,n,i){if(o.type==="child-panel")return[{child:o.child,colStart:v(t,s),colEnd:v(t,r),rowStart:v(e,n),rowEnd:v(e,i)}];let l=[],{children:h,sizes:u,orientation:c}=o;if(c==="horizontal"){let a=s;for(let p=0;p<h.length;p++){let _=a+u[p]*(r-s);l.push(...z(h[p],t,e,a,_,n,i)),a=_}}else{let a=n;for(let p=0;p<h.length;p++){let _=a+u[p]*(i-n);l.push(...z(h[p],t,e,s,r,a,_)),a=_}}return l}var D=(o,t)=>`:host { display: grid; gap: 0px; grid-template-rows: ${o}; grid-template-columns: ${t}; }`,x=(o,t,e)=>`:host ::slotted([slot=${o}]) { grid-column: ${e}; grid-row: ${t}; }`;function y(o,t=!1,e){if(e&&(o=f(o,e[0])),o.type==="child-panel")return`${D("100%","100%")}
2
+ ${x(o.child,"1","1")}`;let s=E(o,"horizontal",0,1),r=[];for(let a=0;a<s.length-1;a++)r.push(s[a+1]-s[a]);let n=r.map(a=>`${t?Math.round(a*100):a*100}%`).join(" "),i=E(o,"vertical",0,1),l=[];for(let a=0;a<i.length-1;a++)l.push(i[a+1]-i[a]);let h=l.map(a=>`${t?Math.round(a*100):a*100}%`).join(" "),u=z(o,s,i,0,1,0,1),c=[D(h,n)];for(let a of u){let p=a.colEnd-a.colStart===1?`${a.colStart+1}`:`${a.colStart+1} / ${a.colEnd+1}`,_=a.rowEnd-a.rowStart===1?`${a.rowStart+1}`:`${a.rowStart+1} / ${a.rowEnd+1}`;c.push(`${x(a.child,_,p)}`),a.child===e?.[1]&&(c.push(`${x(e[0],_,p)}`),c.push(`:host ::slotted([slot=${e[0]}]) { z-index: 1; }`))}return c.join(`
3
+ `)}function m(o,t,e,s=!0){return R(o,t,e,s)}var N={row_start:0,row_end:1,col_start:0,col_end:1};function R(o,t,e,s,r="horizontal",n=structuredClone(N),i=[]){if(e.type==="child-panel")return{type:"layout-path",box:void 0,slot:e.child,path:i,view_window:n,column_offset:(o-n.col_start)/(n.col_end-n.col_start),row_offset:(t-n.row_start)/(n.row_end-n.row_start),orientation:r};if(e.orientation==="vertical"){let l=n.row_start;for(let h=0;h<e.children.length;h++){let c=(n.row_end-n.row_start)*e.sizes[h]+l;if(s&&Math.abs(t-c)<.01)return{path:[...i,h],type:"vertical",view_window:{...n,row_start:l,row_end:c}};if(t>=l&&t<c)return R(o,t,e.children[h],s,"vertical",{...n,row_start:l,row_end:c},[...i,h]);l=c}}else{let l=n.col_start;for(let h=0;h<e.children.length;h++){let u=n.col_end-n.col_start,c=l+u*e.sizes[h];if(s&&Math.abs(o-c)<.01)return{path:[...i,h],type:"horizontal",view_window:{...n,col_start:l,col_end:c}};if(o>=l&&o<c)return R(o,t,e.children[h],s,"horizontal",{...n,col_start:l,col_end:c},[...i,h]);l=c}}return null}function d(o,t,e,s="horizontal"){if(e.length===0){if(o.type==="child-panel")return{type:"split-panel",orientation:s,children:[o,{type:"child-panel",child:t}],sizes:[.5,.5]};{let u=[...o.children,{type:"child-panel",child:t}],c=u.length,a=Array(c).fill(1/c);return{...o,children:u,sizes:a}}}let[r,...n]=e;if(o.type==="child-panel")return d({type:"split-panel",orientation:s,children:[o],sizes:[1]},t,e,s);if(n.length===0||r===o.children.length){let u=[...o.children];u.splice(r,0,{type:"child-panel",child:t});let c=u.length,a=Array(c).fill(1/c);return{...o,children:u,sizes:a}}let i=o.children[r];if(i.type==="child-panel"&&n.length>0){let u=o.orientation==="horizontal"?"vertical":"horizontal",c=d(i,t,n,u),a=[...o.children];return a[r]=c,{...o,children:a}}let l=d(i,t,n,s),h=[...o.children];return h[r]=l,{...o,children:h}}function S(o,t,e){let s=structuredClone(o),r=s,n={horizontal:e,vertical:e};for(let i=0;i<t.length-1;i++)r.type==="split-panel"&&(n[r.orientation]/=r.sizes[t[i]],r=r.children[t[i]]);if(r.type==="split-panel"){let i=n[r.orientation],l=t[t.length-1];r.sizes=H(r.sizes,l,i)}return s}function H(o,t,e){let s=[...o],r=0;for(let i=0;i<=t;i++)r+=o[i];let n=0;for(let i=t+1;i<o.length;i++)n+=o[i];e=Math.sign(e)*Math.min(Math.abs(e),(1-.15)*(e>0?r:n));for(let i=0;i<=t;i++){let l=o[i]/r;s[i]=o[i]-e*l}for(let i=t+1;i<o.length;i++){let l=o[i]/n;s[i]=o[i]+e*l}return s}function O({view_window:{row_start:o,row_end:t,col_start:e,col_end:s},box:r}){let i=o*r.height+0,l=e*r.width+0/2,h=(t-o)*r.height-0,u=(s-e)*r.width-0;return`::slotted(:not([slot])){${`position:absolute!important;z-index:1;top:${i}px;left:${l}px;height:${h}px;width:${u}px;`}}`}function M(o,t,e,s,r){if(r.column_offset<.15||r.column_offset>1-.15)if(r.orientation==="vertical"){let n=d(e,s,[...r.path,r.column_offset<.15?0:1]);r=m(o,t,n,!1)}else{let n=d(e,s,r.path);r=m(o,t,n,!1)}if(r.row_offset<.15&&r.row_offset<r.column_offset||r.row_offset>1-.15&&r.row_offset>r.column_offset)if(r.orientation==="horizontal"){let n=d(e,s,[...r.path,r.row_offset<.15?0:1]);r=m(o,t,n,!1)}else{let n=d(e,s,r.path);r=m(o,t,n,!1)}return r}function w(o){if(o.type==="child-panel")return o;let t=[],e=[];for(let s=0;s<o.children.length;s++){let r=o.children[s],n=o.sizes[s],i=w(r);if(i.type==="split-panel"&&i.orientation===o.orientation)for(let l=0;l<i.children.length;l++)t.push(i.children[l]),e.push(i.sizes[l]*n);else t.push(i),e.push(n)}return{type:"split-panel",orientation:o.orientation,children:t,sizes:e}}var I="absolute",L=class extends HTMLElement{_shadowRoot;_panel;_stylesheet;_dragPath;_slots;_unslotted_slot;constructor(){super(),this._panel=structuredClone(P),this._stylesheet=new CSSStyleSheet,this._unslotted_slot=document.createElement("slot"),this._shadowRoot=this.attachShadow({mode:"open"}),this._shadowRoot.adoptedStyleSheets=[this._stylesheet],this._shadowRoot.appendChild(this._unslotted_slot),this._slots=new Map,this.onPointerDown=this.onPointerDown.bind(this),this.onPointerMove=this.onPointerMove.bind(this),this.onPointerUp=this.onPointerUp.bind(this)}connectedCallback(){this.addEventListener("pointerdown",this.onPointerDown),this.addEventListener("pointerup",this.onPointerUp),this.addEventListener("pointermove",this.onPointerMove)}disconnectedCallback(){this.removeEventListener("pointerdown",this.onPointerDown),this.removeEventListener("pointerup",this.onPointerUp),this.removeEventListener("pointermove",this.onPointerMove)}setOverlayState(t,e,{slot:s},r=I){let n=this._panel;r==="absolute"&&(n=f(n,s),this._slots.get(s)?.assignedElements()[0]?.removeAttribute("slot"));let[i,l,h]=this.relativeCoordinates(t,e),u=m(i,l,n,!1);if(u&&(u=M(i,l,n,s,u)),u){if(r==="interactive"){let c=f(this._panel,s);c=w(d(c,s,u.path));let a=y(c);this._stylesheet.replaceSync(a)}else if(r==="grid"){let c=[s,u.slot],a=y(this._panel,!1,c);this._stylesheet.replaceSync(a)}else if(r==="absolute"){let c=`${y(n)}
4
+ ${O({...u,box:h})}`;this._stylesheet.replaceSync(c)}}}clearOverlayState(t,e,s,r=I){let n=this._panel;r==="absolute"&&(n=f(n,s.slot),this._unslotted_slot.assignedElements()[0]?.setAttribute("slot",s.slot));let[i,l,h]=this.relativeCoordinates(t,e),u=m(i,l,n,!1);u&&(u=M(i,l,n,s.slot,u));let{path:c}=u||s;this.removePanel(s.slot),this.insertPanel(s.slot,c)}insertPanel(t,e=[]){this.restore(d(this._panel,t,e))}removePanel(t){this.restore(f(this._panel,t))}calculateIntersect(t,e,s=!1){let[r,n,i]=this.relativeCoordinates(t,e),l=m(r,n,this._panel,s);return l?.type==="layout-path"?{...l,box:i}:null}restore(t,e=!1){this._panel=e?t:w(t);let s=y(t);this._stylesheet.replaceSync(s);let r=new Set(this._slots.keys());for(let n of C(t))if(r.delete(n),!this._slots.has(n)){let i=document.createElement("slot");i.setAttribute("name",n),this._shadowRoot.appendChild(i),this._slots.set(n,i)}for(let n of r){let i=this._slots.get(n);i&&(this._shadowRoot.removeChild(i),this._slots.delete(n))}}save(){return structuredClone(this._panel)}relativeCoordinates(t,e){let s=this.getBoundingClientRect(),r=(t-s.left)/(s.right-s.left),n=(e-s.top)/(s.bottom-s.top);return[r,n,s]}onPointerDown(t){let[e,s]=this.relativeCoordinates(t.clientX,t.clientY),r=m(e,s,this._panel);r&&r.type!=="layout-path"&&(this._dragPath=[r,e,s],this.setPointerCapture(t.pointerId),t.preventDefault(),t.stopImmediatePropagation())}onPointerMove(t){if(this._dragPath){let[e,s]=this.relativeCoordinates(t.clientX,t.clientY),r=this._panel,[{path:n,type:i},l,h]=this._dragPath,u=i==="horizontal"?l-e:h-s,c=S(r,n,u);this._stylesheet.replaceSync(y(c))}}onPointerUp(t){if(this._dragPath){this.releasePointerCapture(t.pointerId);let[e,s]=this.relativeCoordinates(t.clientX,t.clientY),r=this._panel,[{path:n},i,l]=this._dragPath;if(this._dragPath[0].type==="horizontal"){let h=S(r,n,i-e);this.restore(h,!0)}else{let h=S(r,n,l-s);this.restore(h,!0)}this._dragPath=void 0}}};var Y=`
5
+ :host{--titlebar--height:24px;box-sizing:border-box}
6
+ :host([slot]){margin-top:calc(var(--titlebar--height) + 3px)!important;}
7
+ :host([slot])::part(container){position:absolute;top:0;left:0;right:0;bottom:0;display:flex;flex-direction:column;background-color:inherit;border-radius:inherit}
8
+ :host([slot])::part(titlebar){height:var(--titlebar--height);margin-top:calc(-2px - var(--titlebar--height));user-select: none;}
9
+ :host([slot])::part(body){flex:1 1 auto;}
10
+ `,g=class extends HTMLElement{_shadowRoot;_container_sheet;_layout;_header;_drag_state=null;constructor(){super(),this._container_sheet=new CSSStyleSheet,this._container_sheet.replaceSync(Y),this._shadowRoot=this.attachShadow({mode:"open"}),this._shadowRoot.adoptedStyleSheets=[this._container_sheet],this._shadowRoot.innerHTML='<slot part="container"><slot part="titlebar">header</slot><slot part="body"><slot></slot></slot></slot>',this._layout=this.parentElement,this._header=this._shadowRoot.children[0].children[0]}connectedCallback(){this._header.addEventListener("pointerdown",this.onPointerDown),this._header.addEventListener("pointermove",this.onPointerMove),this._header.addEventListener("pointerup",this.onPointerUp)}disconnectedCallback(){this._header.removeEventListener("pointerdown",this.onPointerDown),this._header.removeEventListener("pointermove",this.onPointerMove),this._header.removeEventListener("pointerup",this.onPointerUp)}onPointerDown=t=>{this._drag_state=this._layout.calculateIntersect(t.clientX,t.clientY),this._drag_state&&(this._header.setPointerCapture(t.pointerId),t.preventDefault(),t.stopImmediatePropagation())};onPointerMove=t=>{this._drag_state&&this._layout.setOverlayState(t.clientX,t.clientY,this._drag_state)};onPointerUp=t=>{this._drag_state&&(this._layout.clearOverlayState(t.clientX,t.clientY,this._drag_state),this._header.releasePointerCapture(t.pointerId),this._drag_state=null)}};customElements.define("regular-layout-frame",g);customElements.define("regular-layout",L);export{L as RegularLayout,g as RegularLayoutFrame};
11
+ //# sourceMappingURL=index.js.map