Make Dimension an actual svg line
This commit is contained in:
parent
cc469b3985
commit
10726d40e4
4 changed files with 52 additions and 79 deletions
|
@ -63,7 +63,8 @@ export const Container: React.FC<IContainerProps> = (props: IContainerProps) =>
|
|||
id={id}
|
||||
xStart={xStart}
|
||||
xEnd={xEnd}
|
||||
y={y}
|
||||
yStart={y}
|
||||
yEnd={y}
|
||||
strokeWidth={strokeWidth}
|
||||
text={text}
|
||||
/>
|
||||
|
|
|
@ -3,48 +3,84 @@ import * as React from 'react';
|
|||
interface IDimensionProps {
|
||||
id: string
|
||||
xStart: number
|
||||
yStart: number
|
||||
xEnd: number
|
||||
y: number
|
||||
yEnd: number
|
||||
text: string
|
||||
strokeWidth: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 2D Parametric function. Returns a new coordinate from the origin coordinate
|
||||
* See for more details https://en.wikipedia.org/wiki/Parametric_equation.
|
||||
* TL;DR a parametric function is a function with a parameter
|
||||
* @param x0 Origin coordinate
|
||||
* @param t The parameter
|
||||
* @param vx Transform vector
|
||||
* @returns Returns a new coordinate from the origin coordinate
|
||||
*/
|
||||
const applyParametric = (x0: number, t: number, vx: number): number => x0 + t * vx;
|
||||
|
||||
export const Dimension: React.FC<IDimensionProps> = (props: IDimensionProps) => {
|
||||
const style: React.CSSProperties = {
|
||||
stroke: 'black'
|
||||
};
|
||||
|
||||
/// We need to find the points of the notches
|
||||
// Get the vector of the line
|
||||
const [deltaX, deltaY] = [(props.xEnd - props.xStart), (props.yEnd - props.yStart)];
|
||||
|
||||
// Get the unit vector
|
||||
const norm = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
|
||||
const [unitX, unitY] = [deltaX / norm, deltaY / norm];
|
||||
|
||||
// Get the perpandicular vector
|
||||
const [perpVecX, perpVecY] = [unitY, -unitX];
|
||||
|
||||
// Use the parametric function to get the coordinates (x = x0 + t * v.x)
|
||||
const startTopX = applyParametric(props.xStart, 4, perpVecX);
|
||||
const startTopY = applyParametric(props.yStart, 4, perpVecY);
|
||||
const startBottomX = applyParametric(props.xStart, -4, perpVecX);
|
||||
const startBottomY = applyParametric(props.yStart, -4, perpVecY);
|
||||
|
||||
const endTopX = applyParametric(props.xEnd, 4, perpVecX);
|
||||
const endTopY = applyParametric(props.yEnd, 4, perpVecY);
|
||||
const endBottomX = applyParametric(props.xEnd, -4, perpVecX);
|
||||
const endBottomY = applyParametric(props.yEnd, -4, perpVecY);
|
||||
|
||||
return (
|
||||
<g key={props.id}>
|
||||
<line
|
||||
x1={props.xStart}
|
||||
y1={props.y - 4 * props.strokeWidth}
|
||||
x2={props.xStart}
|
||||
y2={props.y + 4 * props.strokeWidth}
|
||||
x1={startTopX}
|
||||
y1={startTopY}
|
||||
x2={startBottomX}
|
||||
y2={startBottomY}
|
||||
strokeWidth={props.strokeWidth}
|
||||
style={style}
|
||||
/>
|
||||
<line
|
||||
x1={props.xStart}
|
||||
y1={props.y}
|
||||
y1={props.yStart}
|
||||
x2={props.xEnd}
|
||||
y2={props.y}
|
||||
y2={props.yEnd}
|
||||
strokeWidth={props.strokeWidth}
|
||||
style={style}
|
||||
/>
|
||||
<line
|
||||
x1={props.xEnd}
|
||||
y1={props.y - 4 * props.strokeWidth}
|
||||
x2={props.xEnd}
|
||||
y2={props.y + 4 * props.strokeWidth}
|
||||
x1={endTopX}
|
||||
y1={endTopY}
|
||||
x2={endBottomX}
|
||||
y2={endBottomY}
|
||||
strokeWidth={props.strokeWidth}
|
||||
style={style}
|
||||
/>
|
||||
<text
|
||||
x={(props.xStart + props.xEnd) / 2}
|
||||
y={props.y}
|
||||
y={props.yStart}
|
||||
>
|
||||
{props.text}
|
||||
</text>
|
||||
</g>
|
||||
);
|
||||
};
|
||||
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
import * as React from 'react';
|
||||
import { ContainerModel } from '../../../Interfaces/ContainerModel';
|
||||
import { getDepth, MakeIterator } from '../../../utils/itertools';
|
||||
import { Dimension } from './Dimension';
|
||||
|
||||
interface IDimensionLayerProps {
|
||||
isHidden: boolean
|
||||
roots: ContainerModel | ContainerModel[] | null
|
||||
}
|
||||
|
||||
const GAP: number = 50;
|
||||
|
||||
const getDimensionsNodes = (root: ContainerModel): React.ReactNode[] => {
|
||||
const it = MakeIterator(root);
|
||||
const dimensions: React.ReactNode[] = [];
|
||||
for (const container of it) {
|
||||
// WARN: this might be dangerous later when using other units/rules
|
||||
const width = Number(container.properties.width);
|
||||
|
||||
const id = `dim-${container.properties.id}`;
|
||||
const xStart: number = container.properties.x;
|
||||
const xEnd = xStart + width;
|
||||
const y = -(GAP * (getDepth(container) + 1));
|
||||
const strokeWidth = 1;
|
||||
const text = width.toString();
|
||||
dimensions.push(
|
||||
<Dimension
|
||||
id={id}
|
||||
xStart={xStart}
|
||||
xEnd={xEnd}
|
||||
y={y}
|
||||
strokeWidth={strokeWidth}
|
||||
text={text}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return dimensions;
|
||||
};
|
||||
|
||||
/**
|
||||
* A layer containing all dimension
|
||||
*
|
||||
* @deprecated In order to avoid adding complexity
|
||||
* with computing the position in a group hierarchy,
|
||||
* use Dimension directly inside the Container,
|
||||
* Currently it is glitched as
|
||||
* it does not take parents into account,
|
||||
* and will not work correctly
|
||||
* @param props
|
||||
* @returns
|
||||
*/
|
||||
export const DimensionLayer: React.FC<IDimensionLayerProps> = (props: IDimensionLayerProps) => {
|
||||
let dimensions: React.ReactNode[] = [];
|
||||
if (Array.isArray(props.roots)) {
|
||||
props.roots.forEach(child => {
|
||||
dimensions.concat(getDimensionsNodes(child));
|
||||
});
|
||||
} else if (props.roots !== null) {
|
||||
dimensions = getDimensionsNodes(props.roots);
|
||||
}
|
||||
return (
|
||||
<g visibility={props.isHidden ? 'hidden' : 'visible'}>
|
||||
{ dimensions }
|
||||
</g>
|
||||
);
|
||||
};
|
|
@ -36,3 +36,5 @@ export const DEFAULT_MAINCONTAINER_PROPS: Properties = {
|
|||
fillOpacity: 0,
|
||||
stroke: 'black'
|
||||
};
|
||||
|
||||
export const NOTCHES_LENGTH = 4;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue