@@ -2,7 +2,6 @@ var unzip = new JSZip();
2
2
var reader = new FileReader ( ) ;
3
3
var fileNames ;
4
4
var fileNamesTree ;
5
- var canvasHeight ;
6
5
var imageStyles = {
7
6
background : '#000000' ,
8
7
foreground : '#00FA00' ,
@@ -68,36 +67,18 @@ function isRoot(fileName) {
68
67
}
69
68
70
69
function 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 : [ ] } ;
90
72
91
73
fileNameList . forEach ( file => {
92
- var isFolder = file . endsWith ( '/' ) ;
93
74
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 ;
94
78
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
+ }
101
82
} ) ;
102
83
103
84
return tree ;
@@ -107,7 +88,7 @@ function processFile(zip) {
107
88
fileNames = Object . keys ( zip . files ) ;
108
89
fileNamesTree = toTree ( fileNames , zip . files ) ;
109
90
110
- canvasHeight = fileNames . length * imageStyles . lineHeight + 50 ;
91
+ canvasHeight = fileNames . length * + imageStyles . lineHeight + 50 ;
111
92
var canvasElement = document . createElement ( 'canvas' ) ;
112
93
113
94
canvasElement . setAttribute ( 'id' , 'tree-canvas' ) ;
@@ -122,6 +103,7 @@ function processFile(zip) {
122
103
}
123
104
124
105
function drawTree ( ) {
106
+ var topShift = 30 - + imageStyles . lineHeight ;
125
107
var canvas = document . getElementById ( 'tree-canvas' ) ;
126
108
canvas . width = 582 ;
127
109
canvas . height = canvasHeight ;
@@ -134,36 +116,47 @@ function drawTree() {
134
116
context . fillStyle = imageStyles . background ;
135
117
context . fillRect ( 0 , 0 , canvas . width , canvasHeight ) ;
136
118
137
- fileNamesTree . forEach ( function ( file , index ) {
119
+ const drawFilePath = ( branch ) => {
138
120
context . beginPath ( ) ;
139
121
context . fillStyle = imageStyles . foreground ;
140
122
context . font = imageStyles . fontSize + "px Arial, Helvetica, sans-serif" ;
141
123
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 ;
145
128
146
129
if ( imageStyles . withDashes ) {
147
- fileName = `${ '-' . repeat ( 4 * file . nestLevel ) } ${ file . path } ` ;
130
+ fileName = `${ '-' . repeat ( 4 * nestLevel ) } ${ branch . value } ` ;
148
131
leftShift = 30 ;
149
- topShift = imageStyles . lineHeight * index + 30 ;
150
132
151
- if ( file . nestLevel === 0 ) {
133
+ if ( nestLevel === 0 ) {
152
134
leftShift -= 5 ;
153
135
}
154
136
}
155
137
156
- if ( file . isFolder ) {
138
+ if ( branch . metadata && branch . metadata . isFolder ) {
157
139
fileName += ' /' ;
158
140
}
159
141
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 ) ;
162
144
fileName += ` (~${ fileSize } kb)` ;
163
145
}
164
146
165
147
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 ) ;
167
160
168
161
if ( imageStyles . watermarkEnabled ) {
169
162
context . font = "12px Arial, Helvetica, sans-serif" ;
@@ -175,7 +168,7 @@ function drawTree() {
175
168
function changeSetting ( key , value ) {
176
169
imageStyles [ key ] = value ;
177
170
178
- canvasHeight = fileNames . length * imageStyles . lineHeight + 50 ;
171
+ canvasHeight = fileNames . length * + imageStyles . lineHeight + 30 ;
179
172
var canvas = document . getElementById ( 'tree-canvas' ) ;
180
173
canvas . height = canvasHeight ;
181
174
0 commit comments