-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- upgrade from Electron 9 to 12 - Remove all uses of the deprecated Electron remote module and replace with more secure IPC implementation. This also cleanly decouples the GUI app from the code that accesses the file system, and should enable us to eventually run PanWriter in a browser as well. - convert all PureScript files (which weren't that many) and most JavaScript files (which unfortunately had become many) to TypeScript - remove ability to read user-customizable css files (maybe we can add something like that in the future again, but will have to work with pandoc's document-css, which we'd need to parse properly)
- Loading branch information
Showing
90 changed files
with
14,457 additions
and
4,048 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
node_modules | ||
output | ||
.psci_modules | ||
.psc-ide-port | ||
.psc-package | ||
dist | ||
.DS_Store | ||
.eslintcache | ||
build | ||
public/katex/ | ||
public/paged.polyfill.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { BrowserWindow, dialog } from 'electron' | ||
import { readFile, writeFile } from 'fs' | ||
import { basename, extname } from 'path' | ||
import { promisify } from 'util' | ||
import * as ipc from './ipc' | ||
import { Doc } from '../src/appState/AppState' | ||
import { addToRecentFiles } from './recentFiles' | ||
|
||
|
||
export const openFile = async ( | ||
win: BrowserWindow | ||
, filePath: string | ||
): Promise<Partial<Doc> | undefined> => { | ||
const fileName = pathToName(filePath) | ||
|
||
try { | ||
const md = await promisify(readFile)(filePath, 'utf-8') | ||
win.setTitle(fileName) | ||
win.setRepresentedFilename(filePath) | ||
addToRecentFiles(filePath) | ||
return { md, fileName, filePath, fileDirty: false } | ||
} catch (err) { | ||
dialog.showMessageBox(win, { | ||
type: 'error' | ||
, message: 'Could not open file' | ||
, detail: err.message | ||
}) | ||
win.close() | ||
} | ||
} | ||
|
||
export const saveFile = async ( | ||
win: BrowserWindow | ||
, doc: Doc | ||
, opts: {saveAsNewFile?: boolean} = {} | ||
) => { | ||
const filePath = await showDialog(win, doc, opts.saveAsNewFile) | ||
|
||
if (!filePath) { | ||
return | ||
} | ||
|
||
try { | ||
await promisify(writeFile)(filePath, doc.md) | ||
|
||
const fileName = pathToName(filePath) | ||
win.setTitle(fileName) | ||
win.setRepresentedFilename(filePath) | ||
|
||
ipc.sendMessage(win, { | ||
type: 'updateDoc' | ||
, doc: { fileName, filePath, fileDirty: false } | ||
}) | ||
|
||
addToRecentFiles(filePath) | ||
} catch (err) { | ||
dialog.showMessageBox(win, { | ||
type: 'error' | ||
, message: 'Could not save file' | ||
, detail: err.message | ||
}) | ||
} | ||
} | ||
|
||
const showDialog = async (win: BrowserWindow, doc: Doc, saveAsNewFile?: boolean) => { | ||
// TODO: should we save the filePath on `win` in the main process | ||
// instead of risk it being tampered with in the renderer process? | ||
let { filePath } = doc | ||
if (filePath === undefined || saveAsNewFile) { | ||
const res = await dialog.showSaveDialog(win, { | ||
defaultPath: 'Untitled.md' | ||
, filters: [ | ||
{ name: 'Markdown', extensions: ['md', 'txt', 'markdown'] } | ||
] | ||
}) | ||
filePath = res.filePath | ||
} | ||
return filePath | ||
} | ||
|
||
const pathToName = (filePath: string) => | ||
basename(filePath, extname(filePath)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { BrowserWindow, ipcMain, shell } from 'electron' | ||
import { Doc } from '../src/appState/AppState' | ||
import { Message } from './preload' | ||
|
||
// this file contains the IPC functionality of the main process. | ||
// for the renderer process's part see electron/preload.ts | ||
|
||
export const init = () => { | ||
ipcMain.on('close', (event) => { | ||
const win = BrowserWindow.fromWebContents(event.sender) | ||
win?.close() | ||
}) | ||
|
||
ipcMain.on('minimize', (event) => { | ||
const win = BrowserWindow.fromWebContents(event.sender) | ||
win?.minimize() | ||
}) | ||
|
||
ipcMain.on('maximize', (event) => { | ||
const win = BrowserWindow.fromWebContents(event.sender) | ||
// win.isMaximized() ? win.unmaximize() : win.maximize() | ||
win?.setFullScreen( !win.isFullScreen() ) | ||
}) | ||
|
||
ipcMain.on('openLink', (_event, link: string) => { | ||
shell.openExternal(link) | ||
}) | ||
} | ||
|
||
export const getDoc = async (win: BrowserWindow): Promise<Doc> => { | ||
const replyChannel = 'getDoc' + Math.random().toString() | ||
win.webContents.send('getDoc', replyChannel) | ||
return new Promise(resolve => { | ||
ipcMain.once(replyChannel, (_event, doc) => { | ||
resolve(doc) | ||
}) | ||
}) | ||
} | ||
|
||
export const sendMessage = (win: BrowserWindow, msg: Message) => { | ||
win.webContents.send('dispatch', msg) | ||
} | ||
|
||
export const sendPlatform = (win: BrowserWindow) => { | ||
win.webContents.send('sendPlatform', process.platform) | ||
} | ||
|
||
export type Command = 'printFile' | ||
| 'find' | 'findNext' | 'findPrevious' | ||
| 'addBold' | 'addItalic' | 'addStrikethrough' | ||
|
||
export const sendCommand = (win: BrowserWindow, cmd: Command) => { | ||
win.webContents.send(cmd) | ||
} |
Oops, something went wrong.