Skip to content

Commit 5141c53

Browse files
authored
Merge pull request #11 from texodus/refactor-api-vocab
Refactor API vocabulary for consistency
2 parents 676a982 + b5e0f6f commit 5141c53

29 files changed

Lines changed: 605 additions & 584 deletions

benchmarks/performance.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function generateLargeLayout(depth: number, panelsPerLevel: number): Layout {
4747
function generatePanel(): TabLayout {
4848
const name = `Panel${panelCounter++}`;
4949
return {
50-
type: "child-panel",
50+
type: "tab-layout",
5151
tabs: [name],
5252
};
5353
}
@@ -71,7 +71,7 @@ function generateLargeLayout(depth: number, panelsPerLevel: number): Layout {
7171
}
7272

7373
return {
74-
type: "split-panel",
74+
type: "split-layout",
7575
orientation,
7676
children,
7777
sizes,
@@ -82,14 +82,14 @@ function generateLargeLayout(depth: number, panelsPerLevel: number): Layout {
8282
}
8383

8484
function countPanels(layout: Layout): number {
85-
if (layout.type === "child-panel") {
85+
if (layout.type === "tab-layout") {
8686
return 1;
8787
}
8888
return layout.children.reduce((sum, child) => sum + countPanels(child), 0);
8989
}
9090

9191
function getPanelNames(layout: Layout): string[] {
92-
if (layout.type === "child-panel") {
92+
if (layout.type === "tab-layout") {
9393
return layout.tabs;
9494
}
9595
return layout.children.flatMap((child) => getPanelNames(child));

examples/layout.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
2-
"type": "split-panel",
2+
"type": "split-layout",
33
"orientation": "horizontal",
44
"children": [
55
{
6-
"type": "split-panel",
6+
"type": "split-layout",
77
"orientation": "vertical",
88
"children": [
99
{
10-
"type": "split-panel",
10+
"type": "split-layout",
1111
"orientation": "horizontal",
1212
"children": [
1313
{
14-
"type": "child-panel",
14+
"type": "tab-layout",
1515
"tabs": ["AAA"]
1616
},
1717
{
18-
"type": "split-panel",
18+
"type": "split-layout",
1919
"orientation": "vertical",
2020
"children": [
2121
{
22-
"type": "child-panel",
22+
"type": "tab-layout",
2323
"tabs": ["BBB"]
2424
},
2525
{
26-
"type": "child-panel",
26+
"type": "tab-layout",
2727
"tabs": ["CCC"]
2828
}
2929
],
@@ -33,15 +33,15 @@
3333
"sizes": [0.5, 0.5]
3434
},
3535
{
36-
"type": "split-panel",
36+
"type": "split-layout",
3737
"orientation": "horizontal",
3838
"children": [
3939
{
40-
"type": "child-panel",
40+
"type": "tab-layout",
4141
"tabs": ["DDD"]
4242
},
4343
{
44-
"type": "child-panel",
44+
"type": "tab-layout",
4545
"tabs": ["EEE"]
4646
}
4747
],
@@ -51,7 +51,7 @@
5151
"sizes": [0.5, 0.5]
5252
},
5353
{
54-
"type": "child-panel",
54+
"type": "tab-layout",
5555
"tabs": ["FFF", "GGG", "HHH"]
5656
}
5757
],

src/layout/calculate_edge.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@
1111

1212
import { DEFAULT_PHYSICS, type Physics } from "./constants";
1313
import { insert_child } from "./insert_child";
14-
import type { Layout, LayoutPath, Orientation, ViewWindow } from "./types";
14+
import type {
15+
Layout,
16+
LayoutPath,
17+
LayoutPathTraversal,
18+
Orientation,
19+
ViewWindow,
20+
} from "./types";
1521

1622
/**
1723
* Calculates an insertion point (which may involve splitting a single
18-
* `"child-panel"` into a new `"split-panel"`), based on the cursor position.
24+
* `"tab-layout"` into a new `"split-layout"`), based on the cursor position.
1925
* *
2026
* @param col - The cursor column.
2127
* @param row - The cursor row.
2228
* @param panel - The `Layout` to insert into.
2329
* @param slot - The slot identifier where the insert should occur
2430
* @param drop_target - The `LayoutPath` (from `calculateIntersect`) of the
2531
* panel to either insert next to, or split by.
26-
* @returns A new `LayoutPath` reflecting the updated (maybe) `"split-panel"`,
32+
* @returns A new `LayoutPath` reflecting the updated (maybe) `"split-layout"`,
2733
* which is enough to draw the overlay.
2834
*/
2935
export function calculate_edge(
@@ -133,7 +139,7 @@ function insert_root_edge(
133139
panel: Layout,
134140
slot: string,
135141
drop_target: LayoutPath,
136-
path: number[],
142+
path: LayoutPathTraversal,
137143
is_before: boolean,
138144
orientation: Orientation,
139145
): LayoutPath {
@@ -153,7 +159,7 @@ function insert_axis(
153159
is_before: boolean,
154160
axis_orientation: Orientation,
155161
): LayoutPath {
156-
let result_path: number[];
162+
let result_path: LayoutPathTraversal;
157163

158164
if (drop_target.orientation === axis_orientation) {
159165
// Same orientation - insert into existing split
@@ -183,7 +189,10 @@ function insert_axis(
183189
};
184190
}
185191

186-
function calculate_view_window(panel: Layout, path: number[]): ViewWindow {
192+
function calculate_view_window(
193+
panel: Layout,
194+
path: LayoutPathTraversal,
195+
): ViewWindow {
187196
let view_window: ViewWindow = {
188197
row_start: 0,
189198
row_end: 1,
@@ -193,7 +202,7 @@ function calculate_view_window(panel: Layout, path: number[]): ViewWindow {
193202

194203
let current_panel = panel;
195204
for (const step of path) {
196-
if (current_panel.type === "child-panel") {
205+
if (current_panel.type === "tab-layout") {
197206
break;
198207
}
199208

src/layout/calculate_intersect.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
// ┃ * [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). * ┃
1010
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1111

12-
import type { LayoutPath, LayoutDivider, Layout, ViewWindow } from "./types.ts";
12+
import type {
13+
LayoutPath,
14+
LayoutDivider,
15+
Layout,
16+
ViewWindow,
17+
LayoutPathTraversal,
18+
} from "./types.ts";
1319

1420
const VIEW_WINDOW = {
1521
row_start: 0,
@@ -67,14 +73,14 @@ function calculate_intersection_recursive(
6773
check_dividers: { rect: DOMRect; size: number } | null,
6874
parent_orientation: "horizontal" | "vertical" | null = null,
6975
view_window: ViewWindow = structuredClone(VIEW_WINDOW),
70-
path: number[] = [],
76+
path: LayoutPathTraversal = [],
7177
): LayoutPath | null | LayoutDivider {
7278
if (column < 0 || row < 0 || column > 1 || row > 1) {
7379
return null;
7480
}
7581

7682
// Base case: if this is a child panel, return its name
77-
if (panel.type === "child-panel") {
83+
if (panel.type === "tab-layout") {
7884
const selected = panel.selected ?? 0;
7985
const col_width = view_window.col_end - view_window.col_start;
8086
const row_height = view_window.row_end - view_window.row_start;

src/layout/calculate_path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// ┃ * [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). * ┃
1010
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1111

12-
import type { Layout } from "./types.ts";
12+
import type { Layout, LayoutPathTraversal } from "./types.ts";
1313

1414
/**
1515
* Calculates the index path for a panel with the given name.
@@ -28,9 +28,9 @@ export function calculate_path(name: string, layout: Layout): number[] | null {
2828
function calculate_path_recursive(
2929
name: string,
3030
panel: Layout,
31-
path: number[],
31+
path: LayoutPathTraversal,
3232
): number[] | null {
33-
if (panel.type === "child-panel") {
33+
if (panel.type === "tab-layout") {
3434
if (!panel.tabs.includes(name)) {
3535
return null;
3636
}

src/layout/flatten.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type { Layout } from "./types.ts";
2323
* @returns A new flattened layout tree (original is not mutated).
2424
*/
2525
export function flatten(layout: Layout): Layout {
26-
if (layout.type === "child-panel") {
26+
if (layout.type === "tab-layout") {
2727
layout.selected = layout.selected || 0;
2828
return layout;
2929
}
@@ -36,7 +36,7 @@ export function flatten(layout: Layout): Layout {
3636
const flattenedChild = flatten(child);
3737

3838
if (
39-
flattenedChild.type === "split-panel" &&
39+
flattenedChild.type === "split-layout" &&
4040
flattenedChild.orientation === layout.orientation
4141
) {
4242
for (let j = 0; j < flattenedChild.children.length; j++) {
@@ -54,7 +54,7 @@ export function flatten(layout: Layout): Layout {
5454
}
5555

5656
return {
57-
type: "split-panel",
57+
type: "split-layout",
5858
orientation: layout.orientation,
5959
children: flattenedChildren,
6060
sizes: flattenedSizes,

src/layout/generate_grid.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function collect_track_positions(
4444
end: number,
4545
physics: Physics,
4646
): number[] {
47-
if (panel.type === "child-panel") {
47+
if (panel.type === "tab-layout") {
4848
return [start, end];
4949
}
5050

@@ -100,7 +100,7 @@ function build_cells(
100100
rowEnd: number,
101101
physics: Physics,
102102
): GridCell[] {
103-
if (panel.type === "child-panel") {
103+
if (panel.type === "tab-layout") {
104104
const selected = panel.selected ?? 0;
105105
return [
106106
{
@@ -179,11 +179,11 @@ const child_template = (
179179
* @example
180180
* ```typescript
181181
* const layout = {
182-
* type: "split-panel",
182+
* type: "split-layout",
183183
* orientation: "horizontal",
184184
* children: [
185-
* { type: "child-panel", tabs: "sidebar" },
186-
* { type: "child-panel", tabs: "main" }
185+
* { type: "tab-layout", tabs: "sidebar" },
186+
* { type: "tab-layout", tabs: "main" }
187187
* ],
188188
* sizes: [0.25, 0.75]
189189
* };
@@ -200,7 +200,7 @@ export function create_css_grid_layout(
200200
overlay?: [string, string],
201201
physics: Physics = DEFAULT_PHYSICS,
202202
): string {
203-
if (layout.type === "child-panel") {
203+
if (layout.type === "tab-layout") {
204204
const selected = layout.selected ?? 0;
205205
return [
206206
host_template("100%", "100%"),

src/layout/insert_child.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// ┃ * [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). * ┃
1010
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1111

12-
import type { Layout } from "./types.ts";
12+
import type { Layout, LayoutPathTraversal } from "./types.ts";
1313

1414
/**
1515
* Inserts a new child panel into the layout tree at a specified location.
@@ -28,32 +28,32 @@ import type { Layout } from "./types.ts";
2828
export function insert_child(
2929
panel: Layout,
3030
child: string,
31-
path: number[],
31+
path: LayoutPathTraversal,
3232
orientation?: "horizontal" | "vertical",
3333
): Layout {
3434
const createChildPanel = (childId: string): Layout => ({
35-
type: "child-panel",
35+
type: "tab-layout",
3636
tabs: [childId],
3737
});
3838

3939
if (path.length === 0) {
4040
// Insert at root level
41-
if (panel.type === "child-panel") {
42-
// Add to existing child-panel as a tab
41+
if (panel.type === "tab-layout") {
42+
// Add to existing tab-layout as a tab
4343
return {
44-
type: "child-panel",
44+
type: "tab-layout",
4545
tabs: [child, ...panel.tabs],
4646
};
4747
} else if (orientation) {
4848
// When inserting at edge of root, wrap the entire panel in a new split
4949
return {
50-
type: "split-panel",
50+
type: "split-layout",
5151
orientation: orientation,
5252
children: [createChildPanel(child), panel],
5353
sizes: [0.5, 0.5],
5454
};
5555
} else {
56-
// Append to existing split-panel
56+
// Append to existing split-layout
5757
const newChildren = [...panel.children, createChildPanel(child)];
5858
const newSizes = [...panel.sizes, 1 / (newChildren.length - 1)];
5959
return {
@@ -69,8 +69,8 @@ export function insert_child(
6969

7070
// Special case: when orientation is provided and restPath is empty, handle edge insertion
7171
if (orientation && restPath.length === 0) {
72-
// If panel is a split-panel with the same orientation, insert into its children
73-
if (panel.type === "split-panel" && panel.orientation === orientation) {
72+
// If panel is a split-layout with the same orientation, insert into its children
73+
if (panel.type === "split-layout" && panel.orientation === orientation) {
7474
const newChildren = [...panel.children];
7575
newChildren.splice(index, 0, createChildPanel(child));
7676
const newSizes = [...panel.sizes];
@@ -89,14 +89,14 @@ export function insert_child(
8989
: [panel, createChildPanel(child)];
9090

9191
return {
92-
type: "split-panel",
92+
type: "split-layout",
9393
orientation: orientation,
9494
children,
9595
sizes: [0.5, 0.5],
9696
};
9797
}
9898

99-
if (panel.type === "child-panel") {
99+
if (panel.type === "tab-layout") {
100100
// Stack into child array only when ALL of these conditions are met:
101101
// 1. Path has exactly one element (restPath is empty)
102102
// 2. Orientation was NOT explicitly provided (orientation is undefined)
@@ -117,7 +117,7 @@ export function insert_child(
117117

118118
// Otherwise, wrap in a split panel and recurse
119119
const newPanel: Layout = {
120-
type: "split-panel",
120+
type: "split-layout",
121121
orientation: orientation || "horizontal",
122122
children: [panel],
123123
sizes: [1],
@@ -130,7 +130,7 @@ export function insert_child(
130130
if (orientation && panel.children[index]) {
131131
// When inserting at an edge, create a split panel with the new child and existing child
132132
const newSplitPanel: Layout = {
133-
type: "split-panel",
133+
type: "split-layout",
134134
orientation: orientation,
135135
children: [createChildPanel(child), panel.children[index]],
136136
sizes: [0.5, 0.5],
@@ -159,9 +159,9 @@ export function insert_child(
159159

160160
const targetChild = panel.children[index];
161161

162-
// Determine the orientation to pass down when navigating into a child-panel
162+
// Determine the orientation to pass down when navigating into a tab-layout
163163
const childOrientation =
164-
targetChild.type === "child-panel" &&
164+
targetChild.type === "tab-layout" &&
165165
restPath.length > 0 &&
166166
orientation !== undefined
167167
? panel.orientation === "horizontal"

0 commit comments

Comments
 (0)