index.js 6.4 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6 7
// Warning: Do not use the `let` declarator in this file, it breaks our minification

J
Joao Moreno 已提交
8 9 10 11 12 13 14 15 16 17 18
'use strict';

/*global window,document,define*/

const path = require('path');
const electron = require('electron');
const remote = electron.remote;
const ipc = electron.ipcRenderer;

function onError(error, enableDeveloperTools) {
	if (enableDeveloperTools) {
J
Joao Moreno 已提交
19
		remote.getCurrentWebContents().openDevTools();
J
Joao Moreno 已提交
20 21 22 23 24 25 26 27 28 29 30
	}

	console.error('[uncaught exception]: ' + error);

	if (error.stack) {
		console.error(error.stack);
	}
}

function assign(destination, source) {
	return Object.keys(source)
J
Joao Moreno 已提交
31
		.reduce(function (r, key) { r[key] = source[key]; return r; }, destination);
J
Joao Moreno 已提交
32 33 34 35 36 37
}

function parseURLQueryArgs() {
	const search = window.location.search || '';

	return search.split(/[?&]/)
J
Joao Moreno 已提交
38 39 40 41
		.filter(function (param) { return !!param; })
		.map(function (param) { return param.split('='); })
		.filter(function (param) { return param.length === 2; })
		.reduce(function (r, param) { r[param[0]] = decodeURIComponent(param[1]); return r; }, {});
J
Joao Moreno 已提交
42 43 44 45 46 47 48 49 50 51 52 53
}

function createScript(src, onload) {
	const script = document.createElement('script');
	script.src = src;
	script.addEventListener('load', onload);

	const head = document.getElementsByTagName('head')[0];
	head.insertBefore(script, head.lastChild);
}

function uriFromPath(_path) {
J
Joao Moreno 已提交
54
	var pathName = path.resolve(_path).replace(/\\/g, '/');
J
Joao Moreno 已提交
55 56 57 58 59 60 61 62 63 64
	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}

	return encodeURI('file://' + pathName);
}

function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
65
	var listener;
J
Joao Moreno 已提交
66
	if (enableDeveloperTools) {
67
		const extractKey = function (e) {
J
Joao Moreno 已提交
68 69 70 71 72 73 74 75 76 77 78 79
			return [
				e.ctrlKey ? 'ctrl-' : '',
				e.metaKey ? 'meta-' : '',
				e.altKey ? 'alt-' : '',
				e.shiftKey ? 'shift-' : '',
				e.keyCode
			].join('');
		};

		const TOGGLE_DEV_TOOLS_KB = (process.platform === 'darwin' ? 'meta-alt-73' : 'ctrl-shift-73'); // mac: Cmd-Alt-I, rest: Ctrl-Shift-I
		const RELOAD_KB = (process.platform === 'darwin' ? 'meta-82' : 'ctrl-82'); // mac: Cmd-R, rest: Ctrl-R

B
Benjamin Pasero 已提交
80
		listener = function (e) {
J
Joao Moreno 已提交
81 82
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
83
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
84
			} else if (key === RELOAD_KB) {
85
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
86
			}
B
Benjamin Pasero 已提交
87 88
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
89 90
	}

91
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools) });
B
Benjamin Pasero 已提交
92 93 94 95 96 97 98

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
	}
J
Joao Moreno 已提交
99 100 101 102 103 104 105 106 107 108 109
}

function main() {
	const webFrame = require('electron').webFrame;
	const args = parseURLQueryArgs();
	const configuration = JSON.parse(args['config'] || '{}') || {};

	// Correctly inherit the parent's environment
	assign(process.env, configuration.userEnv);

	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
110
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
111 112 113 114 115 116 117 118
	const config = process.env['VSCODE_NLS_CONFIG'];
	if (config) {
		process.env['VSCODE_NLS_CONFIG'] = config;
		try {
			nlsConfig = JSON.parse(config);
		} catch (e) { /*noop*/ }
	}

J
Joao Moreno 已提交
119
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
120 121 122 123 124 125 126 127
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}

	window.document.documentElement.setAttribute('lang', locale);

128
	const enableDeveloperTools = process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath;
B
Benjamin Pasero 已提交
129
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
130 131

	// disable pinch zoom & apply zoom level early to avoid glitches
132
	const zoomLevel = configuration.zoomLevel;
J
Joao Moreno 已提交
133
	webFrame.setZoomLevelLimits(1, 1);
134 135
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
136 137
	}

138 139 140 141 142 143 144 145 146 147
	// Handle high contrast mode
	if (configuration.highContrast) {
		var themeStorageKey = 'storage://global/workbench.theme';
		var hcTheme = 'hc-black vscode-theme-defaults-themes-hc_black-json';
		if (window.localStorage.getItem(themeStorageKey) !== hcTheme) {
			window.localStorage.setItem(themeStorageKey, hcTheme);
			window.document.body.className = 'monaco-shell ' + hcTheme;
		}
	}

J
Joao Moreno 已提交
148 149
	// Load the loader and start loading the workbench
	const rootUrl = uriFromPath(configuration.appRoot) + '/out';
B
Benjamin Pasero 已提交
150

J
Joao Moreno 已提交
151 152
	// In the bundled version the nls plugin is packaged with the loader so the NLS Plugins
	// loads as soon as the loader loads. To be able to have pseudo translation
153 154
	createScript(rootUrl + '/vs/loader.js', function () {
		define('fs', ['original-fs'], function (originalFS) { return originalFS; }); // replace the patched electron fs with the original node fs for all AMD code
B
Benjamin Pasero 已提交
155

156 157 158
		window.MonacoEnvironment = {};

		const nodeCachedDataErrors = window.MonacoEnvironment.nodeCachedDataErrors = [];
J
Joao Moreno 已提交
159 160 161
		require.config({
			baseUrl: rootUrl,
			'vs/nls': nlsConfig,
162 163
			recordStats: !!configuration.performance,
			nodeCachedDataDir: configuration.nodeCachedDataDir,
J
Johannes Rieken 已提交
164
			onNodeCachedDataError: function (err) { nodeCachedDataErrors.push(err) },
J
Joao Moreno 已提交
165
		});
B
Benjamin Pasero 已提交
166

J
Joao Moreno 已提交
167
		if (nlsConfig.pseudo) {
168
			require(['vs/nls'], function (nlsPlugin) {
J
Joao Moreno 已提交
169 170 171 172
				nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
			});
		}

173
		// Perf Counters
J
Joao Moreno 已提交
174
		const timers = window.MonacoEnvironment.timers = {
175 176 177 178 179 180
			start: new Date(configuration.isInitialStartup ? configuration.perfStartTime : configuration.perfWindowLoadTime),
			isInitialStartup: !!configuration.isInitialStartup,
			hasAccessibilitySupport: !!configuration.accessibilitySupport,
			perfStartTime: new Date(configuration.perfStartTime),
			perfWindowLoadTime: new Date(configuration.perfWindowLoadTime),
			perfBeforeLoadWorkbenchMain: new Date()
J
Joao Moreno 已提交
181 182 183
		};

		require([
184 185 186
			'vs/workbench/electron-browser/workbench.main',
			'vs/nls!vs/workbench/electron-browser/workbench.main',
			'vs/css!vs/workbench/electron-browser/workbench.main'
J
Joao Moreno 已提交
187
		], function () {
188
			timers.perfAfterLoadWorkbenchMain = new Date();
J
Joao Moreno 已提交
189 190

			require('vs/workbench/electron-browser/main')
191
				.startup(configuration)
B
Benjamin Pasero 已提交
192 193 194 195 196
				.done(function () {
					unbind(); // since the workbench is running, unbind our developer related listeners and let the workbench handle them
				}, function (error) {
					onError(error, enableDeveloperTools);
				});
J
Joao Moreno 已提交
197 198 199 200
		});
	});
}

201
main();