Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restore unsaved content #161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions apps/web/src/editor/input/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as monaco from "monaco-editor";
import { RefObject, useEffect } from "react";
import { SAMPLE_TAILWIND } from "../../samples/tailwind";
import { EditorState } from "../type";
import { StoreInterval } from "./input";
import { loadChangesFromIndexedDB } from "./load";
import { autoSaveWorkerURL, debounce, saveChangesToIndexedDB } from "./store";

const envDone = { current: false };

Expand Down Expand Up @@ -76,17 +79,29 @@ export const useEditorCreate = (params: Params): void => {
const container = containerRef.current;
if (container === null) throw Error("`container` is null");

createEnv();
const editor = monaco.editor.create(container, {
...OPTIONS,
value: SAMPLE_TAILWIND,
// value: "",
});
setEditor(editor);
(async () => {
const storedEditorValue = await loadChangesFromIndexedDB();

return () => {
setEditor(null);
editor.dispose();
};
createEnv();
const editor = monaco.editor.create(container, {
...OPTIONS,
value: storedEditorValue || SAMPLE_TAILWIND,
// value: "",
});

const autoSave = debounce(() => {
const editorValue: string = editor.getValue();
saveChangesToIndexedDB(editorValue);
}, StoreInterval);

editor.onDidChangeModelContent(autoSave);

setEditor(editor);

return () => {
setEditor(null);
editor.dispose();
};
})();
}, [setEditor, containerRef]);
};
8 changes: 8 additions & 0 deletions apps/web/src/editor/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ interface Props extends EditorState {
settings: Settings;
}

export enum EditorStoreName {
Database = "monacoEditorDB",
Object = "unsavedChanges",
Key = "data",
}

export const StoreInterval = 2000;

export const EditorInput = (props: Props): JSX.Element => {
const { setEditor, editor, settings } = props;

Expand Down
37 changes: 37 additions & 0 deletions apps/web/src/editor/input/load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { EditorStoreName } from "./input";

export const loadChangesFromIndexedDB = async (): Promise<string> => {
return new Promise<string>((resolve, reject) => {
const dbPromise = window.indexedDB.open(EditorStoreName.Database, 1);

dbPromise.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db = (event.target as IDBOpenDBRequest).result as IDBDatabase;
db.createObjectStore(EditorStoreName.Object, { autoIncrement: true });
};

dbPromise.onsuccess = (event: Event) => {
const db = (event.target as IDBOpenDBRequest).result as IDBDatabase;
const transaction: IDBTransaction = db.transaction(
EditorStoreName.Object,
"readonly"
);
const store: IDBObjectStore = transaction.objectStore(
EditorStoreName.Object
);

const request = store.get(EditorStoreName.Key);

request.onsuccess = () => {
resolve(request.result);
};

request.onerror = () => {
reject(request.error);
};
};

dbPromise.onerror = (event: Event) => {
reject((event.target as IDBOpenDBRequest).error);
};
});
};
55 changes: 55 additions & 0 deletions apps/web/src/editor/input/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { EditorStoreName } from "./input";

export const debounce = <T extends (...args: any[]) => void>(
fn: T,
delay: number
) => {
let timeoutId: ReturnType<typeof setTimeout> | null = null;

return ((...args: Parameters<T>) => {
if (timeoutId !== null) clearTimeout(timeoutId);

timeoutId = setTimeout(() => {
fn(...args);
timeoutId = null;
}, delay);
}) as T;
};

export const saveChangesToIndexedDB = async (
changes: string
): Promise<void> => {
return new Promise<void>((resolve, reject) => {
const dbPromise = window.indexedDB.open(EditorStoreName.Database, 1);

dbPromise.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db = (event.target as IDBOpenDBRequest).result as IDBDatabase;
db.createObjectStore(EditorStoreName.Object);
};

dbPromise.onsuccess = (event: Event) => {
const db = (event.target as IDBOpenDBRequest).result as IDBDatabase;
const transaction: IDBTransaction = db.transaction(
EditorStoreName.Object,
"readwrite"
);
const store: IDBObjectStore = transaction.objectStore(
EditorStoreName.Object
);

const request = store.put(changes, EditorStoreName.Key);

request.onsuccess = () => {
resolve();
};

request.onerror = () => {
reject(request.error);
};
};

dbPromise.onerror = (event: Event) => {
reject((event.target as IDBOpenDBRequest).error);
};
});
};