-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix date rendering by adding
<gitea-locale-date>
- Loading branch information
1 parent
7f856d5
commit fbec70b
Showing
4 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
window.customElements.define('gitea-locale-date', class extends HTMLElement { | ||
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; | ||
|
||
update = () => { | ||
const year = this.getAttribute('year') ?? 'numeric'; | ||
const month = this.getAttribute('month') ?? 'short'; | ||
const weekday = this.getAttribute('weekday') ?? ''; | ||
const day = this.getAttribute('day') ?? 'numeric'; | ||
const lang = this.closest('[lang]')?.getAttribute('lang') || | ||
this.ownerDocument.documentElement.getAttribute('lang') || | ||
''; | ||
const date = new Date(this.getAttribute('date')); | ||
|
||
// apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is | ||
// a UTC date, so the local date will have a offset towards the user's timezone. | ||
// Ref: https://stackoverflow.com/a/14569783/808699 | ||
const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000); | ||
|
||
this.textContent = correctedDate.toLocaleString(lang ?? [], { | ||
...(year && {year}), | ||
...(month && {month}), | ||
...(weekday && {weekday}), | ||
...(day && {day}), | ||
}); | ||
}; | ||
|
||
attributeChangedCallback(_name, oldValue, newValue) { | ||
if (oldValue === newValue || !this.initialized) return; | ||
this.update(); | ||
} | ||
|
||
connectedCallback() { | ||
this.initialized = false; | ||
this.update(); | ||
this.initialized = true; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters