@@ -13,110 +13,92 @@ import {
13
13
import { createHistory } from '../helperFunctions/createHistory' ;
14
14
15
15
//this is the default values for any component added to the app.
16
-
17
- const initialComponentState : ComponentInt = {
18
- id : 0 ,
19
- stateful : false ,
20
- classBased : false ,
21
- title : '' ,
22
- color : getColor ( ) ,
23
- props : [ ] ,
24
- nextPropId : 1 ,
25
- position : {
26
- x : 25 ,
27
- y : 25 ,
28
- width : 800 ,
29
- height : 550 ,
30
- } ,
31
- childrenArray : [ ] ,
32
- nextChildId : 1 ,
33
- focusChildId : 0 ,
34
- } ;
35
-
36
- export const addComponent = (
37
- state : ApplicationStateInt ,
38
- { title } : { title : string }
39
- ) => {
40
- // remove whitespace and digits, capitalize first char
41
- const strippedTitle = title
42
- . replace ( / [ a - z ] + / gi, word => word [ 0 ] . toUpperCase ( ) + word . slice ( 1 ) )
43
- . replace ( / [ - _ \s 0 - 9 \W ] + / gi, '' ) ;
44
-
45
- // duplicate component names not allowed
46
- if (
47
- state . components . find ( ( comp : ComponentInt ) => comp . title === strippedTitle )
48
- ) {
49
- window . alert (
50
- `A component with the name: "${ strippedTitle } " already exists.\n Please think of another name.`
51
- ) ;
52
- return {
53
- ...state ,
54
- } ;
55
- }
56
-
57
- // empty component name not allowed
58
- if ( strippedTitle === '' ) {
59
- return {
60
- ...state ,
61
- } ;
62
- }
63
-
64
- //chooses a color for the component from the random color generator
65
- let componentColor = getColor ( ) ;
66
- //Makes sure no two consecutive components have the same color
67
- const lastComponent = state . components . reduce ( ( acc , curr ) =>
68
- curr . id > acc . id ? curr : acc
69
- ) . color ;
70
- while ( componentColor === lastComponent ) {
71
- componentColor = getColor ( ) ;
72
- }
73
-
74
- //assigns the componentID to whatever is supposed to be next
75
- const componentId = state . nextId ;
76
-
77
- //creates a newcomponent and prepares it to be added to an array of components in the store
78
- const newComponent : ComponentInt = {
79
- ...initialComponentState ,
80
- title : strippedTitle ,
81
- id : componentId ,
82
- color : componentColor ,
83
- childrenArray : [ ] ,
84
- } ;
85
-
86
- const components = [ ...state . components , newComponent ] ;
87
-
88
- //increments both total components and the nextID
89
- const totalComponents = state . totalComponents + 1 ;
90
- const nextId = state . nextId + 1 ;
91
-
92
- //creates an array of component IDs that can be added as children to this newly created component.
93
- //the newly created component and the App component are never selectable.
94
- const selectableChildren = state . components
95
- . map ( ( comp : ComponentInt ) => comp . id )
96
- . filter ( ( id : number ) => id !== newComponent . id ) ;
97
-
98
- const ancestors : Array < number > = [ ] ;
99
-
100
- // reset focused child to null values so when focused component is assigned to the newly created component,
101
- //child from previously focused component doesn;t show up
102
- const newFocusChild = cloneDeep ( state . initialApplicationFocusChild ) ;
103
-
104
- const { history, historyIndex, future } = createHistory ( state ) ;
105
-
106
- return {
107
- ...state ,
108
- totalComponents,
109
- nextId,
110
- components,
111
- focusComponent : newComponent ,
112
- focusChild : newFocusChild ,
113
- ancestors,
114
- selectableChildren, // new component so everyone except yourself is available
115
- history,
116
- historyIndex,
117
- future,
118
- } ;
119
- } ;
16
+ import { initialComponentState } from '../reducers/initialState' ;
17
+
18
+ // export const addComponent = (
19
+ // state: ApplicationStateInt,
20
+ // { title }: { title: string }
21
+ // ) => {
22
+ // // remove whitespace and digits, capitalize first char
23
+ // const strippedTitle = title
24
+ // .replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
25
+ // .replace(/[-_\s0-9\W]+/gi, '');
26
+
27
+ // // duplicate component names not allowed
28
+ // if (
29
+ // state.components.find((comp: ComponentInt) => comp.title === strippedTitle)
30
+ // ) {
31
+ // window.alert(
32
+ // `A component with the name: "${strippedTitle}" already exists.\n Please think of another name.`
33
+ // );
34
+ // return {
35
+ // ...state,
36
+ // };
37
+ // }
38
+
39
+ // // empty component name not allowed
40
+ // if (strippedTitle === '') {
41
+ // return {
42
+ // ...state,
43
+ // };
44
+ // }
45
+
46
+ // //chooses a color for the component from the random color generator
47
+ // let componentColor = getColor();
48
+ // //Makes sure no two consecutive components have the same color
49
+ // const lastComponent = state.components.reduce((acc, curr) =>
50
+ // curr.id > acc.id ? curr : acc
51
+ // ).color;
52
+ // while (componentColor === lastComponent) {
53
+ // componentColor = getColor();
54
+ // }
55
+
56
+ // //assigns the componentID to whatever is supposed to be next
57
+ // const componentId = state.nextId;
58
+
59
+ // //creates a newcomponent and prepares it to be added to an array of components in the store
60
+ // const newComponent: ComponentInt = {
61
+ // ...initialComponentState,
62
+ // title: strippedTitle,
63
+ // id: componentId,
64
+ // color: componentColor,
65
+ // childrenArray: [],
66
+ // };
67
+
68
+ // const components = [...state.components, newComponent];
69
+
70
+ // //increments both total components and the nextID
71
+ // const totalComponents = state.totalComponents + 1;
72
+ // const nextId = state.nextId + 1;
73
+
74
+ // //creates an array of component IDs that can be added as children to this newly created component.
75
+ // //the newly created component and the App component are never selectable.
76
+ // const selectableChildren = state.components
77
+ // .map((comp: ComponentInt) => comp.id)
78
+ // .filter((id: number) => id !== newComponent.id);
79
+
80
+ // const ancestors: Array<number> = [];
81
+
82
+ // // reset focused child to null values so when focused component is assigned to the newly created component,
83
+ // //child from previously focused component doesn;t show up
84
+ // const newFocusChild = cloneDeep(state.initialApplicationFocusChild);
85
+
86
+ // const { history, historyIndex, future } = createHistory(state);
87
+
88
+ // return {
89
+ // ...state,
90
+ // totalComponents,
91
+ // nextId,
92
+ // components,
93
+ // focusComponent: newComponent,
94
+ // focusChild: newFocusChild,
95
+ // ancestors,
96
+ // selectableChildren, // new component so everyone except yourself is available
97
+ // history,
98
+ // historyIndex,
99
+ // future,
100
+ // };
101
+ // };
120
102
121
103
//reducer function to add component or HTML child to currently focused component
122
104
export const addChild = (
0 commit comments