Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/editor-local-storage.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { from_json, to_json } from "@cocalc/util/misc";6import {7delete_local_storage_prefix,8get_local_storage,9set_local_storage,10LS,11} from "./misc/local-storage";1213/*14* decaffeinate suggestions:15* DS102: Remove unnecessary code created because of implicit returns16* DS205: Consider reworking code to avoid use of IIFEs17* DS207: Consider shorter variations of null checks18* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md19*/2021const SEP = "\uFE10";2223function _local_storage_prefix(24project_id: string,25filename?: string,26key?: string27): string {28let s = project_id;29if (filename != null) {30s += filename + SEP;31}32if (key != null) {33s += key;34}35return s;36}3738//39// Set or get something about a project from local storage:40//41// local_storage(project_id): returns everything known about this project.42// local_storage(project_id, filename): get everything about given filename in project43// local_storage(project_id, filename, key): get value of key for given filename in project44// local_storage(project_id, filename, key, value): set value of key45//46// if localStorage is not supported in this browser, it uses a fallback.47//4849export function local_storage_delete(50project_id: string,51filename?: string,52key?: string53) {54const prefix = _local_storage_prefix(project_id, filename, key);55delete_local_storage_prefix(prefix);56}5758export function local_storage(59project_id: string,60filename?: string,61key?: string,62value?: any63) {64const prefix = _local_storage_prefix(project_id, filename, key);65const n = prefix.length;66if (filename != null) {67if (key != null) {68if (value != null) {69set_local_storage(prefix, to_json(value));70} else {71const x = get_local_storage(prefix);72if (x == null) {73return x;74} else {75if (typeof x === "string") {76return from_json(x);77} else {78return x;79}80}81}82} else {83// Everything about a given filename84const obj: any = {};85for (const [k, v] of LS) {86if (k.slice(0, n) === prefix) {87obj[k.split(SEP)[1]] = v;88}89}90return obj;91}92} else {93// Everything about project94const obj: any = {};95for (const [k, v] of LS) {96if (k.slice(0, n) === prefix) {97const x = k.slice(n);98const [filename, key] = x.split(SEP);99if (obj[filename] == null) {100obj[filename] = {};101}102obj[filename][key] = v;103}104}105return obj;106}107}108109110