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.

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