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.

99 lines
3.5 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. const scrollToDetailedStatus = () => {
  17. const history = createHistory();
  18. const detailedStatuses = document.querySelectorAll('.public-layout .detailed-status');
  19. const location = history.location;
  20. if (detailedStatuses.length === 1 && (!location.state || !location.state.scrolledToDetailedStatus)) {
  21. detailedStatuses[0].scrollIntoView();
  22. history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
  23. }
  24. };
  25. ready(() => {
  26. const locale = document.documentElement.lang;
  27. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  28. year: 'numeric',
  29. month: 'long',
  30. day: 'numeric',
  31. hour: 'numeric',
  32. minute: 'numeric',
  33. });
  34. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  35. content.innerHTML = emojify(content.innerHTML);
  36. });
  37. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  38. const datetime = new Date(content.getAttribute('datetime'));
  39. const formattedDate = dateTimeFormat.format(datetime);
  40. content.title = formattedDate;
  41. content.textContent = formattedDate;
  42. });
  43. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  44. const datetime = new Date(content.getAttribute('datetime'));
  45. const now = new Date();
  46. content.title = dateTimeFormat.format(datetime);
  47. content.textContent = timeAgoString({
  48. formatMessage: ({ id, defaultMessage }, values) => (new IntlMessageFormat(messages[id] || defaultMessage, locale)).format(values),
  49. formatDate: (date, options) => (new Intl.DateTimeFormat(locale, options)).format(date),
  50. }, datetime, now, now.getFullYear());
  51. });
  52. const reactComponents = document.querySelectorAll('[data-component]');
  53. if (reactComponents.length > 0) {
  54. import(/* webpackChunkName: "containers/media_container" */ '../mastodon/containers/media_container')
  55. .then(({ default: MediaContainer }) => {
  56. [].forEach.call(reactComponents, (component) => {
  57. [].forEach.call(component.children, (child) => {
  58. component.removeChild(child);
  59. });
  60. });
  61. const content = document.createElement('div');
  62. ReactDOM.render(<MediaContainer locale={locale} components={reactComponents} />, content);
  63. document.body.appendChild(content);
  64. scrollToDetailedStatus();
  65. })
  66. .catch(error => {
  67. console.error(error);
  68. scrollToDetailedStatus();
  69. });
  70. } else {
  71. scrollToDetailedStatus();
  72. }
  73. const parallaxComponents = document.querySelectorAll('.parallax');
  74. if (parallaxComponents.length > 0 ) {
  75. new Rellax('.parallax', { speed: -1 });
  76. }
  77. });
  78. }
  79. loadPolyfills().then(main).catch(error => {
  80. console.error(error);
  81. });