|
|
@ -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) { |
|
|
|