Merged PR 205: Add new AddingBehavior ReplaceParent + Allow AddingBehavior override by response + fix double key in context menu + Rename node-http.js to http.js

This commit is contained in:
Eric Nguyen 2022-10-03 14:48:45 +00:00
parent 34c00d267c
commit 38a6919192
7 changed files with 151 additions and 379 deletions

View file

@ -39,7 +39,7 @@ export function GetAction(
Container: container, Container: container,
PreviousContainer: prev, PreviousContainer: prev,
NextContainer: next, NextContainer: next,
Action: action.Action, Action: action,
ApplicationState: currentState ApplicationState: currentState
}; };
/* eslint-enable */ /* eslint-enable */
@ -59,7 +59,7 @@ export function GetAction(
}; };
} }
function GetPreviousAndNextSiblings(container: IContainerModel): { prev: IContainerModel | undefined; next: IContainerModel | undefined; } { function GetPreviousAndNextSiblings(container: IContainerModel): { prev: IContainerModel | undefined, next: IContainerModel | undefined } {
let prev; let prev;
let next; let next;
if (container.parent !== undefined && if (container.parent !== undefined &&
@ -85,7 +85,8 @@ function HandleSetContainerList(
historyCurrentStep: number, historyCurrentStep: number,
setNewHistory: (newHistory: IHistoryState[]) => void setNewHistory: (newHistory: IHistoryState[]) => void
): void { ): void {
switch (action.AddingBehavior) { const addingBehavior = response.AddingBehavior ?? action.AddingBehavior;
switch (addingBehavior) {
case AddMethod.Append: case AddMethod.Append:
setNewHistory( setNewHistory(
AddContainers( AddContainers(
@ -98,7 +99,7 @@ function HandleSetContainerList(
)); ));
break; break;
case AddMethod.Insert: case AddMethod.Insert:
break; throw new Error('Not yet supported');
case AddMethod.Replace: case AddMethod.Replace:
setNewHistory( setNewHistory(
HandleReplace( HandleReplace(
@ -110,6 +111,25 @@ function HandleSetContainerList(
) )
); );
break; break;
case AddMethod.ReplaceParent:
if (selectedContainer.parent === undefined || selectedContainer.parent === null) {
Swal.fire({
title: 'Error',
text: 'The selected container has not parent to replace',
icon: 'error'
});
return;
}
setNewHistory(
HandleReplace(
selectedContainer.parent,
response,
configuration,
history,
historyCurrentStep
)
);
break;
} }
} }

View file

@ -88,7 +88,7 @@ export function Menu(props: IMenuProps): JSX.Element {
actions.forEach((action, index) => { actions.forEach((action, index) => {
children.push(<MenuItem children.push(<MenuItem
key={`contextmenu-item-${count}`} key={`contextmenu-item-${count}-${index}`}
className="contextmenu-item" className="contextmenu-item"
text={action.text} text={action.text}
onClick={() => action.action(target)} />); onClick={() => action.action(target)} />);

View file

@ -8,5 +8,6 @@
export enum AddMethod { export enum AddMethod {
Append, Append,
Insert, Insert,
Replace Replace,
ReplaceParent
} }

View file

@ -1,10 +1,11 @@
import { IAction } from './IAction';
import { IContainerModel } from './IContainerModel'; import { IContainerModel } from './IContainerModel';
import { IHistoryState } from './IHistoryState'; import { IHistoryState } from './IHistoryState';
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
export interface ISetContainerListRequest { export interface ISetContainerListRequest {
/** Name of the action declared in the API */ /** Name of the action declared in the API */
Action: string Action: IAction
/** Selected container */ /** Selected container */
Container: IContainerModel Container: IContainerModel

View file

@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
import { AddMethod } from '../Enums/AddMethod';
import { IAvailableContainer } from './IAvailableContainer'; import { IAvailableContainer } from './IAvailableContainer';
export interface ISetContainerListResponse { export interface ISetContainerListResponse {
Containers: IAvailableContainer[] Containers: IAvailableContainer[]
AddingBehavior?: AddMethod
} }

View file

@ -1,30 +1,42 @@
// http.js import http from 'http';
import { serve } from 'bun'; const host = 'localhost';
const port = 5000;
serve({ const requestListener = async (request, response) => {
port: 5000, response.setHeader('Access-Control-Allow-Origin', '*');
async fetch(request) { response.setHeader('Access-Control-Allow-Headers', '*');
console.log(`${request.method}: ${request.url}`); response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
if (request.method === 'POST') { if (request.method === 'POST') {
const url = new URL(request.url); response.setHeader('Content-Type', 'application/json');
const url = request.url;
let json; let json;
if (url.pathname === '/GetSVGLayoutConfiguration') { if (url === '/SetContainerList') {
json = GetSVGLayoutConfiguration(); const buffers = [];
} else if (url.pathname === '/SetContainerList') { for await (const chunk of request) {
const bodyParsed = await request.json(); buffers.push(chunk);
}
const data = Buffer.concat(buffers).toString();
const bodyParsed = JSON.parse(data);
console.log(bodyParsed); console.log(bodyParsed);
switch (bodyParsed.Action) { switch (bodyParsed.Action.Action) {
case 'FillHoleWithChassis': case 'FillHoleWithChassis':
json = FillHoleWithChassis(bodyParsed); json = FillHoleWithChassis(bodyParsed);
break; break;
case 'SplitRemplissage': case 'SplitRemplissage':
json = SplitRemplissage(bodyParsed); json = SplitRemplissage(bodyParsed);
break; break;
case 'SplitRemplissageParent':
json = SplitRemplissage(bodyParsed);
default: default:
break; break;
} }
} else if (url.pathname === '/GetFeedback') { } else if (url === '/GetFeedback') {
const bodyParsed = await request.json(); const buffers = [];
for await (const chunk of request) {
buffers.push(chunk);
}
const data = Buffer.concat(buffers).toString();
const bodyParsed = JSON.parse(data);
console.log(bodyParsed); console.log(bodyParsed);
json = { json = {
messages: [ messages: [
@ -35,28 +47,23 @@ serve({
] ]
} }
} else { } else {
// TODO: Return 404 rather than this
json = GetSVGLayoutConfiguration(); json = GetSVGLayoutConfiguration();
} }
return new Response(JSON.stringify(json), { response.writeHead(200);
status: 200, return response.end(JSON.stringify(json));
headers: { } else if (request.method === 'OPTIONS') {
'Content-Type': 'application/json', response.writeHead(200);
'Access-Control-Allow-Origin': '*', return response.end();
'Access-Control-Allow-Headers': '*'
}
});
} }
return new Response('Welcome to Bun!', { response.setHeader('Content-Type', 'text/plain');
status: 200, response.writeHead(200);
headers: { return response.end('Hello world');
'Content-Type': 'text/plain', };
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*' const server = http.createServer(requestListener);
} server.listen(port, host, () => {
}); console.log(`Server is running on http://${host}:${port}`);
}
}); });
const GetSVGLayoutConfiguration = () => { const GetSVGLayoutConfiguration = () => {
@ -64,11 +71,8 @@ const GetSVGLayoutConfiguration = () => {
AvailableContainers: [ AvailableContainers: [
{ {
Type: 'Chassis', Type: 'Chassis',
DisplayedText: 'Chassis?', DisplayedText: 'Chassis horizontal',
Whitelist: ["Trou"], MaxHeight: 200,
MaxWidth: 500,
MinWidth: 200,
Width: 200,
DefaultChildType: 'Trou', DefaultChildType: 'Trou',
Style: { Style: {
fillOpacity: 1, fillOpacity: 1,
@ -76,8 +80,24 @@ const GetSVGLayoutConfiguration = () => {
stroke: 'red', stroke: 'red',
fill: '#d3c9b7', fill: '#d3c9b7',
}, },
ShowSelfDimensions: true, ShowSelfDimensions: [0, 2],
IsDimensionBorrower: true, ShowDimensionWithMarks: [1, 3],
Category: "Stuff"
},
{
Type: 'ChassisVertical',
Orientation: 1,
DisplayedText: 'Chassis vertical',
MaxWidth: 200,
DefaultChildType: 'Trou',
Style: {
fillOpacity: 1,
strokeWidth: 2,
stroke: 'red',
fill: '#d3c9b7',
},
ShowSelfDimensions: [0, 2],
ShowDimensionWithMarks: [1, 3],
Category: "Stuff" Category: "Stuff"
}, },
{ {
@ -121,6 +141,19 @@ const GetSVGLayoutConfiguration = () => {
Url: "" Url: ""
}, },
AddingBehavior: 2 AddingBehavior: 2
},
{
Id: "SplitRemplissageParent",
Action: "SplitRemplissageParent",
Label: "Diviser le parent",
Description: "Diviser le remplissage en insérant un montant",
CustomLogo: {
Base64Image: null,
Name: 'Image1',
Svg: null,
Url: ""
},
AddingBehavior: 3
} }
], ],
Style: { Style: {
@ -138,8 +171,8 @@ const GetSVGLayoutConfiguration = () => {
{ {
Type: 'Montant', Type: 'Montant',
Width: 10, Width: 10,
PositionReference: 1, PositionReference: 4,
MarkPositionToDimensionBorrower: true, MarkPositionToDimensionBorrower: [0],
Style: { Style: {
fillOpacity: 0, fillOpacity: 0,
strokeWidth: 2, strokeWidth: 2,
@ -147,11 +180,24 @@ const GetSVGLayoutConfiguration = () => {
fill: '#713f12', fill: '#713f12',
} }
}, },
{
Type: 'Traverse',
Height: 10,
PositionReference: 4,
Orientation: 1,
MarkPositionToDimensionBorrower: [1],
Style: {
fillOpacity: 0,
strokeWidth: 2,
stroke: '#6517fa',
fill: '#6517fa',
}
},
{ {
Type: 'Dilatation', Type: 'Dilatation',
Width: 4, Width: 4,
PositionReference: 1, PositionReference: 1,
MarkPositionToDimensionBorrower: true, MarkPositionToDimensionBorrower: [0],
Style: { Style: {
fillOpacity: 0, fillOpacity: 0,
strokeWidth: 2, strokeWidth: 2,
@ -222,7 +268,13 @@ const GetSVGLayoutConfiguration = () => {
MainContainer: { MainContainer: {
Type: 'main', Type: 'main',
Width: 800, Width: 800,
Height: 200 Height: 800,
Orientation: 1,
Style: {
stroke: 'black',
strokeWidth: 2,
fillOpacity: 0
}
} }
} }
}; };
@ -265,3 +317,5 @@ const SplitRemplissage = (request) => {
Containers: lstModels Containers: lstModels
}; };
}; };

View file

@ -1,306 +0,0 @@
import http from 'http';
const host = 'localhost';
const port = 5000;
const requestListener = async (request, response) => {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Headers', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
if (request.method === 'POST') {
response.setHeader('Content-Type', 'application/json');
const url = request.url;
let json;
if (url === '/SetContainerList') {
const buffers = [];
for await (const chunk of request) {
buffers.push(chunk);
}
const data = Buffer.concat(buffers).toString();
const bodyParsed = JSON.parse(data);
console.log(bodyParsed);
switch (bodyParsed.Action) {
case 'FillHoleWithChassis':
json = FillHoleWithChassis(bodyParsed);
break;
case 'SplitRemplissage':
json = SplitRemplissage(bodyParsed);
break;
default:
break;
}
} else if (url === '/GetFeedback') {
const buffers = [];
for await (const chunk of request) {
buffers.push(chunk);
}
const data = Buffer.concat(buffers).toString();
const bodyParsed = JSON.parse(data);
console.log(bodyParsed);
json = {
messages: [
{
text: `${new Date()}`,
type: 3
}
]
}
} else {
json = GetSVGLayoutConfiguration();
}
response.writeHead(200);
return response.end(JSON.stringify(json));
} else if (request.method === 'OPTIONS') {
response.writeHead(200);
return response.end();
}
response.setHeader('Content-Type', 'text/plain');
response.writeHead(200);
return response.end('Hello world');
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
const GetSVGLayoutConfiguration = () => {
return {
AvailableContainers: [
{
Type: 'Chassis',
DisplayedText: 'Chassis horizontal',
MaxHeight: 200,
DefaultChildType: 'Trou',
Style: {
fillOpacity: 1,
strokeWidth: 2,
stroke: 'red',
fill: '#d3c9b7',
},
ShowSelfDimensions: [0, 2],
ShowDimensionWithMarks: [1, 3],
Category: "Stuff"
},
{
Type: 'ChassisVertical',
Orientation: 1,
DisplayedText: 'Chassis vertical',
MaxWidth: 200,
DefaultChildType: 'Trou',
Style: {
fillOpacity: 1,
strokeWidth: 2,
stroke: 'red',
fill: '#d3c9b7',
},
ShowSelfDimensions: [0, 2],
ShowDimensionWithMarks: [1, 3],
Category: "Stuff"
},
{
Type: 'Trou',
Blacklist: ["Chassis"],
Margin: {
left: 10,
bottom: 10,
top: 10,
right: 10,
},
DefaultChildType: 'Remplissage',
Style: {
fillOpacity: 1,
strokeWidth: 2,
stroke: 'green',
fill: 'white'
},
Category: "Stuff"
},
{
Type: 'Remplissage',
Category: "Other stuff",
CustomSVG: `
<rect width="{width}" height="{height}" style="{style}"></rect>
<rect width="{width}" height="{height}" stroke="black" fill-opacity="0"></rect>
<line x1="0" y1="0" x2="{width}" y2="{height}" stroke="black" style='{{ "transform":"scaleY(0.5)"}}'></line>
<line x1="{width}" y1="0" x2="0" y2="{height}" stroke="black" style='{userData.styleLine}'></line>
`
,
Actions: [
{
Id: "SplitRemplissage",
Action: "SplitRemplissage",
Label: "Diviser le remplissage",
Description: "Diviser le remplissage en insérant un montant",
CustomLogo: {
Base64Image: null,
Name: 'Image1',
Svg: null,
Url: ""
},
AddingBehavior: 2
}
],
Style: {
fillOpacity: 1,
strokeWidth: 1,
fill: '#bfdbfe'
},
UserData: {
styleLine: {
transform: "scaleY(0.5) translateY(100%)",
transformBox: "fill-box"
}
}
},
{
Type: 'Montant',
Width: 10,
PositionReference: 4,
MarkPositionToDimensionBorrower: [0],
Style: {
fillOpacity: 0,
strokeWidth: 2,
stroke: '#713f12',
fill: '#713f12',
}
},
{
Type: 'Traverse',
Height: 10,
PositionReference: 4,
Orientation: 1,
MarkPositionToDimensionBorrower: [1],
Style: {
fillOpacity: 0,
strokeWidth: 2,
stroke: '#6517fa',
fill: '#6517fa',
}
},
{
Type: 'Dilatation',
Width: 4,
PositionReference: 1,
MarkPositionToDimensionBorrower: [0],
Style: {
fillOpacity: 0,
strokeWidth: 2,
stroke: '#713f12',
fill: '#713f12',
}
},
{
Type: '200',
MaxWidth: 500,
MinWidth: 200,
Style: {
fillOpacity: 1,
strokeWidth: 2,
stroke: 'blue',
fill: 'blue',
}
},
{
Type: '400',
MaxWidth: 500,
MinWidth: 400,
Style: {
fillOpacity: 1,
strokeWidth: 2,
stroke: 'red',
fill: 'red',
}
}
],
AvailableSymbols: [
{
Width: 32,
Height: 32,
Image: {
Base64Image: null,
Name: 'Image1',
Svg: null,
Url: 'https://www.manutan.fr/img/S/GRP/ST/AIG3930272.jpg'
},
Name: 'Poteau structure',
PositionReference: 1
},
{
Width: 32,
Height: 32,
Image: {
Base64Image: null,
Name: 'Image2',
Svg: null,
Url: 'https://e7.pngegg.com/pngimages/647/127/png-clipart-svg-working-group-information-world-wide-web-internet-structure.png'
},
Name: 'Joint de structure',
PositionReference: 0
}
],
Categories: [
{
Type: "Stuff",
DisplayedText: "Stuff made here"
},
{
Type: "Other stuff",
DisplayedText: "Stuff not made here"
}
],
Patterns: [],
MainContainer: {
Type: 'main',
Width: 800,
Height: 800,
Orientation: 1,
Style: {
stroke: 'black',
strokeWidth: 2,
fillOpacity: 0
}
}
}
};
const FillHoleWithChassis = (request) => {
const maxWidthChassis = 3000;
const nbChassis = Math.ceil(request.ContainerAction.Width / maxWidthChassis);
const lstModels = [];
for (let i = 0; i <= nbChassis; i++) {
if (i === 1 && request.ContainerAction.ExistOnBefore) {
lstModels.push({ Type: 'Dilatation' });
}
lstModels.push({ Type: 'Chassis' });
if (i < nbChassis) {
lstModels.push({ Type: 'Dilatation' });
}
if (i === nbChassis && request.ContainerAction.ExistOnAfter) {
lstModels.push({ Type: 'Dilatation' });
}
}
return {
Containers: lstModels
};
};
const SplitRemplissage = (request) => {
const lstModels = [
{
Type: 'Remplissage'
},
{
Type: 'Montant'
},
{
Type: 'Remplissage'
},
];
return {
Containers: lstModels
};
};