Skip to content

Commit 55dc8d2

Browse files
committed
made find parent function in mutations
1 parent 7ee5d3a commit 55dc8d2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/store/mutations.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const cloneDeep = require('lodash.clonedeep')
88
import localforage from 'localforage'
99

1010
//we have to do a search because undo/redo saves payloads as deep clones so passing a memory ref would be detrimental
11+
// This will find you the actual object by ID
1112
const breadthFirstSearch = (array,id) => {
1213
let queue = [...array.filter(el => typeof el === 'object')];
1314
while(queue.length){
@@ -24,6 +25,23 @@ const breadthFirstSearch = (array,id) => {
2425
console.log("We shouldn't be ever getting here, how did you even search an id that didn't exist?")
2526
}
2627

28+
//this would find you the parent of a given id
29+
const breadthFirstSearchParent = (array,id) => {
30+
let queue = [...array.filter(el => typeof el === 'object')];
31+
while(queue.length){
32+
let evaluated = queue.shift()
33+
for(let i=0; i<evaluated.children.length;i++){
34+
if(evaluated.children[i] === id){
35+
return evaluated
36+
}
37+
}
38+
if (evaluated.children.length){
39+
queue.push(...evaluated.children)
40+
}
41+
}
42+
console.log("We shouldn't be ever getting here, how did you even search an id that didn't exist?")
43+
}
44+
2745
const mutations = {
2846
// pushs new component to componentMap
2947
[types.ADD_COMPONENT_TO_COMPONENT_MAP]: (state, payload) => {

0 commit comments

Comments
 (0)