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.

156 lines
5.1 KiB

  1. import loadPolyfills from '../mastodon/load_polyfills';
  2. import ready from '../mastodon/ready';
  3. window.addEventListener('message', e => {
  4. const data = e.data || {};
  5. if (!window.parent || data.type !== 'setHeight') {
  6. return;
  7. }
  8. ready(() => {
  9. window.parent.postMessage({
  10. type: 'setHeight',
  11. id: data.id,
  12. height: document.getElementsByTagName('html')[0].scrollHeight,
  13. }, '*');
  14. });
  15. });
  16. function main() {
  17. const { length } = require('stringz');
  18. const IntlRelativeFormat = require('intl-relativeformat').default;
  19. const { delegate } = require('rails-ujs');
  20. const emojify = require('../mastodon/features/emoji/emoji').default;
  21. const { getLocale } = require('../mastodon/locales');
  22. const { localeData } = getLocale();
  23. const VideoContainer = require('../mastodon/containers/video_container').default;
  24. const React = require('react');
  25. const ReactDOM = require('react-dom');
  26. localeData.forEach(IntlRelativeFormat.__addLocaleData);
  27. ready(() => {
  28. const locale = document.documentElement.lang;
  29. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  30. year: 'numeric',
  31. month: 'long',
  32. day: 'numeric',
  33. hour: 'numeric',
  34. minute: 'numeric',
  35. });
  36. const relativeFormat = new IntlRelativeFormat(locale);
  37. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  38. content.innerHTML = emojify(content.innerHTML);
  39. });
  40. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  41. const datetime = new Date(content.getAttribute('datetime'));
  42. const formattedDate = dateTimeFormat.format(datetime);
  43. content.title = formattedDate;
  44. content.textContent = formattedDate;
  45. });
  46. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  47. const datetime = new Date(content.getAttribute('datetime'));
  48. content.title = dateTimeFormat.format(datetime);
  49. content.textContent = relativeFormat.format(datetime);
  50. });
  51. [].forEach.call(document.querySelectorAll('.logo-button'), (content) => {
  52. content.addEventListener('click', (e) => {
  53. e.preventDefault();
  54. window.open(e.target.href, 'mastodon-intent', 'width=400,height=400,resizable=no,menubar=no,status=no,scrollbars=yes');
  55. });
  56. });
  57. [].forEach.call(document.querySelectorAll('[data-component="Video"]'), (content) => {
  58. const props = JSON.parse(content.getAttribute('data-props'));
  59. ReactDOM.render(<VideoContainer locale={locale} {...props} />, content);
  60. });
  61. const cards = document.querySelectorAll('[data-component="Card"]');
  62. if (cards.length > 0) {
  63. import(/* webpackChunkName: "containers/cards_container" */ '../mastodon/containers/cards_container').then(({ default: CardsContainer }) => {
  64. const content = document.createElement('div');
  65. ReactDOM.render(<CardsContainer locale={locale} cards={cards} />, content);
  66. document.body.appendChild(content);
  67. }).catch(error => console.error(error));
  68. }
  69. const mediaGalleries = document.querySelectorAll('[data-component="MediaGallery"]');
  70. if (mediaGalleries.length > 0) {
  71. const MediaGalleriesContainer = require('../mastodon/containers/media_galleries_container').default;
  72. const content = document.createElement('div');
  73. ReactDOM.render(<MediaGalleriesContainer locale={locale} galleries={mediaGalleries} />, content);
  74. document.body.appendChild(content);
  75. }
  76. });
  77. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  78. if (button !== 0) {
  79. return true;
  80. }
  81. window.location.href = target.href;
  82. return false;
  83. });
  84. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  85. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  86. if (contentEl.style.display === 'block') {
  87. contentEl.style.display = 'none';
  88. target.parentNode.style.marginBottom = 0;
  89. } else {
  90. contentEl.style.display = 'block';
  91. target.parentNode.style.marginBottom = null;
  92. }
  93. return false;
  94. });
  95. delegate(document, '.account_display_name', 'input', ({ target }) => {
  96. const nameCounter = document.querySelector('.name-counter');
  97. if (nameCounter) {
  98. nameCounter.textContent = 30 - length(target.value);
  99. }
  100. });
  101. delegate(document, '.account_note', 'input', ({ target }) => {
  102. const noteCounter = document.querySelector('.note-counter');
  103. if (noteCounter) {
  104. noteCounter.textContent = 160 - length(target.value);
  105. }
  106. });
  107. delegate(document, '#account_avatar', 'change', ({ target }) => {
  108. const avatar = document.querySelector('.card.compact .avatar img');
  109. const [file] = target.files || [];
  110. const url = file ? URL.createObjectURL(file) : avatar.dataset.originalSrc;
  111. avatar.src = url;
  112. });
  113. delegate(document, '#account_header', 'change', ({ target }) => {
  114. const header = document.querySelector('.card.compact');
  115. const [file] = target.files || [];
  116. const url = file ? URL.createObjectURL(file) : header.dataset.originalSrc;
  117. header.style.backgroundImage = `url(${url})`;
  118. });
  119. }
  120. loadPolyfills().then(main).catch(error => {
  121. console.error(error);
  122. });