Implement more options + Add depth dimension (revert DimensionLayer) #37

Merged
Siklos merged 10 commits from dev.moreoptions into dev 2022-08-18 08:46:51 -04:00
13 changed files with 260 additions and 23 deletions

View file

@ -1,5 +1,6 @@
import { Dispatch, SetStateAction } from 'react'; import { Dispatch, SetStateAction } from 'react';
import { IHistoryState } from '../../Interfaces/IHistoryState'; import { IHistoryState } from '../../Interfaces/IHistoryState';
import { ENABLE_SHORTCUTS } from '../../utils/default';
export function onKeyDown( export function onKeyDown(
event: KeyboardEvent, event: KeyboardEvent,
@ -7,6 +8,10 @@ export function onKeyDown(
historyCurrentStep: number, historyCurrentStep: number,
setHistoryCurrentStep: Dispatch<SetStateAction<number>> setHistoryCurrentStep: Dispatch<SetStateAction<number>>
): void { ): void {
if (!ENABLE_SHORTCUTS) {
return;
}
event.preventDefault(); event.preventDefault();
if (event.isComposing || event.keyCode === 229) { if (event.isComposing || event.keyCode === 229) {
return; return;

View file

@ -14,6 +14,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'main', id: 'main',
parentId: null, parentId: null,
displayedText: 'main',
x: 0, x: 0,
y: 0, y: 0,
width: 2000, width: 2000,
@ -47,6 +48,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'main', id: 'main',
parentId: '', parentId: '',
displayedText: 'main',
x: 0, x: 0,
y: 0, y: 0,
width: 2000, width: 2000,
@ -106,6 +108,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'main', id: 'main',
parentId: '', parentId: '',
displayedText: 'main',
x: 0, x: 0,
y: 0, y: 0,
minWidth: 1, minWidth: 1,
@ -125,6 +128,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'child-1', id: 'child-1',
parentId: 'main', parentId: 'main',
displayedText: 'child-1',
x: 0, x: 0,
y: 0, y: 0,
minWidth: 1, minWidth: 1,
@ -145,6 +149,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'child-2', id: 'child-2',
parentId: 'main', parentId: 'main',
displayedText: 'child-2',
x: 0, x: 0,
y: 0, y: 0,
minWidth: 1, minWidth: 1,
@ -185,6 +190,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'main', id: 'main',
parentId: '', parentId: '',
displayedText: 'main',
x: 0, x: 0,
y: 0, y: 0,
minWidth: 1, minWidth: 1,
@ -203,6 +209,7 @@ describe.concurrent('Elements sidebar', () => {
properties: { properties: {
id: 'child-1', id: 'child-1',
parentId: 'main', parentId: 'main',
displayedText: 'child-1',
x: 0, x: 0,
y: 0, y: 0,
minWidth: 1, minWidth: 1,

View file

@ -83,7 +83,9 @@ export const ElementsSidebar: React.FC<IElementsSidebarProps> = (props: IElement
const container = containers[index]; const container = containers[index];
const depth: number = getDepth(container); const depth: number = getDepth(container);
const key = container.properties.id.toString(); const key = container.properties.id.toString();
const text = '|\t'.repeat(depth) + key; const text = container.properties.displayedText === key
? `${'|\t'.repeat(depth)} ${key}`
: `${'|\t'.repeat(depth)} ${container.properties.displayedText} (${key})`;
const selectedClass: string = props.SelectedContainer !== undefined && const selectedClass: string = props.SelectedContainer !== undefined &&
props.SelectedContainer !== null && props.SelectedContainer !== null &&
props.SelectedContainer.properties.id === container.properties.id props.SelectedContainer.properties.id === container.properties.id

View file

@ -52,6 +52,15 @@ const DynamicForm: React.FunctionComponent<IDynamicFormProps> = (props) => {
value={props.properties.parentId?.toString()} value={props.properties.parentId?.toString()}
isDisabled={true} isDisabled={true}
/> />
<InputGroup
labelText='Displayed text'
inputKey='displayedText'
labelClassName=''
inputClassName=''
type='string'
value={props.properties.displayedText?.toString()}
onChange={(event) => props.onChange('displayedText', event.target.value)}
/>
<InputGroup <InputGroup
labelText='x' labelText='x'
inputKey='x' inputKey='x'

View file

@ -23,6 +23,7 @@ describe.concurrent('Properties', () => {
const prop: IProperties = { const prop: IProperties = {
id: 'stuff', id: 'stuff',
parentId: 'parentId', parentId: 'parentId',
displayedText: 'stuff',
x: 1, x: 1,
y: 1, y: 1,
width: 1, width: 1,

View file

@ -52,6 +52,14 @@ const StaticForm: React.FunctionComponent<IStaticFormProps> = (props) => {
defaultValue={props.properties.parentId?.toString()} defaultValue={props.properties.parentId?.toString()}
isDisabled={true} isDisabled={true}
/> />
<InputGroup
labelText='Displayed text'
inputKey='displayedText'
labelClassName=''
inputClassName=''
type='string'
defaultValue={props.properties.displayedText?.toString()}
/>
<InputGroup <InputGroup
labelText='x' labelText='x'
inputKey='x' inputKey='x'

View file

@ -2,7 +2,7 @@ import * as React from 'react';
import { Interweave, Node } from 'interweave'; import { Interweave, Node } from 'interweave';
import { XPositionReference } from '../../../Enums/XPositionReference'; import { XPositionReference } from '../../../Enums/XPositionReference';
import { IContainerModel } from '../../../Interfaces/IContainerModel'; import { IContainerModel } from '../../../Interfaces/IContainerModel';
import { DIMENSION_MARGIN } from '../../../utils/default'; import { DIMENSION_MARGIN, SHOW_CHILDREN_DIMENSIONS, SHOW_PARENT_DIMENSION, SHOW_TEXT } from '../../../utils/default';
import { getDepth } from '../../../utils/itertools'; import { getDepth } from '../../../utils/itertools';
import { Dimension } from './Dimension'; import { Dimension } from './Dimension';
import IProperties from '../../../Interfaces/IProperties'; import IProperties from '../../../Interfaces/IProperties';
@ -54,7 +54,7 @@ export const Container: React.FC<IContainerProps> = (props: IContainerProps) =>
const text = (props.model.properties.width ?? 0).toString(); const text = (props.model.properties.width ?? 0).toString();
let dimensionChildren: JSX.Element | null = null; let dimensionChildren: JSX.Element | null = null;
if (props.model.children.length > 1) { if (props.model.children.length > 1 && SHOW_CHILDREN_DIMENSIONS) {
const { const {
childrenId, childrenId,
xChildrenStart, xChildrenStart,
@ -79,7 +79,8 @@ export const Container: React.FC<IContainerProps> = (props: IContainerProps) =>
transform={transform} transform={transform}
key={`container-${props.model.properties.id}`} key={`container-${props.model.properties.id}`}
> >
<Dimension { SHOW_PARENT_DIMENSION
? <Dimension
id={id} id={id}
xStart={xStart} xStart={xStart}
xEnd={xEnd} xEnd={xEnd}
@ -88,14 +89,18 @@ export const Container: React.FC<IContainerProps> = (props: IContainerProps) =>
strokeWidth={strokeWidth} strokeWidth={strokeWidth}
text={text} text={text}
/> />
: null
}
{ dimensionChildren } { dimensionChildren }
{ svg } { svg }
<text { SHOW_TEXT
? <text
x={xText} x={xText}
y={yText} y={yText}
> >
{props.model.properties.id} {props.model.properties.displayedText}
</text> </text>
: null }
{ containersElements } { containersElements }
</g> </g>
); );

View file

@ -0,0 +1,90 @@
import * as React from 'react';
import { ContainerModel } from '../../../Interfaces/IContainerModel';
import { DIMENSION_MARGIN } from '../../../utils/default';
import { getAbsolutePosition, MakeBFSIterator } from '../../../utils/itertools';
import { transformX } from './Container';
import { Dimension } from './Dimension';
interface IDimensionLayerProps {
roots: ContainerModel | ContainerModel[] | null
}
const getDimensionsNodes = (root: ContainerModel): React.ReactNode[] => {
const it = MakeBFSIterator(root);
const dimensions: React.ReactNode[] = [];
let currentDepth = 0;
let min = Infinity;
let max = -Infinity;
let lastY = 0;
for (const { container, depth } of it) {
if (currentDepth !== depth) {
AddNewDimension(currentDepth, min, max, lastY, dimensions);
currentDepth = depth;
min = Infinity;
max = -Infinity;
}
const absoluteX = getAbsolutePosition(container)[0];
const x = transformX(absoluteX, container.properties.width, container.properties.XPositionReference);
lastY = container.properties.y + container.properties.height;
if (x < min) {
min = x;
}
if (x > max) {
max = x;
}
}
AddNewDimension(currentDepth, min, max, lastY, dimensions);
return dimensions;
};
/**
* A layer containing all dimension
* @param props
* @returns
*/
export const DepthDimensionLayer: 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>
{ dimensions }
</g>
);
};
function AddNewDimension(currentDepth: number, min: number, max: number, lastY: number, dimensions: React.ReactNode[]): void {
const id = `dim-depth-${currentDepth}`;
const xStart = min;
const xEnd = max;
const y = lastY + (DIMENSION_MARGIN * (currentDepth + 1));
const strokeWidth = 1;
const width = xEnd - xStart;
const text = width.toString();
if (width === 0) {
return;
}
dimensions.push(
<Dimension
key={id}
id={id}
xStart={xStart}
yStart={y}
xEnd={xEnd}
yEnd={y}
strokeWidth={strokeWidth}
text={text} />
);
}

View file

@ -0,0 +1,57 @@
import * as React from 'react';
import { ContainerModel } from '../../../Interfaces/IContainerModel';
import { DIMENSION_MARGIN } from '../../../utils/default';
import { getAbsolutePosition, MakeBFSIterator } from '../../../utils/itertools';
import { Dimension } from './Dimension';
interface IDimensionLayerProps {
roots: ContainerModel | ContainerModel[] | null
}
const getDimensionsNodes = (root: ContainerModel): React.ReactNode[] => {
const it = MakeBFSIterator(root);
const dimensions: React.ReactNode[] = [];
for (const { container, depth } of it) {
const width = container.properties.width;
const id = `dim-${container.properties.id}`;
const xStart = getAbsolutePosition(container)[0];
const xEnd = xStart + width;
const y = (container.properties.y + container.properties.height) + (DIMENSION_MARGIN * (depth + 1));
const strokeWidth = 1;
const text = width.toString();
dimensions.push(
<Dimension
key={id}
id={id}
xStart={xStart}
yStart={y}
xEnd={xEnd}
yEnd={y}
strokeWidth={strokeWidth}
text={text}
/>
);
}
return dimensions;
};
/**
* A layer containing all dimension
* @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>
{ dimensions }
</g>
);
};

View file

@ -4,6 +4,9 @@ import { Container } from './Elements/Container';
import { ContainerModel } from '../../Interfaces/IContainerModel'; import { ContainerModel } from '../../Interfaces/IContainerModel';
import { Selector } from './Elements/Selector'; import { Selector } from './Elements/Selector';
import { BAR_WIDTH } from '../Bar/Bar'; import { BAR_WIDTH } from '../Bar/Bar';
import { DimensionLayer } from './Elements/DimensionLayer';
import { DepthDimensionLayer } from './Elements/DepthDimensionLayer';
import { SHOW_DIMENSIONS_PER_DEPTH } from '../../utils/default';
interface ISVGProps { interface ISVGProps {
width: number width: number
@ -74,6 +77,11 @@ export const SVG: React.FC<ISVGProps> = (props: ISVGProps) => {
<svg {...properties}> <svg {...properties}>
{ children } { children }
<Selector selected={props.selected} /> <Selector selected={props.selected} />
{
SHOW_DIMENSIONS_PER_DEPTH
? <DepthDimensionLayer roots={props.children}/>
: null
}
</svg> </svg>
</UncontrolledReactSVGPanZoom> </UncontrolledReactSVGPanZoom>
</div> </div>

View file

@ -11,6 +11,9 @@ export default interface IProperties {
/** id of the parent container (null when there is no parent) */ /** id of the parent container (null when there is no parent) */
parentId: string | null parentId: string | null
/** Text displayed in the container */
displayedText: string
/** horizontal offset */ /** horizontal offset */
x: number x: number

View file

@ -4,6 +4,23 @@ import { IConfiguration } from '../Interfaces/IConfiguration';
import { IContainerModel } from '../Interfaces/IContainerModel'; import { IContainerModel } from '../Interfaces/IContainerModel';
import IProperties from '../Interfaces/IProperties'; import IProperties from '../Interfaces/IProperties';
/// CONTAINRE DEFAULTS ///
export const SHOW_TEXT = true;
/// DIMENSIONS DEFAULTS ///
export const SHOW_PARENT_DIMENSION = true;
export const SHOW_CHILDREN_DIMENSIONS = false;
export const SHOW_DIMENSIONS_PER_DEPTH = true;
export const DIMENSION_MARGIN = 50;
export const NOTCHES_LENGTH = 4;
/// EDITOR DEFAULTS ///
export const ENABLE_SHORTCUTS = true;
export const MAX_HISTORY = 200;
export const DEFAULT_CONFIG: IConfiguration = { export const DEFAULT_CONFIG: IConfiguration = {
AvailableContainers: [ AvailableContainers: [
{ {
@ -31,6 +48,7 @@ export const DEFAULT_CONFIG: IConfiguration = {
export const DEFAULT_MAINCONTAINER_PROPS: IProperties = { export const DEFAULT_MAINCONTAINER_PROPS: IProperties = {
id: 'main', id: 'main',
parentId: 'null', parentId: 'null',
displayedText: 'main',
x: 0, x: 0,
y: 0, y: 0,
minWidth: 1, minWidth: 1,
@ -55,6 +73,7 @@ export const GetDefaultContainerProps = (
): IProperties => ({ ): IProperties => ({
id: `${type}-${typeCount}`, id: `${type}-${typeCount}`,
parentId: parent.properties.id, parentId: parent.properties.id,
displayedText: `${type}-${typeCount}`,
x, x,
y, y,
width: containerConfig.Width ?? containerConfig.MinWidth ?? parent.properties.width, width: containerConfig.Width ?? containerConfig.MinWidth ?? parent.properties.width,
@ -67,8 +86,3 @@ export const GetDefaultContainerProps = (
style: containerConfig.Style, style: containerConfig.Style,
userData: containerConfig.UserData userData: containerConfig.UserData
}); });
export const DIMENSION_MARGIN = 50;
export const NOTCHES_LENGTH = 4;
export const MAX_HISTORY = 200;

View file

@ -22,6 +22,34 @@ export function * MakeIterator(root: IContainerModel): Generator<IContainerModel
} }
} }
export interface ContainerAndDepth {
container: IContainerModel
depth: number
}
/**
* Returns a Generator iterating of over the children depth-first
*/
export function * MakeBFSIterator(root: IContainerModel): Generator<ContainerAndDepth, void, unknown> {
const queue: IContainerModel[] = [root];
let depth = 0;
while (queue.length > 0) {
let levelSize = queue.length;
while (levelSize-- !== 0) {
const container = queue.shift() as IContainerModel;
yield {
container,
depth
};
for (let i = container.children.length - 1; i >= 0; i--) {
const child = container.children[i];
queue.push(child);
}
}
depth++;
}
}
/** /**
* Returns the depth of the container * Returns the depth of the container
* @returns The depth of the container * @returns The depth of the container