From 6f2e378030b94f3a5194784439ec29303b67ecc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Wed, 21 Dec 2022 11:15:02 +0800 Subject: [PATCH] Replace html to text in title and note fields. --- app/assets/stylesheets/calendar.scss | 11 +++++- app/models/event.rb | 48 ++++++++++++++++++++++++ app/views/admin/calendars/_form.html.erb | 40 +++++++++++++++++++- config/routes.rb | 10 +++++ 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/calendar.scss b/app/assets/stylesheets/calendar.scss index ac9e21f..b3c81af 100644 --- a/app/assets/stylesheets/calendar.scss +++ b/app/assets/stylesheets/calendar.scss @@ -697,9 +697,12 @@ & > .modal-content { h3 { margin: 0; + white-space: pre; + } + & > .modal-body { + white-space: pre; } } - .event_summary { margin-right: -15px; margin-bottom: 15px; @@ -755,4 +758,10 @@ -webkit-box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5); box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5); } +} +.fc-event-title{ + white-space: pre; +} +div[contentEditable=true]{ + white-space: pre; } \ No newline at end of file diff --git a/app/models/event.rb b/app/models/event.rb index bdf7646..e8e8743 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -356,4 +356,52 @@ class Event recurring = recurring.concat(events) recurring end + def self.convertToText(str) + # Ensure string. + value = str.to_s + + # Convert encoding. + value = value.gsub(/ /i, ' ') + value = value.gsub(/&/i, '&') + + # Replace `
`. + value = value.gsub(/
/i, "\n") + + # Replace `
` (from Chrome). + value = value.gsub(/
/i, "\n") + + # Replace `

` (from IE). + value = value.gsub(/

/i, "\n") + + # Remove extra tags. + value = value.gsub(/<(.*?)>/, '') + + # Trim each line. + value = value.split("\n").map{|line| line.strip}.join("\n") + + # No more than 2x newline, per "paragraph". + value = value.gsub(/\n\n+/, "\n\n") + + # Clean up spaces. + value = value.gsub(/[ ]+/, ' ') + value = value.strip + + # Expose string. + return value + end + def self.fix_all_white_spaces + self.all.each do |event| + new_attrs = {} + localize_fields = ["title", "note"] + localize_fields.each do |f| + f_trans = "#{f}_translations" + new_attrs[f] = self.convertToText(event[f]) + new_attrs[f_trans] = {} + event[f_trans].each do |l, s| + new_attrs[f_trans][l] = self.convertToText(s) + end + end + Event.where(:id=>event.id).update_all(new_attrs) + end + end end \ No newline at end of file diff --git a/app/views/admin/calendars/_form.html.erb b/app/views/admin/calendars/_form.html.erb index 1c97a95..fbae446 100644 --- a/app/views/admin/calendars/_form.html.erb +++ b/app/views/admin/calendars/_form.html.erb @@ -226,12 +226,50 @@ return indexed_array; } + function convertToText(str){ + // Ensure string. + var value = String(str); + + // Convert encoding. + value = value.replace(/ /gi, ' '); + value = value.replace(/&/gi, '&'); + + // Replace `
`. + value = value.replace(/
/gi, '\n'); + + // Replace `

` (from Chrome). + value = value.replace(/
/gi, '\n'); + + // Replace `

` (from IE). + value = value.replace(/

/gi, '\n'); + + // Remove extra tags. + value = value.replace(/<(.*?)>/g, ''); + + // Trim each line. + value = value + .split('\n') + .map((line = '') => { + return line.trim(); + }) + .join('\n'); + + // No more than 2x newline, per "paragraph". + value = value.replace(/\n\n+/g, '\n\n'); + + // Clean up spaces. + value = value.replace(/[ ]+/g, ' '); + value = value.trim(); + + // Expose string. + return value; + }; function before_submit(ele){ var self = $(ele).parents('form') var content_editables = self.find('*[contenteditable="true"]'); for (var i=0,length = content_editables.length; i