Skip to content

Commit 101280e

Browse files
ts updates
1 parent 19f5ec8 commit 101280e

File tree

3 files changed

+88
-32
lines changed

3 files changed

+88
-32
lines changed

src/utils/Interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface Prop {
1+
export interface PropInt {
22
id: number;
33
key: string;
44
value: string;

src/utils/componentReducer.util.ts

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
ComponentInt,
77
ApplicationStateInt,
88
ChildrenInt,
9-
ChildInt
9+
ChildInt,
10+
ComponentsInt,
11+
PropInt
1012
} from "./interfaces";
1113

1214
const initialComponentState: ComponentInt = {
@@ -226,7 +228,7 @@ export const deleteChild = (
226228

227229
// delete the CHILD from the copied array
228230
const indexToDelete = parentComponentCopy.childrenArray.findIndex(
229-
elem => elem.childId === childId
231+
(elem: ChildInt) => elem.childId === childId
230232
);
231233
if (indexToDelete < 0) {
232234
return window.alert("No such child component found");
@@ -255,8 +257,22 @@ export const deleteChild = (
255257
};
256258

257259
export const handleTransform = (
258-
state,
259-
{ componentId, childId, x, y, width, height }
260+
state: ApplicationStateInt,
261+
{
262+
componentId,
263+
childId,
264+
x,
265+
y,
266+
width,
267+
height
268+
}: {
269+
componentId: number;
270+
childId: number;
271+
x: number;
272+
y: number;
273+
width: number;
274+
height: number;
275+
}
260276
) => {
261277
if (childId === -1) {
262278
// the pseudochild has been transformed, its position is stored in the component
@@ -319,7 +335,7 @@ export const handleTransform = (
319335
focusCHild: newFocusChild
320336
};
321337

322-
const components = [
338+
const components: ComponentsInt = [
323339
...state.components.filter(comp => {
324340
if (comp.id !== componentId) return comp;
325341
}),
@@ -332,7 +348,10 @@ export const handleTransform = (
332348
};
333349
};
334350

335-
export const deleteComponent = (state, { componentId }) => {
351+
export const deleteComponent = (
352+
state: ApplicationStateInt,
353+
{ componentId }: { componentId: number }
354+
) => {
336355
if (componentId === 1) {
337356
return {
338357
...state
@@ -357,14 +376,16 @@ export const deleteComponent = (state, { componentId }) => {
357376
};
358377

359378
export const changeFocusComponent = (
360-
state,
361-
{ title = state.focusComponent.title }
379+
state: ApplicationStateInt,
380+
{ title = state.focusComponent.title }: { title: string }
362381
) => {
363382
/** ****************
364383
* if the prm TITLE is a blank Object it means REFRESH focusd Components.
365384
* sometimes we update state like adding Children/Props etc and we want those changes to be reflected in focus component
366385
************************************************* */
367-
const newFocusComp = state.components.find(comp => comp.title === title);
386+
const newFocusComp: ComponentInt = state.components.find(
387+
comp => comp.title === title
388+
);
368389
// set the "focus child" to the focus child of this particular component .
369390
// const newFocusChildId = newFocusComp.focusChildId;
370391

@@ -380,6 +401,7 @@ export const changeFocusComponent = (
380401
}
381402

382403
const result = getSelectable(newFocusComp, state.components);
404+
//const {selectableChildren, ancestors }: {selectableChildren: } = result;
383405

384406
return {
385407
...state,
@@ -390,11 +412,14 @@ export const changeFocusComponent = (
390412
};
391413
};
392414

393-
export const changeFocusChild = (state, { childId }) => {
415+
export const changeFocusChild = (
416+
state: ApplicationStateInt,
417+
{ childId }: { childId: number }
418+
) => {
394419
const focComp = state.components.find(
395420
comp => comp.title === state.focusComponent.title
396421
);
397-
let newFocusChild = focComp.childrenArray.find(
422+
let newFocusChild: ChildInt = focComp.childrenArray.find(
398423
child => child.childId === childId
399424
);
400425

@@ -409,8 +434,11 @@ export const changeFocusChild = (state, { childId }) => {
409434
width: focComp.position.width,
410435
height: focComp.position.height
411436
},
412-
draggable: true,
413-
color: focComp.color
437+
// draggable: true,
438+
color: focComp.color,
439+
childType: "",
440+
htmlElement: "",
441+
HTMLInfo: {}
414442
};
415443
}
416444

@@ -420,42 +448,66 @@ export const changeFocusChild = (state, { childId }) => {
420448
};
421449
};
422450

423-
export const changeComponentFocusChild = (state, { componentId, childId }) => {
424-
const component = state.components.find(comp => comp.id === componentId);
425-
const modifiedComponent = cloneDeep(component);
451+
export const changeComponentFocusChild = (
452+
state: ApplicationStateInt,
453+
{ componentId, childId }: { componentId: number; childId: number }
454+
) => {
455+
const component: ComponentInt = state.components.find(
456+
comp => comp.id === componentId
457+
);
458+
const modifiedComponent: any = cloneDeep(component);
426459
modifiedComponent.focusChildId = childId;
427-
const components = state.components.filter(comp => comp.id !== componentId);
460+
const components: ComponentsInt = state.components.filter(
461+
comp => comp.id !== componentId
462+
);
428463
return {
429464
...state,
430465
components: [modifiedComponent, ...components]
431466
};
432467
};
433468

434-
export const exportFilesSuccess = (state, { status, dir }) => ({
469+
export const exportFilesSuccess = (
470+
state: ApplicationStateInt,
471+
{ status, dir }: { status: boolean; dir: string }
472+
) => ({
435473
...state,
436474
successOpen: status,
437475
appDir: dir,
438476
loading: false
439477
});
440478

441-
export const exportFilesError = (state, { status, err }) => ({
479+
export const exportFilesError = (
480+
state: ApplicationStateInt,
481+
{ status, err }: { status: boolean; err: string }
482+
) => ({
442483
...state,
443484
errorOpen: status,
444485
appDir: err,
445486
loading: false
446487
});
447488

448-
export const handleClose = (state, status) => ({
489+
export const handleClose = (state: ApplicationStateInt, status: string) => ({
449490
...state,
450491
errorOpen: status,
451492
successOpen: status
452493
});
453494

454-
export const openExpansionPanel = (state, { component }) => ({
495+
export const openExpansionPanel = (
496+
state: ApplicationStateInt,
497+
{ component }: { component: ComponentInt }
498+
) => ({
455499
...state
456500
});
457501

458-
export const addProp = (state, { key, value = null, required, type }) => {
502+
export const addProp = (
503+
state: ApplicationStateInt,
504+
{
505+
key,
506+
value = null,
507+
required,
508+
type
509+
}: { key: string; value: string; required: boolean; type: string }
510+
) => {
459511
if (!state.focusComponent.id) {
460512
console.log("Add prop error. no focused component ");
461513
return state;
@@ -474,13 +526,13 @@ export const addProp = (state, { key, value = null, required, type }) => {
474526
};
475527
const newProps = [...selectedComponent.props, newProp];
476528

477-
const modifiedComponent = {
529+
const modifiedComponent: ComponentInt = {
478530
...selectedComponent,
479531
props: newProps,
480532
nextPropId: selectedComponent.nextPropId + 1
481533
};
482534

483-
const newComponents = state.components.filter(
535+
const newComponents: ComponentsInt = state.components.filter(
484536
comp => comp.id !== selectedComponent.id
485537
);
486538
newComponents.push(modifiedComponent);
@@ -491,18 +543,18 @@ export const addProp = (state, { key, value = null, required, type }) => {
491543
};
492544
};
493545

494-
export const deleteProp = (state, propId) => {
546+
export const deleteProp = (state: ApplicationStateInt, propId: number) => {
495547
if (!state.focusComponent.id) {
496548
console.log("Delete prop error. no focused component ");
497549
return state;
498550
}
499551

500-
const modifiedComponent = cloneDeep(
552+
const modifiedComponent: any = cloneDeep(
501553
state.components.find(comp => comp.id === state.focusComponent.id)
502554
);
503555

504556
const indexToDelete = modifiedComponent.props.findIndex(
505-
prop => prop.id === propId
557+
(prop: PropInt) => prop.id === propId
506558
);
507559
if (indexToDelete === -1) {
508560
console.log(

src/utils/getSelectable.util.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import { ComponentInt, ComponentsInt, ChildInt } from "./interfaces.ts";
22

3+
interface getSelectableInt {
4+
[key: string]: Array<number>;
5+
}
6+
37
function getSelectable(
48
newFocusComponent: ComponentInt,
59
components: ComponentsInt
610
) {
711
const focusComponentId = newFocusComponent.id;
812
const componentsToCheck = components
913
.map((comp: ComponentInt) => comp.id)
10-
.filter((id: Number) => id !== focusComponentId);
14+
.filter((id: number) => id !== focusComponentId);
1115
return findAncestors(components, [focusComponentId], componentsToCheck);
1216
}
1317

1418
function findAncestors(
1519
components: ComponentsInt,
1620
currentCompArr: ComponentsInt,
1721
componentsToCheck: ComponentsInt,
18-
ancestors: Array<Number> = []
19-
): Object {
22+
ancestors: Array<number> = []
23+
): getSelectableInt {
2024
if (!currentCompArr.length) {
2125
return {
22-
ancestors,
26+
ancestors: ancestors,
2327
selectableChildren: componentsToCheck
2428
};
2529
}

0 commit comments

Comments
 (0)