From 69444f9aa63311a90c5f89338a8bd2d52c6db7d4 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Mon, 14 Dec 2020 17:21:23 -0500 Subject: [PATCH] reorganized js --- index.html | 58 +-------------------------------------------------- main.js | 44 +++++++++++++++++---------------------- renderer.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 82 deletions(-) create mode 100644 renderer.js diff --git a/index.html b/index.html index 456f54f..d618d74 100644 --- a/index.html +++ b/index.html @@ -51,63 +51,7 @@ body {
- + diff --git a/main.js b/main.js index d91934d..e32e527 100644 --- a/main.js +++ b/main.js @@ -1,47 +1,41 @@ -const { app, BrowserWindow } = require('electron'); -const shortcut = require('electron-localshortcut'); -const path = require('path'); +const { app, BrowserWindow } = require("electron"); +const shortcut = require("electron-localshortcut"); +const path = require("path"); +// initialize renderer let win; -const createWindow = () => { +function createWindow() { win = new BrowserWindow({ width: 800, height: 600, webPreferences: { contextIsolation: true, nodeIntegration: false, - preload: path.join(__dirname, 'preload.js') + preload: path.join(__dirname, "preload.js") } }); - - win.loadFile(path.join(__dirname, 'index.html')); - - shortcut.register(win, 'CmdOrCtrl+S', () => { - console.log("[main] triggering 'request-render-markdown'...") - win.webContents.send('request-render-markdown', ''); + win.loadFile(path.join(__dirname, "index.html")); + shortcut.register(win, "CmdOrCtrl+S", () => { + console.log("[main] triggering 'request-focus-editor'..."); + win.webContents.send("request-focus-editor", ""); }); - shortcut.register(win, 'CmdOrCtrl+E', () => { - console.log("[main] triggering 'request-focus-editor'...") - win.webContents.send('request-focus-editor', ''); + shortcut.register(win, "CmdOrCtrl+E", () => { + console.log("[main] triggering 'request-render-markdown'..."); + win.webContents.send("request-render-markdown", ""); }); - - win.on('closed', () => { + win.on("closed", () => { win = null; }); }; +app.on("ready", createWindow); -// initialize -app.on('ready', createWindow); - -// macOS convention is to keep process alive (accessible via the dock) until -// explicitly quit; must handle re-initialization -app.on('window-all-closed', () => { - if (process.platform !== 'darwin') { +// macOS best practices +app.on("window-all-closed", () => { + if (process.platform !== "darwin") { app.quit(); } }); - -app.on('activate', () => { +app.on("activate", () => { if (win === null) { createWindow(); } diff --git a/renderer.js b/renderer.js new file mode 100644 index 0000000..7ebe5e4 --- /dev/null +++ b/renderer.js @@ -0,0 +1,60 @@ +// initialize monaco editor +require.config({ paths: { vs: 'node_modules/monaco-editor/min/vs' } }); +require(['vs/editor/editor.main'], function () { + window.editor = monaco.editor.create(document.getElementById('container-editor'), { + language: 'markdown', + minimap: { + enabled: false + }, + value: '' + }); +}); + +// messaging +function postMarkdown(markdown) { + console.log("[renderer] sending 'post-markdown'..."); + window.postMessage({ type: "post-markdown", text: markdown }, "*"); +} +function requestPostFile(filename) { + console.log("[renderer] triggering 'request-post-file' on '" + filename + "'..."); + window.postMessage({ type: "request-post-file", text: filename }, "*"); +} + +// utilities +function buildFileName(filename) { + var el = document.createElement("li"); + el.innerHTML = filename; + el.addEventListener("click", () => { requestPostFile(filename); }); + return el +} +function sortFileNames(a, b) { + return ($(b).text()) < ($(a).text()); +} + +// listen to preload +window.addEventListener("message", (event) => { + if (event.source != window) return; + if (event.data.type && (event.data.type == "request-post-markdown")) { + console.log("[renderer] caught 'request-post-markdown'!"); + postMarkdown(window.editor.getValue()); + }; + if (event.data.type && (event.data.type == "request-insert-filename")) { + console.log("[renderer] caught 'request-insert-filename'!"); + $("#list-filenames").append(buildFileName(event.data.text)); + $("#list-filenames li").sort(sortFileNames).appendTo("#list-filenames"); + }; + if (event.data.type && (event.data.type == "request-insert-html")) { + console.log("[renderer] caught 'request-insert-html'!"); + $("#container-results").html(event.data.text); + $(".container").toggleClass("focused"); + }; + if (event.data.type && (event.data.type == "request-toggle-editor")) { + console.log("[renderer] caught 'request-toggle-editor'!"); + $(".container").toggleClass("focused"); + }; + if (event.data.type && (event.data.type == "post-file")) { + console.log("[renderer] caught 'post-file'!"); + window.editor.setValue(event.data.text); + }; +}, false); + -- 2.45.2