5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 09:06:21 +00:00

olvidar contenido cuando se confirma el guardado

This commit is contained in:
f 2020-11-20 15:01:13 -03:00
parent e51da1e6ae
commit bd0947c956
5 changed files with 29 additions and 9 deletions

View file

@ -6,9 +6,9 @@ const origin = location.origin
*
* Usamos la URL completa sin anchors.
*/
const storageKey = (editorEl) => window.location.href.split('#')[0] + '#' + editorEl.id
const storageKey = (editorEl) => editorEl.querySelector('[data-target="storage-key"]').value
const forgetContent = (editorEl) => window.localStorage.removeItem(storageKey(editorEl))
const forgetContent = (storedKey) => window.localStorage.removeItem(storedKey)
const storeContent = (editorEl, contentEl) => {
if (contentEl.innerText.trim().length === 0) return
@ -511,10 +511,6 @@ function setupEditor (editorEl) {
video.controls = false
}
// Eliminar el contenido al enviar los cambios válidos. Esto no nos
// protege frente a errores en el servidor!
editorEl.closest('form').addEventListener('submit', event => forgetContent(editorEl))
cleanContent(contentEl)
fixContent(contentEl)
@ -539,5 +535,17 @@ document.addEventListener("turbolinks:load", () => {
setupEditor(editorEl)
}
})
const flash = document.querySelector('.js-flash[data-target="editor"]')
if (!flash) return
switch (flash.dataset.action) {
case 'forget-content':
if (!flash.dataset.keys) break
try { JSON.parse(flash.dataset.keys).forEach(forgetContent) } catch { }
break
default:
}
})

View file

@ -81,6 +81,7 @@ class PostsController < ApplicationController
if @post.persisted?
@site.touch
forget_content
redirect_to site_post_path(@site, @post)
else
@ -110,6 +111,7 @@ class PostsController < ApplicationController
if service.update.persisted?
@site.touch
forget_content
redirect_to site_post_path(@site, @post)
else
@ -161,4 +163,10 @@ class PostsController < ApplicationController
l == params[:locale]
end
end
# Instruye al editor a olvidarse el contenido del artículo. Usar
# cuando hayamos guardado la información correctamente.
def forget_content
flash[:js] = { target: 'editor', action: 'forget-content', keys: params[:storage_keys].to_json }
end
end

View file

@ -1,3 +1,3 @@
- flash.each do |type, message|
.alert{ role: 'alert', class: "alert-#{type}" }
= message
- unless type == 'js'
.alert{ role: 'alert', class: "alert-#{type}" }= message

View file

@ -27,3 +27,5 @@
%body{ class: yield(:body) }
.container-fluid#sutty
= yield
- if flash[:js]
.js-flash.d-none{ data: flash[:js] }

View file

@ -4,6 +4,8 @@
post: post, attribute: attribute, metadata: metadata
.editor{ id: attribute }
-# Esto es para luego decirle al navegador que se olvide estas cosas.
= hidden_field_tag 'storage_keys[]', "#{request.original_url}##{attribute}", data: { target: 'storage-key' }
.alert.alert-info
:markdown
#{t('editor.alert')}