index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  17. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. import { useEffect, useEvent, usePageShow, useState, } from 'functional-mini/component';
  38. import '../_util/assert-component2';
  39. import { mountComponent } from '../_util/component';
  40. import { useComponentEvent } from '../_util/hooks/useComponentEvent';
  41. import { useEvent as useStableCallback } from '../_util/hooks/useEvent';
  42. import { useInstanceBoundingClientRect } from '../_util/hooks/useInstanceBoundingClientRect';
  43. import { NoticeBarFunctionalProps } from './props';
  44. var NoticeBar = function (props) {
  45. var _a = useState(''), marqueeStyle = _a[0], setMarqueeStyle = _a[1];
  46. var _b = useState(true), show = _b[0], setShow = _b[1];
  47. var triggerEventOnly = useComponentEvent(props).triggerEventOnly;
  48. var startMarquee = useStableCallback(function (state) {
  49. var loop = props.loop;
  50. var leading = 500;
  51. var duration = state.duration, overflowWidth = state.overflowWidth, viewWidth = state.viewWidth;
  52. var marqueeScrollWidth = overflowWidth;
  53. if (loop) {
  54. marqueeScrollWidth = overflowWidth + viewWidth;
  55. }
  56. var newMarqueeStyle = "transform: translate3d(".concat(-marqueeScrollWidth, "px, 0, 0); transition: ").concat(duration, "s all linear ").concat(typeof leading === 'number' ? "".concat(leading / 1000, "s") : '0s', ";");
  57. setMarqueeStyle(newMarqueeStyle);
  58. });
  59. var getBoundingClientRectWithId = useInstanceBoundingClientRect().getBoundingClientRectWithId;
  60. function measureText(callback) {
  61. var _this = this;
  62. var fps = 40;
  63. var loop = props.loop;
  64. // 计算文本所占据的宽度,计算需要滚动的宽度
  65. setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
  66. var marqueeSize, contentSize, overflowWidth, viewWidth, marqueeScrollWidth;
  67. return __generator(this, function (_a) {
  68. switch (_a.label) {
  69. case 0: return [4 /*yield*/, getBoundingClientRectWithId('.ant-notice-bar-marquee')];
  70. case 1:
  71. marqueeSize = _a.sent();
  72. return [4 /*yield*/, getBoundingClientRectWithId('.ant-notice-bar-content')];
  73. case 2:
  74. contentSize = _a.sent();
  75. overflowWidth = (marqueeSize && contentSize && marqueeSize.width - contentSize.width) ||
  76. 0;
  77. viewWidth = (contentSize === null || contentSize === void 0 ? void 0 : contentSize.width) || 0;
  78. marqueeScrollWidth = overflowWidth;
  79. if (loop) {
  80. marqueeScrollWidth = overflowWidth + viewWidth;
  81. }
  82. if (overflowWidth > 0) {
  83. callback({
  84. overflowWidth: overflowWidth,
  85. viewWidth: viewWidth,
  86. duration: marqueeScrollWidth / fps,
  87. });
  88. }
  89. return [2 /*return*/];
  90. }
  91. });
  92. }); }, 0);
  93. }
  94. useEffect(function () {
  95. var enableMarquee = props.enableMarquee;
  96. if (enableMarquee) {
  97. measureText(startMarquee);
  98. }
  99. });
  100. function resetMarquee(state) {
  101. var loop = props.loop;
  102. var viewWidth = state.viewWidth;
  103. var showMarqueeWidth = '0px';
  104. if (loop) {
  105. showMarqueeWidth = "".concat(viewWidth, "px");
  106. }
  107. var marqueeStyle = "transform: translate3d(".concat(showMarqueeWidth, ", 0, 0); transition: 0s all linear;");
  108. setMarqueeStyle(marqueeStyle);
  109. }
  110. useEvent('onTransitionEnd', function () {
  111. var loop = props.loop;
  112. var trailing = 200;
  113. if (loop) {
  114. setTimeout(function () {
  115. measureText(function (state) {
  116. resetMarquee(state);
  117. });
  118. }, trailing);
  119. }
  120. });
  121. useEvent('onTap', function () {
  122. var mode = props.mode;
  123. if (mode === 'link') {
  124. triggerEventOnly('tap');
  125. }
  126. if (mode === 'closeable') {
  127. setShow(false);
  128. triggerEventOnly('tap');
  129. }
  130. });
  131. usePageShow(function () {
  132. if (props.enableMarquee) {
  133. setMarqueeStyle('');
  134. resetMarquee({
  135. overflowWidth: 0,
  136. duration: 0,
  137. viewWidth: 0,
  138. });
  139. measureText(startMarquee);
  140. }
  141. });
  142. return {
  143. marqueeStyle: marqueeStyle,
  144. show: show,
  145. };
  146. };
  147. mountComponent(NoticeBar, NoticeBarFunctionalProps);