panelgrid 0.2.0 → 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/README.md CHANGED
@@ -11,6 +11,7 @@ A flexible and performant React grid layout library with drag-and-drop and resiz
11
11
  - 🔧 **TypeScript**: Full type safety with comprehensive type definitions
12
12
  - 📦 **Tree-shakeable**: ESM and CommonJS builds available
13
13
  - 🎛️ **Customizable Rearrangement**: Override default collision resolution logic
14
+ - ⚛️ **React Server Components**: Full support for Next.js App Router and RSC
14
15
 
15
16
  ## Documentation & Demo
16
17
 
@@ -60,6 +61,8 @@ const initialPanels: PanelCoordinate[] = [
60
61
  { id: 3, x: 0, y: 2, w: 1, h: 1 },
61
62
  ];
62
63
 
64
+ // Mark panel component with "use client" for Next.js App Router
65
+ "use client";
63
66
  function PanelContent({ id }: { id: number | string }) {
64
67
  return <div>Panel {id}</div>;
65
68
  }
@@ -71,7 +74,7 @@ function App() {
71
74
  columnCount={4}
72
75
  gap={8}
73
76
  >
74
- <PanelGridRenderer itemRenderer={PanelContent} />
77
+ <PanelGridRenderer>{PanelContent}</PanelGridRenderer>
75
78
  </PanelGridProvider>
76
79
  );
77
80
  }
@@ -79,6 +82,53 @@ function App() {
79
82
 
80
83
  **Note:** Don't forget to import the CSS file to enable proper styling for the panels.
81
84
 
85
+ ### Next.js App Router / React Server Components
86
+
87
+ PanelGrid is fully compatible with Next.js App Router and React Server Components. The library exports are marked with `"use client"` where necessary.
88
+
89
+ **Important:** Your panel content components must also be marked with `"use client"` if they:
90
+ - Use React hooks (`useState`, `useEffect`, etc.)
91
+ - Access browser APIs
92
+ - Use event handlers
93
+
94
+ ```tsx
95
+ // app/dashboard/page.tsx (Server Component)
96
+ import { PanelGridProvider, PanelGridRenderer } from 'panelgrid';
97
+ import { PanelContent } from './PanelContent'; // Client Component
98
+ import 'panelgrid/styles.css';
99
+
100
+ export default function DashboardPage() {
101
+ const panels = [
102
+ { id: 1, x: 0, y: 0, w: 2, h: 2 },
103
+ { id: 2, x: 2, y: 0, w: 2, h: 2 },
104
+ ];
105
+
106
+ return (
107
+ <PanelGridProvider panels={panels} columnCount={4} gap={8}>
108
+ <PanelGridRenderer>{PanelContent}</PanelGridRenderer>
109
+ </PanelGridProvider>
110
+ );
111
+ }
112
+ ```
113
+
114
+ ```tsx
115
+ // app/dashboard/PanelContent.tsx (Client Component)
116
+ "use client";
117
+ import { usePanelGridControls } from 'panelgrid';
118
+ import type { PanelId } from 'panelgrid';
119
+
120
+ export function PanelContent({ id }: { id: PanelId }) {
121
+ const { removePanel } = usePanelGridControls();
122
+
123
+ return (
124
+ <div>
125
+ <h3>Panel {id}</h3>
126
+ <button onClick={() => removePanel(id)}>Remove</button>
127
+ </div>
128
+ );
129
+ }
130
+ ```
131
+
82
132
  ## Advanced Usage
83
133
 
84
134
  ### Custom Rearrangement Logic
@@ -116,7 +166,7 @@ function App() {
116
166
  gap={8}
117
167
  rearrangement={customRearrange}
118
168
  >
119
- <PanelGridRenderer itemRenderer={PanelContent} />
169
+ <PanelGridRenderer>{PanelContent}</PanelGridRenderer>
120
170
  </PanelGridProvider>
121
171
  );
122
172
  }
@@ -281,7 +331,30 @@ Renderer component that displays the panels.
281
331
 
282
332
  **Props:**
283
333
 
284
- - `itemRenderer`: `React.ComponentType<{ id: PanelId }>` - Component to render each panel
334
+ - `children`: `React.ComponentType<{ id: PanelId }>` - Component type (not instance) to render each panel
335
+
336
+ **Example:**
337
+
338
+ ```tsx
339
+ "use client";
340
+ function MyPanel({ id }: { id: PanelId }) {
341
+ return <div>Panel {id}</div>;
342
+ }
343
+
344
+ // Pass the component type (not JSX)
345
+ <PanelGridRenderer>{MyPanel}</PanelGridRenderer>
346
+ ```
347
+
348
+ For panels with custom props, create a wrapper component:
349
+
350
+ ```tsx
351
+ "use client";
352
+ function CustomPanel({ id }: { id: PanelId }) {
353
+ return <MyPanel id={id} customProp="value" />;
354
+ }
355
+
356
+ <PanelGridRenderer>{CustomPanel}</PanelGridRenderer>
357
+ ```
285
358
 
286
359
  ### `usePanelGridControls()`
287
360
 
@@ -89,38 +89,85 @@ function constrainToGrid(panel, columnCount) {
89
89
  /**
90
90
  * Rearrange panels to resolve collisions when a panel is moved or resized
91
91
  * Panels are moved horizontally first, then vertically if needed
92
+ * For compound resizes (both width and height change), uses two-phase processing
92
93
  * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置
93
94
  * 横方向を優先し、必要に応じて縦方向に移動
95
+ * 幅と高さが同時に変更される場合は、二段階処理を使用
94
96
  */
95
97
  function rearrangePanels(movingPanel, allPanels, columnCount) {
96
98
  const constrainedMovingPanel = constrainToGrid(movingPanel, columnCount);
99
+ const originalPanel = allPanels.find((p) => p.id === movingPanel.id);
100
+ if (originalPanel) {
101
+ const widthChanged = originalPanel.w !== constrainedMovingPanel.w;
102
+ const heightChanged = originalPanel.h !== constrainedMovingPanel.h;
103
+ if (widthChanged && heightChanged) {
104
+ const afterWidthChange = rearrangePanelsInternal({
105
+ ...constrainedMovingPanel,
106
+ h: originalPanel.h
107
+ }, allPanels, columnCount);
108
+ return rearrangePanelsInternal({
109
+ ...constrainedMovingPanel,
110
+ x: afterWidthChange.find((p) => p.id === movingPanel.id)?.x ?? constrainedMovingPanel.x,
111
+ y: afterWidthChange.find((p) => p.id === movingPanel.id)?.y ?? constrainedMovingPanel.y
112
+ }, afterWidthChange, columnCount);
113
+ }
114
+ }
115
+ return rearrangePanelsInternal(constrainedMovingPanel, allPanels, columnCount);
116
+ }
117
+ /**
118
+ * Internal implementation of panel rearrangement
119
+ * パネル再配置の内部実装
120
+ */
121
+ function rearrangePanelsInternal(movingPanel, allPanels, columnCount) {
97
122
  const panelMap = /* @__PURE__ */ new Map();
98
123
  for (const panel of allPanels) panelMap.set(panel.id, { ...panel });
99
- panelMap.set(constrainedMovingPanel.id, { ...constrainedMovingPanel });
100
- const queue = [{ ...constrainedMovingPanel }];
124
+ panelMap.set(movingPanel.id, { ...movingPanel });
125
+ const queue = [{ ...movingPanel }];
101
126
  const processed = /* @__PURE__ */ new Set();
127
+ const processCount = /* @__PURE__ */ new Map();
128
+ const MAX_PROCESS_COUNT = 10;
102
129
  while (queue.length > 0) {
103
130
  const current = queue.shift();
131
+ const count = processCount.get(current.id) || 0;
132
+ if (count >= MAX_PROCESS_COUNT) continue;
133
+ processCount.set(current.id, count + 1);
134
+ const currentInMap = panelMap.get(current.id);
135
+ if (currentInMap && (currentInMap.x !== current.x || currentInMap.y !== current.y)) continue;
104
136
  if (processed.has(current.id)) continue;
105
- processed.add(current.id);
106
137
  const collidingIds = detectCollisions(current, panelMap);
107
138
  if (collidingIds.length === 0) {
108
139
  panelMap.set(current.id, current);
140
+ processed.add(current.id);
109
141
  continue;
110
142
  }
111
- for (const collidingId of collidingIds) {
143
+ const sortedCollidingIds = collidingIds.sort((a, b) => {
144
+ const panelA = panelMap.get(a);
145
+ const panelB = panelMap.get(b);
146
+ if (panelA.y !== panelB.y) return panelA.y - panelB.y;
147
+ return panelA.x - panelB.x;
148
+ });
149
+ const pushedInIteration = /* @__PURE__ */ new Set();
150
+ for (const collidingId of sortedCollidingIds) {
112
151
  const colliding = panelMap.get(collidingId);
113
152
  if (!colliding) continue;
114
153
  const newPos = findNewPosition(colliding, current, columnCount);
115
- const updated = {
154
+ const candidate = {
116
155
  ...colliding,
117
156
  x: newPos.x,
118
157
  y: newPos.y
119
158
  };
120
- panelMap.set(collidingId, updated);
121
- queue.push(updated);
159
+ let wouldCollideWithPushed = false;
160
+ for (const pushedId of pushedInIteration) if (rectanglesOverlap(candidate, panelMap.get(pushedId))) {
161
+ wouldCollideWithPushed = true;
162
+ break;
163
+ }
164
+ if (wouldCollideWithPushed) continue;
165
+ panelMap.set(collidingId, candidate);
166
+ queue.push(candidate);
167
+ pushedInIteration.add(collidingId);
122
168
  }
123
169
  panelMap.set(current.id, current);
170
+ processed.add(current.id);
124
171
  }
125
172
  return Array.from(panelMap.values());
126
173
  }
@@ -1 +1 @@
1
- {"version":3,"file":"rearrangement.cjs","names":["queue: PanelCoordinate[]","candidate: PanelCoordinate"],"sources":["../../src/helpers/rearrangement.ts"],"sourcesContent":["import type { PanelCoordinate, PanelId } from \"../types\";\n\n/**\n * Check if two rectangles overlap using AABB (Axis-Aligned Bounding Box) test\n * 2つの矩形が重なっているかをAABBテストで判定\n */\nexport function rectanglesOverlap(\n a: { x: number; y: number; w: number; h: number },\n b: { x: number; y: number; w: number; h: number }\n): boolean {\n return !(\n (\n a.x + a.w <= b.x || // a is to the left of b\n b.x + b.w <= a.x || // b is to the left of a\n a.y + a.h <= b.y || // a is above b\n b.y + b.h <= a.y\n ) // b is above a\n );\n}\n\n/**\n * Detect all panels that collide with the given panel\n * 指定されたパネルと衝突する全てのパネルを検出\n */\nexport function detectCollisions(panel: PanelCoordinate, panelMap: Map<PanelId, PanelCoordinate>): PanelId[] {\n const collisions = new Set<PanelId>();\n\n for (const [id, other] of panelMap) {\n if (id === panel.id) continue;\n\n if (rectanglesOverlap(panel, other)) {\n collisions.add(id);\n }\n }\n\n return Array.from(collisions);\n}\n\n/**\n * Check if a panel at the given position would collide with any existing panels\n * 指定された位置にパネルを配置した場合に衝突があるかをチェック\n */\nexport function hasCollision(\n candidate: { x: number; y: number; w: number; h: number },\n excludeId: PanelId,\n panelMap: Map<PanelId, PanelCoordinate>\n): boolean {\n for (const [id, panel] of panelMap) {\n if (id === excludeId) continue;\n if (rectanglesOverlap(candidate, panel)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Calculate the minimum distance to push a panel to avoid collision\n * パネルを押しのけるための最小距離を計算\n */\nfunction calculatePushDistance(\n pusher: PanelCoordinate,\n pushed: PanelCoordinate,\n columnCount: number\n): { direction: \"right\" | \"down\"; distance: number } | null {\n // Calculate how far to push horizontally (to the right)\n // 横方向(右)にどれだけ押すか計算\n const pushRight = pusher.x + pusher.w - pushed.x;\n const canPushRight = pushed.x + pushed.w + pushRight <= columnCount;\n\n // Calculate how far to push vertically (down)\n // 縦方向(下)にどれだけ押すか計算\n const pushDown = pusher.y + pusher.h - pushed.y;\n\n // Priority 1: Horizontal push if possible\n // 優先順位1: 可能なら横方向に押す\n if (canPushRight && pushRight > 0) {\n return { direction: \"right\", distance: pushRight };\n }\n\n // Priority 2: Vertical push\n // 優先順位2: 縦方向に押す\n if (pushDown > 0) {\n return { direction: \"down\", distance: pushDown };\n }\n\n return null;\n}\n\n/**\n * Find a new position for a panel by pushing it away from the colliding panel\n * Priority: horizontal (right) first, then vertical (down)\n * 衝突したパネルを押しのける方向に移動させる\n * 優先順位: 横方向(右)→縦方向(下)\n */\nexport function findNewPosition(\n panel: PanelCoordinate,\n pusher: PanelCoordinate,\n columnCount: number\n): { x: number; y: number } {\n const pushInfo = calculatePushDistance(pusher, panel, columnCount);\n\n if (!pushInfo) {\n // No push needed or can't determine direction\n // Fallback: move down\n return { x: panel.x, y: panel.y + 1 };\n }\n\n if (pushInfo.direction === \"right\") {\n // Push horizontally to the right\n // 横方向(右)に押す\n const newX = panel.x + pushInfo.distance;\n if (newX + panel.w <= columnCount) {\n return { x: newX, y: panel.y };\n }\n // Can't fit horizontally, push down instead\n // 横方向に入らない場合は下に押す\n }\n\n // Push vertically down\n // 縦方向(下)に押す\n return { x: panel.x, y: panel.y + pushInfo.distance };\n}\n\n/**\n * Constrain a panel to stay within grid boundaries\n * パネルをグリッド境界内に制約\n */\nfunction constrainToGrid(panel: PanelCoordinate, columnCount: number): PanelCoordinate {\n // Ensure x + w doesn't exceed columnCount\n // x + w が columnCount を超えないようにする\n const maxX = Math.max(0, columnCount - panel.w);\n const constrainedX = Math.max(0, Math.min(panel.x, maxX));\n\n // Ensure y is non-negative\n // y が負にならないようにする\n const constrainedY = Math.max(0, panel.y);\n\n return {\n ...panel,\n x: constrainedX,\n y: constrainedY,\n };\n}\n\n/**\n * Rearrange panels to resolve collisions when a panel is moved or resized\n * Panels are moved horizontally first, then vertically if needed\n * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置\n * 横方向を優先し、必要に応じて縦方向に移動\n */\nexport function rearrangePanels(\n movingPanel: PanelCoordinate,\n allPanels: PanelCoordinate[],\n columnCount: number\n): PanelCoordinate[] {\n // Constrain the moving panel to grid boundaries\n // 移動中のパネルをグリッド境界内に制約\n const constrainedMovingPanel = constrainToGrid(movingPanel, columnCount);\n\n // Create a map for fast panel lookup\n // パネルIDから座標への高速マップを作成\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, { ...panel });\n }\n\n // Update the moving panel's position in the map\n // 移動中のパネルの位置をマップに反映\n panelMap.set(constrainedMovingPanel.id, { ...constrainedMovingPanel });\n\n // Queue for processing panels that need to be repositioned\n // 再配置が必要なパネルの処理キュー\n const queue: PanelCoordinate[] = [{ ...constrainedMovingPanel }];\n const processed = new Set<PanelId>();\n\n // Process panels until no more collisions\n // 衝突がなくなるまでパネルを処理\n while (queue.length > 0) {\n const current = queue.shift()!;\n\n // Skip if already processed\n if (processed.has(current.id)) {\n continue;\n }\n processed.add(current.id);\n\n // Detect collisions with current panel position\n // 現在のパネル位置での衝突を検出\n const collidingIds = detectCollisions(current, panelMap);\n\n if (collidingIds.length === 0) {\n // No collisions, keep current position\n // 衝突なし、現在の位置を維持\n panelMap.set(current.id, current);\n continue;\n }\n\n // Resolve collisions by pushing colliding panels\n // 衝突したパネルを押しのけて衝突を解決\n for (const collidingId of collidingIds) {\n const colliding = panelMap.get(collidingId);\n if (!colliding) continue;\n\n // Find new position by pushing the colliding panel away\n // 衝突したパネルを押しのける方向に移動\n const newPos = findNewPosition(colliding, current, columnCount);\n\n // Update the panel's position\n // パネルの位置を更新\n const updated = { ...colliding, x: newPos.x, y: newPos.y };\n panelMap.set(collidingId, updated);\n\n // Add to queue for further collision checking\n // 再度衝突チェックのためキューに追加\n queue.push(updated);\n }\n\n // Confirm current panel's position\n // 現在のパネルの位置を確定\n panelMap.set(current.id, current);\n }\n\n // Return the rearranged panels\n // 再配置後のパネルを返す\n return Array.from(panelMap.values());\n}\n\n/**\n * Find a new position for a panel to be added\n * 追加するパネルの新しい位置を見つける\n */\nexport function findNewPositionToAddPanel(\n panelToAdd: Partial<PanelCoordinate>,\n allPanels: PanelCoordinate[],\n columnCount: number\n): { x: number; y: number } {\n const id = panelToAdd.id || Math.random().toString(36).substring(2, 15);\n const w = panelToAdd.w || 1;\n const h = panelToAdd.h || 1;\n\n // Create a map for fast panel lookup\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, panel);\n }\n\n // Calculate maximum row based on existing panels\n // Add some buffer rows to ensure we can find a position\n const maxExistingY = allPanels.length > 0 ? Math.max(...allPanels.map((p) => p.y + p.h)) : 0;\n const MAX_ROWS = Math.max(maxExistingY + 100, 1000);\n\n // Try to find a position starting from top-left\n for (let y = 0; y < MAX_ROWS; y++) {\n for (let x = 0; x <= columnCount - w; x++) {\n const candidate: PanelCoordinate = {\n id,\n x,\n y,\n w,\n h,\n };\n\n // Check if this position has any collisions\n if (!hasCollision(candidate, candidate.id, panelMap)) {\n return { x, y };\n }\n }\n }\n\n // Fallback: if no position found, place at the bottom\n return { x: 0, y: maxExistingY };\n}\n"],"mappings":";;;;;;AAMA,SAAgB,kBACd,GACA,GACS;AACT,QAAO,EAEH,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE;;;;;;AASrB,SAAgB,iBAAiB,OAAwB,UAAoD;CAC3G,MAAM,6BAAa,IAAI,KAAc;AAErC,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,MAAM,GAAI;AAErB,MAAI,kBAAkB,OAAO,MAAM,CACjC,YAAW,IAAI,GAAG;;AAItB,QAAO,MAAM,KAAK,WAAW;;;;;;AAO/B,SAAgB,aACd,WACA,WACA,UACS;AACT,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AACl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AAGnD,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,IAC5B,MAAK,IAAI,IAAI,GAAG,KAAK,cAAc,GAAG,KAAK;EACzC,MAAMC,YAA6B;GACjC;GACA;GACA;GACA;GACA;GACD;AAGD,MAAI,CAAC,aAAa,WAAW,UAAU,IAAI,SAAS,CAClD,QAAO;GAAE;GAAG;GAAG;;AAMrB,QAAO;EAAE,GAAG;EAAG,GAAG;EAAc"}
1
+ {"version":3,"file":"rearrangement.cjs","names":["queue: PanelCoordinate[]","candidate: PanelCoordinate"],"sources":["../../src/helpers/rearrangement.ts"],"sourcesContent":["import type { PanelCoordinate, PanelId } from \"../types\";\n\n/**\n * Check if two rectangles overlap using AABB (Axis-Aligned Bounding Box) test\n * 2つの矩形が重なっているかをAABBテストで判定\n */\nexport function rectanglesOverlap(\n a: { x: number; y: number; w: number; h: number },\n b: { x: number; y: number; w: number; h: number }\n): boolean {\n return !(\n (\n a.x + a.w <= b.x || // a is to the left of b\n b.x + b.w <= a.x || // b is to the left of a\n a.y + a.h <= b.y || // a is above b\n b.y + b.h <= a.y\n ) // b is above a\n );\n}\n\n/**\n * Detect all panels that collide with the given panel\n * 指定されたパネルと衝突する全てのパネルを検出\n */\nexport function detectCollisions(panel: PanelCoordinate, panelMap: Map<PanelId, PanelCoordinate>): PanelId[] {\n const collisions = new Set<PanelId>();\n\n for (const [id, other] of panelMap) {\n if (id === panel.id) continue;\n\n if (rectanglesOverlap(panel, other)) {\n collisions.add(id);\n }\n }\n\n return Array.from(collisions);\n}\n\n/**\n * Check if a panel at the given position would collide with any existing panels\n * 指定された位置にパネルを配置した場合に衝突があるかをチェック\n */\nexport function hasCollision(\n candidate: { x: number; y: number; w: number; h: number },\n excludeId: PanelId,\n panelMap: Map<PanelId, PanelCoordinate>\n): boolean {\n for (const [id, panel] of panelMap) {\n if (id === excludeId) continue;\n if (rectanglesOverlap(candidate, panel)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Calculate the minimum distance to push a panel to avoid collision\n * パネルを押しのけるための最小距離を計算\n */\nfunction calculatePushDistance(\n pusher: PanelCoordinate,\n pushed: PanelCoordinate,\n columnCount: number\n): { direction: \"right\" | \"down\"; distance: number } | null {\n // Calculate how far to push horizontally (to the right)\n // 横方向(右)にどれだけ押すか計算\n const pushRight = pusher.x + pusher.w - pushed.x;\n const canPushRight = pushed.x + pushed.w + pushRight <= columnCount;\n\n // Calculate how far to push vertically (down)\n // 縦方向(下)にどれだけ押すか計算\n const pushDown = pusher.y + pusher.h - pushed.y;\n\n // Priority 1: Horizontal push if possible\n // 優先順位1: 可能なら横方向に押す\n if (canPushRight && pushRight > 0) {\n return { direction: \"right\", distance: pushRight };\n }\n\n // Priority 2: Vertical push\n // 優先順位2: 縦方向に押す\n if (pushDown > 0) {\n return { direction: \"down\", distance: pushDown };\n }\n\n return null;\n}\n\n/**\n * Find a new position for a panel by pushing it away from the colliding panel\n * Priority: horizontal (right) first, then vertical (down)\n * 衝突したパネルを押しのける方向に移動させる\n * 優先順位: 横方向(右)→縦方向(下)\n */\nexport function findNewPosition(\n panel: PanelCoordinate,\n pusher: PanelCoordinate,\n columnCount: number\n): { x: number; y: number } {\n const pushInfo = calculatePushDistance(pusher, panel, columnCount);\n\n if (!pushInfo) {\n // No push needed or can't determine direction\n // Fallback: move down\n return { x: panel.x, y: panel.y + 1 };\n }\n\n if (pushInfo.direction === \"right\") {\n // Push horizontally to the right\n // 横方向(右)に押す\n const newX = panel.x + pushInfo.distance;\n if (newX + panel.w <= columnCount) {\n return { x: newX, y: panel.y };\n }\n // Can't fit horizontally, push down instead\n // 横方向に入らない場合は下に押す\n }\n\n // Push vertically down\n // 縦方向(下)に押す\n return { x: panel.x, y: panel.y + pushInfo.distance };\n}\n\n/**\n * Constrain a panel to stay within grid boundaries\n * パネルをグリッド境界内に制約\n */\nfunction constrainToGrid(panel: PanelCoordinate, columnCount: number): PanelCoordinate {\n // Ensure x + w doesn't exceed columnCount\n // x + w が columnCount を超えないようにする\n const maxX = Math.max(0, columnCount - panel.w);\n const constrainedX = Math.max(0, Math.min(panel.x, maxX));\n\n // Ensure y is non-negative\n // y が負にならないようにする\n const constrainedY = Math.max(0, panel.y);\n\n return {\n ...panel,\n x: constrainedX,\n y: constrainedY,\n };\n}\n\n/**\n * Rearrange panels to resolve collisions when a panel is moved or resized\n * Panels are moved horizontally first, then vertically if needed\n * For compound resizes (both width and height change), uses two-phase processing\n * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置\n * 横方向を優先し、必要に応じて縦方向に移動\n * 幅と高さが同時に変更される場合は、二段階処理を使用\n */\nexport function rearrangePanels(\n movingPanel: PanelCoordinate,\n allPanels: PanelCoordinate[],\n columnCount: number\n): PanelCoordinate[] {\n // Constrain the moving panel to grid boundaries\n // 移動中のパネルをグリッド境界内に制約\n const constrainedMovingPanel = constrainToGrid(movingPanel, columnCount);\n\n // Check if this is a compound resize (both width and height changed)\n // 幅と高さの両方が変更されたかチェック\n const originalPanel = allPanels.find((p) => p.id === movingPanel.id);\n if (originalPanel) {\n const widthChanged = originalPanel.w !== constrainedMovingPanel.w;\n const heightChanged = originalPanel.h !== constrainedMovingPanel.h;\n\n if (widthChanged && heightChanged) {\n // Two-phase processing: width first, then height\n // 二段階処理: 幅を先に、次に高さ\n\n // Phase 1: Apply width change only\n // フェーズ1: 幅の変更のみを適用\n const widthOnlyPanel = {\n ...constrainedMovingPanel,\n h: originalPanel.h, // Keep original height\n };\n const afterWidthChange = rearrangePanelsInternal(widthOnlyPanel, allPanels, columnCount);\n\n // Phase 2: Apply height change to the result\n // フェーズ2: 結果に高さの変更を適用\n const heightChangedPanel = {\n ...constrainedMovingPanel,\n // Use the new position if panel-1 was moved during width phase\n x: afterWidthChange.find((p) => p.id === movingPanel.id)?.x ?? constrainedMovingPanel.x,\n y: afterWidthChange.find((p) => p.id === movingPanel.id)?.y ?? constrainedMovingPanel.y,\n };\n return rearrangePanelsInternal(heightChangedPanel, afterWidthChange, columnCount);\n }\n }\n\n // Single dimension change or move - use normal processing\n // 単一次元の変更または移動 - 通常の処理を使用\n return rearrangePanelsInternal(constrainedMovingPanel, allPanels, columnCount);\n}\n\n/**\n * Internal implementation of panel rearrangement\n * パネル再配置の内部実装\n */\nfunction rearrangePanelsInternal(\n movingPanel: PanelCoordinate,\n allPanels: PanelCoordinate[],\n columnCount: number\n): PanelCoordinate[] {\n // Create a map for fast panel lookup\n // パネルIDから座標への高速マップを作成\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, { ...panel });\n }\n\n // Update the moving panel's position in the map\n // 移動中のパネルの位置をマップに反映\n panelMap.set(movingPanel.id, { ...movingPanel });\n\n // Queue for processing panels that need to be repositioned\n // 再配置が必要なパネルの処理キュー\n const queue: PanelCoordinate[] = [{ ...movingPanel }];\n\n // Track processed panels to avoid reprocessing unnecessarily\n // 不要な再処理を避けるため処理済みパネルを追跡\n const processed = new Set<PanelId>();\n\n // Track processing count to prevent infinite loops\n // 無限ループを防ぐため処理回数を追跡\n const processCount = new Map<PanelId, number>();\n const MAX_PROCESS_COUNT = 10;\n\n // Process panels until no more collisions\n // 衝突がなくなるまでパネルを処理\n while (queue.length > 0) {\n const current = queue.shift()!;\n\n // Safety check: prevent infinite loops\n // 安全チェック: 無限ループを防止\n const count = processCount.get(current.id) || 0;\n if (count >= MAX_PROCESS_COUNT) continue;\n processCount.set(current.id, count + 1);\n\n // Skip if the position in queue doesn't match current position in map (outdated entry)\n // キューの位置がマップの現在位置と一致しない場合はスキップ(古いエントリ)\n const currentInMap = panelMap.get(current.id);\n if (currentInMap && (currentInMap.x !== current.x || currentInMap.y !== current.y)) {\n continue;\n }\n\n // Skip if already processed at this position\n // この位置で既に処理済みの場合はスキップ\n if (processed.has(current.id)) {\n continue;\n }\n\n // Detect collisions with current panel position\n // 現在のパネル位置での衝突を検出\n const collidingIds = detectCollisions(current, panelMap);\n\n if (collidingIds.length === 0) {\n panelMap.set(current.id, current);\n processed.add(current.id);\n continue;\n }\n\n // Sort colliding panels by position (top-left to bottom-right) for consistent processing\n // 一貫した処理のため、衝突パネルを位置順(左上から右下)にソート\n const sortedCollidingIds = collidingIds.sort((a, b) => {\n const panelA = panelMap.get(a)!;\n const panelB = panelMap.get(b)!;\n if (panelA.y !== panelB.y) return panelA.y - panelB.y;\n return panelA.x - panelB.x;\n });\n\n // Track panels pushed in this iteration to detect secondary collisions\n // この反復で押されたパネルを追跡し、二次衝突を検出\n const pushedInIteration = new Set<PanelId>();\n\n // Resolve collisions by pushing colliding panels\n // 衝突したパネルを押しのけて衝突を解決\n for (const collidingId of sortedCollidingIds) {\n const colliding = panelMap.get(collidingId);\n if (!colliding) continue;\n\n // Calculate new position for the colliding panel\n // 衝突パネルの新しい位置を計算\n const newPos = findNewPosition(colliding, current, columnCount);\n const candidate = { ...colliding, x: newPos.x, y: newPos.y };\n\n // Check if this would collide with a panel we just pushed in this same iteration\n // 同じ反復内で押したパネルと衝突するかチェック\n let wouldCollideWithPushed = false;\n for (const pushedId of pushedInIteration) {\n const pushedPanel = panelMap.get(pushedId)!;\n if (rectanglesOverlap(candidate, pushedPanel)) {\n wouldCollideWithPushed = true;\n break;\n }\n }\n\n if (wouldCollideWithPushed) {\n // Skip pushing this panel to avoid creating secondary collisions\n // Let the queue handle it in a subsequent iteration\n continue;\n }\n\n // Update panel map and add to queue for further processing\n // パネルマップを更新し、さらなる処理のためキューに追加\n panelMap.set(collidingId, candidate);\n queue.push(candidate);\n pushedInIteration.add(collidingId);\n }\n\n panelMap.set(current.id, current);\n processed.add(current.id);\n }\n\n // Return the rearranged panels\n // 再配置後のパネルを返す\n return Array.from(panelMap.values());\n}\n\n/**\n * Find a new position for a panel to be added\n * 追加するパネルの新しい位置を見つける\n */\nexport function findNewPositionToAddPanel(\n panelToAdd: Partial<PanelCoordinate>,\n allPanels: PanelCoordinate[],\n columnCount: number\n): { x: number; y: number } {\n const id = panelToAdd.id || Math.random().toString(36).substring(2, 15);\n const w = panelToAdd.w || 1;\n const h = panelToAdd.h || 1;\n\n // Create a map for fast panel lookup\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, panel);\n }\n\n // Calculate maximum row based on existing panels\n // Add some buffer rows to ensure we can find a position\n const maxExistingY = allPanels.length > 0 ? Math.max(...allPanels.map((p) => p.y + p.h)) : 0;\n const MAX_ROWS = Math.max(maxExistingY + 100, 1000);\n\n // Try to find a position starting from top-left\n for (let y = 0; y < MAX_ROWS; y++) {\n for (let x = 0; x <= columnCount - w; x++) {\n const candidate: PanelCoordinate = {\n id,\n x,\n y,\n w,\n h,\n };\n\n // Check if this position has any collisions\n if (!hasCollision(candidate, candidate.id, panelMap)) {\n return { x, y };\n }\n }\n }\n\n // Fallback: if no position found, place at the bottom\n return { x: 0, y: maxExistingY };\n}\n"],"mappings":";;;;;;AAMA,SAAgB,kBACd,GACA,GACS;AACT,QAAO,EAEH,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE;;;;;;AASrB,SAAgB,iBAAiB,OAAwB,UAAoD;CAC3G,MAAM,6BAAa,IAAI,KAAc;AAErC,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,MAAM,GAAI;AAErB,MAAI,kBAAkB,OAAO,MAAM,CACjC,YAAW,IAAI,GAAG;;AAItB,QAAO,MAAM,KAAK,WAAW;;;;;;AAO/B,SAAgB,aACd,WACA,WACA,UACS;AACT,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,UAAW;AACtB,MAAI,kBAAkB,WAAW,MAAM,CACrC,QAAO;;AAGX,QAAO;;;;;;AAOT,SAAS,sBACP,QACA,QACA,aAC0D;CAG1D,MAAM,YAAY,OAAO,IAAI,OAAO,IAAI,OAAO;CAC/C,MAAM,eAAe,OAAO,IAAI,OAAO,IAA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A;GACA;GACA;GACA;GACD;AAGD,MAAI,CAAC,aAAa,WAAW,UAAU,IAAI,SAAS,CAClD,QAAO;GAAE;GAAG;GAAG;;AAMrB,QAAO;EAAE,GAAG;EAAG,GAAG;EAAc"}
@@ -5,8 +5,10 @@ import { PanelCoordinate } from "../types.cjs";
5
5
  /**
6
6
  * Rearrange panels to resolve collisions when a panel is moved or resized
7
7
  * Panels are moved horizontally first, then vertically if needed
8
+ * For compound resizes (both width and height change), uses two-phase processing
8
9
  * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置
9
10
  * 横方向を優先し、必要に応じて縦方向に移動
11
+ * 幅と高さが同時に変更される場合は、二段階処理を使用
10
12
  */
11
13
  declare function rearrangePanels(movingPanel: PanelCoordinate, allPanels: PanelCoordinate[], columnCount: number): PanelCoordinate[];
12
14
  //#endregion
@@ -5,8 +5,10 @@ import { PanelCoordinate } from "../types.mjs";
5
5
  /**
6
6
  * Rearrange panels to resolve collisions when a panel is moved or resized
7
7
  * Panels are moved horizontally first, then vertically if needed
8
+ * For compound resizes (both width and height change), uses two-phase processing
8
9
  * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置
9
10
  * 横方向を優先し、必要に応じて縦方向に移動
11
+ * 幅と高さが同時に変更される場合は、二段階処理を使用
10
12
  */
11
13
  declare function rearrangePanels(movingPanel: PanelCoordinate, allPanels: PanelCoordinate[], columnCount: number): PanelCoordinate[];
12
14
  //#endregion
@@ -88,38 +88,85 @@ function constrainToGrid(panel, columnCount) {
88
88
  /**
89
89
  * Rearrange panels to resolve collisions when a panel is moved or resized
90
90
  * Panels are moved horizontally first, then vertically if needed
91
+ * For compound resizes (both width and height change), uses two-phase processing
91
92
  * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置
92
93
  * 横方向を優先し、必要に応じて縦方向に移動
94
+ * 幅と高さが同時に変更される場合は、二段階処理を使用
93
95
  */
94
96
  function rearrangePanels(movingPanel, allPanels, columnCount) {
95
97
  const constrainedMovingPanel = constrainToGrid(movingPanel, columnCount);
98
+ const originalPanel = allPanels.find((p) => p.id === movingPanel.id);
99
+ if (originalPanel) {
100
+ const widthChanged = originalPanel.w !== constrainedMovingPanel.w;
101
+ const heightChanged = originalPanel.h !== constrainedMovingPanel.h;
102
+ if (widthChanged && heightChanged) {
103
+ const afterWidthChange = rearrangePanelsInternal({
104
+ ...constrainedMovingPanel,
105
+ h: originalPanel.h
106
+ }, allPanels, columnCount);
107
+ return rearrangePanelsInternal({
108
+ ...constrainedMovingPanel,
109
+ x: afterWidthChange.find((p) => p.id === movingPanel.id)?.x ?? constrainedMovingPanel.x,
110
+ y: afterWidthChange.find((p) => p.id === movingPanel.id)?.y ?? constrainedMovingPanel.y
111
+ }, afterWidthChange, columnCount);
112
+ }
113
+ }
114
+ return rearrangePanelsInternal(constrainedMovingPanel, allPanels, columnCount);
115
+ }
116
+ /**
117
+ * Internal implementation of panel rearrangement
118
+ * パネル再配置の内部実装
119
+ */
120
+ function rearrangePanelsInternal(movingPanel, allPanels, columnCount) {
96
121
  const panelMap = /* @__PURE__ */ new Map();
97
122
  for (const panel of allPanels) panelMap.set(panel.id, { ...panel });
98
- panelMap.set(constrainedMovingPanel.id, { ...constrainedMovingPanel });
99
- const queue = [{ ...constrainedMovingPanel }];
123
+ panelMap.set(movingPanel.id, { ...movingPanel });
124
+ const queue = [{ ...movingPanel }];
100
125
  const processed = /* @__PURE__ */ new Set();
126
+ const processCount = /* @__PURE__ */ new Map();
127
+ const MAX_PROCESS_COUNT = 10;
101
128
  while (queue.length > 0) {
102
129
  const current = queue.shift();
130
+ const count = processCount.get(current.id) || 0;
131
+ if (count >= MAX_PROCESS_COUNT) continue;
132
+ processCount.set(current.id, count + 1);
133
+ const currentInMap = panelMap.get(current.id);
134
+ if (currentInMap && (currentInMap.x !== current.x || currentInMap.y !== current.y)) continue;
103
135
  if (processed.has(current.id)) continue;
104
- processed.add(current.id);
105
136
  const collidingIds = detectCollisions(current, panelMap);
106
137
  if (collidingIds.length === 0) {
107
138
  panelMap.set(current.id, current);
139
+ processed.add(current.id);
108
140
  continue;
109
141
  }
110
- for (const collidingId of collidingIds) {
142
+ const sortedCollidingIds = collidingIds.sort((a, b) => {
143
+ const panelA = panelMap.get(a);
144
+ const panelB = panelMap.get(b);
145
+ if (panelA.y !== panelB.y) return panelA.y - panelB.y;
146
+ return panelA.x - panelB.x;
147
+ });
148
+ const pushedInIteration = /* @__PURE__ */ new Set();
149
+ for (const collidingId of sortedCollidingIds) {
111
150
  const colliding = panelMap.get(collidingId);
112
151
  if (!colliding) continue;
113
152
  const newPos = findNewPosition(colliding, current, columnCount);
114
- const updated = {
153
+ const candidate = {
115
154
  ...colliding,
116
155
  x: newPos.x,
117
156
  y: newPos.y
118
157
  };
119
- panelMap.set(collidingId, updated);
120
- queue.push(updated);
158
+ let wouldCollideWithPushed = false;
159
+ for (const pushedId of pushedInIteration) if (rectanglesOverlap(candidate, panelMap.get(pushedId))) {
160
+ wouldCollideWithPushed = true;
161
+ break;
162
+ }
163
+ if (wouldCollideWithPushed) continue;
164
+ panelMap.set(collidingId, candidate);
165
+ queue.push(candidate);
166
+ pushedInIteration.add(collidingId);
121
167
  }
122
168
  panelMap.set(current.id, current);
169
+ processed.add(current.id);
123
170
  }
124
171
  return Array.from(panelMap.values());
125
172
  }
@@ -1 +1 @@
1
- {"version":3,"file":"rearrangement.mjs","names":["queue: PanelCoordinate[]","candidate: PanelCoordinate"],"sources":["../../src/helpers/rearrangement.ts"],"sourcesContent":["import type { PanelCoordinate, PanelId } from \"../types\";\n\n/**\n * Check if two rectangles overlap using AABB (Axis-Aligned Bounding Box) test\n * 2つの矩形が重なっているかをAABBテストで判定\n */\nexport function rectanglesOverlap(\n a: { x: number; y: number; w: number; h: number },\n b: { x: number; y: number; w: number; h: number }\n): boolean {\n return !(\n (\n a.x + a.w <= b.x || // a is to the left of b\n b.x + b.w <= a.x || // b is to the left of a\n a.y + a.h <= b.y || // a is above b\n b.y + b.h <= a.y\n ) // b is above a\n );\n}\n\n/**\n * Detect all panels that collide with the given panel\n * 指定されたパネルと衝突する全てのパネルを検出\n */\nexport function detectCollisions(panel: PanelCoordinate, panelMap: Map<PanelId, PanelCoordinate>): PanelId[] {\n const collisions = new Set<PanelId>();\n\n for (const [id, other] of panelMap) {\n if (id === panel.id) continue;\n\n if (rectanglesOverlap(panel, other)) {\n collisions.add(id);\n }\n }\n\n return Array.from(collisions);\n}\n\n/**\n * Check if a panel at the given position would collide with any existing panels\n * 指定された位置にパネルを配置した場合に衝突があるかをチェック\n */\nexport function hasCollision(\n candidate: { x: number; y: number; w: number; h: number },\n excludeId: PanelId,\n panelMap: Map<PanelId, PanelCoordinate>\n): boolean {\n for (const [id, panel] of panelMap) {\n if (id === excludeId) continue;\n if (rectanglesOverlap(candidate, panel)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Calculate the minimum distance to push a panel to avoid collision\n * パネルを押しのけるための最小距離を計算\n */\nfunction calculatePushDistance(\n pusher: PanelCoordinate,\n pushed: PanelCoordinate,\n columnCount: number\n): { direction: \"right\" | \"down\"; distance: number } | null {\n // Calculate how far to push horizontally (to the right)\n // 横方向(右)にどれだけ押すか計算\n const pushRight = pusher.x + pusher.w - pushed.x;\n const canPushRight = pushed.x + pushed.w + pushRight <= columnCount;\n\n // Calculate how far to push vertically (down)\n // 縦方向(下)にどれだけ押すか計算\n const pushDown = pusher.y + pusher.h - pushed.y;\n\n // Priority 1: Horizontal push if possible\n // 優先順位1: 可能なら横方向に押す\n if (canPushRight && pushRight > 0) {\n return { direction: \"right\", distance: pushRight };\n }\n\n // Priority 2: Vertical push\n // 優先順位2: 縦方向に押す\n if (pushDown > 0) {\n return { direction: \"down\", distance: pushDown };\n }\n\n return null;\n}\n\n/**\n * Find a new position for a panel by pushing it away from the colliding panel\n * Priority: horizontal (right) first, then vertical (down)\n * 衝突したパネルを押しのける方向に移動させる\n * 優先順位: 横方向(右)→縦方向(下)\n */\nexport function findNewPosition(\n panel: PanelCoordinate,\n pusher: PanelCoordinate,\n columnCount: number\n): { x: number; y: number } {\n const pushInfo = calculatePushDistance(pusher, panel, columnCount);\n\n if (!pushInfo) {\n // No push needed or can't determine direction\n // Fallback: move down\n return { x: panel.x, y: panel.y + 1 };\n }\n\n if (pushInfo.direction === \"right\") {\n // Push horizontally to the right\n // 横方向(右)に押す\n const newX = panel.x + pushInfo.distance;\n if (newX + panel.w <= columnCount) {\n return { x: newX, y: panel.y };\n }\n // Can't fit horizontally, push down instead\n // 横方向に入らない場合は下に押す\n }\n\n // Push vertically down\n // 縦方向(下)に押す\n return { x: panel.x, y: panel.y + pushInfo.distance };\n}\n\n/**\n * Constrain a panel to stay within grid boundaries\n * パネルをグリッド境界内に制約\n */\nfunction constrainToGrid(panel: PanelCoordinate, columnCount: number): PanelCoordinate {\n // Ensure x + w doesn't exceed columnCount\n // x + w が columnCount を超えないようにする\n const maxX = Math.max(0, columnCount - panel.w);\n const constrainedX = Math.max(0, Math.min(panel.x, maxX));\n\n // Ensure y is non-negative\n // y が負にならないようにする\n const constrainedY = Math.max(0, panel.y);\n\n return {\n ...panel,\n x: constrainedX,\n y: constrainedY,\n };\n}\n\n/**\n * Rearrange panels to resolve collisions when a panel is moved or resized\n * Panels are moved horizontally first, then vertically if needed\n * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置\n * 横方向を優先し、必要に応じて縦方向に移動\n */\nexport function rearrangePanels(\n movingPanel: PanelCoordinate,\n allPanels: PanelCoordinate[],\n columnCount: number\n): PanelCoordinate[] {\n // Constrain the moving panel to grid boundaries\n // 移動中のパネルをグリッド境界内に制約\n const constrainedMovingPanel = constrainToGrid(movingPanel, columnCount);\n\n // Create a map for fast panel lookup\n // パネルIDから座標への高速マップを作成\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, { ...panel });\n }\n\n // Update the moving panel's position in the map\n // 移動中のパネルの位置をマップに反映\n panelMap.set(constrainedMovingPanel.id, { ...constrainedMovingPanel });\n\n // Queue for processing panels that need to be repositioned\n // 再配置が必要なパネルの処理キュー\n const queue: PanelCoordinate[] = [{ ...constrainedMovingPanel }];\n const processed = new Set<PanelId>();\n\n // Process panels until no more collisions\n // 衝突がなくなるまでパネルを処理\n while (queue.length > 0) {\n const current = queue.shift()!;\n\n // Skip if already processed\n if (processed.has(current.id)) {\n continue;\n }\n processed.add(current.id);\n\n // Detect collisions with current panel position\n // 現在のパネル位置での衝突を検出\n const collidingIds = detectCollisions(current, panelMap);\n\n if (collidingIds.length === 0) {\n // No collisions, keep current position\n // 衝突なし、現在の位置を維持\n panelMap.set(current.id, current);\n continue;\n }\n\n // Resolve collisions by pushing colliding panels\n // 衝突したパネルを押しのけて衝突を解決\n for (const collidingId of collidingIds) {\n const colliding = panelMap.get(collidingId);\n if (!colliding) continue;\n\n // Find new position by pushing the colliding panel away\n // 衝突したパネルを押しのける方向に移動\n const newPos = findNewPosition(colliding, current, columnCount);\n\n // Update the panel's position\n // パネルの位置を更新\n const updated = { ...colliding, x: newPos.x, y: newPos.y };\n panelMap.set(collidingId, updated);\n\n // Add to queue for further collision checking\n // 再度衝突チェックのためキューに追加\n queue.push(updated);\n }\n\n // Confirm current panel's position\n // 現在のパネルの位置を確定\n panelMap.set(current.id, current);\n }\n\n // Return the rearranged panels\n // 再配置後のパネルを返す\n return Array.from(panelMap.values());\n}\n\n/**\n * Find a new position for a panel to be added\n * 追加するパネルの新しい位置を見つける\n */\nexport function findNewPositionToAddPanel(\n panelToAdd: Partial<PanelCoordinate>,\n allPanels: PanelCoordinate[],\n columnCount: number\n): { x: number; y: number } {\n const id = panelToAdd.id || Math.random().toString(36).substring(2, 15);\n const w = panelToAdd.w || 1;\n const h = panelToAdd.h || 1;\n\n // Create a map for fast panel lookup\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, panel);\n }\n\n // Calculate maximum row based on existing panels\n // Add some buffer rows to ensure we can find a position\n const maxExistingY = allPanels.length > 0 ? Math.max(...allPanels.map((p) => p.y + p.h)) : 0;\n const MAX_ROWS = Math.max(maxExistingY + 100, 1000);\n\n // Try to find a position starting from top-left\n for (let y = 0; y < MAX_ROWS; y++) {\n for (let x = 0; x <= columnCount - w; x++) {\n const candidate: PanelCoordinate = {\n id,\n x,\n y,\n w,\n h,\n };\n\n // Check if this position has any collisions\n if (!hasCollision(candidate, candidate.id, panelMap)) {\n return { x, y };\n }\n }\n }\n\n // Fallback: if no position found, place at the bottom\n return { x: 0, y: maxExistingY };\n}\n"],"mappings":";;;;;AAMA,SAAgB,kBACd,GACA,GACS;AACT,QAAO,EAEH,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE;;;;;;AASrB,SAAgB,iBAAiB,OAAwB,UAAoD;CAC3G,MAAM,6BAAa,IAAI,KAAc;AAErC,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,MAAM,GAAI;AAErB,MAAI,kBAAkB,OAAO,MAAM,CACjC,YAAW,IAAI,GAAG;;AAItB,QAAO,MAAM,KAAK,WAAW;;;;;;AAO/B,SAAgB,aACd,WACA,WACA,UACS;AACT,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,UAAW;AACtB,MAAI,kBAAkB,WAAW,MAAM,CACrC,QAAO;;AAGX,QAAO;;;;;;AAOT,SAAS,sBACP,QACA,QACA,aAC0D;CAG1D,MAAM,YAAY,OAAO,IAAI,OAAO,IAAI,OAAO;CAC/C,MAAM,eAAe,OAAO,IAAI,OAAO,IAAI,aAAa;CAIxD,MAAM,WAAW,OAAO,IAAI,OAAO,IAAI,OAAO;AAI9C,KAAI,gBAAgB,YAAY,EAC9B,QAAO;EAAE,WAAW;EAAS,UAAU;EAAW;AAKpD,KAAI,WAAW,EACb,QAAO;EAAE,WAAW;EAAQ,UAAU;EAAU;AAGl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QAAQ,IAAI,QAAQ;AACjC;;AAKF,OAAK,MAAM,eAAe,cAAc;GACtC,MAAM,YAAY,SAAS,IAAI,YAAY;AAC3C,OAAI,CAAC,UAAW;GAIhB,MAAM,SAAS,gBAAgB,WAAW,SAAS,YAAY;GAI/D,MAAM,UAAU;IAAE,GAAG;IAAW,GAAG,OAAO;IAAG,GAAG,OAAO;IAAG;AAC1D,YAAS,IAAI,aAAa,QAAQ;AAIlC,SAAM,KAAK,QAAQ;;AAKrB,WAAS,IAAI,QAAQ,IAAI,QAAQ;;AAKnC,QAAO,MAAM,KAAK,SAAS,QAAQ,CAAC;;;;;;AAOtC,SAAgB,0BACd,YACA,WACA,aAC0B;CAC1B,MAAM,KAAK,WAAW,MAAM,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,UAAU,GAAG,GAAG;CACvE,MAAM,IAAI,WAAW,KAAK;CAC1B,MAAM,IAAI,WAAW,KAAK;CAG1B,MAAM,2BAAW,IAAI,KAA+B;AACpD,MAAK,MAAM,SAAS,UAClB,UAAS,IAAI,MAAM,IAAI,MAAM;CAK/B,MAAM,eAAe,UAAU,SAAS,IAAI,KAAK,IAAI,GAAG,UAAU,KAAK,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG;CAC3F,MAAM,WAAW,KAAK,IAAI,eAAe,KAAK,IAAK;AAGnD,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,IAC5B,MAAK,IAAI,IAAI,GAAG,KAAK,cAAc,GAAG,KAAK;EACzC,MAAMC,YAA6B;GACjC;GACA;GACA;GACA;GACA;GACD;AAGD,MAAI,CAAC,aAAa,WAAW,UAAU,IAAI,SAAS,CAClD,QAAO;GAAE;GAAG;GAAG;;AAMrB,QAAO;EAAE,GAAG;EAAG,GAAG;EAAc"}
1
+ {"version":3,"file":"rearrangement.mjs","names":["queue: PanelCoordinate[]","candidate: PanelCoordinate"],"sources":["../../src/helpers/rearrangement.ts"],"sourcesContent":["import type { PanelCoordinate, PanelId } from \"../types\";\n\n/**\n * Check if two rectangles overlap using AABB (Axis-Aligned Bounding Box) test\n * 2つの矩形が重なっているかをAABBテストで判定\n */\nexport function rectanglesOverlap(\n a: { x: number; y: number; w: number; h: number },\n b: { x: number; y: number; w: number; h: number }\n): boolean {\n return !(\n (\n a.x + a.w <= b.x || // a is to the left of b\n b.x + b.w <= a.x || // b is to the left of a\n a.y + a.h <= b.y || // a is above b\n b.y + b.h <= a.y\n ) // b is above a\n );\n}\n\n/**\n * Detect all panels that collide with the given panel\n * 指定されたパネルと衝突する全てのパネルを検出\n */\nexport function detectCollisions(panel: PanelCoordinate, panelMap: Map<PanelId, PanelCoordinate>): PanelId[] {\n const collisions = new Set<PanelId>();\n\n for (const [id, other] of panelMap) {\n if (id === panel.id) continue;\n\n if (rectanglesOverlap(panel, other)) {\n collisions.add(id);\n }\n }\n\n return Array.from(collisions);\n}\n\n/**\n * Check if a panel at the given position would collide with any existing panels\n * 指定された位置にパネルを配置した場合に衝突があるかをチェック\n */\nexport function hasCollision(\n candidate: { x: number; y: number; w: number; h: number },\n excludeId: PanelId,\n panelMap: Map<PanelId, PanelCoordinate>\n): boolean {\n for (const [id, panel] of panelMap) {\n if (id === excludeId) continue;\n if (rectanglesOverlap(candidate, panel)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Calculate the minimum distance to push a panel to avoid collision\n * パネルを押しのけるための最小距離を計算\n */\nfunction calculatePushDistance(\n pusher: PanelCoordinate,\n pushed: PanelCoordinate,\n columnCount: number\n): { direction: \"right\" | \"down\"; distance: number } | null {\n // Calculate how far to push horizontally (to the right)\n // 横方向(右)にどれだけ押すか計算\n const pushRight = pusher.x + pusher.w - pushed.x;\n const canPushRight = pushed.x + pushed.w + pushRight <= columnCount;\n\n // Calculate how far to push vertically (down)\n // 縦方向(下)にどれだけ押すか計算\n const pushDown = pusher.y + pusher.h - pushed.y;\n\n // Priority 1: Horizontal push if possible\n // 優先順位1: 可能なら横方向に押す\n if (canPushRight && pushRight > 0) {\n return { direction: \"right\", distance: pushRight };\n }\n\n // Priority 2: Vertical push\n // 優先順位2: 縦方向に押す\n if (pushDown > 0) {\n return { direction: \"down\", distance: pushDown };\n }\n\n return null;\n}\n\n/**\n * Find a new position for a panel by pushing it away from the colliding panel\n * Priority: horizontal (right) first, then vertical (down)\n * 衝突したパネルを押しのける方向に移動させる\n * 優先順位: 横方向(右)→縦方向(下)\n */\nexport function findNewPosition(\n panel: PanelCoordinate,\n pusher: PanelCoordinate,\n columnCount: number\n): { x: number; y: number } {\n const pushInfo = calculatePushDistance(pusher, panel, columnCount);\n\n if (!pushInfo) {\n // No push needed or can't determine direction\n // Fallback: move down\n return { x: panel.x, y: panel.y + 1 };\n }\n\n if (pushInfo.direction === \"right\") {\n // Push horizontally to the right\n // 横方向(右)に押す\n const newX = panel.x + pushInfo.distance;\n if (newX + panel.w <= columnCount) {\n return { x: newX, y: panel.y };\n }\n // Can't fit horizontally, push down instead\n // 横方向に入らない場合は下に押す\n }\n\n // Push vertically down\n // 縦方向(下)に押す\n return { x: panel.x, y: panel.y + pushInfo.distance };\n}\n\n/**\n * Constrain a panel to stay within grid boundaries\n * パネルをグリッド境界内に制約\n */\nfunction constrainToGrid(panel: PanelCoordinate, columnCount: number): PanelCoordinate {\n // Ensure x + w doesn't exceed columnCount\n // x + w が columnCount を超えないようにする\n const maxX = Math.max(0, columnCount - panel.w);\n const constrainedX = Math.max(0, Math.min(panel.x, maxX));\n\n // Ensure y is non-negative\n // y が負にならないようにする\n const constrainedY = Math.max(0, panel.y);\n\n return {\n ...panel,\n x: constrainedX,\n y: constrainedY,\n };\n}\n\n/**\n * Rearrange panels to resolve collisions when a panel is moved or resized\n * Panels are moved horizontally first, then vertically if needed\n * For compound resizes (both width and height change), uses two-phase processing\n * パネルの移動・リサイズ時に衝突を解決するようにパネルを再配置\n * 横方向を優先し、必要に応じて縦方向に移動\n * 幅と高さが同時に変更される場合は、二段階処理を使用\n */\nexport function rearrangePanels(\n movingPanel: PanelCoordinate,\n allPanels: PanelCoordinate[],\n columnCount: number\n): PanelCoordinate[] {\n // Constrain the moving panel to grid boundaries\n // 移動中のパネルをグリッド境界内に制約\n const constrainedMovingPanel = constrainToGrid(movingPanel, columnCount);\n\n // Check if this is a compound resize (both width and height changed)\n // 幅と高さの両方が変更されたかチェック\n const originalPanel = allPanels.find((p) => p.id === movingPanel.id);\n if (originalPanel) {\n const widthChanged = originalPanel.w !== constrainedMovingPanel.w;\n const heightChanged = originalPanel.h !== constrainedMovingPanel.h;\n\n if (widthChanged && heightChanged) {\n // Two-phase processing: width first, then height\n // 二段階処理: 幅を先に、次に高さ\n\n // Phase 1: Apply width change only\n // フェーズ1: 幅の変更のみを適用\n const widthOnlyPanel = {\n ...constrainedMovingPanel,\n h: originalPanel.h, // Keep original height\n };\n const afterWidthChange = rearrangePanelsInternal(widthOnlyPanel, allPanels, columnCount);\n\n // Phase 2: Apply height change to the result\n // フェーズ2: 結果に高さの変更を適用\n const heightChangedPanel = {\n ...constrainedMovingPanel,\n // Use the new position if panel-1 was moved during width phase\n x: afterWidthChange.find((p) => p.id === movingPanel.id)?.x ?? constrainedMovingPanel.x,\n y: afterWidthChange.find((p) => p.id === movingPanel.id)?.y ?? constrainedMovingPanel.y,\n };\n return rearrangePanelsInternal(heightChangedPanel, afterWidthChange, columnCount);\n }\n }\n\n // Single dimension change or move - use normal processing\n // 単一次元の変更または移動 - 通常の処理を使用\n return rearrangePanelsInternal(constrainedMovingPanel, allPanels, columnCount);\n}\n\n/**\n * Internal implementation of panel rearrangement\n * パネル再配置の内部実装\n */\nfunction rearrangePanelsInternal(\n movingPanel: PanelCoordinate,\n allPanels: PanelCoordinate[],\n columnCount: number\n): PanelCoordinate[] {\n // Create a map for fast panel lookup\n // パネルIDから座標への高速マップを作成\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, { ...panel });\n }\n\n // Update the moving panel's position in the map\n // 移動中のパネルの位置をマップに反映\n panelMap.set(movingPanel.id, { ...movingPanel });\n\n // Queue for processing panels that need to be repositioned\n // 再配置が必要なパネルの処理キュー\n const queue: PanelCoordinate[] = [{ ...movingPanel }];\n\n // Track processed panels to avoid reprocessing unnecessarily\n // 不要な再処理を避けるため処理済みパネルを追跡\n const processed = new Set<PanelId>();\n\n // Track processing count to prevent infinite loops\n // 無限ループを防ぐため処理回数を追跡\n const processCount = new Map<PanelId, number>();\n const MAX_PROCESS_COUNT = 10;\n\n // Process panels until no more collisions\n // 衝突がなくなるまでパネルを処理\n while (queue.length > 0) {\n const current = queue.shift()!;\n\n // Safety check: prevent infinite loops\n // 安全チェック: 無限ループを防止\n const count = processCount.get(current.id) || 0;\n if (count >= MAX_PROCESS_COUNT) continue;\n processCount.set(current.id, count + 1);\n\n // Skip if the position in queue doesn't match current position in map (outdated entry)\n // キューの位置がマップの現在位置と一致しない場合はスキップ(古いエントリ)\n const currentInMap = panelMap.get(current.id);\n if (currentInMap && (currentInMap.x !== current.x || currentInMap.y !== current.y)) {\n continue;\n }\n\n // Skip if already processed at this position\n // この位置で既に処理済みの場合はスキップ\n if (processed.has(current.id)) {\n continue;\n }\n\n // Detect collisions with current panel position\n // 現在のパネル位置での衝突を検出\n const collidingIds = detectCollisions(current, panelMap);\n\n if (collidingIds.length === 0) {\n panelMap.set(current.id, current);\n processed.add(current.id);\n continue;\n }\n\n // Sort colliding panels by position (top-left to bottom-right) for consistent processing\n // 一貫した処理のため、衝突パネルを位置順(左上から右下)にソート\n const sortedCollidingIds = collidingIds.sort((a, b) => {\n const panelA = panelMap.get(a)!;\n const panelB = panelMap.get(b)!;\n if (panelA.y !== panelB.y) return panelA.y - panelB.y;\n return panelA.x - panelB.x;\n });\n\n // Track panels pushed in this iteration to detect secondary collisions\n // この反復で押されたパネルを追跡し、二次衝突を検出\n const pushedInIteration = new Set<PanelId>();\n\n // Resolve collisions by pushing colliding panels\n // 衝突したパネルを押しのけて衝突を解決\n for (const collidingId of sortedCollidingIds) {\n const colliding = panelMap.get(collidingId);\n if (!colliding) continue;\n\n // Calculate new position for the colliding panel\n // 衝突パネルの新しい位置を計算\n const newPos = findNewPosition(colliding, current, columnCount);\n const candidate = { ...colliding, x: newPos.x, y: newPos.y };\n\n // Check if this would collide with a panel we just pushed in this same iteration\n // 同じ反復内で押したパネルと衝突するかチェック\n let wouldCollideWithPushed = false;\n for (const pushedId of pushedInIteration) {\n const pushedPanel = panelMap.get(pushedId)!;\n if (rectanglesOverlap(candidate, pushedPanel)) {\n wouldCollideWithPushed = true;\n break;\n }\n }\n\n if (wouldCollideWithPushed) {\n // Skip pushing this panel to avoid creating secondary collisions\n // Let the queue handle it in a subsequent iteration\n continue;\n }\n\n // Update panel map and add to queue for further processing\n // パネルマップを更新し、さらなる処理のためキューに追加\n panelMap.set(collidingId, candidate);\n queue.push(candidate);\n pushedInIteration.add(collidingId);\n }\n\n panelMap.set(current.id, current);\n processed.add(current.id);\n }\n\n // Return the rearranged panels\n // 再配置後のパネルを返す\n return Array.from(panelMap.values());\n}\n\n/**\n * Find a new position for a panel to be added\n * 追加するパネルの新しい位置を見つける\n */\nexport function findNewPositionToAddPanel(\n panelToAdd: Partial<PanelCoordinate>,\n allPanels: PanelCoordinate[],\n columnCount: number\n): { x: number; y: number } {\n const id = panelToAdd.id || Math.random().toString(36).substring(2, 15);\n const w = panelToAdd.w || 1;\n const h = panelToAdd.h || 1;\n\n // Create a map for fast panel lookup\n const panelMap = new Map<PanelId, PanelCoordinate>();\n for (const panel of allPanels) {\n panelMap.set(panel.id, panel);\n }\n\n // Calculate maximum row based on existing panels\n // Add some buffer rows to ensure we can find a position\n const maxExistingY = allPanels.length > 0 ? Math.max(...allPanels.map((p) => p.y + p.h)) : 0;\n const MAX_ROWS = Math.max(maxExistingY + 100, 1000);\n\n // Try to find a position starting from top-left\n for (let y = 0; y < MAX_ROWS; y++) {\n for (let x = 0; x <= columnCount - w; x++) {\n const candidate: PanelCoordinate = {\n id,\n x,\n y,\n w,\n h,\n };\n\n // Check if this position has any collisions\n if (!hasCollision(candidate, candidate.id, panelMap)) {\n return { x, y };\n }\n }\n }\n\n // Fallback: if no position found, place at the bottom\n return { x: 0, y: maxExistingY };\n}\n"],"mappings":";;;;;AAMA,SAAgB,kBACd,GACA,GACS;AACT,QAAO,EAEH,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE,KACf,EAAE,IAAI,EAAE,KAAK,EAAE;;;;;;AASrB,SAAgB,iBAAiB,OAAwB,UAAoD;CAC3G,MAAM,6BAAa,IAAI,KAAc;AAErC,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,MAAM,GAAI;AAErB,MAAI,kBAAkB,OAAO,MAAM,CACjC,YAAW,IAAI,GAAG;;AAItB,QAAO,MAAM,KAAK,WAAW;;;;;;AAO/B,SAAgB,aACd,WACA,WACA,UACS;AACT,MAAK,MAAM,CAAC,IAAI,UAAU,UAAU;AAClC,MAAI,OAAO,UAAW;AACtB,MAAI,kBAAkB,WAAW,MAAM,CACrC,QAAO;;AAGX,QAAO;;;;;;AAOT,SAAS,sBACP,QACA,QACA,aAC0D;CAG1D,MAAM,YAAY,OAAO,IAAI,OAAO,IAAI,OAAO;CAC/C,MAAM,eAAe,OAAO,IAAI,OAAO,IAAI,aAAa;CAIxD,MAAM,WAAW,OAAO,IAAI,OAAO,IAAI,OAAO;AAI9C,KAAI,gBAAgB,YAAY,EAC9B,QAAO;EAAE,WAAW;EAAS,UAAU;EAAW;AAKpD,KAAI,WAAW,EACb,QAAO;EAAE,WAAW;EAAQ,UAAU;EAAU;AAGlD,QAAO;;;;;;;;AAST,SAAgB,gBACd,OACA,QACA,aAC0B;CAC1B,MAAM,WAAW,sBAAsB,QAAQ,OAAO,YAAY;AAElE,KAAI,CAAC,SAGH,QAAO;EAAE,GAAG,MAAM;EAAG,GAAG,MAAM,IAAI;EAAG;AAGvC,KAAI,SAAS,cAAc,SAAS;EA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S,CACG,EAAE;AAC7C,6BAAyB;AACzB;;AAIJ,OAAI,uBAGF;AAKF,YAAS,IAAI,aAAa,UAAU;AACpC,SAAM,KAAK,UAAU;AACrB,qBAAkB,IAAI,YAAY;;AAGpC,WAAS,IAAI,QAAQ,IAAI,QAAQ;AACjC,YAAU,IAAI,QAAQ,GAAG;;AAK3B,QAAO,MAAM,KAAK,SAAS,QAAQ,CAAC;;;;;;AAOtC,SAAgB,0BACd,YACA,WACA,aAC0B;CAC1B,MAAM,KAAK,WAAW,MAAM,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,UAAU,GAAG,GAAG;CACvE,MAAM,IAAI,WAAW,KAAK;CAC1B,MAAM,IAAI,WAAW,KAAK;CAG1B,MAAM,2BAAW,IAAI,KAA+B;AACpD,MAAK,MAAM,SAAS,UAClB,UAAS,IAAI,MAAM,IAAI,MAAM;CAK/B,MAAM,eAAe,UAAU,SAAS,IAAI,KAAK,IAAI,GAAG,UAAU,KAAK,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG;CAC3F,MAAM,WAAW,KAAK,IAAI,eAAe,KAAK,IAAK;AAGnD,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,IAC5B,MAAK,IAAI,IAAI,GAAG,KAAK,cAAc,GAAG,KAAK;EACzC,MAAMC,YAA6B;GACjC;GACA;GACA;GACA;GACA;GACD;AAGD,MAAI,CAAC,aAAa,WAAW,UAAU,IAAI,SAAS,CAClD,QAAO;GAAE;GAAG;GAAG;;AAMrB,QAAO;EAAE,GAAG;EAAG,GAAG;EAAc"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "panelgrid",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A flexible and performant React grid layout library with drag-and-drop and resize capabilities",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",