Skip to content

Commit 32f75ef

Browse files
committed
Impl customize server url
1 parent d87aff2 commit 32f75ef

8 files changed

Lines changed: 116 additions & 7 deletions

File tree

constants.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const Config = require('electron-config');
2+
const config = new Config();
3+
4+
const DEFAULT_SERVER_URL = 'https://hackmd.io';
5+
6+
module.exports = {
7+
DEFAULT_SERVER_URL
8+
}

index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
<link rel="stylesheet" type="text/css" href="./app.css">
1010
</head>
1111
<body>
12+
<div class="modal fade" tabindex="-1" role="dialog" id="serverurl-config-modal">
13+
<div class="modal-dialog" role="document">
14+
<div class="modal-content">
15+
<div class="modal-header">
16+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
17+
<h4 class="modal-title">Customize server url</h4>
18+
</div>
19+
<div class="modal-body">
20+
<div class="form-group">
21+
<input type="text" class="form-control" placeholder="https://hackmd.io">
22+
</div>
23+
</div>
24+
<div class="modal-footer">
25+
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
26+
<button type="button" class="btn btn-primary" id="submit-serverurl">Save</button>
27+
</div>
28+
</div>
29+
</div>
30+
</div>
1231
<navbar>
1332
<div id="navbar-container">
1433
<div class="control-buttons">

ipc/consumer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ module.exports = function(commandId, args={}) {
2020
case 'goBack':
2121
BrowserWindow.getFocusedWindow().webContents.send('web:go-back');
2222
break;
23+
case 'configServerUrl':
24+
BrowserWindow.getFocusedWindow().webContents.send('config-serverurl');
25+
break;
2326
default:
2427
break;
2528
}

menu.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Menu = require('electron').Menu || require('electron').remote.Menu;
66
const app = require('electron').app || require('electron').remote.app;
77

88
const consumer = require('./ipc/consumer');
9+
const { getServerUrl } = require('./utils');
910

1011
const isMainProcess = typeof ipcMain !== 'undefined';
1112

@@ -25,7 +26,7 @@ const template = [
2526
label: 'New File',
2627
accelerator: 'CmdOrCtrl+N',
2728
click () {
28-
exec('createWindow', {url: `file://${path.join(__dirname, 'index.html?target=https://hackmd.io/new')}`})
29+
exec('createWindow', {url: `file://${path.join(__dirname, `index.html?target=${path.join(getServerUrl(), '/new')}`)}`})
2930
}
3031
},
3132
{
@@ -66,7 +67,16 @@ const template = [
6667
},
6768
{
6869
role: 'selectall'
69-
}
70+
},
71+
{
72+
type: 'separator',
73+
},
74+
{
75+
label: 'Config server url',
76+
click () {
77+
exec('configServerUrl');
78+
}
79+
}
7080
]
7181
},
7282
{

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
}
2727
},
2828
"dependencies": {
29-
"jquery": "^3.2.1"
29+
"electron-config": "^0.2.1",
30+
"jquery": "^3.2.1",
31+
"validate.js": "^0.11.1"
3032
}
3133
}

renderer.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ const fs = remote.require('fs');
55
const os = remote.require('os');
66
const path = remote.require('path');
77

8+
const Config = require('electron-config');
9+
const config = new Config();
10+
const validate = require('validate.js');
11+
812
const ipcClient = require('./ipc/client');
913

1014
const menu = require('./menu');
1115

12-
const SERVER_URL = 'https://hackmd.io';
16+
const { DEFAULT_SERVER_URL } = require('./constants');
17+
const { getServerUrl } = require('./utils');
1318

1419
const isMac = os.platform() === 'darwin';
1520

@@ -21,10 +26,11 @@ onload = () => {
2126
document.querySelector('#navbar-container .more-menu').style.display = 'none';
2227
}
2328

29+
let targetURL;
2430
if (window.location.search !== '') {
2531
targetURL = window.location.search.match(/\?target=([^?]+)/)[1];
2632
} else {
27-
targetURL = SERVER_URL;
33+
targetURL = getServerUrl();
2834
}
2935

3036
document.body.innerHTML += `<webview src="${targetURL}" id="main-window" disablewebsecurity autosize="on" allowpopups allowfileaccessfromfiles></webview>`;
@@ -37,7 +43,7 @@ onload = () => {
3743
document.querySelector('title').innerHTML = webview.getTitle();
3844

3945
// set dark theme if in home page
40-
if (webview.getURL().split('?')[0].split('#')[0].match(/https:\/\/hackmd.io\/$/)) {
46+
if (webview.getURL().split('?')[0].split('#')[0].match(/https?:\/\/hackmd.io\/$/)) {
4147
document.querySelector('navbar').className = 'dark';
4248
} else {
4349
document.querySelector('navbar').className = '';
@@ -57,7 +63,7 @@ onload = () => {
5763
};
5864

5965
document.querySelector('#navbar-container .home').onclick = () => {
60-
webview.loadURL(SERVER_URL);
66+
webview.loadURL(getServerUrl());
6167
}
6268

6369
document.querySelector('#navbar-container .refresh').onclick = () => {
@@ -122,6 +128,30 @@ onload = () => {
122128
document.querySelector('navbar').style.display = 'inherit';
123129
})
124130

131+
ipcRenderer.on('config-serverurl', () => {
132+
if (!getServerUrl().match(/https?:\/\/hackmd\.io/)) {
133+
$('#serverurl-config-modal.modal input[type="text"]').val(getServerUrl());
134+
}
135+
$('#serverurl-config-modal.modal').modal();
136+
})
137+
138+
$('#serverurl-config-modal.modal #submit-serverurl').click(function () {
139+
let serverurl = $('#serverurl-config-modal.modal input[type="text"]').val();
140+
141+
// reset default
142+
if (serverurl.length === 0) { serverurl = DEFAULT_SERVER_URL; }
143+
144+
const errors = validate({ serverurl }, { serverurl: {url: true}})
145+
if (!errors) {
146+
config.set('serverurl', serverurl);
147+
webview.loadURL(serverurl);
148+
$('#serverurl-config-modal.modal').modal('hide');
149+
} else {
150+
// show me some error
151+
alert(errors.serverurl);
152+
}
153+
})
154+
125155
/* handle _target=blank pages */
126156
webview.addEventListener('new-window', (event) => {
127157
ipcClient('createWindow', { url: `file://${path.join(__dirname, `index.html?target=${event.url}`)}` });

utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const Config = require('electron-config');
2+
const config = new Config();
3+
4+
const getServerUrl = () => config.get('serverurl') || 'https://hackmd.io';
5+
6+
module.exports = {
7+
getServerUrl
8+
}

yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ concat-stream@1.5.0:
230230
readable-stream "~2.0.0"
231231
typedarray "~0.0.5"
232232

233+
conf@^0.11.1:
234+
version "0.11.2"
235+
resolved "https://registry.yarnpkg.com/conf/-/conf-0.11.2.tgz#879f479267600483e502583462ca4063fc9779b2"
236+
dependencies:
237+
dot-prop "^3.0.0"
238+
env-paths "^0.3.0"
239+
mkdirp "^0.5.1"
240+
pkg-up "^1.0.0"
241+
233242
configstore@^2.0.0:
234243
version "2.1.0"
235244
resolved "https://registry.yarnpkg.com/configstore/-/configstore-2.1.0.tgz#737a3a7036e9886102aa6099e47bb33ab1aba1a1"
@@ -404,6 +413,12 @@ electron-builder-util@11.4.0:
404413
source-map-support "^0.4.9"
405414
stat-mode "^0.2.2"
406415

416+
electron-config:
417+
version "0.2.1"
418+
resolved "https://registry.yarnpkg.com/electron-config/-/electron-config-0.2.1.tgz#7e12c26412d06bf3ed3896d0479df162986b95ba"
419+
dependencies:
420+
conf "^0.11.1"
421+
407422
electron-download-tf@3.1.0:
408423
version "3.1.0"
409424
resolved "https://registry.yarnpkg.com/electron-download-tf/-/electron-download-tf-3.1.0.tgz#c6d62c0e0a4c63b67295f57b6b66514c13b8ed8d"
@@ -442,6 +457,10 @@ electron-macos-sign@~1.4.0:
442457
minimist "^1.2.0"
443458
plist "^2.0.1"
444459

460+
env-paths@^0.3.0:
461+
version "0.3.1"
462+
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-0.3.1.tgz#c30ccfcbc30c890943dc08a85582517ef00da463"
463+
445464
error-ex@^1.2.0:
446465
version "1.3.0"
447466
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.0.tgz#e67b43f3e82c96ea3a584ffee0b9fc3325d802d9"
@@ -1074,6 +1093,12 @@ pinkie@^2.0.0:
10741093
version "2.0.4"
10751094
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
10761095

1096+
pkg-up@^1.0.0:
1097+
version "1.0.0"
1098+
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-1.0.0.tgz#3e08fb461525c4421624a33b9f7e6d0af5b05a26"
1099+
dependencies:
1100+
find-up "^1.0.0"
1101+
10771102
plist@^2.0.1:
10781103
version "2.0.1"
10791104
resolved "https://registry.yarnpkg.com/plist/-/plist-2.0.1.tgz#0a32ca9481b1c364e92e18dc55c876de9d01da8b"
@@ -1486,6 +1511,10 @@ validate-npm-package-license@^3.0.1:
14861511
spdx-correct "~1.0.0"
14871512
spdx-expression-parse "~1.0.0"
14881513

1514+
validate.js:
1515+
version "0.11.1"
1516+
resolved "https://registry.yarnpkg.com/validate.js/-/validate.js-0.11.1.tgz#f51c3c6c4a56e6380a20a7eb104245037f2a540d"
1517+
14891518
verror@1.3.6:
14901519
version "1.3.6"
14911520
resolved "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c"

0 commit comments

Comments
 (0)