Skip to content

Commit

Permalink
Impl customize server url
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukaii committed Mar 21, 2017
1 parent d87aff2 commit 32f75ef
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 7 deletions.
8 changes: 8 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Config = require('electron-config');
const config = new Config();

const DEFAULT_SERVER_URL = 'https://hackmd.io';

module.exports = {
DEFAULT_SERVER_URL
}
19 changes: 19 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
<link rel="stylesheet" type="text/css" href="./app.css">
</head>
<body>
<div class="modal fade" tabindex="-1" role="dialog" id="serverurl-config-modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Customize server url</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="text" class="form-control" placeholder="https://hackmd.io">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="submit-serverurl">Save</button>
</div>
</div>
</div>
</div>
<navbar>
<div id="navbar-container">
<div class="control-buttons">
Expand Down
3 changes: 3 additions & 0 deletions ipc/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module.exports = function(commandId, args={}) {
case 'goBack':
BrowserWindow.getFocusedWindow().webContents.send('web:go-back');
break;
case 'configServerUrl':
BrowserWindow.getFocusedWindow().webContents.send('config-serverurl');
break;
default:
break;
}
Expand Down
14 changes: 12 additions & 2 deletions menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Menu = require('electron').Menu || require('electron').remote.Menu;
const app = require('electron').app || require('electron').remote.app;

const consumer = require('./ipc/consumer');
const { getServerUrl } = require('./utils');

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

Expand All @@ -25,7 +26,7 @@ const template = [
label: 'New File',
accelerator: 'CmdOrCtrl+N',
click () {
exec('createWindow', {url: `file://${path.join(__dirname, 'index.html?target=https://hackmd.io/new')}`})
exec('createWindow', {url: `file://${path.join(__dirname, `index.html?target=${path.join(getServerUrl(), '/new')}`)}`})
}
},
{
Expand Down Expand Up @@ -66,7 +67,16 @@ const template = [
},
{
role: 'selectall'
}
},
{
type: 'separator',
},
{
label: 'Config server url',
click () {
exec('configServerUrl');
}
}
]
},
{
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
}
},
"dependencies": {
"jquery": "^3.2.1"
"electron-config": "^0.2.1",
"jquery": "^3.2.1",
"validate.js": "^0.11.1"
}
}
38 changes: 34 additions & 4 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ const fs = remote.require('fs');
const os = remote.require('os');
const path = remote.require('path');

const Config = require('electron-config');
const config = new Config();
const validate = require('validate.js');

const ipcClient = require('./ipc/client');

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

const SERVER_URL = 'https://hackmd.io';
const { DEFAULT_SERVER_URL } = require('./constants');
const { getServerUrl } = require('./utils');

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

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

let targetURL;
if (window.location.search !== '') {
targetURL = window.location.search.match(/\?target=([^?]+)/)[1];
} else {
targetURL = SERVER_URL;
targetURL = getServerUrl();
}

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

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

document.querySelector('#navbar-container .home').onclick = () => {
webview.loadURL(SERVER_URL);
webview.loadURL(getServerUrl());
}

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

ipcRenderer.on('config-serverurl', () => {
if (!getServerUrl().match(/https?:\/\/hackmd\.io/)) {
$('#serverurl-config-modal.modal input[type="text"]').val(getServerUrl());
}
$('#serverurl-config-modal.modal').modal();
})

$('#serverurl-config-modal.modal #submit-serverurl').click(function () {
let serverurl = $('#serverurl-config-modal.modal input[type="text"]').val();

// reset default
if (serverurl.length === 0) { serverurl = DEFAULT_SERVER_URL; }

const errors = validate({ serverurl }, { serverurl: {url: true}})
if (!errors) {
config.set('serverurl', serverurl);
webview.loadURL(serverurl);
$('#serverurl-config-modal.modal').modal('hide');
} else {
// show me some error
alert(errors.serverurl);
}
})

/* handle _target=blank pages */
webview.addEventListener('new-window', (event) => {
ipcClient('createWindow', { url: `file://${path.join(__dirname, `index.html?target=${event.url}`)}` });
Expand Down
8 changes: 8 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Config = require('electron-config');
const config = new Config();

const getServerUrl = () => config.get('serverurl') || 'https://hackmd.io';

module.exports = {
getServerUrl
}
29 changes: 29 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ concat-stream@1.5.0:
readable-stream "~2.0.0"
typedarray "~0.0.5"

conf@^0.11.1:
version "0.11.2"
resolved "https://registry.yarnpkg.com/conf/-/conf-0.11.2.tgz#879f479267600483e502583462ca4063fc9779b2"
dependencies:
dot-prop "^3.0.0"
env-paths "^0.3.0"
mkdirp "^0.5.1"
pkg-up "^1.0.0"

configstore@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/configstore/-/configstore-2.1.0.tgz#737a3a7036e9886102aa6099e47bb33ab1aba1a1"
Expand Down Expand Up @@ -404,6 +413,12 @@ electron-builder-util@11.4.0:
source-map-support "^0.4.9"
stat-mode "^0.2.2"

electron-config:
version "0.2.1"
resolved "https://registry.yarnpkg.com/electron-config/-/electron-config-0.2.1.tgz#7e12c26412d06bf3ed3896d0479df162986b95ba"
dependencies:
conf "^0.11.1"

electron-download-tf@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/electron-download-tf/-/electron-download-tf-3.1.0.tgz#c6d62c0e0a4c63b67295f57b6b66514c13b8ed8d"
Expand Down Expand Up @@ -442,6 +457,10 @@ electron-macos-sign@~1.4.0:
minimist "^1.2.0"
plist "^2.0.1"

env-paths@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-0.3.1.tgz#c30ccfcbc30c890943dc08a85582517ef00da463"

error-ex@^1.2.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.0.tgz#e67b43f3e82c96ea3a584ffee0b9fc3325d802d9"
Expand Down Expand Up @@ -1074,6 +1093,12 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"

pkg-up@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-1.0.0.tgz#3e08fb461525c4421624a33b9f7e6d0af5b05a26"
dependencies:
find-up "^1.0.0"

plist@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/plist/-/plist-2.0.1.tgz#0a32ca9481b1c364e92e18dc55c876de9d01da8b"
Expand Down Expand Up @@ -1486,6 +1511,10 @@ validate-npm-package-license@^3.0.1:
spdx-correct "~1.0.0"
spdx-expression-parse "~1.0.0"

validate.js:
version "0.11.1"
resolved "https://registry.yarnpkg.com/validate.js/-/validate.js-0.11.1.tgz#f51c3c6c4a56e6380a20a7eb104245037f2a540d"

verror@1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c"
Expand Down

0 comments on commit 32f75ef

Please sign in to comment.