Skip to content
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

Fix deleting newly placed components with Backspace key #771

Merged
merged 5 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { Grid, styled } from '@mui/material';
import invariant from 'invariant';

export interface OverlayGridHandle {
gridElement: HTMLDivElement | null;
Expand Down Expand Up @@ -37,17 +38,18 @@ export const OverlayGrid = React.forwardRef<OverlayGridHandle>(function OverlayG
React.useImperativeHandle(
forwardedRef,
() => {
const gridElement = gridRef.current;
invariant(gridElement, 'Overlay grid ref not bound');

let columnEdges: number[] = [];
if (gridRef.current) {
const gridColumnContainers = Array.from(gridRef.current.children);
const gridColumnEdges = gridColumnContainers.map((container: Element) => {
const containerRect = container.firstElementChild?.getBoundingClientRect();
return containerRect
? [Math.round(containerRect.x), Math.round(containerRect.x + containerRect.width)]
: [];
});
columnEdges = gridColumnEdges.flat();
}
const gridColumnContainers = Array.from(gridElement.children);
const gridColumnEdges = gridColumnContainers.map((container: Element) => {
const containerRect = container.firstElementChild?.getBoundingClientRect();
return containerRect
? [Math.round(containerRect.x), Math.round(containerRect.x + containerRect.width)]
: [];
});
columnEdges = gridColumnEdges.flat();

return {
gridElement: gridRef.current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { NodeId } from '@mui/toolpad-core';
import { styled } from '@mui/material';
import clsx from 'clsx';
import invariant from 'invariant';

import * as appDom from '../../../../appDom';
import { useDom, useDomApi } from '../../../DomLoader';
Expand Down Expand Up @@ -164,6 +165,8 @@ export default function RenderOverlay({ canvasHostRef }: RenderOverlayProps) {

const selectedNode = selection && appDom.getNode(dom, selection);

const overlayRef = React.useRef<HTMLDivElement | null>(null);

const draggedNode = React.useMemo(
(): appDom.ElementNode | null =>
newNode || (draggedNodeId && appDom.getNode(dom, draggedNodeId, 'element')),
Expand Down Expand Up @@ -1011,14 +1014,19 @@ export default function RenderOverlay({ canvasHostRef }: RenderOverlayProps) {
}
}

api.dragEnd();

if (selection) {
deleteOrphanedLayoutComponents(draggedNode, dragOverNodeId);
}

api.dragEnd();

if (newNode) {
api.select(newNode.id);

// Refocus on overlay so that keyboard events can keep being caught by it
const overlayElement = overlayRef.current;
invariant(overlayElement, 'Overlay ref not bound');
overlayElement.focus();
}
},
[
Expand Down Expand Up @@ -1309,6 +1317,7 @@ export default function RenderOverlay({ canvasHostRef }: RenderOverlayProps) {

return (
<OverlayRoot
ref={overlayRef}
className={clsx({
[overlayClasses.nodeDrag]: isDraggingOver,
[overlayClasses.resizeHorizontal]:
Expand Down