Skip to content

Commit 2cf4149

Browse files
Merge pull request #24 from oslabs-beta/keyla/childInsideHtmlElements
Keyla/child inside html elements
2 parents c61df90 + 5b009d3 commit 2cf4149

File tree

6 files changed

+106
-12
lines changed

6 files changed

+106
-12
lines changed

src/components/left-sidebar/ComponentTab/Icons.vue

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ Description:
1414
<br />
1515
<span>{{ elementName }}</span>
1616
</button>
17+
18+
<button
19+
@click.prevent="changeState(elementName)"
20+
v-for="(elementName, idx) in childrenComp"
21+
:key="idx + Date.now()"
22+
>
23+
<span class="badge"> {{ elementStorage[elementName] }}</span>
24+
<br />
25+
<i :class="childIcon"></i>
26+
<br />
27+
<span>{{ elementName }}</span>
28+
</button>
29+
30+
1731
</section>
1832
</template>
1933

@@ -22,7 +36,10 @@ import { mapState } from "vuex";
2236
2337
export default {
2438
data() {
25-
return {};
39+
return {
40+
//to give the child componenets of the active components icons
41+
childIcon: ["fa-solid fa-code"]
42+
};
2643
},
2744
name: "Icons",
2845
computed: {
@@ -78,6 +95,14 @@ export default {
7895
}
7996
return computedElementalStorage;
8097
},
98+
childrenComp: function () {
99+
let childrenAvailable = [];
100+
101+
if(this.activeComponent) {
102+
childrenAvailable = this.componentMap[this.activeComponent].children
103+
}
104+
return childrenAvailable;
105+
},
81106
},
82107
methods: {
83108
// Logic to decide where to place selected html element

src/components/right-sidebar/CodeSnippet.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ export default {
8585
else return `<template>\n <div>\n${templateTagStr} </div>\n</template>`;
8686
},
8787
// Creates <template> boilerplate
88-
writeTemplateTag(componentName) {
88+
writeTemplateTag(componentName, activeComponent) {
8989
// console.log(this.activeComponentObj)
90-
// create reference object
9190
const htmlElementMap = {
9291
div: ["<div", "</div>"],
9392
button: ["<button", "</button>"],
@@ -110,6 +109,11 @@ export default {
110109
h5: ["<h5", "</h5>"],
111110
h6: ["<h6", "</h6>"],
112111
};
112+
//add childComponents of the activeCompnent to the htmlElementMap
113+
const childComponents = this.componentMap[this.activeComponent].children;
114+
childComponents.forEach(child => {
115+
htmlElementMap[child]=[`<${child}`, ""] //single
116+
})
113117
function writeNested(childrenArray, indent) {
114118
if (!childrenArray.length) {
115119
return "";
@@ -132,7 +136,7 @@ export default {
132136
133137
}
134138
}
135-
if (child.text === "img" || child.text === "input" || child.text === "link") {
139+
if (child.text === "img" || child.text === "input" || child.text === "link" || childComponents.includes(child.text)) {
136140
nestedString += "/>";
137141
} else { nestedString += ">"; }
138142
@@ -163,6 +167,11 @@ export default {
163167
if (el.class !== "") {
164168
outputStr += " " + "class = " + `"${el.class}"`;
165169
}
170+
// add an extra slash at the end for child Components and single tags
171+
if(childComponents.includes(el.text) || el.text === "img" || el.text === "input" || el.text === "link"){
172+
outputStr += "/"
173+
}
174+
166175
if (el.binding !== "") {
167176
outputStr += ` v-model = "${el.binding}"`
168177
}

src/components/right-sidebar/HTMLQueue.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,14 @@ export default {
137137
newTitle += ` > ${el}`
138138
})
139139
return newTitle;
140+
},
141+
moreExceptions: function () {
142+
let childComponent = [];
143+
if(this.activeComponent) {
144+
childComponent = this.componentMap[this.activeComponent].children;
145+
}
146+
return childComponent;
140147
}
141-
142148
},
143149
methods: {
144150
...mapActions(['setActiveHTML', 'setActiveLayer', 'upOneLayer', 'setSelectedIdDrag', 'setIdDrag', 'setSelectedIdDrop', 'setIdDrop', 'dragDropSortHtmlElements', 'dragDropSortSelectedHtmlElements', 'openAttributeModal', 'addActiveComponentClass', 'addBindingText']),

src/store/actions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ const actions = {
269269
},
270270

271271
// Actions dispatched from left hand panel////////////////////////////////////////
272-
273272
[types.addToComponentElementList]: ({ commit }, payload) => {
274273
// adds element to the HTMLQueue
275274
commit(types.ADD_TO_COMPONENT_HTML_LIST, payload);
@@ -286,7 +285,6 @@ const actions = {
286285
// end of left hand panel/////////////////////////
287286

288287
// HTML Element related actions ////////////////////
289-
290288
[types.addNestedHTML]: ({ commit }, payload) => {
291289
commit(types.ADD_NESTED_HTML, payload);
292290
},
@@ -324,7 +322,10 @@ const actions = {
324322
[types.upOneLayer]: ({ commit }, payload) => {
325323
commit(types.UP_ONE_LAYER, payload);
326324
},
327-
//FOR MUTATING HTML WITH DRAG AND DROP
325+
326+
// end of HTML segment ////////////////////////////////////////////////
327+
328+
// Drag-and-drop ///////////////////////////////////////
328329
[types.setIdDrag]: ({ commit }, payload) => {
329330
commit(types.SET_ID_DRAG, payload)
330331
},
@@ -349,10 +350,9 @@ const actions = {
349350
commit(types.DRAG_DROP_SORT_SELECTED_HTML_ELEMENTS)
350351
},
351352

352-
// end of HTML segment ////////////////////////////////////////////////
353+
// end of Drag-and-drop /////////////////////////////////
353354

354355
// Loading ///////////////////////////////////////////////////////
355-
356356
[types.openProject]: ({ commit }, payload) => {
357357
commit(types.REMOVE_ALL_STATE_PROPS_ACTIONS)
358358
commit(types.SET_ACTIVE_ROUTE, "HomeView");

src/store/mutations.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,34 @@ const mutations = {
330330
}
331331
}
332332
}
333+
//update the component name in the htmlList of all components if it is a child component
334+
for (const item of Object.values(state.componentMap)) {
335+
if (item.htmlList) {
336+
const newArray = [...item.htmlList];
337+
338+
const changeAllChildComponents = (array, name) => {
339+
const queue = [...array.filter(el => typeof el === 'object')];
340+
while(queue.length) {
341+
const evaluate = queue.shift();
342+
if(evaluate.text === name) {
343+
evaluate.text = payload; 
344+
}
345+
for(let i = 0; i < evaluate.children.length; i++) {
346+
if (evaluate.children[i].text === name) {
347+
evaluate.children[i].text = payload;
348+
}
349+
if (evaluate.children.length) {
350+
queue.push(...evaluate.children)
351+
}
352+
}
353+
}
354+
}
355+
356+
changeAllChildComponents(newArray, temp)
357+
item.htmlList = newArray
358+
}
359+
}
360+
333361
},
334362

335363
// *** HTML ELEMENTS *** //////////////////////////////////////////////
@@ -759,7 +787,32 @@ const mutations = {
759787
// state.componentMap[state.activeComponent].htmlList = newHTMLList;
760788
// const newMap = { ...state.componentMap };
761789
// state.componentMap = { ...newMap };
790+
791+
//delete the instances of the Child Component in the activeComponent's htmlList
792+
const componentName = state.activeComponent;
793+
const htmlList = state.componentMap[componentName].htmlList.slice(0);
794+
795+
// splice out child componenets even if nested
796+
function deleteChildFromHtmlList(array, payload) {
797+
for(let i = array.length; i--;) {
798+
799+
if(array[i].children.length) {
800+
deleteChildFromHtmlList(array[i].children, payload)
801+
}
802+
if(array[i].text === payload) {
803+
array.splice(i, 1)
804+
}
805+
806+
}
807+
}
808+
deleteChildFromHtmlList(htmlList, payload);
809+
810+
//updates the htmlList with the child components deleted
811+
state.componentMap[componentName].htmlList = htmlList;
812+
813+
//delete the parent because the payload is no longer a child to the acitive component
762814
delete state.componentMap[payload].parent[state.activeComponent];
815+
763816
// add block
764817
} else {
765818
const child = temp;

src/store/types.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const DELETE_ACTIVE_COMPONENT_CLASS = 'DELETE_ACTIVE_COMPONENT_CLASS'
7777
//
7878
export const REMOVE_ALL_STATE_PROPS_ACTIONS = 'REMOVE_ALL_STATE_PROPS_ACTIONS'
7979
export const UPDATE_PASTE_TIMER = 'UPDATE_PASTE_TIMER'
80-
80+
//Drag and Drop
8181
export const SET_ID_DRAG = 'SET_ID_DRAG'
8282
export const SET_ID_DROP = 'SET_ID_DROP'
8383
export const SET_SELECTED_ID_DRAG = 'SET_SELECTED_ID_DRAG'
@@ -163,14 +163,15 @@ export const updateStartingSize = 'updateStartingSize'
163163
export const deleteUserActions = 'deleteUserActions'
164164
export const deleteUserState = 'deleteUserState'
165165
export const toggleTutorial = 'toggleTutorial'
166-
166+
//Drag and Drop
167167
export const setIdDrag = 'setIdDrag'
168168
export const setIdDrop = 'setIdDrop'
169169
export const setSelectedIdDrag = 'setSelectedIdDrag'
170170
export const setSelectedIdDrop = 'setSelectedIdDrop'
171171
export const dragDropSortHtmlElements = 'dragDropSortHtmlElements'
172172
export const dragDropSortSelectedHtmlElements = 'dragDropSortSelectedHtmlElements'
173173

174+
174175
// inactive mutations
175176
// export const SET_STATE = 'SET_STATE'
176177
// export const DELETE_COMPONENT = 'DELETE_COMPONENT'

0 commit comments

Comments
 (0)