Skip to content

Commit 20771e4

Browse files
authored
Feature/lscache (#136)
* v14.0.0 * Use lscache to store content * Fix missing refs
1 parent 3b620df commit 20771e4

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

examples/editor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from "react"
2+
import lscache from 'lscache';
23

4+
import {EXAMPLE_VALUE} from "./constants"
35
import {
46
//
57
// editor compoent itself
@@ -8,7 +10,6 @@ import {
810
//
911
} from "../src/index"
1012
import {TestPlugin} from "./plugin"
11-
import {EXAMPLE_VALUE} from "./constants"
1213
import //
1314
// picture component that you will need to pass as a prop in order to
1415
// render images within your documents; you can create your own Picture
@@ -66,11 +67,11 @@ export class Editor extends React.PureComponent {
6667
// in localStorage. In that case, an EXAMPLE_VALUE content is to be
6768
// loaded. However, once it's modified, the saved version will be
6869
// loaded every time. Note that
69-
// localStorage.getItem("composer-content-state") is never NULL, even
70+
// lscache.get("composer-content-state") is never NULL, even
7071
// for empty documents after first auto save, since even an empty
7172
// document has a structure that's stored in the LS.
7273
value={
73-
localStorage.getItem("composer-content-state")
74+
lscache.get("composer-content-state")
7475
? null
7576
: EXAMPLE_VALUE
7677
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@roast-cms/french-press-editor",
3-
"version": "13.2.1",
3+
"version": "14.0.0",
44
"description": "An offline-first rich text editor component.",
55
"main": "dist/index.js",
66
"repository": "https://github.com/roast-cms/french-press-editor.git",
@@ -70,6 +70,7 @@
7070
"localforage": "^1.7.1",
7171
"localforage-getitems": "^1.4.1",
7272
"lodash": "^4.17.4",
73+
"lscache": "^1.3.0",
7374
"positions": "^1.6.1",
7475
"slate": "0.36.2",
7576
"slate-auto-replace-legacy": "^0.9.0",

src/utils/storage.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import lscache from "lscache"
12
import throttle from "lodash/throttle"
23

34
import {DEFAULT_EDITOR_STATE} from "../constants/defaults"
@@ -7,16 +8,15 @@ import {DEFAULT_EDITOR_STATE} from "../constants/defaults"
78
* @module loadContent
89
*/
910
export const loadContent = () => {
10-
let local = localStorage.getItem("composer-content-state")
11-
return local ? JSON.parse(local) : DEFAULT_EDITOR_STATE
11+
return lscache.get("composer-content-state") || DEFAULT_EDITOR_STATE
1212
}
1313

1414
/**
1515
* Loads stored text version of user's document from localStorage.
1616
* @module loadTextContent
1717
*/
1818
export const loadTextContent = () => {
19-
return localStorage.getItem("composer-content-text") || ""
19+
return lscache.get("composer-content-text") || ""
2020
}
2121
//
2222
// functions that store content onto localStorage
@@ -27,8 +27,7 @@ export const loadTextContent = () => {
2727
* @param {Object} json
2828
*/
2929
export const storeContentState = json => {
30-
const contentState = JSON.stringify(json)
31-
localStorage.setItem("composer-content-state", contentState)
30+
lscache.set("composer-content-state", json)
3231
}
3332

3433
/**
@@ -41,7 +40,7 @@ export const storeContentState = json => {
4140
*/
4241
export const saveContent = throttle((document, state, callbackStatus) => {
4342
storeContentState(state.toJSON())
44-
localStorage.setItem("composer-content-text", state.document.text)
43+
lscache.set("composer-content-text", state.document.text)
4544
callbackStatus && callbackStatus("ok")
4645
}, 3000)
4746

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5675,6 +5675,11 @@ lru-cache@^5.1.1:
56755675
dependencies:
56765676
yallist "^3.0.2"
56775677

5678+
lscache@^1.3.0:
5679+
version "1.3.0"
5680+
resolved "https://registry.yarnpkg.com/lscache/-/lscache-1.3.0.tgz#2221fd39f3393d2dfbc6ed2231beb52e11e43396"
5681+
integrity sha512-0JwzMSSu3fd3m8QQVbqIxzXywkNLQvgdNehuEtZ66v7f89ybpkZX+WN45SkvChP4AqUPSpDPJKHsAqStOhHgUA==
5682+
56785683
make-dir@^2.0.0, make-dir@^2.1.0:
56795684
version "2.1.0"
56805685
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"

0 commit comments

Comments
 (0)