-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Problem:** Sometimes we want to swap items around in the grid. **Fix:** Ported over the `rearrangeGridSwapStrategy` from the spike branch, which swaps the dragged cell with a target cell that it is dragged over. **Commit Details:** - Added `rearrangeGridSwapStrategy` to `moveOrReorderStrategies`. **Manual Tests:** I hereby swear that: - [x] I opened a hydrogen project and it loaded - [x] I could navigate to various routes in Preview mode
- Loading branch information
1 parent
870ccb4
commit 0167916
Showing
3 changed files
with
428 additions
and
0 deletions.
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
210 changes: 210 additions & 0 deletions
210
...onents/canvas/canvas-strategies/strategies/rearrange-grid-swap-strategy.spec.browser2.tsx
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,210 @@ | ||
import { getPrintedUiJsCode, renderTestEditorWithCode } from '../../ui-jsx.test-utils' | ||
import * as EP from '../../../../core/shared/element-path' | ||
import { selectComponents } from '../../../../components/editor/actions/meta-actions' | ||
import { CanvasControlsContainerID } from '../../controls/new-canvas-controls' | ||
import { | ||
mouseDownAtPoint, | ||
mouseMoveToPoint, | ||
mouseUpAtPoint, | ||
pressKey, | ||
} from '../../event-helpers.test-utils' | ||
import { canvasPoint } from '../../../../core/shared/math-utils' | ||
import { GridCellTestId } from '../../controls/grid-controls' | ||
|
||
const testProject = ` | ||
import * as React from 'react' | ||
import { Storyboard } from 'utopia-api' | ||
export var storyboard = ( | ||
<Storyboard data-uid='sb'> | ||
<div | ||
data-uid='grid' | ||
data-testid='grid' | ||
style={{ | ||
position: 'absolute', | ||
left: 25, | ||
top: 305, | ||
display: 'grid', | ||
gap: 10, | ||
width: 600, | ||
height: 600, | ||
gridTemplateColumns: '2.4fr 1fr 1fr', | ||
gridTemplateRows: '99px 109px 90px', | ||
height: 'max-content', | ||
}} | ||
> | ||
<div | ||
data-uid='row-1-column-1' | ||
data-testid='row-1-column-1' | ||
style={{ | ||
backgroundColor: 'green', | ||
}} | ||
/> | ||
<div | ||
data-uid='row-2-column-1' | ||
data-testid='row-2-column-1' | ||
style={{ backgroundColor: 'blue' }} | ||
/> | ||
<div | ||
data-uid='row-1-column-3' | ||
data-testid='row-1-column-3' | ||
style={{ backgroundColor: 'pink' }} | ||
/> | ||
<div | ||
data-uid='row-1-column-2' | ||
data-testid='row-1-column-2' | ||
style={{ | ||
backgroundColor: 'green', | ||
}} | ||
/> | ||
<div | ||
data-uid='row-2-column-2' | ||
data-testid='row-2-column-2' | ||
style={{ | ||
backgroundColor: 'blue', | ||
}} | ||
/> | ||
<div | ||
data-uid='row-2-column-3' | ||
data-testid='row-2-column-3' | ||
style={{ backgroundColor: 'pink' }} | ||
/> | ||
<div | ||
data-uid='row-3-column-1' | ||
data-testid='row-3-column-1' | ||
style={{ backgroundColor: 'green' }} | ||
/> | ||
<div | ||
data-uid='row-3-column-2' | ||
data-testid='row-3-column-2' | ||
style={{ backgroundColor: 'blue' }} | ||
/> | ||
<div | ||
data-uid='row-3-column-3' | ||
data-testid='row-3-column-3' | ||
style={{ backgroundColor: 'pink' }} | ||
/> | ||
</div> | ||
</Storyboard> | ||
) | ||
` | ||
|
||
describe('swap an element', () => { | ||
it('swap out an element for another from a different column and row', async () => { | ||
const renderResult = await renderTestEditorWithCode(testProject, 'await-first-dom-report') | ||
const draggedItem = EP.fromString(`sb/grid/row-1-column-2`) | ||
await renderResult.dispatch(selectComponents([draggedItem], false), true) | ||
await renderResult.getDispatchFollowUpActionsFinished() | ||
const canvasControlsLayer = renderResult.renderedDOM.getByTestId(CanvasControlsContainerID) | ||
const draggedItemElement = renderResult.renderedDOM.getByTestId( | ||
GridCellTestId(EP.fromString(`sb/grid/row-1-column-2`)), | ||
) | ||
const draggedItemRect = draggedItemElement.getBoundingClientRect() | ||
const startPoint = canvasPoint({ | ||
x: draggedItemRect.x + draggedItemRect.width / 2, | ||
y: draggedItemRect.y + draggedItemRect.height / 2, | ||
}) | ||
const swapTargetElement = renderResult.renderedDOM.getByTestId( | ||
GridCellTestId(EP.fromString(`sb/grid/row-2-column-1`)), | ||
) | ||
const swapTargetRect = swapTargetElement.getBoundingClientRect() | ||
const endPoint = canvasPoint({ | ||
x: swapTargetRect.x + swapTargetRect.width / 2, | ||
y: swapTargetRect.y + swapTargetRect.height / 2, | ||
}) | ||
await mouseMoveToPoint(draggedItemElement, startPoint) | ||
await mouseDownAtPoint(draggedItemElement, startPoint) | ||
await mouseMoveToPoint(canvasControlsLayer, endPoint) | ||
await pressKey('Tab') | ||
await renderResult.getDispatchFollowUpActionsFinished() | ||
expect( | ||
renderResult.getEditorState().editor.canvas.interactionSession?.userPreferredStrategy, | ||
).toEqual('rearrange-grid-swap-strategy') | ||
await mouseUpAtPoint(canvasControlsLayer, endPoint) | ||
await renderResult.getDispatchFollowUpActionsFinished() | ||
|
||
expect(getPrintedUiJsCode(renderResult.getEditorState())) | ||
.toEqual(`import * as React from 'react' | ||
import { Storyboard } from 'utopia-api' | ||
export var storyboard = ( | ||
<Storyboard data-uid='sb'> | ||
<div | ||
data-uid='grid' | ||
data-testid='grid' | ||
style={{ | ||
position: 'absolute', | ||
left: 25, | ||
top: 305, | ||
display: 'grid', | ||
gap: 10, | ||
width: 600, | ||
height: 600, | ||
gridTemplateColumns: '2.4fr 1fr 1fr', | ||
gridTemplateRows: '99px 109px 90px', | ||
height: 'max-content', | ||
}} | ||
> | ||
<div | ||
data-uid='row-1-column-1' | ||
data-testid='row-1-column-1' | ||
style={{ backgroundColor: 'green' }} | ||
/> | ||
<div | ||
data-uid='row-1-column-2' | ||
data-testid='row-1-column-2' | ||
style={{ | ||
backgroundColor: 'green', | ||
gridColumnStart: 'auto', | ||
gridColumnEnd: 'auto', | ||
gridRowStart: 'auto', | ||
gridRowEnd: 'auto', | ||
}} | ||
/> | ||
<div | ||
data-uid='row-1-column-3' | ||
data-testid='row-1-column-3' | ||
style={{ backgroundColor: 'pink' }} | ||
/> | ||
<div | ||
data-uid='row-2-column-1' | ||
data-testid='row-2-column-1' | ||
style={{ | ||
backgroundColor: 'blue', | ||
gridColumnStart: 'auto', | ||
gridColumnEnd: 'auto', | ||
gridRowStart: 'auto', | ||
gridRowEnd: 'auto', | ||
}} | ||
/> | ||
<div | ||
data-uid='row-2-column-2' | ||
data-testid='row-2-column-2' | ||
style={{ backgroundColor: 'blue' }} | ||
/> | ||
<div | ||
data-uid='row-2-column-3' | ||
data-testid='row-2-column-3' | ||
style={{ backgroundColor: 'pink' }} | ||
/> | ||
<div | ||
data-uid='row-3-column-1' | ||
data-testid='row-3-column-1' | ||
style={{ backgroundColor: 'green' }} | ||
/> | ||
<div | ||
data-uid='row-3-column-2' | ||
data-testid='row-3-column-2' | ||
style={{ backgroundColor: 'blue' }} | ||
/> | ||
<div | ||
data-uid='row-3-column-3' | ||
data-testid='row-3-column-3' | ||
style={{ backgroundColor: 'pink' }} | ||
/> | ||
</div> | ||
</Storyboard> | ||
) | ||
`) | ||
}) | ||
}) |
Oops, something went wrong.