Skip to content

Commit 387e180

Browse files
committed
ImportComponent now generates an importObj that is populated with relevant data from the imported .vue file.
Co-authored-by: Kerolos Nesem <[email protected]>
1 parent d3d2ea8 commit 387e180

File tree

1 file changed

+157
-29
lines changed

1 file changed

+157
-29
lines changed

src/components/home_sidebar_items/ComponentTab/ImportComponent.vue

Lines changed: 157 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export default {
2727
2828
openVueFile(data) {
2929
if (data === undefined) return;
30+
const importObj = {};
31+
let compName = data[0].slice(data[0].lastIndexOf('/')+1).replace(/[^a-z0-9-_.]/gi, '');
3032
let vueFile = fs.readFileSync(data[0], "utf8");
31-
const importObj = {
32-
htmlList: []
33-
};
34-
33+
importObj.componentName = compName;
34+
3535
const htmlElementMap = { //OverVue state management only handles these HTML tags.
3636
div: ["<div>", "</div>"],
3737
button: ["<button>", "</button>"],
@@ -50,48 +50,112 @@ export default {
5050
let htmlString = vueFile.substring(vueFile.indexOf('<template >')+10, vueFile.indexOf('</template>'));
5151
let scriptString = vueFile.substring(vueFile.indexOf(`<script>`)+8, vueFile.indexOf(`/script>`)-1)
5252
htmlParser(htmlString);
53-
createHTMLList(htmlList);
53+
importObj.props = parsingStringToProps(scriptString);
54+
importObj.actions = parsingStringToAction(scriptString);
55+
importObj.state = parsingStringToState(scriptString);
56+
htmlList.pop(); htmlList.shift(); //OverVue adds a <div></div> wrapper to all components. remove this before importing.
57+
let groupings = findGroupings(htmlList);
58+
let groupingObj = objectGenerator(groupings);
59+
let groupingArray = [];
60+
for (const key in groupingObj){
61+
groupingArray.push(groupingObj[key])
62+
}
63+
importObj.htmlList = groupingArray;
64+
console.log(importObj)
5465
66+
5567
function htmlParser(htmlString){
5668
if (!htmlString){return}
5769
//remove new lines and tabs from HTML
58-
htmlString = htmlString.replaceAll('\n', '').replaceAll('\t', '')
70+
htmlString = htmlString.replaceAll('\n', '').replaceAll('\t', '');
5971
//find index for the first < and > in the string
60-
let openTag = htmlString.indexOf('<')
61-
let closeTag = htmlString.indexOf('>')
72+
let openTag = htmlString.indexOf('<');
73+
let closeTag = htmlString.indexOf('>');
6274
6375
//push the substring wrapped by < and > into the helper func
64-
pushElement(htmlString.substring(openTag+1, closeTag))
76+
buildList(htmlString.substring(openTag+1, closeTag))
6577
6678
//recursively call htmlParser on the remainder of the string
6779
return htmlParser(htmlString.substring(closeTag+1))
6880
}
6981
70-
function pushElement(substring){
82+
function buildList(substring){
7183
for (const elem in htmlElementMap){
7284
for (let i = 0; i < htmlElementMap[elem].length; i++){
7385
//if the htmlElementMap[elem][index] matches the substring, push it into the output array
86+
if (substring === 'p'){
87+
htmlList.push(htmlElementMap.paragraph[0])
88+
return;
89+
} else if (substring === '/p') {
90+
htmlList.push(htmlElementMap.paragraph[1])
91+
return;
92+
}
7493
if (htmlElementMap[elem][i].indexOf(substring) !== -1){
7594
htmlList.push(htmlElementMap[elem][i])
76-
break; //exit, or else div will match with /div, for example.
95+
return;
7796
}
7897
}
7998
}
8099
}
81100
82-
function createHTMLList (htmlList){
83-
let currentOpen = null;
84-
for (let i = 0; i < htmlList.length; i++){
85-
const elemText = htmlList[i].substring(1,htmlList[i].length-1);
86-
101+
function findGroupings(array){
102+
let count = 0; //tracks where the parent ends
103+
let stopIndexes = []; //an array that will be used to slice out the parent/child relationships
104+
for (let i = 0; i < array.length; i++){
105+
if (array[i][1] !== '/'){
106+
if (array[i] === '<img>' || array[i] === '<a href="#"/>' || array[i] === '<input />'){
107+
if (count === 0){
108+
stopIndexes.push(i);
109+
continue;
110+
}
111+
} else {
112+
count++;
113+
}
114+
} else {
115+
count--;
116+
}
117+
if (count === 0){
118+
stopIndexes.push(i)
119+
}
87120
}
121+
122+
let groupings = [];
123+
let startIndex = 0;
124+
for (let i = 0; i < stopIndexes.length; i++){
125+
groupings.push(array.slice(startIndex, stopIndexes[i]+1));
126+
startIndex = stopIndexes[i]+1;
127+
}
128+
129+
return groupings;
88130
}
89131
90-
console.log(importObj)
91-
92-
93-
}
94-
}
132+
function objectGenerator(array){
133+
let groupingObj = {};
134+
let idGen = Date.now()
135+
for (let i = 0; i < array.length; i++){
136+
for (const key in htmlElementMap){
137+
if (array[i][0] === htmlElementMap[key][0]){
138+
groupingObj[i] = {text: key, id: idGen}
139+
}
140+
}
141+
array[i].pop();
142+
array[i].shift();
143+
if (array[i].length > 0){
144+
const childGroupings = findGroupings(array[i]);
145+
const childrenObj = objectGenerator(childGroupings)
146+
const childrenArray = [];
147+
for (const key in childrenObj){
148+
childrenArray.push(childrenObj[key])
149+
}
150+
groupingObj[i].children = childrenArray;
151+
} else {
152+
groupingObj[i].children = [];
153+
}
154+
}
155+
return groupingObj;
156+
}
157+
}
158+
}
95159
}
96160
97161
//img, link, input do NOT need closing tags -- cannot have children either
@@ -103,18 +167,80 @@ export default {
103167
//string.slice(indexof({),indexof(:))
104168
105169
function parsingStringToProps (str){
170+
let props = [];
171+
172+
let split = str.split(' ');
173+
174+
for(let i = 0; i < split.length; i++){
175+
176+
if(split[i].includes(':') && split[i] !== 'computed:' && split[i] !== 'methods:' && split[i] !== 'name:' && split[i] !=='components:'){
177+
178+
let cleanStr = split[i].slice(0,split[i].indexOf(':'))
106179
107-
//Split the strings into an array
108-
//look for the last '{' in the string
109-
//look for the first ':' after the last '{'
110-
//save the string in between as a first prop
180+
props.push(cleanStr)
181+
182+
}
111183
112-
//if after ':' there's a ',' look for ':'
113-
// save the string in between as a second prop
114184
115-
//if after ':' there's a ',' invoke the second function in search for the third prop, etc..
116-
// save the string in between as third prop
185+
}
186+
console.log('props: ', props)
187+
return props
117188
}
189+
// First slice from map actions until
190+
function parsingStringToAction (str){
191+
let action = [];
192+
193+
//indexOfMapActions =
194+
let trashedSlice = str.slice(str.lastIndexOf('...mapActions')+15);
195+
let slice = trashedSlice.slice(0, trashedSlice.indexOf('])'));
196+
197+
let split = slice.split(' ')
198+
199+
for(let i = 0; i < split.length; i++){
200+
201+
if (split[i].includes(',')){
202+
203+
204+
205+
let cleanStr = split[i].slice(split[i].indexOf('"')+1,split[i].lastIndexOf('"'))
206+
207+
action.push(cleanStr)
208+
}
209+
210+
211+
}
212+
console.log('actions', action)
213+
return action
214+
}
215+
216+
217+
function parsingStringToState (str){
218+
let state = [];
219+
220+
let trashedSlice = str.slice(str.lastIndexOf('...mapState')+15);
221+
let slice = trashedSlice.slice(0, trashedSlice.indexOf('])'));
222+
223+
224+
let split = slice.split(' ')
225+
226+
for(let i = 0; i < split.length; i++){
227+
228+
if (split[i].includes(',')){
229+
230+
let cleanStr = split[i].slice(split[i].indexOf('"')+1,split[i].lastIndexOf('"'))
231+
232+
233+
state.push(cleanStr)
234+
235+
}
236+
237+
238+
}
239+
console.log('state', state)
240+
return state
241+
}
242+
243+
118244
/*
119245
//
120246
data () {
@@ -143,6 +269,8 @@ export default {
143269
methods: {
144270
...mapActions([
145271
"TestActionForImport",
272+
"AnotherTestAction",
273+
"AThirdTestAction"
146274
]),
147275
},
148276

0 commit comments

Comments
 (0)