From 2c5465fc0d9b062997035dcc6263907774789151 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 23 Oct 2024 16:18:12 -0300 Subject: [PATCH] fix: no detectar espaciado como cambios #17649 --- .../controllers/unsaved_changes_controller.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/javascript/controllers/unsaved_changes_controller.js b/app/javascript/controllers/unsaved_changes_controller.js index 08ac4a9b..e7c03627 100644 --- a/app/javascript/controllers/unsaved_changes_controller.js +++ b/app/javascript/controllers/unsaved_changes_controller.js @@ -2,8 +2,7 @@ import { Controller } from "@hotwired/stimulus"; export default class extends Controller { connect() { - this.originalFormData = new FormData(this.element); - this.originalFormDataSerialized = this.serializeFormData(this.originalFormData); + this.originalFormDataSerialized = this.serializeFormData(this.element); this.submitting = false; } @@ -33,13 +32,24 @@ export default class extends Controller { event.preventDefault(); } - serializeFormData(formData) { + formData(form) { + const formData = new FormData(form); + formData.delete("authenticity_token"); - return (new URLSearchParams(formData)).toString();; + return formData; + } + + /* + * Elimina saltos de línea y espacios al serializar, para evitar + * detectar cambios cuando cambió el espaciado, por ejemplo cuando el + * editor con formato aplica espacios o elimina saltos de línea. + */ + serializeFormData(form) { + return (new URLSearchParams(this.formData(form))).toString().replaceAll("+", "").replaceAll("%0A", ""); } hasChanged() { - return (this.originalFormDataSerialized !== this.serializeFormData(new FormData(this.element))); + return (this.originalFormDataSerialized !== this.serializeFormData(this.element)); } }