UNPKG

web-highlighter

Version:

✨A no-runtime dependency lib for text highlighting & persistence on any website ✨🖍️

72 lines (62 loc) 1.62 kB
class LocalStore { constructor(id) { this.key = id !== undefined ? `highlight-mengshou-${id}` : 'highlight-mengshou'; } storeToJson() { const store = localStorage.getItem(this.key); let sources; try { sources = JSON.parse(store) || []; } catch (e) { sources = []; } return sources; } jsonToStore(stores) { localStorage.setItem(this.key, JSON.stringify(stores)); } save(data) { const stores = this.storeToJson(); const map = {}; stores.forEach((store, idx) => map[store.hs.id] = idx); if (!Array.isArray(data)) { data = [data]; } data.forEach(store => { // update if (map[store.hs.id] !== undefined) { stores[map[store.hs.id]] = store; } // append else { stores.push(store); } }) this.jsonToStore(stores); } forceSave(store) { const stores = this.storeToJson(); stores.push(store); this.jsonToStore(stores); } remove(id) { const stores = this.storeToJson(); let index = null; for (let i = 0; i < stores.length; i++) { if (stores[i].hs.id === id) { index = i; break; } } stores.splice(index, 1); this.jsonToStore(stores); } getAll() { return this.storeToJson(); } removeAll() { this.jsonToStore([]); } } export default LocalStore;