FEATURE: adds countdown attribute to [date] (#8037)

When used, dates will be displayed as a countdown in a human friendly way.
This commit is contained in:
Joffrey JAFFEUX 2019-08-24 11:39:20 -05:00 committed by GitHub
parent 39c31a3d76
commit bf05a8da96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 1 deletions

View File

@ -128,6 +128,15 @@
}
function _applyFormatting(dateTime, displayedTimezone, options) {
if (options.countdown) {
const diffTime = dateTime.diff(moment());
if (diffTime < 0) {
return I18n.t("discourse_local_dates.relative_dates.countdown.passed");
} else {
return moment.duration(diffTime).humanize();
}
}
const sameTimezone = _isEqualZones(displayedTimezone, moment.tz.guess());
const inCalendarRange = dateTime.isBetween(
moment().subtract(2, "days"),
@ -313,6 +322,7 @@
options.displayedTimezone = $element.attr("data-displayed-timezone");
options.format =
$element.attr("data-format") || (options.time ? "LLL" : "LL");
options.countdown = $element.attr("data-countdown");
processElement($element, options);
});

View File

@ -9,7 +9,8 @@ function addLocalDate(buffer, matches, state) {
timezone: null,
format: null,
timezones: null,
displayedTimezone: null
displayedTimezone: null,
countdown: null
};
let parsed = parseBBCodeTag(
@ -26,6 +27,7 @@ function addLocalDate(buffer, matches, state) {
config.recurring = parsed.attrs.recurring;
config.timezones = parsed.attrs.timezones;
config.displayedTimezone = parsed.attrs.displayedTimezone;
config.countdown = parsed.attrs.countdown;
token = new state.Token("span_open", "span", 1);
token.attrs = [["data-date", state.md.utils.escapeHtml(config.date)]];
@ -57,6 +59,13 @@ function addLocalDate(buffer, matches, state) {
token.attrs.push(["data-format", state.md.utils.escapeHtml(config.format)]);
}
if (config.countdown) {
token.attrs.push([
"data-countdown",
state.md.utils.escapeHtml(config.countdown)
]);
}
if (config.calendar) {
token.attrs.push([
"data-calendar",

View File

@ -12,6 +12,10 @@
&.past {
border-bottom-color: $primary-low-mid;
}
&.past[data-countdown] {
color: $primary-medium;
}
}
}

View File

@ -5,6 +5,8 @@ en:
today: Today %{time}
tomorrow: Tomorrow %{time}
yesterday: Yesterday %{time}
countdown:
passed: date has passed
title: Insert date / time
create:
form:

View File

@ -79,4 +79,11 @@ RSpec.describe "Local Dates" do
expect(cooked).not_to include("data-timezone=")
end
it 'supports countdowns' do
raw = "[date=2018-11-01 time=12:00 countdown=true]"
cooked = Fabricate(:post, raw: raw).cooked
expect(cooked).to include("data-countdown=")
end
end