1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

Merge pull request #2144 from kelvin-wong/retain-collapsed-storage-state

Add collapsed state for storage
This commit is contained in:
Sosuke Suzuki
2018-06-30 20:11:01 +09:00
committed by GitHub
7 changed files with 93 additions and 5 deletions

View File

@@ -21,8 +21,10 @@ class StorageItem extends React.Component {
constructor (props) { constructor (props) {
super(props) super(props)
const { storage } = this.props
this.state = { this.state = {
isOpen: true isOpen: !!storage.isOpen
} }
} }
@@ -68,8 +70,18 @@ class StorageItem extends React.Component {
} }
handleToggleButtonClick (e) { handleToggleButtonClick (e) {
const { storage, dispatch } = this.props
const isOpen = !this.state.isOpen
dataApi.toggleStorage(storage.key, isOpen)
.then((storage) => {
dispatch({
type: 'EXPAND_STORAGE',
storage,
isOpen
})
})
this.setState({ this.setState({
isOpen: !this.state.isOpen isOpen: isOpen
}) })
} }

View File

@@ -37,7 +37,8 @@ function addStorage (input) {
key, key,
name: input.name, name: input.name,
type: input.type, type: input.type,
path: input.path path: input.path,
isOpen: false
} }
return Promise.resolve(newStorage) return Promise.resolve(newStorage)
@@ -48,7 +49,8 @@ function addStorage (input) {
key: newStorage.key, key: newStorage.key,
type: newStorage.type, type: newStorage.type,
name: newStorage.name, name: newStorage.name,
path: newStorage.path path: newStorage.path,
isOpen: false
}) })
localStorage.setItem('storages', JSON.stringify(rawStorages)) localStorage.setItem('storages', JSON.stringify(rawStorages))

View File

@@ -1,5 +1,6 @@
const dataApi = { const dataApi = {
init: require('./init'), init: require('./init'),
toggleStorage: require('./toggleStorage'),
addStorage: require('./addStorage'), addStorage: require('./addStorage'),
renameStorage: require('./renameStorage'), renameStorage: require('./renameStorage'),
removeStorage: require('./removeStorage'), removeStorage: require('./removeStorage'),

View File

@@ -8,7 +8,8 @@ function resolveStorageData (storageCache) {
key: storageCache.key, key: storageCache.key,
name: storageCache.name, name: storageCache.name,
type: storageCache.type, type: storageCache.type,
path: storageCache.path path: storageCache.path,
isOpen: storageCache.isOpen
} }
const boostnoteJSONPath = path.join(storageCache.path, 'boostnote.json') const boostnoteJSONPath = path.join(storageCache.path, 'boostnote.json')

View File

@@ -0,0 +1,28 @@
const _ = require('lodash')
const resolveStorageData = require('./resolveStorageData')
/**
* @param {String} key
* @param {Boolean} isOpen
* @return {Object} Storage meta data
*/
function toggleStorage (key, isOpen) {
let cachedStorageList
try {
cachedStorageList = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(cachedStorageList)) throw new Error('invalid storages')
} catch (err) {
console.log('error got')
console.error(err)
return Promise.reject(err)
}
const targetStorage = _.find(cachedStorageList, {key: key})
if (targetStorage == null) return Promise.reject('Storage')
targetStorage.isOpen = isOpen
localStorage.setItem('storages', JSON.stringify(cachedStorageList))
return resolveStorageData(targetStorage)
}
module.exports = toggleStorage

View File

@@ -360,6 +360,12 @@ function data (state = defaultDataMap(), action) {
state.storageMap = new Map(state.storageMap) state.storageMap = new Map(state.storageMap)
state.storageMap.set(action.storage.key, action.storage) state.storageMap.set(action.storage.key, action.storage)
return state return state
case 'EXPAND_STORAGE':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
action.storage.isOpen = action.isOpen
state.storageMap.set(action.storage.key, action.storage)
return state
} }
return state return state
} }

View File

@@ -0,0 +1,38 @@
const test = require('ava')
const toggleStorage = require('browser/main/lib/dataApi/toggleStorage')
global.document = require('jsdom').jsdom('<body></body>')
global.window = document.defaultView
global.navigator = window.navigator
const Storage = require('dom-storage')
const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true })
const path = require('path')
const _ = require('lodash')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')
const storagePath = path.join(os.tmpdir(), 'test/toggle-storage')
test.beforeEach((t) => {
t.context.storage = TestDummy.dummyStorage(storagePath)
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
})
test.serial('Toggle a storage location', (t) => {
const storageKey = t.context.storage.cache.key
return Promise.resolve()
.then(function doTest () {
return toggleStorage(storageKey, true)
})
.then(function assert (data) {
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
t.true(_.find(cachedStorageList, {key: storageKey}).isOpen === true)
})
})
test.after(function after () {
localStorage.clear()
sander.rimrafSync(storagePath)
})