From ad39446202d3f054be3455193e6f5da7d76862f0 Mon Sep 17 00:00:00 2001 From: YUCLing Date: Fri, 27 Sep 2024 23:25:01 +0800 Subject: [PATCH 1/8] chore: humanTime improvements --- framework/core/js/src/common/utils/humanTime.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/framework/core/js/src/common/utils/humanTime.ts b/framework/core/js/src/common/utils/humanTime.ts index 870153ae28..bcaf681fe3 100644 --- a/framework/core/js/src/common/utils/humanTime.ts +++ b/framework/core/js/src/common/utils/humanTime.ts @@ -15,14 +15,12 @@ export default function humanTime(time: dayjs.ConfigType): string { d = now; } - const day = 864e5; - const diff = d.diff(dayjs()); let ago: string; // If this date was more than a month ago, we'll show the name of the month // in the string. If it wasn't this year, we'll show the year as well. - if (diff < -30 * day) { - if (d.year() === dayjs().year()) { + if (d.diff(now, 'day') < -30) { + if (d.isSame(now, 'year')) { ago = d.format('D MMM'); } else { ago = d.format('ll'); From 2b5dd57b8deb443d480f1c6d575ef463cbb3136b Mon Sep 17 00:00:00 2001 From: YUCLing Date: Fri, 27 Sep 2024 23:25:45 +0800 Subject: [PATCH 2/8] chore: correct liveHumanTimes comment --- framework/core/js/src/common/utils/liveHumanTimes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/core/js/src/common/utils/liveHumanTimes.ts b/framework/core/js/src/common/utils/liveHumanTimes.ts index cea16fde59..bcb0d53754 100644 --- a/framework/core/js/src/common/utils/liveHumanTimes.ts +++ b/framework/core/js/src/common/utils/liveHumanTimes.ts @@ -10,7 +10,7 @@ function updateHumanTimes() { } /** - * The `liveHumanTimes` initializer sets up a loop every 1 second to update + * The `liveHumanTimes` initializer sets up a loop every 10 seconds to update * timestamps rendered with the `humanTime` helper. */ export default function liveHumanTimes() { From 8a156c7b224677835816dc0f77fdf1671a76b6ca Mon Sep 17 00:00:00 2001 From: YUCLing Date: Fri, 27 Sep 2024 23:26:32 +0800 Subject: [PATCH 3/8] add: flarum customized time formats --- framework/core/js/src/common/index.ts | 2 ++ .../core/js/src/common/utils/dayjsPlugins.ts | 21 +++++++++++++++++++ .../core/js/src/common/utils/humanTime.ts | 2 +- .../js/src/forum/components/PostStream.js | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 framework/core/js/src/common/utils/dayjsPlugins.ts diff --git a/framework/core/js/src/common/index.ts b/framework/core/js/src/common/index.ts index 0b7deb7004..ddb1b16e2e 100644 --- a/framework/core/js/src/common/index.ts +++ b/framework/core/js/src/common/index.ts @@ -11,9 +11,11 @@ import 'jquery.hotkeys/jquery.hotkeys'; import relativeTime from 'dayjs/plugin/relativeTime'; import localizedFormat from 'dayjs/plugin/localizedFormat'; +import { customFormats } from './utils/dayjsPlugins'; dayjs.extend(relativeTime); dayjs.extend(localizedFormat); +dayjs.extend(customFormats); import './registry'; diff --git a/framework/core/js/src/common/utils/dayjsPlugins.ts b/framework/core/js/src/common/utils/dayjsPlugins.ts new file mode 100644 index 0000000000..f999b05dab --- /dev/null +++ b/framework/core/js/src/common/utils/dayjsPlugins.ts @@ -0,0 +1,21 @@ +export const customFormats: import('dayjs').PluginFunc = function (_option, c, _factory) { + const proto = c.prototype; + const oldFormat = proto.format; + + const t = (format?: string) => + format?.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)) + + const englishFormats: Record = { + F: "DD MMMM", + FF: "MMMM YYYY" + }; + + proto.format = function(template) { + const { formats = {} } = (this as any).$locale(); + const result = template?.replace(/(\[[^\]]+])|(f{1,2}|F{1,2})/g, (_, a, b) => { + const B = b && b.toUpperCase(); + return a || formats[b] || englishFormats[b] || t(formats[B]); + }); + return oldFormat.call(this, result); + } +} \ No newline at end of file diff --git a/framework/core/js/src/common/utils/humanTime.ts b/framework/core/js/src/common/utils/humanTime.ts index bcaf681fe3..d450c7df4c 100644 --- a/framework/core/js/src/common/utils/humanTime.ts +++ b/framework/core/js/src/common/utils/humanTime.ts @@ -21,7 +21,7 @@ export default function humanTime(time: dayjs.ConfigType): string { // in the string. If it wasn't this year, we'll show the year as well. if (d.diff(now, 'day') < -30) { if (d.isSame(now, 'year')) { - ago = d.format('D MMM'); + ago = d.format('f'); } else { ago = d.format('ll'); } diff --git a/framework/core/js/src/forum/components/PostStream.js b/framework/core/js/src/forum/components/PostStream.js index 288c2d9fb2..1d18e6d7e7 100644 --- a/framework/core/js/src/forum/components/PostStream.js +++ b/framework/core/js/src/forum/components/PostStream.js @@ -270,7 +270,7 @@ export default class PostStream extends Component { // set the index to the last post. this.stream.index = indexFromViewPort !== null ? indexFromViewPort + 1 : this.stream.count(); this.stream.visible = visible; - if (period) this.stream.description = dayjs(period).format('MMMM YYYY'); + if (period) this.stream.description = dayjs(period).format('FF'); } /** From 8bc69c8d216ad62dcafcb4dce12218768e452204 Mon Sep 17 00:00:00 2001 From: YUCLing Date: Fri, 27 Sep 2024 23:50:17 +0800 Subject: [PATCH 4/8] chore: quote styles --- framework/core/js/src/common/utils/dayjsPlugins.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/core/js/src/common/utils/dayjsPlugins.ts b/framework/core/js/src/common/utils/dayjsPlugins.ts index f999b05dab..4ccbea4623 100644 --- a/framework/core/js/src/common/utils/dayjsPlugins.ts +++ b/framework/core/js/src/common/utils/dayjsPlugins.ts @@ -6,8 +6,8 @@ export const customFormats: import('dayjs').PluginFunc = function (_option, c, _ format?.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)) const englishFormats: Record = { - F: "DD MMMM", - FF: "MMMM YYYY" + F: 'DD MMMM', + FF: 'MMMM YYYY' }; proto.format = function(template) { From 86df915944e874cfadddc13b0bf7a2f803fc609d Mon Sep 17 00:00:00 2001 From: YUCLing Date: Fri, 27 Sep 2024 23:52:59 +0800 Subject: [PATCH 5/8] add: fallback for uppercased formats --- framework/core/js/src/common/utils/dayjsPlugins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/core/js/src/common/utils/dayjsPlugins.ts b/framework/core/js/src/common/utils/dayjsPlugins.ts index 4ccbea4623..07079a1469 100644 --- a/framework/core/js/src/common/utils/dayjsPlugins.ts +++ b/framework/core/js/src/common/utils/dayjsPlugins.ts @@ -14,7 +14,7 @@ export const customFormats: import('dayjs').PluginFunc = function (_option, c, _ const { formats = {} } = (this as any).$locale(); const result = template?.replace(/(\[[^\]]+])|(f{1,2}|F{1,2})/g, (_, a, b) => { const B = b && b.toUpperCase(); - return a || formats[b] || englishFormats[b] || t(formats[B]); + return a || formats[b] || englishFormats[b] || t(formats[B]) || t(englishFormats[B]); }); return oldFormat.call(this, result); } From 5ea7616514fc7cb08bf22a8e77c09563c5bf94e5 Mon Sep 17 00:00:00 2001 From: YUCLing Date: Sat, 28 Sep 2024 00:47:51 +0800 Subject: [PATCH 6/8] chore: fix code style --- .../core/js/src/common/utils/dayjsPlugins.ts | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/framework/core/js/src/common/utils/dayjsPlugins.ts b/framework/core/js/src/common/utils/dayjsPlugins.ts index 07079a1469..5fc04ec19c 100644 --- a/framework/core/js/src/common/utils/dayjsPlugins.ts +++ b/framework/core/js/src/common/utils/dayjsPlugins.ts @@ -1,21 +1,20 @@ export const customFormats: import('dayjs').PluginFunc = function (_option, c, _factory) { - const proto = c.prototype; - const oldFormat = proto.format; + const proto = c.prototype; + const oldFormat = proto.format; - const t = (format?: string) => - format?.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)) + const t = (format?: string) => format?.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)); - const englishFormats: Record = { - F: 'DD MMMM', - FF: 'MMMM YYYY' - }; + const englishFormats: Record = { + F: 'DD MMMM', + FF: 'MMMM YYYY', + }; - proto.format = function(template) { - const { formats = {} } = (this as any).$locale(); - const result = template?.replace(/(\[[^\]]+])|(f{1,2}|F{1,2})/g, (_, a, b) => { - const B = b && b.toUpperCase(); - return a || formats[b] || englishFormats[b] || t(formats[B]) || t(englishFormats[B]); - }); - return oldFormat.call(this, result); - } -} \ No newline at end of file + proto.format = function (template) { + const { formats = {} } = (this as any).$locale(); + const result = template?.replace(/(\[[^\]]+])|(f{1,2}|F{1,2})/g, (_, a, b) => { + const B = b && b.toUpperCase(); + return a || formats[b] || englishFormats[b] || t(formats[B]) || t(englishFormats[B]); + }); + return oldFormat.call(this, result); + }; +}; From 2164d9e12320d598486f0eeb85d7e2727d4a41ac Mon Sep 17 00:00:00 2001 From: YUCLing Date: Sat, 28 Sep 2024 02:30:57 +0800 Subject: [PATCH 7/8] fix: dayjsPlugins cannot be added to reg --- framework/core/js/src/common/index.ts | 4 ++-- framework/core/js/src/common/utils/dayjsPlugins.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/core/js/src/common/index.ts b/framework/core/js/src/common/index.ts index ddb1b16e2e..0f64e66739 100644 --- a/framework/core/js/src/common/index.ts +++ b/framework/core/js/src/common/index.ts @@ -11,11 +11,11 @@ import 'jquery.hotkeys/jquery.hotkeys'; import relativeTime from 'dayjs/plugin/relativeTime'; import localizedFormat from 'dayjs/plugin/localizedFormat'; -import { customFormats } from './utils/dayjsPlugins'; +import dayjsPlugins from './utils/dayjsPlugins'; dayjs.extend(relativeTime); dayjs.extend(localizedFormat); -dayjs.extend(customFormats); +dayjs.extend(dayjsPlugins.customFormats); import './registry'; diff --git a/framework/core/js/src/common/utils/dayjsPlugins.ts b/framework/core/js/src/common/utils/dayjsPlugins.ts index 5fc04ec19c..27787d81ac 100644 --- a/framework/core/js/src/common/utils/dayjsPlugins.ts +++ b/framework/core/js/src/common/utils/dayjsPlugins.ts @@ -1,4 +1,4 @@ -export const customFormats: import('dayjs').PluginFunc = function (_option, c, _factory) { +const customFormats: import('dayjs').PluginFunc = function (_option, c, _factory) { const proto = c.prototype; const oldFormat = proto.format; @@ -18,3 +18,7 @@ export const customFormats: import('dayjs').PluginFunc = function (_option, c, _ return oldFormat.call(this, result); }; }; + +export default { + customFormats +}; \ No newline at end of file From 6b7f32dae45f6156d3d7bd6b058f33edc1c480c6 Mon Sep 17 00:00:00 2001 From: YUCLing Date: Sat, 28 Sep 2024 02:36:58 +0800 Subject: [PATCH 8/8] chore: code style formatting --- framework/core/js/src/common/utils/dayjsPlugins.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/core/js/src/common/utils/dayjsPlugins.ts b/framework/core/js/src/common/utils/dayjsPlugins.ts index 27787d81ac..8a1da4974c 100644 --- a/framework/core/js/src/common/utils/dayjsPlugins.ts +++ b/framework/core/js/src/common/utils/dayjsPlugins.ts @@ -20,5 +20,5 @@ const customFormats: import('dayjs').PluginFunc = function (_option, c, _factory }; export default { - customFormats -}; \ No newline at end of file + customFormats, +};