Skip to content

Commit

Permalink
Save progress to the session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
x0k committed Jun 7, 2024
1 parent 727603b commit 86f5e66
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/adapters/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ export function createSyncStorage<T>(
save(data: T): void {
storage.setItem(key, JSON.stringify(data));
},
clear(): void {
storage.removeItem(key);
},
};
}
1 change: 0 additions & 1 deletion src/components/editor/editor-surface.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Resizer, { Orientation } from './resizer.svelte';
interface Props {
contentId: string;
model: editor.IModel;
widthStorage: SyncStorage<number>;
panel: Snippet<[{ resizer: Snippet, api: SurfaceApi }]>;
Expand Down
46 changes: 30 additions & 16 deletions src/components/editor/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
interface Props<L extends Language, I, O> {
contentId: string;
testsData: TestData<I, O>[];
initialValues: Record<L, Promise<string>>;
initialValues: Record<L, string>;
testRunnerFactories: Record<L, TestRunnerFactory<I, O>>;
}
Expand All @@ -43,34 +43,48 @@
let lang = $state(
initialLang in testRunnerFactories ? initialLang : defaultLang
);
let contentStorage = $derived(createSyncStorage(
sessionStorage,
`editor-${contentId}-${lang}`,
initialValues[lang],
));
const model = editor.createModel("");
function resetEditorContent () {
initialValues[lang].then(value => {
model.setValue(value);
})
}
$effect(() => {
model.setValue(contentStorage.load());
editor.setModelLanguage(model, MONACO_LANGUAGE_ID[lang]);
langStorage.save(lang);
resetEditorContent()
});
let monacoLang = $derived(MONACO_LANGUAGE_ID[lang]);
$effect(() => {
editor.setModelLanguage(model, monacoLang);
});
let saveCallbackId: NodeJS.Timeout
const disposable = model.onDidChangeContent(() => {
clearTimeout(saveCallbackId)
saveCallbackId = setTimeout(() => {
contentStorage.save(model.getValue());
}, 1000)
return () => {
clearTimeout(saveCallbackId)
}
})
return () => {
clearTimeout(saveCallbackId)
disposable.dispose()
}
})
const widthStorage = createSyncStorage(
localStorage,
"editor-width",
window.innerWidth - 800
);
function resetEditorContent () {
contentStorage.clear();
model.setValue(initialValues[lang]);
}
</script>

<EditorSurface contentId="{contentId}-{lang}" {model} {widthStorage} >
<EditorSurface {model} {widthStorage} >
{#snippet panel({ resizer, api })}
<Panel
{api}
Expand Down
15 changes: 10 additions & 5 deletions src/content/design-patterns/factory/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import { jsTestRunnerFactory, tsTestRunnerFactory } from './js/test-runners'
import { testRunnerFactory as pyTestRunnerFactory } from './python/test-runners'
const INITIAL_VALUES: Record<Language, Promise<string>> = {
[Language.PHP]: import('./php/code.php?raw').then(m => m.default),
[Language.TypeScript]: import('./js/code.ts?raw').then(m => m.default),
[Language.JavaScript]: import('./js/code.js?raw').then(m => m.default),
[Language.Python]: import('./python/code.py?raw').then(m => m.default),
import phpInitialValue from './php/code.php?raw'
import tsInitialValue from './js/code.ts?raw'
import jsInitialValue from './js/code.js?raw'
import pyInitialValue from './python/code.py?raw'
const INITIAL_VALUES: Record<Language, string> = {
[Language.PHP]: phpInitialValue,
[Language.TypeScript]: tsInitialValue,
[Language.JavaScript]: jsInitialValue,
[Language.Python]: pyInitialValue,
}
const TEST_RUNNER_FACTORIES: Record<Language, TestRunnerFactory<Input, Output>> = {
Expand Down
1 change: 1 addition & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface Position {
export interface SyncStorage<T> {
load(): T
save(data: T): void
clear(): void
}

0 comments on commit 86f5e66

Please sign in to comment.