@taybart/corvid 0.1.21 → 0.1.23
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/dist/index.js +15 -19
- package/dist/local_storage.d.ts +10 -1
- package/dist/ls.js +15 -19
- package/dist/utils.js +95 -0
- package/package.json +8 -4
package/dist/index.js
CHANGED
|
@@ -61,11 +61,11 @@ var local_storage_namespaceObject = {};
|
|
|
61
61
|
__webpack_require__.r(local_storage_namespaceObject);
|
|
62
62
|
__webpack_require__.d(local_storage_namespaceObject, {
|
|
63
63
|
clear: ()=>clear,
|
|
64
|
+
default: ()=>local_storage,
|
|
64
65
|
get: ()=>get,
|
|
65
66
|
getJSON: ()=>getJSON,
|
|
66
67
|
listen: ()=>listen,
|
|
67
68
|
set: ()=>set,
|
|
68
|
-
setObj: ()=>setObj,
|
|
69
69
|
unlisten: ()=>unlisten,
|
|
70
70
|
update: ()=>local_storage_update
|
|
71
71
|
});
|
|
@@ -708,6 +708,7 @@ function getJSON(key, _default) {
|
|
|
708
708
|
function local_storage_update(key, update1, broadcast = false) {
|
|
709
709
|
const prev = get(key);
|
|
710
710
|
const value = update1(prev);
|
|
711
|
+
localStorage.setItem(key, value);
|
|
711
712
|
if (prev !== value || broadcast) {
|
|
712
713
|
const event = new CustomEvent('@corvid/ls-update', {
|
|
713
714
|
detail: {
|
|
@@ -717,9 +718,11 @@ function local_storage_update(key, update1, broadcast = false) {
|
|
|
717
718
|
});
|
|
718
719
|
document.dispatchEvent(event);
|
|
719
720
|
}
|
|
720
|
-
localStorage.setItem(key, value);
|
|
721
721
|
}
|
|
722
722
|
function set(key, value, broadcast = false) {
|
|
723
|
+
if ('object' == typeof value) value = JSON.stringify(value);
|
|
724
|
+
console.log('set');
|
|
725
|
+
localStorage.setItem(key, value);
|
|
723
726
|
const prev = get(key);
|
|
724
727
|
if (prev !== value || broadcast) {
|
|
725
728
|
const event = new CustomEvent('@corvid/ls-update', {
|
|
@@ -728,25 +731,9 @@ function set(key, value, broadcast = false) {
|
|
|
728
731
|
value
|
|
729
732
|
}
|
|
730
733
|
});
|
|
734
|
+
console.log('broadcast');
|
|
731
735
|
document.dispatchEvent(event);
|
|
732
736
|
}
|
|
733
|
-
if ('object' == typeof value) value = JSON.stringify(value);
|
|
734
|
-
localStorage.setItem(key, value);
|
|
735
|
-
}
|
|
736
|
-
function setObj(update, prefix, broadcast = false) {
|
|
737
|
-
const flatten = (ob)=>{
|
|
738
|
-
const ret = {};
|
|
739
|
-
for(let i in ob)if (ob.hasOwnProperty(i)) if ('object' == typeof ob[i] && null !== ob[i]) {
|
|
740
|
-
const flat = flatten(ob[i]);
|
|
741
|
-
for(let x in flat)if (flat.hasOwnProperty(x)) ret[`${i}.${x}`] = flat[x];
|
|
742
|
-
} else ret[i] = ob[i];
|
|
743
|
-
return ret;
|
|
744
|
-
};
|
|
745
|
-
for (let [k, v] of Object.entries(flatten(update))){
|
|
746
|
-
let key = k;
|
|
747
|
-
if (prefix) key = `${prefix}.${k}`;
|
|
748
|
-
set(key, v, broadcast);
|
|
749
|
-
}
|
|
750
737
|
}
|
|
751
738
|
const listeners = new Map();
|
|
752
739
|
function listen(key, cb) {
|
|
@@ -778,4 +765,13 @@ function unlisten(key, cb) {
|
|
|
778
765
|
function clear(key) {
|
|
779
766
|
localStorage.removeItem(key);
|
|
780
767
|
}
|
|
768
|
+
const local_storage = {
|
|
769
|
+
get,
|
|
770
|
+
getJSON,
|
|
771
|
+
set,
|
|
772
|
+
update: local_storage_update,
|
|
773
|
+
listen,
|
|
774
|
+
unlisten,
|
|
775
|
+
clear
|
|
776
|
+
};
|
|
781
777
|
export { clipboard, dom_namespaceObject as dom, genID, utils_logLevel as logLevel, logger, local_storage_namespaceObject as ls, network_namespaceObject as network, strings_namespaceObject as strings, style_namespaceObject as style };
|
package/dist/local_storage.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export declare function get(key: string, _default?: any): any;
|
|
|
3
3
|
export declare function getJSON(key: string, _default?: any): any;
|
|
4
4
|
export declare function update(key: string, update: (current: any) => any, broadcast?: boolean): void;
|
|
5
5
|
export declare function set(key: string, value: any, broadcast?: boolean): void;
|
|
6
|
-
export declare function setObj(update: object, prefix?: string, broadcast?: boolean): void;
|
|
7
6
|
export declare function listen(key: string, cb: (update: {
|
|
8
7
|
key: string;
|
|
9
8
|
value: any;
|
|
@@ -13,3 +12,13 @@ export declare function unlisten(key: string, cb: (update: {
|
|
|
13
12
|
value: any;
|
|
14
13
|
}) => void | el): void;
|
|
15
14
|
export declare function clear(key: string): void;
|
|
15
|
+
declare const _default_1: {
|
|
16
|
+
get: typeof get;
|
|
17
|
+
getJSON: typeof getJSON;
|
|
18
|
+
set: typeof set;
|
|
19
|
+
update: typeof update;
|
|
20
|
+
listen: typeof listen;
|
|
21
|
+
unlisten: typeof unlisten;
|
|
22
|
+
clear: typeof clear;
|
|
23
|
+
};
|
|
24
|
+
export default _default_1;
|
package/dist/ls.js
CHANGED
|
@@ -263,6 +263,7 @@ function getJSON(key, _default) {
|
|
|
263
263
|
function local_storage_update(key, update1, broadcast = false) {
|
|
264
264
|
const prev = get(key);
|
|
265
265
|
const value = update1(prev);
|
|
266
|
+
localStorage.setItem(key, value);
|
|
266
267
|
if (prev !== value || broadcast) {
|
|
267
268
|
const event = new CustomEvent('@corvid/ls-update', {
|
|
268
269
|
detail: {
|
|
@@ -272,9 +273,11 @@ function local_storage_update(key, update1, broadcast = false) {
|
|
|
272
273
|
});
|
|
273
274
|
document.dispatchEvent(event);
|
|
274
275
|
}
|
|
275
|
-
localStorage.setItem(key, value);
|
|
276
276
|
}
|
|
277
277
|
function set(key, value, broadcast = false) {
|
|
278
|
+
if ('object' == typeof value) value = JSON.stringify(value);
|
|
279
|
+
console.log('set');
|
|
280
|
+
localStorage.setItem(key, value);
|
|
278
281
|
const prev = get(key);
|
|
279
282
|
if (prev !== value || broadcast) {
|
|
280
283
|
const event = new CustomEvent('@corvid/ls-update', {
|
|
@@ -283,25 +286,9 @@ function set(key, value, broadcast = false) {
|
|
|
283
286
|
value
|
|
284
287
|
}
|
|
285
288
|
});
|
|
289
|
+
console.log('broadcast');
|
|
286
290
|
document.dispatchEvent(event);
|
|
287
291
|
}
|
|
288
|
-
if ('object' == typeof value) value = JSON.stringify(value);
|
|
289
|
-
localStorage.setItem(key, value);
|
|
290
|
-
}
|
|
291
|
-
function setObj(update, prefix, broadcast = false) {
|
|
292
|
-
const flatten = (ob)=>{
|
|
293
|
-
const ret = {};
|
|
294
|
-
for(let i in ob)if (ob.hasOwnProperty(i)) if ('object' == typeof ob[i] && null !== ob[i]) {
|
|
295
|
-
const flat = flatten(ob[i]);
|
|
296
|
-
for(let x in flat)if (flat.hasOwnProperty(x)) ret[`${i}.${x}`] = flat[x];
|
|
297
|
-
} else ret[i] = ob[i];
|
|
298
|
-
return ret;
|
|
299
|
-
};
|
|
300
|
-
for (let [k, v] of Object.entries(flatten(update))){
|
|
301
|
-
let key = k;
|
|
302
|
-
if (prefix) key = `${prefix}.${k}`;
|
|
303
|
-
set(key, v, broadcast);
|
|
304
|
-
}
|
|
305
292
|
}
|
|
306
293
|
const listeners = new Map();
|
|
307
294
|
function listen(key, cb) {
|
|
@@ -333,4 +320,13 @@ function unlisten(key, cb) {
|
|
|
333
320
|
function clear(key) {
|
|
334
321
|
localStorage.removeItem(key);
|
|
335
322
|
}
|
|
336
|
-
|
|
323
|
+
const local_storage = {
|
|
324
|
+
get,
|
|
325
|
+
getJSON,
|
|
326
|
+
set,
|
|
327
|
+
update: local_storage_update,
|
|
328
|
+
listen,
|
|
329
|
+
unlisten,
|
|
330
|
+
clear
|
|
331
|
+
};
|
|
332
|
+
export { clear, local_storage as default, get, getJSON, listen, set, unlisten, local_storage_update as update };
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
3
|
+
value: value,
|
|
4
|
+
enumerable: true,
|
|
5
|
+
configurable: true,
|
|
6
|
+
writable: true
|
|
7
|
+
});
|
|
8
|
+
else obj[key] = value;
|
|
9
|
+
return obj;
|
|
10
|
+
}
|
|
11
|
+
function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
|
|
12
|
+
return [
|
|
13
|
+
...crypto.getRandomValues(new Uint8Array(len))
|
|
14
|
+
].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
|
|
15
|
+
}
|
|
16
|
+
const clipboard = {
|
|
17
|
+
check () {
|
|
18
|
+
if (!navigator.clipboard) throw new Error('Clipboard API not supported, or context is not https');
|
|
19
|
+
},
|
|
20
|
+
async copy (text) {
|
|
21
|
+
this.check();
|
|
22
|
+
await navigator.clipboard.writeText(text);
|
|
23
|
+
},
|
|
24
|
+
async copyArbitrary (data) {
|
|
25
|
+
this.check();
|
|
26
|
+
await navigator.clipboard.write(data);
|
|
27
|
+
},
|
|
28
|
+
async read () {
|
|
29
|
+
this.check();
|
|
30
|
+
return await navigator.clipboard.readText();
|
|
31
|
+
},
|
|
32
|
+
async readArbitrary () {
|
|
33
|
+
this.check();
|
|
34
|
+
return await navigator.clipboard.read();
|
|
35
|
+
},
|
|
36
|
+
listen (query, cb) {
|
|
37
|
+
let el;
|
|
38
|
+
el = 'string' == typeof query ? document.querySelector(query) : query;
|
|
39
|
+
if (!el) throw new Error(`no element from query: ${query}`);
|
|
40
|
+
el.addEventListener('copy', (ev)=>{
|
|
41
|
+
const cbEv = ev;
|
|
42
|
+
const selection = document.getSelection();
|
|
43
|
+
if (selection) {
|
|
44
|
+
const text = selection.toString();
|
|
45
|
+
if (text && cbEv.clipboardData) cbEv.clipboardData.setData('text/plain', cb(text));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var utils_logLevel = /*#__PURE__*/ function(logLevel) {
|
|
51
|
+
logLevel[logLevel["none"] = -1] = "none";
|
|
52
|
+
logLevel[logLevel["error"] = 0] = "error";
|
|
53
|
+
logLevel[logLevel["warn"] = 1] = "warn";
|
|
54
|
+
logLevel[logLevel["info"] = 2] = "info";
|
|
55
|
+
logLevel[logLevel["debug"] = 3] = "debug";
|
|
56
|
+
logLevel[logLevel["trace"] = 4] = "trace";
|
|
57
|
+
return logLevel;
|
|
58
|
+
}({});
|
|
59
|
+
class logger {
|
|
60
|
+
error(...args) {
|
|
61
|
+
if (this.level >= 0) console.error(`[corvid] ${this.prefix}`, ...args);
|
|
62
|
+
}
|
|
63
|
+
warn(...args) {
|
|
64
|
+
if (this.level >= 1) console.warn(`[corvid] ${this.prefix}`, ...args);
|
|
65
|
+
}
|
|
66
|
+
info(...args) {
|
|
67
|
+
if (this.level >= 2) console.info(`[corvid] ${this.prefix}`, ...args);
|
|
68
|
+
}
|
|
69
|
+
debug(...args) {
|
|
70
|
+
if (this.level >= 3) console.debug(`[corvid] ${this.prefix}`, ...args);
|
|
71
|
+
}
|
|
72
|
+
trace(...args) {
|
|
73
|
+
if (this.level >= 4) console.trace(`[corvid] ${this.prefix}`, ...args);
|
|
74
|
+
}
|
|
75
|
+
log(...args) {
|
|
76
|
+
console.log(`[corvid] ${this.prefix}`, ...args);
|
|
77
|
+
}
|
|
78
|
+
constructor(level = 2, prefix){
|
|
79
|
+
_define_property(this, "level", void 0);
|
|
80
|
+
_define_property(this, "prefix", void 0);
|
|
81
|
+
this.level = level;
|
|
82
|
+
this.prefix = prefix ? `(${prefix}):` : ':';
|
|
83
|
+
if (-1 === this.level) [
|
|
84
|
+
'error',
|
|
85
|
+
'warn',
|
|
86
|
+
'info',
|
|
87
|
+
'debug',
|
|
88
|
+
'trace',
|
|
89
|
+
'log'
|
|
90
|
+
].forEach((methodName)=>{
|
|
91
|
+
this[methodName] = ()=>{};
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export { clipboard, genID, utils_logLevel as logLevel, logger };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taybart/corvid",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.23",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"types": "./dist/style.d.ts",
|
|
17
17
|
"import": "./dist/style.js"
|
|
18
18
|
},
|
|
19
|
-
"./
|
|
20
|
-
"types": "./dist/
|
|
19
|
+
"./ls": {
|
|
20
|
+
"types": "./dist/local_storage.d.ts",
|
|
21
21
|
"import": "./dist/ls.js"
|
|
22
22
|
},
|
|
23
23
|
"./network": {
|
|
@@ -25,8 +25,12 @@
|
|
|
25
25
|
"import": "./dist/network.js"
|
|
26
26
|
},
|
|
27
27
|
"./qr": {
|
|
28
|
-
"types": "./dist/qr.d.ts",
|
|
28
|
+
"types": "./dist/qr/index.d.ts",
|
|
29
29
|
"import": "./dist/qr.js"
|
|
30
|
+
},
|
|
31
|
+
"./utils": {
|
|
32
|
+
"types": "./dist/utils.d.ts",
|
|
33
|
+
"import": "./dist/utils.js"
|
|
30
34
|
}
|
|
31
35
|
},
|
|
32
36
|
"module": "./dist/index.js",
|