Skip to content

Commit

Permalink
Merge pull request #113 from caorushizi/feat/dialog
Browse files Browse the repository at this point in the history
feat: ✨  show download dialog
  • Loading branch information
caorushizi authored Mar 16, 2024
2 parents 5a923fb + 1be42d2 commit 4486a25
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 49 deletions.
5 changes: 5 additions & 0 deletions packages/main/src/controller/DownloadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default class DownloadController implements Controller {
private readonly mainWindow: MainWindow,
) {}

@handle("show-download-dialog")
async showDownloadDialog(e: IpcMainEvent, data: DownloadItem) {
this.mainWindow.window?.webContents.send("show-download-dialog", data);
}

@handle("add-download-item")
async addDownloadItem(e: IpcMainEvent, video: DownloadItem) {
const item = await this.videoRepository.addVideo(video);
Expand Down
6 changes: 4 additions & 2 deletions packages/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const electronApi = {
webviewShow: (): Promise<void> => ipcRenderer.invoke("webview-show"),
webviewUrlContextMenu: (): Promise<void> =>
ipcRenderer.invoke("webview-url-contextmenu"),
downloadNow: (video: DownloadItem): Promise<void> =>
downloadNow: (video: Omit<DownloadItem, "id">): Promise<void> =>
ipcRenderer.invoke("download-now", video),
combineToHomePage: (store: BrowserStore): Promise<void> =>
ipcRenderer.invoke("combine-to-home-page", store),
Expand All @@ -87,10 +87,12 @@ const electronApi = {
ipcRenderer.invoke("set-shared-state", state),
setUserAgent: (isMobile: boolean): Promise<void> =>
ipcRenderer.invoke("webview-change-user-agent", isMobile),
downloadItem: (data: { name: string; url: string; type: string }) =>
downloadItem: (data: Omit<DownloadItem, "id">) =>
ipcRenderer.invoke("add-download-item", data),
getDownloadLog: (id: number): Promise<string> =>
ipcRenderer.invoke("get-download-log", id),
showDownloadDialog: (data: Omit<DownloadItem, "id">) =>
ipcRenderer.invoke("show-download-dialog", data),
};

contextBridge.exposeInMainWorld(apiKey, electronApi);
7 changes: 2 additions & 5 deletions packages/main/src/services/WebviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,14 @@ export default class WebviewService {
}

setBounds(bounds: Electron.Rectangle): void {
// if (isWin) {
// bounds.y = bounds.y + 30;
// } else {
// bounds.y = bounds.y - 0;
// }
this.view.setBounds(bounds);
}

async loadURL(url?: string, isNewWindow?: boolean) {
const canGoBack = this.webContents.canGoBack();

try {
this.webContents.stop();
await this.webContents.loadURL(url || "");
} catch (err: unknown) {
this.logger.error("加载 url 时出现错误: ", err);
Expand All @@ -165,6 +161,7 @@ export default class WebviewService {
}

async goHome() {
this.webContents.stop();
this.webContents.clearHistory();
}

Expand Down
7 changes: 5 additions & 2 deletions packages/main/src/windows/MainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ export default class MainWindow extends Window {
this.send("download-stop", id);
};

receiveMessage = async (id: number, message: any) => {
receiveMessage = async (id: number, message: string) => {
// 将日志写入数据库中
await this.videoRepository.appendDownloadLog(id, message);
this.send("download-message", id, message);
const showTerminal = this.store.get("showTerminal");
if (showTerminal) {
this.send("download-message", id, message);
}
};

send(channel: string, ...args: any[]) {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"dependencies": {
"jquery": "^3.7.1",
"lit": "^3.1.2",
"mitt": "^3.0.1",
"nanoid": "^5.0.4"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LitElement, html, css } from "lit";
import { customElement, property } from "lit/decorators.js";
import { BILIBILI_DOWNLOAD_BUTTON, downloadItem } from "../helper";
import { BILIBILI_DOWNLOAD_BUTTON, showDownloadDialog } from "../helper";
import $ from "jquery";

@customElement("one-button")
export class OneButton extends LitElement {
export class BilibiliButton extends LitElement {
@property({ type: Number })
index = 0;

Expand Down Expand Up @@ -39,7 +39,7 @@ export class OneButton extends LitElement {
const videoImage = $(BILIBILI_DOWNLOAD_BUTTON).eq(this.index);
const url = videoImage.attr("href") || "";
const name = videoImage.parent().find(".bili-video-card__info--tit").text();
downloadItem({ name, url, type: "bilibili" });
showDownloadDialog({ name, url, type: "bilibili" });
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/components/FloatButton.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LitElement, html, css } from "lit";
import { customElement, property } from "lit/decorators.js";
import logo from "../assets/logo.png";
import { addIpcListener, downloadItem } from "../helper";
import { addIpcListener, showDownloadDialog } from "../helper";

@customElement("float-button")
export class FloatButton extends LitElement {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class FloatButton extends LitElement {
e.preventDefault();
e.stopPropagation();

downloadItem({
showDownloadDialog({
name: this.data.name,
url: this.data.url,
type: this.data.type,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { OneButton } from "./OneButton";
export { BilibiliButton } from "./BilibiliButton";
export { FloatButton } from "./FloatButton";
3 changes: 0 additions & 3 deletions packages/plugin/src/helper/events.ts

This file was deleted.

8 changes: 5 additions & 3 deletions packages/plugin/src/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { nanoid } from "nanoid";

export { emitter } from "./events";

const eventMap = new Map();

const getIpcId = (func: any) => {
Expand All @@ -28,11 +26,15 @@ export function removeIpcListener(eventName: string, func: any) {
export interface Item {
name: string;
url: string;
type: string;
type: any;
}

export function downloadItem(item: Item) {
window.electron.downloadItem(item);
}

export function showDownloadDialog(item: Item) {
window.electron.showDownloadDialog(item);
}

export const BILIBILI_DOWNLOAD_BUTTON = ".bili-video-card__image--link";
2 changes: 1 addition & 1 deletion packages/plugin/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function bilibili() {
const isAd = $(card).find(".bili-video-card__info--ad");
if (isAd.length) return;

const downloadButton = document.createElement("one-button");
const downloadButton = document.createElement("bilibili-button");
downloadButton.index = index;
card.appendChild(downloadButton);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OneButton } from "./components";
import { BilibiliButton } from "./components";
import { ElectronApi } from "../../main/types/preload";

export enum DownloadType {
Expand All @@ -15,7 +15,7 @@ export interface WebSource {

declare global {
interface HTMLElementTagNameMap {
"one-button": OneButton;
"bilibili-button": BilibiliButton;
}
interface Window {
electron: ElectronApi;
Expand Down
1 change: 1 addition & 0 deletions packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dayjs": "^1.11.10",
"i18next": "^23.8.2",
"i18next-browser-languagedetector": "^7.2.0",
"localforage": "^1.10.0",
"match-sorter": "^6.3.3",
"nanoid": "^5.0.4",
"react": "^18.2.0",
Expand Down
18 changes: 16 additions & 2 deletions packages/renderer/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ i18n
light: "Light",
pleaseSelectTheme: "Please select theme",
displayLanguage: "Display Language",
chinese: "Chinese",
chinese: "中文",
english: "English",
pleaseSelectLanguage: "Please select language",
downloadPrompt: "Download Prompt",
Expand Down Expand Up @@ -126,6 +126,13 @@ Referer: http://www.example.com`,
exportLog: "Export Log",
showTerminal: "Show Console",
consoleOutput: "Console",
downloadNow: "Download Now",
addToDownloadList: "Add to Download List",
videoType: "Video Type",
pleaseSelectVideoType: "Please select video type",
numberOfEpisodes: "Number of episodes",
showNumberOfEpisodes: "Show number of episodes",
canUseMouseWheelToAdjust: "Can use mouse wheel to adjust",
},
},
zh: {
Expand Down Expand Up @@ -182,7 +189,7 @@ Referer: http://www.example.com`,
pleaseSelectDownloadDir: "请选择视频下载目录",
downloaderTheme: "下载器主题",
followSystem: "跟随系统",
english: "英文",
english: "English",
pleaseSelectLanguage: "请选择显示语言",
downloadPrompt: "下载完成提示",
browserSetting: "浏览器设置",
Expand Down Expand Up @@ -239,6 +246,13 @@ Referer: http://www.example.com`,
exportLog: "导出日志",
showTerminal: "显示控制台",
consoleOutput: "控制台",
downloadNow: "立即下载",
addToDownloadList: "添加到下载列表",
videoType: "视频类型",
pleaseSelectVideoType: "请选择视频类型",
numberOfEpisodes: "集数",
showNumberOfEpisodes: "显示集数",
canUseMouseWheelToAdjust: "可以使用鼠标滚轮调整",
},
},
},
Expand Down
Loading

0 comments on commit 4486a25

Please sign in to comment.