fix: use idb for elements and app state

This commit is contained in:
Ryan Di
2025-07-31 18:21:56 +10:00
parent 37ad85cbaf
commit cff7516318
4 changed files with 238 additions and 30 deletions
+18 -3
View File
@@ -112,7 +112,9 @@ import {
import { updateStaleImageStatuses } from "./data/FileManager";
import {
importFromLocalStorage,
importFromIndexedDB,
importUsernameFromLocalStorage,
migrateFromLocalStorageToIndexedDB,
} from "./data/localStorage";
import { loadFilesFromFirebase } from "./data/firebase";
@@ -218,7 +220,15 @@ const initializeScene = async (opts: {
);
const externalUrlMatch = window.location.hash.match(/^#url=(.*)$/);
const localDataState = importFromLocalStorage();
// migrate from localStorage to IndexedDB if needed
await migrateFromLocalStorageToIndexedDB();
// try to load from IndexedDB first, fallback to localStorage
let localDataState = await importFromIndexedDB();
if (!localDataState.elements.length && !localDataState.appState) {
// fallback to localStorage if IndexedDB is empty
localDataState = importFromLocalStorage();
}
let scene: RestoredDataState & {
scrollToContent?: boolean;
@@ -504,7 +514,7 @@ const ExcalidrawWrapper = () => {
TITLE_TIMEOUT,
);
const syncData = debounce(() => {
const syncData = debounce(async () => {
if (isTestEnv()) {
return;
}
@@ -514,7 +524,12 @@ const ExcalidrawWrapper = () => {
) {
// don't sync if local state is newer or identical to browser state
if (isBrowserStorageStateNewer(STORAGE_KEYS.VERSION_DATA_STATE)) {
const localDataState = importFromLocalStorage();
// try to load from IndexedDB first, fallback to localStorage
let localDataState = await importFromIndexedDB();
if (!localDataState.elements.length && !localDataState.appState) {
// fallback to localStorage if IndexedDB is empty
localDataState = importFromLocalStorage();
}
const username = importUsernameFromLocalStorage();
setLangCode(getPreferredLanguage());
excalidrawAPI.updateScene({