Skip to content

Commit e53db6c

Browse files
committed
优化自定义下载
1 parent 6c14f38 commit e53db6c

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

contents/custom.tsx

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
DownSquareOutlined,
66
UpSquareOutlined
77
} from "@ant-design/icons"
8-
import { FloatButton, message, Modal } from "antd"
8+
import { Button, Flex, message, Modal } from "antd"
99
import antdResetCssText from "data-text:antd/dist/reset.css"
1010
import type { PlasmoCSConfig, PlasmoGetShadowHostId } from "plasmo"
1111
import { useEffect, useRef, useState } from "react"
@@ -46,6 +46,9 @@ export default function CustomOverlay() {
4646
const [codesDom, setCodesDom] = useState([])
4747
const [codes, setCodes] = useStorage("app-codes", [])
4848
const [isCurrentDom, setIsCurrentDom] = useState<boolean>(false)
49+
const [rect, setRect] = useState<any>(() => {
50+
return { top: 0, right: 0 }
51+
})
4952
const [messageApi, contextHolder] = message.useMessage()
5053

5154
useEffect(() => {
@@ -156,7 +159,7 @@ export default function CustomOverlay() {
156159
}
157160

158161
function getSelection() {
159-
addCss(`.codebox-current { border: 1px solid #7983ff; }`)
162+
addCss(`.codebox-current { border: 1px solid #7983ff!important; }`)
160163
document.addEventListener("mousemove", (event) => {
161164
const target = event.target as HTMLElement
162165
if (isReady && target && !isSelect) {
@@ -166,22 +169,30 @@ export default function CustomOverlay() {
166169
})
167170
document.addEventListener("click", (event) => {
168171
const target = event.target as HTMLElement
169-
if (isReady && target) {
172+
const tooltip = target.closest("#codebox-csui")
173+
if (isReady && target && !tooltip) {
170174
isSelect = true
171175
setIsCurrentDom(true)
172176
removeCurrentDom()
173177
target.classList.add("codebox-current")
178+
setTooltip()
174179
event.stopPropagation()
175180
event.preventDefault()
176-
setTimeout(() => {
177-
if (confirm("是否下载?")) {
178-
handleDownload()
179-
}
180-
}, 1000)
181181
}
182182
})
183183
}
184184

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+
185196
function removeCurrentDom() {
186197
const currentList = document.querySelectorAll(".codebox-current")
187198
currentList.forEach((el) => {
@@ -240,6 +251,9 @@ export default function CustomOverlay() {
240251
cancelText: "取消",
241252
onOk: () => {
242253
handleDownload()
254+
},
255+
onCancel: () => {
256+
handleCancel()
243257
}
244258
})
245259
}
@@ -258,6 +272,7 @@ export default function CustomOverlay() {
258272
if (parent) {
259273
removeCurrentDom()
260274
parent.classList.add("codebox-current")
275+
setTooltip()
261276
}
262277
event.stopPropagation()
263278
}
@@ -268,6 +283,7 @@ export default function CustomOverlay() {
268283
if (child) {
269284
removeCurrentDom()
270285
child.classList.add("codebox-current")
286+
setTooltip()
271287
}
272288
event.stopPropagation()
273289
}
@@ -277,30 +293,48 @@ export default function CustomOverlay() {
277293
{contextHolder}
278294
<StyleProvider container={document.getElementById(HOST_ID).shadowRoot}>
279295
{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>
304338
) : (
305339
<></>
306340
)}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-box",
33
"displayName": "__MSG_extensionName__",
4-
"version": "0.7.3",
4+
"version": "0.7.5",
55
"description": "__MSG_extensionDescription__",
66
"author": "027xiguapi. <458813868@qq.com>",
77
"scripts": {
@@ -21,15 +21,15 @@
2121
"homepage": "https://github.com/027xiguapi/code-box#readme",
2222
"dependencies": {
2323
"@ant-design/cssinjs": "^1.21.1",
24-
"@ant-design/icons": "^5.4.0",
24+
"@ant-design/icons": "^5.5.1",
2525
"@dnd-kit/core": "^6.1.0",
2626
"@dnd-kit/sortable": "^8.0.0",
2727
"@dnd-kit/utilities": "^3.2.2",
2828
"@joplin/turndown": "^4.0.74",
2929
"@joplin/turndown-plugin-gfm": "^1.0.56",
3030
"@plasmohq/messaging": "^0.6.2",
3131
"@plasmohq/storage": "^1.11.0",
32-
"antd": "^5.19.4",
32+
"antd": "^5.21.1",
3333
"dayjs": "^1.11.12",
3434
"file-saver": "^2.0.5",
3535
"html2canvas": "^1.4.1",

0 commit comments

Comments
 (0)