Skip to content

Commit da9059e

Browse files
authored
Merge pull request #7 from hackmdio/customize-server-url
Customize server url
2 parents 6506566 + 086fe40 commit da9059e

16 files changed

+427
-6
lines changed

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,29 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<link rel="stylesheet" type="text/css" href="vendor/font-awesome.min.css">
8+
<link rel="stylesheet" type="text/css" href="vendor/css/bootstrap.min.css">
89
<link rel="stylesheet" type="text/css" href="./app.css">
910
</head>
1011
<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 HackMD server</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>
1131
<navbar>
1232
<div id="navbar-container">
1333
<div class="control-buttons">
@@ -27,6 +47,8 @@
2747
</div>
2848
</div>
2949
</navbar>
50+
<script>window.$ = window.jQuery = require('jquery');</script>
51+
<script src="vendor/js/bootstrap.min.js"></script>
3052
<script src="renderer.js"></script>
3153
</body>
3254
</html>

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: 'Customize HackMD server',
76+
click () {
77+
exec('configServerUrl');
78+
}
79+
}
7080
]
7181
},
7282
{

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@
2424
"mac": {
2525
"category": "public.app-category.productivity"
2626
}
27+
},
28+
"dependencies": {
29+
"electron-config": "^0.2.1",
30+
"jquery": "^3.2.1",
31+
"validate.js": "^0.11.1"
2732
}
2833
}

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: { allowLocal: 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+
}

vendor/css/bootstrap.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/css/bootstrap.min.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
19.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)