You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GiteaAbsoluteDate.js 1.3KB

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