@cocreate/local-storage 1.16.4 → 1.16.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +301 -235
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.16.5](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.16.4...v1.16.5) (2026-03-05)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* storage support for local and session storage, added fallback to Map for unsupported browsers ([9774762](https://github.com/CoCreate-app/CoCreate-local-storage/commit/9774762ed66298adaec453804835211930269519))
|
|
7
|
+
|
|
1
8
|
## [1.16.4](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.16.3...v1.16.4) (2026-02-04)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/local-storage",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.5",
|
|
4
4
|
"description": "A Simple component similar to CoCreate-input & CoCreate-fetch which Save and fetch data for inputs, textarea, selects from localstorage. Easily configured using HTML5 attributes and/or JavaScript API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"local-storage",
|
package/src/index.js
CHANGED
|
@@ -1,247 +1,313 @@
|
|
|
1
1
|
import Observer from "@cocreate/observer";
|
|
2
2
|
import Actions from "@cocreate/actions";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
4
|
+
let support = true;
|
|
5
|
+
const storage = new Map();
|
|
6
|
+
|
|
7
|
+
function init() {
|
|
8
|
+
var elements = document.querySelectorAll(
|
|
9
|
+
"[localstorage-set], [localstorage-get], [sessionstorage-set], [sessionstorage-get]"
|
|
10
|
+
);
|
|
11
|
+
initElements(elements);
|
|
12
|
+
|
|
13
|
+
window.addEventListener("storage", function (e) {
|
|
14
|
+
// Determine which storage type triggered the event
|
|
15
|
+
const type = e.storageArea === window.sessionStorage ? "sessionstorage" : "localstorage";
|
|
16
|
+
|
|
17
|
+
elements = document.querySelectorAll(
|
|
18
|
+
`[${type}-get="${e.key}"]`
|
|
19
|
+
);
|
|
20
|
+
for (let element of elements) {
|
|
21
|
+
// TODO: Decide how best to handle considering storage is also handled by event.
|
|
22
|
+
if (
|
|
23
|
+
element.hasAttribute(`${type}-attribute`) ||
|
|
24
|
+
element.hasAttribute(`${type}-value`) ||
|
|
25
|
+
element.hasAttribute(`${type}-key`)
|
|
26
|
+
)
|
|
27
|
+
return;
|
|
28
|
+
let value = getItem(e.key, type);
|
|
29
|
+
if (value != null) {
|
|
30
|
+
element.setValue(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Initializes a collection of elements.
|
|
38
|
+
* @param {Array<Element>} elements - An array of elements to be initialized.
|
|
39
|
+
*/
|
|
40
|
+
function initElements(elements) {
|
|
41
|
+
for (let element of elements) initElement(element);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Initializes a single element.
|
|
46
|
+
* @param {Element} element - The element to initialize. This function expects an 'Element' type.
|
|
47
|
+
*/
|
|
48
|
+
function initElement(element) {
|
|
49
|
+
const types = ["localstorage", "sessionstorage"];
|
|
50
|
+
let hasValue = false;
|
|
51
|
+
|
|
52
|
+
for (let type of types) {
|
|
53
|
+
let key = element.getAttribute(`${type}-get`) || element.getAttribute(`${type}-set`);
|
|
54
|
+
if (!key) continue;
|
|
55
|
+
|
|
56
|
+
let value = getItem(key, type);
|
|
57
|
+
if (value) {
|
|
58
|
+
element.setValue(value);
|
|
59
|
+
hasValue = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!hasValue) {
|
|
64
|
+
let inputEvent = new CustomEvent("input", {
|
|
65
|
+
bubbles: true
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
Object.defineProperty(inputEvent, "target", {
|
|
69
|
+
writable: false,
|
|
70
|
+
value: element
|
|
71
|
+
});
|
|
72
|
+
element.dispatchEvent(inputEvent);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
element.addEventListener("input", (e) => {
|
|
76
|
+
let isRealtime = element.getAttribute("realtime");
|
|
77
|
+
// Returns if realtime false.
|
|
78
|
+
if (!isRealtime && isRealtime === "false") return;
|
|
79
|
+
|
|
80
|
+
for (let type of types) {
|
|
81
|
+
let currentKey = e.target.getAttribute(`${type}-set`);
|
|
82
|
+
if (!currentKey) continue;
|
|
83
|
+
|
|
84
|
+
// Set the key and value in the respective storage.
|
|
85
|
+
setItem(currentKey, e.target.value, type);
|
|
86
|
+
|
|
87
|
+
let elements = document.querySelectorAll(
|
|
88
|
+
`[${type}-get="${currentKey}"]`
|
|
89
|
+
);
|
|
90
|
+
for (let el of elements) {
|
|
91
|
+
if (el != element) {
|
|
92
|
+
let value = getItem(currentKey, type);
|
|
93
|
+
// Set the value of the element.
|
|
94
|
+
if (value != null) {
|
|
95
|
+
el.setValue(value);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param key
|
|
105
|
+
* @param type - 'localstorage' or 'sessionstorage'
|
|
106
|
+
* @return { Mixed } The value associated with the key or undefined if not found.
|
|
107
|
+
*/
|
|
108
|
+
function getItem(key, type = "localstorage") {
|
|
109
|
+
if (support) {
|
|
110
|
+
return type === "sessionstorage" ? window.sessionStorage.getItem(key) : window.localStorage.getItem(key);
|
|
111
|
+
} else {
|
|
112
|
+
return storage.get(`${type}-${key}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param key - key name to store the value under
|
|
118
|
+
* @param value - value to store
|
|
119
|
+
* @param type - 'localstorage' or 'sessionstorage'
|
|
120
|
+
*/
|
|
121
|
+
function setItem(key, value, type = "localstorage") {
|
|
122
|
+
try {
|
|
123
|
+
if (support) {
|
|
124
|
+
const storageObj = type === "sessionstorage" ? window.sessionStorage : window.localStorage;
|
|
125
|
+
const oldValue = storageObj.getItem(key);
|
|
126
|
+
|
|
127
|
+
storageObj.setItem(key, value);
|
|
128
|
+
|
|
129
|
+
const keys = [
|
|
130
|
+
"organization_id",
|
|
131
|
+
"user_id",
|
|
132
|
+
"clientId",
|
|
133
|
+
"session_id"
|
|
134
|
+
];
|
|
135
|
+
if (keys.includes(key)) {
|
|
136
|
+
const updateEvent = new CustomEvent("updateAttributes", {
|
|
137
|
+
detail: { key, newValue: value, oldValue, type }
|
|
138
|
+
});
|
|
139
|
+
window.dispatchEvent(updateEvent);
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
storage.set(`${type}-${key}`, value);
|
|
143
|
+
}
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (
|
|
146
|
+
error instanceof DOMException &&
|
|
147
|
+
(error.code === 22 ||
|
|
148
|
+
error.code === 1014 ||
|
|
149
|
+
error.name === "QuotaExceededError" ||
|
|
150
|
+
error.name === "NS_ERROR_DOM_QUOTA_REACHED")
|
|
151
|
+
) {
|
|
152
|
+
console.log(
|
|
153
|
+
"Storage limit exceeded. Falling back to Map."
|
|
154
|
+
);
|
|
155
|
+
} else {
|
|
156
|
+
console.error("Error setting item:", error);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param key - The key name to delete from the storage
|
|
163
|
+
* @param type - 'localstorage' or 'sessionstorage'
|
|
164
|
+
*/
|
|
165
|
+
function removeItem(key, type = "localstorage") {
|
|
166
|
+
if (support) {
|
|
167
|
+
if (type === "sessionstorage") window.sessionStorage.removeItem(key);
|
|
168
|
+
else window.localStorage.removeItem(key);
|
|
169
|
+
} else {
|
|
170
|
+
storage.delete(`${type}-${key}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @param action
|
|
176
|
+
*
|
|
177
|
+
* @return { undefined } No return
|
|
178
|
+
*/
|
|
179
|
+
async function runStorage(action) {
|
|
180
|
+
const form = action.form;
|
|
181
|
+
if (!form) return;
|
|
182
|
+
|
|
183
|
+
const types = ["localstorage", "sessionstorage"];
|
|
184
|
+
|
|
185
|
+
for (let type of types) {
|
|
186
|
+
let set_els = form.querySelectorAll(`[${type}-set]`);
|
|
187
|
+
for (let el of set_els) {
|
|
188
|
+
let key = el.getAttribute(`${type}-set`);
|
|
189
|
+
let value = await el.getValue();
|
|
190
|
+
if (value && key) {
|
|
191
|
+
setItem(key, value, type);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let get_els = form.querySelectorAll(`[${type}-get]`);
|
|
196
|
+
for (let el of get_els) {
|
|
197
|
+
let key = el.getAttribute(`${type}-get`);
|
|
198
|
+
if (!key) continue;
|
|
199
|
+
let value = getItem(key, type);
|
|
200
|
+
if (value != null) {
|
|
201
|
+
el.setValue(value);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let remove_els = form.querySelectorAll(`[${type}-remove]`);
|
|
206
|
+
for (let el of remove_els) {
|
|
207
|
+
el.setValue("");
|
|
208
|
+
let key = el.getAttribute(`${type}-remove`);
|
|
209
|
+
if (key) {
|
|
210
|
+
removeItem(key, type);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
action.element.dispatchEvent(
|
|
216
|
+
new CustomEvent("localStorage", {
|
|
217
|
+
detail: {}
|
|
218
|
+
})
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const localStorage = {
|
|
223
|
+
getItem: (key) => getItem(key, "localstorage"),
|
|
224
|
+
setItem: (key, value) => setItem(key, value, "localstorage"),
|
|
225
|
+
removeItem: (key) => removeItem(key, "localstorage")
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const sessionStorage = {
|
|
229
|
+
getItem: (key) => getItem(key, "sessionstorage"),
|
|
230
|
+
setItem: (key, value) => setItem(key, value, "sessionstorage"),
|
|
231
|
+
removeItem: (key) => removeItem(key, "sessionstorage")
|
|
216
232
|
};
|
|
217
233
|
|
|
234
|
+
function checkSupport() {
|
|
235
|
+
// Check if the browser supports Web Storage.
|
|
236
|
+
if (!("localStorage" in window) || !("sessionStorage" in window)) {
|
|
237
|
+
console.log("This browser doesn't support Web Storage.");
|
|
238
|
+
support = false;
|
|
239
|
+
} else {
|
|
240
|
+
try {
|
|
241
|
+
window.localStorage.getItem("test");
|
|
242
|
+
window.sessionStorage.getItem("test");
|
|
243
|
+
if (window.CoCreateConfig && window.CoCreateConfig.localStorage === false)
|
|
244
|
+
support = false;
|
|
245
|
+
} catch (e) {
|
|
246
|
+
support = false;
|
|
247
|
+
} finally {
|
|
248
|
+
if (support) init();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
218
253
|
Actions.init(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
254
|
+
{
|
|
255
|
+
name: "localStorage",
|
|
256
|
+
endEvent: "localStorage",
|
|
257
|
+
callback: (data) => {
|
|
258
|
+
runStorage(data);
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
name: "localStorageRemove",
|
|
263
|
+
endEvent: "localStorageRemoved",
|
|
264
|
+
callback: (data) => {
|
|
265
|
+
runStorage(data);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
name: "sessionStorage",
|
|
270
|
+
endEvent: "sessionStorage",
|
|
271
|
+
callback: (data) => {
|
|
272
|
+
runStorage(data);
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
name: "sessionStorageRemove",
|
|
277
|
+
endEvent: "sessionStorageRemoved",
|
|
278
|
+
callback: (data) => {
|
|
279
|
+
runStorage(data);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
233
282
|
);
|
|
234
283
|
|
|
235
284
|
Observer.init({
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
285
|
+
types: ["addedNodes"],
|
|
286
|
+
selector: "[localstorage-set], [sessionstorage-set]",
|
|
287
|
+
callback: async function (mutation) {
|
|
288
|
+
const types = ["localstorage", "sessionstorage"];
|
|
289
|
+
for (let type of types) {
|
|
290
|
+
let key = mutation.target.getAttribute(`${type}-set`);
|
|
291
|
+
if (key) {
|
|
292
|
+
let value = await mutation.target.getValue();
|
|
293
|
+
if (value) setItem(key, value, type);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
243
297
|
});
|
|
244
298
|
|
|
245
|
-
|
|
299
|
+
checkSupport();
|
|
300
|
+
|
|
246
301
|
|
|
247
|
-
export default
|
|
302
|
+
export default {
|
|
303
|
+
init,
|
|
304
|
+
initElements,
|
|
305
|
+
initElement,
|
|
306
|
+
getItem,
|
|
307
|
+
setItem,
|
|
308
|
+
removeItem,
|
|
309
|
+
runStorage,
|
|
310
|
+
checkSupport,
|
|
311
|
+
localStorage,
|
|
312
|
+
sessionStorage
|
|
313
|
+
};
|