5
5
DownSquareOutlined ,
6
6
UpSquareOutlined
7
7
} from "@ant-design/icons"
8
- import { FloatButton , message , Modal } from "antd"
8
+ import { Button , Flex , message , Modal } from "antd"
9
9
import antdResetCssText from "data-text:antd/dist/reset.css"
10
10
import type { PlasmoCSConfig , PlasmoGetShadowHostId } from "plasmo"
11
11
import { useEffect , useRef , useState } from "react"
@@ -46,6 +46,9 @@ export default function CustomOverlay() {
46
46
const [ codesDom , setCodesDom ] = useState ( [ ] )
47
47
const [ codes , setCodes ] = useStorage ( "app-codes" , [ ] )
48
48
const [ isCurrentDom , setIsCurrentDom ] = useState < boolean > ( false )
49
+ const [ rect , setRect ] = useState < any > ( ( ) => {
50
+ return { top : 0 , right : 0 }
51
+ } )
49
52
const [ messageApi , contextHolder ] = message . useMessage ( )
50
53
51
54
useEffect ( ( ) => {
@@ -156,7 +159,7 @@ export default function CustomOverlay() {
156
159
}
157
160
158
161
function getSelection ( ) {
159
- addCss ( `.codebox-current { border: 1px solid #7983ff; }` )
162
+ addCss ( `.codebox-current { border: 1px solid #7983ff!important ; }` )
160
163
document . addEventListener ( "mousemove" , ( event ) => {
161
164
const target = event . target as HTMLElement
162
165
if ( isReady && target && ! isSelect ) {
@@ -166,22 +169,30 @@ export default function CustomOverlay() {
166
169
} )
167
170
document . addEventListener ( "click" , ( event ) => {
168
171
const target = event . target as HTMLElement
169
- if ( isReady && target ) {
172
+ const tooltip = target . closest ( "#codebox-csui" )
173
+ if ( isReady && target && ! tooltip ) {
170
174
isSelect = true
171
175
setIsCurrentDom ( true )
172
176
removeCurrentDom ( )
173
177
target . classList . add ( "codebox-current" )
178
+ setTooltip ( )
174
179
event . stopPropagation ( )
175
180
event . preventDefault ( )
176
- setTimeout ( ( ) => {
177
- if ( confirm ( "是否下载?" ) ) {
178
- handleDownload ( )
179
- }
180
- } , 1000 )
181
181
}
182
182
} )
183
183
}
184
184
185
+ function setTooltip ( ) {
186
+ const currentDom = document . querySelector < HTMLElement > ( ".codebox-current" )
187
+ const rect = currentDom . getBoundingClientRect ( )
188
+ const distanceTop = rect . top + window . scrollY
189
+ const distanceLeft = rect . left + window . scrollX
190
+ const top = distanceTop < 50 ? distanceTop + 10 : distanceTop - 40
191
+ const left = distanceLeft + 10
192
+
193
+ setRect ( { top, left } )
194
+ }
195
+
185
196
function removeCurrentDom ( ) {
186
197
const currentList = document . querySelectorAll ( ".codebox-current" )
187
198
currentList . forEach ( ( el ) => {
@@ -240,6 +251,9 @@ export default function CustomOverlay() {
240
251
cancelText : "取消" ,
241
252
onOk : ( ) => {
242
253
handleDownload ( )
254
+ } ,
255
+ onCancel : ( ) => {
256
+ handleCancel ( )
243
257
}
244
258
} )
245
259
}
@@ -258,6 +272,7 @@ export default function CustomOverlay() {
258
272
if ( parent ) {
259
273
removeCurrentDom ( )
260
274
parent . classList . add ( "codebox-current" )
275
+ setTooltip ( )
261
276
}
262
277
event . stopPropagation ( )
263
278
}
@@ -268,6 +283,7 @@ export default function CustomOverlay() {
268
283
if ( child ) {
269
284
removeCurrentDom ( )
270
285
child . classList . add ( "codebox-current" )
286
+ setTooltip ( )
271
287
}
272
288
event . stopPropagation ( )
273
289
}
@@ -277,30 +293,48 @@ export default function CustomOverlay() {
277
293
{ contextHolder }
278
294
< StyleProvider container = { document . getElementById ( HOST_ID ) . shadowRoot } >
279
295
{ isCurrentDom ? (
280
- < FloatButton . Group
281
- shape = "square"
282
- style = { { insetBlockStart : 80 , insetBlockEnd : "auto" } } >
283
- < FloatButton
284
- icon = { < CheckOutlined /> }
285
- onClick = { handleConfirm }
286
- tooltip = "<div>确定</div>"
287
- />
288
- < FloatButton
289
- icon = { < UpSquareOutlined /> }
290
- onClick = { handleSetParent }
291
- tooltip = "父节点"
292
- />
293
- < FloatButton
294
- icon = { < DownSquareOutlined /> }
295
- onClick = { handleSetChild }
296
- tooltip = "子节点"
297
- />
298
- < FloatButton
299
- icon = { < CloseOutlined /> }
300
- onClick = { handleCancel }
301
- tooltip = "取消"
302
- />
303
- </ FloatButton . Group >
296
+ < div
297
+ className = "codebox-tooltip"
298
+ style = { {
299
+ position : "absolute" ,
300
+ top : `${ rect . top } px` ,
301
+ left : `${ rect . left } px` ,
302
+ width : "390px" ,
303
+ background : "#fff" ,
304
+ border : "1px solid #eee" ,
305
+ borderRadius : "5px"
306
+ } } >
307
+ < Flex wrap gap = "small" >
308
+ < Button
309
+ color = "primary"
310
+ variant = "text"
311
+ icon = { < CheckOutlined /> }
312
+ onClick = { handleConfirm } >
313
+ 确定
314
+ </ Button >
315
+ < Button
316
+ color = "default"
317
+ variant = "text"
318
+ icon = { < UpSquareOutlined /> }
319
+ onClick = { handleSetParent } >
320
+ 父节点
321
+ </ Button >
322
+ < Button
323
+ color = "default"
324
+ variant = "text"
325
+ icon = { < DownSquareOutlined /> }
326
+ onClick = { handleSetChild } >
327
+ 子节点
328
+ </ Button >
329
+ < Button
330
+ color = "danger"
331
+ variant = "text"
332
+ icon = { < CloseOutlined /> }
333
+ onClick = { handleCancel } >
334
+ 取消
335
+ </ Button >
336
+ </ Flex >
337
+ </ div >
304
338
) : (
305
339
< > </ >
306
340
) }
0 commit comments