mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-30 01:42:29 +03:00
060d791a63
no issue The `settings` service has been a source of confusion when writing with modern Ember patterns because it's use of the deprecated `ProxyMixin` forced all property access/setting to go via `.get()` and `.set()` whereas the rest of the system has mostly (there are a few other uses of ProxyObjects remaining) eliminated the use of the non-native get/set methods. - removed use of `ProxyMixin` in the `settings` service by grabbing the attributes off the setting model after fetching and using `Object.defineProperty()` to add native getters/setters that pass through to the model's getters/setters. Ember's autotracking automatically works across the native getters/setters so we can then use the service as if it was any other native object - updated all code to use `settings.{attrName}` directly for getting/setting instead of `.get()` and `.set()` - removed use of observer in the `customViews` service because it was being set up before the native properties had been added on the settings service meaning autotracking wasn't able to set up properly
65 lines
2.3 KiB
JavaScript
65 lines
2.3 KiB
JavaScript
import Helper from '@ember/component/helper';
|
|
import moment from 'moment-timezone';
|
|
import {assert} from '@ember/debug';
|
|
import {inject as service} from '@ember/service';
|
|
|
|
export function formatPostTime(timeago, {timezone = 'etc/UTC', format, draft, scheduled, published}) {
|
|
if (draft) {
|
|
// No special handling for drafts, just use moment.from
|
|
return moment(timeago).from(moment.utc());
|
|
}
|
|
|
|
let time = moment.tz(timeago, timezone);
|
|
|
|
if (format) {
|
|
return time.format(format);
|
|
}
|
|
|
|
let now = moment.tz(moment.utc(), timezone);
|
|
|
|
let utcOffset;
|
|
if (time.utcOffset() === 0) {
|
|
utcOffset = '(UTC)';
|
|
} else {
|
|
utcOffset = `(UTC${time.format('Z').replace(/([+-])0/, '$1').replace(/:00/, '')})`;
|
|
}
|
|
|
|
// If not a draft and post was published <= 12 hours ago
|
|
// or scheduled to be published <= 12 hours from now, use moment.from
|
|
if (Math.abs(now.diff(time, 'hours')) <= 12) {
|
|
return time.from(now);
|
|
}
|
|
|
|
// If scheduled for or published on the same day, render the time + Today
|
|
if (time.isSame(now, 'day')) {
|
|
let formatted = time.format(`HH:mm [${utcOffset}] [Today]`);
|
|
return scheduled ? `at ${formatted}` : formatted;
|
|
}
|
|
|
|
// if published yesterday, render time + yesterday
|
|
// This check comes before scheduled, because there are likely to be more published
|
|
// posts than scheduled posts.
|
|
if (published && time.isSame(now.clone().subtract(1, 'days').startOf('day'), 'day')) {
|
|
return time.format(`HH:mm [${utcOffset}] [Yesterday]`);
|
|
}
|
|
|
|
// if scheduled for tomorrow, render the time + tomorrow
|
|
if (scheduled && time.isSame(now.clone().add(1, 'days').startOf('day'), 'day')) {
|
|
return time.format(`[at] HH:mm [${utcOffset}] [tomorrow]`);
|
|
}
|
|
|
|
// Else, render just the date if published, or the time & date if scheduled
|
|
let f = scheduled ? `[at] HH:mm [${utcOffset}] [on] DD MMM YYYY` : 'DD MMM YYYY';
|
|
return time.format(f);
|
|
}
|
|
|
|
export default class GhFormatPostTimeHelper extends Helper {
|
|
@service settings;
|
|
|
|
compute([timeago], options) {
|
|
assert('You must pass a time to the gh-format-post-time helper', timeago);
|
|
|
|
return formatPostTime(timeago, Object.assign({}, options, {timezone: this.settings.timezone}));
|
|
}
|
|
}
|