Skip to content

Commit

Permalink
Major refactorings (#57)
Browse files Browse the repository at this point in the history
- 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
mb21 committed Apr 9, 2021
1 parent 0063e63 commit c6b4815
Show file tree
Hide file tree
Showing 154 changed files with 45,584 additions and 4,048 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
node_modules
output
.psci_modules
.psc-ide-port
.psc-package
dist
.DS_Store
.eslintcache
build
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img src="build/icons/icon.png" align="right" width="128">
<img src="icons/icon.png" align="right" width="128">

# PanWriter

Expand All @@ -23,7 +23,7 @@ Select `File -> 'Print / PDF'` and `PDF -> 'Save as PDF'` in the print dialog (e

This will export exactly what’s shown in the preview, and not use pandoc at all.

You can change the styling of the preview and immediately see the changes. (You can later save your CSS as a theme, see [Document types](#document-types--themes) below.)
You can change the styling of the preview and immediately see the changes.

![](screenshot-css.png)

Expand Down Expand Up @@ -83,13 +83,11 @@ If the directory does not exist, you can create it.

### Default CSS and YAML

PanWriter will look for a `default.css` file in the user data directory, to load CSS for the preview. If that file is not found, it will use sensible defaults.

If you put a `default.yaml` file in the data directory, PanWriter will merge this with the YAML in your input file (to determine the command-line arguments to call pandoc with) and add the `--metadata-file` option. The YAML should be in the same format as above.

### Document types / themes

You can e.g. put `type: letter` in the YAML of your input document. In that case, PanWriter will look for `letter.yaml` and `letter.css` instead of `default.yaml` and `default.css` in the user data directory.
You can e.g. put `type: letter` in the YAML of your input document. In that case, PanWriter will look for `letter.yaml` instead of `default.yaml` in the user data directory.

### Markdown syntax

Expand Down
82 changes: 82 additions & 0 deletions electron/file.ts
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))
54 changes: 54 additions & 0 deletions electron/ipc.ts
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)
}
Loading

0 comments on commit c6b4815

Please sign in to comment.