@@ -3,6 +3,7 @@ const CSON = require('season')
33const path = require ( 'path' )
44const _ = require ( 'lodash' )
55const sander = require ( 'sander' )
6+ const consts = require ( 'browser/lib/consts' )
67
78let storages = [ ]
89let notes = [ ]
@@ -67,6 +68,7 @@ class Storage {
6768 let initialStorage = {
6869 folders : [ ]
6970 }
71+
7072 return sander . writeFile ( path . join ( this . cache . path , 'boostnote.json' ) , JSON . stringify ( initialStorage ) )
7173 } else throw err
7274 } )
@@ -127,10 +129,11 @@ function init () {
127129 let caches
128130 try {
129131 caches = JSON . parse ( localStorage . getItem ( 'storages' ) )
132+ if ( ! _ . isArray ( caches ) ) throw new Error ( 'Cached data is not valid.' )
130133 } catch ( e ) {
131134 console . error ( e )
132135 caches = [ ]
133- localStorage . getItem ( 'storages' , JSON . stringify ( caches ) )
136+ localStorage . setItem ( 'storages' , JSON . stringify ( caches ) )
134137 }
135138
136139 return caches . map ( ( cache ) => {
@@ -236,15 +239,14 @@ function addStorage (input) {
236239 note . folder = folder . key
237240 _notes . push ( Note . forge ( note ) )
238241 } )
239-
240- notes = notes . slice ( ) . concat ( _notes )
241242 } )
242243
243- return Promise . all ( notes )
244- . then ( ( notes ) => {
244+ return Promise . all ( _notes )
245+ . then ( ( _notes ) => {
246+ notes = notes . concat ( _notes )
245247 let data = {
246248 storage : storage ,
247- notes : notes
249+ notes : _notes
248250 }
249251 return isFolderRemoved
250252 ? storage . saveData ( ) . then ( ( ) => data )
@@ -255,6 +257,17 @@ function addStorage (input) {
255257 storages = storages . filter ( ( storage ) => storage . key !== data . storage . key )
256258 storages . push ( data . storage )
257259 _saveCaches ( )
260+
261+ if ( data . storage . data . folders . length < 1 ) {
262+ return createFolder ( data . storage . key , {
263+ name : 'Default' ,
264+ color : consts . FOLDER_COLORS [ 0 ]
265+ } ) . then ( ( ) => data )
266+ }
267+
268+ return data
269+ } )
270+ . then ( ( data ) => {
258271 return {
259272 storage : data . storage . toJSON ( ) ,
260273 notes : data . notes . map ( ( note ) => note . toJSON ( ) )
@@ -269,6 +282,88 @@ function removeStorage (key) {
269282 return Promise . resolve ( true )
270283}
271284
285+ function migrateFromV5 ( key , data ) {
286+ let oldFolders = data . folders
287+ let oldArticles = data . articles
288+ let storage = _ . find ( storages , { key : key } )
289+ if ( storage == null ) throw new Error ( 'Storage doesn\'t exist.' )
290+
291+ let migrateFolders = oldFolders . map ( ( oldFolder ) => {
292+ let folderKey = keygen ( )
293+ while ( storage . data . folders . some ( ( folder ) => folder . key === folderKey ) ) {
294+ folderKey = keygen ( )
295+ }
296+ let newFolder = {
297+ key : folderKey ,
298+ name : oldFolder . name ,
299+ color : consts . FOLDER_COLORS [ Math . floor ( Math . random ( ) * 7 ) % 7 ]
300+ }
301+ storage . data . folders . push ( newFolder )
302+ let articles = oldArticles . filter ( ( article ) => article . FolderKey === oldFolder . key )
303+ let folderNotes = [ ]
304+ articles . forEach ( ( article ) => {
305+ let noteKey = keygen ( )
306+ while ( notes . some ( ( note ) => note . storage === key && note . folder === folderKey && note . key === noteKey ) ) {
307+ key = keygen ( )
308+ }
309+ if ( article . mode === 'markdown' ) {
310+ let newNote = new Note ( {
311+ tags : article . tags ,
312+ createdAt : article . createdAt ,
313+ updatedAt : article . updatedAt ,
314+ folder : folderKey ,
315+ storage : key ,
316+ type : 'MARKDOWN_NOTE' ,
317+ isStarred : false ,
318+ title : article . title ,
319+ content : '# ' + article . title + '\n\n' + article . content ,
320+ key : noteKey
321+ } )
322+ notes . push ( newNote )
323+ folderNotes . push ( newNote )
324+ } else {
325+ let newNote = new Note ( {
326+ tags : article . tags ,
327+ createdAt : article . createdAt ,
328+ updatedAt : article . updatedAt ,
329+ folder : folderKey ,
330+ storage : key ,
331+ type : 'SNIPPET_NOTE' ,
332+ isStarred : false ,
333+ title : article . title ,
334+ description : article . title ,
335+ key : noteKey ,
336+ snippets : [ {
337+ name : article . mode ,
338+ mode : article . mode ,
339+ content : article . content
340+ } ]
341+ } )
342+ notes . push ( newNote )
343+ folderNotes . push ( newNote )
344+ }
345+ } )
346+
347+ return sander
348+ . writeFile ( path . join ( storage . cache . path , folderKey , 'data.json' ) , JSON . stringify ( {
349+ notes : folderNotes . map ( ( note ) => {
350+ let json = note . toJSON ( )
351+ delete json . storage
352+ return json
353+ } )
354+ } ) )
355+ } )
356+ return Promise . all ( migrateFolders )
357+ . then ( ( ) => storage . saveData ( ) )
358+ . then ( ( ) => {
359+ return {
360+ storage : storage . toJSON ( ) ,
361+ notes : notes . filter ( ( note ) => note . storage === key )
362+ . map ( ( note ) => note . toJSON ( ) )
363+ }
364+ } )
365+ }
366+
272367function createFolder ( key , input ) {
273368 let storage = _ . find ( storages , { key : key } )
274369 if ( storage == null ) throw new Error ( 'Storage doesn\'t exist.' )
@@ -441,5 +536,6 @@ export default {
441536 createSnippetNote,
442537 updateNote,
443538 removeNote,
444- moveNote
539+ moveNote,
540+ migrateFromV5
445541}
0 commit comments