mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
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:
parent
39c31a3d76
commit
bf05a8da96
5 changed files with 33 additions and 1 deletions
|
@ -128,6 +128,15 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function _applyFormatting(dateTime, displayedTimezone, options) {
|
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 sameTimezone = _isEqualZones(displayedTimezone, moment.tz.guess());
|
||||||
const inCalendarRange = dateTime.isBetween(
|
const inCalendarRange = dateTime.isBetween(
|
||||||
moment().subtract(2, "days"),
|
moment().subtract(2, "days"),
|
||||||
|
@ -313,6 +322,7 @@
|
||||||
options.displayedTimezone = $element.attr("data-displayed-timezone");
|
options.displayedTimezone = $element.attr("data-displayed-timezone");
|
||||||
options.format =
|
options.format =
|
||||||
$element.attr("data-format") || (options.time ? "LLL" : "LL");
|
$element.attr("data-format") || (options.time ? "LLL" : "LL");
|
||||||
|
options.countdown = $element.attr("data-countdown");
|
||||||
|
|
||||||
processElement($element, options);
|
processElement($element, options);
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,7 +9,8 @@ function addLocalDate(buffer, matches, state) {
|
||||||
timezone: null,
|
timezone: null,
|
||||||
format: null,
|
format: null,
|
||||||
timezones: null,
|
timezones: null,
|
||||||
displayedTimezone: null
|
displayedTimezone: null,
|
||||||
|
countdown: null
|
||||||
};
|
};
|
||||||
|
|
||||||
let parsed = parseBBCodeTag(
|
let parsed = parseBBCodeTag(
|
||||||
|
@ -26,6 +27,7 @@ function addLocalDate(buffer, matches, state) {
|
||||||
config.recurring = parsed.attrs.recurring;
|
config.recurring = parsed.attrs.recurring;
|
||||||
config.timezones = parsed.attrs.timezones;
|
config.timezones = parsed.attrs.timezones;
|
||||||
config.displayedTimezone = parsed.attrs.displayedTimezone;
|
config.displayedTimezone = parsed.attrs.displayedTimezone;
|
||||||
|
config.countdown = parsed.attrs.countdown;
|
||||||
|
|
||||||
token = new state.Token("span_open", "span", 1);
|
token = new state.Token("span_open", "span", 1);
|
||||||
token.attrs = [["data-date", state.md.utils.escapeHtml(config.date)]];
|
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)]);
|
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) {
|
if (config.calendar) {
|
||||||
token.attrs.push([
|
token.attrs.push([
|
||||||
"data-calendar",
|
"data-calendar",
|
||||||
|
|
|
@ -12,6 +12,10 @@
|
||||||
&.past {
|
&.past {
|
||||||
border-bottom-color: $primary-low-mid;
|
border-bottom-color: $primary-low-mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&.past[data-countdown] {
|
||||||
|
color: $primary-medium;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@ en:
|
||||||
today: Today %{time}
|
today: Today %{time}
|
||||||
tomorrow: Tomorrow %{time}
|
tomorrow: Tomorrow %{time}
|
||||||
yesterday: Yesterday %{time}
|
yesterday: Yesterday %{time}
|
||||||
|
countdown:
|
||||||
|
passed: date has passed
|
||||||
title: Insert date / time
|
title: Insert date / time
|
||||||
create:
|
create:
|
||||||
form:
|
form:
|
||||||
|
|
|
@ -79,4 +79,11 @@ RSpec.describe "Local Dates" do
|
||||||
|
|
||||||
expect(cooked).not_to include("data-timezone=")
|
expect(cooked).not_to include("data-timezone=")
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue