5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 15:56: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. * 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) => { const storeContent = (editorEl, contentEl) => {
if (contentEl.innerText.trim().length === 0) return if (contentEl.innerText.trim().length === 0) return
@ -511,10 +511,6 @@ function setupEditor (editorEl) {
video.controls = false 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) cleanContent(contentEl)
fixContent(contentEl) fixContent(contentEl)
@ -539,5 +535,17 @@ document.addEventListener("turbolinks:load", () => {
setupEditor(editorEl) 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? if @post.persisted?
@site.touch @site.touch
forget_content
redirect_to site_post_path(@site, @post) redirect_to site_post_path(@site, @post)
else else
@ -110,6 +111,7 @@ class PostsController < ApplicationController
if service.update.persisted? if service.update.persisted?
@site.touch @site.touch
forget_content
redirect_to site_post_path(@site, @post) redirect_to site_post_path(@site, @post)
else else
@ -161,4 +163,10 @@ class PostsController < ApplicationController
l == params[:locale] l == params[:locale]
end end
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 end

View file

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

View file

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

View file

@ -4,6 +4,8 @@
post: post, attribute: attribute, metadata: metadata post: post, attribute: attribute, metadata: metadata
.editor{ id: attribute } .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 .alert.alert-info
:markdown :markdown
#{t('editor.alert')} #{t('editor.alert')}