2021-04-28 18:48:50 +00:00
|
|
|
import { Editor } from "editor/editor";
|
2021-02-12 15:57:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Guarda una copia local de los cambios para poder recuperarlos
|
|
|
|
* después.
|
|
|
|
*
|
|
|
|
* Usamos la URL completa sin anchors.
|
|
|
|
*/
|
2021-04-28 18:48:50 +00:00
|
|
|
function getStorageKey(editor: Editor): string {
|
|
|
|
const keyEl = editor.editorEl.querySelector<HTMLInputElement>(
|
|
|
|
'[data-target="storage-key"]'
|
|
|
|
);
|
|
|
|
if (!keyEl)
|
|
|
|
throw new Error("No encuentro la llave para guardar los artículos");
|
|
|
|
return keyEl.value;
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:48:50 +00:00
|
|
|
export function forgetContent(storedKey: string): void {
|
|
|
|
window.localStorage.removeItem(storedKey);
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:48:50 +00:00
|
|
|
export function storeContent(editor: Editor): void {
|
|
|
|
if (editor.contentEl.innerText.trim().length === 0) return;
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-04-28 18:48:50 +00:00
|
|
|
window.localStorage.setItem(
|
|
|
|
getStorageKey(editor),
|
|
|
|
editor.contentEl.innerHTML
|
|
|
|
);
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 18:48:50 +00:00
|
|
|
export function restoreContent(editor: Editor): void {
|
|
|
|
const content = window.localStorage.getItem(getStorageKey(editor));
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-04-28 18:48:50 +00:00
|
|
|
if (!content) return;
|
|
|
|
if (content.trim().length === 0) return;
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-04-28 18:48:50 +00:00
|
|
|
editor.contentEl.innerHTML = content;
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|