Barrage.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var __async = (__this, __arguments, generator) => {
  19. return new Promise((resolve, reject) => {
  20. var fulfilled = (value) => {
  21. try {
  22. step(generator.next(value));
  23. } catch (e) {
  24. reject(e);
  25. }
  26. };
  27. var rejected = (value) => {
  28. try {
  29. step(generator.throw(value));
  30. } catch (e) {
  31. reject(e);
  32. }
  33. };
  34. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  35. step((generator = generator.apply(__this, __arguments)).next());
  36. });
  37. };
  38. var stdin_exports = {};
  39. __export(stdin_exports, {
  40. barrageProps: () => barrageProps,
  41. default: () => stdin_default
  42. });
  43. module.exports = __toCommonJS(stdin_exports);
  44. var import_vue = require("vue");
  45. var import_use_expose = require("../composables/use-expose");
  46. var import_utils = require("../utils");
  47. const barrageProps = {
  48. top: (0, import_utils.makeNumericProp)(10),
  49. rows: (0, import_utils.makeNumericProp)(4),
  50. duration: (0, import_utils.makeNumericProp)(4e3),
  51. autoPlay: import_utils.truthProp,
  52. delay: (0, import_utils.makeNumberProp)(300),
  53. modelValue: (0, import_utils.makeArrayProp)()
  54. };
  55. const [name, bem] = (0, import_utils.createNamespace)("barrage");
  56. var stdin_default = (0, import_vue.defineComponent)({
  57. name,
  58. props: barrageProps,
  59. emits: ["update:modelValue"],
  60. setup(props, {
  61. emit,
  62. slots
  63. }) {
  64. const barrageWrapper = (0, import_vue.ref)();
  65. const className = bem("item");
  66. const total = (0, import_vue.ref)(0);
  67. const barrageItems = [];
  68. const createBarrageItem = (text, delay = props.delay) => {
  69. const item = document.createElement("span");
  70. item.className = className;
  71. item.innerText = String(text);
  72. item.style.animationDuration = `${props.duration}ms`;
  73. item.style.animationDelay = `${delay}ms`;
  74. item.style.animationName = "van-barrage";
  75. item.style.animationTimingFunction = "linear";
  76. return item;
  77. };
  78. const isInitBarrage = (0, import_vue.ref)(true);
  79. const isPlay = (0, import_vue.ref)(props.autoPlay);
  80. const appendBarrageItem = ({
  81. id,
  82. text
  83. }, i) => {
  84. var _a;
  85. const item = createBarrageItem(text, isInitBarrage.value ? i * props.delay : void 0);
  86. if (!props.autoPlay && isPlay.value === false) {
  87. item.style.animationPlayState = "paused";
  88. }
  89. (_a = barrageWrapper.value) == null ? void 0 : _a.append(item);
  90. total.value++;
  91. const top = (total.value - 1) % +props.rows * item.offsetHeight + +props.top;
  92. item.style.top = `${top}px`;
  93. item.dataset.id = String(id);
  94. barrageItems.push(item);
  95. item.addEventListener("animationend", () => {
  96. emit("update:modelValue", [...props.modelValue].filter((v) => String(v.id) !== item.dataset.id));
  97. });
  98. };
  99. const updateBarrages = (newValue, oldValue) => {
  100. const map = new Map(oldValue.map((item) => [item.id, item]));
  101. newValue.forEach((item, i) => {
  102. if (map.has(item.id)) {
  103. map.delete(item.id);
  104. } else {
  105. appendBarrageItem(item, i);
  106. }
  107. });
  108. map.forEach((item) => {
  109. const index = barrageItems.findIndex((span) => span.dataset.id === String(item.id));
  110. if (index > -1) {
  111. barrageItems[index].remove();
  112. barrageItems.splice(index, 1);
  113. }
  114. });
  115. isInitBarrage.value = false;
  116. };
  117. (0, import_vue.watch)(() => props.modelValue.slice(), (newValue, oldValue) => updateBarrages(newValue != null ? newValue : [], oldValue != null ? oldValue : []), {
  118. deep: true
  119. });
  120. const rootStyle = (0, import_vue.ref)({});
  121. (0, import_vue.onMounted)(() => __async(this, null, function* () {
  122. var _a;
  123. rootStyle.value["--move-distance"] = `-${(_a = barrageWrapper.value) == null ? void 0 : _a.offsetWidth}px`;
  124. yield (0, import_vue.nextTick)();
  125. updateBarrages(props.modelValue, []);
  126. }));
  127. const play = () => {
  128. isPlay.value = true;
  129. barrageItems.forEach((item) => {
  130. item.style.animationPlayState = "running";
  131. });
  132. };
  133. const pause = () => {
  134. isPlay.value = false;
  135. barrageItems.forEach((item) => {
  136. item.style.animationPlayState = "paused";
  137. });
  138. };
  139. (0, import_use_expose.useExpose)({
  140. play,
  141. pause
  142. });
  143. return () => {
  144. var _a;
  145. return (0, import_vue.createVNode)("div", {
  146. "class": bem(),
  147. "ref": barrageWrapper,
  148. "style": rootStyle.value
  149. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
  150. };
  151. }
  152. });