Skip to content

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

FIX: Respect custom date format selection in local dates (#36860)

Régis Hanoladbb820c9462
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/dates.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/dates.gjs
index 28519d26a6c..18cc3aaef72 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/dates.gjs
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/dates.gjs
@@ -138,6 +138,7 @@ export default class DiscoursePostEventDates extends Component {
format,
timezone: date.tz(),
postId: this.args.event.id,
+ calendar: "on",
};
if (this.args.event.showLocalTime) {
diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.gjs b/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.gjs
index 6ba01fed44a..ff6fda8b0f2 100644
--- a/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.gjs
+++ b/plugins/discourse-local-dates/assets/javascripts/discourse/components/modal/local-dates-create.gjs
@@ -127,7 +127,7 @@ export default class LocalDatesCreate extends Component {
}
let format = options.format;
- if (timeInferred && this.formats.includes(format)) {
+ if (timeInferred && (format === "LLL" || format === "LLLL")) {
format = "LL";
}
@@ -160,7 +160,7 @@ export default class LocalDatesCreate extends Component {
}
let format = options.format;
- if (timeInferred && this.formats.includes(format)) {
+ if (timeInferred && (format === "LLL" || format === "LLLL")) {
format = "LL";
}
diff --git a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js
index e87cd6b15ba..b304662b871 100644
--- a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js
+++ b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js
@@ -94,10 +94,12 @@ function buildOptionsFromElement(element, siteSettings) {
.split("|")
.filter(Boolean);
opts.timezone = dataset.timezone;
- opts.calendar = (dataset.calendar || "on") === "on";
opts.displayedTimezone = dataset.displayedTimezone;
opts.format = dataset.format || (opts.time ? "LLL" : "LL");
opts.countdown = dataset.countdown;
+ opts.calendar = dataset.calendar
+ ? dataset.calendar === "on"
+ : !dataset.format;
return opts;
}
diff --git a/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-composer-test.js b/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-composer-test.js
index 4434551b0f1..37e26c1d69a 100644
--- a/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-composer-test.js
+++ b/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-composer-test.js
@@ -153,4 +153,28 @@ acceptance("Local Dates - composer", function (needs) {
"it adds the current date"
);
});
+
+ test("date modal - selecting format preserves it in output", async function (assert) {
+ await visit("/");
+ await click("#create-topic");
+ const categoryChooser = selectKit(".category-chooser");
+ await categoryChooser.expand();
+ await categoryChooser.selectRowByValue(2);
+ await click(".toolbar-menu__options-trigger");
+ await click("[data-name='local-dates']");
+
+ await click(".advanced-mode-btn");
+
+ // Select LTS format (time-only format) - second item in the list
+ await click("ul.formats li.format:nth-child(2) a.moment-format");
+ assert.dom("input.format-input").hasValue("LTS");
+
+ // Insert the date
+ await click(".btn-primary");
+
+ // Verify the format is preserved in the output (not changed to LL)
+ assert
+ .dom(".d-editor-input")
+ .hasValue(/format=LTS/, "preserves LTS format in output");
+ });
});
diff --git a/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js b/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js
index 8ac75b81686..dcef2ef90b5 100644
--- a/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js
+++ b/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js
@@ -72,4 +72,54 @@ module("Unit | discourse-local-dates", function (hooks) {
}
);
});
+
+ test("applyLocalDates disables calendar mode when custom format is specified", function (assert) {
+ const element = createElementFromHTML(
+ "<span " +
+ 'data-date="2022-10-06" ' +
+ 'data-time="17:21:00" ' +
+ 'class="discourse-local-date" ' +
+ 'data-format="LLL" ' +
+ 'data-timezone="Asia/Singapore">' +
+ "</span>"
+ );
+
+ freezeTime(
+ { date: "2022-10-06T10:10:10", timezone: "Asia/Singapore" },
+ () => {
+ applyLocalDates([element], {
+ discourse_local_dates_enabled: true,
+ });
+
+ // With calendar mode, this would show "Today 5:21 PM"
+ // With calendar disabled (due to custom format), it shows full date
+ assert
+ .dom(".relative-time", element)
+ .hasText("October 6, 2022 5:21 PM");
+ }
+ );
+ });
+
+ test("applyLocalDates uses calendar mode when no custom format is specified", function (assert) {
+ const element = createElementFromHTML(
+ "<span " +
+ 'data-date="2022-10-06" ' +
+ 'data-time="17:21:00" ' +
+ 'class="discourse-local-date" ' +
+ 'data-timezone="Asia/Singapore">' +
+ "</span>"
+ );
+
+ freezeTime(
+ { date: "2022-10-06T10:10:10", timezone: "Asia/Singapore" },
+ () => {
+ applyLocalDates([element], {
+ discourse_local_dates_enabled: true,
+ });
+
+ // Without custom format, calendar mode is enabled, showing relative date
+ assert.dom(".relative-time", element).hasText("Today 5:21 PM");
+ }
+ );
+ });
});