Save scene in URL (#220)

Co-authored-by: Christopher Chedeau <vjeuxx@gmail.com>
This commit is contained in:
Timur Khazamov 2020-01-07 08:08:23 +05:00 committed by Christopher Chedeau
parent abbc04df0e
commit db973c61e8
3 changed files with 40 additions and 3 deletions

View file

@ -181,3 +181,22 @@ export function saveToLocalStorage(
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(elements));
localStorage.setItem(LOCAL_STORAGE_KEY_STATE, JSON.stringify(state));
}
export function restoreFromURL(elements: ExcalidrawElement[]) {
try {
const [savedElements, savedState] = document.location.hash
.slice(1)
.split(":")
.map(atob);
return restore(elements, savedElements, savedState);
} catch (ex) {
return null;
}
}
export function saveToURL(elements: ExcalidrawElement[], state: AppState) {
const hash = [JSON.stringify(elements), JSON.stringify(state)]
.map(btoa)
.join(":");
document.location.replace("#" + hash);
}

View file

@ -13,7 +13,9 @@ export {
loadFromJSON,
saveAsJSON,
restoreFromLocalStorage,
saveToLocalStorage
saveToLocalStorage,
restoreFromURL,
saveToURL
} from "./data";
export { hasBackground, hasStroke, getElementAtPosition } from "./comparisons";
export { createScene } from "./createScene";