@vxe-ui/core 3.5.0 → 3.5.2
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/index.d.ts +3 -0
- package/es/index.esm.js +3 -0
- package/es/index.js +3 -0
- package/es/src/core.js +1 -1
- package/es/src/db-utils.d.ts +2 -0
- package/es/src/db-utils.js +234 -0
- package/es/src/log.js +1 -1
- package/lib/index.common.js +3 -0
- package/lib/index.js +3 -0
- package/lib/index.umd.js +248 -17
- package/lib/index.umd.min.js +1 -1
- package/lib/src/core.js +1 -1
- package/lib/src/db-utils.js +263 -0
- package/lib/src/log.js +1 -1
- package/package.json +1 -1
- package/packages/index.ts +3 -0
- package/packages/src/db-utils.ts +267 -0
- package/types/core/db-utils.d.ts +44 -0
- package/types/core/global-config.d.ts +4 -6
- package/types/core/index.d.ts +7 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
31
|
+
var db_utils_exports = {};
|
|
32
|
+
__export(db_utils_exports, {
|
|
33
|
+
dbUtils: () => dbUtils
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(db_utils_exports);
|
|
36
|
+
var import_xe_utils = __toESM(require("xe-utils"));
|
|
37
|
+
var import_configStore = require("./configStore");
|
|
38
|
+
class IdbStore {
|
|
39
|
+
constructor(key, version) {
|
|
40
|
+
__publicField(this, "storeName", "vxe_storage");
|
|
41
|
+
__publicField(this, "key");
|
|
42
|
+
__publicField(this, "version");
|
|
43
|
+
__publicField(this, "dbPromise", null);
|
|
44
|
+
__publicField(this, "dbName", "");
|
|
45
|
+
this.key = key;
|
|
46
|
+
this.version = version;
|
|
47
|
+
}
|
|
48
|
+
getDBName() {
|
|
49
|
+
return `${this.key}_${this.version}`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 重置连接缓存,下次调用 openDB 会重新建立连接
|
|
53
|
+
*/
|
|
54
|
+
resetDB() {
|
|
55
|
+
this.dbPromise = null;
|
|
56
|
+
this.dbName = "";
|
|
57
|
+
}
|
|
58
|
+
openDB() {
|
|
59
|
+
const name = this.getDBName();
|
|
60
|
+
if (this.dbPromise && this.dbName === name) {
|
|
61
|
+
return this.dbPromise;
|
|
62
|
+
}
|
|
63
|
+
this.dbName = name;
|
|
64
|
+
this.dbPromise = new Promise((resolve, reject) => {
|
|
65
|
+
const request = indexedDB.open(name, 1);
|
|
66
|
+
request.onupgradeneeded = () => {
|
|
67
|
+
const db = request.result;
|
|
68
|
+
if (!db.objectStoreNames.contains(this.storeName)) {
|
|
69
|
+
db.createObjectStore(this.storeName, { keyPath: "id" });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
request.onsuccess = () => {
|
|
73
|
+
const db = request.result;
|
|
74
|
+
db.onversionchange = () => {
|
|
75
|
+
db.close();
|
|
76
|
+
this.resetDB();
|
|
77
|
+
};
|
|
78
|
+
db.onclose = () => {
|
|
79
|
+
this.resetDB();
|
|
80
|
+
};
|
|
81
|
+
resolve(db);
|
|
82
|
+
};
|
|
83
|
+
request.onerror = () => {
|
|
84
|
+
reject(request.error || new Error(`IdbStore open failed: ${name}`));
|
|
85
|
+
};
|
|
86
|
+
request.onblocked = () => {
|
|
87
|
+
console.warn(`[IdbStore] open blocked: ${name}`);
|
|
88
|
+
};
|
|
89
|
+
}).catch((err) => {
|
|
90
|
+
this.resetDB();
|
|
91
|
+
throw err;
|
|
92
|
+
});
|
|
93
|
+
return this.dbPromise;
|
|
94
|
+
}
|
|
95
|
+
getById(id) {
|
|
96
|
+
return this.openDB().then(
|
|
97
|
+
(db) => new Promise((resolve, reject) => {
|
|
98
|
+
const req = db.transaction(this.storeName, "readonly").objectStore(this.storeName).get(id);
|
|
99
|
+
req.onsuccess = () => resolve(req.result);
|
|
100
|
+
req.onerror = () => reject(req.error);
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
getAll() {
|
|
105
|
+
return this.openDB().then(
|
|
106
|
+
(db) => new Promise((resolve, reject) => {
|
|
107
|
+
const store = db.transaction(this.storeName, "readonly").objectStore(this.storeName);
|
|
108
|
+
if (import_xe_utils.default.isFunction(store.getAll)) {
|
|
109
|
+
const req2 = store.getAll();
|
|
110
|
+
req2.onsuccess = () => resolve(req2.result || []);
|
|
111
|
+
req2.onerror = () => reject(req2.error);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const list = [];
|
|
115
|
+
const req = store.openCursor();
|
|
116
|
+
req.onsuccess = () => {
|
|
117
|
+
const cursor = req.result;
|
|
118
|
+
if (cursor) {
|
|
119
|
+
list.push(cursor.value);
|
|
120
|
+
cursor.continue();
|
|
121
|
+
} else {
|
|
122
|
+
resolve(list);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
req.onerror = () => reject(req.error);
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
updateById(id, data) {
|
|
130
|
+
if (data == null) {
|
|
131
|
+
return this.deleteById(id);
|
|
132
|
+
}
|
|
133
|
+
return this.openDB().then(
|
|
134
|
+
(db) => new Promise((resolve, reject) => {
|
|
135
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
136
|
+
tx.objectStore(this.storeName).put({ id, data });
|
|
137
|
+
tx.oncomplete = () => resolve();
|
|
138
|
+
tx.onerror = () => reject(tx.error);
|
|
139
|
+
})
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
deleteById(id) {
|
|
143
|
+
return this.openDB().then(
|
|
144
|
+
(db) => new Promise((resolve, reject) => {
|
|
145
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
146
|
+
tx.objectStore(this.storeName).delete(id);
|
|
147
|
+
tx.oncomplete = () => resolve();
|
|
148
|
+
tx.onerror = () => reject(tx.error);
|
|
149
|
+
})
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* 清空当前库的数据
|
|
154
|
+
*/
|
|
155
|
+
clear() {
|
|
156
|
+
return this.openDB().then(
|
|
157
|
+
(db) => new Promise((resolve, reject) => {
|
|
158
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
159
|
+
tx.objectStore(this.storeName).clear();
|
|
160
|
+
tx.oncomplete = () => resolve();
|
|
161
|
+
tx.onerror = () => reject(tx.error);
|
|
162
|
+
})
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 清理非当前版本的旧库
|
|
167
|
+
*/
|
|
168
|
+
cleanupOldDBs() {
|
|
169
|
+
if (!indexedDB.databases) {
|
|
170
|
+
return Promise.resolve();
|
|
171
|
+
}
|
|
172
|
+
const current = this.getDBName();
|
|
173
|
+
const prefix = `${this.key}_`;
|
|
174
|
+
return indexedDB.databases().then((dbs) => {
|
|
175
|
+
dbs.forEach((db) => {
|
|
176
|
+
const n = db.name;
|
|
177
|
+
if (!n || n === current || !n.startsWith(prefix)) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const rest = n.slice(prefix.length);
|
|
181
|
+
if (!rest.includes("_")) {
|
|
182
|
+
indexedDB.deleteDatabase(n);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
class LocalStorageStore {
|
|
189
|
+
constructor(key, version) {
|
|
190
|
+
__publicField(this, "key");
|
|
191
|
+
__publicField(this, "version");
|
|
192
|
+
this.key = key;
|
|
193
|
+
this.version = version;
|
|
194
|
+
}
|
|
195
|
+
readAll() {
|
|
196
|
+
const storageData = import_xe_utils.default.toStringJSON(localStorage.getItem(this.key) || "");
|
|
197
|
+
return storageData && String(storageData._v) === String(this.version) ? storageData : { _v: this.version };
|
|
198
|
+
}
|
|
199
|
+
writeAll(map) {
|
|
200
|
+
localStorage.setItem(this.key, import_xe_utils.default.toJSONString(map));
|
|
201
|
+
}
|
|
202
|
+
getById(id) {
|
|
203
|
+
const map = this.readAll();
|
|
204
|
+
const has = Object.prototype.hasOwnProperty.call(map, id);
|
|
205
|
+
return Promise.resolve(has && map[id] != null ? { id, data: map[id] } : void 0);
|
|
206
|
+
}
|
|
207
|
+
getAll() {
|
|
208
|
+
const map = this.readAll();
|
|
209
|
+
const list = [];
|
|
210
|
+
Object.keys(map).forEach((k) => {
|
|
211
|
+
if (k !== "_v" && map[k] != null) {
|
|
212
|
+
list.push({ id: k, data: map[k] });
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
return Promise.resolve(list);
|
|
216
|
+
}
|
|
217
|
+
updateById(id, data) {
|
|
218
|
+
if (data == null) {
|
|
219
|
+
return this.deleteById(id);
|
|
220
|
+
}
|
|
221
|
+
const map = this.readAll();
|
|
222
|
+
map[id] = data;
|
|
223
|
+
this.writeAll(map);
|
|
224
|
+
return Promise.resolve();
|
|
225
|
+
}
|
|
226
|
+
deleteById(id) {
|
|
227
|
+
const map = this.readAll();
|
|
228
|
+
delete map[id];
|
|
229
|
+
this.writeAll(map);
|
|
230
|
+
return Promise.resolve();
|
|
231
|
+
}
|
|
232
|
+
clear() {
|
|
233
|
+
this.writeAll({ _v: this.version });
|
|
234
|
+
return Promise.resolve();
|
|
235
|
+
}
|
|
236
|
+
cleanupOldDBs() {
|
|
237
|
+
const raw = localStorage.getItem(this.key);
|
|
238
|
+
if (!raw) {
|
|
239
|
+
return Promise.resolve();
|
|
240
|
+
}
|
|
241
|
+
const storageData = import_xe_utils.default.toStringJSON(raw);
|
|
242
|
+
if (!storageData || String(storageData._v) !== String(this.version)) {
|
|
243
|
+
localStorage.removeItem(this.key);
|
|
244
|
+
}
|
|
245
|
+
return Promise.resolve();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const dbUtils = {
|
|
249
|
+
/**
|
|
250
|
+
* 创建存储实例
|
|
251
|
+
* - driver = 'indexedDB' 且环境支持 => IdbStore
|
|
252
|
+
* - 其余情况(未传 / 不支持)=> LocalStorageStore
|
|
253
|
+
*/
|
|
254
|
+
createStore(key, options) {
|
|
255
|
+
const opts = Object.assign({}, options);
|
|
256
|
+
const version = opts.version || import_configStore.globalConfigStore.version || 0;
|
|
257
|
+
const driver = opts.driver;
|
|
258
|
+
if ((!driver || driver === "indexedDB") && typeof indexedDB !== "undefined") {
|
|
259
|
+
return new IdbStore(key, version);
|
|
260
|
+
}
|
|
261
|
+
return new LocalStorageStore(key, version);
|
|
262
|
+
}
|
|
263
|
+
};
|
package/lib/src/log.js
CHANGED
package/package.json
CHANGED
package/packages/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { clipboard } from './src/clipboard'
|
|
|
17
17
|
import { permission } from './src/permission'
|
|
18
18
|
import { zIndex } from './src/z-index'
|
|
19
19
|
import { baseUtils } from './src/base-utils'
|
|
20
|
+
import { dbUtils } from './src/db-utils'
|
|
20
21
|
import { domUtils } from './src/dom-utils'
|
|
21
22
|
import { scrollbarUtils } from './src/scrollbar-utils'
|
|
22
23
|
import { popperUtils } from './src/popper-utils'
|
|
@@ -113,6 +114,7 @@ export const VxeUI = Object.assign(VxeCore, {
|
|
|
113
114
|
zIndex,
|
|
114
115
|
|
|
115
116
|
baseUtils,
|
|
117
|
+
dbUtils,
|
|
116
118
|
domUtils,
|
|
117
119
|
scrollbarUtils,
|
|
118
120
|
popperUtils,
|
|
@@ -153,6 +155,7 @@ export * from './src/permission'
|
|
|
153
155
|
export * from './src/z-index'
|
|
154
156
|
|
|
155
157
|
export * from './src/base-utils'
|
|
158
|
+
export * from './src/db-utils'
|
|
156
159
|
export * from './src/dom-utils'
|
|
157
160
|
export * from './src/scrollbar-utils'
|
|
158
161
|
export * from './src/popper-utils'
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import XEUtils from 'xe-utils'
|
|
2
|
+
import { globalConfigStore } from './configStore'
|
|
3
|
+
|
|
4
|
+
import type { VxeDBUtils, VxeDBStore, VxeDBOptions } from '../../types'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* IndexedDB 存储
|
|
8
|
+
* - 库名 = `${key}_${version}`,version 变化 => 换库
|
|
9
|
+
*/
|
|
10
|
+
class IdbStore implements VxeDBStore {
|
|
11
|
+
private storeName = 'vxe_storage'
|
|
12
|
+
private key: string
|
|
13
|
+
private version: string | number
|
|
14
|
+
private dbPromise: Promise<IDBDatabase> | null = null
|
|
15
|
+
private dbName = ''
|
|
16
|
+
|
|
17
|
+
constructor(key: string, version: string | number) {
|
|
18
|
+
this.key = key
|
|
19
|
+
this.version = version
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private getDBName() {
|
|
23
|
+
return `${this.key}_${this.version}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 重置连接缓存,下次调用 openDB 会重新建立连接
|
|
28
|
+
*/
|
|
29
|
+
private resetDB() {
|
|
30
|
+
this.dbPromise = null
|
|
31
|
+
this.dbName = ''
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private openDB(): Promise<IDBDatabase> {
|
|
35
|
+
const name = this.getDBName()
|
|
36
|
+
if (this.dbPromise && this.dbName === name) {
|
|
37
|
+
return this.dbPromise
|
|
38
|
+
}
|
|
39
|
+
this.dbName = name
|
|
40
|
+
this.dbPromise = new Promise<IDBDatabase>((resolve, reject) => {
|
|
41
|
+
const request = indexedDB.open(name, 1)
|
|
42
|
+
request.onupgradeneeded = () => {
|
|
43
|
+
const db = request.result
|
|
44
|
+
if (!db.objectStoreNames.contains(this.storeName)) {
|
|
45
|
+
db.createObjectStore(this.storeName, { keyPath: 'id' })
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
request.onsuccess = () => {
|
|
49
|
+
const db = request.result
|
|
50
|
+
// 连接失效时清缓存,下次自动重建
|
|
51
|
+
db.onversionchange = () => {
|
|
52
|
+
db.close()
|
|
53
|
+
this.resetDB()
|
|
54
|
+
}
|
|
55
|
+
db.onclose = () => {
|
|
56
|
+
this.resetDB()
|
|
57
|
+
}
|
|
58
|
+
resolve(db)
|
|
59
|
+
}
|
|
60
|
+
// 失败时先 reject,由外层 catch 统一清缓存
|
|
61
|
+
request.onerror = () => {
|
|
62
|
+
reject(request.error || new Error(`IdbStore open failed: ${name}`))
|
|
63
|
+
}
|
|
64
|
+
// blocked 仅警告,不 reject:其他连接关闭后仍可能 onsuccess
|
|
65
|
+
request.onblocked = () => {
|
|
66
|
+
console.warn(`[IdbStore] open blocked: ${name}`)
|
|
67
|
+
}
|
|
68
|
+
}).catch((err) => {
|
|
69
|
+
// 覆盖 request.onerror 和同步抛错两种情况,避免缓存 rejected Promise
|
|
70
|
+
this.resetDB()
|
|
71
|
+
throw err
|
|
72
|
+
})
|
|
73
|
+
return this.dbPromise
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
getById(id: string): Promise<{ id: string; data: any } | undefined> {
|
|
77
|
+
return this.openDB().then((db) =>
|
|
78
|
+
new Promise<{ id: string; data: any } | undefined>((resolve, reject) => {
|
|
79
|
+
const req = db.transaction(this.storeName, 'readonly').objectStore(this.storeName).get(id)
|
|
80
|
+
req.onsuccess = () => resolve(req.result)
|
|
81
|
+
req.onerror = () => reject(req.error)
|
|
82
|
+
})
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getAll(): Promise<{ id: string; data: any }[]> {
|
|
87
|
+
return this.openDB().then((db) =>
|
|
88
|
+
new Promise<{ id: string; data: any }[]>((resolve, reject) => {
|
|
89
|
+
const store = db.transaction(this.storeName, 'readonly').objectStore(this.storeName)
|
|
90
|
+
if (XEUtils.isFunction(store.getAll)) {
|
|
91
|
+
const req = store.getAll()
|
|
92
|
+
req.onsuccess = () => resolve(req.result || [])
|
|
93
|
+
req.onerror = () => reject(req.error)
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
const list: { id: string; data: any }[] = []
|
|
97
|
+
const req = store.openCursor()
|
|
98
|
+
req.onsuccess = () => {
|
|
99
|
+
const cursor = req.result
|
|
100
|
+
if (cursor) {
|
|
101
|
+
list.push(cursor.value)
|
|
102
|
+
cursor.continue()
|
|
103
|
+
} else {
|
|
104
|
+
resolve(list)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
req.onerror = () => reject(req.error)
|
|
108
|
+
})
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
updateById(id: string, data: any): Promise<void> {
|
|
113
|
+
// 与 LocalStorageStore 对齐:null / undefined 等价删除
|
|
114
|
+
if (data == null) {
|
|
115
|
+
return this.deleteById(id)
|
|
116
|
+
}
|
|
117
|
+
return this.openDB().then((db) =>
|
|
118
|
+
new Promise<void>((resolve, reject) => {
|
|
119
|
+
const tx = db.transaction(this.storeName, 'readwrite')
|
|
120
|
+
tx.objectStore(this.storeName).put({ id, data })
|
|
121
|
+
tx.oncomplete = () => resolve()
|
|
122
|
+
tx.onerror = () => reject(tx.error)
|
|
123
|
+
})
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
deleteById(id: string): Promise<void> {
|
|
128
|
+
return this.openDB().then((db) =>
|
|
129
|
+
new Promise<void>((resolve, reject) => {
|
|
130
|
+
const tx = db.transaction(this.storeName, 'readwrite')
|
|
131
|
+
tx.objectStore(this.storeName).delete(id)
|
|
132
|
+
tx.oncomplete = () => resolve()
|
|
133
|
+
tx.onerror = () => reject(tx.error)
|
|
134
|
+
})
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 清空当前库的数据
|
|
140
|
+
*/
|
|
141
|
+
clear(): Promise<void> {
|
|
142
|
+
return this.openDB().then((db) =>
|
|
143
|
+
new Promise<void>((resolve, reject) => {
|
|
144
|
+
const tx = db.transaction(this.storeName, 'readwrite')
|
|
145
|
+
tx.objectStore(this.storeName).clear()
|
|
146
|
+
tx.oncomplete = () => resolve()
|
|
147
|
+
tx.onerror = () => reject(tx.error)
|
|
148
|
+
})
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 清理非当前版本的旧库
|
|
154
|
+
*/
|
|
155
|
+
cleanupOldDBs(): Promise<void> {
|
|
156
|
+
if (!indexedDB.databases) {
|
|
157
|
+
return Promise.resolve()
|
|
158
|
+
}
|
|
159
|
+
const current = this.getDBName()
|
|
160
|
+
const prefix = `${this.key}_`
|
|
161
|
+
return indexedDB.databases().then((dbs) => {
|
|
162
|
+
dbs.forEach((db) => {
|
|
163
|
+
const n = db.name
|
|
164
|
+
if (!n || n === current || !n.startsWith(prefix)) {
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
const rest = n.slice(prefix.length)
|
|
168
|
+
if (!rest.includes('_')) {
|
|
169
|
+
indexedDB.deleteDatabase(n)
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* localStorage 存储
|
|
178
|
+
* - 储存结构:{ _v: version, [id]: data },靠 _v 区分版本
|
|
179
|
+
* - version 变化 => 读取时视为空{} => 重置
|
|
180
|
+
*/
|
|
181
|
+
class LocalStorageStore implements VxeDBStore {
|
|
182
|
+
private key: string
|
|
183
|
+
private version: string | number
|
|
184
|
+
|
|
185
|
+
constructor(key: string, version: string | number) {
|
|
186
|
+
this.key = key
|
|
187
|
+
this.version = version
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private readAll(): Record<string, any> {
|
|
191
|
+
const storageData = XEUtils.toStringJSON(localStorage.getItem(this.key) || '')
|
|
192
|
+
return storageData && String(storageData._v) === String(this.version) ? storageData : { _v: this.version }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private writeAll(map: Record<string, any>) {
|
|
196
|
+
localStorage.setItem(this.key, XEUtils.toJSONString(map))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
getById(id: string): Promise<{ id: string; data: any } | undefined> {
|
|
200
|
+
const map = this.readAll()
|
|
201
|
+
const has = Object.prototype.hasOwnProperty.call(map, id)
|
|
202
|
+
return Promise.resolve(has && map[id] != null ? { id, data: map[id] } : undefined)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getAll(): Promise<{ id: string; data: any }[]> {
|
|
206
|
+
const map = this.readAll()
|
|
207
|
+
const list: { id: string; data: any }[] = []
|
|
208
|
+
Object.keys(map).forEach((k) => {
|
|
209
|
+
if (k !== '_v' && map[k] != null) {
|
|
210
|
+
list.push({ id: k, data: map[k] })
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
return Promise.resolve(list)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
updateById(id: string, data: any): Promise<void> {
|
|
217
|
+
// null / undefined 等价删除
|
|
218
|
+
if (data == null) {
|
|
219
|
+
return this.deleteById(id)
|
|
220
|
+
}
|
|
221
|
+
const map = this.readAll()
|
|
222
|
+
map[id] = data
|
|
223
|
+
this.writeAll(map)
|
|
224
|
+
return Promise.resolve()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
deleteById(id: string): Promise<void> {
|
|
228
|
+
const map = this.readAll()
|
|
229
|
+
delete map[id]
|
|
230
|
+
this.writeAll(map)
|
|
231
|
+
return Promise.resolve()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
clear(): Promise<void> {
|
|
235
|
+
this.writeAll({ _v: this.version })
|
|
236
|
+
return Promise.resolve()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
cleanupOldDBs(): Promise<void> {
|
|
240
|
+
const raw = localStorage.getItem(this.key)
|
|
241
|
+
if (!raw) {
|
|
242
|
+
return Promise.resolve()
|
|
243
|
+
}
|
|
244
|
+
const storageData = XEUtils.toStringJSON(raw)
|
|
245
|
+
if (!storageData || String(storageData._v) !== String(this.version)) {
|
|
246
|
+
localStorage.removeItem(this.key)
|
|
247
|
+
}
|
|
248
|
+
return Promise.resolve()
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export const dbUtils: VxeDBUtils = {
|
|
253
|
+
/**
|
|
254
|
+
* 创建存储实例
|
|
255
|
+
* - driver = 'indexedDB' 且环境支持 => IdbStore
|
|
256
|
+
* - 其余情况(未传 / 不支持)=> LocalStorageStore
|
|
257
|
+
*/
|
|
258
|
+
createStore(key: string, options?: VxeDBOptions): VxeDBStore {
|
|
259
|
+
const opts = Object.assign({}, options)
|
|
260
|
+
const version = opts.version || globalConfigStore.version || 0
|
|
261
|
+
const driver = opts.driver
|
|
262
|
+
if ((!driver || driver === 'indexedDB') && typeof indexedDB !== 'undefined') {
|
|
263
|
+
return new IdbStore(key, version)
|
|
264
|
+
}
|
|
265
|
+
return new LocalStorageStore(key, version)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export interface VxeDBStore<T = any> {
|
|
2
|
+
/**
|
|
3
|
+
* 根据 ID 获取数据
|
|
4
|
+
* @param id
|
|
5
|
+
*/
|
|
6
|
+
getById(id: string): Promise<{ id: string; data: T } | undefined>
|
|
7
|
+
/**
|
|
8
|
+
* 获取所有数据
|
|
9
|
+
*/
|
|
10
|
+
getAll(): Promise<{ id: string; data: T }[]>
|
|
11
|
+
/**
|
|
12
|
+
* 根据 ID 更新数据
|
|
13
|
+
* @param id
|
|
14
|
+
*/
|
|
15
|
+
updateById(id: string, data: T): Promise<void>
|
|
16
|
+
/**
|
|
17
|
+
* 根据 ID 删除数据
|
|
18
|
+
* @param id
|
|
19
|
+
*/
|
|
20
|
+
deleteById(id: string): Promise<void>
|
|
21
|
+
/**
|
|
22
|
+
* 清空当前存储的数据
|
|
23
|
+
*/
|
|
24
|
+
clear(): Promise<void>
|
|
25
|
+
/**
|
|
26
|
+
* 清理旧版本数据
|
|
27
|
+
*/
|
|
28
|
+
cleanupOldDBs(): Promise<void>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface VxeDBOptions {
|
|
32
|
+
version?: string | number
|
|
33
|
+
driver?: 'indexedDB' | 'localStorage' | '' | null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface VxeDBUtils {
|
|
37
|
+
/**
|
|
38
|
+
* 创建存储实例
|
|
39
|
+
* - driver = 'indexedDB' 且环境支持 => IdbStore
|
|
40
|
+
* @param key
|
|
41
|
+
* @param options
|
|
42
|
+
*/
|
|
43
|
+
createStore<T = any>(key: string, options?: VxeDBOptions): VxeDBStore<T>
|
|
44
|
+
}
|
|
@@ -33,6 +33,10 @@ export interface VxeGlobalConfig {
|
|
|
33
33
|
* 全局默认 z-index
|
|
34
34
|
*/
|
|
35
35
|
zIndex?: number
|
|
36
|
+
/**
|
|
37
|
+
* 全局版本号,对于某些带数据缓存的功能有用到,上升版本号可以用于重置组件保存的数据
|
|
38
|
+
*/
|
|
39
|
+
version?: string | number
|
|
36
40
|
/**
|
|
37
41
|
* 全局组件尺寸
|
|
38
42
|
*/
|
|
@@ -55,12 +59,6 @@ export interface VxeGlobalConfig {
|
|
|
55
59
|
* @returns string
|
|
56
60
|
*/
|
|
57
61
|
i18n?:(key: string, args?: any) => string | number
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 已废弃,请使用 VxeUI.tableVersion
|
|
61
|
-
* @deprecated
|
|
62
|
-
*/
|
|
63
|
-
version?: string | number
|
|
64
62
|
/**
|
|
65
63
|
* 已废弃,请使用 VxeUI.setConfig({ table: { resizeInterval: 300 } })
|
|
66
64
|
* @deprecated
|
package/types/core/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { VxeGlobalClipboard } from './clipboard'
|
|
|
17
17
|
import { VxeGlobalPermission } from './permission'
|
|
18
18
|
import { VxeZIndex } from './z-index'
|
|
19
19
|
import { VxeBaseUtils } from './base-utils'
|
|
20
|
+
import { VxeDBUtils } from './db-utils'
|
|
20
21
|
import { VxeDomUtils } from './dom-utils'
|
|
21
22
|
import { VxeScrollbarUtils } from './scrollbar-utils'
|
|
22
23
|
import { VxePopperUtils } from './popper-utils'
|
|
@@ -78,6 +79,7 @@ export const permission: VxeGlobalPermission
|
|
|
78
79
|
export const zIndex: VxeZIndex
|
|
79
80
|
|
|
80
81
|
export const baseUtils: VxeBaseUtils
|
|
82
|
+
export const dbUtils: VxeDBUtils
|
|
81
83
|
export const domUtils: VxeDomUtils
|
|
82
84
|
export const scrollbarUtils: VxeScrollbarUtils
|
|
83
85
|
export const popperUtils: VxePopperUtils
|
|
@@ -223,6 +225,10 @@ export interface VxeUIExport {
|
|
|
223
225
|
* 基础工具函数
|
|
224
226
|
*/
|
|
225
227
|
baseUtils: VxeBaseUtils
|
|
228
|
+
/**
|
|
229
|
+
* 数据储存函数
|
|
230
|
+
*/
|
|
231
|
+
dbUtils: VxeDBUtils
|
|
226
232
|
/**
|
|
227
233
|
* Document 工具函数
|
|
228
234
|
*/
|
|
@@ -312,6 +318,7 @@ export * from './permission'
|
|
|
312
318
|
export * from './z-index'
|
|
313
319
|
|
|
314
320
|
export * from './base-utils'
|
|
321
|
+
export * from './db-utils'
|
|
315
322
|
export * from './dom-utils'
|
|
316
323
|
export * from './scrollbar-utils'
|
|
317
324
|
export * from './popper-utils'
|