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.

85 lines
3.1 KiB

  1. import loadPolyfills from '../mastodon/load_polyfills';
  2. import ready from '../mastodon/ready';
  3. import { start } from '../mastodon/common';
  4. start();
  5. function main() {
  6. const IntlMessageFormat = require('intl-messageformat').default;
  7. const { timeAgoString } = require('../mastodon/components/relative_timestamp');
  8. const { delegate } = require('rails-ujs');
  9. const emojify = require('../mastodon/features/emoji/emoji').default;
  10. const { getLocale } = require('../mastodon/locales');
  11. const { messages } = getLocale();
  12. const React = require('react');
  13. const ReactDOM = require('react-dom');
  14. const Rellax = require('rellax');
  15. const createHistory = require('history').createBrowserHistory;
  16. ready(() => {
  17. const locale = document.documentElement.lang;
  18. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  19. year: 'numeric',
  20. month: 'long',
  21. day: 'numeric',
  22. hour: 'numeric',
  23. minute: 'numeric',
  24. });
  25. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  26. content.innerHTML = emojify(content.innerHTML);
  27. });
  28. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  29. const datetime = new Date(content.getAttribute('datetime'));
  30. const formattedDate = dateTimeFormat.format(datetime);
  31. content.title = formattedDate;
  32. content.textContent = formattedDate;
  33. });
  34. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  35. const datetime = new Date(content.getAttribute('datetime'));
  36. const now = new Date();
  37. content.title = dateTimeFormat.format(datetime);
  38. content.textContent = timeAgoString({
  39. formatMessage: ({ id, defaultMessage }, values) => (new IntlMessageFormat(messages[id] || defaultMessage, locale)).format(values),
  40. formatDate: (date, options) => (new Intl.DateTimeFormat(locale, options)).format(date),
  41. }, datetime, now, now.getFullYear());
  42. });
  43. const reactComponents = document.querySelectorAll('[data-component]');
  44. if (reactComponents.length > 0) {
  45. import(/* webpackChunkName: "containers/media_container" */ '../mastodon/containers/media_container')
  46. .then(({ default: MediaContainer }) => {
  47. const content = document.createElement('div');
  48. ReactDOM.render(<MediaContainer locale={locale} components={reactComponents} />, content);
  49. document.body.appendChild(content);
  50. })
  51. .catch(error => console.error(error));
  52. }
  53. const parallaxComponents = document.querySelectorAll('.parallax');
  54. if (parallaxComponents.length > 0 ) {
  55. new Rellax('.parallax', { speed: -1 });
  56. }
  57. const history = createHistory();
  58. const detailedStatuses = document.querySelectorAll('.public-layout .detailed-status');
  59. const location = history.location;
  60. if (detailedStatuses.length === 1 && (!location.state || !location.state.scrolledToDetailedStatus)) {
  61. detailedStatuses[0].scrollIntoView();
  62. history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
  63. }
  64. });
  65. }
  66. loadPolyfills().then(main).catch(error => {
  67. console.error(error);
  68. });