1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-04 14:46:57 -05:00

Fix absolute-date (#32375)

This commit is contained in:
wxiaoguang 2024-10-30 17:50:19 +08:00 committed by GitHub
parent f938dbc9b5
commit ce4d909bd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 34 deletions

16
package-lock.json generated
View File

@ -47,7 +47,6 @@
"sortablejs": "1.15.2", "sortablejs": "1.15.2",
"swagger-ui-dist": "5.17.14", "swagger-ui-dist": "5.17.14",
"tailwindcss": "3.4.10", "tailwindcss": "3.4.10",
"temporal-polyfill": "0.2.5",
"throttle-debounce": "5.0.2", "throttle-debounce": "5.0.2",
"tinycolor2": "1.6.0", "tinycolor2": "1.6.0",
"tippy.js": "6.3.7", "tippy.js": "6.3.7",
@ -14748,21 +14747,6 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/temporal-polyfill": {
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/temporal-polyfill/-/temporal-polyfill-0.2.5.tgz",
"integrity": "sha512-ye47xp8Cb0nDguAhrrDS1JT1SzwEV9e26sSsrWzVu+yPZ7LzceEcH0i2gci9jWfOfSCCgM3Qv5nOYShVUUFUXA==",
"license": "MIT",
"dependencies": {
"temporal-spec": "^0.2.4"
}
},
"node_modules/temporal-spec": {
"version": "0.2.4",
"resolved": "https://registry.npmjs.org/temporal-spec/-/temporal-spec-0.2.4.tgz",
"integrity": "sha512-lDMFv4nKQrSjlkHKAlHVqKrBG4DyFfa9F74cmBZ3Iy3ed8yvWnlWSIdi4IKfSqwmazAohBNwiN64qGx4y5Q3IQ==",
"license": "ISC"
},
"node_modules/terser": { "node_modules/terser": {
"version": "5.31.6", "version": "5.31.6",
"resolved": "https://registry.npmjs.org/terser/-/terser-5.31.6.tgz", "resolved": "https://registry.npmjs.org/terser/-/terser-5.31.6.tgz",

View File

@ -46,7 +46,6 @@
"sortablejs": "1.15.2", "sortablejs": "1.15.2",
"swagger-ui-dist": "5.17.14", "swagger-ui-dist": "5.17.14",
"tailwindcss": "3.4.10", "tailwindcss": "3.4.10",
"temporal-polyfill": "0.2.5",
"throttle-debounce": "5.0.2", "throttle-debounce": "5.0.2",
"tinycolor2": "1.6.0", "tinycolor2": "1.6.0",
"tippy.js": "6.3.7", "tippy.js": "6.3.7",

View File

@ -7,9 +7,15 @@ test('toAbsoluteLocaleDate', () => {
day: 'numeric', day: 'numeric',
})).toEqual('March 15, 2024'); })).toEqual('March 15, 2024');
expect(toAbsoluteLocaleDate('2024-03-15', 'de-DE', { expect(toAbsoluteLocaleDate('2024-03-15T01:02:03', 'de-DE', {
year: 'numeric', year: 'numeric',
month: 'long', month: 'long',
day: 'numeric', day: 'numeric',
})).toEqual('15. März 2024'); })).toEqual('15. März 2024');
expect(toAbsoluteLocaleDate('12345-03-15 01:02:03', '', {
year: 'numeric',
month: 'short',
day: 'numeric',
})).toEqual('Mar 15, 12345');
}); });

View File

@ -1,30 +1,28 @@
import {Temporal} from 'temporal-polyfill'; export function toAbsoluteLocaleDate(date: string, lang: string, opts: Intl.DateTimeFormatOptions) {
return new Date(date).toLocaleString(lang || [], opts);
export function toAbsoluteLocaleDate(dateStr, lang, opts) {
return Temporal.PlainDate.from(dateStr).toLocaleString(lang ?? [], opts);
} }
window.customElements.define('absolute-date', class extends HTMLElement { window.customElements.define('absolute-date', class extends HTMLElement {
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];
initialized = false;
update = () => { update = () => {
const year = this.getAttribute('year') ?? ''; const opt: Intl.DateTimeFormatOptions = {};
const month = this.getAttribute('month') ?? ''; for (const attr of ['year', 'month', 'weekday', 'day']) {
const weekday = this.getAttribute('weekday') ?? ''; if (this.getAttribute(attr)) opt[attr] = this.getAttribute(attr);
const day = this.getAttribute('day') ?? ''; }
const lang = this.closest('[lang]')?.getAttribute('lang') || const lang = this.closest('[lang]')?.getAttribute('lang') ||
this.ownerDocument.documentElement.getAttribute('lang') || ''; this.ownerDocument.documentElement.getAttribute('lang') || '';
// only use the first 10 characters, e.g. the `yyyy-mm-dd` part // only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ)
const dateStr = this.getAttribute('date').substring(0, 10); let date = this.getAttribute('date');
let dateSep = date.indexOf('T');
dateSep = dateSep === -1 ? date.indexOf(' ') : dateSep;
date = dateSep === -1 ? date : date.substring(0, dateSep);
if (!this.shadowRoot) this.attachShadow({mode: 'open'}); if (!this.shadowRoot) this.attachShadow({mode: 'open'});
this.shadowRoot.textContent = toAbsoluteLocaleDate(dateStr, lang, { this.shadowRoot.textContent = toAbsoluteLocaleDate(date, lang, opt);
...(year && {year}),
...(month && {month}),
...(weekday && {weekday}),
...(day && {day}),
});
}; };
attributeChangedCallback(_name, oldValue, newValue) { attributeChangedCallback(_name, oldValue, newValue) {