code.gitea.io/gitea@v1.22.3/web_src/js/webcomponents/absolute-date.js (about) 1 import {Temporal} from 'temporal-polyfill'; 2 3 export function toAbsoluteLocaleDate(dateStr, lang, opts) { 4 return Temporal.PlainDate.from(dateStr).toLocaleString(lang ?? [], opts); 5 } 6 7 window.customElements.define('absolute-date', class extends HTMLElement { 8 static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; 9 10 update = () => { 11 const year = this.getAttribute('year') ?? ''; 12 const month = this.getAttribute('month') ?? ''; 13 const weekday = this.getAttribute('weekday') ?? ''; 14 const day = this.getAttribute('day') ?? ''; 15 const lang = this.closest('[lang]')?.getAttribute('lang') || 16 this.ownerDocument.documentElement.getAttribute('lang') || ''; 17 18 // only use the first 10 characters, e.g. the `yyyy-mm-dd` part 19 const dateStr = this.getAttribute('date').substring(0, 10); 20 21 if (!this.shadowRoot) this.attachShadow({mode: 'open'}); 22 this.shadowRoot.textContent = toAbsoluteLocaleDate(dateStr, lang, { 23 ...(year && {year}), 24 ...(month && {month}), 25 ...(weekday && {weekday}), 26 ...(day && {day}), 27 }); 28 }; 29 30 attributeChangedCallback(_name, oldValue, newValue) { 31 if (!this.initialized || oldValue === newValue) return; 32 this.update(); 33 } 34 35 connectedCallback() { 36 this.initialized = false; 37 this.update(); 38 this.initialized = true; 39 } 40 });