@@ -140,68 +140,19 @@ cwc.fileHandler.FileLoader.prototype.loadGDriveFileData = function(id,
140
140
cwc . fileHandler . FileLoader . prototype . handleFileData = function ( data ,
141
141
filename = '' , fileHandler = null , gDriveId = undefined ) {
142
142
return new Promise ( ( resolve ) => {
143
- this . log_ . info ( 'Handle file data ... (' , data . length , ')' ) ;
144
- let fileInstance = this . helper . getInstance ( 'file' , true ) ;
145
- let modeInstance = this . helper . getInstance ( 'mode' , true ) ;
146
143
let mimeType = cwc . utils . mime . getTypeByNameAndContent ( filename || '' , data ) ;
147
- this . log_ . info ( 'MIME-type:' , mimeType ) ;
144
+ this . log_ . info ( 'Handle file data ... (' , data . length , ') with MIME-type:' ,
145
+ mimeType ) ;
148
146
149
- let modeType ;
147
+ // Load compatible file mode.
150
148
if ( mimeType === cwc . utils . mime . Type . CWC . type ) {
151
- // Handle CWC file format
152
- let file = new cwc . fileFormat . File ( data ) ;
153
- modeType = cwc . mode . Config . getMode (
154
- /** @type {cwc.mode.Type } */ ( file . getMode ( ) ) ) ;
155
- this . log_ . info ( 'Loading CWC file with mode' , modeType , '...' ) ;
156
- fileInstance . setFile ( file ) ;
157
- fileInstance . setMimeType ( cwc . utils . mime . getByType ( mimeType ) ) ;
158
- modeInstance . setMode ( modeType ) ;
159
- modeInstance . setFilename ( filename ) ;
160
-
161
- // Handling Blockly and normal Editor content.
162
- let editorContent = file . getContentData ( ) ;
163
- for ( let entry in editorContent ) {
164
- if ( Object . prototype . hasOwnProperty . call ( editorContent , entry ) ) {
165
- let content = editorContent [ entry ] ;
166
- let contentType = content . getType ( ) ;
167
- switch ( contentType ) {
168
- case cwc . utils . mime . Type . BLOCKLY . type :
169
- modeInstance . addBlocklyView (
170
- content . getName ( ) , content . getContent ( ) ) ;
171
- break ;
172
- case cwc . utils . mime . Type . COFFEESCRIPT . type :
173
- case cwc . utils . mime . Type . CSS . type :
174
- case cwc . utils . mime . Type . HTML . type :
175
- case cwc . utils . mime . Type . JAVASCRIPT . type :
176
- modeInstance . addEditorView (
177
- content . getName ( ) , content . getContent ( ) , contentType ) ;
178
- break ;
179
- default :
180
- this . log_ . warn ( 'Unknown content type:' , contentType ) ;
181
- }
182
- }
183
- }
184
-
185
- // Handle UI mode
186
- let fileUi = fileInstance . getUi ( ) ;
187
- if ( fileUi ) {
188
- if ( fileUi === 'blockly' ) {
189
- modeInstance . showBlockly ( ) ;
190
- } else if ( fileUi === 'editor' ) {
191
- modeInstance . showEditor ( ) ;
192
- }
193
- }
149
+ this . loadCWCFile ( new cwc . fileFormat . File ( data ) , filename ) ;
194
150
} else {
195
- // Handle raw file format
196
- modeType = cwc . mode . Config . getModeByMimeType ( mimeType ) ;
197
- this . log_ . info ( 'Loading raw data with mode' , modeType , '...' ) ;
198
- fileInstance . setRawFile ( data , filename ) ;
199
- fileInstance . setMimeType ( cwc . utils . mime . getByType ( mimeType ) ) ;
200
- modeInstance . setMode ( modeType ) ;
201
- modeInstance . addEditorView ( '__default__' , data , mimeType ) ;
151
+ this . loadRawFile ( data , filename ) ;
202
152
}
203
153
204
154
// Sets file handler for local or gDrive files
155
+ let fileInstance = this . helper . getInstance ( 'file' ) ;
205
156
if ( fileHandler ) {
206
157
if ( fileHandler . name ) {
207
158
fileInstance . setFilename ( fileHandler . name ) ;
@@ -211,21 +162,83 @@ cwc.fileHandler.FileLoader.prototype.handleFileData = function(data,
211
162
fileInstance . setGDriveId ( gDriveId ) ;
212
163
}
213
164
214
- // Sets the file title.
215
- let fileTitle = fileInstance . getFileTitle ( ) || fileInstance . getFilename ( ) ;
216
- if ( fileTitle ) {
217
- modeInstance . setTitle ( fileTitle ) ;
218
- }
219
-
220
- // Handle post modification tasks.
221
- modeInstance . postMode ( modeType ) ;
222
-
223
165
this . helper . showSuccess ( 'Loaded file ' + filename + ' successful.' ) ;
224
166
resolve ( ) ;
225
167
} ) ;
226
168
} ;
227
169
228
170
171
+ /**
172
+ * @param {!cwc.fileFormat.File } file
173
+ * @param {string= } filename
174
+ */
175
+ cwc . fileHandler . FileLoader . prototype . loadCWCFile = function ( file ,
176
+ filename = '' ) {
177
+ let modeType = cwc . mode . Config . getMode (
178
+ /** @type {cwc.mode.Type } */ ( file . getMode ( ) ) ) ;
179
+ this . log_ . info ( 'Loading CWC file with mode' , modeType , '...' ) ;
180
+
181
+ let fileInstance = this . helper . getInstance ( 'file' ) ;
182
+ fileInstance . setFile ( file ) ;
183
+ fileInstance . setMimeType ( cwc . utils . mime . Type . CWC ) ;
184
+
185
+ let modeInstance = this . helper . getInstance ( 'mode' ) ;
186
+ modeInstance . setMode ( modeType ) ;
187
+ modeInstance . setFilename ( filename ) ;
188
+
189
+ // Handling Blockly and normal Editor content.
190
+ let editorContent = file . getContentData ( ) ;
191
+ for ( let entry in editorContent ) {
192
+ if ( Object . prototype . hasOwnProperty . call ( editorContent , entry ) ) {
193
+ let content = editorContent [ entry ] ;
194
+ let contentType = content . getType ( ) ;
195
+ switch ( contentType ) {
196
+ case cwc . utils . mime . Type . BLOCKLY . type :
197
+ modeInstance . addBlocklyView (
198
+ content . getName ( ) , content . getContent ( ) ) ;
199
+ break ;
200
+ case cwc . utils . mime . Type . COFFEESCRIPT . type :
201
+ case cwc . utils . mime . Type . CSS . type :
202
+ case cwc . utils . mime . Type . HTML . type :
203
+ case cwc . utils . mime . Type . JAVASCRIPT . type :
204
+ modeInstance . addEditorView (
205
+ content . getName ( ) , content . getContent ( ) , contentType ) ;
206
+ break ;
207
+ default :
208
+ this . log_ . warn ( 'Unknown content type:' , contentType ) ;
209
+ }
210
+ }
211
+ }
212
+
213
+ // Handle library files
214
+ // let cacheInstance = this.helper.getInstance('cache');
215
+
216
+ modeInstance . postMode ( ) ;
217
+ } ;
218
+
219
+
220
+ /**
221
+ * @param {!string } content
222
+ * @param {string= } filename
223
+ */
224
+ cwc . fileHandler . FileLoader . prototype . loadRawFile = function ( content ,
225
+ filename = '' ) {
226
+ let mimeType = cwc . utils . mime . getTypeByNameAndContent ( filename , content ) ;
227
+ let modeType = cwc . mode . Config . getModeByMimeType ( mimeType ) ;
228
+ this . log_ . info ( 'Loading' , mimeType , 'file with mode' , modeType , '...' ) ;
229
+
230
+ let fileInstance = this . helper . getInstance ( 'file' ) ;
231
+ fileInstance . setRawFile ( content , filename ) ;
232
+ fileInstance . setMimeType ( cwc . utils . mime . getByType ( mimeType ) ) ;
233
+
234
+ let modeInstance = this . helper . getInstance ( 'mode' ) ;
235
+ modeInstance . setMode ( modeType ) ;
236
+ modeInstance . setFilename ( filename ) ;
237
+ modeInstance . addEditorView ( '__default__' , content , mimeType ) ;
238
+ modeInstance . postMode ( ) ;
239
+ } ;
240
+
241
+
229
242
/**
230
243
* @param {!Function } callback
231
244
* @param {Object= } scope
0 commit comments