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.

144 lines
4.5 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 IntlRelativeFormat = require('intl-relativeformat').default;
  21. const { delegate } = require('rails-ujs');
  22. const emojify = require('../mastodon/features/emoji/emoji').default;
  23. const { getLocale } = require('../mastodon/locales');
  24. const { localeData } = getLocale();
  25. const React = require('react');
  26. const ReactDOM = require('react-dom');
  27. localeData.forEach(IntlRelativeFormat.__addLocaleData);
  28. ready(() => {
  29. const locale = document.documentElement.lang;
  30. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  31. year: 'numeric',
  32. month: 'long',
  33. day: 'numeric',
  34. hour: 'numeric',
  35. minute: 'numeric',
  36. });
  37. const relativeFormat = new IntlRelativeFormat(locale);
  38. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  39. content.innerHTML = emojify(content.innerHTML);
  40. });
  41. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  42. const datetime = new Date(content.getAttribute('datetime'));
  43. const formattedDate = dateTimeFormat.format(datetime);
  44. content.title = formattedDate;
  45. content.textContent = formattedDate;
  46. });
  47. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  48. const datetime = new Date(content.getAttribute('datetime'));
  49. content.title = dateTimeFormat.format(datetime);
  50. content.textContent = relativeFormat.format(datetime);
  51. });
  52. [].forEach.call(document.querySelectorAll('.logo-button'), (content) => {
  53. content.addEventListener('click', (e) => {
  54. e.preventDefault();
  55. window.open(e.target.href, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  56. });
  57. });
  58. const reactComponents = document.querySelectorAll('[data-component]');
  59. if (reactComponents.length > 0) {
  60. import(/* webpackChunkName: "containers/media_container" */ '../mastodon/containers/media_container')
  61. .then(({ default: MediaContainer }) => {
  62. const content = document.createElement('div');
  63. ReactDOM.render(<MediaContainer locale={locale} components={reactComponents} />, content);
  64. document.body.appendChild(content);
  65. })
  66. .catch(error => console.error(error));
  67. }
  68. });
  69. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  70. if (button !== 0) {
  71. return true;
  72. }
  73. window.location.href = target.href;
  74. return false;
  75. });
  76. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  77. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  78. if (contentEl.style.display === 'block') {
  79. contentEl.style.display = 'none';
  80. target.parentNode.style.marginBottom = 0;
  81. } else {
  82. contentEl.style.display = 'block';
  83. target.parentNode.style.marginBottom = null;
  84. }
  85. return false;
  86. });
  87. delegate(document, '.account_display_name', 'input', ({ target }) => {
  88. const nameCounter = document.querySelector('.name-counter');
  89. if (nameCounter) {
  90. nameCounter.textContent = 30 - length(target.value);
  91. }
  92. });
  93. delegate(document, '.account_note', 'input', ({ target }) => {
  94. const noteCounter = document.querySelector('.note-counter');
  95. if (noteCounter) {
  96. noteCounter.textContent = 160 - length(target.value);
  97. }
  98. });
  99. delegate(document, '#account_avatar', 'change', ({ target }) => {
  100. const avatar = document.querySelector('.card.compact .avatar img');
  101. const [file] = target.files || [];
  102. const url = file ? URL.createObjectURL(file) : avatar.dataset.originalSrc;
  103. avatar.src = url;
  104. });
  105. delegate(document, '#account_header', 'change', ({ target }) => {
  106. const header = document.querySelector('.card.compact');
  107. const [file] = target.files || [];
  108. const url = file ? URL.createObjectURL(file) : header.dataset.originalSrc;
  109. header.style.backgroundImage = `url(${url})`;
  110. });
  111. }
  112. loadPolyfills().then(main).catch(error => {
  113. console.error(error);
  114. });