DEV: Include helper in core for applyLocalDates (#16325)

This commit is contained in:
Penar Musaraj 2022-03-30 20:58:38 -04:00 committed by GitHub
parent 57b20393ac
commit 943083f4df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 17 deletions

View File

@ -0,0 +1,11 @@
export function applyLocalDates(dates, siteSettings) {
if (!siteSettings.discourse_local_dates_enabled) {
return;
}
const _applyLocalDates = requirejs(
"discourse/plugins/discourse-local-dates/initializers/discourse-local-dates"
).applyLocalDates;
_applyLocalDates(dates, siteSettings);
}

View File

@ -13,6 +13,7 @@ import { notEmpty } from "@ember/object/computed";
import { propertyNotEqual } from "discourse/lib/computed"; import { propertyNotEqual } from "discourse/lib/computed";
import { schedule } from "@ember/runloop"; import { schedule } from "@ember/runloop";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import { applyLocalDates } from "discourse/lib/local-dates";
export default Component.extend({ export default Component.extend({
timeFormat: "HH:mm:ss", timeFormat: "HH:mm:ss",
@ -56,19 +57,21 @@ export default Component.extend({
}); });
}, },
@observes("markup") @observes("computedConfig.{from,to,options}", "options", "isValid", "isRange")
_renderPreview() { _renderPreview() {
discourseDebounce( discourseDebounce(
this, this,
function () { function () {
const markup = this.markup; const markup = this.markup;
if (markup) { if (markup) {
cookAsync(markup).then((result) => { cookAsync(markup).then((result) => {
this.set("currentPreview", result); this.set("currentPreview", result);
schedule("afterRender", () => schedule("afterRender", () => {
this.$(".preview .discourse-local-date").applyLocalDates() applyLocalDates(
); document.querySelectorAll(".preview .discourse-local-date"),
this.siteSettings
);
});
}); });
} }
}, },
@ -321,7 +324,6 @@ export default Component.extend({
text = this._generateDateMarkup(config.from, options, isRange); text = this._generateDateMarkup(config.from, options, isRange);
} }
} }
return text; return text;
}, },

View File

@ -8,6 +8,7 @@ import { downloadCalendar } from "discourse/lib/download-calendar";
import { renderIcon } from "discourse-common/lib/icon-library"; import { renderIcon } from "discourse-common/lib/icon-library";
import I18n from "I18n"; import I18n from "I18n";
// Import applyLocalDates from discourse/lib/local-dates instead
export function applyLocalDates(dates, siteSettings) { export function applyLocalDates(dates, siteSettings) {
if (!siteSettings.discourse_local_dates_enabled) { if (!siteSettings.discourse_local_dates_enabled) {
return; return;

View File

@ -1,6 +1,11 @@
import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers"; import {
acceptance,
query,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit"; import { test } from "qunit";
import { click, fillIn, visit } from "@ember/test-helpers"; import { click, fillIn, visit } from "@ember/test-helpers";
import selectKit from "discourse/tests/helpers/select-kit-helper";
acceptance("Local Dates - composer", function (needs) { acceptance("Local Dates - composer", function (needs) {
needs.user(); needs.user();
@ -61,4 +66,19 @@ acceptance("Local Dates - composer", function (needs) {
); );
assert.notOk(getAttr("time"), "it doesnt have time"); assert.notOk(getAttr("time"), "it doesnt have time");
}); });
test("date modal", async function (assert) {
await visit("/");
await click("#create-topic");
await click(".d-editor-button-bar .local-dates");
const timezoneChooser = selectKit(".timezone-input");
await timezoneChooser.expand();
await timezoneChooser.selectRowByValue("Asia/Macau");
assert.ok(
query(".preview .discourse-local-date").textContent.includes("Macau"),
"it outputs a preview date in selected timezone"
);
});
}); });

View File

@ -14,17 +14,15 @@ import { relativeAge } from "discourse/lib/formatter";
import round from "discourse/lib/round"; import round from "discourse/lib/round";
import showModal from "discourse/lib/show-modal"; import showModal from "discourse/lib/show-modal";
import bootbox from "bootbox"; import bootbox from "bootbox";
import { applyLocalDates } from "discourse/lib/local-dates";
const FETCH_VOTERS_COUNT = 25; const FETCH_VOTERS_COUNT = 25;
function optionHtml(option) { function optionHtml(option, siteSettings = {}) {
const $node = $(`<span>${option.html}</span>`); const el = document.createElement("span");
el.innerHTML = option.html;
$node.find(".discourse-local-date").each((_index, elem) => { applyLocalDates(el.querySelectorAll(".discourse-local-date"), siteSettings);
$(elem).applyLocalDates(); return new RawHtml({ html: `<span>${el.innerHTML}</span>` });
});
return new RawHtml({ html: `<span>${$node.html()}</span>` });
} }
function infoTextHtml(text) { function infoTextHtml(text) {
@ -66,7 +64,7 @@ createWidget("discourse-poll-option", {
} }
contents.push(" "); contents.push(" ");
contents.push(optionHtml(option)); contents.push(optionHtml(option, this.siteSettings));
return contents; return contents;
}, },
@ -178,7 +176,10 @@ createWidget("discourse-poll-standard-results", {
contents.push( contents.push(
h( h(
"div.option", "div.option",
h("p", [h("span.percentage", `${per}%`), optionHtml(option)]) h("p", [
h("span.percentage", `${per}%`),
optionHtml(option, this.siteSettings),
])
) )
); );