5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-15 06:01:42 +00:00

guardar artículos para poder recuperarlos después

This commit is contained in:
f 2020-11-19 18:49:53 -03:00
parent 68ed757c79
commit 2608372889
2 changed files with 33 additions and 3 deletions

View file

@ -1,5 +1,28 @@
const origin = location.origin
/*
* Guarda una copia local de los cambios para poder recuperarlos
* después.
*
* Usamos la URL completa sin anchors.
*/
const storageKey = (editorEl) => window.location.href.split('#')[0] + '#' + editorEl.id
const storeContent = (editorEl, contentEl) => {
if (contentEl.innerText.trim().length === 0) return
window.localStorage.setItem(storageKey(editorEl), contentEl.innerHTML)
}
const restoreContent = (editorEl, contentEl) => {
const content = window.localStorage.getItem(storageKey(editorEl))
if (!content) return
if (content.trim().length === 0) return
contentEl.innerHTML = content
}
function uploadFile (file) {
return new Promise((resolve, reject) => {
const upload = new ActiveStorage.DirectUpload(
@ -431,8 +454,10 @@ function setupEditor (editorEl) {
const htmlEl = editorEl.querySelector("textarea")
const observer = new MutationObserver((mutationList, observer) => {
cleanContent(contentEl)
htmlEl.value = contentEl.innerHTML
fixContent(contentEl)
storeContent(editorEl, contentEl)
htmlEl.value = contentEl.innerHTML
})
observer.observe(contentEl, {
childList: true,
@ -485,8 +510,13 @@ function setupEditor (editorEl) {
}
cleanContent(contentEl)
htmlEl.value = contentEl.innerHTML
fixContent(contentEl)
// Recuperar el contenido si hay algo guardado
// TODO: Preguntar si se lo quiere recuperar.
restoreContent(editorEl, contentEl)
htmlEl.value = contentEl.innerHTML
}
// TODO: por ahora confiamos, quizás queremos filtrar estilos?

View file

@ -3,7 +3,7 @@
= render 'posts/attribute_feedback',
post: post, attribute: attribute, metadata: metadata
.editor
.editor{ id: attribute }
.alert.alert-info
:markdown
#{t('editor.alert')}