Add FileBrowser API to BdApi (#842)
This commit is contained in:
parent
65dc310e03
commit
b123e57c17
|
@ -12,4 +12,5 @@ export const OPEN_WINDOW = "bd-open-window";
|
|||
export const INSPECT_ELEMENT = "bd-inspect-element";
|
||||
export const MINIMUM_SIZE = "bd-minimum-size";
|
||||
export const DEVTOOLS_WARNING = "bd-remove-devtools-message";
|
||||
export const REGISTER_PRELOAD = "bd-register-preload";
|
||||
export const OPEN_DIALOG = "bd-open-dialog";
|
||||
export const REGISTER_PRELOAD = "bd-register-preload";
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {ipcMain as ipc, BrowserWindow, app} from "electron";
|
||||
import {ipcMain as ipc, BrowserWindow, app, dialog} from "electron";
|
||||
|
||||
import * as IPCEvents from "common/constants/ipcevents";
|
||||
|
||||
|
@ -50,8 +50,8 @@ const runScript = async (event, script) => {
|
|||
const openDevTools = event => event.sender.openDevTools();
|
||||
const closeDevTools = event => event.sender.closeDevTools();
|
||||
|
||||
const createBrowserWindow = async (event, url, {windowOptions, closeOnUrl} = {}) => {
|
||||
return await new Promise(resolve => {
|
||||
const createBrowserWindow = (event, url, {windowOptions, closeOnUrl} = {}) => {
|
||||
return new Promise(resolve => {
|
||||
const windowInstance = new BrowserWindow(windowOptions);
|
||||
windowInstance.webContents.on("did-navigate", (_, navUrl) => {
|
||||
if (navUrl != closeOnUrl) return;
|
||||
|
@ -77,6 +77,47 @@ const setMinimumSize = (event, width, height) => {
|
|||
|
||||
const stopDevtoolsWarning = event => event.sender.removeAllListeners("devtools-opened");
|
||||
|
||||
const openDialog = (event, options = {}) => {
|
||||
const {
|
||||
mode = "open",
|
||||
openDirectory = false,
|
||||
openFile = true,
|
||||
multiSelections = false,
|
||||
filters,
|
||||
promptToCreate = false,
|
||||
defaultPath,
|
||||
title,
|
||||
showOverwriteConfirmation,
|
||||
message,
|
||||
showHiddenFiles,
|
||||
modal = false
|
||||
} = options;
|
||||
const openFunction = {
|
||||
open: dialog.showOpenDialog,
|
||||
save: dialog.showSaveDialog
|
||||
}[mode];
|
||||
if (!openFunction) return Promise.resolve({error: "Unkown Mode: " + mode});
|
||||
|
||||
return openFunction.apply(dialog, [
|
||||
modal && BrowserWindow.fromWebContents(event.sender),
|
||||
{
|
||||
defaultPath,
|
||||
filters,
|
||||
title,
|
||||
message,
|
||||
createDirectory: true,
|
||||
properties: [
|
||||
showHiddenFiles && "showHiddenFiles",
|
||||
openDirectory && "openDirectory",
|
||||
promptToCreate && "promptToCreate",
|
||||
openDirectory && "openDirectory",
|
||||
openFile && "openFile",
|
||||
multiSelections && "multiSelections",
|
||||
showOverwriteConfirmation && "showOverwriteConfirmation"
|
||||
].filter(e => e),
|
||||
}
|
||||
].filter(e => e));
|
||||
};
|
||||
const registerPreload = (event, path) => {
|
||||
app.commandLine.appendSwitch("preload", path);
|
||||
};
|
||||
|
@ -92,6 +133,7 @@ export default class IPCMain {
|
|||
ipc.on(IPCEvents.DEVTOOLS_WARNING, stopDevtoolsWarning);
|
||||
ipc.on(IPCEvents.REGISTER_PRELOAD, registerPreload);
|
||||
ipc.handle(IPCEvents.RUN_SCRIPT, runScript);
|
||||
ipc.handle(IPCEvents.OPEN_DIALOG, openDialog);
|
||||
ipc.handle(IPCEvents.OPEN_WINDOW, createBrowserWindow);
|
||||
}
|
||||
}
|
|
@ -43,4 +43,8 @@ export default new class IPCRenderer {
|
|||
stopDevtoolsWarning() {
|
||||
return ipc.send(IPCEvents.DEVTOOLS_WARNING);
|
||||
}
|
||||
|
||||
openDialog(options) {
|
||||
return ipc.invoke(IPCEvents.OPEN_DIALOG, options);
|
||||
}
|
||||
};
|
|
@ -12,6 +12,7 @@ import Settings from "./settingsmanager";
|
|||
import Logger from "common/logger";
|
||||
import Patcher from "./patcher";
|
||||
import Emotes from "../builtins/emotes/emotes";
|
||||
import ipc from "./ipc";
|
||||
|
||||
const BdApi = {
|
||||
get React() {return DiscordModules.React;},
|
||||
|
@ -265,6 +266,14 @@ BdApi.setBDData = function(key, data) {
|
|||
return DataStore.setBDData(key, data);
|
||||
};
|
||||
|
||||
// Opens a filesystem dialog
|
||||
BdApi.openDialog = async function (options) {
|
||||
const data = await ipc.openDialog(options);
|
||||
if (data.error) throw new Error(data.error);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
const makeAddonAPI = (manager) => new class AddonAPI {
|
||||
get folder() {return manager.addonFolder;}
|
||||
isEnabled(idOrFile) {return manager.isEnabled(idOrFile);}
|
||||
|
|
Loading…
Reference in New Issue