2013-12-04 17:24:04 +00:00
|
|
|
define([
|
|
|
|
"storage/file",
|
|
|
|
"command",
|
|
|
|
"settings!ace,user",
|
2014-07-07 18:49:13 +00:00
|
|
|
"util/i18n",
|
2013-12-04 17:24:04 +00:00
|
|
|
"util/dom2"
|
2014-07-07 18:49:13 +00:00
|
|
|
], function(File, command, Settings, i18n) {
|
2013-08-20 00:53:03 +00:00
|
|
|
/*
|
|
|
|
Module for loading the editor, adding window resizing and other events. Returns the editor straight from Ace.
|
|
|
|
*/
|
2013-09-03 15:47:59 +00:00
|
|
|
var userConfig = Settings.get("user");
|
|
|
|
var aceConfig = Settings.get("ace");
|
2013-08-20 00:53:03 +00:00
|
|
|
|
|
|
|
var editor = window.editor = ace.edit("editor");
|
|
|
|
|
2013-08-23 23:03:46 +00:00
|
|
|
var themes = document.querySelector(".theme");
|
|
|
|
|
2014-02-27 06:34:02 +00:00
|
|
|
//disable focusing on the editor except by program
|
|
|
|
document.find("textarea").setAttribute("tabindex", -1);
|
|
|
|
|
2013-09-02 01:26:22 +00:00
|
|
|
//one-time startup
|
|
|
|
var init = function() {
|
2013-09-03 15:47:59 +00:00
|
|
|
aceConfig.themes.forEach(function(theme) {
|
2013-09-02 01:26:22 +00:00
|
|
|
var option = document.createElement("option");
|
2013-09-15 17:37:44 +00:00
|
|
|
option.innerHTML = theme.label;
|
2013-09-02 01:26:22 +00:00
|
|
|
option.setAttribute("value", theme.name);
|
|
|
|
themes.append(option);
|
|
|
|
});
|
|
|
|
reset();
|
2014-01-12 23:35:30 +00:00
|
|
|
//let main.js know this module is ready
|
|
|
|
return "editor";
|
2013-09-02 01:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//reloaded when settings change
|
|
|
|
var reset = function() {
|
2013-09-04 19:26:09 +00:00
|
|
|
userConfig = Settings.get("user");
|
2013-09-03 15:47:59 +00:00
|
|
|
themes.value = userConfig.defaultTheme;
|
2013-09-02 01:26:22 +00:00
|
|
|
editor.setTheme("ace/theme/" + themes.value);
|
2014-06-22 00:57:48 +00:00
|
|
|
editor.setOptions({
|
|
|
|
scrollPastEnd: userConfig.scrollPastEnd,
|
|
|
|
showGutter: !userConfig.hideGutter
|
|
|
|
});
|
2013-09-20 15:08:41 +00:00
|
|
|
editor.setShowPrintMargin(userConfig.showMargin || false);
|
|
|
|
editor.setPrintMarginColumn(userConfig.wrapLimit || 80);
|
2013-10-24 19:32:30 +00:00
|
|
|
editor.setShowInvisibles(userConfig.showWhitespace || false);
|
2014-02-06 20:26:01 +00:00
|
|
|
editor.setHighlightActiveLine(userConfig.highlightLine || false);
|
2013-09-10 23:26:36 +00:00
|
|
|
editor.container.style.fontFamily = userConfig.fontFamily || null;
|
2013-11-19 16:20:05 +00:00
|
|
|
defaultFontSize();
|
2013-10-28 16:18:43 +00:00
|
|
|
ace.config.loadModule("ace/ext/language_tools", function() {
|
|
|
|
editor.setOptions({
|
|
|
|
enableBasicAutocompletion: userConfig.autocomplete
|
|
|
|
});
|
|
|
|
});
|
2013-09-10 23:26:36 +00:00
|
|
|
};
|
2013-09-02 01:26:22 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
var defaultFontSize = function(c) {
|
2013-11-19 16:20:05 +00:00
|
|
|
var size = Settings.get("user").fontSize;
|
|
|
|
editor.container.style.fontSize = size ? size + "px" : null;
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-11-19 16:20:05 +00:00
|
|
|
};
|
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
var adjustFontSize = function(delta, c) {
|
2013-11-19 16:20:05 +00:00
|
|
|
var current = editor.container.style.fontSize;
|
|
|
|
if (current) {
|
|
|
|
current = current.replace("px", "") * 1;
|
|
|
|
} else {
|
|
|
|
current = Settings.get("user").fontSize;
|
|
|
|
}
|
|
|
|
var adjusted = current + delta;
|
|
|
|
editor.container.style.fontSize = adjusted + "px";
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2014-02-27 06:34:02 +00:00
|
|
|
};
|
2013-11-19 16:20:05 +00:00
|
|
|
|
|
|
|
command.on("editor:default-zoom", defaultFontSize);
|
|
|
|
command.on("editor:adjust-zoom", adjustFontSize);
|
|
|
|
|
2013-09-02 01:26:22 +00:00
|
|
|
command.on("init:startup", init);
|
|
|
|
command.on("init:restart", reset);
|
2013-08-23 23:03:46 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("editor:theme", function(theme, c) {
|
2013-08-23 23:03:46 +00:00
|
|
|
editor.setTheme("ace/theme/" + theme);
|
2013-12-16 17:21:55 +00:00
|
|
|
themes.value = theme;
|
2013-09-05 15:28:40 +00:00
|
|
|
editor.focus();
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-08-23 23:03:46 +00:00
|
|
|
});
|
|
|
|
|
2014-01-20 17:10:24 +00:00
|
|
|
command.on("editor:print", function(c) {
|
2014-12-02 22:13:16 +00:00
|
|
|
ace.require("ace/config").loadModule("ace/ext/static_highlight", function(highlighter) {
|
2014-01-20 17:10:24 +00:00
|
|
|
var session = editor.getSession();
|
2014-12-02 22:13:16 +00:00
|
|
|
var printable = highlighter.renderSync(session.getValue(), session.getMode(), editor.renderer.theme);
|
2014-01-20 17:10:24 +00:00
|
|
|
var iframe = document.createElement("iframe");
|
|
|
|
var css = "<style>" + printable.css + "</style>";
|
|
|
|
var doc = css + printable.html;
|
|
|
|
iframe.srcdoc = doc;
|
|
|
|
iframe.width = iframe.height = 1;
|
|
|
|
iframe.style.display = "none";
|
|
|
|
document.body.append(iframe);
|
|
|
|
setTimeout(function() {
|
|
|
|
iframe.contentWindow.print();
|
|
|
|
setTimeout(function() {
|
|
|
|
iframe.remove();
|
2014-02-27 06:34:02 +00:00
|
|
|
});
|
2014-01-20 17:10:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-20 17:00:29 +00:00
|
|
|
command.on("editor:word-count", function(c) {
|
|
|
|
var text = editor.getSession().getValue();
|
2014-07-07 18:49:13 +00:00
|
|
|
var lines = text.split("\n").length;
|
|
|
|
var characters = text.length;
|
2014-02-22 04:29:41 +00:00
|
|
|
var words = text.match(/\b\S+\b/g);
|
|
|
|
words = words ? words.length : 0;
|
2014-07-07 18:49:13 +00:00
|
|
|
command.fire("status:toast", i18n.get("editorWordCount", characters, words, lines));
|
2014-02-20 17:00:29 +00:00
|
|
|
});
|
|
|
|
|
2013-08-20 00:53:03 +00:00
|
|
|
return editor;
|
|
|
|
|
2014-05-29 14:41:53 +00:00
|
|
|
});
|