-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Fix date rendering by adding <gitea-absolute-date>
#29725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3ce25db
Fix date rendering by adding `<gitea-locale-date>`
silverwind a27ce98
add shadowRoot and fallback text
silverwind ef17866
update comment
silverwind 870e3b7
partial test fix
silverwind bc70934
fix tests
silverwind ceb95fe
default all js attrs to empty
silverwind 44da6fb
extract only yyyy-mm-dd in js
silverwind c7589d8
fix closing tags
silverwind 48c7392
swap check
silverwind cd9c7fb
Update web_src/js/webcomponents/GiteaLocaleDate.js
silverwind d75a670
comment fix
silverwind 0b98d59
merge comments
silverwind f82351a
rename to gitea-absolute-date
silverwind 86a46fb
remove unnecessary tooltip attributes
silverwind 3c4f4be
Merge branch 'main' into locale-date
GiteaBot 97c3ce8
Merge branch 'main' into locale-date
GiteaBot daed7de
Merge branch 'main' into locale-date
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| window.customElements.define('gitea-locale-date', class extends HTMLElement { | ||
| static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; | ||
|
|
||
| update = () => { | ||
| const year = this.getAttribute('year') ?? ''; | ||
| const month = this.getAttribute('month') ?? ''; | ||
| const weekday = this.getAttribute('weekday') ?? ''; | ||
| const day = this.getAttribute('day') ?? ''; | ||
| const lang = this.closest('[lang]')?.getAttribute('lang') || | ||
| this.ownerDocument.documentElement.getAttribute('lang') || | ||
| ''; | ||
|
|
||
| // only extract the `yyyy-mm-dd` part | ||
silverwind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const date = new Date(this.getAttribute('date').substring(0, 10)); | ||
|
|
||
| // 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 UTC which we reverse here. | ||
| // Ref: https://stackoverflow.com/a/14569783/808699 | ||
| const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000); | ||
|
|
||
| if (!this.shadowRoot) this.attachShadow({mode: 'open'}); | ||
| this.shadowRoot.textContent = correctedDate.toLocaleString(lang ?? [], { | ||
| ...(year && {year}), | ||
| ...(month && {month}), | ||
| ...(weekday && {weekday}), | ||
| ...(day && {day}), | ||
| }); | ||
| }; | ||
|
|
||
| attributeChangedCallback(_name, oldValue, newValue) { | ||
| if (!this.initialized || oldValue === newValue) return; | ||
| this.update(); | ||
| } | ||
|
|
||
| connectedCallback() { | ||
| this.initialized = false; | ||
| this.update(); | ||
| this.initialized = true; | ||
| } | ||
| }); | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not extend
<relative-time>?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We aren't replacing https://github.com/github/relative-time-element, we are just adding another custom element to render absolute dates in locale format.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and to answer specifically:
<relative-time>is not meant to be passed dates, only datetimes. So it's currently unsuitable for the date rendering and I'm not sure whether upstream would accept adateparameter as it would increase internal complexity of the component a lot.