mini-program-parser
Advanced tools
@@ -7,3 +7,3 @@ Component({ | ||
| html: { type: String }, | ||
| config: { type: Object, value: { adaptive: true } }, | ||
| config: { type: Object, value: {} }, | ||
| }, | ||
@@ -17,35 +17,55 @@ data: { | ||
| ready() { | ||
| const { html, config } = this.data; | ||
| const { adaptive = true } = config; | ||
| if (html) { | ||
| if (adaptive) { | ||
| this.queryTimer = setInterval(() => { | ||
| this.getContainerWidth(); | ||
| }, 100); | ||
| } else { | ||
| this.setData({ | ||
| renderVisible: true, | ||
| }); | ||
| } | ||
| } | ||
| this.init(); | ||
| }, | ||
| observers: { | ||
| // 适应异步请求富文本数据 | ||
| html() { | ||
| this.init(); | ||
| }, | ||
| }, | ||
| methods: { | ||
| init() { | ||
| const { html, config } = this.data; | ||
| const { adaptive = true } = config; | ||
| if (html) { | ||
| if (adaptive) { | ||
| this.queryCount = 0; | ||
| this.queryTimer = setInterval(() => { | ||
| this.getContainerWidth(); | ||
| }, 100); | ||
| } else { | ||
| this.setData({ | ||
| renderVisible: true, | ||
| }); | ||
| } | ||
| } | ||
| }, | ||
| getContainerWidth() { | ||
| if (this.queryTimer || this.queryCount > 20) { | ||
| // 获取外层容器宽度 | ||
| wx.createSelectorQuery() | ||
| .in(this) | ||
| .select(".mini-parser") | ||
| .boundingClientRect((res) => { | ||
| if (res && res.width > 0) { | ||
| this.queryTimer = null; | ||
| this.setData({ | ||
| containerWidth: res.width, | ||
| renderVisible: true, | ||
| }); | ||
| } else { | ||
| this.queryCount += 1; | ||
| } | ||
| }) | ||
| .exec(); | ||
| if (this.queryTimer) { | ||
| if (this.queryCount < 10) { | ||
| // 获取外层容器宽度 | ||
| wx.createSelectorQuery() | ||
| .in(this) | ||
| .select('.mini-parser') | ||
| .boundingClientRect(res => { | ||
| if (res && res.width > 0) { | ||
| clearInterval(this.queryTimer); | ||
| this.queryTimer = null; | ||
| this.setData({ | ||
| containerWidth: res.width, | ||
| renderVisible: true, | ||
| }); | ||
| } else { | ||
| this.queryCount += 1; | ||
| } | ||
| }) | ||
| .exec(); | ||
| } else { | ||
| // 找不到外层元素时,将自适应设置为false | ||
| clearInterval(this.queryTimer); | ||
| this.setData({ | ||
| config: { ...this.data.config, adaptive: false }, | ||
| renderVisible: true, | ||
| }); | ||
| } | ||
| } | ||
@@ -52,0 +72,0 @@ }, |
+1
-1
| { | ||
| "name": "mini-program-parser", | ||
| "version": "0.3.5", | ||
| "version": "0.3.6", | ||
| "scripts": { | ||
@@ -5,0 +5,0 @@ "dev": "rollup -c ./rollup.dev.config.js --watch", |
55330
1.04%1051
1.94%