Skip to content

The only downloader you need. 下载器的集大成者。

feat(download-button): 添加下载按钮拖拽和关闭功能

XiaoYouChRea87fdf7ce22
diff --git a/browser_extension/app/src/page-media/download-button/download-button.ts b/browser_extension/app/src/page-media/download-button/download-button.ts
index 68d6bf3c..42b84df3 100644
--- a/browser_extension/app/src/page-media/download-button/download-button.ts
+++ b/browser_extension/app/src/page-media/download-button/download-button.ts
@@ -32,6 +32,10 @@ const FADE_DURATION_MS = 300;
let mouseMoveQueued = false;
let lastMouseX = -1;
let lastMouseY = -1;
+ let dismissed = false;
+ let dragOffsetX = 0;
+ let dragOffsetY = 0;
+ const DRAG_THRESHOLD = 5;
host.id = "ghostDownloaderMediaDownload";
Object.assign(host.style, {
@@ -49,19 +53,24 @@ const FADE_DURATION_MS = 300;
:host {
font: 13px/18px "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}
+ .wrapper {
+ display: flex;
+ align-items: stretch;
+ border-radius: 4px;
+ box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18), 0 1px 4px rgba(0, 0, 0, 0.12);
+ }
button {
align-items: center;
background: rgba(255, 255, 255, 0.96);
border: 1px solid #d1d1d1;
- border-radius: 4px;
- box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18), 0 1px 4px rgba(0, 0, 0, 0.12);
+ border-right: none;
+ border-radius: 4px 0 0 4px;
color: #242424;
cursor: pointer;
- display: inline-flex;
+ display: flex;
font: inherit;
font-weight: 600;
gap: 6px;
- min-height: 32px;
padding: 6px 10px;
white-space: nowrap;
}
@@ -86,22 +95,45 @@ const FADE_DURATION_MS = 300;
.error {
color: #b10e1c;
}
+ .close {
+ align-items: center;
+ background: rgba(255, 255, 255, 0.96);
+ border: 1px solid #d1d1d1;
+ border-left: none;
+ border-radius: 0 4px 4px 0;
+ color: #616161;
+ cursor: pointer;
+ display: flex;
+ font: inherit;
+ justify-content: center;
+ padding: 0 6px;
+ }
+ .close:hover {
+ background: #f5f5f5;
+ color: #242424;
+ }
+ .close:active {
+ background: #e0e0e0;
+ }
</style>
- <button type="button" title="${chrome.i18n.getMessage("sendCurrentMediaToGhostDownloader")}">
- <svg class="icon" viewBox="0 0 20 20" aria-hidden="true">
- <path fill="currentColor" d="M10 2.5a.75.75 0 0 1 .75.75v7.69l2.72-2.72a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 1.06-1.06l2.72 2.72V3.25A.75.75 0 0 1 10 2.5Zm-5.25 11a.75.75 0 0 1 .75.75v1.25h9v-1.25a.75.75 0 0 1 1.5 0v2a.75.75 0 0 1-.75.75H4.75A.75.75 0 0 1 4 16.25v-2a.75.75 0 0 1 .75-.75Z"/>
- </svg>
- <span class="label">${chrome.i18n.getMessage("downloadThisMedia")}</span>
- <span class="status"></span>
- </button>
+ <div class="wrapper">
+ <button type="button" title="${chrome.i18n.getMessage("sendCurrentMediaToGhostDownloader")}">
+ <svg class="icon" viewBox="0 0 20 20" aria-hidden="true">
+ <path fill="currentColor" d="M10 2.5a.75.75 0 0 1 .75.75v7.69l2.72-2.72a.75.75 0 1 1 1.06 1.06l-4 4a.75.75 0 0 1-1.06 0l-4-4a.75.75 0 0 1 1.06-1.06l2.72 2.72V3.25A.75.75 0 0 1 10 2.5Zm-5.25 11a.75.75 0 0 1 .75.75v1.25h9v-1.25a.75.75 0 0 1 1.5 0v2a.75.75 0 0 1-.75.75H4.75A.75.75 0 0 1 4 16.25v-2a.75.75 0 0 1 .75-.75Z"/>
+ </svg>
+ <span class="label">${chrome.i18n.getMessage("downloadThisMedia")}</span>
+ <span class="status"></span>
+ </button><span class="close" title="${chrome.i18n.getMessage("cancel")}">✕</span>
+ </div>
`;
const button = root.querySelector("button")!;
const label = root.querySelector(".label")!;
const status = root.querySelector(".status")!;
+ const closeBtn = root.querySelector(".close")!;
function showButton(): void {
- if (isVisible) { return; }
+ if (isVisible || dismissed) { return; }
isVisible = true;
host.style.display = "block";
void host.offsetHeight;
@@ -183,12 +215,15 @@ const FADE_DURATION_MS = 300;
}
}
+ left += dragOffsetX;
+ top += dragOffsetY;
host.style.left = `${Math.max(gap, Math.min(left, innerWidth - buttonWidth - gap))}px`;
host.style.top = `${Math.max(gap, Math.min(top, innerHeight - buttonHeight - gap))}px`;
}
function enableOverlay(): void {
enabled = true;
+ dismissed = false;
if (document.documentElement && !host.isConnected) {
document.documentElement.appendChild(host);
}
@@ -292,9 +327,62 @@ const FADE_DURATION_MS = 300;
}
}
+ closeBtn.addEventListener("click", (event) => {
+ event.preventDefault();
+ event.stopPropagation();
+ dismissed = true;
+ hideButton();
+ });
+
+ let dragStartX = 0;
+ let dragStartY = 0;
+ let dragStartLeft = 0;
+ let dragStartTop = 0;
+ let isDragging = false;
+ let didDrag = false;
+
+ function onDragMove(event: MouseEvent): void {
+ const dx = event.clientX - dragStartX;
+ const dy = event.clientY - dragStartY;
+ if (!isDragging) {
+ if (Math.abs(dx) < DRAG_THRESHOLD && Math.abs(dy) < DRAG_THRESHOLD) { return; }
+ isDragging = true;
+ host.style.transition = "none";
+ }
+ host.style.left = `${dragStartLeft + dx}px`;
+ host.style.top = `${dragStartTop + dy}px`;
+ }
+
+ function onDragEnd(event: MouseEvent): void {
+ document.removeEventListener("mousemove", onDragMove);
+ document.removeEventListener("mouseup", onDragEnd);
+ if (isDragging) {
+ dragOffsetX += event.clientX - dragStartX;
+ dragOffsetY += event.clientY - dragStartY;
+ host.style.transition = `opacity ${FADE_DURATION_MS}ms ease`;
+ didDrag = true;
+ isDragging = false;
+ scheduleUpdate();
+ }
+ }
+
+ host.addEventListener("mousedown", (event) => {
+ if ((event.composedPath()[0] as Element)?.closest?.(".close")) { return; }
+ event.preventDefault();
+ dragStartX = event.clientX;
+ dragStartY = event.clientY;
+ dragStartLeft = parseFloat(host.style.left) || 0;
+ dragStartTop = parseFloat(host.style.top) || 0;
+ isDragging = false;
+ didDrag = false;
+ document.addEventListener("mousemove", onDragMove);
+ document.addEventListener("mouseup", onDragEnd);
+ });
+
button.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
+ if (didDrag) { didDrag = false; return; }
void downloadMedia();
});