Skip to content
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

Timeago helper function #780

Merged
merged 9 commits into from
Jul 5, 2023
Merged

Timeago helper function #780

merged 9 commits into from
Jul 5, 2023

Conversation

eritbh
Copy link
Member

@eritbh eritbh commented Jul 4, 2023

Adds a helper function TBui.relativeTime() which creates <time> elements that are automatically rendered as relative times, without UI code being required to manually invoke timeago. This should help avoid issues like #777 in the future.

timeago needs to be run once the target element is already in the DOM, so this implementation keeps track of the elements the helper function has created and uses a MutationObserver to call timeago automatically once they are in the DOM. This is especially helpful for the submission/comment UI builder functions, which until now required consuming code to manually $result.find('time').timeago() after the result was added to the DOM.

export function relativeTime (date) {
// create element
const el = document.createElement('time');
el.dateTime = date.toISOString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just to make sure we know what's going on here - putting the dateTime property on an element allows you to use the .timeago() jquery thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's correct. timeago is a plugin from npm; it's not directly obvious where we load it from, but you can read more about it here: https://www.npmjs.com/package/timeago

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it looks good

@eritbh eritbh added this to the next milestone Jul 5, 2023
@eritbh eritbh merged commit dff8189 into master Jul 5, 2023
@eritbh eritbh deleted the timeago-helper-function branch July 5, 2023 02:13
eritbh added a commit that referenced this pull request Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants