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

107 lines
2.7 KiB

  1. import emojify from 'mastodon/emoji';
  2. import { length } from 'stringz';
  3. import { default as dateFormat } from 'date-fns/format';
  4. import distanceInWordsStrict from 'date-fns/distance_in_words_strict';
  5. import { delegate } from 'rails-ujs';
  6. require.context('../images/', true);
  7. const parseFormat = (format) => format.replace(/%(\w)/g, (_, modifier) => {
  8. switch (modifier) {
  9. case '%':
  10. return '%';
  11. case 'a':
  12. return 'ddd';
  13. case 'A':
  14. return 'ddd';
  15. case 'b':
  16. return 'MMM';
  17. case 'B':
  18. return 'MMMM';
  19. case 'd':
  20. return 'DD';
  21. case 'H':
  22. return 'HH';
  23. case 'I':
  24. return 'hh';
  25. case 'l':
  26. return 'H';
  27. case 'm':
  28. return 'M';
  29. case 'M':
  30. return 'mm';
  31. case 'p':
  32. return 'A';
  33. case 'S':
  34. return 'ss';
  35. case 'w':
  36. return 'd';
  37. case 'y':
  38. return 'YY';
  39. case 'Y':
  40. return 'YYYY';
  41. default:
  42. return `%${modifier}`;
  43. }
  44. });
  45. document.addEventListener('DOMContentLoaded', () => {
  46. [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
  47. content.innerHTML = emojify(content.innerHTML);
  48. });
  49. [].forEach.call(document.querySelectorAll('time[data-format]'), (content) => {
  50. const format = parseFormat(content.dataset.format);
  51. const formattedDate = dateFormat(content.getAttribute('datetime'), format);
  52. content.textContent = formattedDate;
  53. });
  54. [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
  55. const timeAgo = distanceInWordsStrict(new Date(), content.getAttribute('datetime'), {
  56. addSuffix: true,
  57. });
  58. content.textContent = timeAgo;
  59. });
  60. });
  61. delegate(document, '.video-player video', 'click', ({ target }) => {
  62. if (target.paused) {
  63. target.play();
  64. } else {
  65. target.pause();
  66. }
  67. });
  68. delegate(document, '.media-spoiler', 'click', ({ target }) => {
  69. target.style.display = 'none';
  70. });
  71. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  72. if (button !== 0) {
  73. return true;
  74. }
  75. window.location.href = target.href;
  76. return false;
  77. });
  78. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  79. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  80. if (contentEl.style.display === 'block') {
  81. contentEl.style.display = 'none';
  82. target.parentNode.style.marginBottom = 0;
  83. } else {
  84. contentEl.style.display = 'block';
  85. target.parentNode.style.marginBottom = null;
  86. }
  87. return false;
  88. });
  89. delegate(document, '.account_display_name', 'input', ({ target }) => {
  90. const nameCounter = document.querySelector('.name-counter');
  91. nameCounter.textContent = 30 - length(target.value);
  92. });
  93. delegate(document, '.account_note', 'input', ({ target }) => {
  94. const noteCounter = document.querySelector('.note-counter');
  95. noteCounter.textContent = 160 - length(target.value);
  96. });