Skip to content

Commit 346fd1c

Browse files
perf: Set the DOM layer of the floating assistant to the highest level
1 parent a1a1870 commit 346fd1c

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

frontend/public/assistant.js

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,52 @@
9696
</div>
9797
`
9898
}
99+
100+
function getHighestZIndexValue() {
101+
try {
102+
let maxZIndex = -Infinity
103+
let foundAny = false
104+
105+
const allElements = document.all || document.querySelectorAll('*')
106+
107+
for (let i = 0; i < allElements.length; i++) {
108+
const element = allElements[i]
109+
110+
if (!element || element.nodeType !== 1) continue
111+
112+
const styles = window.getComputedStyle(element)
113+
114+
const position = styles.position
115+
if (position === 'static') continue
116+
117+
const zIndex = styles.zIndex
118+
let zIndexValue
119+
120+
if (zIndex === 'auto') {
121+
zIndexValue = 0
122+
} else {
123+
zIndexValue = parseInt(zIndex, 10)
124+
if (isNaN(zIndexValue)) continue
125+
}
126+
127+
foundAny = true
128+
129+
// 快速返回:如果找到很大的z-index,很可能就是最大值
130+
/* if (zIndexValue > 10000) {
131+
return zIndexValue;
132+
} */
133+
134+
if (zIndexValue > maxZIndex) {
135+
maxZIndex = zIndexValue
136+
}
137+
}
138+
return foundAny ? maxZIndex : 0
139+
} catch (error) {
140+
console.warn('获取最高z-index时出错,返回默认值0:', error)
141+
return 0
142+
}
143+
}
144+
99145
/**
100146
* 初始化引导
101147
* @param {*} root
@@ -256,6 +302,9 @@
256302

257303
// 初始化全局样式
258304
function initsqlbot_assistantStyle(root, sqlbot_assistantId, data) {
305+
const maxZIndex = getHighestZIndexValue()
306+
const zIndex = Math.max((maxZIndex || 0) + 1, 10000)
307+
const maskZIndex = zIndex + 1
259308
style = document.createElement('style')
260309
style.type = 'text/css'
261310
style.innerText = `
@@ -279,7 +328,7 @@
279328
280329
#sqlbot-assistant .sqlbot-assistant-mask {
281330
position: fixed;
282-
z-index: 10001;
331+
z-index: ${maskZIndex};
283332
background-color: transparent;
284333
height: 100%;
285334
width: 100%;
@@ -293,7 +342,7 @@
293342
position: absolute;
294343
${data.x_type}: ${data.x_val}px;
295344
${data.y_type}: ${data.y_val}px;
296-
z-index: 10001;
345+
z-index: ${maskZIndex};
297346
}
298347
#sqlbot-assistant .sqlbot-assistant-tips {
299348
position: fixed;
@@ -304,7 +353,7 @@
304353
color: #ffffff;
305354
font-size: 14px;
306355
background: #3370FF;
307-
z-index: 10001;
356+
z-index: ${maskZIndex};
308357
}
309358
#sqlbot-assistant .sqlbot-assistant-tips .sqlbot-assistant-arrow {
310359
position: absolute;
@@ -366,10 +415,11 @@
366415
${data.x_type}: ${data.x_val}px;
367416
${data.y_type}: ${data.y_val}px;
368417
cursor: pointer;
369-
z-index:10000;
418+
z-index: ${zIndex};
370419
}
371420
#sqlbot-assistant #sqlbot-assistant-chat-container{
372-
z-index:10000;position: relative;
421+
z-index: ${zIndex};
422+
position: relative;
373423
border-radius: 8px;
374424
//border: 1px solid #ffffff;
375425
background: linear-gradient(188deg, rgba(235, 241, 255, 0.20) 39.6%, rgba(231, 249, 255, 0.20) 94.3%), #EFF0F1;

0 commit comments

Comments
 (0)