show time/weekday in tooltip for style entries (#1205)

This commit is contained in:
tophf 2021-03-11 07:20:38 +03:00 committed by GitHub
parent ff63b84489
commit 9d19d61913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View File

@ -140,17 +140,31 @@ Object.assign(t, {
} }
}, },
formatDate(date) { _intl: null,
_intlY: null,
_intlYHM: null,
_intlWYHM: null,
formatDate(date, needsTime) {
if (!date) { if (!date) {
return ''; return '';
} }
try { try {
const now = new Date();
const newDate = new Date(Number(date) || date); const newDate = new Date(Number(date) || date);
const string = newDate.toLocaleDateString([chrome.i18n.getUILanguage(), 'en'], { const needsYear = newDate.getYear() !== now.getYear();
day: '2-digit', const needsWeekDay = needsTime && (now - newDate <= 7 * 24 * 3600e3);
const intlKey = `_intl${needsWeekDay ? 'W' : ''}${needsYear ? 'Y' : ''}${needsTime ? 'HM' : ''}`;
const intl = t[intlKey] ||
(t[intlKey] = new Intl.DateTimeFormat([chrome.i18n.getUILanguage(), 'en'], {
day: 'numeric',
month: 'short', month: 'short',
year: newDate.getYear() === new Date().getYear() ? undefined : '2-digit', year: needsYear ? '2-digit' : undefined,
}); hour: needsTime ? 'numeric' : undefined,
minute: needsTime ? '2-digit' : undefined,
weekday: needsWeekDay ? 'long' : undefined,
}));
const string = intl.format(newDate);
return string === 'Invalid Date' ? '' : string; return string === 'Invalid Date' ? '' : string;
} catch (e) { } catch (e) {
return ''; return '';

View File

@ -23,8 +23,8 @@ const Events = {
const style = link.closest('.entry').styleMeta; const style = link.closest('.entry').styleMeta;
const ucd = style.usercssData; const ucd = style.usercssData;
link.title = link.title =
`${t('dateInstalled')}: ${t.formatDate(style.installDate) || '—'}\n` + `${t('dateInstalled')}: ${t.formatDate(style.installDate, true) || '—'}\n` +
`${t('dateUpdated')}: ${t.formatDate(style.updateDate) || '—'}\n` + `${t('dateUpdated')}: ${t.formatDate(style.updateDate, true) || '—'}\n` +
(ucd ? `UserCSS, v.${ucd.version}` : ''); (ucd ? `UserCSS, v.${ucd.version}` : '');
}, },