mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
ui: migrate scratchpad drafts to IndexedDB
This commit is contained in:
1 parent
c25c1d5e89
commit
86d2a36731
3 files changed
+57
-21
No files matched your search
@@ -1,7 +1,9 @@
|
||||
import { openDB as _open } from 'idb';
|
||||
import { alert } from 'vj/components/dialog';
|
||||
|
||||
export const openDB = _open('hydro', 2, {
|
||||
const scratchpadCleanup: string[] = [];
|
||||
|
||||
export const openDB = _open('hydro', 3, {
|
||||
upgrade(db, oldVersion) {
|
||||
if (oldVersion < 1) {
|
||||
const solutionStore = db.createObjectStore('solutions', { keyPath: 'id' });
|
||||
@@ -18,6 +20,23 @@ export const openDB = _open('hydro', 2, {
|
||||
}
|
||||
}
|
||||
if (oldVersion < 2) db.createObjectStore('domain-info', { keyPath: 'id' });
|
||||
if (oldVersion < 3) {
|
||||
const scratchpadStore = db.createObjectStore('scratchpad-drafts', { keyPath: 'id' });
|
||||
const drafts = new Map<string, { id: string, code?: string, lang?: string }>();
|
||||
const scratchpadKey = /^(\d+\/[A-Za-z]\w{3,31}\/\d+(?:@[\dA-Fa-f]{24})?)(#lang)?$/;
|
||||
for (const key of Object.keys(localStorage)) {
|
||||
const match = key.match(scratchpadKey);
|
||||
if (!match) continue;
|
||||
const value = localStorage.getItem(key);
|
||||
if (value === null) continue;
|
||||
const draft = drafts.get(match[1]) || { id: match[1] };
|
||||
if (match[2]) draft.lang = value;
|
||||
else draft.code = value;
|
||||
drafts.set(draft.id, draft);
|
||||
scratchpadCleanup.push(key);
|
||||
}
|
||||
for (const draft of drafts.values()) scratchpadStore.put(draft);
|
||||
}
|
||||
},
|
||||
blocked(currentVersion, blockedVersion) {
|
||||
console.error('IDB Blocked by version', blockedVersion, 'want', currentVersion);
|
||||
@@ -30,4 +49,7 @@ export const openDB = _open('hydro', 2, {
|
||||
terminated() {
|
||||
console.error('IDB Terminated');
|
||||
},
|
||||
}).then((db) => {
|
||||
for (const key of scratchpadCleanup) localStorage.removeItem(key);
|
||||
return db;
|
||||
});
|
||||
Reference in new issue
Block a user