-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dashboard][Collapsable Panels] New collision resolution algorithm #204134
Merged
Heenawter
merged 19 commits into
elastic:main
from
Heenawter:kbn-grid-layout_new-collision-algorithm_2024-12-12
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8fbdd94
Try new algorithm
Heenawter c176992
Undo grid changes
Heenawter 07ba89b
Fix empty row bug
Heenawter d40a91d
Merge branch 'main' into kbn-grid-layout_new-collision-algorithm_2024…
Heenawter f545723
Make Lens embeddables work again
Heenawter 3b9ee92
Add some comments
Heenawter 8280887
Small cleanup
Heenawter 7bf2bd0
Another small cleanup
Heenawter c811e5c
Add tests
Heenawter 0cc0081
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine 903b774
Fix failing test due to reordering
Heenawter e25ca79
Merge branch 'main' into kbn-grid-layout_new-collision-algorithm_2024…
Heenawter d8ce89a
Switch to recursive algorithm
Heenawter 7c70bc7
Fix state comparison
Heenawter ddf6b68
Merge branch 'main' into kbn-grid-layout_new-collision-algorithm_2024…
Heenawter a5cf12b
Undo test changes
Heenawter 043dac5
Small cleanup
Heenawter 3f3911d
Merge branch 'main' into kbn-grid-layout_new-collision-algorithm_2024…
Heenawter 973356e
Merge branch 'main' into kbn-grid-layout_new-collision-algorithm_2024…
Heenawter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/kbn-grid-layout/grid/utils/resolve_grid_row.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { resolveGridRow } from './resolve_grid_row'; | ||
|
||
describe('resolve grid row', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to have jest coverage here! |
||
test('does nothing if grid row has no collisions', () => { | ||
const gridRow = { | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 3, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 4, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 5, column: 0, height: 1, width: 7 }, | ||
panel4: { id: 'panel4', row: 0, column: 6, height: 3, width: 1 }, | ||
}, | ||
}; | ||
const result = resolveGridRow(gridRow); | ||
expect(result).toEqual(gridRow); | ||
}); | ||
|
||
test('resolves grid row if it has collisions without drag event', () => { | ||
const result = resolveGridRow({ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 3, width: 4 }, | ||
panel2: { id: 'panel2', row: 3, column: 0, height: 2, width: 2 }, | ||
panel3: { id: 'panel3', row: 3, column: 2, height: 2, width: 2 }, | ||
panel4: { id: 'panel4', row: 0, column: 3, height: 5, width: 4 }, | ||
}, | ||
}); | ||
expect(result).toEqual({ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 3, width: 4 }, | ||
panel2: { id: 'panel2', row: 3, column: 0, height: 2, width: 2 }, | ||
panel3: { id: 'panel3', row: 8, column: 2, height: 2, width: 2 }, // pushed down | ||
panel4: { id: 'panel4', row: 3, column: 3, height: 5, width: 4 }, // pushed down | ||
}, | ||
}); | ||
}); | ||
|
||
test('drag causes no collision', () => { | ||
const result = resolveGridRow( | ||
{ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 1, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 2, column: 0, height: 1, width: 7 }, | ||
}, | ||
}, | ||
{ id: 'panel4', row: 0, column: 7, height: 3, width: 1 } | ||
); | ||
|
||
expect(result).toEqual({ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 1, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 2, column: 0, height: 1, width: 7 }, | ||
panel4: { id: 'panel4', row: 0, column: 7, height: 3, width: 1 }, | ||
}, | ||
}); | ||
}); | ||
|
||
test('drag causes collision with one panel that pushes down others', () => { | ||
const result = resolveGridRow( | ||
{ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 1, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 2, column: 0, height: 1, width: 8 }, | ||
panel4: { id: 'panel4', row: 3, column: 4, height: 3, width: 4 }, | ||
}, | ||
}, | ||
{ id: 'panel5', row: 2, column: 0, height: 3, width: 3 } | ||
); | ||
|
||
expect(result).toEqual({ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 1, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 5, column: 0, height: 1, width: 8 }, // pushed down | ||
panel4: { id: 'panel4', row: 6, column: 4, height: 3, width: 4 }, // pushed down | ||
panel5: { id: 'panel5', row: 2, column: 0, height: 3, width: 3 }, | ||
}, | ||
}); | ||
}); | ||
|
||
test('drag causes collision with multiple panels', () => { | ||
const result = resolveGridRow( | ||
{ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 3, width: 4 }, | ||
panel2: { id: 'panel2', row: 3, column: 0, height: 2, width: 2 }, | ||
panel3: { id: 'panel3', row: 3, column: 2, height: 2, width: 2 }, | ||
}, | ||
}, | ||
{ id: 'panel4', row: 0, column: 3, height: 5, width: 4 } | ||
); | ||
expect(result).toEqual({ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 5, column: 0, height: 3, width: 4 }, // pushed down | ||
panel2: { id: 'panel2', row: 8, column: 0, height: 2, width: 2 }, // pushed down | ||
panel3: { id: 'panel3', row: 8, column: 2, height: 2, width: 2 }, // pushed down | ||
panel4: { id: 'panel4', row: 0, column: 3, height: 5, width: 4 }, | ||
}, | ||
}); | ||
}); | ||
|
||
test('drag causes collision with every panel', () => { | ||
const result = resolveGridRow( | ||
{ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 0, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 1, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 2, column: 0, height: 1, width: 7 }, | ||
}, | ||
}, | ||
{ id: 'panel4', row: 0, column: 6, height: 3, width: 1 } | ||
); | ||
|
||
expect(result).toEqual({ | ||
title: 'Test', | ||
isCollapsed: false, | ||
panels: { | ||
panel1: { id: 'panel1', row: 3, column: 0, height: 1, width: 7 }, | ||
panel2: { id: 'panel2', row: 4, column: 0, height: 1, width: 7 }, | ||
panel3: { id: 'panel3', row: 5, column: 0, height: 1, width: 7 }, | ||
panel4: { id: 'panel4', row: 0, column: 6, height: 3, width: 1 }, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,18 @@ const getAllCollisionsWithPanel = ( | |
return collidingPanels; | ||
}; | ||
|
||
const getFirstCollision = (gridLayout: GridRowData, keysInOrder: string[]): string | undefined => { | ||
for (const panelA of keysInOrder) { | ||
for (const panelB of keysInOrder) { | ||
if (panelA === panelB) continue; | ||
if (collides(gridLayout.panels[panelA], gridLayout.panels[panelB])) { | ||
return panelA; | ||
} | ||
} | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const getKeysInOrder = (panels: GridRowData['panels'], draggedId?: string): string[] => { | ||
const panelKeys = Object.keys(panels); | ||
return panelKeys.sort((panelKeyA, panelKeyB) => { | ||
|
@@ -81,28 +93,45 @@ export const resolveGridRow = ( | |
originalRowData: GridRowData, | ||
dragRequest?: GridPanelData | ||
): GridRowData => { | ||
const nextRowData = { ...originalRowData, panels: { ...originalRowData.panels } }; | ||
|
||
// Apply drag request | ||
let nextRowData = { ...originalRowData, panels: { ...originalRowData.panels } }; | ||
// apply drag request | ||
if (dragRequest) { | ||
nextRowData.panels[dragRequest.id] = dragRequest; | ||
} | ||
// return nextRowData; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how this commented out line stuck around for so long 🤦 It's been there from the very first POC 🫠 |
||
|
||
// push all panels down if they collide with another panel | ||
// get keys in order from top to bottom, left to right, with priority on the dragged item if it exists | ||
const sortedKeys = getKeysInOrder(nextRowData.panels, dragRequest?.id); | ||
|
||
for (const key of sortedKeys) { | ||
const panel = nextRowData.panels[key]; | ||
const collisions = getAllCollisionsWithPanel(panel, nextRowData, sortedKeys); | ||
|
||
for (const collision of collisions) { | ||
const rowOverlap = panel.row + panel.height - collision.row; | ||
if (rowOverlap > 0) { | ||
collision.row += rowOverlap; | ||
} | ||
} | ||
// while the layout has at least one collision, try to resolve them in order | ||
let collision = getFirstCollision(nextRowData, sortedKeys); | ||
while (collision !== undefined) { | ||
nextRowData = resolvePanelCollisions(nextRowData, nextRowData.panels[collision], sortedKeys); | ||
collision = getFirstCollision(nextRowData, sortedKeys); | ||
} | ||
const compactedGrid = compactGridRow(nextRowData); | ||
return compactedGrid; | ||
return compactGridRow(nextRowData); // compact the grid to close any gaps | ||
}; | ||
|
||
/** | ||
* for each panel that collides with `panelToResolve`, push the colliding panel down by a single row and | ||
* recursively handle any collisions that result from that move | ||
*/ | ||
function resolvePanelCollisions( | ||
rowData: GridRowData, | ||
panelToResolve: GridPanelData, | ||
keysInOrder: string[] | ||
): GridRowData { | ||
const collisions = getAllCollisionsWithPanel(panelToResolve, rowData, keysInOrder); | ||
for (const collision of collisions) { | ||
if (collision.id === panelToResolve.id) continue; | ||
rowData.panels[collision.id].row++; | ||
rowData = resolvePanelCollisions( | ||
rowData, | ||
rowData.panels[collision.id], | ||
/** | ||
* when recursively resolving any collisions that result from moving this colliding panel down, | ||
* ignore if `collision` is still colliding with `panelToResolve` to prevent an infinite loop | ||
*/ | ||
keysInOrder.filter((key) => key !== panelToResolve.id) | ||
); | ||
} | ||
return rowData; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added so that the Lens embeddables work - I think a recent merge broke them in this example 🤷