@@ -22,20 +22,25 @@ function start() {
2222
2323 isCommit = location . href . match ( urlCommitRegex ) ;
2424
25- if ( location . href . match ( urlPullRegex ) || location . href . match ( urlCommitRegex ) ) { // show only on PR files page
26- initialSetup ( ) ;
25+ if (
26+ ( location . href . match ( urlPullRegex ) || location . href . match ( urlCommitRegex ) ) // show only on PR files page
27+ ) {
28+ chrome . storage . sync . get ( [ 'closed' , 'collapsed' , 'folders' ] , items => initialSetup ( items ) ) ;
2729 }
2830 }
2931 } , 500 ) ;
3032}
3133
32- function initialSetup ( ) {
34+ function initialSetup ( savedItems ) {
3335 if ( $ ( '.js-diff-progressive-spinner' ) . length || ! $ ( '#files' ) . length ) {
3436 return ;
3537 }
3638
3739 injectCss ( isCommit ? 0 : 178 , isCommit ? 20 : 0 ) ; // style.js
38- injectHTML ( ) ;
40+ injectHTML ( savedItems ) ;
41+
42+ savedItems . collapsed ? collapseAllDiffBlocks ( ) : expandAllDiffBlocks ( ) ;
43+ savedItems . closed ? close ( ) : open ( ) ;
3944
4045 areDiffBlocksCollapsed ( ) ? $ ( '#collapseAll' ) . hide ( ) : $ ( '#expandAll' ) . hide ( ) ;
4146
@@ -52,25 +57,15 @@ function initialSetup() {
5257 }
5358 } ) ;
5459
55- $ ( '#openAll' ) . click ( ( ) => {
56- $ ( '.gct-folder' ) . addClass ( 'gct-folder-open' ) ;
57- } ) ;
58-
59- $ ( '#closeAll' ) . click ( ( ) => {
60- $ ( '.gct-folder' ) . removeClass ( 'gct-folder-open' ) ;
61- } ) ;
60+ $ ( '#openAll' ) . click ( ( ) => open ( ) ) ;
61+ $ ( '#closeAll' ) . click ( ( ) => close ( ) ) ;
6262
63- $ ( '#expandAll' ) . click ( ( ) => {
64- expandAllDiffBlocks ( ) ;
65- } ) ;
66-
67- $ ( '#collapseAll' ) . click ( ( ) => {
68- collapseAllDiffBlocks ( ) ;
69- } ) ;
63+ $ ( '#expandAll' ) . click ( ( ) => expandAllDiffBlocks ( ) ) ;
64+ $ ( '#collapseAll' ) . click ( ( ) => collapseAllDiffBlocks ( ) ) ;
7065}
7166
72- function injectHTML ( ) {
73- tree = buildTree ( ) ;
67+ function injectHTML ( savedItems ) {
68+ tree = buildTree ( savedItems ) ;
7469 $ (
7570 `<div class="gct-file-tree">
7671 <div class="gct-header">
@@ -126,7 +121,7 @@ function buildHtmlTree(tree) {
126121 return content ;
127122}
128123
129- function buildTree ( ) {
124+ function buildTree ( savedItems ) {
130125 var tree = { } ;
131126
132127 $ ( '.file-info' ) . map ( ( i , item ) => {
@@ -163,9 +158,61 @@ function buildTree() {
163158 tree = mergeObjects ( tree , nodeObj ) ;
164159 } ) ;
165160
161+ if ( savedItems . folders ) {
162+ tree = joinEmptyFolders ( tree , [ ] ) ;
163+
164+ if ( tree . merge ) {
165+ let temporaryTree = { } ;
166+ temporaryTree [ tree . key ] = tree . obj ;
167+ tree = temporaryTree ;
168+ }
169+ }
170+
166171 return tree ;
167172}
168173
174+ function joinEmptyFolders ( obj , paths ) {
175+ let current = obj ;
176+ paths . map ( path => current = current [ path ] ) ;
177+
178+ const files = Object . keys ( current ) . filter ( key => Array . isArray ( current [ key ] ) ) ;
179+
180+ Object . keys ( current )
181+ . filter ( key => ! Array . isArray ( current [ key ] ) ) . map ( key => {
182+ const childPath = [ ...paths , key ] ;
183+ const folder = joinEmptyFolders ( obj , childPath ) ;
184+ if ( folder . merge && files . length === 0 ) {
185+ childPath [ childPath . length - 1 ] = `${ key } /${ folder . key } ` ;
186+ current [ `${ key } /${ folder . key } ` ] = folder . obj ;
187+ delete current [ key ] ;
188+
189+ return {
190+ merge : true ,
191+ obj : { ...current } ,
192+ key : key ,
193+ } ;
194+ }
195+ } ) ;
196+
197+ // yes, I need to extract again the keys from the current, it could be modified
198+ var keys = Object . keys ( current ) . filter ( key => ! Array . isArray ( current [ key ] ) ) ;
199+
200+ // has only sub-folder, so should merge the keys
201+ if ( keys . length === 1 ) {
202+ let childKey = keys [ 0 ] ;
203+
204+ if ( current [ keys [ 0 ] ] . files && current [ keys [ 0 ] ] . files . length ) {
205+ return {
206+ merge : true ,
207+ obj : current [ keys [ 0 ] ] ,
208+ key : keys [ 0 ] ,
209+ } ;
210+ }
211+ }
212+
213+ return current ;
214+ }
215+
169216function mergeObjects ( og , so ) {
170217 for ( var key in so ) {
171218 if ( ! og [ key ] ) {
@@ -216,3 +263,11 @@ function collapseAllDiffBlocks() {
216263 }
217264 } ) ;
218265}
266+
267+ function open ( ) {
268+ $ ( '.gct-folder' ) . addClass ( 'gct-folder-open' ) ;
269+ }
270+
271+ function close ( ) {
272+ $ ( '.gct-folder' ) . removeClass ( 'gct-folder-open' ) ;
273+ }
0 commit comments