-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Save data offline (close #7367)
- Loading branch information
Showing
9 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Storage | ||
|
||
Storage module for WordPress; built using localForage. | ||
|
||
## Installation | ||
|
||
Install the module | ||
|
||
```bash | ||
npm install @wordpress/storage --save | ||
``` | ||
|
||
<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@wordpress/storage", | ||
"version": "0.0.1", | ||
"description": "Offline storage module for WordPress.", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"shortcode" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/storage/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"dependencies": { | ||
"@babel/runtime-corejs2": "7.0.0-beta.56", | ||
"localforage": "^1.6.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import localforage from 'localforage'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { registerStore } from '@wordpress/data'; | ||
|
||
/** | ||
* Returns a localForage instance given a storeName. | ||
* | ||
* @param {string} storeName alphanumeric+underscore identifier for | ||
* a store. Passed to localForage's `config()` | ||
* method as the `storeName` prop. | ||
* | ||
* @return {localforage} localForage instance | ||
*/ | ||
export default function createStorage( storeName ) { | ||
if ( ! storeName || ! storeName.length ) { | ||
throw new Error( 'storeName is required for editor/utils/storage' ); | ||
} | ||
|
||
return localforage.createInstance( { | ||
name: 'WordPress Editor', | ||
storeName, | ||
} ); | ||
} | ||
|
||
const DEFAULT_STATE = {}; | ||
|
||
const actions = { | ||
getItem( storeName, key ) { | ||
return { | ||
type: 'GET_STORAGE_ITEM', | ||
key, | ||
storeName, | ||
}; | ||
}, | ||
|
||
getItemFromStorageBackend( storeName, key ) { | ||
return { | ||
type: 'GET_STORAGE_ITEM_FROM_STORAGE_BACKEND', | ||
key, | ||
storeName, | ||
}; | ||
}, | ||
|
||
setItem( storeName, key, value ) { | ||
return { | ||
type: 'SET_STORAGE_ITEM', | ||
key, | ||
storeName, | ||
value, | ||
}; | ||
}, | ||
}; | ||
|
||
registerStore( 'storage', { | ||
reducer( state = DEFAULT_STATE, action ) { | ||
switch ( action.type ) { | ||
case 'RETRIEVE_STORAGE_ITEM': | ||
return { | ||
...state, | ||
[ action.storageName ]: { | ||
[ action.key ]: action.value, | ||
}, | ||
}; | ||
} | ||
|
||
return state; | ||
}, | ||
|
||
actions, | ||
|
||
selectors: {}, | ||
|
||
controls: { | ||
GET_STORAGE_ITEM_FROM_STORAGE_BACKEND( action ) { | ||
return createStorage( action.storeName ).getItem( action.key ); | ||
}, | ||
SET_ITEM( action ) { | ||
return createStorage( action.storeName ).setItem( action.key, action.value ); | ||
}, | ||
}, | ||
|
||
resolvers: { | ||
* getItem( state, storeName, key ) { | ||
const value = yield actions.getItemFromStorageBackend( storeName, key ); | ||
|
||
return actions.retrieveStorageItem( storeName, key, value ); | ||
}, | ||
* setItem( state, storeName, key, value ) { | ||
yield actions.setItemInStorageBackend( storeName, key, value ); | ||
|
||
return actions.retrieveStorageItem( storeName, key, value ); | ||
}, | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
// import { next, replace, attrs } from '../'; | ||
|
||
describe( 'storage', () => { | ||
it( 'should work', () => { | ||
expect( true ).toEqual( true ); | ||
} ); | ||
} ); |