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

97 lines
2.9 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. require.context('../images/', true);
  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. if (['interactive', 'complete'].includes(document.readyState)) {
  36. loaded();
  37. } else {
  38. document.addEventListener('DOMContentLoaded', loaded);
  39. }
  40. delegate(document, '.video-player video', 'click', ({ target }) => {
  41. if (target.paused) {
  42. target.play();
  43. } else {
  44. target.pause();
  45. }
  46. });
  47. delegate(document, '.media-spoiler', 'click', ({ target }) => {
  48. target.style.display = 'none';
  49. });
  50. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  51. if (button !== 0) {
  52. return true;
  53. }
  54. window.location.href = target.href;
  55. return false;
  56. });
  57. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  58. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  59. if (contentEl.style.display === 'block') {
  60. contentEl.style.display = 'none';
  61. target.parentNode.style.marginBottom = 0;
  62. } else {
  63. contentEl.style.display = 'block';
  64. target.parentNode.style.marginBottom = null;
  65. }
  66. return false;
  67. });
  68. delegate(document, '.account_display_name', 'input', ({ target }) => {
  69. const nameCounter = document.querySelector('.name-counter');
  70. if (nameCounter) {
  71. nameCounter.textContent = 30 - length(target.value);
  72. }
  73. });
  74. delegate(document, '.account_note', 'input', ({ target }) => {
  75. const noteCounter = document.querySelector('.note-counter');
  76. if (noteCounter) {
  77. noteCounter.textContent = 160 - length(target.value);
  78. }
  79. });
  80. }
  81. loadPolyfills().then(main).catch(error => {
  82. console.error(error);
  83. });