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.

172 lines
5.6 KiB

  1. import loadPolyfills from '../mastodon/load_polyfills';
  2. import ready from '../mastodon/ready';
  3. import { start } from '../mastodon/common';
  4. start();
  5. window.addEventListener('message', e => {
  6. const data = e.data || {};
  7. if (!window.parent || data.type !== 'setHeight') {
  8. return;
  9. }
  10. ready(() => {
  11. window.parent.postMessage({
  12. type: 'setHeight',
  13. id: data.id,
  14. height: document.getElementsByTagName('html')[0].scrollHeight,
  15. }, '*');
  16. });
  17. });
  18. function main() {
  19. const { length } = require('stringz');
  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. ready(() => {
  31. const locale = document.documentElement.lang;
  32. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  33. year: 'numeric',
  34. month: 'long',
  35. day: 'numeric',
  36. hour: 'numeric',
  37. minute: 'numeric',
  38. });
  39. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  40. content.innerHTML = emojify(content.innerHTML);
  41. });
  42. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  43. const datetime = new Date(content.getAttribute('datetime'));
  44. const formattedDate = dateTimeFormat.format(datetime);
  45. content.title = formattedDate;
  46. content.textContent = formattedDate;
  47. });
  48. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  49. const datetime = new Date(content.getAttribute('datetime'));
  50. const now = new Date();
  51. content.title = dateTimeFormat.format(datetime);
  52. content.textContent = timeAgoString({
  53. formatMessage: ({ id, defaultMessage }, values) => (new IntlMessageFormat(messages[id] || defaultMessage, locale)).format(values),
  54. formatDate: (date, options) => (new Intl.DateTimeFormat(locale, options)).format(date),
  55. }, datetime, now, datetime.getFullYear());
  56. });
  57. [].forEach.call(document.querySelectorAll('.modal-button'), (content) => {
  58. content.addEventListener('click', (e) => {
  59. e.preventDefault();
  60. window.open(e.target.href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  61. });
  62. });
  63. const reactComponents = document.querySelectorAll('[data-component]');
  64. if (reactComponents.length > 0) {
  65. import(/* webpackChunkName: "containers/media_container" */ '../mastodon/containers/media_container')
  66. .then(({ default: MediaContainer }) => {
  67. const content = document.createElement('div');
  68. ReactDOM.render(<MediaContainer locale={locale} components={reactComponents} />, content);
  69. document.body.appendChild(content);
  70. })
  71. .catch(error => console.error(error));
  72. }
  73. new Rellax('.parallax', { speed: -1 });
  74. const history = createHistory();
  75. const detailedStatuses = document.querySelectorAll('.public-layout .detailed-status');
  76. const location = history.location;
  77. if (detailedStatuses.length === 1 && (!location.state || !location.state.scrolledToDetailedStatus)) {
  78. detailedStatuses[0].scrollIntoView();
  79. history.replace(location.pathname, { ...location.state, scrolledToDetailedStatus: true });
  80. }
  81. });
  82. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  83. if (button !== 0) {
  84. return true;
  85. }
  86. window.location.href = target.href;
  87. return false;
  88. });
  89. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  90. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  91. if (contentEl.style.display === 'block') {
  92. contentEl.style.display = 'none';
  93. target.parentNode.style.marginBottom = 0;
  94. } else {
  95. contentEl.style.display = 'block';
  96. target.parentNode.style.marginBottom = null;
  97. }
  98. return false;
  99. });
  100. delegate(document, '#account_display_name', 'input', ({ target }) => {
  101. const nameCounter = document.querySelector('.name-counter');
  102. const name = document.querySelector('.card .display-name strong');
  103. if (nameCounter) {
  104. nameCounter.textContent = 30 - length(target.value);
  105. }
  106. if (name) {
  107. name.innerHTML = emojify(target.value);
  108. }
  109. });
  110. delegate(document, '#account_note', 'input', ({ target }) => {
  111. const noteCounter = document.querySelector('.note-counter');
  112. if (noteCounter) {
  113. noteCounter.textContent = 160 - length(target.value);
  114. }
  115. });
  116. delegate(document, '#account_avatar', 'change', ({ target }) => {
  117. const avatar = document.querySelector('.card .avatar img');
  118. const [file] = target.files || [];
  119. const url = file ? URL.createObjectURL(file) : avatar.dataset.originalSrc;
  120. avatar.src = url;
  121. });
  122. delegate(document, '#account_header', 'change', ({ target }) => {
  123. const header = document.querySelector('.card .card__img img');
  124. const [file] = target.files || [];
  125. const url = file ? URL.createObjectURL(file) : header.dataset.originalSrc;
  126. header.src = url;
  127. });
  128. delegate(document, '#account_locked', 'change', ({ target }) => {
  129. const lock = document.querySelector('.card .display-name i');
  130. if (target.checked) {
  131. lock.style.display = 'inline';
  132. } else {
  133. lock.style.display = 'none';
  134. }
  135. });
  136. }
  137. loadPolyfills().then(main).catch(error => {
  138. console.error(error);
  139. });