-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.app
144 lines (124 loc) · 4.1 KB
/
lib.app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module elib/elib-ace/lib
section ace editor
// https://github.com/ajaxorg/ace/wiki/Embedding---API
// usage:
// form{
// codeEditor(foo.code, "foo", false) // to edit text value of foo.code, use "foo" once per page
// }
// make sure to add the following to css
// #editor<foo> {
// position: relative;
// width: 100%;
// height: 300px;
// border: 1px solid black;
// }
define requireACE(lang : String){
includeJS( IncludePaths.jQueryJS() )
includeJS("src/ace.js")
includeJS("src/ext-language_tools.js")
includeJS("src/mode-plain_text.js")
includeJS("src/mode-" + lang + ".js")
includeJS("src/theme-eclipse.js")
includeJS("initACE.js")
}
define ace(code: Ref<Text>, lang : String){
ace(code, lang, id, false)
}
define ace(code: Ref<Text>, lang : String, readonly: Bool){
ace(code, lang, id, readonly)
}
define ace(code: Ref<Text>, lang : String, idAttr: String){
ace(code, lang, idAttr, false)
}
define aceView(code: Ref<Text>, lang : String){
ace(code, lang, id, true)
}
define aceView(code: Ref<Text>, lang : String, idAttr: String){
ace(code, lang, idAttr, true)
}
// 619px; height: 500px
// todo: adapt size to window document.documentElement.clientWidth
define ace(code: Ref<Text>, lang : String, idAttr: String, readonly: Bool){
var normalizedLang := getAceLanguageId(lang)
var req := getRequestParameter( idAttr )
requireACE(normalizedLang)
div[class="editorContainer"]{
div[class="aceEditor", id="aceEditor_" + idAttr,
style="border: 1px solid #999;"
]{
//In case of a validation error, render the submitted text
if(req != null){
text(req)
} else {
output(code)
}
}
}
inputTextInternal(code, idAttr)[style="display:none",id=idAttr]
<script>
$(document).ready( function(){
createAceEditor('~idAttr', ~readonly, '~normalizedLang')
} );
</script>
}
template inputAce(code : Ref<String>, aceLang: String){
var req := getRequestParameter( id )
requireACE(aceLang)
div[class="ace-single-line", id="aceEditor_"+id, style="border: 1px solid #999;"]{
//In case of a validation error, render the submitted text
if(req != null){
output(req)
} else {
output(code)
}
}
inputStringInternal(code, id)[style="display:none"]
<script>
var el = document.getElementById("aceEditor_~id")
var editor = ace.edit(el);
editor.setOptions({
maxLines: 1, // make it 1 line
autoScrollEditorIntoView: true,
highlightActiveLine: false,
printMargin: false,
showGutter: false,
mode: "ace/mode/~aceLang",
theme: "ace/theme/tomorrow"
});
// remove newlines in pasted text
editor.on("paste", function(e){
e.text = e.text.replace(/[\r\n]+/g, " ");
});
var textinput = document.getElementsByName("~id")[0];
editor.getSession().on('change', function(){
textinput.value = editor.getSession().getValue();
});
// make mouse position clipping nicer
editor.renderer.screenToTextCoordinates = function(x, y){
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// disable Enter Shift-Enter keys
editor.commands.bindKey("Enter|Shift-Enter", "null")
</script>
}
function getAceLanguageId( language: String ): String {
case( language.toLowerCase() ){
"scala" { return "scala"; }
"scala212" { return "scala"; }
"java" { return "java"; }
"java8" { return "java"; }
"c" { return "c_cpp"; }
"js" { return "javascript"; }
"sql" { return "sql"; }
"css" { return "css"; }
"html" { return "html"; }
"python" { return "python"; }
"python2" { return "python"; }
"python3" { return "python"; }
default { return language.toLowerCase(); }
}
}