-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo.html
213 lines (192 loc) · 8.09 KB
/
demo.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tern.Ace demo</title>
<style type="text/css" media="screen">
body {
overflow: hidden;
background-color: #EFEFEF;
}
#editor {
margin: 0;
position: absolute;
top: 0;
bottom: 0;
right: 320px;
left: 0;
border-right: 1px solid black;
}
#info {
position: absolute;
top: 30px;
bottom: 10px;
right: 10px;
width: 300px;
font-size: 12px;
}
.hotkeys td {
vertical-align: top;
}
.hotkeys tr td:nth-child(2) {
padding-left: 7px;
}
h3 {
margin-top: 20px;
margin-bottom: 3px
}
</style>
</head>
<body>
<pre id="editor"></pre>
<div id="info" style="overflow:auto;">
<a href="https://github.com/sevin7676/Ace.Tern" target="_blank">https://github.com/sevin7676/Ace.Tern</a>
<br/>
<br/>Mode:
<select id="modeSelect" onchange="window.location.hash=this.value; setModeFromHash();">
<option>javascript</option>
<option>html</option>
</select>
<h3>Demo Hot Keys</h3>
<table class="hotkeys">
<tr style="font-weight:bold;">
<td>Key</td>
<td>Description</td>
</tr>
<tr>
<td>Ctrl+Space</td>
<td>Start Auto Complete (also trigger automtically after typing '.')</td>
</tr>
<tr>
<td>Ctrl+I</td>
<td>Show type</td>
</tr>
<tr>
<td>Alt+.</td>
<td>Jump to definition</td>
</tr>
<tr>
<td>Alt+,</td>
<td>Jump back</td>
</tr>
<tr>
<td>Ctrl+E</td>
<td>Find references (popup posistion has bug)</td>
</tr>
<tr>
<td>Ctrl+Shift+E</td>
<td>Rename (popup posistion has bug)</td>
</tr>
</table>
<div style="font-style:italic; margin-top:20px;">Add 'noworker' to querystring to make this demo run without a web worker
<br/>
<br/>
Last updated 2017.02.08 10:53
</div>
</div>
<!-- load ace -->
<script src="ace-builds/src-noconflict/ace.js"></script>
<script>
// set to false to prevent using worker, which is needed to run this from local html file due to browser security restritions
var useWebWorker = window.location.search.toLowerCase().indexOf('noworker') == -1;
//create editor
var editor = ace.edit("editor");
editor.getSession().setUseWorker(useWebWorker);
setModeFromHash(); //set editor content for demo only
//#region not relevant to tern, just some deafults I prefer
editor.setTheme("ace/theme/chrome");
editor.getSession().setUseWrapMode(true);
editor.getSession().setWrapLimitRange(null, null);
editor.setShowPrintMargin(false);
editor.$blockScrolling = Infinity;//prevents ace from logging annoying warnings
//#endregion
ace.config.loadModule('ace/ext/tern', function () {
editor.setOptions({
/**
* Either `true` or `false` or to enable with custom options pass object that
* has options for tern server: http://ternjs.net/doc/manual.html#server_api
* If `true`, then default options will be used
*/
enableTern: {
/* http://ternjs.net/doc/manual.html#option_defs */
defs: ['browser', 'ecma5'],
/* http://ternjs.net/doc/manual.html#plugins */
plugins: {
doc_comment: {
fullDocs: true
}
},
/**
* (default is true) If web worker is used for tern server.
* This is recommended as it offers better performance, but prevents this from working in a local html file due to browser security restrictions
*/
useWorker: useWebWorker,
/* if your editor supports switching between different files (such as tabbed interface) then tern can do this when jump to defnition of function in another file is called, but you must tell tern what to execute in order to jump to the specified file */
switchToDoc: function (name, start) {
console.log('switchToDoc called but not defined. name=' + name + '; start=', start);
},
/**
* if passed, this function will be called once ternServer is started.
* This is needed when useWorker=false because the tern source files are loaded asynchronously before the server is started.
*/
startedCb: function () {
//once tern is enabled, it can be accessed via editor.ternServer
console.log('editor.ternServer:', editor.ternServer);
},
},
/**
* when using tern, it takes over Ace's built in snippets support.
* this setting affects all modes when using tern, not just javascript.
*/
enableSnippets: true,
/**
* when using tern, Ace's basic text auto completion is enabled still by deafult.
* This settings affects all modes when using tern, not just javascript.
* For javascript mode the basic auto completion will be added to completion results if tern fails to find completions or if you double tab the hotkey for get completion (default is ctrl+space, so hit ctrl+space twice rapidly to include basic text completions in the result)
*/
enableBasicAutocompletion: true,
});
});
//#region not relevant to tern (custom beautify plugin) and demo loading
ace.config.loadModule('ace/ext/html_beautify', function (beautify) {
editor.setOptions({
// beautify when closing bracket typed in javascript or css mode
autoBeautify: true,
// this enables the plugin to work with hotkeys (ctrl+b to beautify)
htmlBeautify: true,
});
//modify beautify options as needed:
window.beautifyOptions = beautify.options;
console.log('beautifyOptions:', beautifyOptions);
});
function GetFile(file, c) {
var xhr = new XMLHttpRequest();
xhr.open("get", file, true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) c(xhr.responseText, xhr.status);
};
}
function setModeFromHash() {
var available = [];
var modeSelect = document.getElementById('modeSelect');
for (var i = 0; i < modeSelect.options.length; i++) {
available.push(modeSelect.options[i].text);
}
var mode = window.location.hash.replace('#', '');
if (!mode || available.indexOf(mode) === -1) {
window.location.hash = modeSelect.value;
setModeFromHash();
return;
}
if (modeSelect.value != mode)
modeSelect.options[available.indexOf(mode)].selected = true;
editor.session.setMode("ace/mode/" + mode);
GetFile(mode == 'javascript' ? 'DemoJavascript.js' : 'demo.html', function (f) {
editor.setValue(f);
});
}
//#endregion
</script>
</body>
</html>