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.

213 lines
6.5 KiB

  1. import escapeTextContentForBrowser from 'escape-html';
  2. import loadPolyfills from '../mastodon/load_polyfills';
  3. import ready from '../mastodon/ready';
  4. import { start } from '../mastodon/common';
  5. start();
  6. window.addEventListener('message', e => {
  7. const data = e.data || {};
  8. if (!window.parent || data.type !== 'setHeight') {
  9. return;
  10. }
  11. ready(() => {
  12. window.parent.postMessage({
  13. type: 'setHeight',
  14. id: data.id,
  15. height: document.getElementsByTagName('html')[0].scrollHeight,
  16. }, '*');
  17. });
  18. });
  19. function main() {
  20. const IntlMessageFormat = require('intl-messageformat').default;
  21. const { timeAgoString } = require('../mastodon/components/relative_timestamp');
  22. const { delegate } = require('rails-ujs');
  23. const emojify = require('../mastodon/features/emoji/emoji').default;
  24. const { getLocale } = require('../mastodon/locales');
  25. const { messages } = getLocale();
  26. const React = require('react');
  27. const ReactDOM = require('react-dom');
  28. const Rellax = require('rellax');
  29. const createHistory = require('history').createBrowserHistory;
  30. const scrollToDetailedStatus = () => {
  31. const history = createHistory();
  32. const detailedStatuses = document.querySelectorAll('.public-layout .detailed-status');
  33. const location = history.location;
  34. if (detailedStatuses.length === 1 && (!location.state || !location.state.scrolledToDetailedStatus)) {
  35. detailedStatuses[0].scrollIntoView();
  36. history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
  37. }
  38. };
  39. ready(() => {
  40. const locale = document.documentElement.lang;
  41. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  42. year: 'numeric',
  43. month: 'long',
  44. day: 'numeric',
  45. hour: 'numeric',
  46. minute: 'numeric',
  47. });
  48. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  49. content.innerHTML = emojify(content.innerHTML);
  50. });
  51. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  52. const datetime = new Date(content.getAttribute('datetime'));
  53. const formattedDate = dateTimeFormat.format(datetime);
  54. content.title = formattedDate;
  55. content.textContent = formattedDate;
  56. });
  57. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  58. const datetime = new Date(content.getAttribute('datetime'));
  59. const now = new Date();
  60. content.title = dateTimeFormat.format(datetime);
  61. content.textContent = timeAgoString({
  62. formatMessage: ({ id, defaultMessage }, values) => (new IntlMessageFormat(messages[id] || defaultMessage, locale)).format(values),
  63. formatDate: (date, options) => (new Intl.DateTimeFormat(locale, options)).format(date),
  64. }, datetime, now, now.getFullYear());
  65. });
  66. const reactComponents = document.querySelectorAll('[data-component]');
  67. if (reactComponents.length > 0) {
  68. import(/* webpackChunkName: "containers/media_container" */ '../mastodon/containers/media_container')
  69. .then(({ default: MediaContainer }) => {
  70. [].forEach.call(reactComponents, (component) => {
  71. [].forEach.call(component.children, (child) => {
  72. component.removeChild(child);
  73. });
  74. });
  75. const content = document.createElement('div');
  76. ReactDOM.render(<MediaContainer locale={locale} components={reactComponents} />, content);
  77. document.body.appendChild(content);
  78. scrollToDetailedStatus();
  79. })
  80. .catch(error => {
  81. console.error(error);
  82. scrollToDetailedStatus();
  83. });
  84. } else {
  85. scrollToDetailedStatus();
  86. }
  87. const parallaxComponents = document.querySelectorAll('.parallax');
  88. if (parallaxComponents.length > 0 ) {
  89. new Rellax('.parallax', { speed: -1 });
  90. }
  91. });
  92. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  93. if (button !== 0) {
  94. return true;
  95. }
  96. window.location.href = target.href;
  97. return false;
  98. });
  99. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  100. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  101. if (contentEl.style.display === 'block') {
  102. contentEl.style.display = 'none';
  103. target.parentNode.style.marginBottom = 0;
  104. } else {
  105. contentEl.style.display = 'block';
  106. target.parentNode.style.marginBottom = null;
  107. }
  108. return false;
  109. });
  110. delegate(document, '.modal-button', 'click', e => {
  111. e.preventDefault();
  112. let href;
  113. if (e.target.nodeName !== 'A') {
  114. href = e.target.parentNode.href;
  115. } else {
  116. href = e.target.href;
  117. }
  118. window.open(href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  119. });
  120. delegate(document, '#account_display_name', 'input', ({ target }) => {
  121. const name = document.querySelector('.card .display-name strong');
  122. if (name) {
  123. if (target.value) {
  124. name.innerHTML = emojify(escapeTextContentForBrowser(target.value));
  125. } else {
  126. name.textContent = document.querySelector('#default_account_display_name').textContent;
  127. }
  128. }
  129. });
  130. delegate(document, '#account_avatar', 'change', ({ target }) => {
  131. const avatar = document.querySelector('.card .avatar img');
  132. const [file] = target.files || [];
  133. const url = file ? URL.createObjectURL(file) : avatar.dataset.originalSrc;
  134. avatar.src = url;
  135. });
  136. delegate(document, '#account_header', 'change', ({ target }) => {
  137. const header = document.querySelector('.card .card__img img');
  138. const [file] = target.files || [];
  139. const url = file ? URL.createObjectURL(file) : header.dataset.originalSrc;
  140. header.src = url;
  141. });
  142. delegate(document, '#account_locked', 'change', ({ target }) => {
  143. const lock = document.querySelector('.card .display-name i');
  144. if (target.checked) {
  145. lock.style.display = 'inline';
  146. } else {
  147. lock.style.display = 'none';
  148. }
  149. });
  150. delegate(document, '.input-copy input', 'click', ({ target }) => {
  151. target.select();
  152. });
  153. delegate(document, '.input-copy button', 'click', ({ target }) => {
  154. const input = target.parentNode.querySelector('.input-copy__wrapper input');
  155. input.focus();
  156. input.select();
  157. try {
  158. if (document.execCommand('copy')) {
  159. input.blur();
  160. target.parentNode.classList.add('copied');
  161. setTimeout(() => {
  162. target.parentNode.classList.remove('copied');
  163. }, 700);
  164. }
  165. } catch (err) {
  166. console.error(err);
  167. }
  168. });
  169. }
  170. loadPolyfills().then(main).catch(error => {
  171. console.error(error);
  172. });