2024-06-04 22:05:44 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import { CSSProperties } from "react";
|
|
|
|
import { XYCoord } from "react-dnd";
|
2024-08-26 20:56:00 +02:00
|
|
|
import { DropDirection, FlexDirection, NavigateDirection } from "./types";
|
2024-06-04 22:05:44 +02:00
|
|
|
|
|
|
|
export function reverseFlexDirection(flexDirection: FlexDirection): FlexDirection {
|
|
|
|
return flexDirection === FlexDirection.Row ? FlexDirection.Column : FlexDirection.Row;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function determineDropDirection(dimensions?: Dimensions, offset?: XYCoord | null): DropDirection | undefined {
|
2024-06-19 20:15:14 +02:00
|
|
|
// console.log("determineDropDirection", dimensions, offset);
|
2024-06-04 22:05:44 +02:00
|
|
|
if (!offset || !dimensions) return undefined;
|
|
|
|
const { width, height, left, top } = dimensions;
|
|
|
|
let { x, y } = offset;
|
|
|
|
x -= left;
|
|
|
|
y -= top;
|
|
|
|
|
|
|
|
// Lies outside of the box
|
|
|
|
if (y < 0 || y > height || x < 0 || x > width) return undefined;
|
|
|
|
|
2024-06-17 23:14:09 +02:00
|
|
|
// Determines if a drop point falls within the center fifth of the box, meaning we should return Center.
|
|
|
|
const centerX1 = (2 * width) / 5;
|
|
|
|
const centerX2 = (3 * width) / 5;
|
|
|
|
const centerY1 = (2 * height) / 5;
|
|
|
|
const centerY2 = (3 * height) / 5;
|
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-17 23:14:09 +02:00
|
|
|
if (x > centerX1 && x < centerX2 && y > centerY1 && y < centerY2) return DropDirection.Center;
|
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
|
|
|
const diagonal1 = y * width - x * height;
|
|
|
|
const diagonal2 = y * width + x * height - height * width;
|
|
|
|
|
|
|
|
// Lies on diagonal
|
|
|
|
if (diagonal1 == 0 || diagonal2 == 0) return undefined;
|
|
|
|
|
|
|
|
let code = 0;
|
|
|
|
|
|
|
|
if (diagonal2 > 0) {
|
|
|
|
code += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diagonal1 > 0) {
|
|
|
|
code += 2;
|
|
|
|
code = 5 - code;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Determines whether a drop is close to an edge of the box, meaning drop direction should be OuterX, instead of X
|
|
|
|
const xOuter1 = width / 5;
|
|
|
|
const xOuter2 = width - width / 5;
|
|
|
|
const yOuter1 = height / 5;
|
|
|
|
const yOuter2 = height - height / 5;
|
|
|
|
|
|
|
|
if (y < yOuter1 || y > yOuter2 || x < xOuter1 || x > xOuter2) {
|
|
|
|
code += 4;
|
|
|
|
}
|
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2024-08-15 03:40:41 +02:00
|
|
|
export function setTransform(
|
|
|
|
{ top, left, width, height }: Dimensions,
|
|
|
|
setSize = true,
|
|
|
|
roundVals = true
|
|
|
|
): CSSProperties {
|
2024-06-04 22:05:44 +02:00
|
|
|
// Replace unitless items with px
|
2024-08-15 03:40:41 +02:00
|
|
|
const topRounded = roundVals ? Math.floor(top) : top;
|
|
|
|
const leftRounded = roundVals ? Math.floor(left) : left;
|
|
|
|
const widthRounded = roundVals ? Math.ceil(width) : width;
|
|
|
|
const heightRounded = roundVals ? Math.ceil(height) : height;
|
2024-07-19 03:46:04 +02:00
|
|
|
const translate = `translate3d(${leftRounded}px,${topRounded}px, 0)`;
|
2024-06-04 22:05:44 +02:00
|
|
|
return {
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
transform: translate,
|
|
|
|
WebkitTransform: translate,
|
|
|
|
MozTransform: translate,
|
|
|
|
msTransform: translate,
|
|
|
|
OTransform: translate,
|
2024-07-19 03:46:04 +02:00
|
|
|
width: setSize ? `${widthRounded}px` : undefined,
|
|
|
|
height: setSize ? `${heightRounded}px` : undefined,
|
2024-06-04 22:05:44 +02:00
|
|
|
position: "absolute",
|
|
|
|
};
|
|
|
|
}
|
2024-08-26 20:56:00 +02:00
|
|
|
|
|
|
|
export function getCenter(dimensions: Dimensions): Point {
|
|
|
|
return {
|
|
|
|
x: dimensions.left + dimensions.width / 2,
|
|
|
|
y: dimensions.top + dimensions.height / 2,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function navigateDirectionToOffset(direction: NavigateDirection): Point {
|
|
|
|
switch (direction) {
|
|
|
|
case NavigateDirection.Up:
|
|
|
|
return { x: 0, y: -1 };
|
|
|
|
case NavigateDirection.Down:
|
|
|
|
return { x: 0, y: 1 };
|
|
|
|
case NavigateDirection.Left:
|
|
|
|
return { x: -1, y: 0 };
|
|
|
|
case NavigateDirection.Right:
|
|
|
|
return { x: 1, y: 0 };
|
|
|
|
}
|
|
|
|
}
|