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

91 lines
2.8 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 main() {
  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. document.addEventListener('DOMContentLoaded', () => {
  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, '.media-spoiler', 'click', ({ target }) => {
  43. target.style.display = 'none';
  44. });
  45. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  46. if (button !== 0) {
  47. return true;
  48. }
  49. window.location.href = target.href;
  50. return false;
  51. });
  52. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  53. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  54. if (contentEl.style.display === 'block') {
  55. contentEl.style.display = 'none';
  56. target.parentNode.style.marginBottom = 0;
  57. } else {
  58. contentEl.style.display = 'block';
  59. target.parentNode.style.marginBottom = null;
  60. }
  61. return false;
  62. });
  63. delegate(document, '.account_display_name', 'input', ({ target }) => {
  64. const nameCounter = document.querySelector('.name-counter');
  65. if (nameCounter) {
  66. nameCounter.textContent = 30 - length(target.value);
  67. }
  68. });
  69. delegate(document, '.account_note', 'input', ({ target }) => {
  70. const noteCounter = document.querySelector('.note-counter');
  71. if (noteCounter) {
  72. noteCounter.textContent = 160 - length(target.value);
  73. }
  74. });
  75. }
  76. loadPolyfills().then(main).catch(error => {
  77. console.log(error); // eslint-disable-line no-console
  78. });