Skip to content

A platform for community discussion. Free, open, simple.

FIX: Fix local-dates in non-post contexts, and in long topics (#14565)

David Taylor837bd5a6e5f2
diff --git a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js.es6 b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js.es6
index 2737417080f..e19c50702de 100644
--- a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js.es6
+++ b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js.es6
@@ -6,6 +6,7 @@ import { withPluginApi } from "discourse/lib/plugin-api";
import showModal from "discourse/lib/show-modal";
import { downloadCalendar } from "discourse/lib/download-calendar";
import { renderIcon } from "discourse-common/lib/icon-library";
+import I18n from "I18n";
export function applyLocalDates(dates, siteSettings) {
if (!siteSettings.discourse_local_dates_enabled) {
@@ -76,6 +77,9 @@ function _rangeElements(element) {
function initializeDiscourseLocalDates(api) {
const siteSettings = api.container.lookup("site-settings:main");
const chat = api.container.lookup("service:chat");
+ const defaultTitle = I18n.t("discourse_local_dates.default_title", {
+ site_name: siteSettings.title,
+ });
if (chat) {
chat.addToolbarButton({
@@ -99,11 +103,15 @@ function initializeDiscourseLocalDates(api) {
}
api.decorateCookedElement(
- (elem) => {
- applyLocalDates(
- elem.querySelectorAll(".discourse-local-date"),
- siteSettings
- );
+ (elem, helper) => {
+ const dates = elem.querySelectorAll(".discourse-local-date");
+
+ applyLocalDates(dates, siteSettings);
+
+ const topicTitle = helper?.getModel()?.topic?.title;
+ dates.forEach((date) => {
+ date.dataset.title = date.dataset.title || topicTitle || defaultTitle;
+ });
},
{ id: "discourse-local-date" }
);
@@ -207,11 +215,11 @@ function _downloadCalendarNode(element) {
.toISOString()
);
}
+ node.setAttribute("data-title", startDataset.title);
node.setAttribute(
- "data-title",
- document.querySelector("#topic-title a").innerText
+ "data-post-id",
+ element.closest("article")?.dataset?.postId
);
- node.setAttribute("data-post-id", element.closest("article").dataset.postId);
return node;
}
diff --git a/plugins/discourse-local-dates/config/locales/client.en.yml b/plugins/discourse-local-dates/config/locales/client.en.yml
index dafc1af05bd..a968b96f870 100644
--- a/plugins/discourse-local-dates/config/locales/client.en.yml
+++ b/plugins/discourse-local-dates/config/locales/client.en.yml
@@ -34,3 +34,4 @@ en:
every_three_months: "Every three months"
every_six_months: "Every six months"
every_year: "Every year"
+ default_title: "%{site_name} Event"
diff --git a/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js.es6 b/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js.es6
index 32d7963d9c0..25b3e4562be 100644
--- a/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js.es6
+++ b/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js.es6
@@ -57,7 +57,7 @@ acceptance(
assert.ok(!exists(document.querySelector("#discourse-modal-title")));
assert.ok(
window.open.calledWith(
- "https://www.google.com/calendar/event?action=TEMPLATE&text=Local%20dates%20&dates=20210930T110000Z/20210930T120000Z",
+ "https://www.google.com/calendar/event?action=TEMPLATE&text=Local%20dates&dates=20210930T110000Z/20210930T120000Z",
"_blank",
"noopener",
"noreferrer"