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.

94 lines
3.4 KiB

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