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.

99 lines
3.0 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. import { processBio } from '../mastodon/features/account/util/bio_metadata';
  8. require.context('../images/', true);
  9. const { localeData } = getLocale();
  10. localeData.forEach(IntlRelativeFormat.__addLocaleData);
  11. function loaded() {
  12. const locale = document.documentElement.lang;
  13. const dateTimeFormat = new Intl.DateTimeFormat(locale, {
  14. year: 'numeric',
  15. month: 'long',
  16. day: 'numeric',
  17. hour: 'numeric',
  18. minute: 'numeric',
  19. });
  20. const relativeFormat = new IntlRelativeFormat(locale);
  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. function main() {
  36. if (['interactive', 'complete'].includes(document.readyState)) {
  37. loaded();
  38. } else {
  39. document.addEventListener('DOMContentLoaded', loaded);
  40. }
  41. delegate(document, '.video-player video', 'click', ({ target }) => {
  42. if (target.paused) {
  43. target.play();
  44. } else {
  45. target.pause();
  46. }
  47. });
  48. delegate(document, '.media-spoiler', 'click', ({ target }) => {
  49. target.style.display = 'none';
  50. });
  51. delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
  52. if (button !== 0) {
  53. return true;
  54. }
  55. window.location.href = target.href;
  56. return false;
  57. });
  58. delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
  59. const contentEl = target.parentNode.parentNode.querySelector('.e-content');
  60. if (contentEl.style.display === 'block') {
  61. contentEl.style.display = 'none';
  62. target.parentNode.style.marginBottom = 0;
  63. } else {
  64. contentEl.style.display = 'block';
  65. target.parentNode.style.marginBottom = null;
  66. }
  67. return false;
  68. });
  69. delegate(document, '.account_display_name', 'input', ({ target }) => {
  70. const nameCounter = document.querySelector('.name-counter');
  71. if (nameCounter) {
  72. nameCounter.textContent = 30 - length(target.value);
  73. }
  74. });
  75. delegate(document, '.account_note', 'input', ({ target }) => {
  76. const noteCounter = document.querySelector('.note-counter');
  77. if (noteCounter) {
  78. const noteWithoutMetadata = processBio(target.value).text;
  79. noteCounter.textContent = 500 - length(noteWithoutMetadata);
  80. }
  81. });
  82. }
  83. loadPolyfills().then(main).catch(error => {
  84. console.error(error);
  85. });