闭社主体 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.

96 lines
3.0 KiB

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