From bd0947c95675108fd45d097232f6c24a8b33f7f4 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 20 Nov 2020 15:01:13 -0300 Subject: [PATCH] olvidar contenido cuando se confirma el guardado --- app/assets/javascripts/02-editor.js | 22 +++++++++++++++------- app/controllers/posts_controller.rb | 8 ++++++++ app/views/layouts/_flash.haml | 4 ++-- app/views/layouts/application.html.haml | 2 ++ app/views/posts/attributes/_content.haml | 2 ++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/02-editor.js b/app/assets/javascripts/02-editor.js index 1406f6d8..68d9bd33 100644 --- a/app/assets/javascripts/02-editor.js +++ b/app/assets/javascripts/02-editor.js @@ -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: + } +}) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f727da61..d60879d7 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/views/layouts/_flash.haml b/app/views/layouts/_flash.haml index 23d8035b..149f946f 100644 --- a/app/views/layouts/_flash.haml +++ b/app/views/layouts/_flash.haml @@ -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 diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 8a33f8b1..4049bf10 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -27,3 +27,5 @@ %body{ class: yield(:body) } .container-fluid#sutty = yield + - if flash[:js] + .js-flash.d-none{ data: flash[:js] } diff --git a/app/views/posts/attributes/_content.haml b/app/views/posts/attributes/_content.haml index d9395dec..d1d1ecf0 100644 --- a/app/views/posts/attributes/_content.haml +++ b/app/views/posts/attributes/_content.haml @@ -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')}