-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: save and restore models with indexedDB
Showing
5 changed files
with
3,448 additions
and
2,707 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { openDB, type DBSchema } from 'idb'; | ||
import type * as monaco from 'monaco-editor'; | ||
|
||
export interface SavedModel { | ||
value: string; | ||
language: string; | ||
uri: string; | ||
} | ||
|
||
interface MyDB extends DBSchema { | ||
models: { | ||
key: string; | ||
value: SavedModel; | ||
}; | ||
} | ||
|
||
async function initDB() { | ||
return openDB<MyDB>('models', 1, { | ||
upgrade(db) { | ||
db.createObjectStore('models', { keyPath: 'uri' }); | ||
}, | ||
}); | ||
} | ||
|
||
export async function saveModels(models: monaco.editor.ITextModel[]) { | ||
console.log('Saving models...', models.length); | ||
const db = await initDB(); | ||
await db.clear('models'); | ||
await Promise.all( | ||
models.map(async (model) => { | ||
await db.add('models', { | ||
value: model.getValue(), | ||
language: model.getLanguageId(), | ||
uri: model.uri.toString(), | ||
}); | ||
}), | ||
); | ||
} | ||
|
||
export async function clearSavedModels() { | ||
const db = await initDB(); | ||
await db.clear('models'); | ||
} | ||
|
||
export async function saveModel(model: monaco.editor.ITextModel) { | ||
const db = await initDB(); | ||
await db.put('models', { | ||
value: model.getValue(), | ||
language: model.getLanguageId(), | ||
uri: model.uri.toString(), | ||
}); | ||
} | ||
|
||
export async function loadSavedModels(): Promise<SavedModel[]> { | ||
const db = await initDB(); | ||
return db.getAll('models'); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.