FIX: allowes forcing timezone displayed in local-dates

Use case being you might want to always display a specific timezone, but still have the possibility to show the tooltip with various timezones.
This commit is contained in:
Joffrey JAFFEUX 2018-09-12 15:54:36 +02:00 committed by Guo Xiang Tan
parent 33541c4096
commit 4b3c2490c3
4 changed files with 57 additions and 30 deletions

View File

@ -45,7 +45,10 @@
} }
}); });
var relativeTime = relativeTime.tz(moment.tz.guess()); var relativeTime = relativeTime.tz(
options.forceTimezone || moment.tz.guess()
);
if ( if (
options.format !== "YYYY-MM-DD HH:mm:ss" && options.format !== "YYYY-MM-DD HH:mm:ss" &&
relativeTime.isBetween( relativeTime.isBetween(
@ -65,18 +68,18 @@
var joinedPreviews = previews.join("\n"); var joinedPreviews = previews.join("\n");
var displayedTime = relativeTime.replace(
"TZ",
_formatTimezone(options.forceTimezone || moment.tz.guess()).join(": ")
);
$element $element
.html(html) .html(html)
.attr("title", joinedPreviews) .attr("title", joinedPreviews)
.attr("data-tooltip", joinedPreviews) .attr("data-tooltip", joinedPreviews)
.addClass("cooked") .addClass("cooked")
.find(".relative-time") .find(".relative-time")
.text( .text(displayedTime);
relativeTime.replace(
"TZ",
_formatTimezone(moment.tz.guess()).join(": ")
)
);
if (repeat) { if (repeat) {
this.timeout = setTimeout(function() { this.timeout = setTimeout(function() {
@ -94,6 +97,7 @@
options.time = $this.attr("data-time"); options.time = $this.attr("data-time");
options.recurring = $this.attr("data-recurring"); options.recurring = $this.attr("data-recurring");
options.timezones = $this.attr("data-timezones") || "Etc/UTC"; options.timezones = $this.attr("data-timezones") || "Etc/UTC";
options.forceTimezone = $this.attr("data-force-timezone");
processElement($this, options); processElement($this, options);
}); });

View File

@ -18,6 +18,7 @@ function addLocalDate(buffer, matches, state) {
config.date = parsed.attrs.date; config.date = parsed.attrs.date;
config.time = parsed.attrs.time; config.time = parsed.attrs.time;
config.forceTimezone = parsed.attrs.forceTimezone;
config.recurring = parsed.attrs.recurring; config.recurring = parsed.attrs.recurring;
config.format = parsed.attrs.format || config.format; config.format = parsed.attrs.format || config.format;
config.timezones = parsed.attrs.timezones || config.timezones; config.timezones = parsed.attrs.timezones || config.timezones;
@ -28,9 +29,15 @@ function addLocalDate(buffer, matches, state) {
["data-date", state.md.utils.escapeHtml(config.date)], ["data-date", state.md.utils.escapeHtml(config.date)],
["data-time", state.md.utils.escapeHtml(config.time)], ["data-time", state.md.utils.escapeHtml(config.time)],
["data-format", state.md.utils.escapeHtml(config.format)], ["data-format", state.md.utils.escapeHtml(config.format)],
["data-timezones", state.md.utils.escapeHtml(config.timezones)] ["data-timezones", state.md.utils.escapeHtml(config.timezones)],
]; ];
if (config.forceTimezone) {
token.attrs.push(
["data-force-timezone", state.md.utils.escapeHtml(config.forceTimezone)]
);
}
if (config.recurring) { if (config.recurring) {
token.attrs.push([ token.attrs.push([
"data-recurring", "data-recurring",

View File

@ -0,0 +1,38 @@
require 'rails_helper'
RSpec.describe "Local Dates" do
before do
freeze_time
end
it "should work" do
post = Fabricate(:post, raw: <<~SQL)
[date=2018-05-08 time=22:00 format="L LTS" forceTimezone="UTC" timezones="Europe/Paris|America/Los_Angeles"]
SQL
cooked = post.cooked
expect(cooked).to include('class="discourse-local-date"')
expect(cooked).to include('data-date="2018-05-08"')
expect(cooked).to include('data-format="L LTS"')
expect(cooked).to include('data-force-timezone="UTC"')
expect(cooked).to include(
'data-timezones="Europe/Paris|America/Los_Angeles"'
)
expect(cooked).to include('05/08/2018 3:00:00 PM (America: Los Angeles)')
expect(cooked).to include('05/09/2018 12:00:00 AM (Europe: Paris)')
end
it 'requires the right attributes to convert to a local date' do
post = Fabricate(:post, raw: <<~SQL)
[date]
SQL
cooked = post.cooked
expect(post.cooked).to include("<p>[date]</p>")
expect(cooked).to_not include('data-force-timezone=')
end
end

View File

@ -1,20 +1,6 @@
require 'rails_helper' require 'rails_helper'
describe PrettyText do describe PrettyText do
it 'supports inserting date' do
freeze_time
cooked = PrettyText.cook <<~MD
[date=2018-05-08 time=22:00 format="L LTS" timezones="Europe/Paris|America/Los_Angeles"]
MD
expect(cooked).to include('class="discourse-local-date"')
expect(cooked).to include('data-date="2018-05-08"')
expect(cooked).to include('data-format="L LTS"')
expect(cooked).to include('data-timezones="Europe/Paris|America/Los_Angeles"')
expect(cooked).to include('05/08/2018 3:00:00 PM (America: Los Angeles)')
expect(cooked).to include('05/09/2018 12:00:00 AM (Europe: Paris)')
end
it 'uses a simplified syntax in emails' do it 'uses a simplified syntax in emails' do
freeze_time freeze_time
cooked = PrettyText.cook <<~MD cooked = PrettyText.cook <<~MD
@ -26,12 +12,4 @@ describe PrettyText do
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail) expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
end end
it 'needs attributes to convert to a local date' do
cooked = PrettyText.cook <<~MD
[date]
MD
expect(cooked).to include("<p>[date]</p>")
end
end end