index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <view :class="'_root ' + (selectable ? '_select' : '')" :style="containerStyle">
  3. <slot v-if="!nodes[0]" />
  4. <node id="_root" :childs="nodes" :opts="[lazyLoad, loadingImg, errorImg, showImgMenu, selectable]" @add.stop.prevent="addFun($event, { tagId: '_root' })" />
  5. </view>
  6. </template>
  7. <script>
  8. 'use strict';
  9. import node from './node/node';
  10. function e(t) {
  11. '@babel/helpers - typeof';
  12. return (e =
  13. 'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator
  14. ? function (e) {
  15. return typeof e;
  16. }
  17. : function (e) {
  18. return e && 'function' == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? 'symbol' : typeof e;
  19. })(t);
  20. }
  21. function t(e, t, o) {
  22. (t = n(t)) in e
  23. ? Object.defineProperty(e, t, {
  24. value: o,
  25. enumerable: true,
  26. configurable: true,
  27. writable: true
  28. })
  29. : (e[t] = o);
  30. return e;
  31. }
  32. function n(t) {
  33. var n = o(t, 'string');
  34. return 'symbol' == e(n) ? n : n + '';
  35. }
  36. function o(t, n) {
  37. if ('object' != e(t) || !t) {
  38. return t;
  39. }
  40. var o = t[Symbol.toPrimitive];
  41. if (void 0 !== o) {
  42. var i = o.call(t, n || 'default');
  43. if ('object' != e(i)) {
  44. return i;
  45. }
  46. throw new TypeError('@@toPrimitive must return a primitive value.');
  47. }
  48. return ('string' === n ? String : Number)(t);
  49. }
  50. /*!
  51. * mp-html v2.5.0
  52. * https://github.com/jin-yufeng/mp-html
  53. *
  54. * Released under the MIT license
  55. * Author: Jin Yufeng
  56. */
  57. var i = require('./parser');
  58. var r = [];
  59. export default {
  60. components: {
  61. node
  62. },
  63. data() {
  64. return {
  65. nodes: []
  66. };
  67. },
  68. props: {
  69. containerStyle: String,
  70. content: {
  71. type: String,
  72. default: ''
  73. },
  74. copyLink: {
  75. type: Boolean,
  76. default: true
  77. },
  78. domain: String,
  79. errorImg: String,
  80. lazyLoad: Boolean,
  81. loadingImg: String,
  82. pauseVideo: {
  83. type: Boolean,
  84. default: true
  85. },
  86. previewImg: {
  87. type: null,
  88. default: true
  89. },
  90. scrollTable: Boolean,
  91. selectable: null,
  92. setTitle: {
  93. type: Boolean,
  94. default: true
  95. },
  96. showImgMenu: {
  97. type: Boolean,
  98. default: true
  99. },
  100. tagStyle: Object,
  101. useAnchor: null
  102. },
  103. created: function () {
  104. this.plugins = [];
  105. for (var e = r.length; e--; ) {
  106. this.plugins.push(new r[e](this));
  107. }
  108. },
  109. destroyed: function () {
  110. this.hookFun('onDetached');
  111. },
  112. methods: {
  113. inFun: function (e, t, n) {
  114. if (e && t && n) {
  115. this._in = {
  116. page: e,
  117. selector: t,
  118. scrollTop: n
  119. };
  120. }
  121. },
  122. navigateTo: function (e, n) {
  123. var that = this;
  124. return new Promise(function (i, r) {
  125. if (!that.useAnchor) {
  126. return void r(Error('Anchor is disabled'));
  127. }
  128. var a = uni
  129. .createSelectorQuery()
  130. .in(that._in ? that._in.page : that)
  131. .select((that._in ? that._in.selector : '._root') + (e ? ''.concat('>>>', '#').concat(e) : ''))
  132. .boundingClientRect();
  133. that._in ? a.select(that._in.selector).scrollOffset().select(that._in.selector).boundingClientRect() : a.selectViewport().scrollOffset();
  134. a.exec(function (e) {
  135. if (!e[0]) {
  136. return void r(Error('Label not found'));
  137. }
  138. var a = e[1].scrollTop + e[0].top - (e[2] ? e[2].top : 0) + (n || parseInt(that.useAnchor) || 0);
  139. that._in
  140. ? that._in.page.setData(t({}, that._in.scrollTop, a))
  141. : uni.pageScrollTo({
  142. scrollTop: a,
  143. duration: 300
  144. });
  145. i();
  146. });
  147. });
  148. },
  149. getText: function (e) {
  150. var t = '';
  151. (function e(n) {
  152. for (var o = 0; o < n.length; o++) {
  153. var i = n[o];
  154. if ('text' === i.type) t += i.text.replace(/&amp;/g, '&');
  155. else if ('br' === i.name) {
  156. t += '\n';
  157. } else {
  158. var r = 'p' === i.name || 'div' === i.name || 'tr' === i.name || 'li' === i.name || ('h' === i.name[0] && i.name[1] > '0' && i.name[1] < '7');
  159. if (r && t && '\n' !== t[t.length - 1]) {
  160. t += '\n';
  161. }
  162. if (i.children) {
  163. e(i.children);
  164. }
  165. r && '\n' !== t[t.length - 1] ? (t += '\n') : ('td' !== i.name && 'th' !== i.name) || (t += '\t');
  166. }
  167. }
  168. })(e || this.nodes);
  169. return t;
  170. },
  171. getRect: function () {
  172. var that = this;
  173. return new Promise(function (t, n) {
  174. uni.createSelectorQuery()
  175. .in(that)
  176. .select('._root')
  177. .boundingClientRect()
  178. .exec(function (e) {
  179. return e[0] ? t(e[0]) : n(Error('Root label not found'));
  180. });
  181. });
  182. },
  183. pauseMedia: function () {
  184. for (var e = (this._videos || []).length; e--; ) {
  185. this._videos[e].pause();
  186. }
  187. },
  188. setPlaybackRate: function (e) {
  189. this.playbackRate = e;
  190. for (var t = (this._videos || []).length; t--; ) {
  191. this._videos[t].playbackRate(e);
  192. }
  193. },
  194. setContent: function (e, t) {
  195. var that = this;
  196. (this.imgList && t) || (this.imgList = []);
  197. this._videos = [];
  198. var o = {};
  199. var r = new i(this).parse(e);
  200. if (t) {
  201. for (var a = this.nodes.length, s = r.length; s--; ) {
  202. o['nodes['.concat(a + s, ']')] = r[s];
  203. }
  204. } else {
  205. o.nodes = r;
  206. }
  207. this.setData(o, function () {
  208. that.hookFun('onLoad');
  209. that.$emit('load');
  210. });
  211. if (this.lazyLoad || this.imgList._unloadimgs < this.imgList.length / 2) {
  212. var l = 0;
  213. var c = function (e) {
  214. (e && e.height) || (e = {});
  215. e.height === l
  216. ? that.$emit('ready', {
  217. detail: e
  218. })
  219. : ((l = e.height),
  220. setTimeout(function () {
  221. that.getRect().then(c).catch(c);
  222. }, 350));
  223. };
  224. this.getRect().then(c).catch(c);
  225. } else {
  226. this.imgList._unloadimgs ||
  227. this.getRect()
  228. .then(function (e) {
  229. that.$emit('ready', {
  230. detail: e
  231. });
  232. })
  233. .catch(function () {
  234. that.$emit('ready', {
  235. detail: {}
  236. });
  237. });
  238. }
  239. },
  240. hookFun: function (e) {
  241. for (var t = r.length; t--; ) {
  242. if (this.plugins[t][e]) {
  243. this.plugins[t][e]();
  244. }
  245. }
  246. },
  247. addFun: function (e, _dataset) {
  248. /* ---处理dataset begin--- */
  249. this.handleDataset(e, _dataset);
  250. /* ---处理dataset end--- */
  251. e.detail.root = this;
  252. }
  253. },
  254. watch: {
  255. content: {
  256. handler: function (e) {
  257. this.setContent(e);
  258. },
  259. immediate: true
  260. }
  261. }
  262. };
  263. </script>
  264. <style>
  265. ._root {
  266. /* padding: 1px 0;
  267. overflow-x: auto;
  268. overflow-y: hidden; */
  269. }
  270. ._select {
  271. -webkit-user-select: text;
  272. user-select: text;
  273. }
  274. </style>