Monaco for Discord Desktop

This commit is contained in:
Vendicated 2022-10-22 04:41:33 +02:00
parent 23d4cae123
commit 44f6f71c3e
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
11 changed files with 173 additions and 90 deletions

View file

@ -16,80 +16,26 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { IpcEvents } from "../utils";
import { debounce } from "../utils/debounce";
import { find } from "../webpack/webpack";
import monacoHtml from "@fileContent/monacoWin.html";
import { Queue } from "../utils/Queue";
const queue = new Queue();
const setCss = debounce((css: string) => {
VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, css);
queue.add(() => VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, css));
});
// FIXME: Discord Desktop support.
// open() fails to create the popup and returns null. Probably have to
// do some logic in main
// adapted from https://stackoverflow.com/a/63179814
export async function launchMonacoEditor() {
const win = open("about:blank", void 0, "popup,width=1000,height=1000")!;
win.setCss = setCss;
win.getCurrentCss = () => VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS);
win.callback = (editor: any) => {
editor.onDidChangeModelContent(() =>
setCss(editor.getValue())
);
};
win.getTheme = () => find(m => m.ProtoClass?.typeName.endsWith("PreloadedUserSettings"))
.getCurrentValue().appearance.theme === 1
? "vs-dark"
: "vs-light";
let { theme } = find(m => m.ProtoClass?.typeName.endsWith("PreloadedUserSettings"))
.getCurrentValue().appearance;
theme = theme === 1 ? "vs-dark" : "vs-light";
// problem?
win.document.write(`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>QuickCss Editor</title>
<link rel="stylesheet" data-name="vs/editor/editor.main" href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs/editor/editor.main.min.css">
<style>
html, body, #container {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs/loader.min.js"></script>
<script>
var editor;
require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs' }});
require(["vs/editor/editor.main"], () => {
getCurrentCss().then(css => {
callback(editor = monaco.editor.create(document.getElementById('container'), {
value: css,
language: 'css',
theme: '${theme}',
}));
});
});
window.addEventListener("resize", () => {
// make monaco re-layout
editor.layout();
});
</script>
</body>
</html>
`);
win.document.write(monacoHtml);
}

View file

@ -77,7 +77,7 @@ export default ErrorBoundary.wrap(function Settings() {
Launch Directory
</Button>
<Button
onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_QUICKCSS)}
onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_MONACO_EDITOR)}
size={Button.Sizes.SMALL}
disabled={settingsDir === "Loading..."}
>

View file

@ -0,0 +1,52 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>QuickCss Editor</title>
<link rel="stylesheet" data-name="vs/editor/editor.main"
href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs/editor/editor.main.min.css">
<style>
html,
body,
#container {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs/loader.min.js"></script>
<script>
require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs' } });
require(["vs/editor/editor.main"], () => {
getCurrentCss().then(css => {
var editor = monaco.editor.create(document.getElementById('container'), {
value: css,
language: 'css',
theme: getTheme(),
});
editor.onDidChangeModelContent(() =>
setCss(editor.getValue())
);
window.addEventListener("resize", () => {
// make monaco re-layout
editor.layout();
});
});
});
</script>
</body>
</html>

View file

@ -22,8 +22,10 @@ import { open, readFile, writeFile } from "fs/promises";
import { join } from "path";
import { debounce } from "../utils/debounce";
import IpcEvents from "../utils/IpcEvents";
import monacoHtml from "@fileContent/../components/monacoWin.html;base64";
import "./updater";
import { Queue } from "../utils/Queue";
const DATA_DIR = process.env.VENCORD_USER_DATA_DIR ?? (
process.env.DISCORD_USER_DATA_DIR
@ -71,15 +73,19 @@ ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => {
shell.openExternal(url);
});
const cssWriteQueue = new Queue();
const settingsWriteQueue = new Queue();
ipcMain.handle(IpcEvents.GET_QUICK_CSS, () => readCss());
ipcMain.handle(IpcEvents.SET_QUICK_CSS, (_, css) =>
cssWriteQueue.add(() => writeFile(QUICKCSS_PATH, css))
);
ipcMain.handle(IpcEvents.GET_SETTINGS_DIR, () => SETTINGS_DIR);
ipcMain.on(IpcEvents.GET_SETTINGS, e => e.returnValue = readSettings());
let settingsWriteQueue = Promise.resolve();
ipcMain.handle(IpcEvents.SET_SETTINGS, (_, s) => {
settingsWriteQueue = settingsWriteQueue.then(() => writeFile(SETTINGS_FILE, s));
settingsWriteQueue.add(() => writeFile(SETTINGS_FILE, s));
});
@ -91,3 +97,13 @@ export function initIpc(mainWindow: BrowserWindow) {
}, 50));
});
}
ipcMain.handle(IpcEvents.OPEN_MONACO_EDITOR, async () => {
const win = new BrowserWindow({
title: "QuickCss Editor",
webPreferences: {
preload: join(__dirname, "preload.js"),
}
});
await win.loadURL(`data:text/html;base64,${monacoHtml}`);
});

5
src/modules.d.ts vendored
View file

@ -28,3 +28,8 @@ declare module "git-hash" {
const hash: string;
export default hash;
}
declare module "@fileContent/*" {
const content: string;
export default content;
}

View file

@ -21,6 +21,7 @@ import { readFileSync } from "fs";
import { join } from "path";
import VencordNative from "./VencordNative";
import IpcEvents from "./utils/IpcEvents";
import { debounce } from "./utils/debounce";
if (electron.desktopCapturer === void 0) {
// Fix for desktopCapturer being main only in Electron 17+
@ -39,6 +40,14 @@ if (electron.desktopCapturer === void 0) {
contextBridge.exposeInMainWorld("VencordNative", VencordNative);
webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8"));
require(process.env.DISCORD_PRELOAD!);
if (location.protocol !== "data:") {
// Discord
webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8"));
require(process.env.DISCORD_PRELOAD!);
} else {
// Monaco Popout
contextBridge.exposeInMainWorld("setCss", debounce(s => VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, s)));
contextBridge.exposeInMainWorld("getCurrentCss", () => VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS));
// shrug
contextBridge.exposeInMainWorld("getTheme", () => "vs-dark");
}

View file

@ -43,5 +43,6 @@ export default strEnum({
GET_HASHES: "VencordGetHashes",
UPDATE: "VencordUpdate",
BUILD: "VencordBuild",
GET_DESKTOP_CAPTURE_SOURCES: "VencordGetDesktopCaptureSources"
GET_DESKTOP_CAPTURE_SOURCES: "VencordGetDesktopCaptureSources",
OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor"
} as const);

27
src/utils/Queue.ts Normal file
View file

@ -0,0 +1,27 @@
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Promisable } from "type-fest";
export class Queue {
private promise = Promise.resolve();
add(func: () => Promisable<void>) {
this.promise = this.promise.then(func);
}
}