@@ -2,7 +2,6 @@ var unzip = new JSZip();
22var reader = new FileReader ( ) ;
33var fileNames ;
44var fileNamesTree ;
5- var canvasHeight ;
65var imageStyles = {
76 background : '#000000' ,
87 foreground : '#00FA00' ,
@@ -68,36 +67,18 @@ function isRoot(fileName) {
6867}
6968
7069function toTree ( fileNameList , files ) {
71- var tree = [ ] ;
72- /* Sort files and folders by pushing files to the end of the list */
73- fileNameList . sort ( ( prev , next ) => {
74- var prevSlashesLength = prev . match ( / \/ / ig) ;
75- var nextSlashesLength = next . match ( / \/ / ig) ;
76-
77- if ( isRoot ( prev ) || isRoot ( next ) ) {
78- return 0 ;
79- }
80-
81- if ( prevSlashesLength < nextSlashesLength ) {
82- return 1 ;
83- }
84- if ( prevSlashesLength > nextSlashesLength ) {
85- return - 1 ;
86- }
87-
88- return 0 ;
89- } ) ;
70+ var tree = new Tree ( ) ;
71+ tree . _root = { value : fileNameList [ 0 ] . replace ( '/' , '' ) , metadata : { isFolder : true } , children : [ ] } ;
9072
9173 fileNameList . forEach ( file => {
92- var isFolder = file . endsWith ( '/' ) ;
9374 var filePaths = file . split ( '/' ) . filter ( path => ! ! path ) ;
75+ var isFolder = file . endsWith ( '/' ) ;
76+ var size = files [ file ] . _data . uncompressedSize ;
77+ var nestLevel = filePaths . length - 1 ;
9478
95- tree . push ( {
96- path : filePaths [ filePaths . length - 1 ] ,
97- nestLevel : filePaths . length - 1 ,
98- isFolder : isFolder ,
99- size : files [ file ] . _data . uncompressedSize
100- } ) ;
79+ if ( ! isRoot ( file ) ) {
80+ tree . add ( filePaths [ filePaths . length - 1 ] , { isFolder, size, nestLevel } , filePaths [ filePaths . length - 2 ] ) ;
81+ }
10182 } ) ;
10283
10384 return tree ;
@@ -107,7 +88,7 @@ function processFile(zip) {
10788 fileNames = Object . keys ( zip . files ) ;
10889 fileNamesTree = toTree ( fileNames , zip . files ) ;
10990
110- canvasHeight = fileNames . length * imageStyles . lineHeight + 50 ;
91+ canvasHeight = fileNames . length * + imageStyles . lineHeight + 50 ;
11192 var canvasElement = document . createElement ( 'canvas' ) ;
11293
11394 canvasElement . setAttribute ( 'id' , 'tree-canvas' ) ;
@@ -122,6 +103,7 @@ function processFile(zip) {
122103}
123104
124105function drawTree ( ) {
106+ var topShift = 30 - + imageStyles . lineHeight ;
125107 var canvas = document . getElementById ( 'tree-canvas' ) ;
126108 canvas . width = 582 ;
127109 canvas . height = canvasHeight ;
@@ -134,36 +116,47 @@ function drawTree() {
134116 context . fillStyle = imageStyles . background ;
135117 context . fillRect ( 0 , 0 , canvas . width , canvasHeight ) ;
136118
137- fileNamesTree . forEach ( function ( file , index ) {
119+ const drawFilePath = ( branch ) => {
138120 context . beginPath ( ) ;
139121 context . fillStyle = imageStyles . foreground ;
140122 context . font = imageStyles . fontSize + "px Arial, Helvetica, sans-serif" ;
141123
142- var fileName = file . path ;
143- var leftShift = 25 + file . nestLevel * 25 ;
144- var topShift = imageStyles . lineHeight * index + 30 ;
124+ var nestLevel = branch . metadata && branch . metadata . nestLevel || 0 ;
125+ var fileName = branch . value ;
126+ var leftShift = 25 + nestLevel * 25 ;
127+ topShift += + imageStyles . lineHeight ;
145128
146129 if ( imageStyles . withDashes ) {
147- fileName = `${ '-' . repeat ( 4 * file . nestLevel ) } ${ file . path } ` ;
130+ fileName = `${ '-' . repeat ( 4 * nestLevel ) } ${ branch . value } ` ;
148131 leftShift = 30 ;
149- topShift = imageStyles . lineHeight * index + 30 ;
150132
151- if ( file . nestLevel === 0 ) {
133+ if ( nestLevel === 0 ) {
152134 leftShift -= 5 ;
153135 }
154136 }
155137
156- if ( file . isFolder ) {
138+ if ( branch . metadata && branch . metadata . isFolder ) {
157139 fileName += ' /' ;
158140 }
159141
160- if ( imageStyles . withSizes && file . size ) {
161- var fileSize = ( file . size / 1000 ) . toFixed ( 2 ) ;
142+ if ( imageStyles . withSizes && branch . metadata && branch . metadata . size ) {
143+ var fileSize = ( branch . metadata . size / 1000 ) . toFixed ( 2 ) ;
162144 fileName += ` (~${ fileSize } kb)` ;
163145 }
164146
165147 context . fillText ( fileName , leftShift , topShift ) ;
166- } ) ;
148+ }
149+
150+ const traverseFilesTree = ( branch ) => {
151+ drawFilePath ( branch ) ;
152+ if ( branch . children . length ) {
153+ branch . children . forEach ( chiildBranch => {
154+ traverseFilesTree ( chiildBranch ) ;
155+ } ) ;
156+ }
157+ } ;
158+
159+ traverseFilesTree ( fileNamesTree . _root , 0 ) ;
167160
168161 if ( imageStyles . watermarkEnabled ) {
169162 context . font = "12px Arial, Helvetica, sans-serif" ;
@@ -175,7 +168,7 @@ function drawTree() {
175168function changeSetting ( key , value ) {
176169 imageStyles [ key ] = value ;
177170
178- canvasHeight = fileNames . length * imageStyles . lineHeight + 50 ;
171+ canvasHeight = fileNames . length * + imageStyles . lineHeight + 30 ;
179172 var canvas = document . getElementById ( 'tree-canvas' ) ;
180173 canvas . height = canvasHeight ;
181174
0 commit comments