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.

143 lines
4.2 KiB

  1. import loadPolyfills from '../mastodon/load_polyfills';
  2. import ready from '../mastodon/ready';
  3. window.addEventListener('message', e => {
  4. const data = e.data || {};
  5. if (!window.parent || data.type !== 'setHeight') {
  6. return;
  7. }
  8. ready(() => {
  9. window.parent.postMessage({
  10. type: 'setHeight',
  11. id: data.id,
  12. height: document.getElementsByTagName('html')[0].scrollHeight,
  13. }, '*');
  14. });
  15. });
  16. function main() {
  17. const { length } = require('stringz');
  18. const IntlRelativeFormat = require('intl-relativeformat').default;
  19. const { delegate } = require('rails-ujs');
  20. const emojify = require('../mastodon/emoji').default;
  21. const { getLocale } = require('../mastodon/locales');
  22. const { localeData } = getLocale();
  23. localeData.forEach(IntlRelativeFormat.__addLocaleData);
  24. ready(() => {
  25. const locale = document.documentElement.lang;
  26. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  27. year: 'numeric',
  28. month: 'long',
  29. day: 'numeric',
  30. hour: 'numeric',
  31. minute: 'numeric',
  32. });
  33. const relativeFormat = new IntlRelativeFormat(locale);
  34. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  35. content.innerHTML = emojify(content.innerHTML);
  36. });
  37. [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
  38. const datetime = new Date(content.getAttribute('datetime'));
  39. const formattedDate = dateTimeFormat.format(datetime);
  40. content.title = formattedDate;
  41. content.textContent = formattedDate;
  42. });
  43. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  44. const datetime = new Date(content.getAttribute('datetime'));
  45. content.title = dateTimeFormat.format(datetime);
  46. content.textContent = relativeFormat.format(datetime);
  47. });
  48. [].forEach.call(document.querySelectorAll('.logo-button'), (content) => {
  49. content.addEventListener('click', (e) => {
  50. e.preventDefault();
  51. window.open(e.target.href, 'mastodon-intent', 'width=400,height=400,resizable=no,menubar=no,status=no,scrollbars=yes');
  52. });
  53. });
  54. });
  55. delegate(document, '.video-player video', 'click', ({ target }) => {
  56. if (target.paused) {
  57. target.play();
  58. } else {
  59. target.pause();
  60. }
  61. });
  62. delegate(document, '.activity-stream .media-spoiler-wrapper .media-spoiler', 'click', function() {
  63. this.parentNode.classList.add('media-spoiler-wrapper__visible');
  64. });
  65. delegate(document, '.activity-stream .media-spoiler-wrapper .spoiler-button', 'click', function() {
  66. this.parentNode.classList.remove('media-spoiler-wrapper__visible');
  67. });
  68. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  69. if (button !== 0) {
  70. return true;
  71. }
  72. window.location.href = target.href;
  73. return false;
  74. });
  75. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  76. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  77. if (contentEl.style.display === 'block') {
  78. contentEl.style.display = 'none';
  79. target.parentNode.style.marginBottom = 0;
  80. } else {
  81. contentEl.style.display = 'block';
  82. target.parentNode.style.marginBottom = null;
  83. }
  84. return false;
  85. });
  86. delegate(document, '.account_display_name', 'input', ({ target }) => {
  87. const nameCounter = document.querySelector('.name-counter');
  88. if (nameCounter) {
  89. nameCounter.textContent = 30 - length(target.value);
  90. }
  91. });
  92. delegate(document, '.account_note', 'input', ({ target }) => {
  93. const noteCounter = document.querySelector('.note-counter');
  94. if (noteCounter) {
  95. noteCounter.textContent = 160 - length(target.value);
  96. }
  97. });
  98. delegate(document, '#account_avatar', 'change', ({ target }) => {
  99. const avatar = document.querySelector('.card.compact .avatar img');
  100. const [file] = target.files || [];
  101. const url = URL.createObjectURL(file);
  102. avatar.src = url;
  103. });
  104. delegate(document, '#account_header', 'change', ({ target }) => {
  105. const header = document.querySelector('.card.compact');
  106. const [file] = target.files || [];
  107. const url = URL.createObjectURL(file);
  108. header.style.backgroundImage = `url(${url})`;
  109. });
  110. }
  111. loadPolyfills().then(main).catch(error => {
  112. console.error(error);
  113. });