UNPKG

jodit

Version:

Jodit is an awesome and useful wysiwyg editor with filebrowser

78 lines (77 loc) 2.25 kB
/*! * Jodit Editor (https://xdsoft.net/jodit/) * Released under MIT see LICENSE.txt in the project root for license information. * Copyright (c) 2013-2026 Valerii Chupurnov. All rights reserved. https://xdsoft.net */ /** * Check if user disable local storages/cookie etc. */ export const canUsePersistentStorage = (strategy = 'localStorage') => { const cache = new Map(); return (() => { if (cache.has(strategy)) { return cache.get(strategy); } const tmpKey = '___Jodit___' + Math.random().toString(); const storage = strategy === 'sessionStorage' ? sessionStorage : localStorage; try { storage.setItem(tmpKey, '1'); const result = storage.getItem(tmpKey) === '1'; storage.removeItem(tmpKey); cache.set(strategy, result); return result; } catch (_a) { } cache.set(strategy, false); return false; })(); }; /** * Persistent storage in localStorage or sessionStorage */ export class LocalStorageProvider { get storage() { return this.strategy === 'sessionStorage' ? sessionStorage : localStorage; } set(key, value) { try { const buffer = this.storage.getItem(this.rootKey); const json = buffer ? JSON.parse(buffer) : {}; json[key] = value; this.storage.setItem(this.rootKey, JSON.stringify(json)); } catch (_a) { } return this; } delete(key) { try { this.storage.removeItem(this.rootKey); } catch (_a) { } return this; } get(key) { try { const buffer = this.storage.getItem(this.rootKey); const json = buffer ? JSON.parse(buffer) : {}; return json[key] !== undefined ? json[key] : undefined; } catch (_a) { } } exists(key) { return this.get(key) != null; } constructor(rootKey, strategy = 'localStorage') { this.rootKey = rootKey; this.strategy = strategy; } clear() { try { this.storage.removeItem(this.rootKey); } catch (_a) { } return this; } }