Skip to content

Instantly share code, notes, and snippets.

@mkaminsky11
Last active January 4, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkaminsky11/8624455 to your computer and use it in GitHub Desktop.
Save mkaminsky11/8624455 to your computer and use it in GitHub Desktop.
ace control
$(window).bind('contextmenu', function(e) {
// do stuff here instead of normal context menu
if(!yes_context){
return false;
}
});
function getHint(){
editor.execCommand("startAutocomplete");
yes_context = false; //this temporarily disables the context menu
}
var editor = ace.edit("content");
ace.require("ace/ext/language_tools");
editor.on("change", function(e){
setState("unsaved");
checkUndo(); //can you undo?
yes_context = true;
});
editor.session.setUseWrapMode(true);
editor.session.setWrapLimitRange();
editor.setShowFoldWidgets(true);
editor.setShowPrintMargin(false);
editor.getSession().selection.on('changeCursor', function(e){ //cursor position indicator
var display = editor.getCursorPosition().column + "|" + editor.getCursorPosition().row;
$("#indicator").html(display);
});
/*
Line numbers, line wrap, autocomplete
*/
var line_number = true;
function line_numbers() {
if(line_number){
editor.renderer.setShowGutter(false);
line_number = false;
}
else{
editor.renderer.setShowGutter(true);
line_number = true;
}
}
function line_wrap() {
if(editor.session.getUseWrapMode()){
editor.session.setUseWrapMode(false);
}
else{
editor.session.setUseWrapMode(true);
editor.session.setWrapLimitRange();
}
}
function auto_bind(){
if(autoC === true){
autoC = false;
}
else{
autoC = true;
}
}
//intitialize modes
var supportedModes = {
ABAP: ["abap"],
ActionScript:["as"],
ADA: ["ada|adb"],
Apache_Conf: ["^htaccess|^htgroups|^htpasswd|^conf|htaccess|htgroups|htpasswd"],
AsciiDoc: ["asciidoc"],
Assembly_x86:["asm"],
AutoHotKey: ["ahk"],
BatchFile: ["bat|cmd"],
C9Search: ["c9search_results"],
C_Cpp: ["cpp|c|cc|cxx|h|hh|hpp"],
Cirru: ["cirru|cr"],
Clojure: ["clj"],
Cobol: ["CBL|COB"],
coffee: ["coffee|cf|cson|^Cakefile"],
ColdFusion: ["cfm"],
CSharp: ["cs"],
CSS: ["css"],
Curly: ["curly"],
D: ["d|di"],
Dart: ["dart"],
Diff: ["diff|patch"],
Dot: ["dot"],
Erlang: ["erl|hrl"],
EJS: ["ejs"],
Forth: ["frt|fs|ldr"],
FTL: ["ftl"],
Gherkin: ["feature"],
Glsl: ["glsl|frag|vert"],
golang: ["go"],
Groovy: ["groovy"],
HAML: ["haml"],
Handlebars: ["hbs|handlebars|tpl|mustache"],
Haskell: ["hs"],
haXe: ["hx"],
HTML: ["html|htm|xhtml"],
HTML_Ruby: ["erb|rhtml|html.erb"],
INI: ["ini|conf|cfg|prefs"],
Jack: ["jack"],
Jade: ["jade"],
Java: ["java"],
JavaScript: ["js|jsm"],
JSON: ["json"],
JSONiq: ["jq"],
JSP: ["jsp"],
JSX: ["jsx"],
Julia: ["jl"],
LaTeX: ["tex|latex|ltx|bib"],
LESS: ["less"],
Liquid: ["liquid"],
Lisp: ["lisp"],
LiveScript: ["ls"],
LogiQL: ["logic|lql"],
LSL: ["lsl"],
Lua: ["lua"],
LuaPage: ["lp"],
Lucene: ["lucene"],
Makefile: ["^Makefile|^GNUmakefile|^makefile|^OCamlMakefile|make"],
MATLAB: ["matlab"],
Markdown: ["md|markdown"],
MEL: ["mel"],
MySQL: ["mysql"],
MUSHCode: ["mc|mush"],
Nix: ["nix"],
ObjectiveC: ["m|mm"],
OCaml: ["ml|mli"],
Pascal: ["pas|p"],
Perl: ["pl|pm"],
pgSQL: ["pgsql"],
PHP: ["php|phtml"],
Powershell: ["ps1"],
Prolog: ["plg|prolog"],
Properties: ["properties"],
Protobuf: ["proto"],
Python: ["py"],
R: ["r"],
RDoc: ["Rd"],
RHTML: ["Rhtml"],
Ruby: ["rb|ru|gemspec|rake|^Guardfile|^Rakefile|^Gemfile"],
Rust: ["rs"],
SASS: ["sass"],
SCAD: ["scad"],
Scala: ["scala"],
Smarty: ["smarty|tpl"],
Scheme: ["scm|rkt"],
SCSS: ["scss"],
SH: ["sh|bash|^.bashrc"],
SJS: ["sjs"],
Space: ["space"],
snippets: ["snippets"],
Soy_Template:["soy"],
SQL: ["sql"],
Stylus: ["styl|stylus"],
SVG: ["svg"],
Tcl: ["tcl"],
Tex: ["tex"],
Text: ["txt"],
Textile: ["textile"],
Toml: ["toml"],
Twig: ["twig"],
Typescript: ["ts|typescript|str"],
VBScript: ["vbs"],
Velocity: ["vm"],
Verilog: ["v|vh|sv|svh"],
XML: ["xml|rdf|rss|wsdl|xslt|atom|mathml|mml|xul|xbl"],
XQuery: ["xq"],
YAML: ["yaml|yml"]
};
//javascript = 40
var nameOverrides = {
ObjectiveC: "Objective-C",
CSharp: "C#",
golang: "Go",
C_Cpp: "C/C++",
coffee: "CoffeeScript",
HTML_Ruby: "HTML (Ruby)",
FTL: "FreeMarker"
};
var modesByName = {};
var modes = new Array();
var modeNames = [];
var extensions = [];
for (var name in supportedModes) {
var data = supportedModes[name]; //the data
extensions.push(data);
var displayName = (nameOverrides[name] || name).replace(/_/g, " ");
modeNames.push(displayName);
var filename = name.toLowerCase();
var mode = require("ace/mode/" + filename).Mode;
modesByName[filename] = mode;
modes.push(mode);
var onclick_name = "stuff." + data[0].split("|")[0];
var onclick_html = new String("checkFileName('" + onclick_name + "')");
var new_html = "<li class='list-group-item' onclick=\"" + onclick_html + "\">" + displayName + "</li>";
$(".mode_ul").html($(".mode_ul").html() + new_html);
}
//set the file name
function checkFileName(fileName){
$(".run-button").addClass("hide");
$("#console_toggle").addClass("hide");
//BASIC AUTOCOMPLETE
basicAuto();
/*
JAVASCRIPT
*/
if(fileName.indexOf(".js") !== -1){
//javascript
$(".run-button").removeClass("hide");
$("#console_toggle").removeClass("hide");
}
else{
if(console_open){
closeConsole();
}
}
var e = exten(fileName);
if(e !== "docx" && e !== "jpg" && e !== "png" && e !== "pptx" && e !== "jpeg" && e !== "xls"){
for(var a = 0; a < extensions.length; a++){
for(var b = 0; b < extensions[a].length; b++){
var s = extensions[a][b].split("|");
for(var c= 0; c < s.length; c++){
if(s[c] === e){
editor.getSession().setMode(new modes[a]());
sendMessage("Mode changed!", "success");
}
}
}
}
}
else{
badType();
}
}
function basicAuto(){
editor.setOptions({
enableBasicAutocompletion: true
});
}
function badType(){
window.location="https://codeyourcloud.com/error/file";
}
function exten(string){
var ret = "none";
if(string.indexOf(".") !== -1){
var sp = string.split(".");
ret = sp[sp.length-1];
}
return ret;
}
function updatePreview() {
var previewFrame = document.getElementById('preview');
var preview = previewFrame.contentDocument || previewFrame.contentWindow.document;
preview.open();
preview.write(editor.getValue());
preview.close();
}
setInterval(function(){
updatePreview();
}, 3000);
/*
Initialize themes
*/
var themeData = [
["Chrome"],
["Clouds"],
["Crimson Editor"],
["Dawn"],
["Dreamweaver"],
["Eclipse"],
["GitHub"],
["Solarized Light"],
["TextMate"],
["Tomorrow"],
["XCode"],
["Kuroir"],
["KatzenMilch"],
["Ambiance","ambiance"],
["Chaos","chaos"],
["Clouds Midnight","clouds_midnight"],
["Cobalt","cobalt"],
["idle Fingers","idle_fingers"],
["krTheme","kr_theme"],
["Merbivore","merbivore" ],
["Merbivore Soft","merbivore_soft"],
["Mono Industrial","mono_industrial"],
["Monokai","monokai"],
["Pastel on dark","pastel_on_dark"],
["Solarized Dark","solarized_dark"],
["Terminal","terminal"],
["Tomorrow Night","tomorrow_night"],
["Tomorrow Night Blue","tomorrow_night_blue"],
["Tomorrow Night Bright","tomorrow_night_bright"],
["Tomorrow Night 80s","tomorrow_night_eighties"],
["Twilight","twilight"],
["Vibrant Ink","vibrant_ink"]
];
for(var i = 0; i < themeData.length; i++){
var theme_name = "";
var theme_formal_name = "";
if(themeData[i].length == 2){
theme_name = themeData[i][0];
theme_formal_name = themeData[i][1];
}
else{
theme_name = themeData[i][0];
theme_formal_name = themeData[i][0].toLowerCase();
}
var theme_html = "<li class='list-group-item' onclick=\"setTheme('" + theme_formal_name + "')\">" + theme_name + "</li>"
$(".theme_ul").html($(".theme_ul").html() + theme_html);
}
setTheme("merbivore_soft");
function setTheme(theme_name){
editor.setTheme("ace/theme/" + theme_name);
}
/*
Undo/redo
*/
function navUndo() {
if(editor.getSession().getUndoManager().hasUndo()){
editor.undo();
}
else{
addClass("undoB", "disabled");
}
checkUndo();
}
function checkUndo(){
if(editor.getSession().getUndoManager().hasUndo()){
removeClass("undoB", "disabled");
}
else{
addClass("undoB", "disabled");
}
if(editor.getSession().getUndoManager().hasRedo()){
removeClass("redoB", "disabled");
}
else{
addClass("redoB", "disabled");
}
}
function navRedo() {
if(editor.getSession().getUndoManager().hasRedo()){
editor.redo();
}
else{
addClass("redoB", "disabled");
}
checkUndo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment