Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a Settings/Preferences window #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore-engines true
13 changes: 10 additions & 3 deletions electron/dataDir.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { readFile } from 'fs/promises'
import * as jsYaml from 'js-yaml'
import { app } from 'electron'
import { basename, sep } from 'path'
import { Meta } from '../src/appState/AppState'
import { pandocPreferences } from './settings';

export const dataDir = [app.getPath('appData'), 'PanWriterUserData', ''].join(sep)
export function getDataDir() {
let dataDir : string = pandocPreferences.value('main.userDataDir');
if(dataDir.endsWith(sep)) {
return dataDir;
} else {
return [dataDir, ''].join(sep);
}
}

/**
* reads the right default yaml file
Expand All @@ -13,7 +20,7 @@ export const dataDir = [app.getPath('appData'), 'PanWriterUserData', ''].join(se
export const readDataDirFile = async (fileName: string): Promise<[Meta | undefined, string]> => {
try {
// make sure only PanWriterUserData directory can be accessed
fileName = dataDir + basename(fileName)
fileName = getDataDir() + basename(fileName)

const str = await readFile(fileName, 'utf8')
const yaml = jsYaml.safeLoad(str)
Expand Down
18 changes: 17 additions & 1 deletion electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { importFile } from './pandoc/import'
import { saveFile, openFile } from './file'
import { Message } from './preload'
import { clearRecentFiles, getRecentFiles } from './recentFiles'
import { pandocPreferences } from './settings';

const { autoUpdater } = require('electron-updater')
require('fix-path')() // needed to execute pandoc on macOS prod build
Expand Down Expand Up @@ -189,7 +190,9 @@ const createWindow = async (filePath?: string, toImport=false, wasCreatedOnStart
const emptyStartupWindow = !(initialFilePath || initialFileIsToImport)
createWindow(initialFilePath, initialFileIsToImport, emptyStartupWindow)
}
autoUpdater.checkForUpdatesAndNotify()
if(pandocPreferences.value('main.autoUpdate') != 'false') {
autoUpdater.checkForUpdatesAndNotify()
}
})
})()

Expand Down Expand Up @@ -254,6 +257,11 @@ const windowSendMessage = async (msg: Message) => {
}
}

const openPreferences = () => {
// Show the preferences window on demand.
pandocPreferences.show();
}

const setMenu = async (aWindowIsOpen=true, useRecentFilesCache=false) => {
const recentFiles = await getRecentFiles(useRecentFilesCache)
const template: Electron.MenuItemConstructorOptions[] = [
Expand Down Expand Up @@ -324,6 +332,14 @@ const setMenu = async (aWindowIsOpen=true, useRecentFilesCache=false) => {
, accelerator: 'CmdOrCtrl+I'
, click: () => openDialog(true)
}
, {type: 'separator'}
, { label: 'Preferences'
, accelerator: 'CmdOrCtrl+P'
, click: () => openPreferences()
}
, { label: 'Close window'
, click: () => closeWindow()
}
]
}
, { label: 'Edit'
Expand Down
48 changes: 48 additions & 0 deletions electron/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { app } from 'electron'
import { sep } from 'path'
const ElectronPreferences = require('electron-preferences');

const defaultDataDir = [app.getPath('appData'), 'PanWriterUserData', ''].join(sep)

export const pandocPreferences = new ElectronPreferences({
'dataStore': defaultDataDir + 'preferences.json',
'defaults': {
'main': {
'userDataDir': defaultDataDir,
'autoUpdate' : 'true'
}
},
'sections': [
{
'id': 'main',
'label': 'Main',
'form': {
'groups': [
{
'label': 'Directories',
'fields': [
{
'label': 'User data directory',
'key': 'userDataDir',
'type': 'directory',
'help': 'PanWriter user directory'
}
]
},
{
'label': 'Updates',
'fields': [
{
'label': 'Auto Update',
'key': 'autoUpdate',
'type': 'radio',
'options': [{ label: 'Yes', value: 'true' }, {'label': 'No', 'value': 'false'}],
'help': 'Automatic upgrade to new versions'
}
]
}
]
}
}
]
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
},
"dependencies": {
"codemirror": "^5.61.0",
"electron-preferences": "^2.4.1",
"electron-updater": "^4.3.5",
"fix-path": "^3.0.0",
"js-yaml": "^3.14.0",
Expand Down
Loading