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.

268 lines
8.4 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. import loadKeyboardExtensions from '../mastodon/load_keyboard_extensions';
  6. start();
  7. window.addEventListener('message', e => {
  8. const data = e.data || {};
  9. if (!window.parent || data.type !== 'setHeight') {
  10. return;
  11. }
  12. ready(() => {
  13. window.parent.postMessage({
  14. type: 'setHeight',
  15. id: data.id,
  16. height: document.getElementsByTagName('html')[0].scrollHeight,
  17. }, '*');
  18. });
  19. });
  20. function main() {
  21. const IntlMessageFormat = require('intl-messageformat').default;
  22. const { timeAgoString } = require('../mastodon/components/relative_timestamp');
  23. const { delegate } = require('rails-ujs');
  24. const emojify = require('../mastodon/features/emoji/emoji').default;
  25. const { getLocale } = require('../mastodon/locales');
  26. const { messages } = getLocale();
  27. const React = require('react');
  28. const ReactDOM = require('react-dom');
  29. const Rellax = require('rellax');
  30. const { createBrowserHistory } = require('history');
  31. const scrollToDetailedStatus = () => {
  32. const history = createBrowserHistory();
  33. const detailedStatuses = document.querySelectorAll('.public-layout .detailed-status');
  34. const location = history.location;
  35. if (detailedStatuses.length === 1 && (!location.state || !location.state.scrolledToDetailedStatus)) {
  36. detailedStatuses[0].scrollIntoView();
  37. history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
  38. }
  39. };
  40. const getEmojiAnimationHandler = (swapTo) => {
  41. return ({ target }) => {
  42. target.src = target.getAttribute(swapTo);
  43. };
  44. };
  45. ready(() => {
  46. const locale = document.documentElement.lang;
  47. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  48. year: 'numeric',
  49. month: 'long',
  50. day: 'numeric',
  51. hour: 'numeric',
  52. minute: 'numeric',
  53. });
  54. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  55. content.innerHTML = emojify(content.innerHTML);
  56. });
  57. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  58. const datetime = new Date(content.getAttribute('datetime'));
  59. const formattedDate = dateTimeFormat.format(datetime);
  60. content.title = formattedDate;
  61. content.textContent = formattedDate;
  62. });
  63. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  64. const datetime = new Date(content.getAttribute('datetime'));
  65. const now = new Date();
  66. content.title = dateTimeFormat.format(datetime);
  67. content.textContent = timeAgoString({
  68. formatMessage: ({ id, defaultMessage }, values) => (new IntlMessageFormat(messages[id] || defaultMessage, locale)).format(values),
  69. formatDate: (date, options) => (new Intl.DateTimeFormat(locale, options)).format(date),
  70. }, datetime, now, now.getFullYear());
  71. });
  72. const reactComponents = document.querySelectorAll('[data-component]');
  73. if (reactComponents.length > 0) {
  74. import(/* webpackChunkName: "containers/media_container" */ '../mastodon/containers/media_container')
  75. .then(({ default: MediaContainer }) => {
  76. [].forEach.call(reactComponents, (component) => {
  77. [].forEach.call(component.children, (child) => {
  78. component.removeChild(child);
  79. });
  80. });
  81. const content = document.createElement('div');
  82. ReactDOM.render(<MediaContainer locale={locale} components={reactComponents} />, content);
  83. document.body.appendChild(content);
  84. scrollToDetailedStatus();
  85. })
  86. .catch(error => {
  87. console.error(error);
  88. scrollToDetailedStatus();
  89. });
  90. } else {
  91. scrollToDetailedStatus();
  92. }
  93. const parallaxComponents = document.querySelectorAll('.parallax');
  94. if (parallaxComponents.length > 0 ) {
  95. new Rellax('.parallax', { speed: -1 });
  96. }
  97. delegate(document, '.custom-emoji', 'mouseover', getEmojiAnimationHandler('data-original'));
  98. delegate(document, '.custom-emoji', 'mouseout', getEmojiAnimationHandler('data-static'));
  99. });
  100. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  101. if (button !== 0) {
  102. return true;
  103. }
  104. window.location.href = target.href;
  105. return false;
  106. });
  107. delegate(document, '.status__content__spoiler-link', 'click', function() {
  108. const contentEl = this.parentNode.parentNode.querySelector('.e-content');
  109. if (contentEl.style.display === 'block') {
  110. contentEl.style.display = 'none';
  111. this.parentNode.style.marginBottom = 0;
  112. } else {
  113. contentEl.style.display = 'block';
  114. this.parentNode.style.marginBottom = null;
  115. }
  116. return false;
  117. });
  118. delegate(document, '.blocks-table button.icon-button', 'click', function(e) {
  119. e.preventDefault();
  120. const classList = this.firstElementChild.classList;
  121. classList.toggle('fa-chevron-down');
  122. classList.toggle('fa-chevron-up');
  123. this.parentElement.parentElement.nextElementSibling.classList.toggle('hidden');
  124. });
  125. delegate(document, '.modal-button', 'click', e => {
  126. e.preventDefault();
  127. let href;
  128. if (e.target.nodeName !== 'A') {
  129. href = e.target.parentNode.href;
  130. } else {
  131. href = e.target.href;
  132. }
  133. window.open(href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  134. });
  135. delegate(document, '#account_display_name', 'input', ({ target }) => {
  136. const name = document.querySelector('.card .display-name strong');
  137. if (name) {
  138. if (target.value) {
  139. name.innerHTML = emojify(escapeTextContentForBrowser(target.value));
  140. } else {
  141. name.textContent = document.querySelector('#default_account_display_name').textContent;
  142. }
  143. }
  144. });
  145. delegate(document, '#account_avatar', 'change', ({ target }) => {
  146. const avatar = document.querySelector('.card .avatar img');
  147. const [file] = target.files || [];
  148. const url = file ? URL.createObjectURL(file) : avatar.dataset.originalSrc;
  149. avatar.src = url;
  150. });
  151. const getProfileAvatarAnimationHandler = (swapTo) => {
  152. //animate avatar gifs on the profile page when moused over
  153. return ({ target }) => {
  154. const swapSrc = target.getAttribute(swapTo);
  155. //only change the img source if autoplay is off and the image src is actually different
  156. if(target.getAttribute('data-autoplay') !== 'true' && target.src !== swapSrc) {
  157. target.src = swapSrc;
  158. }
  159. };
  160. };
  161. delegate(document, 'img#profile_page_avatar', 'mouseover', getProfileAvatarAnimationHandler('data-original'));
  162. delegate(document, 'img#profile_page_avatar', 'mouseout', getProfileAvatarAnimationHandler('data-static'));
  163. delegate(document, '#account_header', 'change', ({ target }) => {
  164. const header = document.querySelector('.card .card__img img');
  165. const [file] = target.files || [];
  166. const url = file ? URL.createObjectURL(file) : header.dataset.originalSrc;
  167. header.src = url;
  168. });
  169. delegate(document, '#account_locked', 'change', ({ target }) => {
  170. const lock = document.querySelector('.card .display-name i');
  171. if (target.checked) {
  172. lock.style.display = 'inline';
  173. } else {
  174. lock.style.display = 'none';
  175. }
  176. });
  177. delegate(document, '.input-copy input', 'click', ({ target }) => {
  178. target.focus();
  179. target.select();
  180. target.setSelectionRange(0, target.value.length);
  181. });
  182. delegate(document, '.input-copy button', 'click', ({ target }) => {
  183. const input = target.parentNode.querySelector('.input-copy__wrapper input');
  184. const oldReadOnly = input.readonly;
  185. input.readonly = false;
  186. input.focus();
  187. input.select();
  188. input.setSelectionRange(0, input.value.length);
  189. try {
  190. if (document.execCommand('copy')) {
  191. input.blur();
  192. target.parentNode.classList.add('copied');
  193. setTimeout(() => {
  194. target.parentNode.classList.remove('copied');
  195. }, 700);
  196. }
  197. } catch (err) {
  198. console.error(err);
  199. }
  200. input.readonly = oldReadOnly;
  201. });
  202. delegate(document, '.sidebar__toggle__icon', 'click', () => {
  203. const target = document.querySelector('.sidebar ul');
  204. if (target.style.display === 'block') {
  205. target.style.display = 'none';
  206. } else {
  207. target.style.display = 'block';
  208. }
  209. });
  210. }
  211. loadPolyfills()
  212. .then(main)
  213. .then(loadKeyboardExtensions)
  214. .catch(error => {
  215. console.error(error);
  216. });