public.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (document.body.classList.contains('with-modals')) {
  92. const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
  93. const scrollbarWidthStyle = document.createElement('style');
  94. scrollbarWidthStyle.id = 'scrollbar-width';
  95. document.head.appendChild(scrollbarWidthStyle);
  96. scrollbarWidthStyle.sheet.insertRule(`body.with-modals--active { margin-right: ${scrollbarWidth}px; }`, 0);
  97. }
  98. });
  99. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  100. if (button !== 0) {
  101. return true;
  102. }
  103. window.location.href = target.href;
  104. return false;
  105. });
  106. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  107. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  108. if (contentEl.style.display === 'block') {
  109. contentEl.style.display = 'none';
  110. target.parentNode.style.marginBottom = 0;
  111. } else {
  112. contentEl.style.display = 'block';
  113. target.parentNode.style.marginBottom = null;
  114. }
  115. return false;
  116. });
  117. delegate(document, '.modal-button', 'click', e => {
  118. e.preventDefault();
  119. let href;
  120. if (e.target.nodeName !== 'A') {
  121. href = e.target.parentNode.href;
  122. } else {
  123. href = e.target.href;
  124. }
  125. window.open(href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  126. });
  127. delegate(document, '#account_display_name', 'input', ({ target }) => {
  128. const name = document.querySelector('.card .display-name strong');
  129. if (name) {
  130. if (target.value) {
  131. name.innerHTML = emojify(escapeTextContentForBrowser(target.value));
  132. } else {
  133. name.textContent = document.querySelector('#default_account_display_name').textContent;
  134. }
  135. }
  136. });
  137. delegate(document, '#account_avatar', 'change', ({ target }) => {
  138. const avatar = document.querySelector('.card .avatar img');
  139. const [file] = target.files || [];
  140. const url = file ? URL.createObjectURL(file) : avatar.dataset.originalSrc;
  141. avatar.src = url;
  142. });
  143. delegate(document, '#account_header', 'change', ({ target }) => {
  144. const header = document.querySelector('.card .card__img img');
  145. const [file] = target.files || [];
  146. const url = file ? URL.createObjectURL(file) : header.dataset.originalSrc;
  147. header.src = url;
  148. });
  149. delegate(document, '#account_locked', 'change', ({ target }) => {
  150. const lock = document.querySelector('.card .display-name i');
  151. if (target.checked) {
  152. lock.style.display = 'inline';
  153. } else {
  154. lock.style.display = 'none';
  155. }
  156. });
  157. delegate(document, '.input-copy input', 'click', ({ target }) => {
  158. target.focus();
  159. target.select();
  160. target.setSelectionRange(0, target.value.length);
  161. });
  162. delegate(document, '.input-copy button', 'click', ({ target }) => {
  163. const input = target.parentNode.querySelector('.input-copy__wrapper input');
  164. const oldReadOnly = input.readonly;
  165. input.readonly = false;
  166. input.focus();
  167. input.select();
  168. input.setSelectionRange(0, input.value.length);
  169. try {
  170. if (document.execCommand('copy')) {
  171. input.blur();
  172. target.parentNode.classList.add('copied');
  173. setTimeout(() => {
  174. target.parentNode.classList.remove('copied');
  175. }, 700);
  176. }
  177. } catch (err) {
  178. console.error(err);
  179. }
  180. input.readonly = oldReadOnly;
  181. });
  182. }
  183. loadPolyfills().then(main).catch(error => {
  184. console.error(error);
  185. });