@didaoktv/didaoui-uniapp 2.12.0 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +7 -7
  2. package/components/dd-album/dd-album.vue +280 -228
  3. package/components/dd-avatar/dd-avatar.vue +132 -75
  4. package/components/dd-avatar-group/dd-avatar-group.vue +52 -30
  5. package/components/dd-barcode/dd-barcode.vue +884 -947
  6. package/components/dd-button/dd-button.vue +47 -21
  7. package/components/dd-canvas/dd-canvas.vue +1156 -985
  8. package/components/dd-car-keyboard/dd-car-keyboard.vue +12 -12
  9. package/components/dd-champion-card/dd-champion-card.vue +49 -21
  10. package/components/dd-checkbox-group/dd-checkbox-group.vue +2 -0
  11. package/components/dd-copy/dd-copy.vue +51 -56
  12. package/components/dd-cropper/dd-cropper.vue +1224 -1135
  13. package/components/dd-divider/dd-divider.vue +9 -2
  14. package/components/dd-fold/dd-fold.vue +133 -0
  15. package/components/dd-form/dd-form.vue +2 -0
  16. package/components/dd-icon/dd-icon.vue +1 -1
  17. package/components/dd-keyboard/dd-keyboard.vue +95 -46
  18. package/components/dd-link/dd-link.vue +72 -49
  19. package/components/dd-markdown/dd-markdown.vue +154 -154
  20. package/components/dd-number-keyboard/dd-number-keyboard.vue +109 -94
  21. package/components/dd-parse/dd-parse.vue +473 -442
  22. package/components/dd-poster/dd-poster.vue +579 -567
  23. package/components/dd-pull-refresh/dd-pull-refresh.vue +1 -9
  24. package/components/dd-qrcode/dd-qrcode.vue +431 -405
  25. package/components/dd-radio-group/dd-radio-group.vue +1 -1
  26. package/components/dd-segmented-tab/dd-segmented-tab.vue +22 -14
  27. package/components/dd-signature/dd-signature.vue +377 -357
  28. package/components/dd-step/dd-step.vue +1 -1
  29. package/components/dd-sticky/dd-sticky.vue +5 -10
  30. package/components/dd-swipe-action/dd-swipe-action.vue +1 -9
  31. package/components/dd-swipe-item/dd-swipe-item.vue +2 -0
  32. package/components/dd-swipeable-tab/dd-swipeable-tab.vue +1 -5
  33. package/components/dd-switch/dd-switch.vue +1 -1
  34. package/components/dd-text/dd-text.vue +304 -82
  35. package/components/dd-waterfall/dd-waterfall.vue +89 -0
  36. package/index.ts +8 -6
  37. package/libs/function/index.d.ts +88 -0
  38. package/libs/function/index.js +14 -0
  39. package/libs/function/test.d.ts +65 -0
  40. package/package.json +1 -1
  41. package/scss/_mixins.scss +8 -0
  42. package/scss/_theme-tokens.scss +7 -0
  43. package/scss/_variables.scss +283 -156
  44. package/components/dd-album/album.js +0 -19
  45. package/components/dd-album/props.js +0 -89
  46. package/components/dd-avatar/avatar.js +0 -19
  47. package/components/dd-avatar/props.js +0 -84
  48. package/components/dd-avatar-group/avatarGroup.js +0 -14
  49. package/components/dd-avatar-group/props.js +0 -57
  50. package/components/dd-keyboard/keyboard.js +0 -19
  51. package/components/dd-keyboard/props.js +0 -79
  52. package/components/dd-link/link.js +0 -17
  53. package/components/dd-link/props.js +0 -44
  54. package/components/dd-number-keyboard/numberKeyboard.js +0 -8
  55. package/components/dd-number-keyboard/props.js +0 -24
  56. package/components/dd-parse/props.js +0 -57
  57. package/components/dd-text/props.js +0 -120
  58. package/components/dd-text/text.js +0 -30
  59. package/components/dd-text/value.js +0 -87
  60. package/scss/_dd-shared.scss +0 -40
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <view id="_root" :class="(selectable ? '_select ' : '') + '_root'" :style="containerStyle">
2
+ <view id="dd-parse-root" :class="'dd-parse__root' + (selectable ? ' dd-parse--select' : '')" :style="containerStyle">
3
3
  <slot v-if="!nodes[0]" />
4
4
  <!-- #ifndef APP-PLUS-NVUE -->
5
5
  <node v-else :childs="nodes" :opts="[lazyLoad, loadingImg, errorImg, showImgMenu, selectable]" name="span" />
@@ -10,7 +10,7 @@
10
10
  </view>
11
11
  </template>
12
12
 
13
- <script>
13
+ <script setup lang="ts">
14
14
  /**
15
15
  * mp-html v2.5.1
16
16
  * @description 富文本组件
@@ -38,487 +38,518 @@
38
38
  * @event {Function} error 媒体加载出错时触发
39
39
  */
40
40
  // #ifndef APP-PLUS-NVUE
41
+ // @ts-ignore node 以无扩展名方式导入 .vue 文件,vue-tsc 无法解析
41
42
  import node from './node/node'
42
43
  // #endif
44
+ // @ts-ignore parser.js 为 JS 实现且无类型声明,导入方式与原 Options API 一致
43
45
  import Parser from './parser'
44
- const plugins = []
46
+ import { getCurrentInstance, nextTick, onMounted, onUnmounted, ref, shallowRef, toRefs, watch } from 'vue'
47
+
48
+ // 插件注册表(与实例 plugins 同为空数组,原实现两处均未被填充)
49
+ const plugins: any[] = []
45
50
  // #ifdef APP-PLUS-NVUE
51
+ // @ts-ignore weex 为 nvue 运行时全局对象,@dcloudio/types 未声明
46
52
  const dom = weex.requireModule('dom')
47
53
  // #endif
48
- export default {
49
- name: 'dd-parse',
50
- data() {
51
- return {
52
- nodes: [],
53
- // #ifdef APP-PLUS-NVUE
54
- height: 3
55
- // #endif
56
- }
57
- },
58
- props: {
59
- containerStyle: {
60
- type: String,
61
- default: ''
62
- },
63
- content: {
64
- type: String,
65
- default: ''
66
- },
67
- copyLink: {
68
- type: [Boolean, String],
69
- default: true
70
- },
71
- domain: String,
72
- errorImg: {
73
- type: String,
74
- default: ''
75
- },
76
- lazyLoad: {
77
- type: [Boolean, String],
78
- default: false
79
- },
80
- loadingImg: {
81
- type: String,
82
- default: ''
83
- },
84
- pauseVideo: {
85
- type: [Boolean, String],
86
- default: true
87
- },
88
- previewImg: {
89
- type: [Boolean, String],
90
- default: true
91
- },
92
- scrollTable: [Boolean, String],
93
- selectable: [Boolean, String],
94
- setTitle: {
95
- type: [Boolean, String],
96
- default: true
97
- },
98
- showImgMenu: {
99
- type: [Boolean, String],
100
- default: true
101
- },
102
- tagStyle: Object,
103
- useAnchor: [Boolean, Number]
104
- },
105
- // #ifdef VUE3
106
- emits: ['load', 'ready', 'imgtap', 'linktap', 'play', 'error'],
54
+
55
+ // node 子组件依赖 $parent.$options.name === 'dd-parse' 定位根实例
56
+ defineOptions({ name: 'dd-parse' })
57
+
58
+ interface Props {
59
+ containerStyle?: string
60
+ content?: string
61
+ copyLink?: boolean | string
62
+ domain?: string
63
+ errorImg?: string
64
+ lazyLoad?: boolean | string
65
+ loadingImg?: string
66
+ pauseVideo?: boolean | string
67
+ previewImg?: boolean | string
68
+ scrollTable?: boolean | string
69
+ selectable?: boolean | string
70
+ setTitle?: boolean | string
71
+ showImgMenu?: boolean | string
72
+ tagStyle?: Record<string, any>
73
+ useAnchor?: boolean | number
74
+ }
75
+
76
+ const props = withDefaults(defineProps<Props>(), {
77
+ containerStyle: '',
78
+ content: '',
79
+ copyLink: true,
80
+ errorImg: '',
81
+ lazyLoad: false,
82
+ loadingImg: '',
83
+ pauseVideo: true,
84
+ previewImg: true,
85
+ setTitle: true,
86
+ showImgMenu: true,
87
+ })
88
+
89
+ const emit = defineEmits<{
90
+ (e: 'load'): void
91
+ (e: 'ready', rect: any): void
92
+ (e: 'imgtap', attrs: any): void
93
+ (e: 'linktap', attrs: any): void
94
+ (e: 'play', info?: any): void
95
+ (e: 'error', err: any): void
96
+ }>()
97
+
98
+ const instance = getCurrentInstance()!
99
+ // Parser 与 node 子组件都以组件实例作为上下文读写这些状态
100
+ const vm = instance.proxy as any
101
+
102
+ const nodes = ref<any[]>([])
103
+ // #ifdef APP-PLUS-NVUE
104
+ const height = ref(3)
105
+ // #endif
106
+ const web = ref<any>(null)
107
+ // imgList/_videos 原实现是非响应式的实例属性(不参与渲染),用 shallowRef 保持原始数组
108
+ const imgList = shallowRef<any>([])
109
+ // node 子组件通过 $parent 读写
110
+ const _videos = shallowRef<any[]>([])
111
+ const playbackRate = ref<number | undefined>()
112
+ let _in: any = null
113
+ let _navigateTo: any = null
114
+ let _ready = false
115
+
116
+ // Parser 还会读写 imgList/plugins/nodes;script setup 的绑定不会出现在实例代理上,
117
+ // 这里以实例代理为底补上这三个键(props 仍走代理,与原 Options API 行为一致)
118
+ const parserCtx: any = new Proxy(vm, {
119
+ get(target, key) {
120
+ if (key === 'imgList') return imgList.value
121
+ if (key === 'plugins') return plugins
122
+ if (key === 'nodes') return nodes.value
123
+ return target[key]
124
+ }
125
+ })
126
+
127
+ watch(
128
+ () => props.content,
129
+ (content) => {
130
+ setContent(content)
131
+ }
132
+ )
133
+
134
+ onMounted(() => {
135
+ if (props.content && !nodes.value.length) {
136
+ setContent(props.content)
137
+ }
138
+ })
139
+
140
+ onUnmounted(() => {
141
+ _hook('onDetached')
142
+ })
143
+
144
+ /**
145
+ * @description 规范化链接地址
146
+ * @param {any} href
147
+ * @return {String}
148
+ */
149
+ function normalizeHref(href: any) {
150
+ return typeof href === 'string' ? href.trim() : ''
151
+ }
152
+
153
+ /**
154
+ * @description 判断是否为外部链接
155
+ * @param {String} href
156
+ * @return {Boolean}
157
+ */
158
+ function isExternalLink(href: string) {
159
+ return href.split('?')[0].includes('://')
160
+ }
161
+
162
+ /**
163
+ * @description 打开外部链接
164
+ * @param {String} href
165
+ */
166
+ function openExternalLink(href: string) {
167
+ // #ifdef APP-PLUS
168
+ try {
169
+ plus.runtime.openWeb(href)
170
+ } catch (e) { }
107
171
  // #endif
108
- // #ifndef APP-PLUS-NVUE
109
- components: {
110
- node
111
- },
172
+ // #ifdef APP-HARMONY
173
+ try {
174
+ plus.runtime.openURL(href)
175
+ } catch (e) { }
112
176
  // #endif
113
- watch: {
114
- content(content) {
115
- this.setContent(content)
177
+ }
178
+
179
+ /**
180
+ * @description 将锚点跳转的范围限定在一个 scroll-view 内
181
+ * @param {Object} page scroll-view 所在页面的示例
182
+ * @param {String} selector scroll-view 的选择器
183
+ * @param {String} scrollTop scroll-view scroll-top 属性绑定的变量名
184
+ */
185
+ function anchorIn(page: any, selector: any, scrollTop: any) {
186
+ // #ifndef APP-PLUS-NVUE
187
+ if (page && selector && scrollTop) {
188
+ _in = {
189
+ page,
190
+ selector,
191
+ scrollTop
116
192
  }
117
- },
118
- created() {
119
- this.plugins = []
120
- for (let i = plugins.length; i--;) {
121
- this.plugins.push(new plugins[i](this))
193
+ }
194
+ // #endif
195
+ }
196
+
197
+ /**
198
+ * @description 锚点跳转
199
+ * @param {String} id 要跳转的锚点 id
200
+ * @param {Number} offset 跳转位置的偏移量
201
+ * @returns {Promise}
202
+ */
203
+ function navigateTo(id?: string, offset?: number) {
204
+ return new Promise<void>((resolve, reject) => {
205
+ if (!props.useAnchor) {
206
+ reject(Error('Anchor is disabled'))
207
+ return
122
208
  }
123
- },
124
- mounted() {
125
- if (this.content && !this.nodes.length) {
126
- this.setContent(this.content)
209
+ offset = offset || parseInt(props.useAnchor as any) || 0
210
+ // #ifdef APP-PLUS-NVUE
211
+ if (!id) {
212
+ dom.scrollToElement(web.value, {
213
+ offset
214
+ })
215
+ resolve()
216
+ } else {
217
+ _navigateTo = {
218
+ resolve,
219
+ reject,
220
+ offset
221
+ }
222
+ web.value.evalJs('uni.postMessage({data:{action:"getOffset",offset:(document.getElementById(' + id + ')||{}).offsetTop}})')
127
223
  }
128
- },
129
- beforeUnmount() {
130
- this._hook('onDetached')
131
- },
132
- methods: {
133
- /**
134
- * @description 规范化链接地址
135
- * @param {any} href
136
- * @return {String}
137
- */
138
- normalizeHref(href) {
139
- return typeof href === 'string' ? href.trim() : ''
140
- },
141
-
142
- /**
143
- * @description 判断是否为外部链接
144
- * @param {String} href
145
- * @return {Boolean}
146
- */
147
- isExternalLink(href) {
148
- return href.split('?')[0].includes('://')
149
- },
150
-
151
- /**
152
- * @description 打开外部链接
153
- * @param {String} href
154
- */
155
- openExternalLink(href) {
156
- // #ifdef APP-PLUS
157
- try {
158
- plus.runtime.openWeb(href)
159
- } catch (e) { }
160
- // #endif
161
- // #ifdef APP-HARMONY
162
- try {
163
- plus.runtime.openURL(href)
164
- } catch (e) { }
224
+ // #endif
225
+ // #ifndef APP-PLUS-NVUE
226
+ let deep = ' '
227
+ // #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO
228
+ deep = '>>>'
229
+ // #endif
230
+ const selector = uni.createSelectorQuery()
231
+ // #ifndef MP-ALIPAY
232
+ .in(_in ? _in.page : vm)
165
233
  // #endif
166
- },
167
-
168
- /**
169
- * @description 将锚点跳转的范围限定在一个 scroll-view 内
170
- * @param {Object} page scroll-view 所在页面的示例
171
- * @param {String} selector scroll-view 的选择器
172
- * @param {String} scrollTop scroll-view scroll-top 属性绑定的变量名
173
- */
174
- in(page, selector, scrollTop) {
175
- // #ifndef APP-PLUS-NVUE
176
- if (page && selector && scrollTop) {
177
- this._in = {
178
- page,
179
- selector,
180
- scrollTop
181
- }
234
+ .select((_in ? _in.selector : '.dd-parse__root') + (id ? `${deep}#${id}` : '')).boundingClientRect()
235
+ if (_in) {
236
+ // @ts-ignore scrollOffset 的回调在 @dcloudio/types 中声明为必填,运行时可选
237
+ selector.select(_in.selector).scrollOffset()
238
+ .select(_in.selector).boundingClientRect()
239
+ } else {
240
+ // 获取 scroll-view 的位置和滚动距离
241
+ // @ts-ignore 同上
242
+ selector.selectViewport().scrollOffset() // 获取窗口的滚动距离
243
+ }
244
+ selector.exec((res: any) => {
245
+ if (!res[0]) {
246
+ reject(Error('Label not found'))
247
+ return
182
248
  }
183
- // #endif
184
- },
249
+ const scrollTop = res[1].scrollTop + res[0].top - (res[2] ? res[2].top : 0) + (offset as number)
250
+ if (_in) {
251
+ // scroll-view 跳转
252
+ _in.page[_in.scrollTop] = scrollTop
253
+ } else {
254
+ // 页面跳转
255
+ uni.pageScrollTo({
256
+ scrollTop,
257
+ duration: 300
258
+ })
259
+ }
260
+ resolve()
261
+ })
262
+ // #endif
263
+ })
264
+ }
185
265
 
186
- /**
187
- * @description 锚点跳转
188
- * @param {String} id 要跳转的锚点 id
189
- * @param {Number} offset 跳转位置的偏移量
190
- * @returns {Promise}
191
- */
192
- navigateTo(id, offset) {
193
- return new Promise((resolve, reject) => {
194
- if (!this.useAnchor) {
195
- reject(Error('Anchor is disabled'))
196
- return
197
- }
198
- offset = offset || parseInt(this.useAnchor) || 0
199
- // #ifdef APP-PLUS-NVUE
200
- if (!id) {
201
- dom.scrollToElement(this.$refs.web, {
202
- offset
203
- })
204
- resolve()
205
- } else {
206
- this._navigateTo = {
207
- resolve,
208
- reject,
209
- offset
210
- }
211
- this.$refs.web.evalJs('uni.postMessage({data:{action:"getOffset",offset:(document.getElementById(' + id + ')||{}).offsetTop}})')
266
+ /**
267
+ * @description 获取文本内容
268
+ * @return {String}
269
+ */
270
+ function getText(nodeList?: any[]) {
271
+ let text = '';
272
+ (function traversal(nodes: any[]) {
273
+ for (let i = 0; i < nodes.length; i++) {
274
+ const node = nodes[i]
275
+ if (node.type === 'text') {
276
+ text += node.text.replace(/&amp;/g, '&')
277
+ } else if (node.name === 'br') {
278
+ text += '\n'
279
+ } else {
280
+ // 块级标签前后加换行
281
+ const isBlock = node.name === 'p' || node.name === 'div' || node.name === 'tr' || node.name === 'li' || (node.name[0] === 'h' && node.name[1] > '0' && node.name[1] < '7')
282
+ if (isBlock && text && text[text.length - 1] !== '\n') {
283
+ text += '\n'
212
284
  }
213
- // #endif
214
- // #ifndef APP-PLUS-NVUE
215
- let deep = ' '
216
- // #ifdef MP-WEIXIN || MP-QQ || MP-TOUTIAO
217
- deep = '>>>'
218
- // #endif
219
- const selector = uni.createSelectorQuery()
220
- // #ifndef MP-ALIPAY
221
- .in(this._in ? this._in.page : this)
222
- // #endif
223
- .select((this._in ? this._in.selector : '._root') + (id ? `${deep}#${id}` : '')).boundingClientRect()
224
- if (this._in) {
225
- selector.select(this._in.selector).scrollOffset()
226
- .select(this._in.selector).boundingClientRect()
227
- } else {
228
- // 获取 scroll-view 的位置和滚动距离
229
- selector.selectViewport().scrollOffset() // 获取窗口的滚动距离
285
+ // 递归获取子节点的文本
286
+ if (node.children) {
287
+ traversal(node.children)
230
288
  }
231
- selector.exec(res => {
232
- if (!res[0]) {
233
- reject(Error('Label not found'))
234
- return
235
- }
236
- const scrollTop = res[1].scrollTop + res[0].top - (res[2] ? res[2].top : 0) + offset
237
- if (this._in) {
238
- // scroll-view 跳转
239
- this._in.page[this._in.scrollTop] = scrollTop
240
- } else {
241
- // 页面跳转
242
- uni.pageScrollTo({
243
- scrollTop,
244
- duration: 300
245
- })
246
- }
247
- resolve()
248
- })
249
- // #endif
250
- })
251
- },
252
-
253
- /**
254
- * @description 获取文本内容
255
- * @return {String}
256
- */
257
- getText(nodes) {
258
- let text = '';
259
- (function traversal(nodes) {
260
- for (let i = 0; i < nodes.length; i++) {
261
- const node = nodes[i]
262
- if (node.type === 'text') {
263
- text += node.text.replace(/&amp;/g, '&')
264
- } else if (node.name === 'br') {
265
- text += '\n'
266
- } else {
267
- // 块级标签前后加换行
268
- const isBlock = node.name === 'p' || node.name === 'div' || node.name === 'tr' || node.name === 'li' || (node.name[0] === 'h' && node.name[1] > '0' && node.name[1] < '7')
269
- if (isBlock && text && text[text.length - 1] !== '\n') {
270
- text += '\n'
271
- }
272
- // 递归获取子节点的文本
273
- if (node.children) {
274
- traversal(node.children)
275
- }
276
- if (isBlock && text[text.length - 1] !== '\n') {
277
- text += '\n'
278
- } else if (node.name === 'td' || node.name === 'th') {
279
- text += '\t'
280
- }
281
- }
289
+ if (isBlock && text[text.length - 1] !== '\n') {
290
+ text += '\n'
291
+ } else if (node.name === 'td' || node.name === 'th') {
292
+ text += '\t'
282
293
  }
283
- })(nodes || this.nodes)
284
- return text
285
- },
286
-
287
- /**
288
- * @description 获取内容大小和位置
289
- * @return {Promise}
290
- */
291
- getRect() {
292
- return new Promise((resolve, reject) => {
293
- uni.createSelectorQuery()
294
- // #ifndef MP-ALIPAY
295
- .in(this)
296
- // #endif
297
- .select('#_root').boundingClientRect().exec(res => res[0] ? resolve(res[0]) : reject(Error('Root label not found')))
298
- })
299
- },
300
-
301
- /**
302
- * @description 暂停播放媒体
303
- */
304
- pauseMedia() {
305
- for (let i = (this._videos || []).length; i--;) {
306
- this._videos[i].pause()
307
294
  }
308
- // #ifdef APP-PLUS
309
- const command = 'for(var e=document.getElementsByTagName("video"),i=e.length;i--;)e[i].pause()'
310
- // #ifndef APP-PLUS-NVUE
311
- let page = this.$parent
312
- while (!page.$scope) page = page.$parent
313
- page.$scope.$getAppWebview().evalJS(command)
314
- // #endif
315
- // #ifdef APP-PLUS-NVUE
316
- this.$refs.web.evalJs(command)
317
- // #endif
318
- // #endif
319
- },
295
+ }
296
+ })(nodeList || nodes.value)
297
+ return text
298
+ }
320
299
 
321
- /**
322
- * @description 设置媒体播放速率
323
- * @param {Number} rate 播放速率
324
- */
325
- setPlaybackRate(rate) {
326
- this.playbackRate = rate
327
- for (let i = (this._videos || []).length; i--;) {
328
- this._videos[i].playbackRate(rate)
329
- }
330
- // #ifdef APP-PLUS
331
- const command = 'for(var e=document.getElementsByTagName("video"),i=e.length;i--;)e[i].playbackRate=' + rate
332
- // #ifndef APP-PLUS-NVUE
333
- let page = this.$parent
334
- while (!page.$scope) page = page.$parent
335
- page.$scope.$getAppWebview().evalJS(command)
336
- // #endif
337
- // #ifdef APP-PLUS-NVUE
338
- this.$refs.web.evalJs(command)
339
- // #endif
300
+ /**
301
+ * @description 获取内容大小和位置
302
+ * @return {Promise}
303
+ */
304
+ function getRect() {
305
+ return new Promise<any>((resolve, reject) => {
306
+ uni.createSelectorQuery()
307
+ // #ifndef MP-ALIPAY
308
+ .in(vm)
340
309
  // #endif
341
- },
310
+ .select('#dd-parse-root').boundingClientRect().exec((res: any) => res[0] ? resolve(res[0]) : reject(Error('Root label not found')))
311
+ })
312
+ }
342
313
 
343
- /**
344
- * @description 设置内容
345
- * @param {String} content html 内容
346
- * @param {Boolean} append 是否在尾部追加
347
- */
348
- setContent(content, append) {
349
- if (!append || !this.imgList) {
350
- this.imgList = []
351
- }
352
- const nodes = new Parser(this).parse(content)
353
- // #ifdef APP-PLUS-NVUE
354
- if (this._ready) {
355
- this._set(nodes, append)
356
- }
357
- // #endif
358
- this.$set(this, 'nodes', append ? (this.nodes || []).concat(nodes) : nodes)
314
+ /**
315
+ * @description 暂停播放媒体
316
+ */
317
+ function pauseMedia() {
318
+ for (let i = (_videos.value || []).length; i--;) {
319
+ _videos.value[i].pause()
320
+ }
321
+ // #ifdef APP-PLUS
322
+ const command = 'for(var e=document.getElementsByTagName("video"),i=e.length;i--;)e[i].pause()'
323
+ // #ifndef APP-PLUS-NVUE
324
+ let page: any = vm.$parent
325
+ while (!page.$scope) page = page.$parent
326
+ page.$scope.$getAppWebview().evalJS(command)
327
+ // #endif
328
+ // #ifdef APP-PLUS-NVUE
329
+ web.value.evalJs(command)
330
+ // #endif
331
+ // #endif
332
+ }
359
333
 
360
- // #ifndef APP-PLUS-NVUE
361
- this._videos = []
362
- this.$nextTick(() => {
363
- this._hook('onLoad')
364
- this.$emit('load')
365
- })
334
+ /**
335
+ * @description 设置媒体播放速率
336
+ * @param {Number} rate 播放速率
337
+ */
338
+ function setPlaybackRate(rate: number) {
339
+ playbackRate.value = rate
340
+ for (let i = (_videos.value || []).length; i--;) {
341
+ _videos.value[i].playbackRate(rate)
342
+ }
343
+ // #ifdef APP-PLUS
344
+ const command = 'for(var e=document.getElementsByTagName("video"),i=e.length;i--;)e[i].playbackRate=' + rate
345
+ // #ifndef APP-PLUS-NVUE
346
+ let page: any = vm.$parent
347
+ while (!page.$scope) page = page.$parent
348
+ page.$scope.$getAppWebview().evalJS(command)
349
+ // #endif
350
+ // #ifdef APP-PLUS-NVUE
351
+ web.value.evalJs(command)
352
+ // #endif
353
+ // #endif
354
+ }
366
355
 
367
- if (this.lazyLoad || this.imgList._unloadimgs < this.imgList.length / 2) {
368
- // 设置懒加载,每 350ms 获取高度,不变则认为加载完毕
369
- let height = 0
370
- const callback = rect => {
371
- if (!rect || !rect.height) rect = {}
372
- // 350ms 总高度无变化就触发 ready 事件
373
- if (rect.height === height) {
374
- this.$emit('ready', rect)
375
- } else {
376
- height = rect.height
377
- setTimeout(() => {
378
- this.getRect().then(callback).catch(callback)
379
- }, 350)
380
- }
381
- }
382
- this.getRect().then(callback).catch(callback)
356
+ /**
357
+ * @description 设置内容
358
+ * @param {String} content html 内容
359
+ * @param {Boolean} append 是否在尾部追加
360
+ */
361
+ function setContent(content: string, append?: boolean) {
362
+ if (!append || !imgList.value) {
363
+ imgList.value = []
364
+ }
365
+ const parsed = new Parser(parserCtx).parse(content)
366
+ // #ifdef APP-PLUS-NVUE
367
+ if (_ready) {
368
+ _set(parsed, append)
369
+ }
370
+ // #endif
371
+ nodes.value = append ? (nodes.value || []).concat(parsed) : parsed
372
+
373
+ // #ifndef APP-PLUS-NVUE
374
+ _videos.value = []
375
+ nextTick(() => {
376
+ _hook('onLoad')
377
+ emit('load')
378
+ })
379
+
380
+ if (props.lazyLoad || imgList.value._unloadimgs < imgList.value.length / 2) {
381
+ // 设置懒加载,每 350ms 获取高度,不变则认为加载完毕
382
+ let height = 0
383
+ const callback = (rect: any) => {
384
+ if (!rect || !rect.height) rect = {}
385
+ // 350ms 总高度无变化就触发 ready 事件
386
+ if (rect.height === height) {
387
+ emit('ready', rect)
383
388
  } else {
384
- // 未设置懒加载,等待所有图片加载完毕
385
- if (!this.imgList._unloadimgs) {
386
- this.getRect().then(rect => {
387
- this.$emit('ready', rect)
388
- }).catch(() => {
389
- this.$emit('ready', {})
390
- })
391
- }
389
+ height = rect.height
390
+ setTimeout(() => {
391
+ getRect().then(callback).catch(callback)
392
+ }, 350)
392
393
  }
393
- // #endif
394
- },
394
+ }
395
+ getRect().then(callback).catch(callback)
396
+ } else {
397
+ // 未设置懒加载,等待所有图片加载完毕
398
+ if (!imgList.value._unloadimgs) {
399
+ getRect().then((rect) => {
400
+ emit('ready', rect)
401
+ }).catch(() => {
402
+ emit('ready', {})
403
+ })
404
+ }
405
+ }
406
+ // #endif
407
+ }
395
408
 
396
- /**
397
- * @description 调用插件钩子函数
398
- */
399
- _hook(name) {
400
- for (let i = plugins.length; i--;) {
401
- if (this.plugins[i][name]) {
402
- this.plugins[i][name]()
403
- }
404
- }
405
- },
409
+ /**
410
+ * @description 调用插件钩子函数
411
+ */
412
+ function _hook(name: string) {
413
+ for (let i = plugins.length; i--;) {
414
+ if (plugins[i][name]) {
415
+ plugins[i][name]()
416
+ }
417
+ }
418
+ }
406
419
 
407
- // #ifdef APP-PLUS-NVUE
408
- /**
409
- * @description 设置内容
410
- */
411
- _set(nodes, append) {
412
- this.$refs.web.evalJs('setContent(' + JSON.stringify(nodes).replace(/%22/g, '') + ',' + JSON.stringify([this.containerStyle.replace(/(?:margin|padding)[^;]+/g, ''), this.errorImg, this.loadingImg, this.pauseVideo, this.scrollTable, this.selectable]) + ',' + append + ')')
413
- },
420
+ // #ifdef APP-PLUS-NVUE
421
+ /**
422
+ * @description 设置内容
423
+ */
424
+ function _set(nodesArg: any[], append?: boolean) {
425
+ web.value.evalJs('setContent(' + JSON.stringify(nodesArg).replace(/%22/g, '') + ',' + JSON.stringify([props.containerStyle.replace(/(?:margin|padding)[^;]+/g, ''), props.errorImg, props.loadingImg, props.pauseVideo, props.scrollTable, props.selectable]) + ',' + append + ')')
426
+ }
414
427
 
415
- /**
416
- * @description 接收到 web-view 消息
417
- */
418
- _onMessage(e) {
419
- const message = e.detail.data[0]
420
- switch (message.action) {
421
- // web-view 初始化完毕
422
- case 'onJSBridgeReady':
423
- this._ready = true
424
- if (this.nodes) {
425
- this._set(this.nodes)
426
- }
427
- break
428
- // 内容 dom 加载完毕
429
- case 'onLoad':
430
- this.height = message.height
431
- this._hook('onLoad')
432
- this.$emit('load')
433
- break
434
- // 所有图片加载完毕
435
- case 'onReady':
436
- this.getRect().then(res => {
437
- this.$emit('ready', res)
438
- }).catch(() => {
439
- this.$emit('ready', {})
440
- })
441
- break
442
- // 总高度发生变化
443
- case 'onHeightChange':
444
- this.height = message.height
445
- break
446
- // 图片点击
447
- case 'onImgTap':
448
- this.$emit('imgtap', message.attrs)
449
- if (this.previewImg) {
450
- uni.previewImage({
451
- current: parseInt(message.attrs.i),
452
- urls: this.imgList
428
+ /**
429
+ * @description 接收到 web-view 消息
430
+ */
431
+ function _onMessage(e: any) {
432
+ const message = e.detail.data[0]
433
+ switch (message.action) {
434
+ // web-view 初始化完毕
435
+ case 'onJSBridgeReady':
436
+ _ready = true
437
+ if (nodes.value) {
438
+ _set(nodes.value)
439
+ }
440
+ break
441
+ // 内容 dom 加载完毕
442
+ case 'onLoad':
443
+ height.value = message.height
444
+ _hook('onLoad')
445
+ emit('load')
446
+ break
447
+ // 所有图片加载完毕
448
+ case 'onReady':
449
+ getRect().then((res) => {
450
+ emit('ready', res)
451
+ }).catch(() => {
452
+ emit('ready', {})
453
+ })
454
+ break
455
+ // 总高度发生变化
456
+ case 'onHeightChange':
457
+ height.value = message.height
458
+ break
459
+ // 图片点击
460
+ case 'onImgTap':
461
+ emit('imgtap', message.attrs)
462
+ if (props.previewImg) {
463
+ uni.previewImage({
464
+ current: parseInt(message.attrs.i),
465
+ urls: imgList.value
466
+ })
467
+ }
468
+ break
469
+ // 链接点击
470
+ case 'onLinkTap': {
471
+ const href = normalizeHref(message.attrs.href)
472
+ emit('linktap', message.attrs)
473
+ if (href) {
474
+ // 锚点跳转
475
+ if (href[0] === '#') {
476
+ if (props.useAnchor) {
477
+ dom.scrollToElement(web.value, {
478
+ offset: message.offset
453
479
  })
454
480
  }
455
- break
456
- // 链接点击
457
- case 'onLinkTap': {
458
- const href = this.normalizeHref(message.attrs.href)
459
- this.$emit('linktap', message.attrs)
460
- if (href) {
461
- // 锚点跳转
462
- if (href[0] === '#') {
463
- if (this.useAnchor) {
464
- dom.scrollToElement(this.$refs.web, {
465
- offset: message.offset
466
- })
467
- }
468
- } else if (this.isExternalLink(href)) {
469
- // 打开外链
470
- if (this.copyLink) {
471
- this.openExternalLink(href)
472
- }
473
- } else {
474
- uni.navigateTo({
475
- url: href,
476
- fail() {
477
- uni.switchTab({
478
- url: href
479
- })
480
- }
481
+ } else if (isExternalLink(href)) {
482
+ // 打开外链
483
+ if (props.copyLink) {
484
+ openExternalLink(href)
485
+ }
486
+ } else {
487
+ uni.navigateTo({
488
+ url: href,
489
+ fail() {
490
+ uni.switchTab({
491
+ url: href
481
492
  })
482
493
  }
483
- }
484
- break
485
- }
486
- case 'onPlay':
487
- this.$emit('play')
488
- break
489
- // 获取到锚点的偏移量
490
- case 'getOffset':
491
- if (typeof message.offset === 'number') {
492
- dom.scrollToElement(this.$refs.web, {
493
- offset: message.offset + this._navigateTo.offset
494
- })
495
- this._navigateTo.resolve()
496
- } else {
497
- this._navigateTo.reject(Error('Label not found'))
498
- }
499
- break
500
- // 点击
501
- case 'onClick':
502
- this.$emit('tap')
503
- this.$emit('click')
504
- break
505
- // 出错
506
- case 'onError':
507
- this.$emit('error', {
508
- source: message.source,
509
- attrs: message.attrs
510
494
  })
495
+ }
511
496
  }
497
+ break
512
498
  }
513
- // #endif
499
+ case 'onPlay':
500
+ emit('play')
501
+ break
502
+ // 获取到锚点的偏移量
503
+ case 'getOffset':
504
+ if (typeof message.offset === 'number') {
505
+ dom.scrollToElement(web.value, {
506
+ offset: message.offset + _navigateTo.offset
507
+ })
508
+ _navigateTo.resolve()
509
+ } else {
510
+ _navigateTo.reject(Error('Label not found'))
511
+ }
512
+ break
513
+ // 点击
514
+ case 'onClick':
515
+ (emit as any)('tap')
516
+ (emit as any)('click')
517
+ break
518
+ // 出错
519
+ case 'onError':
520
+ emit('error', {
521
+ source: message.source,
522
+ attrs: message.attrs
523
+ })
514
524
  }
515
525
  }
526
+ // #endif
527
+
528
+ // node 子组件经 $parent(即本组件暴露对象)读写状态与 props,方法供外部 $refs 调用
529
+ defineExpose({
530
+ // props(node.vue 直接读取 pauseVideo/previewImg/showImgMenu/copyLink/lazyLoad 等)
531
+ ...toRefs(props),
532
+ in: anchorIn,
533
+ navigateTo,
534
+ getText,
535
+ getRect,
536
+ pauseMedia,
537
+ setPlaybackRate,
538
+ setContent,
539
+ normalizeHref,
540
+ isExternalLink,
541
+ openExternalLink,
542
+ nodes,
543
+ imgList,
544
+ _videos,
545
+ playbackRate
546
+ })
516
547
  </script>
517
548
 
518
549
  <style>
519
550
  /* #ifndef APP-PLUS-NVUE */
520
551
  /* 根节点样式 */
521
- ._root {
552
+ .dd-parse__root {
522
553
  padding: 1px 0;
523
554
  overflow-x: auto;
524
555
  overflow-y: hidden;
@@ -526,7 +557,7 @@ export default {
526
557
  }
527
558
 
528
559
  /* 长按复制 */
529
- ._select {
560
+ .dd-parse--select {
530
561
  user-select: text;
531
562
  }
532
563