@@ -2,23 +2,31 @@ import { EditorStep, CodeFile } from "../mini-editor"
22
33// extend steps with info from previous steps
44
5- export function reduceSteps (
5+ /**
6+ * Extends `extraStep` with info from `baseStep`.
7+ *
8+ * @param baseStep it could be the header step or the previous step
9+ * @param step the step to be extended
10+ * @param filter if it is defined, show only the files in the array.
11+ *
12+ */
13+ export function reduceStep (
614 baseStep : EditorStep ,
7- extraStep : EditorStep ,
15+ step : EditorStep ,
816 filter ?: string [ ]
917) : EditorStep {
10- let files = reduceFiles ( baseStep . files , extraStep . files )
18+ let files = reduceFiles ( baseStep . files , step . files )
1119
1220 const newNorthPanel = reducePanel (
1321 baseStep . northPanel ,
14- extraStep . northPanel ,
15- extraStep . southPanel
22+ step . northPanel ,
23+ step . southPanel
1624 ) !
1725
1826 const newSouthPanel = reducePanel (
1927 baseStep . southPanel ,
20- extraStep . southPanel ,
21- extraStep . northPanel
28+ step . southPanel ,
29+ step . northPanel
2230 )
2331
2432 if ( filter ) {
@@ -35,7 +43,7 @@ export function reduceSteps(
3543
3644 return {
3745 ...baseStep ,
38- ...extraStep ,
46+ ...step ,
3947 files : files ,
4048 northPanel : newNorthPanel ,
4149 southPanel : newSouthPanel ,
@@ -68,33 +76,36 @@ function reducePanel(
6876}
6977
7078function reduceFiles (
71- baseFiles : CodeFile [ ] ,
72- extraFiles : CodeFile [ ]
79+ oldFiles : CodeFile [ ] ,
80+ newFiles : CodeFile [ ]
7381) : CodeFile [ ] {
7482 const filesMap = { } as { [ name : string ] : CodeFile }
75- baseFiles . forEach ( f => ( filesMap [ f . name ] = f ) )
76- extraFiles . forEach ( ef => {
77- const bf = filesMap [ ef . name ]
78- if ( ! bf ) {
79- filesMap [ ef . name ] = ef
83+ oldFiles . forEach ( f => ( filesMap [ f . name ] = f ) )
84+ newFiles . forEach ( newFile => {
85+ const prevFile = filesMap [ newFile . name ]
86+ if ( ! prevFile ) {
87+ filesMap [ newFile . name ] = newFile
8088 return
8189 }
8290
83- // merge old and new files
84- const { code, ...rest } = ef
91+ // if the file is in both arrays, merge the content
92+ // but if the new file is empty, keep the old content
93+ const { code, ...rest } = newFile
8594 if ( isEmpty ( code ) ) {
86- filesMap [ ef . name ] = { ...bf , ...rest }
95+ filesMap [ newFile . name ] = { ...prevFile , ...rest }
8796 } else {
88- filesMap [ ef . name ] = ef
97+ filesMap [ newFile . name ] = newFile
8998 }
9099 } )
91100
101+ // return a new array following the original order:
102+ // first the old files, then the new ones
92103 const result = [ ] as CodeFile [ ]
93- baseFiles . forEach ( f => {
104+ oldFiles . forEach ( f => {
94105 result . push ( filesMap [ f . name ] )
95106 delete filesMap [ f . name ]
96107 } )
97- extraFiles . forEach (
108+ newFiles . forEach (
98109 f => filesMap [ f . name ] && result . push ( filesMap [ f . name ] )
99110 )
100111
@@ -103,7 +114,7 @@ function reduceFiles(
103114
104115function isEmpty ( code : CodeFile [ "code" ] ) {
105116 const anyContent = code . lines . some ( l =>
106- l . tokens . some ( t => t . content . trim ( ) == "" )
117+ l . tokens . some ( t => t . content . trim ( ) ! == "" )
107118 )
108119 return ! anyContent
109120}
0 commit comments