Skip to content

Commit b2d34ab

Browse files
committed
add initialization modal & migration func
1 parent f628ed5 commit b2d34ab

9 files changed

Lines changed: 410 additions & 339 deletions

File tree

browser/main/Main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import dataApi from 'browser/main/lib/dataApi'
1010
import StatusBar from './StatusBar'
1111
import _ from 'lodash'
1212
import ConfigManager from 'browser/main/lib/ConfigManager'
13+
import modal from 'browser/main/lib/modal'
14+
import InitModal from 'browser/main/modals/InitModal'
1315

1416
class Main extends React.Component {
1517
constructor (props) {
@@ -34,6 +36,10 @@ class Main extends React.Component {
3436
storages: data.storages,
3537
notes: data.notes
3638
})
39+
40+
if (data.storages.length < 1) {
41+
modal.open(InitModal)
42+
}
3743
})
3844
}
3945

browser/main/lib/ConfigManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const defaultConfig = {
2222
fontFamily: 'Monaco, Consolas',
2323
indentType: 'space',
2424
indentSize: '4',
25-
switchPreview: 'RIGHTCLICK'
25+
switchPreview: 'BLUR' // Available value: RIGHTCLICK, BLUR
2626
},
2727
preview: {
2828
fontSize: '14',

browser/main/lib/dataApi.js

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const CSON = require('season')
33
const path = require('path')
44
const _ = require('lodash')
55
const sander = require('sander')
6+
const consts = require('browser/lib/consts')
67

78
let storages = []
89
let 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+
272367
function 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

Comments
 (0)