workspace.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. // This code was adapted from Takashi Yamamiya's Javascript Workspace,
  23. // which you can visit at http://metatoys.org/propella/js/workspace.js
  24. // ---------------------------------------------------------------------------------------------------------------------------
  25. // INSTRUCTIONS: a client page must (1) include something along these lines:
  26. /*
  27. <body onLoad="$('workspaceForm').onkeydown=onShortCutKey">
  28. ...
  29. <form id=workspaceForm>
  30. <b>Source</b><br>
  31. <textarea cols=132 rows=24 name=source>...</textarea><br>
  32. <input type=button value="print it (ctrl+p)" onClick="printIt()">
  33. <input type=button value="do it (ctrl+d)" onClick="doIt()">
  34. <br><br>
  35. <b>Translation</b>
  36. <br>
  37. <div id=translation>
  38. <textarea cols=132 rows=4 name=translation>
  39. </textArea>
  40. </div>
  41. <b>Transcript</b>
  42. <br>
  43. <div id=transcript>
  44. <textarea cols=132 rows=4 name=transcript></textArea>
  45. </div>
  46. </form>
  47. ...
  48. </body>
  49. */
  50. // and (2) define its own translateCode function
  51. function translateCode(s) {
  52. //throw { errorPos: 0 }
  53. return s
  54. }
  55. // ---------------------------------------------------------------------------------------------------------------------------
  56. /* event handler for short cut key */
  57. function onShortCutKey(evt) {
  58. evt = evt ? evt : window.event;
  59. if (!evt)
  60. return undefined;
  61. if (!(evt.altKey || evt.ctrlKey || evt.metaKey))
  62. return true;
  63. var charCode = evt.charCode ? evt.charCode : evt.keyCode
  64. try {
  65. var handledIt = true
  66. switch (charCode) {
  67. case 68: doIt(); break
  68. case 80: printIt(); break
  69. case 83: saveIt(); break
  70. default: handledIt = false; return true
  71. }
  72. }
  73. finally {
  74. if (handledIt) {
  75. if (evt.preventDefault) {
  76. evt.preventDefault()
  77. evt.stopPropagation()
  78. }
  79. else {
  80. evt.returnValue = false
  81. evt.cancelBubble = true
  82. }
  83. }
  84. }
  85. return false
  86. }
  87. function printIt() {
  88. var result = evalSelection()
  89. if (!result)
  90. return
  91. var editor = result.source.editor,
  92. end = result.source.end,
  93. head = editor.value.substring(0, end),
  94. tail = editor.value.substring(end),
  95. oldScrollTop = editor.scrollTop
  96. editor.value = head + result.result + tail;
  97. editor.scrollTop = oldScrollTop
  98. setCaretSelection(editor, end, head.length + result.result.length)
  99. }
  100. function doIt() {
  101. var result = evalSelection()
  102. if (result)
  103. result.source.editor.focus()
  104. }
  105. function saveIt() { }
  106. /* Get selection of textarea */
  107. function getCaretSelection(field) {
  108. field.focus();
  109. var result = { start: 0, end: 0 };
  110. // IE support based on http://groups.drupal.org/node/1210
  111. if(typeof $('workspaceForm').source.selectionEnd == "undefined") {
  112. var range = document.selection.createRange(),
  113. rangeCopy = range.duplicate()
  114. rangeCopy.moveToElementText(field)
  115. rangeCopy.setEndPoint( 'EndToEnd', range )
  116. result.start = rangeCopy.text.length - range.text.length
  117. result.end = result.start + range.text.length
  118. }
  119. else {
  120. result.start = field.selectionStart
  121. result.end = field.selectionEnd
  122. }
  123. return result
  124. }
  125. /* Set selection of textarea */
  126. function setCaretSelection(field, start, end) {
  127. field.focus()
  128. // IE
  129. if(typeof $('workspaceForm').source.selectionEnd == "undefined") {
  130. var range = field.createTextRange()
  131. range.expand("textedit")
  132. var dStart = start - (field.value.substring(0, start).split("\n").length - 1),
  133. dEnd = end - field.value.length + field.value.substring(end + 1).split("\n").length - 1
  134. range.moveStart("character", dStart)
  135. range.moveEnd("character", dEnd)
  136. range.select()
  137. }
  138. else {
  139. field.selectionStart = start
  140. field.selectionEnd = end
  141. }
  142. }
  143. /* Get expression from textarea */
  144. function getSource() {
  145. var editor = $('workspaceForm').source,
  146. selection = getCaretSelection(editor),
  147. start = selection.start,
  148. end = selection.end,
  149. text = editor.value.substring(start, end)
  150. if (start == end) {
  151. var alltext = editor.value, index = 0
  152. if (start > 0 && alltext.charAt(start) == "\n")
  153. start--
  154. while (start > 0 && alltext.charAt(start) != "\n")
  155. start--
  156. if (alltext.charAt(start) == "\n")
  157. start++
  158. while (end < alltext.length && alltext.charAt(end) != "\r" && alltext.charAt(end) != "\n")
  159. end++
  160. text = alltext.substring(start, end);
  161. setCaretSelection(editor, start, end);
  162. }
  163. return {
  164. editor: editor,
  165. start: start,
  166. end: end,
  167. text: text
  168. }
  169. }
  170. function evalSelection() {
  171. var source = getSource()
  172. try { $('workspaceForm').translation.value = translateCode(source.text) }
  173. catch (e) {
  174. if (e.errorPos != undefined) {
  175. var errorPos = source.start + e.errorPos
  176. errorMsg = " Parse error ->",
  177. oldScrollTop = $('workspaceForm').source.scrollTop
  178. $('workspaceForm').source.value = $('workspaceForm').source.value.substring(0, errorPos) + errorMsg +
  179. $('workspaceForm').source.value.substring(errorPos, $('workspaceForm').source.value.length)
  180. $('workspaceForm').source.scrollTop = oldScrollTop
  181. setCaretSelection($('workspaceForm').source, errorPos, errorPos + errorMsg.length)
  182. }
  183. return undefined
  184. }
  185. try {
  186. return {
  187. source: source,
  188. result: " " + eval($('workspaceForm').translation.value)
  189. }
  190. } catch (e) {
  191. alert("Oops!\n\n" + e)
  192. throw e
  193. }
  194. }
  195. Transcript = {
  196. show: function(x) {
  197. $('workspaceForm').transcript.value = $('workspaceForm').transcript.value + x + "\n"
  198. $('workspaceForm').transcript.scrollTop = $('workspaceForm').transcript.scrollHeight
  199. },
  200. clear: function() {
  201. $('workspaceForm').transcript.value = ""
  202. }
  203. }