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.

109 lines
3.6 KiB

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