Skip to content

Commit

Permalink
Merge pull request #55 from LuckyRainDay/master
Browse files Browse the repository at this point in the history
fix: 修复多页面时,新页面打开we-debug后,页面栈中其他页面we-debug被异常打开的问题
  • Loading branch information
dlhandsome committed Mar 26, 2024
2 parents 7161ac6 + 4c4f9eb commit 12f0dd1
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/runtime/miniprogram/src/mask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,80 @@ Component({
store.event.emit('debug:mask:hide-modal');
},
showMask() {
if (!this.isComponentInCurrentPage()) {
return;
}
this.setData({
showMask: true
});
},
closeMask() {
if (!this.isComponentInCurrentPage()) {
return;
}
setTimeout(() => {
this.setData({
showMask: false
});
}, animateDuration);
},
/** 当前组件是否在当前页面中 */
isComponentInCurrentPage() {
const currentPageUniqueId = this.getPageUniqueId(this.getCurrentPage());
let parent = this.selectOwnerComponent();
while (parent) {
if (currentPageUniqueId === this.getPageUniqueId(parent)) {
return true;
}
parent = parent?.selectOwnerComponent();
}
return false;
},
/** 获取当前页面实例 */
getCurrentPage() {
// 不支持getCurrentPages的,直接忽略上报
if (typeof getCurrentPages !== 'function') {
return null;
}

const pages = getCurrentPages();
if (pages.length === 0) {
return null;
}

return pages[pages.length - 1];
},
/** 获取当前页面的uniqueId */
getPageUniqueId(page) {
const defaultValue = '';

const getPageUniqueId = page => {
if (!page) {
return defaultValue;
}
if (typeof page.getPageId === 'function') {
return page.getPageId();
}
return page.__wxExparserNodeId__ || page.__wxWebviewId__ || page.route || page.__route__ || defaultValue;
};
// 如果传入page,则以page为准,如果未传入,则以当前页面为准
const uniqueId = getPageUniqueId(page);
if (uniqueId) {
return uniqueId;
}

// 不支持getCurrentPages的,返回默认值
if (typeof getCurrentPages !== 'function') {
return defaultValue;
}

const pages = getCurrentPages();
if (pages.length === 0) {
return defaultValue;
}

return getPageUniqueId(pages[pages.length - 1]);
},
addListeners() {
store.event.on('debug:mask:show-modal', this.showMask.bind(this));
store.event.on('debug:mask:hide-modal', this.closeMask.bind(this));
Expand Down

0 comments on commit 12f0dd1

Please sign in to comment.