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
2.9 KiB

  1. import emojify from 'mastodon/emoji';
  2. import { getLocale } from 'mastodon/locales';
  3. import { length } from 'stringz';
  4. import IntlRelativeFormat from 'intl-relativeformat';
  5. import { delegate } from 'rails-ujs';
  6. require.context('../images/', true);
  7. const { localeData } = getLocale();
  8. localeData.forEach(IntlRelativeFormat.__addLocaleData);
  9. function main() {
  10. const locale = document.documentElement.lang;
  11. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  12. year: 'numeric',
  13. month: 'long',
  14. day: 'numeric',
  15. hour: 'numeric',
  16. minute: 'numeric',
  17. });
  18. const relativeFormat = new IntlRelativeFormat(locale);
  19. document.addEventListener('DOMContentLoaded', () => {
  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. delegate(document, '.video-player video', 'click', ({ target }) => {
  35. if (target.paused) {
  36. target.play();
  37. } else {
  38. target.pause();
  39. }
  40. });
  41. delegate(document, '.media-spoiler', 'click', ({ target }) => {
  42. target.style.display = 'none';
  43. });
  44. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  45. if (button !== 0) {
  46. return true;
  47. }
  48. window.location.href = target.href;
  49. return false;
  50. });
  51. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  52. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  53. if (contentEl.style.display === 'block') {
  54. contentEl.style.display = 'none';
  55. target.parentNode.style.marginBottom = 0;
  56. } else {
  57. contentEl.style.display = 'block';
  58. target.parentNode.style.marginBottom = null;
  59. }
  60. return false;
  61. });
  62. delegate(document, '.account_display_name', 'input', ({ target }) => {
  63. const nameCounter = document.querySelector('.name-counter');
  64. if (nameCounter) {
  65. nameCounter.textContent = 30 - length(target.value);
  66. }
  67. });
  68. delegate(document, '.account_note', 'input', ({ target }) => {
  69. const noteCounter = document.querySelector('.note-counter');
  70. if (noteCounter) {
  71. noteCounter.textContent = 160 - length(target.value);
  72. }
  73. });
  74. }
  75. if (!window.Intl) {
  76. import(/* webpackChunkName: "base_polyfills" */ 'mastodon/base_polyfills').then(() => {
  77. main();
  78. }).catch(error => {
  79. console.log(error); // eslint-disable-line no-console
  80. });
  81. } else {
  82. main();
  83. }