CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editor-local-storage.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { from_json, to_json } from "@cocalc/util/misc";
7
import {
8
delete_local_storage_prefix,
9
get_local_storage,
10
set_local_storage,
11
LS,
12
} from "./misc/local-storage";
13
14
/*
15
* decaffeinate suggestions:
16
* DS102: Remove unnecessary code created because of implicit returns
17
* DS205: Consider reworking code to avoid use of IIFEs
18
* DS207: Consider shorter variations of null checks
19
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
20
*/
21
22
const SEP = "\uFE10";
23
24
function _local_storage_prefix(
25
project_id: string,
26
filename?: string,
27
key?: string
28
): string {
29
let s = project_id;
30
if (filename != null) {
31
s += filename + SEP;
32
}
33
if (key != null) {
34
s += key;
35
}
36
return s;
37
}
38
39
//
40
// Set or get something about a project from local storage:
41
//
42
// local_storage(project_id): returns everything known about this project.
43
// local_storage(project_id, filename): get everything about given filename in project
44
// local_storage(project_id, filename, key): get value of key for given filename in project
45
// local_storage(project_id, filename, key, value): set value of key
46
//
47
// if localStorage is not supported in this browser, it uses a fallback.
48
//
49
50
export function local_storage_delete(
51
project_id: string,
52
filename?: string,
53
key?: string
54
) {
55
const prefix = _local_storage_prefix(project_id, filename, key);
56
delete_local_storage_prefix(prefix);
57
}
58
59
export function local_storage(
60
project_id: string,
61
filename?: string,
62
key?: string,
63
value?: any
64
) {
65
const prefix = _local_storage_prefix(project_id, filename, key);
66
const n = prefix.length;
67
if (filename != null) {
68
if (key != null) {
69
if (value != null) {
70
set_local_storage(prefix, to_json(value));
71
} else {
72
const x = get_local_storage(prefix);
73
if (x == null) {
74
return x;
75
} else {
76
if (typeof x === "string") {
77
return from_json(x);
78
} else {
79
return x;
80
}
81
}
82
}
83
} else {
84
// Everything about a given filename
85
const obj: any = {};
86
for (const [k, v] of LS) {
87
if (k.slice(0, n) === prefix) {
88
obj[k.split(SEP)[1]] = v;
89
}
90
}
91
return obj;
92
}
93
} else {
94
// Everything about project
95
const obj: any = {};
96
for (const [k, v] of LS) {
97
if (k.slice(0, n) === prefix) {
98
const x = k.slice(n);
99
const [filename, key] = x.split(SEP);
100
if (obj[filename] == null) {
101
obj[filename] = {};
102
}
103
obj[filename][key] = v;
104
}
105
}
106
return obj;
107
}
108
}
109
110