2024-06-04 22:05:44 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-06-19 21:22:34 +02:00
|
|
|
import useResizeObserver from "@react-hook/resize-observer";
|
2024-06-04 22:05:44 +02:00
|
|
|
import clsx from "clsx";
|
2024-06-19 08:44:53 +02:00
|
|
|
import { toPng } from "html-to-image";
|
2024-07-03 23:31:02 +02:00
|
|
|
import { PrimitiveAtom, atom, useAtom, useAtomValue, useSetAtom, useStore } from "jotai";
|
2024-06-12 02:42:10 +02:00
|
|
|
import React, {
|
2024-06-07 02:58:37 +02:00
|
|
|
CSSProperties,
|
|
|
|
ReactNode,
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
Suspense,
|
2024-07-03 23:31:02 +02:00
|
|
|
memo,
|
2024-06-07 02:58:37 +02:00
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useLayoutEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from "react";
|
2024-06-19 20:15:14 +02:00
|
|
|
import { DropTargetMonitor, useDrag, useDragLayer, useDrop } from "react-dnd";
|
|
|
|
import { debounce, throttle } from "throttle-debounce";
|
2024-06-21 19:18:35 +02:00
|
|
|
import { useDevicePixelRatio } from "use-device-pixel-ratio";
|
2024-07-03 23:31:02 +02:00
|
|
|
import { globalLayoutTransformsMap } from "./layoutAtom";
|
2024-06-12 02:42:10 +02:00
|
|
|
import { findNode } from "./layoutNode";
|
2024-07-03 23:31:02 +02:00
|
|
|
import { layoutTreeStateReducer } from "./layoutState";
|
2024-06-04 22:05:44 +02:00
|
|
|
import {
|
|
|
|
ContentRenderer,
|
|
|
|
LayoutNode,
|
|
|
|
LayoutTreeAction,
|
|
|
|
LayoutTreeActionType,
|
|
|
|
LayoutTreeComputeMoveNodeAction,
|
|
|
|
LayoutTreeDeleteNodeAction,
|
2024-06-07 02:58:37 +02:00
|
|
|
LayoutTreeMoveNodeAction,
|
2024-07-03 23:31:02 +02:00
|
|
|
LayoutTreeResizeNodeAction,
|
|
|
|
LayoutTreeSetPendingAction,
|
2024-06-04 22:05:44 +02:00
|
|
|
LayoutTreeState,
|
2024-06-17 23:14:09 +02:00
|
|
|
LayoutTreeSwapNodeAction,
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
PreviewRenderer,
|
2024-06-04 22:05:44 +02:00
|
|
|
WritableLayoutTreeStateAtom,
|
2024-06-12 02:42:10 +02:00
|
|
|
} from "./model";
|
2024-07-03 23:31:02 +02:00
|
|
|
import { NodeRefMap } from "./nodeRefMap";
|
2024-06-04 22:05:44 +02:00
|
|
|
import "./tilelayout.less";
|
2024-06-19 20:15:14 +02:00
|
|
|
import { Dimensions, FlexDirection, setTransform as createTransform, determineDropDirection } from "./utils";
|
2024-06-04 22:05:44 +02:00
|
|
|
|
2024-06-26 18:31:43 +02:00
|
|
|
/**
|
|
|
|
* contains callbacks and information about the contents (or styling) of of the TileLayout
|
|
|
|
* nothing in here is specific to the TileLayout itself
|
|
|
|
*/
|
|
|
|
export interface TileLayoutContents<T> {
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* A callback that accepts the data from the leaf node and displays the leaf contents to the user.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
renderContent: ContentRenderer<T>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* A callback that accepts the data from the leaf node and returns a preview that can be shown when the user drags a node.
|
|
|
|
*/
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
renderPreview?: PreviewRenderer<T>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* A callback that is called when a node gets deleted from the LayoutTreeState.
|
|
|
|
* @param data The contents of the node that was deleted.
|
|
|
|
*/
|
2024-06-06 02:21:40 +02:00
|
|
|
onNodeDelete?: (data: T) => Promise<void>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The class name to use for the top-level div of the tile layout.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
className?: string;
|
2024-06-21 19:18:35 +02:00
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
/**
|
|
|
|
* A callback for getting the cursor point in reference to the current window. This removes Electron as a runtime dependency, allowing for better integration with Storybook.
|
|
|
|
* @returns The cursor position relative to the current window.
|
|
|
|
*/
|
|
|
|
getCursorPoint?: () => Point;
|
|
|
|
|
2024-06-21 21:32:38 +02:00
|
|
|
/**
|
|
|
|
* tabId this TileLayout is associated with
|
|
|
|
*/
|
2024-06-26 21:22:27 +02:00
|
|
|
tabId?: string;
|
2024-06-26 18:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TileLayoutProps<T> {
|
|
|
|
/**
|
|
|
|
* The atom containing the layout tree state.
|
|
|
|
*/
|
|
|
|
layoutTreeStateAtom: WritableLayoutTreeStateAtom<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* callbacks and information about the contents (or styling) of the TileLayout or contents
|
|
|
|
*/
|
|
|
|
contents: TileLayoutContents<T>;
|
2024-06-21 21:32:38 +02:00
|
|
|
|
2024-06-21 19:18:35 +02:00
|
|
|
/**
|
|
|
|
* A callback for getting the cursor point in reference to the current window. This removes Electron as a runtime dependency, allowing for better integration with Storybook.
|
|
|
|
* @returns The cursor position relative to the current window.
|
|
|
|
*/
|
|
|
|
getCursorPoint?: () => Point;
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-19 08:44:53 +02:00
|
|
|
const DragPreviewWidth = 300;
|
|
|
|
const DragPreviewHeight = 300;
|
|
|
|
|
2024-06-26 21:22:27 +02:00
|
|
|
function TileLayoutComponent<T>({ layoutTreeStateAtom, contents, getCursorPoint }: TileLayoutProps<T>) {
|
2024-06-04 22:05:44 +02:00
|
|
|
const overlayContainerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const displayContainerRef = useRef<HTMLDivElement>(null);
|
2024-07-03 23:31:02 +02:00
|
|
|
const jotaiStore = useStore();
|
|
|
|
const layoutTreeState = useAtomValue(layoutTreeStateAtom);
|
|
|
|
const [nodeRefsAtom] = useState<PrimitiveAtom<NodeRefMap>>(atom(new NodeRefMap()));
|
|
|
|
const nodeRefs = useAtomValue(nodeRefsAtom);
|
|
|
|
const dispatch = useCallback(
|
|
|
|
(action: LayoutTreeAction) => {
|
|
|
|
const currentState = jotaiStore.get(layoutTreeStateAtom);
|
|
|
|
jotaiStore.set(layoutTreeStateAtom, layoutTreeStateReducer(currentState, action));
|
2024-06-04 22:05:44 +02:00
|
|
|
},
|
2024-07-03 23:31:02 +02:00
|
|
|
[layoutTreeStateAtom, jotaiStore]
|
2024-06-04 22:05:44 +02:00
|
|
|
);
|
2024-07-03 23:31:02 +02:00
|
|
|
const [showOverlayAtom] = useState<PrimitiveAtom<boolean>>(atom(false));
|
|
|
|
const [showOverlay, setShowOverlay] = useAtom(showOverlayAtom);
|
|
|
|
|
|
|
|
function onPointerOver() {
|
|
|
|
setShowOverlay(true);
|
|
|
|
}
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
const [overlayTransform, setOverlayTransform] = useState<CSSProperties>();
|
2024-06-21 21:32:38 +02:00
|
|
|
const [layoutLeafTransforms, setLayoutLeafTransformsRaw] = useState<Record<string, CSSProperties>>({});
|
|
|
|
|
|
|
|
const setLayoutLeafTransforms = (transforms: Record<string, CSSProperties>) => {
|
2024-06-26 18:31:43 +02:00
|
|
|
globalLayoutTransformsMap.set(contents.tabId, transforms);
|
2024-06-21 21:32:38 +02:00
|
|
|
setLayoutLeafTransformsRaw(transforms);
|
|
|
|
};
|
2024-06-04 22:05:44 +02:00
|
|
|
|
2024-06-19 20:15:14 +02:00
|
|
|
const { activeDrag, dragClientOffset } = useDragLayer((monitor) => ({
|
|
|
|
activeDrag: monitor.isDragging(),
|
|
|
|
dragClientOffset: monitor.getClientOffset(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Effect to detect when the cursor leaves the TileLayout hit trap so we can remove any placeholders. This cannot be done using pointer capture
|
|
|
|
// because that conflicts with the DnD layer.
|
|
|
|
useEffect(
|
|
|
|
debounce(100, () => {
|
2024-06-21 19:18:35 +02:00
|
|
|
const cursorPoint = getCursorPoint?.() ?? dragClientOffset;
|
2024-06-19 20:15:14 +02:00
|
|
|
if (cursorPoint && displayContainerRef.current) {
|
|
|
|
const displayContainerRect = displayContainerRef.current.getBoundingClientRect();
|
|
|
|
const normalizedX = cursorPoint.x - displayContainerRect.x;
|
|
|
|
const normalizedY = cursorPoint.y - displayContainerRect.y;
|
|
|
|
if (
|
|
|
|
normalizedX <= 0 ||
|
|
|
|
normalizedX >= displayContainerRect.width ||
|
|
|
|
normalizedY <= 0 ||
|
|
|
|
normalizedY >= displayContainerRect.height
|
|
|
|
) {
|
|
|
|
dispatch({ type: LayoutTreeActionType.ClearPendingAction });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[dragClientOffset]
|
|
|
|
);
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to update the transforms on the displayed leafs and move the overlay over the display layer when dragging.
|
|
|
|
*/
|
|
|
|
const updateTransforms = useCallback(
|
2024-06-19 20:15:14 +02:00
|
|
|
debounce(30, () => {
|
2024-07-03 23:31:02 +02:00
|
|
|
// TODO: janky way of preventing updates while a node resize is underway
|
|
|
|
if (layoutTreeState.pendingAction?.type === LayoutTreeActionType.ResizeNode) return;
|
2024-06-04 22:05:44 +02:00
|
|
|
if (overlayContainerRef.current && displayContainerRef.current) {
|
|
|
|
const displayBoundingRect = displayContainerRef.current.getBoundingClientRect();
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("displayBoundingRect", displayBoundingRect);
|
2024-06-04 22:05:44 +02:00
|
|
|
const overlayBoundingRect = overlayContainerRef.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
const newLayoutLeafTransforms: Record<string, CSSProperties> = {};
|
|
|
|
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log(
|
|
|
|
// "nodeRefs",
|
|
|
|
// nodeRefs,
|
|
|
|
// "layoutLeafs",
|
|
|
|
// layoutTreeState.leafs,
|
|
|
|
// "layoutTreeState",
|
|
|
|
// layoutTreeState
|
|
|
|
// );
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
for (const leaf of layoutTreeState.leafs) {
|
|
|
|
const leafRef = nodeRefs.get(leaf.id);
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
// console.log("current leafRef", leafRef.current);
|
2024-06-04 22:05:44 +02:00
|
|
|
if (leafRef?.current) {
|
|
|
|
const leafBounding = leafRef.current.getBoundingClientRect();
|
|
|
|
const transform = createTransform({
|
|
|
|
top: leafBounding.top - overlayBoundingRect.top,
|
|
|
|
left: leafBounding.left - overlayBoundingRect.left,
|
|
|
|
width: leafBounding.width,
|
|
|
|
height: leafBounding.height,
|
|
|
|
});
|
|
|
|
newLayoutLeafTransforms[leafRef.current.id] = transform;
|
|
|
|
} else {
|
|
|
|
console.warn("missing leaf", leaf.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setLayoutLeafTransforms(newLayoutLeafTransforms);
|
|
|
|
|
|
|
|
const newOverlayOffset = displayBoundingRect.top + 2 * displayBoundingRect.height;
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("overlayOffset", newOverlayOffset);
|
2024-06-04 22:05:44 +02:00
|
|
|
setOverlayTransform(
|
|
|
|
createTransform(
|
|
|
|
{
|
2024-07-03 23:31:02 +02:00
|
|
|
top: activeDrag || showOverlay ? 0 : newOverlayOffset,
|
2024-06-04 22:05:44 +02:00
|
|
|
left: 0,
|
|
|
|
width: overlayBoundingRect.width,
|
|
|
|
height: overlayBoundingRect.height,
|
|
|
|
},
|
|
|
|
false
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-06-19 20:15:14 +02:00
|
|
|
}),
|
2024-07-03 23:31:02 +02:00
|
|
|
[activeDrag, showOverlay, overlayContainerRef, displayContainerRef, layoutTreeState.leafs, nodeRefs.generation]
|
2024-06-04 22:05:44 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Update the transforms whenever we drag something and whenever the layout updates.
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
updateTransforms();
|
2024-06-11 00:16:29 +02:00
|
|
|
}, [updateTransforms]);
|
2024-06-04 22:05:44 +02:00
|
|
|
|
2024-06-06 23:57:37 +02:00
|
|
|
useResizeObserver(overlayContainerRef, () => updateTransforms());
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
// Ensure that we don't see any jostling in the layout when we're rendering it the first time.
|
|
|
|
// `animate` will be disabled until after the transforms have all applied the first time.
|
|
|
|
const [animate, setAnimate] = useState(false);
|
|
|
|
useEffect(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
setAnimate(true);
|
|
|
|
}, 50);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onLeafClose = useCallback(
|
2024-06-06 02:21:40 +02:00
|
|
|
async (node: LayoutNode<T>) => {
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("onLeafClose", node);
|
2024-06-04 22:05:44 +02:00
|
|
|
const deleteAction: LayoutTreeDeleteNodeAction = {
|
|
|
|
type: LayoutTreeActionType.DeleteNode,
|
|
|
|
nodeId: node.id,
|
|
|
|
};
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("calling dispatch", deleteAction);
|
2024-06-04 22:05:44 +02:00
|
|
|
dispatch(deleteAction);
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("calling onNodeDelete", node);
|
2024-06-26 18:31:43 +02:00
|
|
|
await contents.onNodeDelete?.(node.data);
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("node deleted");
|
2024-06-04 22:05:44 +02:00
|
|
|
},
|
2024-06-26 18:31:43 +02:00
|
|
|
[contents.onNodeDelete, dispatch]
|
2024-06-04 22:05:44 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
<Suspense>
|
2024-07-03 23:31:02 +02:00
|
|
|
<div className={clsx("tile-layout", contents.className, { animate })} onPointerOver={onPointerOver}>
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
<div key="display" ref={displayContainerRef} className="display-container">
|
2024-06-26 18:31:43 +02:00
|
|
|
<DisplayNodesWrapper
|
|
|
|
contents={contents}
|
|
|
|
ready={animate}
|
|
|
|
onLeafClose={onLeafClose}
|
|
|
|
layoutTreeState={layoutTreeState}
|
|
|
|
layoutLeafTransforms={layoutLeafTransforms}
|
|
|
|
/>
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
</div>
|
|
|
|
<Placeholder
|
|
|
|
key="placeholder"
|
2024-06-04 22:05:44 +02:00
|
|
|
layoutTreeState={layoutTreeState}
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
overlayContainerRef={overlayContainerRef}
|
2024-07-03 23:31:02 +02:00
|
|
|
nodeRefsAtom={nodeRefsAtom}
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
style={{ top: 10000, ...overlayTransform }}
|
2024-06-04 22:05:44 +02:00
|
|
|
/>
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
<div
|
|
|
|
key="overlay"
|
|
|
|
ref={overlayContainerRef}
|
|
|
|
className="overlay-container"
|
|
|
|
style={{ top: 10000, ...overlayTransform }}
|
|
|
|
>
|
|
|
|
<OverlayNode
|
|
|
|
layoutNode={layoutTreeState.rootNode}
|
|
|
|
layoutTreeState={layoutTreeState}
|
|
|
|
dispatch={dispatch}
|
2024-07-03 23:31:02 +02:00
|
|
|
nodeRefsAtom={nodeRefsAtom}
|
|
|
|
showOverlayAtom={showOverlayAtom}
|
|
|
|
siblingSize={layoutTreeState.rootNode?.size}
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2024-06-04 22:05:44 +02:00
|
|
|
</div>
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
</Suspense>
|
2024-06-04 22:05:44 +02:00
|
|
|
);
|
2024-06-26 21:22:27 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
export const TileLayout = memo(TileLayoutComponent) as typeof TileLayoutComponent;
|
2024-06-26 18:31:43 +02:00
|
|
|
|
|
|
|
interface DisplayNodesWrapperProps<T> {
|
|
|
|
/**
|
|
|
|
* The layout tree state.
|
|
|
|
*/
|
|
|
|
layoutTreeState: LayoutTreeState<T>;
|
|
|
|
/**
|
|
|
|
* contains callbacks and information about the contents (or styling) of of the TileLayout
|
|
|
|
*/
|
|
|
|
contents: TileLayoutContents<T>;
|
|
|
|
/**
|
|
|
|
* A callback that is called when a leaf node gets closed.
|
|
|
|
* @param node The node that is closed.
|
|
|
|
*/
|
|
|
|
onLeafClose: (node: LayoutNode<T>) => void;
|
|
|
|
/**
|
|
|
|
* A series of CSS properties used to display a leaf node with the correct dimensions and position, as determined from its corresponding OverlayNode.
|
|
|
|
*/
|
|
|
|
layoutLeafTransforms: Record<string, CSSProperties>;
|
|
|
|
/**
|
|
|
|
* Determines whether the leaf nodes are ready to be displayed to the user.
|
|
|
|
*/
|
|
|
|
ready: boolean;
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
const DisplayNodesWrapper = memo(
|
2024-06-26 18:31:43 +02:00
|
|
|
<T,>({ layoutTreeState, contents, onLeafClose, layoutLeafTransforms, ready }: DisplayNodesWrapperProps<T>) => {
|
|
|
|
if (!layoutLeafTransforms) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return layoutTreeState.leafs.map((leaf) => {
|
|
|
|
return (
|
|
|
|
<DisplayNode
|
|
|
|
key={leaf.id}
|
|
|
|
layoutNode={leaf}
|
|
|
|
contents={contents}
|
|
|
|
transform={layoutLeafTransforms[leaf.id]}
|
|
|
|
onLeafClose={onLeafClose}
|
|
|
|
ready={ready}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2024-06-04 22:05:44 +02:00
|
|
|
|
2024-06-14 04:36:06 +02:00
|
|
|
interface DisplayNodeProps<T> {
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The leaf node object, containing the data needed to display the leaf contents to the user.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
layoutNode: LayoutNode<T>;
|
2024-06-26 18:31:43 +02:00
|
|
|
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
2024-06-26 18:31:43 +02:00
|
|
|
* contains callbacks and information about the contents (or styling) of of the TileLayout
|
2024-06-14 04:33:06 +02:00
|
|
|
*/
|
2024-06-26 18:31:43 +02:00
|
|
|
contents: TileLayoutContents<T>;
|
|
|
|
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* A callback that is called when a leaf node gets closed.
|
|
|
|
* @param node The node that is closed.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
onLeafClose: (node: LayoutNode<T>) => void;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* Determines whether a leaf's contents should be displayed to the user.
|
|
|
|
*/
|
2024-06-05 20:56:04 +02:00
|
|
|
ready: boolean;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* A series of CSS properties used to display a leaf node with the correct dimensions and position, as determined from its corresponding OverlayNode.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
transform: CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dragItemType = "TILE_ITEM";
|
|
|
|
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
2024-06-14 04:36:06 +02:00
|
|
|
* The draggable and displayable portion of a leaf node in a layout tree.
|
2024-06-14 04:33:06 +02:00
|
|
|
*/
|
2024-07-03 23:31:02 +02:00
|
|
|
const DisplayNode = memo(<T,>({ layoutNode, contents, transform, onLeafClose, ready }: DisplayNodeProps<T>) => {
|
2024-06-04 22:05:44 +02:00
|
|
|
const tileNodeRef = useRef<HTMLDivElement>(null);
|
2024-06-20 08:00:57 +02:00
|
|
|
const dragHandleRef = useRef<HTMLDivElement>(null);
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
const previewRef = useRef<HTMLDivElement>(null);
|
2024-06-04 22:05:44 +02:00
|
|
|
|
2024-06-21 19:18:35 +02:00
|
|
|
const devicePixelRatio = useDevicePixelRatio();
|
|
|
|
|
2024-06-11 23:51:35 +02:00
|
|
|
const [{ isDragging }, drag, dragPreview] = useDrag(
|
2024-06-04 22:05:44 +02:00
|
|
|
() => ({
|
|
|
|
type: dragItemType,
|
|
|
|
item: () => layoutNode,
|
|
|
|
collect: (monitor) => ({
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
[layoutNode]
|
|
|
|
);
|
|
|
|
|
2024-06-21 19:18:35 +02:00
|
|
|
const [previewElementGeneration, setPreviewElementGeneration] = useState(0);
|
|
|
|
const previewElement = useMemo(() => {
|
|
|
|
setPreviewElementGeneration(previewElementGeneration + 1);
|
|
|
|
return (
|
|
|
|
<div key="preview" className="tile-preview-container">
|
|
|
|
<div
|
|
|
|
className="tile-preview"
|
|
|
|
ref={previewRef}
|
|
|
|
style={{
|
|
|
|
width: DragPreviewWidth,
|
|
|
|
height: DragPreviewHeight,
|
|
|
|
transform: `scale(${1 / devicePixelRatio})`,
|
|
|
|
}}
|
|
|
|
>
|
2024-06-26 18:31:43 +02:00
|
|
|
{contents.renderPreview?.(layoutNode.data)}
|
2024-06-21 19:18:35 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2024-06-26 18:31:43 +02:00
|
|
|
}, [contents.renderPreview, devicePixelRatio]);
|
2024-06-21 19:18:35 +02:00
|
|
|
|
2024-06-19 08:44:53 +02:00
|
|
|
const [previewImage, setPreviewImage] = useState<HTMLImageElement>(null);
|
2024-06-21 19:18:35 +02:00
|
|
|
const [previewImageGeneration, setPreviewImageGeneration] = useState(0);
|
2024-06-11 23:28:17 +02:00
|
|
|
const generatePreviewImage = useCallback(() => {
|
2024-06-21 19:18:35 +02:00
|
|
|
const offsetX = (DragPreviewWidth * devicePixelRatio - DragPreviewWidth) / 2 + 10;
|
|
|
|
const offsetY = (DragPreviewHeight * devicePixelRatio - DragPreviewHeight) / 2 + 10;
|
|
|
|
if (previewImage !== null && previewElementGeneration === previewImageGeneration) {
|
2024-06-19 08:44:53 +02:00
|
|
|
dragPreview(previewImage, { offsetY, offsetX });
|
2024-06-12 01:28:41 +02:00
|
|
|
} else if (previewRef.current) {
|
2024-06-21 19:18:35 +02:00
|
|
|
setPreviewImageGeneration(previewElementGeneration);
|
2024-06-11 23:28:17 +02:00
|
|
|
toPng(previewRef.current).then((url) => {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = url;
|
2024-06-21 19:18:35 +02:00
|
|
|
setPreviewImage(img);
|
|
|
|
dragPreview(img, { offsetY, offsetX });
|
2024-06-11 23:28:17 +02:00
|
|
|
});
|
|
|
|
}
|
2024-06-21 19:18:35 +02:00
|
|
|
}, [
|
|
|
|
dragPreview,
|
|
|
|
previewRef.current,
|
|
|
|
previewElementGeneration,
|
|
|
|
previewImageGeneration,
|
|
|
|
previewImage,
|
|
|
|
devicePixelRatio,
|
|
|
|
]);
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
// Register the tile item as a draggable component
|
|
|
|
useEffect(() => {
|
2024-06-21 19:18:35 +02:00
|
|
|
drag(dragHandleRef);
|
|
|
|
}, [drag, dragHandleRef.current]);
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
const onClose = useCallback(() => {
|
|
|
|
onLeafClose(layoutNode);
|
|
|
|
}, [layoutNode, onLeafClose]);
|
|
|
|
|
2024-06-05 20:56:04 +02:00
|
|
|
const leafContent = useMemo(() => {
|
|
|
|
return (
|
|
|
|
layoutNode.data && (
|
|
|
|
<div key="leaf" className="tile-leaf">
|
2024-06-26 18:31:43 +02:00
|
|
|
{contents.renderContent(layoutNode.data, ready, onClose, dragHandleRef)}
|
2024-06-05 20:56:04 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
);
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
}, [layoutNode.data, ready, onClose]);
|
2024-06-05 20:56:04 +02:00
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
return (
|
|
|
|
<div
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
className={clsx("tile-node", { dragging: isDragging })}
|
2024-06-04 22:05:44 +02:00
|
|
|
ref={tileNodeRef}
|
|
|
|
id={layoutNode.id}
|
|
|
|
style={{
|
|
|
|
...transform,
|
|
|
|
}}
|
2024-06-11 23:28:17 +02:00
|
|
|
onPointerEnter={generatePreviewImage}
|
2024-07-03 23:31:02 +02:00
|
|
|
onPointerOver={(event) => event.stopPropagation()}
|
2024-06-04 22:05:44 +02:00
|
|
|
>
|
2024-06-05 20:56:04 +02:00
|
|
|
{leafContent}
|
2024-06-21 19:18:35 +02:00
|
|
|
{previewElement}
|
2024-06-04 22:05:44 +02:00
|
|
|
</div>
|
|
|
|
);
|
2024-06-26 18:31:43 +02:00
|
|
|
});
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
interface OverlayNodeProps<T> {
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The layout node object corresponding to the OverlayNode.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
layoutNode: LayoutNode<T>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The layout tree state.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
layoutTreeState: LayoutTreeState<T>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The reducer function for mutating the layout tree state.
|
|
|
|
* @param action The action to perform.
|
|
|
|
*/
|
2024-06-04 22:05:44 +02:00
|
|
|
dispatch: (action: LayoutTreeAction) => void;
|
2024-07-03 23:31:02 +02:00
|
|
|
|
|
|
|
nodeRefsAtom: PrimitiveAtom<NodeRefMap>;
|
|
|
|
|
|
|
|
showOverlayAtom: PrimitiveAtom<boolean>;
|
|
|
|
|
|
|
|
showResizeOverlay?: boolean;
|
|
|
|
|
|
|
|
siblingSize: number;
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* An overlay representing the true flexbox layout of the LayoutTreeState. This holds the drop targets for moving around nodes and is used to calculate the
|
2024-06-14 04:36:06 +02:00
|
|
|
* dimensions of the corresponding DisplayNode for each LayoutTreeState leaf.
|
2024-06-14 04:33:06 +02:00
|
|
|
*/
|
2024-07-03 23:31:02 +02:00
|
|
|
const OverlayNode = <T,>({
|
|
|
|
layoutNode,
|
|
|
|
layoutTreeState,
|
|
|
|
dispatch,
|
|
|
|
nodeRefsAtom,
|
|
|
|
showOverlayAtom,
|
|
|
|
showResizeOverlay,
|
|
|
|
siblingSize,
|
|
|
|
}: OverlayNodeProps<T>) => {
|
2024-06-04 22:05:44 +02:00
|
|
|
const overlayRef = useRef<HTMLDivElement>(null);
|
|
|
|
const leafRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
const setShowOverlay = useSetAtom(showOverlayAtom);
|
|
|
|
|
|
|
|
const setNodeRefs = useSetAtom(nodeRefsAtom);
|
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
const [, drop] = useDrop(
|
|
|
|
() => ({
|
|
|
|
accept: dragItemType,
|
|
|
|
canDrop: (_, monitor) => {
|
|
|
|
const dragItem = monitor.getItem<LayoutNode<T>>();
|
|
|
|
if (monitor.isOver({ shallow: true }) && dragItem?.id !== layoutNode.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
drop: (_, monitor) => {
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("drop start", layoutNode.id, layoutTreeState.pendingAction);
|
2024-06-04 22:05:44 +02:00
|
|
|
if (!monitor.didDrop() && layoutTreeState.pendingAction) {
|
|
|
|
dispatch({
|
|
|
|
type: LayoutTreeActionType.CommitPendingAction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-06-19 20:15:14 +02:00
|
|
|
hover: throttle(30, (_, monitor: DropTargetMonitor<unknown, unknown>) => {
|
2024-06-19 01:03:00 +02:00
|
|
|
if (monitor.isOver({ shallow: true })) {
|
|
|
|
if (monitor.canDrop()) {
|
|
|
|
const dragItem = monitor.getItem<LayoutNode<T>>();
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("computing operation", layoutNode, dragItem, layoutTreeState.pendingAction);
|
2024-06-19 01:03:00 +02:00
|
|
|
dispatch({
|
|
|
|
type: LayoutTreeActionType.ComputeMove,
|
|
|
|
node: layoutNode,
|
|
|
|
nodeToMove: dragItem,
|
|
|
|
direction: determineDropDirection(
|
|
|
|
overlayRef.current?.getBoundingClientRect(),
|
|
|
|
monitor.getClientOffset()
|
|
|
|
),
|
|
|
|
} as LayoutTreeComputeMoveNodeAction<T>);
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: LayoutTreeActionType.ClearPendingAction,
|
|
|
|
});
|
|
|
|
}
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
2024-06-19 20:15:14 +02:00
|
|
|
}),
|
2024-06-04 22:05:44 +02:00
|
|
|
}),
|
|
|
|
[overlayRef.current, layoutNode, layoutTreeState, dispatch]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Register the tile item as a draggable component
|
|
|
|
useEffect(() => {
|
|
|
|
const layoutNodeId = layoutNode?.id;
|
|
|
|
if (overlayRef?.current) {
|
|
|
|
drop(overlayRef);
|
2024-07-03 23:31:02 +02:00
|
|
|
setNodeRefs((nodeRefs) => {
|
|
|
|
nodeRefs.set(layoutNodeId, overlayRef);
|
|
|
|
return nodeRefs;
|
|
|
|
});
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
2024-07-03 23:31:02 +02:00
|
|
|
setNodeRefs((nodeRefs) => {
|
|
|
|
nodeRefs.delete(layoutNodeId);
|
|
|
|
return nodeRefs;
|
|
|
|
});
|
2024-06-04 22:05:44 +02:00
|
|
|
};
|
|
|
|
}, [overlayRef]);
|
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
function onPointerOverLeaf(event: React.PointerEvent<HTMLDivElement>) {
|
|
|
|
event.stopPropagation();
|
|
|
|
setShowOverlay(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [resizeOnCurrentNode, setResizeOnCurrentNode] = useState(false);
|
|
|
|
const [pendingSize, setPendingSize] = useState<number>(undefined);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (showResizeOverlay) {
|
|
|
|
setResizeOnCurrentNode(false);
|
|
|
|
setPendingSize(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (layoutTreeState.pendingAction?.type === LayoutTreeActionType.ResizeNode) {
|
|
|
|
const resizeAction = layoutTreeState.pendingAction as LayoutTreeResizeNodeAction;
|
|
|
|
const resizeOperation = resizeAction?.resizeOperations?.find(
|
|
|
|
(operation) => operation.nodeId === layoutNode.id
|
|
|
|
);
|
|
|
|
if (resizeOperation) {
|
|
|
|
setResizeOnCurrentNode(true);
|
|
|
|
setPendingSize(resizeOperation.size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setResizeOnCurrentNode(false);
|
|
|
|
setPendingSize(undefined);
|
|
|
|
}, [showResizeOverlay, layoutTreeState.pendingAction]);
|
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
const generateChildren = () => {
|
|
|
|
if (Array.isArray(layoutNode.children)) {
|
2024-07-03 23:31:02 +02:00
|
|
|
const totalSize = layoutNode.children.reduce((partialSum, child) => partialSum + child.size, 0);
|
|
|
|
return layoutNode.children
|
|
|
|
.map((childItem, i) => {
|
|
|
|
return [
|
|
|
|
<OverlayNode
|
|
|
|
key={childItem.id}
|
|
|
|
layoutNode={childItem}
|
|
|
|
layoutTreeState={layoutTreeState}
|
|
|
|
dispatch={dispatch}
|
|
|
|
nodeRefsAtom={nodeRefsAtom}
|
|
|
|
showOverlayAtom={showOverlayAtom}
|
|
|
|
showResizeOverlay={resizeOnCurrentNode || showResizeOverlay}
|
|
|
|
siblingSize={totalSize}
|
|
|
|
/>,
|
|
|
|
<ResizeHandle
|
|
|
|
key={`resize-${layoutNode.id}-${i}`}
|
|
|
|
parentNode={layoutNode}
|
|
|
|
index={i}
|
|
|
|
dispatch={dispatch}
|
|
|
|
nodeRefsAtom={nodeRefsAtom}
|
|
|
|
siblingSize={totalSize}
|
|
|
|
/>,
|
|
|
|
];
|
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
.slice(0, -1);
|
2024-06-04 22:05:44 +02:00
|
|
|
} else {
|
2024-07-03 23:31:02 +02:00
|
|
|
return [<div ref={leafRef} key="leaf" className="overlay-leaf" onPointerOver={onPointerOverLeaf}></div>];
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!layoutNode) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
const sizePercentage = ((pendingSize ?? layoutNode.size) / siblingSize) * 100;
|
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={overlayRef}
|
2024-07-03 23:31:02 +02:00
|
|
|
className={clsx("overlay-node", { resizing: resizeOnCurrentNode || showResizeOverlay })}
|
2024-06-04 22:05:44 +02:00
|
|
|
id={layoutNode.id}
|
|
|
|
style={{
|
2024-07-03 23:31:02 +02:00
|
|
|
flexBasis: `${sizePercentage.toPrecision(5)}%`,
|
2024-06-04 22:05:44 +02:00
|
|
|
flexDirection: layoutNode.flexDirection,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{generateChildren()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2024-06-07 02:58:37 +02:00
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
interface ResizeHandleProps<T> {
|
|
|
|
parentNode: LayoutNode<T>;
|
|
|
|
index: number;
|
|
|
|
dispatch: (action: LayoutTreeAction) => void;
|
|
|
|
nodeRefsAtom: PrimitiveAtom<NodeRefMap>;
|
|
|
|
siblingSize: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ResizeHandle = <T,>({ parentNode, index, dispatch, nodeRefsAtom, siblingSize }: ResizeHandleProps<T>) => {
|
|
|
|
const resizeHandleRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
// The pointer currently captured, or undefined.
|
|
|
|
const [trackingPointer, setTrackingPointer] = useState<number>(undefined);
|
|
|
|
const nodeRefs = useAtomValue(nodeRefsAtom);
|
|
|
|
|
|
|
|
// Cached values set in startResize
|
|
|
|
const [parentRect, setParentRect] = useState<Dimensions>();
|
|
|
|
const [combinedNodesRect, setCombinedNodesRect] = useState<Dimensions>();
|
|
|
|
const [gapSize, setGapSize] = useState(0);
|
|
|
|
const [pixelToSizeRatio, setPixelToSizeRatio] = useState(0);
|
|
|
|
|
|
|
|
// Precompute some values that will be needed by the handlePointerMove function
|
|
|
|
const startResize = useCallback(
|
|
|
|
throttle(30, () => {
|
|
|
|
const parentRef = nodeRefs.get(parentNode.id);
|
|
|
|
const node1Ref = nodeRefs.get(parentNode.children![index].id);
|
|
|
|
const node2Ref = nodeRefs.get(parentNode.children![index + 1].id);
|
|
|
|
if (parentRef?.current && node1Ref?.current && node2Ref?.current) {
|
|
|
|
const parentIsRow = parentNode.flexDirection === FlexDirection.Row;
|
|
|
|
const parentRectNew = parentRef.current.getBoundingClientRect();
|
|
|
|
setParentRect(parentRectNew);
|
|
|
|
const node1Rect = node1Ref.current.getBoundingClientRect();
|
|
|
|
const node2Rect = node2Ref.current.getBoundingClientRect();
|
|
|
|
const gapSize = parentIsRow
|
|
|
|
? node2Rect.left - (node1Rect.left + node1Rect.width)
|
|
|
|
: node2Rect.top - (node1Rect.top + node1Rect.height);
|
|
|
|
setGapSize(gapSize);
|
|
|
|
const parentPixelsMinusGap =
|
|
|
|
(parentIsRow ? parentRectNew.width : parentRectNew.height) -
|
|
|
|
(gapSize * parentNode.children!.length - 1);
|
|
|
|
const newPixelToSizeRatio = siblingSize / parentPixelsMinusGap;
|
|
|
|
// console.log("newPixelToSizeRatio", newPixelToSizeRatio, siblingSize, parentPixelsMinusGap);
|
|
|
|
setPixelToSizeRatio(newPixelToSizeRatio);
|
|
|
|
const newCombinedNodesRect: Dimensions = {
|
|
|
|
top: node1Rect.top,
|
|
|
|
left: node1Rect.left,
|
|
|
|
height: parentIsRow ? node1Rect.height : node1Rect.height + node2Rect.height + gapSize,
|
|
|
|
width: parentIsRow ? node1Rect.width + node2Rect.width + gapSize : node1Rect.width,
|
|
|
|
};
|
|
|
|
setCombinedNodesRect(newCombinedNodesRect);
|
|
|
|
// console.log(
|
|
|
|
// "startResize",
|
|
|
|
// parentNode,
|
|
|
|
// index,
|
|
|
|
// parentIsRow,
|
|
|
|
// gapSize,
|
|
|
|
// parentRectNew,
|
|
|
|
// node1Rect,
|
|
|
|
// node2Rect,
|
|
|
|
// newCombinedNodesRect
|
|
|
|
// );
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[parentNode, nodeRefs]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Calculates the new size of the two nodes on either side of the handle, based on the position of the cursor
|
|
|
|
const handlePointerMove = useCallback(
|
|
|
|
(clientX: number, clientY: number) => {
|
|
|
|
const parentIsRow = parentNode.flexDirection === FlexDirection.Row;
|
|
|
|
const combinedStart = parentIsRow ? combinedNodesRect.left : combinedNodesRect.top;
|
|
|
|
const combinedEnd = parentIsRow
|
|
|
|
? combinedNodesRect.left + combinedNodesRect.width
|
|
|
|
: combinedNodesRect.top + combinedNodesRect.height;
|
|
|
|
const clientPoint = parentIsRow ? clientX : clientY;
|
|
|
|
// console.log("handlePointerMove", parentNode, index, clientX, clientY, parentRect, combinedNodesRect);
|
|
|
|
if (clientPoint > combinedStart + 10 && clientPoint < combinedEnd - 10) {
|
|
|
|
const halfGap = gapSize / 2;
|
|
|
|
const sizeNode1 = clientPoint - combinedStart - halfGap;
|
|
|
|
const sizeNode2 = combinedEnd - clientPoint + halfGap;
|
|
|
|
const resizeAction: LayoutTreeResizeNodeAction = {
|
|
|
|
type: LayoutTreeActionType.ResizeNode,
|
|
|
|
resizeOperations: [
|
|
|
|
{
|
|
|
|
nodeId: parentNode.children![index].id,
|
|
|
|
size: parseFloat((sizeNode1 * pixelToSizeRatio).toPrecision(5)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodeId: parentNode.children![index + 1].id,
|
|
|
|
size: parseFloat((sizeNode2 * pixelToSizeRatio).toPrecision(5)),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
const setPendingAction: LayoutTreeSetPendingAction = {
|
|
|
|
type: LayoutTreeActionType.SetPendingAction,
|
|
|
|
action: resizeAction,
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(setPendingAction);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[dispatch, parentNode, parentRect, combinedNodesRect, pixelToSizeRatio, gapSize]
|
|
|
|
);
|
|
|
|
|
|
|
|
// We want to use pointer capture so the operation continues even if the pointer leaves the bounds of the handle
|
|
|
|
function onPointerDown(event: React.PointerEvent<HTMLDivElement>) {
|
|
|
|
resizeHandleRef.current?.setPointerCapture(event.pointerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This indicates that we're ready to start tracking the resize operation via the pointer
|
|
|
|
function onPointerCapture(event: React.PointerEvent<HTMLDivElement>) {
|
|
|
|
setTrackingPointer(event.pointerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to wait a bit before committing the pending resize operation in case some events haven't arrived yet.
|
|
|
|
const onPointerRelease = useCallback(
|
|
|
|
debounce(30, (event: React.PointerEvent<HTMLDivElement>) => {
|
|
|
|
setTrackingPointer(undefined);
|
|
|
|
dispatch({ type: LayoutTreeActionType.CommitPendingAction });
|
|
|
|
}),
|
|
|
|
[dispatch]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only track pointer moves if we have a captured pointer.
|
|
|
|
const onPointerMove = useCallback(
|
|
|
|
throttle(10, (event: React.PointerEvent<HTMLDivElement>) => {
|
|
|
|
if (trackingPointer === event.pointerId) {
|
|
|
|
handlePointerMove(event.clientX, event.clientY);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[trackingPointer, handlePointerMove]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Don't render if we are dealing with the last child of a parent
|
|
|
|
if (index + 1 >= parentNode.children!.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={resizeHandleRef}
|
|
|
|
className={clsx("resize-handle", `flex-${parentNode.flexDirection}`)}
|
|
|
|
onPointerDown={onPointerDown}
|
|
|
|
onGotPointerCapture={onPointerCapture}
|
|
|
|
onLostPointerCapture={onPointerRelease}
|
|
|
|
onPointerMove={onPointerMove}
|
|
|
|
onPointerEnter={startResize}
|
|
|
|
>
|
|
|
|
<div className="line" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-07 02:58:37 +02:00
|
|
|
interface PlaceholderProps<T> {
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The layout tree state.
|
|
|
|
*/
|
2024-06-07 02:58:37 +02:00
|
|
|
layoutTreeState: LayoutTreeState<T>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* A reference to the div containing the overlay nodes. Used to normalize the position of the target node as the overlay container is moved in and out of view.
|
|
|
|
*/
|
2024-06-07 02:58:37 +02:00
|
|
|
overlayContainerRef: React.RefObject<HTMLElement>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* The mapping of all layout nodes to their corresponding mounted overlay node.
|
|
|
|
*/
|
2024-07-03 23:31:02 +02:00
|
|
|
nodeRefsAtom: PrimitiveAtom<NodeRefMap>;
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* Any styling to apply to the placeholder container div.
|
|
|
|
*/
|
2024-06-07 02:58:37 +02:00
|
|
|
style: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
2024-06-14 04:33:06 +02:00
|
|
|
/**
|
|
|
|
* An overlay to preview pending actions on the layout tree.
|
|
|
|
*/
|
2024-07-03 23:31:02 +02:00
|
|
|
const Placeholder = <T,>({ layoutTreeState, overlayContainerRef, nodeRefsAtom, style }: PlaceholderProps<T>) => {
|
2024-06-07 02:58:37 +02:00
|
|
|
const [placeholderOverlay, setPlaceholderOverlay] = useState<ReactNode>(null);
|
|
|
|
|
2024-07-03 23:31:02 +02:00
|
|
|
const nodeRefs = useAtomValue(nodeRefsAtom);
|
|
|
|
|
2024-06-07 02:58:37 +02:00
|
|
|
useEffect(() => {
|
|
|
|
let newPlaceholderOverlay: ReactNode;
|
2024-06-17 23:14:09 +02:00
|
|
|
if (overlayContainerRef?.current) {
|
|
|
|
switch (layoutTreeState?.pendingAction?.type) {
|
|
|
|
case LayoutTreeActionType.Move: {
|
|
|
|
const action = layoutTreeState.pendingAction as LayoutTreeMoveNodeAction<T>;
|
|
|
|
let parentId: string;
|
|
|
|
if (action.insertAtRoot) {
|
|
|
|
parentId = layoutTreeState.rootNode.id;
|
|
|
|
} else {
|
|
|
|
parentId = action.parentId;
|
|
|
|
}
|
2024-06-07 02:58:37 +02:00
|
|
|
|
2024-06-17 23:14:09 +02:00
|
|
|
const parentNode = findNode(layoutTreeState.rootNode, parentId);
|
|
|
|
if (action.index !== undefined && parentNode) {
|
|
|
|
const targetIndex = Math.min(
|
|
|
|
parentNode.children ? parentNode.children.length - 1 : 0,
|
|
|
|
Math.max(0, action.index - 1)
|
|
|
|
);
|
|
|
|
let targetNode = parentNode?.children?.at(targetIndex);
|
|
|
|
let targetRef: React.RefObject<HTMLElement>;
|
|
|
|
if (targetNode) {
|
|
|
|
targetRef = nodeRefs.get(targetNode.id);
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
} else {
|
2024-06-17 23:14:09 +02:00
|
|
|
targetRef = nodeRefs.get(parentNode.id);
|
|
|
|
targetNode = parentNode;
|
|
|
|
}
|
|
|
|
if (targetRef?.current) {
|
|
|
|
const overlayBoundingRect = overlayContainerRef.current.getBoundingClientRect();
|
|
|
|
const targetBoundingRect = targetRef.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Placeholder should be either half the height or half the width of the targetNode, depending on the flex direction of the targetNode's parent.
|
|
|
|
// Default to placing the placeholder in the first half of the target node.
|
|
|
|
const placeholderDimensions: Dimensions = {
|
|
|
|
height:
|
|
|
|
parentNode.flexDirection === FlexDirection.Column
|
|
|
|
? targetBoundingRect.height / 2
|
|
|
|
: targetBoundingRect.height,
|
|
|
|
width:
|
|
|
|
parentNode.flexDirection === FlexDirection.Row
|
|
|
|
? targetBoundingRect.width / 2
|
|
|
|
: targetBoundingRect.width,
|
|
|
|
top: targetBoundingRect.top - overlayBoundingRect.top,
|
|
|
|
left: targetBoundingRect.left - overlayBoundingRect.left,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (action.index > targetIndex) {
|
|
|
|
if (action.index >= (parentNode.children?.length ?? 1)) {
|
|
|
|
// If there are no more nodes after the specified index, place the placeholder in the second half of the target node (either right or bottom).
|
|
|
|
placeholderDimensions.top +=
|
|
|
|
parentNode.flexDirection === FlexDirection.Column &&
|
|
|
|
targetBoundingRect.height / 2;
|
|
|
|
placeholderDimensions.left +=
|
|
|
|
parentNode.flexDirection === FlexDirection.Row && targetBoundingRect.width / 2;
|
|
|
|
} else {
|
|
|
|
// Otherwise, place the placeholder between the target node (the one after which it will be inserted) and the next node
|
|
|
|
placeholderDimensions.top +=
|
|
|
|
parentNode.flexDirection === FlexDirection.Column &&
|
|
|
|
(3 * targetBoundingRect.height) / 4;
|
|
|
|
placeholderDimensions.left +=
|
|
|
|
parentNode.flexDirection === FlexDirection.Row &&
|
|
|
|
(3 * targetBoundingRect.width) / 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const placeholderTransform = createTransform(placeholderDimensions);
|
|
|
|
newPlaceholderOverlay = <div className="placeholder" style={{ ...placeholderTransform }} />;
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
}
|
2024-06-07 02:58:37 +02:00
|
|
|
}
|
2024-06-17 23:14:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LayoutTreeActionType.Swap: {
|
2024-07-03 23:31:02 +02:00
|
|
|
const action = layoutTreeState.pendingAction as LayoutTreeSwapNodeAction;
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("placeholder for swap", action);
|
2024-07-03 23:31:02 +02:00
|
|
|
const targetNodeId = action.node1Id;
|
|
|
|
const targetRef = nodeRefs.get(targetNodeId);
|
2024-06-17 23:14:09 +02:00
|
|
|
if (targetRef?.current) {
|
|
|
|
const overlayBoundingRect = overlayContainerRef.current.getBoundingClientRect();
|
|
|
|
const targetBoundingRect = targetRef.current.getBoundingClientRect();
|
|
|
|
const placeholderDimensions: Dimensions = {
|
|
|
|
top: targetBoundingRect.top - overlayBoundingRect.top,
|
|
|
|
left: targetBoundingRect.left - overlayBoundingRect.left,
|
|
|
|
height: targetBoundingRect.height,
|
|
|
|
width: targetBoundingRect.width,
|
|
|
|
};
|
|
|
|
|
|
|
|
const placeholderTransform = createTransform(placeholderDimensions);
|
|
|
|
newPlaceholderOverlay = <div className="placeholder" style={{ ...placeholderTransform }} />;
|
|
|
|
}
|
|
|
|
break;
|
2024-06-07 02:58:37 +02:00
|
|
|
}
|
2024-06-17 23:14:09 +02:00
|
|
|
default:
|
|
|
|
// No-op
|
|
|
|
break;
|
2024-06-07 02:58:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setPlaceholderOverlay(newPlaceholderOverlay);
|
2024-07-03 23:31:02 +02:00
|
|
|
}, [layoutTreeState.pendingAction, nodeRefs, overlayContainerRef]);
|
2024-06-07 02:58:37 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="placeholder-container" style={style}>
|
|
|
|
{placeholderOverlay}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|