闭社主体 forked from https://github.com/tootsuite/mastodon
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.

121 lines
4.1 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.title = dateTimeFormat.format(datetime);
  33. content.textContent = relativeFormat.format(datetime);
  34. });
  35. [].forEach.call(document.querySelectorAll('.logo-button'), (content) => {
  36. content.addEventListener('click', (e) => {
  37. e.preventDefault();
  38. window.open(e.target.href, 'mastodon-intent', 'width=400,height=400,resizable=no,menubar=no,status=no,scrollbars=yes');
  39. });
  40. });
  41. if (window.parent) {
  42. window.parent.postMessage(['setHeight', document.getElementsByTagName('html')[0].scrollHeight], '*');
  43. }
  44. });
  45. delegate(document, '.video-player video', 'click', ({ target }) => {
  46. if (target.paused) {
  47. target.play();
  48. } else {
  49. target.pause();
  50. }
  51. });
  52. delegate(document, '.activity-stream .media-spoiler-wrapper .media-spoiler', 'click', function() {
  53. this.parentNode.classList.add('media-spoiler-wrapper__visible');
  54. });
  55. delegate(document, '.activity-stream .media-spoiler-wrapper .spoiler-button', 'click', function() {
  56. this.parentNode.classList.remove('media-spoiler-wrapper__visible');
  57. });
  58. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  59. if (button !== 0) {
  60. return true;
  61. }
  62. window.location.href = target.href;
  63. return false;
  64. });
  65. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  66. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  67. if (contentEl.style.display === 'block') {
  68. contentEl.style.display = 'none';
  69. target.parentNode.style.marginBottom = 0;
  70. } else {
  71. contentEl.style.display = 'block';
  72. target.parentNode.style.marginBottom = null;
  73. }
  74. return false;
  75. });
  76. delegate(document, '.account_display_name', 'input', ({ target }) => {
  77. const nameCounter = document.querySelector('.name-counter');
  78. if (nameCounter) {
  79. nameCounter.textContent = 30 - length(target.value);
  80. }
  81. });
  82. delegate(document, '.account_note', 'input', ({ target }) => {
  83. const noteCounter = document.querySelector('.note-counter');
  84. if (noteCounter) {
  85. noteCounter.textContent = 160 - length(target.value);
  86. }
  87. });
  88. delegate(document, '#account_avatar', 'change', ({ target }) => {
  89. const avatar = document.querySelector('.card.compact .avatar img');
  90. const [file] = target.files || [];
  91. const url = URL.createObjectURL(file);
  92. avatar.src = url;
  93. });
  94. delegate(document, '#account_header', 'change', ({ target }) => {
  95. const header = document.querySelector('.card.compact');
  96. const [file] = target.files || [];
  97. const url = URL.createObjectURL(file);
  98. header.style.backgroundImage = `url(${url})`;
  99. });
  100. }
  101. loadPolyfills().then(main).catch(error => {
  102. console.error(error);
  103. });