chunk.JFYGGU2T.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {
  2. watch
  3. } from "./chunk.XX234VRK.js";
  4. import {
  5. T,
  6. e,
  7. h,
  8. n,
  9. r2 as r
  10. } from "./chunk.5PIDMFOE.js";
  11. import {
  12. __decorateClass
  13. } from "./chunk.IHGPZX35.js";
  14. // src/components/relative-time/relative-time.ts
  15. var SlRelativeTime = class extends h {
  16. constructor() {
  17. super(...arguments);
  18. this.isoTime = "";
  19. this.relativeTime = "";
  20. this.titleTime = "";
  21. this.format = "long";
  22. this.numeric = "auto";
  23. this.sync = false;
  24. }
  25. connectedCallback() {
  26. super.connectedCallback();
  27. this.updateTime();
  28. }
  29. disconnectedCallback() {
  30. super.disconnectedCallback();
  31. clearTimeout(this.updateTimeout);
  32. }
  33. updateTime() {
  34. const now = new Date();
  35. const date = new Date(this.date);
  36. if (isNaN(date.getMilliseconds())) {
  37. this.relativeTime = "";
  38. this.isoTime = "";
  39. return;
  40. }
  41. const diff = +date - +now;
  42. const availableUnits = [
  43. { max: 276e4, value: 6e4, unit: "minute" },
  44. { max: 72e6, value: 36e5, unit: "hour" },
  45. { max: 5184e5, value: 864e5, unit: "day" },
  46. { max: 24192e5, value: 6048e5, unit: "week" },
  47. { max: 28512e6, value: 2592e6, unit: "month" },
  48. { max: Infinity, value: 31536e6, unit: "year" }
  49. ];
  50. const { unit, value } = availableUnits.find((unit2) => Math.abs(diff) < unit2.max);
  51. this.isoTime = date.toISOString();
  52. this.titleTime = new Intl.DateTimeFormat(this.locale, {
  53. month: "long",
  54. year: "numeric",
  55. day: "numeric",
  56. hour: "numeric",
  57. minute: "numeric",
  58. timeZoneName: "short"
  59. }).format(date);
  60. this.relativeTime = new Intl.RelativeTimeFormat(this.locale, {
  61. numeric: this.numeric,
  62. style: this.format
  63. }).format(Math.round(diff / value), unit);
  64. clearTimeout(this.updateTimeout);
  65. if (this.sync) {
  66. const getTimeUntilNextUnit = (unit2) => {
  67. const units = { second: 1e3, minute: 6e4, hour: 36e5, day: 864e5 };
  68. const value2 = units[unit2];
  69. return value2 - now.getTime() % value2;
  70. };
  71. let nextInterval;
  72. if (unit === "minute") {
  73. nextInterval = getTimeUntilNextUnit("second");
  74. } else if (unit === "hour") {
  75. nextInterval = getTimeUntilNextUnit("minute");
  76. } else if (unit === "day") {
  77. nextInterval = getTimeUntilNextUnit("hour");
  78. } else {
  79. nextInterval = getTimeUntilNextUnit("day");
  80. }
  81. this.updateTimeout = setTimeout(() => this.updateTime(), nextInterval);
  82. }
  83. }
  84. render() {
  85. return T` <time datetime=${this.isoTime} title=${this.titleTime}>${this.relativeTime}</time> `;
  86. }
  87. };
  88. __decorateClass([
  89. r()
  90. ], SlRelativeTime.prototype, "isoTime", 2);
  91. __decorateClass([
  92. r()
  93. ], SlRelativeTime.prototype, "relativeTime", 2);
  94. __decorateClass([
  95. r()
  96. ], SlRelativeTime.prototype, "titleTime", 2);
  97. __decorateClass([
  98. e()
  99. ], SlRelativeTime.prototype, "date", 2);
  100. __decorateClass([
  101. e()
  102. ], SlRelativeTime.prototype, "locale", 2);
  103. __decorateClass([
  104. e()
  105. ], SlRelativeTime.prototype, "format", 2);
  106. __decorateClass([
  107. e()
  108. ], SlRelativeTime.prototype, "numeric", 2);
  109. __decorateClass([
  110. e({ type: Boolean })
  111. ], SlRelativeTime.prototype, "sync", 2);
  112. __decorateClass([
  113. watch("date"),
  114. watch("locale"),
  115. watch("format"),
  116. watch("numeric"),
  117. watch("sync")
  118. ], SlRelativeTime.prototype, "updateTime", 1);
  119. SlRelativeTime = __decorateClass([
  120. n("sl-relative-time")
  121. ], SlRelativeTime);
  122. var relative_time_default = SlRelativeTime;
  123. export {
  124. relative_time_default
  125. };