forked from alexwarth/ometa-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace.js
198 lines (182 loc) · 5.84 KB
/
workspace.js
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
// This code was adapted from Takashi Yamamiya's Javascript Workspace,
// which you can visit at http://metatoys.org/propella/js/workspace.js
// ---------------------------------------------------------------------------------------------------------------------------
// INSTRUCTIONS: a client page must (1) include something along these lines:
/*
<body onLoad="$('workspaceForm').onkeydown=onShortCutKey">
...
<form id=workspaceForm>
<b>Source</b><br>
<textarea cols=132 rows=24 name=source>...</textarea><br>
<input type=button value="print it (ctrl+p)" onClick="printIt()">
<input type=button value="do it (ctrl+d)" onClick="doIt()">
<br><br>
<b>Translation</b>
<br>
<div id=translation>
<textarea cols=132 rows=4 name=translation>
</textArea>
</div>
<b>Transcript</b>
<br>
<div id=transcript>
<textarea cols=132 rows=4 name=transcript></textArea>
</div>
</form>
...
</body>
*/
// and (2) define its own translateCode function
function translateCode(s) {
//throw { errorPos: 0 }
return s
}
// ---------------------------------------------------------------------------------------------------------------------------
/* event handler for short cut key */
function onShortCutKey(evt) {
evt = evt ? evt : window.event;
if (!evt)
return undefined;
if (!(evt.altKey || evt.ctrlKey || evt.metaKey))
return true;
var charCode = evt.charCode ? evt.charCode : evt.keyCode
try {
var handledIt = true
switch (charCode) {
case 68: doIt(); break
case 80: printIt(); break
case 83: saveIt(); break
default: handledIt = false; return true
}
}
finally {
if (handledIt) {
if (evt.preventDefault) {
evt.preventDefault()
evt.stopPropagation()
}
else {
evt.returnValue = false
evt.cancelBubble = true
}
}
}
return false
}
function printIt() {
var result = evalSelection()
if (!result)
return
var editor = result.source.editor,
end = result.source.end,
head = editor.value.substring(0, end),
tail = editor.value.substring(end),
oldScrollTop = editor.scrollTop
editor.value = head + result.result + tail;
editor.scrollTop = oldScrollTop
setCaretSelection(editor, end, head.length + result.result.length)
}
function doIt() {
var result = evalSelection()
if (result)
result.source.editor.focus()
}
function saveIt() { }
/* Get selection of textarea */
function getCaretSelection(field) {
field.focus();
var result = { start: 0, end: 0 };
// IE support based on http://groups.drupal.org/node/1210
if(typeof $('workspaceForm').source.selectionEnd == "undefined") {
var range = document.selection.createRange(),
rangeCopy = range.duplicate()
rangeCopy.moveToElementText(field)
rangeCopy.setEndPoint( 'EndToEnd', range )
result.start = rangeCopy.text.length - range.text.length
result.end = result.start + range.text.length
}
else {
result.start = field.selectionStart
result.end = field.selectionEnd
}
return result
}
/* Set selection of textarea */
function setCaretSelection(field, start, end) {
field.focus()
// IE
if(typeof $('workspaceForm').source.selectionEnd == "undefined") {
var range = field.createTextRange()
range.expand("textedit")
var dStart = start - (field.value.substring(0, start).split("\n").length - 1),
dEnd = end - field.value.length + field.value.substring(end + 1).split("\n").length - 1
range.moveStart("character", dStart)
range.moveEnd("character", dEnd)
range.select()
}
else {
field.selectionStart = start
field.selectionEnd = end
}
}
/* Get expression from textarea */
function getSource() {
var editor = $('workspaceForm').source,
selection = getCaretSelection(editor),
start = selection.start,
end = selection.end,
text = editor.value.substring(start, end)
if (start == end) {
var alltext = editor.value, index = 0
if (start > 0 && alltext.charAt(start) == "\n")
start--
while (start > 0 && alltext.charAt(start) != "\n")
start--
if (alltext.charAt(start) == "\n")
start++
while (end < alltext.length && alltext.charAt(end) != "\r" && alltext.charAt(end) != "\n")
end++
text = alltext.substring(start, end);
setCaretSelection(editor, start, end);
}
return {
editor: editor,
start: start,
end: end,
text: text
}
}
function evalSelection() {
var source = getSource()
try { $('workspaceForm').translation.value = translateCode(source.text) }
catch (e) {
if (e.errorPos != undefined) {
var errorPos = source.start + e.errorPos
errorMsg = " Parse error ->",
oldScrollTop = $('workspaceForm').source.scrollTop
$('workspaceForm').source.value = $('workspaceForm').source.value.substring(0, errorPos) + errorMsg +
$('workspaceForm').source.value.substring(errorPos, $('workspaceForm').source.value.length)
$('workspaceForm').source.scrollTop = oldScrollTop
setCaretSelection($('workspaceForm').source, errorPos, errorPos + errorMsg.length)
}
return undefined
}
try {
return {
source: source,
result: " " + eval($('workspaceForm').translation.value)
}
} catch (e) {
alert("Oops!\n\n" + e)
throw e
}
}
Transcript = {
show: function(x) {
$('workspaceForm').transcript.value = $('workspaceForm').transcript.value + x + "\n"
$('workspaceForm').transcript.scrollTop = $('workspaceForm').transcript.scrollHeight
},
clear: function() {
$('workspaceForm').transcript.value = ""
}
}