@wxn0brp/nya-dock 0.0.3 → 0.0.5

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/README.md CHANGED
@@ -56,7 +56,7 @@ First, you need a container element in your HTML to host the dock layout, and in
56
56
  Import the `controller` and use it to configure and initialize the layout. Note that you also need to import the stylesheet.
57
57
 
58
58
  ```typescript
59
- import { controller } from "@wxn0brp/nya-dock/state";
59
+ import { controller } from "@wxn0brp/nya-dock";
60
60
  import "@wxn0brp/nya-dock/style.css"; // or in html: <link rel="stylesheet" href="node_modules/@wxn0brp/nya-dock/dist/style.css">
61
61
 
62
62
  // 1. Get the master container element
@@ -91,6 +91,7 @@ The layout is defined by a `StructNode`, which is an array of panel IDs and nest
91
91
 
92
92
  - **Horizontal Split (Row):** `["panelA", "panelB"]`
93
93
  - **Vertical Split (Column):** `["panelA", "panelB", 1]` (the `1` indicates a vertical split)
94
+ - **Tab group:** `{ tabs: ["panelA", "panelB"], active: 0 }`
94
95
 
95
96
  You can nest these structures to create complex layouts:
96
97
 
@@ -98,6 +99,57 @@ You can nest these structures to create complex layouts:
98
99
  // panelA is on the left.
99
100
  // The right side is a vertical split between panelB and panelC.
100
101
  const complexLayout = ["panelA", ["panelB", "panelC", 1]];
102
+
103
+ // panelA on the left, panelB and panelC grouped as tabs on the right.
104
+ const tabLayout = ["panelA", { tabs: ["panelB", "panelC"], active: 0 }];
105
+ ```
106
+
107
+ ### Layout Builders
108
+
109
+ Hand-writing nested arrays is easy to get wrong, so the package exports three small builders: `row`, `column` and `tabs`. They return the same internal tree (`NyaSplit`/`NyaStack`) that `setDefaultState` accepts, and they compose with each other.
110
+
111
+ ```typescript
112
+ import { controller, row, column, tabs } from "@wxn0brp/nya-dock";
113
+
114
+ controller.setDefaultState(
115
+ row(
116
+ "panelA",
117
+ column(
118
+ "panelB",
119
+ row(tabs(["panelC", "panelD"]), "panelE"),
120
+ ),
121
+ ),
122
+ );
123
+ ```
124
+
125
+ - `row(...children)` - horizontal split, at least two children.
126
+ - `column(...children)` - vertical split, at least two children.
127
+ - `tabs(ids, active?)` or `tabs(...ids)` - a tab group, `active` defaults to `0`.
128
+
129
+ ### Tab Groups
130
+
131
+ Panels can share one slot as tabs. A group is created by docking a panel onto the center of another panel, or by declaring `{ tabs, active }` in the layout structure. The `active` field holds the index of the visible tab and defaults to `0`.
132
+
133
+ Switch the active tab programmatically with the panel id:
134
+
135
+ ```typescript
136
+ controller.selectTab("panelB");
137
+ ```
138
+
139
+ Dragging a panel onto the center of a tab group adds it as a new tab. Dragging a tab onto another tab reorders it within the group, onto the center of another panel merges it into that group, and onto an edge of a panel docks it as a separate panel again.
140
+
141
+ ### Resizing and Accessibility
142
+
143
+ Each split renders a real separator between its two children. The separator is focusable and exposes `role="separator"` with `aria-orientation` and `aria-valuenow`, `aria-valuemin`, `aria-valuemax`.
144
+
145
+ - Drag the separator to resize.
146
+ - Double-click it, or focus it and press `Enter`/`Home`, to reset to 50%.
147
+ - With the separator focused, use `ArrowLeft`/`ArrowRight` (row) or `ArrowUp`/`ArrowDown` (column) to resize. Hold `Shift` for a larger step.
148
+
149
+ The minimum panel size defaults to `100` and can be changed per controller:
150
+
151
+ ```typescript
152
+ controller.settings.minSize = 120;
101
153
  ```
102
154
 
103
155
  ### CSS Customization
@@ -110,6 +162,9 @@ const complexLayout = ["panelA", ["panelB", "panelC", 1]];
110
162
  --nya-dock-header-border: #444;
111
163
  --nya-dock-header-color: #ddd;
112
164
 
165
+ --nya-dock-tab-active: #3a3a3a;
166
+ --nya-dock-focus: blueviolet;
167
+
113
168
  --nya-dock-content-color: #aaa;
114
169
  --nya-dock-content-border: #444;
115
170
 
@@ -131,4 +186,4 @@ MIT [LICENSE](./LICENSE)
131
186
 
132
187
  ## Contributing
133
188
 
134
- Contributions are welcome!
189
+ Contributions are welcome!
@@ -0,0 +1,16 @@
1
+ import { NyaSplit, NyaStack } from "./types.js";
2
+ export interface HistoryState {
3
+ split: NyaSplit | NyaStack;
4
+ sizes: Record<string, string>;
5
+ }
6
+ export declare class HistoryManager {
7
+ _stack: HistoryState[];
8
+ _index: number;
9
+ _maxSize: number;
10
+ constructor(maxSize?: number);
11
+ push(state: HistoryState): void;
12
+ undo(): HistoryState | null;
13
+ redo(): HistoryState | null;
14
+ canUndo(): boolean;
15
+ canRedo(): boolean;
16
+ }
@@ -0,0 +1,33 @@
1
+ export class HistoryManager {
2
+ _stack = [];
3
+ _index = -1;
4
+ _maxSize;
5
+ constructor(maxSize = 50) {
6
+ this._maxSize = maxSize;
7
+ }
8
+ push(state) {
9
+ this._stack.splice(this._index + 1);
10
+ this._stack.push(state);
11
+ if (this._stack.length > this._maxSize)
12
+ this._stack.shift();
13
+ this._index = this._stack.length - 1;
14
+ }
15
+ undo() {
16
+ if (!this.canUndo())
17
+ return null;
18
+ this._index--;
19
+ return this._stack[this._index];
20
+ }
21
+ redo() {
22
+ if (!this.canRedo())
23
+ return null;
24
+ this._index++;
25
+ return this._stack[this._index];
26
+ }
27
+ canUndo() {
28
+ return this._index > 0;
29
+ }
30
+ canRedo() {
31
+ return this._index < this._stack.length - 1;
32
+ }
33
+ }
package/dist/index.d.ts CHANGED
@@ -2,3 +2,8 @@ import logger from "./logger.js";
2
2
  import "./mouse/index.js";
3
3
  import { controller } from "./state.js";
4
4
  export { controller, logger };
5
+ export { row, column, tabs } from "./utils/layout.js";
6
+ export declare const undo: () => boolean;
7
+ export declare const redo: () => boolean;
8
+ export declare const canUndo: () => boolean;
9
+ export declare const canRedo: () => boolean;
package/dist/index.js CHANGED
@@ -2,3 +2,8 @@ import logger from "./logger.js";
2
2
  import "./mouse/index.js";
3
3
  import { controller } from "./state.js";
4
4
  export { controller, logger };
5
+ export { row, column, tabs } from "./utils/layout.js";
6
+ export const undo = () => controller.undo();
7
+ export const redo = () => controller.redo();
8
+ export const canUndo = () => controller.canUndo();
9
+ export const canRedo = () => controller.canRedo();
package/dist/logger.js CHANGED
@@ -15,7 +15,7 @@ function exportLogs() {
15
15
  function log(level, ...args) {
16
16
  if (LOG_LEVELS[level] >= currentLogLevel) {
17
17
  console.log(`[${level}]`, ...args);
18
- logs.push(`[${level}] ${args.map(a => typeof a === "string" ? a : JSON.stringify(a)).join(" ")}`);
18
+ logs.push(`[${level}] ${args.map(a => (typeof a === "string" ? a : JSON.stringify(a))).join(" ")}`);
19
19
  }
20
20
  }
21
21
  const logger = {
@@ -4,17 +4,30 @@ import { controller } from "../state.js";
4
4
  import { saveNyaState } from "../storage.js";
5
5
  import { detectDockZone, getRelativePosition } from "../utils/detect.js";
6
6
  function removePreviewClass(zone) {
7
- document.querySelectorAll(`.dock-${zone}`).forEach(dock => dock.classList.remove("dock-" + zone));
7
+ document
8
+ .querySelectorAll(`.dock-${zone}`)
9
+ .forEach(dock => dock.classList.remove("dock-" + zone));
8
10
  }
9
11
  function removePreviewClasses() {
10
12
  removePreviewClass("left");
11
13
  removePreviewClass("right");
12
14
  removePreviewClass("top");
13
15
  removePreviewClass("bottom");
16
+ removePreviewClass("center");
14
17
  }
15
18
  let draggingPanel = null;
16
- document.addEventListener("mousedown", (e) => {
19
+ document.addEventListener("mousedown", e => {
17
20
  const target = e.target;
21
+ const tab = target.closest(".nya-tab");
22
+ if (tab) {
23
+ const panel = controller._panels.get(tab.dataset.nya_tab);
24
+ if (!panel)
25
+ return;
26
+ draggingPanel = panel;
27
+ logger.info("Dragging tab started", panel);
28
+ document.body.style.cursor = "move";
29
+ return;
30
+ }
18
31
  if (!target.classList.contains("panel")) {
19
32
  logger.debug("Mousedown event ignored: target is not a panel");
20
33
  return;
@@ -34,7 +47,9 @@ function locatePanel(e, log = false) {
34
47
  logger.debug("Mouse event ignored: no panel is being dragged");
35
48
  return;
36
49
  }
37
- const allPanels = [...controller._panels.values()].filter(p => p !== draggingPanel);
50
+ const allPanels = [
51
+ ...controller._panels.values(),
52
+ ].filter(p => p !== draggingPanel);
38
53
  if (log)
39
54
  logger.debug("All panels", allPanels);
40
55
  const elemUnder = document.elementFromPoint(e.clientX, e.clientY);
@@ -43,7 +58,17 @@ function locatePanel(e, log = false) {
43
58
  logger.debug("Mouse event ignored: no element under cursor");
44
59
  return null;
45
60
  }
46
- const targetPanel = elemUnder.closest(".panel");
61
+ const tab = elemUnder.closest(".nya-tab");
62
+ let targetPanel;
63
+ let zone = "center";
64
+ if (tab) {
65
+ targetPanel = controller._panels.get(tab.dataset.nya_tab) ?? null;
66
+ }
67
+ else {
68
+ targetPanel = elemUnder.closest(".panel");
69
+ if (targetPanel)
70
+ zone = detectDockZone(e, targetPanel);
71
+ }
47
72
  if (!targetPanel) {
48
73
  if (log)
49
74
  logger.debug("Mouse event ignored: no target panel under cursor");
@@ -56,14 +81,8 @@ function locatePanel(e, log = false) {
56
81
  logger.debug("Mouse event ignored: target panel is not a valid drop target");
57
82
  return null;
58
83
  }
59
- const zone = detectDockZone(e, targetPanel);
60
84
  if (log)
61
85
  logger.info(`Docking to ${zone}`);
62
- if (zone === "center") {
63
- if (log)
64
- logger.debug("Docking to center, no action taken");
65
- return null;
66
- }
67
86
  let sourceId = draggingPanel.dataset.nya_id;
68
87
  const targetId = targetPanel.dataset.nya_id;
69
88
  if (!sourceId)
@@ -82,9 +101,15 @@ function locatePanel(e, log = false) {
82
101
  logger.debug("Mouse event ignored: source and target are the same");
83
102
  return null;
84
103
  }
85
- return { sourceId, targetId, zone, targetPanel, draggingPanel };
104
+ return {
105
+ sourceId,
106
+ targetId,
107
+ zone,
108
+ targetPanel,
109
+ draggingPanel,
110
+ };
86
111
  }
87
- document.addEventListener("mouseup", (e) => {
112
+ document.addEventListener("mouseup", e => {
88
113
  function end() {
89
114
  logger.info("Dragging finished");
90
115
  draggingPanel = null;
@@ -97,13 +122,11 @@ document.addEventListener("mouseup", (e) => {
97
122
  }
98
123
  const { sourceId, targetId, zone } = result;
99
124
  controller.movePanel(sourceId, targetId, zone);
100
- controller._render();
101
- controller._setDefaultSize();
102
125
  saveNyaState();
103
126
  removePreviewClasses();
104
127
  end();
105
128
  });
106
- document.addEventListener("mousemove", (e) => {
129
+ document.addEventListener("mousemove", e => {
107
130
  const result = locatePanel(e);
108
131
  if (!result)
109
132
  return;
@@ -1,53 +1,102 @@
1
1
  import { clamp } from "@wxn0brp/flanker-ui/utils";
2
- import { DRAG, RESIZE_MIN } from "../const.js";
3
2
  import logger from "../logger.js";
4
3
  import { controller } from "../state.js";
5
4
  import { getRelativePosition } from "../utils/detect.js";
6
5
  import { saveNyaState } from "../storage.js";
7
- let draggingPanel = null;
6
+ let divider = null;
8
7
  let leftPanel = null;
9
- let panelType = "width";
10
- document.addEventListener("mousedown", (e) => {
8
+ let axis = "width";
9
+ let resizeStarted = false;
10
+ let resizePending = false;
11
+ function setSize(el, axisValue, value) {
12
+ const split = el.parentElement;
13
+ const total = axisValue === "width" ? split.offsetWidth : split.offsetHeight;
14
+ const min = controller.settings.minSize;
15
+ if (!total)
16
+ return value;
17
+ const clamped = clamp(min, value, total - min);
18
+ split.style.setProperty("--size", `${clamped}px`);
19
+ const now = Math.round((clamped / total) * 100);
20
+ el.setAttribute("aria-valuenow", String(now));
21
+ el.setAttribute("aria-valuemin", String(Math.round((min / total) * 100)));
22
+ el.setAttribute("aria-valuemax", String(100 - Math.round((min / total) * 100)));
23
+ return clamped;
24
+ }
25
+ document.addEventListener("mousedown", e => {
11
26
  const target = e.target;
12
- if (!target.classList.contains("panel"))
27
+ if (!target.classList.contains("nya-resize"))
13
28
  return;
14
- const _panelType = target.parentElement.classList.contains("column") ? "height" : "width";
15
- const _leftPanel = target.parentElement.children[0];
16
- const data = getRelativePosition(e, _leftPanel);
17
- if (_panelType === "width") {
18
- const delta = _leftPanel.offsetWidth - data.x;
19
- if (delta > DRAG || delta < 0)
20
- return;
21
- }
22
- else {
23
- const delta = _leftPanel.offsetHeight - data.y;
24
- if (delta > DRAG || delta < 0)
25
- return;
26
- }
27
- draggingPanel = target;
28
- leftPanel = _leftPanel;
29
- panelType = _panelType;
30
- logger.debug(`Resizing panel ${panelType}`);
31
- document.body.style.cursor = panelType === "width" ? "col-resize" : "row-resize";
29
+ const split = target.parentElement;
30
+ divider = target;
31
+ leftPanel = split.children[0];
32
+ axis = split.classList.contains("column") ? "height" : "width";
33
+ resizeStarted = false;
34
+ document.body.style.cursor = axis === "width" ? "col-resize" : "row-resize";
35
+ logger.debug(`Resizing panel ${axis}`);
32
36
  });
33
- document.addEventListener("mousemove", (e) => {
34
- if (!draggingPanel)
37
+ document.addEventListener("mousemove", e => {
38
+ if (!divider)
35
39
  return;
40
+ resizeStarted = true;
36
41
  const data = getRelativePosition(e, leftPanel);
37
- let value = 0;
38
- if (panelType === "width")
39
- value = clamp(RESIZE_MIN, data.x, draggingPanel.parentElement.offsetWidth - RESIZE_MIN);
40
- else
41
- value = clamp(RESIZE_MIN, data.y, draggingPanel.parentElement.offsetHeight - RESIZE_MIN);
42
- const id = draggingPanel.parentElement.dataset.nya_split;
43
- const split = controller._splits.get(id);
44
- split.style.setProperty("--size", `${value}px`);
42
+ const value = setSize(divider, axis, axis === "width" ? data.x : data.y);
45
43
  logger.debug(`Resizing to ${value}`);
46
44
  });
47
- document.addEventListener("mouseup", (e) => {
48
- if (draggingPanel)
45
+ document.addEventListener("mouseup", e => {
46
+ if (divider) {
49
47
  logger.debug("Resizing finished");
50
- draggingPanel = null;
48
+ if (resizeStarted)
49
+ controller._pushHistory();
50
+ }
51
+ divider = null;
52
+ resizeStarted = false;
51
53
  document.body.style.cursor = "";
52
54
  saveNyaState();
53
55
  });
56
+ document.addEventListener("dblclick", e => {
57
+ const target = e.target;
58
+ if (!target.classList.contains("nya-resize"))
59
+ return;
60
+ const total = target.parentElement.classList.contains("column")
61
+ ? target.parentElement.offsetHeight
62
+ : target.parentElement.offsetWidth;
63
+ setSize(target, getAxis(target), total / 2);
64
+ controller._pushHistory();
65
+ saveNyaState();
66
+ });
67
+ document.addEventListener("keydown", e => {
68
+ const target = e.target;
69
+ if (!target.classList.contains("nya-resize"))
70
+ return;
71
+ const axisValue = getAxis(target);
72
+ const split = target.parentElement;
73
+ const total = axisValue === "width" ? split.offsetWidth : split.offsetHeight;
74
+ const first = split.children[0];
75
+ const current = axisValue === "width" ? first.offsetWidth : first.offsetHeight;
76
+ const step = Math.round(total * (e.shiftKey ? 0.1 : 0.02));
77
+ const decrease = axisValue === "width" ? "ArrowLeft" : "ArrowUp";
78
+ const increase = axisValue === "width" ? "ArrowRight" : "ArrowDown";
79
+ let next;
80
+ if (e.key === decrease)
81
+ next = current - step;
82
+ else if (e.key === increase)
83
+ next = current + step;
84
+ else if (e.key === "Enter" || e.key === "Home")
85
+ next = total / 2;
86
+ else
87
+ return;
88
+ e.preventDefault();
89
+ const applied = setSize(target, axisValue, next);
90
+ if (applied !== current)
91
+ resizePending = true;
92
+ });
93
+ document.addEventListener("keyup", e => {
94
+ if (!resizePending)
95
+ return;
96
+ resizePending = false;
97
+ controller._pushHistory();
98
+ saveNyaState();
99
+ });
100
+ function getAxis(el) {
101
+ return el.parentElement.classList.contains("column") ? "height" : "width";
102
+ }
package/dist/render.js CHANGED
@@ -1,11 +1,14 @@
1
+ import { isStack } from "./utils/tabs.js";
1
2
  export function render(controller) {
2
3
  const tempElement = document.createElement("div");
3
4
  for (const panel of controller._panels.values()) {
4
5
  tempElement.appendChild(panel);
5
6
  }
6
7
  controller.master.innerHTML = "";
7
- const masterSplit = renderSplit(controller, controller._split);
8
- controller.master.appendChild(masterSplit);
8
+ const root = controller._split;
9
+ controller.master.appendChild(isStack(root)
10
+ ? renderStack(controller, root, "0")
11
+ : renderSplit(controller, root, "0"));
9
12
  }
10
13
  function renderSplit(controller, split, prefix = "0") {
11
14
  const div = document.createElement("div");
@@ -13,19 +16,66 @@ function renderSplit(controller, split, prefix = "0") {
13
16
  div.classList.add(split.type);
14
17
  div.dataset.nya_split = prefix;
15
18
  controller._splits.set(prefix, div);
16
- for (let i = 0; i < split.nodes.length; i++) {
17
- const node = split.nodes[i];
18
- if (typeof node === "string") {
19
- const panel = controller._panels.get(node);
20
- div.appendChild(panel);
21
- }
22
- else {
23
- const splitPanel = document.createElement("div");
24
- splitPanel.classList.add("panel");
25
- const splitContent = renderSplit(controller, node, `${prefix}.${i}`);
26
- splitPanel.appendChild(splitContent);
27
- div.appendChild(splitPanel);
28
- }
19
+ const [first, second] = split.nodes;
20
+ div.appendChild(renderNode(controller, first, `${prefix}.0`));
21
+ div.appendChild(renderSeparator(split));
22
+ div.appendChild(renderNode(controller, second, `${prefix}.1`));
23
+ return div;
24
+ }
25
+ function renderNode(controller, node, prefix) {
26
+ if (typeof node === "string") {
27
+ const panel = controller._panels.get(node);
28
+ if (panel)
29
+ panel.style.display = "";
30
+ return panel;
29
31
  }
32
+ if (isStack(node))
33
+ return renderStack(controller, node, prefix);
34
+ const splitPanel = document.createElement("div");
35
+ splitPanel.classList.add("panel");
36
+ splitPanel.appendChild(renderSplit(controller, node, prefix));
37
+ return splitPanel;
38
+ }
39
+ function renderStack(controller, stack, prefix) {
40
+ const div = document.createElement("div");
41
+ div.classList.add("nya-stack");
42
+ div.dataset.nya_stack = prefix;
43
+ const tabs = document.createElement("div");
44
+ tabs.classList.add("nya-stack-tabs");
45
+ const body = document.createElement("div");
46
+ body.classList.add("nya-stack-body");
47
+ stack.tabs.forEach((id, index) => {
48
+ const panel = controller._panels.get(id);
49
+ if (!panel)
50
+ return;
51
+ const button = document.createElement("button");
52
+ button.type = "button";
53
+ button.classList.add("nya-tab");
54
+ if (index === stack.active)
55
+ button.classList.add("active");
56
+ button.textContent = panelTitle(panel, id);
57
+ button.dataset.nya_tab = id;
58
+ button.addEventListener("click", () => controller.selectTab(id));
59
+ tabs.appendChild(button);
60
+ panel.style.display = index === stack.active ? "" : "none";
61
+ body.appendChild(panel);
62
+ });
63
+ div.appendChild(tabs);
64
+ div.appendChild(body);
30
65
  return div;
31
66
  }
67
+ function panelTitle(panel, fallback) {
68
+ const header = panel.querySelector(".panel-header");
69
+ return header?.textContent?.trim() || fallback;
70
+ }
71
+ function renderSeparator(split) {
72
+ const divider = document.createElement("div");
73
+ divider.classList.add("nya-resize");
74
+ divider.setAttribute("role", "separator");
75
+ divider.setAttribute("aria-orientation", split.type === "row" ? "vertical" : "horizontal");
76
+ divider.setAttribute("aria-valuemin", "0");
77
+ divider.setAttribute("aria-valuemax", "100");
78
+ divider.setAttribute("aria-valuenow", "50");
79
+ divider.tabIndex = 0;
80
+ return divider;
81
+ }
package/dist/state.d.ts CHANGED
@@ -1,19 +1,33 @@
1
- import { Direction, NyaSplit, StructNode } from "./types.js";
1
+ import { Direction, NyaSplit, NyaStack, StructNode, StructStack } from "./types.js";
2
+ import { HistoryManager, HistoryState } from "./history.js";
2
3
  export declare class NyaController {
3
- _split: NyaSplit;
4
+ _split: NyaSplit | NyaStack;
4
5
  _panels: Map<string, HTMLDivElement>;
5
6
  _splits: Map<string, HTMLDivElement>;
6
7
  master: HTMLDivElement;
7
8
  settings: {
8
9
  key: string;
10
+ minSize: number;
9
11
  };
10
- _struct: NyaSplit;
11
- setDefaultState(state: NyaSplit | StructNode): void;
12
+ _struct: NyaSplit | NyaStack;
13
+ history: HistoryManager;
14
+ constructor();
15
+ setDefaultState(state: NyaSplit | NyaStack | StructNode | StructStack): void;
12
16
  init(): void;
13
17
  registerPanel(id: string, panel: HTMLDivElement): void;
14
18
  _render(): void;
15
19
  _setDefaultSize(): void;
16
- movePanel(sourceId: string, targetId: string, zone: Direction): void;
20
+ _syncSeparators(): void;
21
+ _syncStack(panelId: string, active: number): void;
22
+ selectTab(panelId: string): boolean;
23
+ _captureState(): HistoryState;
24
+ _pushHistory(): void;
25
+ _restoreState(state: HistoryState): void;
26
+ undo(): boolean;
27
+ redo(): boolean;
28
+ canUndo(): boolean;
29
+ canRedo(): boolean;
30
+ movePanel(sourceId: string, targetId: string, zone: Direction | "center"): void;
17
31
  reset(): void;
18
32
  }
19
33
  export declare const controller: NyaController;