Replace html to text in title and note fields.

This commit is contained in:
邱博亞 2022-12-21 11:15:02 +08:00
parent 61850ff461
commit 6f2e378030
4 changed files with 107 additions and 2 deletions

View File

@ -697,9 +697,12 @@
& > .modal-content { & > .modal-content {
h3 { h3 {
margin: 0; margin: 0;
white-space: pre;
}
& > .modal-body {
white-space: pre;
} }
} }
.event_summary { .event_summary {
margin-right: -15px; margin-right: -15px;
margin-bottom: 15px; margin-bottom: 15px;
@ -756,3 +759,9 @@
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;
}

View File

@ -356,4 +356,52 @@ class Event
recurring = recurring.concat(events) recurring = recurring.concat(events)
recurring recurring
end end
def self.convertToText(str)
# Ensure string.
value = str.to_s
# Convert encoding.
value = value.gsub(/ /i, ' ')
value = value.gsub(/&/i, '&')
# Replace `<br>`.
value = value.gsub(/<br>/i, "\n")
# Replace `<div>` (from Chrome).
value = value.gsub(/<div>/i, "\n")
# Replace `<p>` (from IE).
value = value.gsub(/<p>/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 end

View File

@ -226,12 +226,50 @@
return indexed_array; return indexed_array;
} }
function convertToText(str){
// Ensure string.
var value = String(str);
// Convert encoding.
value = value.replace(/&nbsp;/gi, ' ');
value = value.replace(/&amp;/gi, '&');
// Replace `<br>`.
value = value.replace(/<br>/gi, '\n');
// Replace `<div>` (from Chrome).
value = value.replace(/<div>/gi, '\n');
// Replace `<p>` (from IE).
value = value.replace(/<p>/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){ function before_submit(ele){
var self = $(ele).parents('form') var self = $(ele).parents('form')
var content_editables = self.find('*[contenteditable="true"]'); var content_editables = self.find('*[contenteditable="true"]');
for (var i=0,length = content_editables.length; i<length; i++){ for (var i=0,length = content_editables.length; i<length; i++){
var tp1 = content_editables.eq(i); var tp1 = content_editables.eq(i);
tp1.siblings('input[type="hidden"]').val(tp1.html()); tp1.siblings('input[type="hidden"]').val(convertToText(tp1.html()));
} }
self.submit(); self.submit();
} }

View File

@ -7,6 +7,7 @@ Rails.application.routes.draw do
update_flag = s.respond_to?(:tmp_flags) update_flag = s.respond_to?(:tmp_flags)
need_update = !update_flag || !(s.tmp_flags.include?('cf2')) need_update = !update_flag || !(s.tmp_flags.include?('cf2'))
need_update2 = !update_flag || !(s.tmp_flags.include?('cf3')) need_update2 = !update_flag || !(s.tmp_flags.include?('cf3'))
need_update3 = !update_flag || !(s.tmp_flags.include?('cf4'))
calendar_setting = CalendarSetting.first calendar_setting = CalendarSetting.first
calendar_setting = CalendarSetting.create if calendar_setting.nil? calendar_setting = CalendarSetting.create if calendar_setting.nil?
if need_update if need_update
@ -48,6 +49,15 @@ Rails.application.routes.draw do
end end
puts "Calendar fix!" puts "Calendar fix!"
end end
if need_update3
Event.fix_all_white_spaces
if update_flag
s = Site.first
s.tmp_flags << 'cf4'
s.save
end
puts "Calendar fix!"
end
end end
end end
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do