通用评论

index.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _slider = require("antd/es/slider");
  6. var _slider2 = _interopRequireDefault(_slider);
  7. var _icon = require("antd/es/icon");
  8. var _icon2 = _interopRequireDefault(_icon);
  9. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  10. require("antd/es/slider/style");
  11. require("antd/es/icon/style");
  12. var _react = require("react");
  13. var _react2 = _interopRequireDefault(_react);
  14. var _dayjs = require("dayjs");
  15. var _dayjs2 = _interopRequireDefault(_dayjs);
  16. var _duration = require("dayjs/plugin/duration");
  17. var _duration2 = _interopRequireDefault(_duration);
  18. var _utc = require("dayjs/plugin/utc");
  19. var _utc2 = _interopRequireDefault(_utc);
  20. require("./index.less");
  21. var _loading = require("../../assert/loading.gif");
  22. var _loading2 = _interopRequireDefault(_loading);
  23. var _btn_audio_play = require("../../assert/btn_audio_play.png");
  24. var _btn_audio_play2 = _interopRequireDefault(_btn_audio_play);
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  27. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  28. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  29. _dayjs2.default.extend(_duration2.default);
  30. _dayjs2.default.extend(_utc2.default);
  31. var AudioPlayer = function (_React$Component) {
  32. _inherits(AudioPlayer, _React$Component);
  33. function AudioPlayer(props) {
  34. _classCallCheck(this, AudioPlayer);
  35. var _this = _possibleConstructorReturn(this, (AudioPlayer.__proto__ || Object.getPrototypeOf(AudioPlayer)).call(this, props));
  36. _this.state = {
  37. duration: 0,
  38. currentDuration: 0,
  39. isPlaying: false,
  40. isLoading: false
  41. };
  42. return _this;
  43. }
  44. _createClass(AudioPlayer, [{
  45. key: "componentDidMount",
  46. value: function componentDidMount() {
  47. var _this2 = this;
  48. if (this.player) {
  49. var src = this.props.src;
  50. this.player.oncanplay = function (event) {
  51. return _this2.setState({ duration: event.target.duration });
  52. };
  53. this.player.onended = function () {
  54. return _this2.setState({ isPlaying: false, currentDuration: 0 });
  55. };
  56. this.player.onpause = function () {
  57. return _this2.setState({ isPlaying: false, isLoading: false });
  58. };
  59. this.player.onplay = function () {
  60. return _this2.setState({ isPlaying: false, isLoading: true });
  61. };
  62. this.player.onplaying = function () {
  63. return _this2.setState({ isPlaying: true, isLoading: false });
  64. };
  65. this.player.ontimeupdate = function (event) {
  66. return _this2.setState({ currentDuration: event.target.currentTime });
  67. };
  68. this.player.src = src;
  69. }
  70. }
  71. }, {
  72. key: "componentDidUpdate",
  73. value: function componentDidUpdate(prevProps) {
  74. var _this3 = this;
  75. if (this.props.src !== prevProps.src) {
  76. this.player.oncanplay = function (event) {
  77. return _this3.setState({ duration: event.target.duration });
  78. };
  79. this.player.onended = function () {
  80. return _this3.setState({ isPlaying: false, currentDuration: 0 });
  81. };
  82. this.player.onpause = function () {
  83. return _this3.setState({ isPlaying: false, isLoading: false });
  84. };
  85. this.player.onplay = function () {
  86. return _this3.setState({ isPlaying: false, isLoading: true });
  87. };
  88. this.player.onplaying = function () {
  89. return _this3.setState({ isPlaying: true, isLoading: false });
  90. };
  91. this.player.ontimeupdate = function (event) {
  92. return _this3.setState({ currentDuration: event.target.currentTime });
  93. };
  94. this.player.src = this.props.src;
  95. }
  96. }
  97. }, {
  98. key: "clickPlayOrPause",
  99. value: function clickPlayOrPause(isPlaying) {
  100. if (!this.player) {
  101. return;
  102. }
  103. if (isPlaying) {
  104. this.player.pause();
  105. } else {
  106. // 如果是播放,先暂停其他的播放
  107. var playerList = document.getElementsByTagName("audio");
  108. if (playerList) {
  109. Array.from(playerList).forEach(function (player) {
  110. if (!player.paused) {
  111. player.pause();
  112. }
  113. });
  114. }
  115. this.player.play();
  116. }
  117. }
  118. }, {
  119. key: "getPlayIcon",
  120. value: function getPlayIcon() {
  121. var _state = this.state,
  122. isPlaying = _state.isPlaying,
  123. isLoading = _state.isLoading;
  124. var playIconElem = void 0;
  125. if (isLoading) {
  126. playIconElem = _react2.default.createElement("img", { className: "icon-loading", src: _loading2.default, alt: "iconLoading" });
  127. } else if (isPlaying) {
  128. playIconElem = _react2.default.createElement(_icon2.default, { className: "pause", type: "pause" });
  129. } else {
  130. playIconElem = _react2.default.createElement("img", { className: "icon-loading", src: _btn_audio_play2.default, alt: "iconPlaying" });
  131. }
  132. return playIconElem;
  133. }
  134. }, {
  135. key: "render",
  136. value: function render() {
  137. var _this4 = this;
  138. var _state2 = this.state,
  139. currentDuration = _state2.currentDuration,
  140. isPlaying = _state2.isPlaying,
  141. duration = _state2.duration;
  142. return _react2.default.createElement(
  143. "div",
  144. { className: "comment-item-speak-audio-container" },
  145. _react2.default.createElement("audio", {
  146. ref: function ref(_ref) {
  147. _this4.player = _ref;
  148. },
  149. style: { display: "none" }
  150. }),
  151. _react2.default.createElement(
  152. "span",
  153. {
  154. className: "icon",
  155. onClick: function onClick() {
  156. _this4.clickPlayOrPause(isPlaying);
  157. }
  158. },
  159. this.getPlayIcon()
  160. ),
  161. _react2.default.createElement(_slider2.default, {
  162. step: 0.001,
  163. className: "slider",
  164. tooltipVisible: false,
  165. value: currentDuration,
  166. max: duration,
  167. onChange: function onChange(value) {
  168. if (_this4.player) {
  169. _this4.player.currentTime = value;
  170. }
  171. }
  172. }),
  173. _react2.default.createElement(
  174. "span",
  175. { className: "time" },
  176. _dayjs2.default.utc(_dayjs2.default.duration(currentDuration, "seconds").asMilliseconds()).format("mm:ss"),
  177. "/",
  178. _dayjs2.default.utc(_dayjs2.default.duration(duration, "seconds").asMilliseconds()).format("mm:ss")
  179. )
  180. );
  181. }
  182. }]);
  183. return AudioPlayer;
  184. }(_react2.default.Component);
  185. exports.default = AudioPlayer;
  186. //# sourceMappingURL=index.js.map