mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Merge pull request #2144 from kelvin-wong/retain-collapsed-storage-state
Add collapsed state for storage
This commit is contained in:
@@ -21,8 +21,10 @@ class StorageItem extends React.Component {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
|
||||
const { storage } = this.props
|
||||
|
||||
this.state = {
|
||||
isOpen: true
|
||||
isOpen: !!storage.isOpen
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,8 +70,18 @@ class StorageItem extends React.Component {
|
||||
}
|
||||
|
||||
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({
|
||||
isOpen: !this.state.isOpen
|
||||
isOpen: isOpen
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,8 @@ function addStorage (input) {
|
||||
key,
|
||||
name: input.name,
|
||||
type: input.type,
|
||||
path: input.path
|
||||
path: input.path,
|
||||
isOpen: false
|
||||
}
|
||||
|
||||
return Promise.resolve(newStorage)
|
||||
@@ -48,7 +49,8 @@ function addStorage (input) {
|
||||
key: newStorage.key,
|
||||
type: newStorage.type,
|
||||
name: newStorage.name,
|
||||
path: newStorage.path
|
||||
path: newStorage.path,
|
||||
isOpen: false
|
||||
})
|
||||
|
||||
localStorage.setItem('storages', JSON.stringify(rawStorages))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const dataApi = {
|
||||
init: require('./init'),
|
||||
toggleStorage: require('./toggleStorage'),
|
||||
addStorage: require('./addStorage'),
|
||||
renameStorage: require('./renameStorage'),
|
||||
removeStorage: require('./removeStorage'),
|
||||
|
||||
@@ -8,7 +8,8 @@ function resolveStorageData (storageCache) {
|
||||
key: storageCache.key,
|
||||
name: storageCache.name,
|
||||
type: storageCache.type,
|
||||
path: storageCache.path
|
||||
path: storageCache.path,
|
||||
isOpen: storageCache.isOpen
|
||||
}
|
||||
|
||||
const boostnoteJSONPath = path.join(storageCache.path, 'boostnote.json')
|
||||
|
||||
28
browser/main/lib/dataApi/toggleStorage.js
Normal file
28
browser/main/lib/dataApi/toggleStorage.js
Normal 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
|
||||
@@ -360,6 +360,12 @@ function data (state = defaultDataMap(), action) {
|
||||
state.storageMap = new Map(state.storageMap)
|
||||
state.storageMap.set(action.storage.key, action.storage)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user