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
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",