pubo-web 1.0.206 → 1.0.210
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/es/dom/drag.d.ts +6 -20
- package/es/dom/drag.js +11 -19
- package/es/file/parser.d.ts +3 -3
- package/es/file/parser.js +11 -7
- package/es/file/pick.js +4 -3
- package/es/load-script/index.d.ts +5 -5
- package/es/load-script/index.js +8 -9
- package/es/storage/cookie.d.ts +5 -2
- package/es/storage/cookie.js +13 -4
- package/es/storage/index.d.ts +3 -3
- package/es/storage/index.js +4 -7
- package/es/storage/indexed-db/indexed-db-storage.d.ts +4 -4
- package/es/storage/indexed-db/indexed-db-storage.js +7 -8
- package/es/storage/indexed-db/indexed-db-utils.d.ts +7 -8
- package/es/storage/indexed-db/indexed-db-utils.js +16 -27
- package/es/websocket/client.d.ts +4 -2
- package/es/websocket/client.js +7 -10
- package/lib/dom/drag.d.ts +6 -20
- package/lib/dom/drag.js +11 -19
- package/lib/file/parser.d.ts +3 -3
- package/lib/file/parser.js +11 -7
- package/lib/file/pick.js +4 -3
- package/lib/load-script/index.d.ts +5 -5
- package/lib/load-script/index.js +9 -12
- package/lib/storage/cookie.d.ts +5 -2
- package/lib/storage/cookie.js +13 -4
- package/lib/storage/index.d.ts +3 -3
- package/lib/storage/index.js +4 -7
- package/lib/storage/indexed-db/indexed-db-storage.d.ts +4 -4
- package/lib/storage/indexed-db/indexed-db-storage.js +7 -8
- package/lib/storage/indexed-db/indexed-db-utils.d.ts +7 -8
- package/lib/storage/indexed-db/indexed-db-utils.js +16 -27
- package/lib/websocket/client.d.ts +4 -2
- package/lib/websocket/client.js +7 -10
- package/package.json +5 -11
- package/dist/pubo-web.js +0 -1
package/es/dom/drag.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
interface DragEvent {
|
|
2
2
|
offsetX: number;
|
|
3
3
|
offsetY: number;
|
|
4
4
|
key?: string;
|
|
@@ -6,29 +6,15 @@ type OnMove = (n: {
|
|
|
6
6
|
pageY: number;
|
|
7
7
|
startX: number;
|
|
8
8
|
startY: number;
|
|
9
|
-
}
|
|
9
|
+
}
|
|
10
|
+
type OnMove = (event: DragEvent) => void;
|
|
10
11
|
type OnMoveEnd = () => void;
|
|
11
12
|
interface DragMethodProps {
|
|
12
13
|
key?: string;
|
|
13
|
-
/**
|
|
14
|
-
* Tracks the movement of the drag and triggers the onMove callback.
|
|
15
|
-
* @callback OnMove
|
|
16
|
-
* @param {object} options - The options object.
|
|
17
|
-
* @param {number} options.offsetX - The X offset of the drag.
|
|
18
|
-
* @param {number} options.offsetY - The Y offset of the drag.
|
|
19
|
-
* @param {number} options.pageX - The X coordinate of the drag.
|
|
20
|
-
* @param {number} options.pageY - The Y coordinate of the drag.
|
|
21
|
-
* @param {number} options.startX - The initial X coordinate of the drag.
|
|
22
|
-
* @param {number} options.startY - The initial Y coordinate of the drag.
|
|
23
|
-
* @param {string} options.key - The key associated with the drag.
|
|
24
|
-
*/
|
|
25
14
|
onMove?: OnMove;
|
|
26
|
-
/**
|
|
27
|
-
* Ends the drag event and triggers the onMoveEnd callback.
|
|
28
|
-
* @callback OnMoveEnd
|
|
29
|
-
*/
|
|
30
15
|
onMoveEnd?: OnMoveEnd;
|
|
31
16
|
}
|
|
17
|
+
type MouseEventHandler = (e: MouseEvent | TouchEvent) => void;
|
|
32
18
|
/**
|
|
33
19
|
* Class for handling drag events in a UI.
|
|
34
20
|
*/
|
|
@@ -37,8 +23,8 @@ export declare class DragMethod {
|
|
|
37
23
|
private readonly cache;
|
|
38
24
|
private readonly onMouseMove;
|
|
39
25
|
private readonly onMouseUp;
|
|
40
|
-
readonly onMouseDown:
|
|
41
|
-
readonly onTouchStart:
|
|
26
|
+
readonly onMouseDown: MouseEventHandler;
|
|
27
|
+
readonly onTouchStart: MouseEventHandler;
|
|
42
28
|
onMove?: OnMove;
|
|
43
29
|
onMoveEnd?: OnMoveEnd;
|
|
44
30
|
constructor({ key, onMove, onMoveEnd }?: DragMethodProps);
|
package/es/dom/drag.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Class for handling drag events in a UI.
|
|
3
3
|
*/ export class DragMethod {
|
|
4
|
-
key
|
|
4
|
+
key;
|
|
5
5
|
cache = {
|
|
6
6
|
pageX: 0,
|
|
7
7
|
pageY: 0,
|
|
@@ -27,15 +27,11 @@
|
|
|
27
27
|
if (typeof this.onMove !== 'function') {
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var _e_pageX;
|
|
36
|
-
const pageX = (_e_pageX = e.pageX) !== null && _e_pageX !== void 0 ? _e_pageX : (_e_touches_ = e.touches[0]) === null || _e_touches_ === void 0 ? void 0 : _e_touches_.pageX;
|
|
37
|
-
var _e_pageY;
|
|
38
|
-
const pageY = (_e_pageY = e.pageY) !== null && _e_pageY !== void 0 ? _e_pageY : (_e_touches_1 = e.touches[0]) === null || _e_touches_1 === void 0 ? void 0 : _e_touches_1.pageY;
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
var _e_touches__pageX;
|
|
32
|
+
const pageX = 'pageX' in e ? e.pageX : (_e_touches__pageX = (_e_touches_ = e.touches[0]) === null || _e_touches_ === void 0 ? void 0 : _e_touches_.pageX) !== null && _e_touches__pageX !== void 0 ? _e_touches__pageX : 0;
|
|
33
|
+
var _e_touches__pageY;
|
|
34
|
+
const pageY = 'pageY' in e ? e.pageY : (_e_touches__pageY = (_e_touches_1 = e.touches[0]) === null || _e_touches_1 === void 0 ? void 0 : _e_touches_1.pageY) !== null && _e_touches__pageY !== void 0 ? _e_touches__pageY : 0;
|
|
39
35
|
this.onMove({
|
|
40
36
|
offsetX: pageX - this.cache.pageX,
|
|
41
37
|
offsetY: pageY - this.cache.pageY,
|
|
@@ -63,19 +59,15 @@
|
|
|
63
59
|
}
|
|
64
60
|
_onMouseDown(e) {
|
|
65
61
|
var _e_touches_, _e_touches_1;
|
|
66
|
-
|
|
67
|
-
e.preventDefault();
|
|
68
|
-
} else {
|
|
69
|
-
e.returnValue = false;
|
|
70
|
-
}
|
|
62
|
+
e.preventDefault();
|
|
71
63
|
if (typeof this.onMove !== 'function' || this.cache.dragging) {
|
|
72
64
|
return;
|
|
73
65
|
}
|
|
74
66
|
this.clearListener();
|
|
75
|
-
var
|
|
76
|
-
const pageX =
|
|
77
|
-
var
|
|
78
|
-
const pageY =
|
|
67
|
+
var _e_touches__pageX;
|
|
68
|
+
const pageX = 'pageX' in e ? e.pageX : (_e_touches__pageX = (_e_touches_ = e.touches[0]) === null || _e_touches_ === void 0 ? void 0 : _e_touches_.pageX) !== null && _e_touches__pageX !== void 0 ? _e_touches__pageX : 0;
|
|
69
|
+
var _e_touches__pageY;
|
|
70
|
+
const pageY = 'pageY' in e ? e.pageY : (_e_touches__pageY = (_e_touches_1 = e.touches[0]) === null || _e_touches_1 === void 0 ? void 0 : _e_touches_1.pageY) !== null && _e_touches__pageY !== void 0 ? _e_touches__pageY : 0;
|
|
79
71
|
this.cache.dragging = true;
|
|
80
72
|
this.cache.pageX = pageX;
|
|
81
73
|
this.cache.pageY = pageY;
|
package/es/file/parser.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const blob2text: (blob:
|
|
2
|
-
export declare const blob2base64: (blob:
|
|
3
|
-
export declare const blob2file: (blob:
|
|
1
|
+
export declare const blob2text: (blob: Blob) => Promise<string>;
|
|
2
|
+
export declare const blob2base64: (blob: Blob) => Promise<string>;
|
|
3
|
+
export declare const blob2file: (blob: Blob, name: string, type: string) => File;
|
package/es/file/parser.js
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
export const blob2text = (blob)=>{
|
|
2
|
-
return new Promise((resolve)=>{
|
|
2
|
+
return new Promise((resolve, reject)=>{
|
|
3
3
|
const reader = new FileReader();
|
|
4
|
-
reader.onload = (
|
|
4
|
+
reader.onload = ()=>resolve(reader.result);
|
|
5
|
+
reader.onerror = reject;
|
|
5
6
|
reader.readAsText(blob);
|
|
6
7
|
});
|
|
7
8
|
};
|
|
8
9
|
export const blob2base64 = (blob)=>{
|
|
9
|
-
return new Promise(
|
|
10
|
+
return new Promise((resolve, reject)=>{
|
|
10
11
|
const reader = new FileReader();
|
|
11
12
|
if (!blob) {
|
|
12
|
-
reject('
|
|
13
|
+
reject(new Error('文件为空!'));
|
|
13
14
|
} else {
|
|
14
|
-
reader.onload = (
|
|
15
|
-
reader.onabort = ()=>reject();
|
|
15
|
+
reader.onload = ()=>resolve(reader.result);
|
|
16
|
+
reader.onabort = ()=>reject(new Error('读取中断'));
|
|
17
|
+
reader.onerror = reject;
|
|
16
18
|
reader.readAsDataURL(blob);
|
|
17
19
|
}
|
|
18
20
|
});
|
|
19
21
|
};
|
|
20
22
|
export const blob2file = (blob, name, type)=>{
|
|
21
|
-
return new File(
|
|
23
|
+
return new File([
|
|
24
|
+
blob
|
|
25
|
+
], name, {
|
|
22
26
|
type
|
|
23
27
|
});
|
|
24
28
|
};
|
package/es/file/pick.js
CHANGED
|
@@ -7,7 +7,7 @@ export const pickFiles = ()=>{
|
|
|
7
7
|
window.removeEventListener('focus', onFocus);
|
|
8
8
|
setTimeout(()=>{
|
|
9
9
|
if (!picked) {
|
|
10
|
-
reject('no files picked');
|
|
10
|
+
reject(new Error('no files picked'));
|
|
11
11
|
}
|
|
12
12
|
}, 1000);
|
|
13
13
|
};
|
|
@@ -18,8 +18,9 @@ export const pickFiles = ()=>{
|
|
|
18
18
|
el.style.zIndex = '-1';
|
|
19
19
|
window.addEventListener('focus', onFocus);
|
|
20
20
|
el.onchange = (e)=>{
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
const target = e.target;
|
|
22
|
+
picked = target.files !== null && target.files.length > 0;
|
|
23
|
+
resolve(target.files);
|
|
23
24
|
};
|
|
24
25
|
document.body.appendChild(el);
|
|
25
26
|
el.click();
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
interface LoadScriptOptions {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
}
|
|
1
4
|
/**
|
|
2
5
|
* Loads a script from the given URL with the provided options.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} url - The URL of the script to load
|
|
5
|
-
* @param {any} options - Additional options for loading the script
|
|
6
|
-
* @return {Promise<any>} A Promise that resolves with the URL once the script is loaded
|
|
7
6
|
*/
|
|
8
|
-
export declare const loadScript: (url: string, options?:
|
|
7
|
+
export declare const loadScript: (url: string, options?: LoadScriptOptions) => Promise<string>;
|
|
8
|
+
export {};
|
package/es/load-script/index.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Loads a script from the given URL with the provided options.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} url - The URL of the script to load
|
|
5
|
-
* @param {any} options - Additional options for loading the script
|
|
6
|
-
* @return {Promise<any>} A Promise that resolves with the URL once the script is loaded
|
|
7
3
|
*/ export const loadScript = (url, options = {})=>{
|
|
8
4
|
const findScript = ()=>{
|
|
9
5
|
const list = document.getElementsByTagName('script');
|
|
@@ -16,11 +12,13 @@
|
|
|
16
12
|
};
|
|
17
13
|
return new Promise((resolve)=>{
|
|
18
14
|
const old = findScript();
|
|
19
|
-
let el
|
|
15
|
+
let el;
|
|
20
16
|
if (!old) {
|
|
21
17
|
el = document.createElement('script');
|
|
22
18
|
} else if (old._state === 'complete') {
|
|
23
19
|
return resolve(url);
|
|
20
|
+
} else {
|
|
21
|
+
el = old;
|
|
24
22
|
}
|
|
25
23
|
for (const key of Object.keys(options)){
|
|
26
24
|
el[key] = options[key];
|
|
@@ -30,9 +28,10 @@
|
|
|
30
28
|
el.removeEventListener('load', onLoad);
|
|
31
29
|
el._state = 'complete';
|
|
32
30
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
if (!old) {
|
|
32
|
+
el.src = url;
|
|
33
|
+
el.addEventListener('load', onLoad);
|
|
34
|
+
document.body.appendChild(el);
|
|
35
|
+
}
|
|
37
36
|
});
|
|
38
37
|
};
|
package/es/storage/cookie.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export declare const getCookieValue: (key: string) => string;
|
|
2
|
-
export declare const getCookie: () =>
|
|
3
|
-
export declare const setCookieItem: (key: string, value: string
|
|
2
|
+
export declare const getCookie: () => Record<string, string>;
|
|
3
|
+
export declare const setCookieItem: (key: string, value: string, options?: {
|
|
4
|
+
expires?: number;
|
|
5
|
+
path?: string;
|
|
6
|
+
}) => void;
|
package/es/storage/cookie.js
CHANGED
|
@@ -10,11 +10,20 @@ export const getCookie = ()=>{
|
|
|
10
10
|
}
|
|
11
11
|
const tmp = document.cookie.split(';');
|
|
12
12
|
tmp.forEach((item)=>{
|
|
13
|
-
const [key,
|
|
14
|
-
|
|
13
|
+
const [key, ...valueParts] = item.split('=');
|
|
14
|
+
if (key) {
|
|
15
|
+
res[key.trim()] = valueParts.join('=');
|
|
16
|
+
}
|
|
15
17
|
});
|
|
16
18
|
return res;
|
|
17
19
|
};
|
|
18
|
-
export const setCookieItem = (key, value)=>{
|
|
19
|
-
|
|
20
|
+
export const setCookieItem = (key, value, options)=>{
|
|
21
|
+
let cookie = `${key}=${value}`;
|
|
22
|
+
if (options === null || options === void 0 ? void 0 : options.expires) {
|
|
23
|
+
cookie += `; expires=${new Date(Date.now() + options.expires).toUTCString()}`;
|
|
24
|
+
}
|
|
25
|
+
if (options === null || options === void 0 ? void 0 : options.path) {
|
|
26
|
+
cookie += `; path=${options.path}`;
|
|
27
|
+
}
|
|
28
|
+
document.cookie = cookie;
|
|
20
29
|
};
|
package/es/storage/index.d.ts
CHANGED
|
@@ -13,10 +13,10 @@ export declare class WebStorage {
|
|
|
13
13
|
private readonly storage;
|
|
14
14
|
private readonly zip?;
|
|
15
15
|
constructor(props: WebStorageProps);
|
|
16
|
-
get state():
|
|
17
|
-
set state(data:
|
|
16
|
+
get state(): unknown | null;
|
|
17
|
+
set state(data: unknown);
|
|
18
18
|
get key(): string;
|
|
19
|
-
merge(data:
|
|
19
|
+
merge(data: Record<string, unknown>): void;
|
|
20
20
|
clear(): void;
|
|
21
21
|
}
|
|
22
22
|
export {};
|
package/es/storage/index.js
CHANGED
|
@@ -15,16 +15,13 @@ export class WebStorage {
|
|
|
15
15
|
}
|
|
16
16
|
if (value) {
|
|
17
17
|
return JSON.parse(value);
|
|
18
|
-
} else {
|
|
19
|
-
return null;
|
|
20
18
|
}
|
|
19
|
+
return null;
|
|
21
20
|
}
|
|
22
21
|
set state(data) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
this.storage.setItem(this._key, temp);
|
|
22
|
+
const temp = JSON.stringify(data);
|
|
23
|
+
const value = this.zip ? this.zip.deflate(temp) : temp;
|
|
24
|
+
this.storage.setItem(this._key, value);
|
|
28
25
|
}
|
|
29
26
|
get key() {
|
|
30
27
|
return this._key;
|
|
@@ -3,9 +3,9 @@ export declare class Storage {
|
|
|
3
3
|
private readonly utils;
|
|
4
4
|
private readonly store;
|
|
5
5
|
constructor(utils: IndexedDBUtils, store: string);
|
|
6
|
-
getState(): Promise<
|
|
7
|
-
setState(values:
|
|
8
|
-
merge(values:
|
|
6
|
+
getState(): Promise<Record<string, unknown> | undefined>;
|
|
7
|
+
setState(values: Record<string, unknown>): void;
|
|
8
|
+
merge(values: Record<string, unknown>): Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
export interface StorageFactoryType {
|
|
11
11
|
name: string;
|
|
@@ -16,7 +16,7 @@ export declare class IndexedStorage {
|
|
|
16
16
|
private readonly name;
|
|
17
17
|
private readonly version;
|
|
18
18
|
utils: IndexedDBUtils;
|
|
19
|
-
private
|
|
19
|
+
private readonly cache;
|
|
20
20
|
private readonly tables;
|
|
21
21
|
private connected;
|
|
22
22
|
private connecting;
|
|
@@ -18,18 +18,17 @@ export class Storage {
|
|
|
18
18
|
}
|
|
19
19
|
async merge(values) {
|
|
20
20
|
const state = await this.getState();
|
|
21
|
-
this.setState
|
|
21
|
+
this.setState({
|
|
22
22
|
...state,
|
|
23
|
-
...values
|
|
24
|
-
|
|
25
|
-
};
|
|
23
|
+
...values
|
|
24
|
+
});
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
export class IndexedStorage {
|
|
29
28
|
name;
|
|
30
29
|
version;
|
|
31
30
|
utils;
|
|
32
|
-
|
|
31
|
+
cache = new Map();
|
|
33
32
|
tables = [];
|
|
34
33
|
connected = false;
|
|
35
34
|
connecting = false;
|
|
@@ -58,9 +57,9 @@ export class IndexedStorage {
|
|
|
58
57
|
this.connecting = false;
|
|
59
58
|
}
|
|
60
59
|
get(store) {
|
|
61
|
-
if (!this.
|
|
62
|
-
this.
|
|
60
|
+
if (!this.cache.has(store) && this.utils) {
|
|
61
|
+
this.cache.set(store, new Storage(this.utils, store));
|
|
63
62
|
}
|
|
64
|
-
return this.
|
|
63
|
+
return this.cache.get(store);
|
|
65
64
|
}
|
|
66
65
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export interface IndexedTable {
|
|
2
2
|
name: string;
|
|
3
|
-
options?:
|
|
4
|
-
default?:
|
|
3
|
+
options?: IDBObjectStoreParameters;
|
|
4
|
+
default?: Record<string, unknown>;
|
|
5
5
|
}
|
|
6
6
|
export declare class IndexedDBUtils {
|
|
7
7
|
private readonly name;
|
|
@@ -9,12 +9,11 @@ export declare class IndexedDBUtils {
|
|
|
9
9
|
private readonly indexedDB;
|
|
10
10
|
private readonly tables;
|
|
11
11
|
private db;
|
|
12
|
-
private transaction;
|
|
13
12
|
constructor(name: string, version: number, tables: IndexedTable[]);
|
|
14
|
-
open(): Promise<
|
|
13
|
+
open(): Promise<string>;
|
|
15
14
|
private createTransaction;
|
|
16
|
-
get: (store: string,
|
|
17
|
-
getAll: (store: string,
|
|
18
|
-
delete: (store: string,
|
|
19
|
-
put: (store: string,
|
|
15
|
+
get: (store: string, ...args: unknown[]) => Promise<unknown>;
|
|
16
|
+
getAll: (store: string, ...args: unknown[]) => Promise<unknown[] | undefined>;
|
|
17
|
+
delete: (store: string, ...args: unknown[]) => Promise<void | undefined>;
|
|
18
|
+
put: (store: string, ...args: unknown[]) => Promise<IDBValidKey | undefined>;
|
|
20
19
|
}
|
|
@@ -1,32 +1,24 @@
|
|
|
1
1
|
export class IndexedDBUtils {
|
|
2
2
|
name;
|
|
3
3
|
version;
|
|
4
|
-
indexedDB
|
|
4
|
+
indexedDB;
|
|
5
5
|
tables;
|
|
6
|
-
db;
|
|
7
|
-
transaction;
|
|
6
|
+
db = null;
|
|
8
7
|
constructor(name, version, tables){
|
|
9
8
|
this.name = name;
|
|
10
9
|
this.version = version;
|
|
11
10
|
this.tables = tables;
|
|
11
|
+
this.indexedDB = self.indexedDB;
|
|
12
12
|
}
|
|
13
13
|
open() {
|
|
14
14
|
return new Promise((resolve, reject)=>{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
if (!this.indexedDB) {
|
|
16
|
+
reject(new Error('IndexedDB is not supported'));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const request = this.indexedDB.open(this.name, this.version);
|
|
20
|
+
request.onupgradeneeded = (event)=>{
|
|
19
21
|
const db = event.target.result;
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
this.transaction = event.target.transaction;
|
|
22
|
-
this.transaction.oncomplete = async ()=>{
|
|
23
|
-
this.transaction = null;
|
|
24
|
-
for (const table of this.tables){
|
|
25
|
-
if (table.default) {
|
|
26
|
-
await this.put(table.name, table.default);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
22
|
for (const table of this.tables){
|
|
31
23
|
if (!db.objectStoreNames.contains(table.name)) {
|
|
32
24
|
db.createObjectStore(table.name, table.options || {
|
|
@@ -35,17 +27,14 @@ export class IndexedDBUtils {
|
|
|
35
27
|
});
|
|
36
28
|
}
|
|
37
29
|
}
|
|
38
|
-
this.db = db;
|
|
39
30
|
};
|
|
40
31
|
request.onsuccess = ()=>{
|
|
41
|
-
|
|
42
|
-
this.db = request.result;
|
|
43
|
-
}
|
|
32
|
+
this.db = request.result;
|
|
44
33
|
resolve('success');
|
|
45
34
|
};
|
|
46
|
-
request.onerror = (
|
|
47
|
-
console.error('indexed db connect error: ',
|
|
48
|
-
reject(
|
|
35
|
+
request.onerror = (event)=>{
|
|
36
|
+
console.error('indexed db connect error: ', event);
|
|
37
|
+
reject(event);
|
|
49
38
|
};
|
|
50
39
|
});
|
|
51
40
|
}
|
|
@@ -53,7 +42,7 @@ export class IndexedDBUtils {
|
|
|
53
42
|
return (store, ...args)=>{
|
|
54
43
|
return new Promise((resolve, reject)=>{
|
|
55
44
|
if (!this.db) {
|
|
56
|
-
reject('not
|
|
45
|
+
reject(new Error('not connected'));
|
|
57
46
|
return;
|
|
58
47
|
}
|
|
59
48
|
const transaction = this.db.transaction([
|
|
@@ -61,10 +50,10 @@ export class IndexedDBUtils {
|
|
|
61
50
|
], 'readwrite');
|
|
62
51
|
const objectStore = transaction.objectStore(store);
|
|
63
52
|
const request = getRequest(objectStore, ...args);
|
|
64
|
-
request.onerror =
|
|
53
|
+
request.onerror = (event)=>{
|
|
65
54
|
reject(event);
|
|
66
55
|
};
|
|
67
|
-
request.onsuccess =
|
|
56
|
+
request.onsuccess = ()=>{
|
|
68
57
|
resolve(request.result);
|
|
69
58
|
};
|
|
70
59
|
});
|
package/es/websocket/client.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Emitter } from 'pubo-utils';
|
|
2
|
+
type ConnectionStatus = 0 | 1 | 2 | 3;
|
|
2
3
|
export declare class WebsocketClient {
|
|
3
4
|
private client;
|
|
4
5
|
private _status;
|
|
@@ -10,8 +11,9 @@ export declare class WebsocketClient {
|
|
|
10
11
|
private reconnect;
|
|
11
12
|
private onClose;
|
|
12
13
|
private onMessage;
|
|
13
|
-
get status():
|
|
14
|
+
get status(): ConnectionStatus;
|
|
14
15
|
connect(): void;
|
|
15
16
|
close(): void;
|
|
16
|
-
send(data:
|
|
17
|
+
send(data: unknown, isJson?: boolean): void;
|
|
17
18
|
}
|
|
19
|
+
export {};
|
package/es/websocket/client.js
CHANGED
|
@@ -16,15 +16,15 @@ export class WebsocketClient {
|
|
|
16
16
|
setTimeout(()=>this.connect(), 1000);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
onClose()
|
|
19
|
+
onClose = ()=>{
|
|
20
20
|
if (this._status !== 2) {
|
|
21
21
|
this._status = 3;
|
|
22
22
|
}
|
|
23
23
|
this.reconnect();
|
|
24
|
-
}
|
|
25
|
-
onMessage(e)
|
|
24
|
+
};
|
|
25
|
+
onMessage = (e)=>{
|
|
26
26
|
this.emitter.emit('message', e.data);
|
|
27
|
-
}
|
|
27
|
+
};
|
|
28
28
|
get status() {
|
|
29
29
|
return this._status;
|
|
30
30
|
}
|
|
@@ -33,8 +33,8 @@ export class WebsocketClient {
|
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
35
|
this.client = new WebSocket(this.url);
|
|
36
|
-
this.client.onclose = this.onClose
|
|
37
|
-
this.client.onmessage = this.onMessage
|
|
36
|
+
this.client.onclose = this.onClose;
|
|
37
|
+
this.client.onmessage = this.onMessage;
|
|
38
38
|
this.client.onopen = ()=>{
|
|
39
39
|
this.emitter.emit('connect');
|
|
40
40
|
this._status = 1;
|
|
@@ -49,10 +49,7 @@ export class WebsocketClient {
|
|
|
49
49
|
}
|
|
50
50
|
send(data, isJson = false) {
|
|
51
51
|
var _this_client;
|
|
52
|
-
|
|
53
|
-
if (isJson) {
|
|
54
|
-
res = JSON.stringify(data);
|
|
55
|
-
}
|
|
52
|
+
const res = isJson ? JSON.stringify(data) : data;
|
|
56
53
|
(_this_client = this.client) === null || _this_client === void 0 ? void 0 : _this_client.send(res);
|
|
57
54
|
}
|
|
58
55
|
}
|
package/lib/dom/drag.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
interface DragEvent {
|
|
2
2
|
offsetX: number;
|
|
3
3
|
offsetY: number;
|
|
4
4
|
key?: string;
|
|
@@ -6,29 +6,15 @@ type OnMove = (n: {
|
|
|
6
6
|
pageY: number;
|
|
7
7
|
startX: number;
|
|
8
8
|
startY: number;
|
|
9
|
-
}
|
|
9
|
+
}
|
|
10
|
+
type OnMove = (event: DragEvent) => void;
|
|
10
11
|
type OnMoveEnd = () => void;
|
|
11
12
|
interface DragMethodProps {
|
|
12
13
|
key?: string;
|
|
13
|
-
/**
|
|
14
|
-
* Tracks the movement of the drag and triggers the onMove callback.
|
|
15
|
-
* @callback OnMove
|
|
16
|
-
* @param {object} options - The options object.
|
|
17
|
-
* @param {number} options.offsetX - The X offset of the drag.
|
|
18
|
-
* @param {number} options.offsetY - The Y offset of the drag.
|
|
19
|
-
* @param {number} options.pageX - The X coordinate of the drag.
|
|
20
|
-
* @param {number} options.pageY - The Y coordinate of the drag.
|
|
21
|
-
* @param {number} options.startX - The initial X coordinate of the drag.
|
|
22
|
-
* @param {number} options.startY - The initial Y coordinate of the drag.
|
|
23
|
-
* @param {string} options.key - The key associated with the drag.
|
|
24
|
-
*/
|
|
25
14
|
onMove?: OnMove;
|
|
26
|
-
/**
|
|
27
|
-
* Ends the drag event and triggers the onMoveEnd callback.
|
|
28
|
-
* @callback OnMoveEnd
|
|
29
|
-
*/
|
|
30
15
|
onMoveEnd?: OnMoveEnd;
|
|
31
16
|
}
|
|
17
|
+
type MouseEventHandler = (e: MouseEvent | TouchEvent) => void;
|
|
32
18
|
/**
|
|
33
19
|
* Class for handling drag events in a UI.
|
|
34
20
|
*/
|
|
@@ -37,8 +23,8 @@ export declare class DragMethod {
|
|
|
37
23
|
private readonly cache;
|
|
38
24
|
private readonly onMouseMove;
|
|
39
25
|
private readonly onMouseUp;
|
|
40
|
-
readonly onMouseDown:
|
|
41
|
-
readonly onTouchStart:
|
|
26
|
+
readonly onMouseDown: MouseEventHandler;
|
|
27
|
+
readonly onTouchStart: MouseEventHandler;
|
|
42
28
|
onMove?: OnMove;
|
|
43
29
|
onMoveEnd?: OnMoveEnd;
|
|
44
30
|
constructor({ key, onMove, onMoveEnd }?: DragMethodProps);
|
package/lib/dom/drag.js
CHANGED
|
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "DragMethod", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
class DragMethod {
|
|
12
|
-
key
|
|
12
|
+
key;
|
|
13
13
|
cache = {
|
|
14
14
|
pageX: 0,
|
|
15
15
|
pageY: 0,
|
|
@@ -35,15 +35,11 @@ class DragMethod {
|
|
|
35
35
|
if (typeof this.onMove !== 'function') {
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var _e_pageX;
|
|
44
|
-
const pageX = (_e_pageX = e.pageX) !== null && _e_pageX !== void 0 ? _e_pageX : (_e_touches_ = e.touches[0]) === null || _e_touches_ === void 0 ? void 0 : _e_touches_.pageX;
|
|
45
|
-
var _e_pageY;
|
|
46
|
-
const pageY = (_e_pageY = e.pageY) !== null && _e_pageY !== void 0 ? _e_pageY : (_e_touches_1 = e.touches[0]) === null || _e_touches_1 === void 0 ? void 0 : _e_touches_1.pageY;
|
|
38
|
+
e.preventDefault();
|
|
39
|
+
var _e_touches__pageX;
|
|
40
|
+
const pageX = 'pageX' in e ? e.pageX : (_e_touches__pageX = (_e_touches_ = e.touches[0]) === null || _e_touches_ === void 0 ? void 0 : _e_touches_.pageX) !== null && _e_touches__pageX !== void 0 ? _e_touches__pageX : 0;
|
|
41
|
+
var _e_touches__pageY;
|
|
42
|
+
const pageY = 'pageY' in e ? e.pageY : (_e_touches__pageY = (_e_touches_1 = e.touches[0]) === null || _e_touches_1 === void 0 ? void 0 : _e_touches_1.pageY) !== null && _e_touches__pageY !== void 0 ? _e_touches__pageY : 0;
|
|
47
43
|
this.onMove({
|
|
48
44
|
offsetX: pageX - this.cache.pageX,
|
|
49
45
|
offsetY: pageY - this.cache.pageY,
|
|
@@ -71,19 +67,15 @@ class DragMethod {
|
|
|
71
67
|
}
|
|
72
68
|
_onMouseDown(e) {
|
|
73
69
|
var _e_touches_, _e_touches_1;
|
|
74
|
-
|
|
75
|
-
e.preventDefault();
|
|
76
|
-
} else {
|
|
77
|
-
e.returnValue = false;
|
|
78
|
-
}
|
|
70
|
+
e.preventDefault();
|
|
79
71
|
if (typeof this.onMove !== 'function' || this.cache.dragging) {
|
|
80
72
|
return;
|
|
81
73
|
}
|
|
82
74
|
this.clearListener();
|
|
83
|
-
var
|
|
84
|
-
const pageX =
|
|
85
|
-
var
|
|
86
|
-
const pageY =
|
|
75
|
+
var _e_touches__pageX;
|
|
76
|
+
const pageX = 'pageX' in e ? e.pageX : (_e_touches__pageX = (_e_touches_ = e.touches[0]) === null || _e_touches_ === void 0 ? void 0 : _e_touches_.pageX) !== null && _e_touches__pageX !== void 0 ? _e_touches__pageX : 0;
|
|
77
|
+
var _e_touches__pageY;
|
|
78
|
+
const pageY = 'pageY' in e ? e.pageY : (_e_touches__pageY = (_e_touches_1 = e.touches[0]) === null || _e_touches_1 === void 0 ? void 0 : _e_touches_1.pageY) !== null && _e_touches__pageY !== void 0 ? _e_touches__pageY : 0;
|
|
87
79
|
this.cache.dragging = true;
|
|
88
80
|
this.cache.pageX = pageX;
|
|
89
81
|
this.cache.pageY = pageY;
|
package/lib/file/parser.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const blob2text: (blob:
|
|
2
|
-
export declare const blob2base64: (blob:
|
|
3
|
-
export declare const blob2file: (blob:
|
|
1
|
+
export declare const blob2text: (blob: Blob) => Promise<string>;
|
|
2
|
+
export declare const blob2base64: (blob: Blob) => Promise<string>;
|
|
3
|
+
export declare const blob2file: (blob: Blob, name: string, type: string) => File;
|
package/lib/file/parser.js
CHANGED
|
@@ -20,26 +20,30 @@ _export(exports, {
|
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
const blob2text = (blob)=>{
|
|
23
|
-
return new Promise((resolve)=>{
|
|
23
|
+
return new Promise((resolve, reject)=>{
|
|
24
24
|
const reader = new FileReader();
|
|
25
|
-
reader.onload = (
|
|
25
|
+
reader.onload = ()=>resolve(reader.result);
|
|
26
|
+
reader.onerror = reject;
|
|
26
27
|
reader.readAsText(blob);
|
|
27
28
|
});
|
|
28
29
|
};
|
|
29
30
|
const blob2base64 = (blob)=>{
|
|
30
|
-
return new Promise(
|
|
31
|
+
return new Promise((resolve, reject)=>{
|
|
31
32
|
const reader = new FileReader();
|
|
32
33
|
if (!blob) {
|
|
33
|
-
reject('
|
|
34
|
+
reject(new Error('文件为空!'));
|
|
34
35
|
} else {
|
|
35
|
-
reader.onload = (
|
|
36
|
-
reader.onabort = ()=>reject();
|
|
36
|
+
reader.onload = ()=>resolve(reader.result);
|
|
37
|
+
reader.onabort = ()=>reject(new Error('读取中断'));
|
|
38
|
+
reader.onerror = reject;
|
|
37
39
|
reader.readAsDataURL(blob);
|
|
38
40
|
}
|
|
39
41
|
});
|
|
40
42
|
};
|
|
41
43
|
const blob2file = (blob, name, type)=>{
|
|
42
|
-
return new File(
|
|
44
|
+
return new File([
|
|
45
|
+
blob
|
|
46
|
+
], name, {
|
|
43
47
|
type
|
|
44
48
|
});
|
|
45
49
|
};
|
package/lib/file/pick.js
CHANGED
|
@@ -17,7 +17,7 @@ const pickFiles = ()=>{
|
|
|
17
17
|
window.removeEventListener('focus', onFocus);
|
|
18
18
|
setTimeout(()=>{
|
|
19
19
|
if (!picked) {
|
|
20
|
-
reject('no files picked');
|
|
20
|
+
reject(new Error('no files picked'));
|
|
21
21
|
}
|
|
22
22
|
}, 1000);
|
|
23
23
|
};
|
|
@@ -28,8 +28,9 @@ const pickFiles = ()=>{
|
|
|
28
28
|
el.style.zIndex = '-1';
|
|
29
29
|
window.addEventListener('focus', onFocus);
|
|
30
30
|
el.onchange = (e)=>{
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const target = e.target;
|
|
32
|
+
picked = target.files !== null && target.files.length > 0;
|
|
33
|
+
resolve(target.files);
|
|
33
34
|
};
|
|
34
35
|
document.body.appendChild(el);
|
|
35
36
|
el.click();
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
interface LoadScriptOptions {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
}
|
|
1
4
|
/**
|
|
2
5
|
* Loads a script from the given URL with the provided options.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} url - The URL of the script to load
|
|
5
|
-
* @param {any} options - Additional options for loading the script
|
|
6
|
-
* @return {Promise<any>} A Promise that resolves with the URL once the script is loaded
|
|
7
6
|
*/
|
|
8
|
-
export declare const loadScript: (url: string, options?:
|
|
7
|
+
export declare const loadScript: (url: string, options?: LoadScriptOptions) => Promise<string>;
|
|
8
|
+
export {};
|
package/lib/load-script/index.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Loads a script from the given URL with the provided options.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} url - The URL of the script to load
|
|
5
|
-
* @param {any} options - Additional options for loading the script
|
|
6
|
-
* @return {Promise<any>} A Promise that resolves with the URL once the script is loaded
|
|
7
|
-
*/ "use strict";
|
|
1
|
+
"use strict";
|
|
8
2
|
Object.defineProperty(exports, "__esModule", {
|
|
9
3
|
value: true
|
|
10
4
|
});
|
|
@@ -26,11 +20,13 @@ const loadScript = (url, options = {})=>{
|
|
|
26
20
|
};
|
|
27
21
|
return new Promise((resolve)=>{
|
|
28
22
|
const old = findScript();
|
|
29
|
-
let el
|
|
23
|
+
let el;
|
|
30
24
|
if (!old) {
|
|
31
25
|
el = document.createElement('script');
|
|
32
26
|
} else if (old._state === 'complete') {
|
|
33
27
|
return resolve(url);
|
|
28
|
+
} else {
|
|
29
|
+
el = old;
|
|
34
30
|
}
|
|
35
31
|
for (const key of Object.keys(options)){
|
|
36
32
|
el[key] = options[key];
|
|
@@ -40,9 +36,10 @@ const loadScript = (url, options = {})=>{
|
|
|
40
36
|
el.removeEventListener('load', onLoad);
|
|
41
37
|
el._state = 'complete';
|
|
42
38
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
if (!old) {
|
|
40
|
+
el.src = url;
|
|
41
|
+
el.addEventListener('load', onLoad);
|
|
42
|
+
document.body.appendChild(el);
|
|
43
|
+
}
|
|
47
44
|
});
|
|
48
45
|
};
|
package/lib/storage/cookie.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export declare const getCookieValue: (key: string) => string;
|
|
2
|
-
export declare const getCookie: () =>
|
|
3
|
-
export declare const setCookieItem: (key: string, value: string
|
|
2
|
+
export declare const getCookie: () => Record<string, string>;
|
|
3
|
+
export declare const setCookieItem: (key: string, value: string, options?: {
|
|
4
|
+
expires?: number;
|
|
5
|
+
path?: string;
|
|
6
|
+
}) => void;
|
package/lib/storage/cookie.js
CHANGED
|
@@ -31,11 +31,20 @@ const getCookie = ()=>{
|
|
|
31
31
|
}
|
|
32
32
|
const tmp = document.cookie.split(';');
|
|
33
33
|
tmp.forEach((item)=>{
|
|
34
|
-
const [key,
|
|
35
|
-
|
|
34
|
+
const [key, ...valueParts] = item.split('=');
|
|
35
|
+
if (key) {
|
|
36
|
+
res[key.trim()] = valueParts.join('=');
|
|
37
|
+
}
|
|
36
38
|
});
|
|
37
39
|
return res;
|
|
38
40
|
};
|
|
39
|
-
const setCookieItem = (key, value)=>{
|
|
40
|
-
|
|
41
|
+
const setCookieItem = (key, value, options)=>{
|
|
42
|
+
let cookie = `${key}=${value}`;
|
|
43
|
+
if (options === null || options === void 0 ? void 0 : options.expires) {
|
|
44
|
+
cookie += `; expires=${new Date(Date.now() + options.expires).toUTCString()}`;
|
|
45
|
+
}
|
|
46
|
+
if (options === null || options === void 0 ? void 0 : options.path) {
|
|
47
|
+
cookie += `; path=${options.path}`;
|
|
48
|
+
}
|
|
49
|
+
document.cookie = cookie;
|
|
41
50
|
};
|
package/lib/storage/index.d.ts
CHANGED
|
@@ -13,10 +13,10 @@ export declare class WebStorage {
|
|
|
13
13
|
private readonly storage;
|
|
14
14
|
private readonly zip?;
|
|
15
15
|
constructor(props: WebStorageProps);
|
|
16
|
-
get state():
|
|
17
|
-
set state(data:
|
|
16
|
+
get state(): unknown | null;
|
|
17
|
+
set state(data: unknown);
|
|
18
18
|
get key(): string;
|
|
19
|
-
merge(data:
|
|
19
|
+
merge(data: Record<string, unknown>): void;
|
|
20
20
|
clear(): void;
|
|
21
21
|
}
|
|
22
22
|
export {};
|
package/lib/storage/index.js
CHANGED
|
@@ -25,16 +25,13 @@ class WebStorage {
|
|
|
25
25
|
}
|
|
26
26
|
if (value) {
|
|
27
27
|
return JSON.parse(value);
|
|
28
|
-
} else {
|
|
29
|
-
return null;
|
|
30
28
|
}
|
|
29
|
+
return null;
|
|
31
30
|
}
|
|
32
31
|
set state(data) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
this.storage.setItem(this._key, temp);
|
|
32
|
+
const temp = JSON.stringify(data);
|
|
33
|
+
const value = this.zip ? this.zip.deflate(temp) : temp;
|
|
34
|
+
this.storage.setItem(this._key, value);
|
|
38
35
|
}
|
|
39
36
|
get key() {
|
|
40
37
|
return this._key;
|
|
@@ -3,9 +3,9 @@ export declare class Storage {
|
|
|
3
3
|
private readonly utils;
|
|
4
4
|
private readonly store;
|
|
5
5
|
constructor(utils: IndexedDBUtils, store: string);
|
|
6
|
-
getState(): Promise<
|
|
7
|
-
setState(values:
|
|
8
|
-
merge(values:
|
|
6
|
+
getState(): Promise<Record<string, unknown> | undefined>;
|
|
7
|
+
setState(values: Record<string, unknown>): void;
|
|
8
|
+
merge(values: Record<string, unknown>): Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
export interface StorageFactoryType {
|
|
11
11
|
name: string;
|
|
@@ -16,7 +16,7 @@ export declare class IndexedStorage {
|
|
|
16
16
|
private readonly name;
|
|
17
17
|
private readonly version;
|
|
18
18
|
utils: IndexedDBUtils;
|
|
19
|
-
private
|
|
19
|
+
private readonly cache;
|
|
20
20
|
private readonly tables;
|
|
21
21
|
private connected;
|
|
22
22
|
private connecting;
|
|
@@ -36,18 +36,17 @@ class Storage {
|
|
|
36
36
|
}
|
|
37
37
|
async merge(values) {
|
|
38
38
|
const state = await this.getState();
|
|
39
|
-
this.setState
|
|
39
|
+
this.setState({
|
|
40
40
|
...state,
|
|
41
|
-
...values
|
|
42
|
-
|
|
43
|
-
};
|
|
41
|
+
...values
|
|
42
|
+
});
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
class IndexedStorage {
|
|
47
46
|
name;
|
|
48
47
|
version;
|
|
49
48
|
utils;
|
|
50
|
-
|
|
49
|
+
cache = new Map();
|
|
51
50
|
tables = [];
|
|
52
51
|
connected = false;
|
|
53
52
|
connecting = false;
|
|
@@ -76,9 +75,9 @@ class IndexedStorage {
|
|
|
76
75
|
this.connecting = false;
|
|
77
76
|
}
|
|
78
77
|
get(store) {
|
|
79
|
-
if (!this.
|
|
80
|
-
this.
|
|
78
|
+
if (!this.cache.has(store) && this.utils) {
|
|
79
|
+
this.cache.set(store, new Storage(this.utils, store));
|
|
81
80
|
}
|
|
82
|
-
return this.
|
|
81
|
+
return this.cache.get(store);
|
|
83
82
|
}
|
|
84
83
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export interface IndexedTable {
|
|
2
2
|
name: string;
|
|
3
|
-
options?:
|
|
4
|
-
default?:
|
|
3
|
+
options?: IDBObjectStoreParameters;
|
|
4
|
+
default?: Record<string, unknown>;
|
|
5
5
|
}
|
|
6
6
|
export declare class IndexedDBUtils {
|
|
7
7
|
private readonly name;
|
|
@@ -9,12 +9,11 @@ export declare class IndexedDBUtils {
|
|
|
9
9
|
private readonly indexedDB;
|
|
10
10
|
private readonly tables;
|
|
11
11
|
private db;
|
|
12
|
-
private transaction;
|
|
13
12
|
constructor(name: string, version: number, tables: IndexedTable[]);
|
|
14
|
-
open(): Promise<
|
|
13
|
+
open(): Promise<string>;
|
|
15
14
|
private createTransaction;
|
|
16
|
-
get: (store: string,
|
|
17
|
-
getAll: (store: string,
|
|
18
|
-
delete: (store: string,
|
|
19
|
-
put: (store: string,
|
|
15
|
+
get: (store: string, ...args: unknown[]) => Promise<unknown>;
|
|
16
|
+
getAll: (store: string, ...args: unknown[]) => Promise<unknown[] | undefined>;
|
|
17
|
+
delete: (store: string, ...args: unknown[]) => Promise<void | undefined>;
|
|
18
|
+
put: (store: string, ...args: unknown[]) => Promise<IDBValidKey | undefined>;
|
|
20
19
|
}
|
|
@@ -11,32 +11,24 @@ Object.defineProperty(exports, "IndexedDBUtils", {
|
|
|
11
11
|
class IndexedDBUtils {
|
|
12
12
|
name;
|
|
13
13
|
version;
|
|
14
|
-
indexedDB
|
|
14
|
+
indexedDB;
|
|
15
15
|
tables;
|
|
16
|
-
db;
|
|
17
|
-
transaction;
|
|
16
|
+
db = null;
|
|
18
17
|
constructor(name, version, tables){
|
|
19
18
|
this.name = name;
|
|
20
19
|
this.version = version;
|
|
21
20
|
this.tables = tables;
|
|
21
|
+
this.indexedDB = self.indexedDB;
|
|
22
22
|
}
|
|
23
23
|
open() {
|
|
24
24
|
return new Promise((resolve, reject)=>{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
if (!this.indexedDB) {
|
|
26
|
+
reject(new Error('IndexedDB is not supported'));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const request = this.indexedDB.open(this.name, this.version);
|
|
30
|
+
request.onupgradeneeded = (event)=>{
|
|
29
31
|
const db = event.target.result;
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
this.transaction = event.target.transaction;
|
|
32
|
-
this.transaction.oncomplete = async ()=>{
|
|
33
|
-
this.transaction = null;
|
|
34
|
-
for (const table of this.tables){
|
|
35
|
-
if (table.default) {
|
|
36
|
-
await this.put(table.name, table.default);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
32
|
for (const table of this.tables){
|
|
41
33
|
if (!db.objectStoreNames.contains(table.name)) {
|
|
42
34
|
db.createObjectStore(table.name, table.options || {
|
|
@@ -45,17 +37,14 @@ class IndexedDBUtils {
|
|
|
45
37
|
});
|
|
46
38
|
}
|
|
47
39
|
}
|
|
48
|
-
this.db = db;
|
|
49
40
|
};
|
|
50
41
|
request.onsuccess = ()=>{
|
|
51
|
-
|
|
52
|
-
this.db = request.result;
|
|
53
|
-
}
|
|
42
|
+
this.db = request.result;
|
|
54
43
|
resolve('success');
|
|
55
44
|
};
|
|
56
|
-
request.onerror = (
|
|
57
|
-
console.error('indexed db connect error: ',
|
|
58
|
-
reject(
|
|
45
|
+
request.onerror = (event)=>{
|
|
46
|
+
console.error('indexed db connect error: ', event);
|
|
47
|
+
reject(event);
|
|
59
48
|
};
|
|
60
49
|
});
|
|
61
50
|
}
|
|
@@ -63,7 +52,7 @@ class IndexedDBUtils {
|
|
|
63
52
|
return (store, ...args)=>{
|
|
64
53
|
return new Promise((resolve, reject)=>{
|
|
65
54
|
if (!this.db) {
|
|
66
|
-
reject('not
|
|
55
|
+
reject(new Error('not connected'));
|
|
67
56
|
return;
|
|
68
57
|
}
|
|
69
58
|
const transaction = this.db.transaction([
|
|
@@ -71,10 +60,10 @@ class IndexedDBUtils {
|
|
|
71
60
|
], 'readwrite');
|
|
72
61
|
const objectStore = transaction.objectStore(store);
|
|
73
62
|
const request = getRequest(objectStore, ...args);
|
|
74
|
-
request.onerror =
|
|
63
|
+
request.onerror = (event)=>{
|
|
75
64
|
reject(event);
|
|
76
65
|
};
|
|
77
|
-
request.onsuccess =
|
|
66
|
+
request.onsuccess = ()=>{
|
|
78
67
|
resolve(request.result);
|
|
79
68
|
};
|
|
80
69
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Emitter } from 'pubo-utils';
|
|
2
|
+
type ConnectionStatus = 0 | 1 | 2 | 3;
|
|
2
3
|
export declare class WebsocketClient {
|
|
3
4
|
private client;
|
|
4
5
|
private _status;
|
|
@@ -10,8 +11,9 @@ export declare class WebsocketClient {
|
|
|
10
11
|
private reconnect;
|
|
11
12
|
private onClose;
|
|
12
13
|
private onMessage;
|
|
13
|
-
get status():
|
|
14
|
+
get status(): ConnectionStatus;
|
|
14
15
|
connect(): void;
|
|
15
16
|
close(): void;
|
|
16
|
-
send(data:
|
|
17
|
+
send(data: unknown, isJson?: boolean): void;
|
|
17
18
|
}
|
|
19
|
+
export {};
|
package/lib/websocket/client.js
CHANGED
|
@@ -26,15 +26,15 @@ class WebsocketClient {
|
|
|
26
26
|
setTimeout(()=>this.connect(), 1000);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
onClose()
|
|
29
|
+
onClose = ()=>{
|
|
30
30
|
if (this._status !== 2) {
|
|
31
31
|
this._status = 3;
|
|
32
32
|
}
|
|
33
33
|
this.reconnect();
|
|
34
|
-
}
|
|
35
|
-
onMessage(e)
|
|
34
|
+
};
|
|
35
|
+
onMessage = (e)=>{
|
|
36
36
|
this.emitter.emit('message', e.data);
|
|
37
|
-
}
|
|
37
|
+
};
|
|
38
38
|
get status() {
|
|
39
39
|
return this._status;
|
|
40
40
|
}
|
|
@@ -43,8 +43,8 @@ class WebsocketClient {
|
|
|
43
43
|
return;
|
|
44
44
|
}
|
|
45
45
|
this.client = new WebSocket(this.url);
|
|
46
|
-
this.client.onclose = this.onClose
|
|
47
|
-
this.client.onmessage = this.onMessage
|
|
46
|
+
this.client.onclose = this.onClose;
|
|
47
|
+
this.client.onmessage = this.onMessage;
|
|
48
48
|
this.client.onopen = ()=>{
|
|
49
49
|
this.emitter.emit('connect');
|
|
50
50
|
this._status = 1;
|
|
@@ -59,10 +59,7 @@ class WebsocketClient {
|
|
|
59
59
|
}
|
|
60
60
|
send(data, isJson = false) {
|
|
61
61
|
var _this_client;
|
|
62
|
-
|
|
63
|
-
if (isJson) {
|
|
64
|
-
res = JSON.stringify(data);
|
|
65
|
-
}
|
|
62
|
+
const res = isJson ? JSON.stringify(data) : data;
|
|
66
63
|
(_this_client = this.client) === null || _this_client === void 0 ? void 0 : _this_client.send(res);
|
|
67
64
|
}
|
|
68
65
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pubo-web",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.210",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"module": "./es/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
|
-
"unpkg": "dist/index.js",
|
|
8
7
|
"sideEffects": false,
|
|
9
8
|
"scripts": {
|
|
10
|
-
"build": "gulp
|
|
9
|
+
"build": "gulp"
|
|
11
10
|
},
|
|
12
11
|
"files": [
|
|
13
|
-
"dist",
|
|
14
12
|
"lib",
|
|
15
13
|
"es",
|
|
16
14
|
"package.json"
|
|
@@ -19,20 +17,16 @@
|
|
|
19
17
|
"node": ">=8.0.0"
|
|
20
18
|
},
|
|
21
19
|
"dependencies": {
|
|
22
|
-
"pubo-utils": "^1.0.
|
|
20
|
+
"pubo-utils": "^1.0.210"
|
|
23
21
|
},
|
|
24
|
-
"gitHead": "
|
|
22
|
+
"gitHead": "aff7b585c4f83e46aa8838a337d448948caaee7d",
|
|
25
23
|
"devDependencies": {
|
|
26
24
|
"del": "^5.1.0",
|
|
27
25
|
"eslint": "^8.42.0",
|
|
28
|
-
"eslint-plugin-react-hooks": "^4.0.8",
|
|
29
26
|
"gulp": "^4.0.2",
|
|
30
27
|
"gulp-typescript": "^6.0.0-alpha.1",
|
|
31
28
|
"prettier": "^2.8.8",
|
|
32
29
|
"pretty-quick": "^2.0.1",
|
|
33
|
-
"typescript": "^4.9.5"
|
|
34
|
-
"webpack": "^5.75.0",
|
|
35
|
-
"webpack-cli": "^5.0.1",
|
|
36
|
-
"webpack-merge": "^5.8.0"
|
|
30
|
+
"typescript": "^4.9.5"
|
|
37
31
|
}
|
|
38
32
|
}
|
package/dist/pubo-web.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["pubo-web"]=t():e["pubo-web"]=t()}(this,(()=>(()=>{"use strict";var e={d:(t,s)=>{for(var o in s)e.o(s,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:s[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{DragMethod:()=>u,IndexedStorage:()=>b,WebStorage:()=>r,WebsocketClient:()=>l,blob2base64:()=>n,blob2file:()=>i,blob2text:()=>o,downloadFile:()=>c,getCookie:()=>g,getCookieValue:()=>p,loadScript:()=>s,pickFiles:()=>d,setCookieItem:()=>f});const s=(e,t={})=>new Promise((s=>{const o=(()=>{const t=document.getElementsByTagName("script");for(const s of t)if(s.src===e)return s;return null})();let n=o;if(o){if("complete"===o._state)return s(e)}else n=document.createElement("script");for(const e of Object.keys(t))n[e]=t[e];const i=()=>{s(e),n.removeEventListener("load",i),n._state="complete"};return n.src=e,n.addEventListener("load",i),document.body.appendChild(n),"success"})),o=e=>new Promise((t=>{const s=new FileReader;s.onload=e=>t(e.target.result),s.readAsText(e)})),n=e=>new Promise((function(t,s){const o=new FileReader;e?(o.onload=e=>t(e.target.result),o.onabort=()=>s(),o.readAsDataURL(e)):s("文件爲空!")})),i=(e,t,s)=>new File(e,t,{type:s}),c=(e,t)=>{const s=document.createElement("a");s.href=e,t&&(s.download=t),s.style.position="fixed",s.style.visibility="hidden",document.body.appendChild(s),s.click(),document.body.removeChild(s)};class r{_key;storage;zip;constructor(e){const{type:t="sessionStorage",key:s}=e;this._key=s,this.storage=window[t],this.zip=e.zip}get state(){let e=this.storage.getItem(this._key);return this.zip&&e&&(e=this.zip.inflate(e)),e?JSON.parse(e):null}set state(e){let t=JSON.stringify(e);this.zip&&(t=this.zip.deflate(t)),this.storage.setItem(this._key,t)}get key(){return this._key}merge(e){const t=this.state;this.state={...t,...e}}clear(){this.storage.removeItem(this._key)}}const a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";class h{state={};ids={};on(e,t){if(this.state[e]||(this.state[e]={}),"function"!=typeof t)throw new Error("第二个参数必须为function!");const s=`${((e=8)=>{let t="";for(let s=0;s<e;s++)t+=a.charAt(Math.floor(Math.random()*a.length));return t})(40)}_${Date.now()}`;return this.state[e][s]=t,this.ids[s]=e,s}cancel(e){if(!e)return;const t=this.ids[e];t&&this.state[t]&&(delete this.state[t][e],0===Object.keys(this.state[t]).length&&delete this.state[t],delete this.ids[e])}clear(){this.state={},this.ids={}}emit(e,t){const s=this.state[e];if(s)for(const e of Object.keys(s)){const o=s[e];"function"==typeof o&&o(t)}}async emitAsync(e,t){const s=this.state[e];if(s)for(const e of Object.keys(s)){const o=s[e];if("function"==typeof o)try{await o(t)}catch(e){console.log(e)}}}clone(){return{state:{...this.state},ids:{...this.ids}}}restore(e){this.state=e.state,this.ids=e.ids}}class l{client=null;_status=0;url;emitter=new h;constructor({url:e}){this.url=e}reconnect(){this.client&&this.client.close(),this.client=null,3===this._status&&setTimeout((()=>this.connect()),1e3)}onClose(){2!==this._status&&(this._status=3),this.reconnect()}onMessage(e){this.emitter.emit("message",e.data)}get status(){return this._status}connect(){this.client||(this.client=new WebSocket(this.url),this.client.onclose=this.onClose.bind(this),this.client.onmessage=this.onMessage.bind(this),this.client.onopen=()=>{this.emitter.emit("connect"),this._status=1})}close(){this._status=2,this.client&&this.client.close(),this.client=null}send(e,t=!1){var s;let o=e;t&&(o=JSON.stringify(e)),null===(s=this.client)||void 0===s||s.send(o)}}class u{key="";cache={pageX:0,pageY:0,dragging:!1};onMouseMove;onMouseUp;onMouseDown;onTouchStart;onMove;onMoveEnd;constructor({key:e="",onMove:t,onMoveEnd:s}={}){this.key=e,this.onMouseDown=this._onMouseDown.bind(this),this.onTouchStart=this._onMouseDown.bind(this),this.onMouseMove=this._onMouseMove.bind(this),this.onMouseUp=this._onMouseUp.bind(this),this.onMove=t,this.onMoveEnd=s}_onMouseMove(e){var t,s,o;if("function"!=typeof this.onMove)return;e.preventDefault?e.preventDefault():e.returnValue=!1;const n=null!==(o=e.pageX)&&void 0!==o?o:null===(t=e.touches[0])||void 0===t?void 0:t.pageX;var i;const c=null!==(i=e.pageY)&&void 0!==i?i:null===(s=e.touches[0])||void 0===s?void 0:s.pageY;this.onMove({offsetX:n-this.cache.pageX,offsetY:c-this.cache.pageY,pageX:n,pageY:c,startX:this.cache.pageX,startY:this.cache.pageY,key:this.key}),this.cache.pageX=n,this.cache.pageY=c}clearListener(){window.removeEventListener("mousemove",this.onMouseMove),window.removeEventListener("touchmove",this.onMouseMove),window.removeEventListener("mouseup",this.onMouseUp),window.removeEventListener("touchend",this.onMouseUp)}_onMouseUp(){this.clearListener(),this.cache.dragging=!1,"function"==typeof this.onMoveEnd&&this.onMoveEnd()}_onMouseDown(e){var t,s,o;if(e.preventDefault&&e.pageX?e.preventDefault():e.returnValue=!1,"function"!=typeof this.onMove||this.cache.dragging)return;this.clearListener();const n=null!==(o=e.pageX)&&void 0!==o?o:null===(t=e.touches[0])||void 0===t?void 0:t.pageX;var i;const c=null!==(i=e.pageY)&&void 0!==i?i:null===(s=e.touches[0])||void 0===s?void 0:s.pageY;this.cache.dragging=!0,this.cache.pageX=n,this.cache.pageY=c,window.addEventListener("mousemove",this.onMouseMove),window.addEventListener("touchmove",this.onMouseMove,{passive:!1}),window.addEventListener("mouseup",this.onMouseUp),window.addEventListener("touchend",this.onMouseUp,{passive:!1})}}const d=()=>new Promise(((e,t)=>{const s=document.createElement("input");let o=!1;const n=()=>{document.body.removeChild(s),window.removeEventListener("focus",n),setTimeout((()=>{o||t("no files picked")}),1e3)};s.type="file",s.style.visibility="hidden",s.style.position="fixed",s.style.top="0px",s.style.zIndex="-1",window.addEventListener("focus",n),s.onchange=t=>{o=t.target.files.length>0,e(t.target.files)},document.body.appendChild(s),s.click()})),p=e=>{const t=new RegExp(`(^| )${e}=([^;]*)(;|$)`).exec(document.cookie);return t?t[2]:""},g=()=>{const e={};return document.cookie?(document.cookie.split(";").forEach((t=>{const[s,o]=t.split("=");e[s]=o})),e):e},f=(e,t)=>{document.cookie=`${e}=${t}`},v=e=>new Promise((t=>setTimeout(t,e)));class m{name;version;indexedDB=self.indexedDB;tables;db;transaction;constructor(e,t,s){this.name=e,this.version=t,this.tables=s}open(){return new Promise(((e,t)=>{var s;const o=null===(s=this.indexedDB)||void 0===s?void 0:s.open(this.name,this.version);o.onupgradeneeded=async e=>{const t=e.target.result;this.transaction=e.target.transaction,this.transaction.oncomplete=async()=>{this.transaction=null;for(const e of this.tables)e.default&&await this.put(e.name,e.default)};for(const e of this.tables)t.objectStoreNames.contains(e.name)||t.createObjectStore(e.name,e.options||{keyPath:"_id",autoIncrement:!0});this.db=t},o.onsuccess=()=>{this.db||(this.db=o.result),e("success")},o.onerror=e=>{console.error("indexed db connect error: ",e),t(e)}}))}createTransaction(e){return(t,...s)=>new Promise(((o,n)=>{if(!this.db)return void n("not connect");const i=this.db.transaction([t],"readwrite").objectStore(t),c=e(i,...s);c.onerror=function(e){n(e)},c.onsuccess=function(){o(c.result)}}))}get=this.createTransaction(((e,t)=>e.get(t)));getAll=this.createTransaction((e=>e.getAll()));delete=this.createTransaction(((e,t)=>e.delete(t)));put=this.createTransaction(((e,t)=>e.put(t)))}class y{utils;store;constructor(e,t){this.utils=e,this.store=t}async getState(){return this.utils.get(this.store,1)}setState(e){this.utils.put(this.store,{...e,_id:1})}async merge(e){const t=await this.getState();this.setState={...t,...e,_id:1}}}class b{name;version;utils;_cache={};tables=[];connected=!1;connecting=!1;constructor({name:e,version:t,tables:s}){this.name=e,this.version=t,this.tables=null!=s?s:[]}register(e){this.tables.push(...e)}async connect(){if(!this.connected)if(this.connecting)for(;!this.connecting;)await v(100);else this.connecting=!0,this.utils=new m(this.name,this.version,this.tables),await this.utils.open(),this.connected=!0,this.connecting=!1}get(e){return!this._cache[e]&&this.utils&&(this._cache[e]=new y(this.utils,e)),this._cache[e]}}return t})()));
|