feat: allows textBoxes to be dragged from any point

This commit is contained in:
Mark Mankarious
2023-09-02 10:10:25 +01:00
parent ca087b4322
commit 2f6cfa1274
2 changed files with 22 additions and 16 deletions

View File

@@ -1,6 +1,5 @@
import React, { useMemo } from 'react';
import { ProjectionOrientationEnum } from 'src/types';
import { CoordsUtils } from 'src/utils';
import { getTextBoxTo } from 'src/utils';
import { useTextBox } from 'src/hooks/useTextBox';
import { TransformControls } from './TransformControls';
@@ -12,18 +11,8 @@ export const TextBoxTransformControls = ({ id }: Props) => {
const textBox = useTextBox(id);
const to = useMemo(() => {
if (textBox.orientation === ProjectionOrientationEnum.X) {
return CoordsUtils.add(textBox.tile, {
x: textBox.size.width,
y: 0
});
}
return CoordsUtils.add(textBox.tile, {
x: 0,
y: -textBox.size.width
});
}, [textBox.orientation, textBox.size.width, textBox.tile]);
return getTextBoxTo(textBox);
}, [textBox]);
return <TransformControls from={textBox.tile} to={to} />;
};

View File

@@ -21,7 +21,8 @@ import {
Rect,
ProjectionOrientationEnum,
AnchorPositionsEnum,
BoundingBox
BoundingBox,
TextBox
} from 'src/types';
import {
CoordsUtils,
@@ -436,6 +437,20 @@ export const connectorPathTileToGlobal = (tile: Coords, origin: Coords) => {
);
};
export const getTextBoxTo = (textBox: TextBox) => {
if (textBox.orientation === ProjectionOrientationEnum.X) {
return CoordsUtils.add(textBox.tile, {
x: textBox.size.width,
y: 0
});
}
return CoordsUtils.add(textBox.tile, {
x: 0,
y: -textBox.size.width
});
};
interface GetItemAtTile {
tile: Coords;
scene: Scene;
@@ -452,7 +467,9 @@ export const getItemAtTile = ({
if (node) return node;
const textBox = scene.textBoxes.find((tb) => {
return CoordsUtils.isEqual(tb.tile, tile);
const textBoxBounds = getBoundingBox([tb.tile, getTextBoxTo(tb)]);
return isWithinBounds(tile, textBoxBounds);
});
if (textBox) return textBox;