editorCommands.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as internalEditor from "./editor";
  23. import serviceLocator from "../clientExtensions/ServiceLocator";
  24. import HostInterop from "../interop";
  25. import ClientExtensionEventNames from "../clientExtensions/ClientExtensionEventNames";
  26. import DuktapeDebugger from "../debugger/DuktapeDebugger";
  27. /**
  28. * Set the editor theme and configuration based upon the file extension
  29. * @param {string} fileExt
  30. */
  31. export function configure(fileExt: string, filename: string) {
  32. let monacoEditor = <monaco.editor.IStandaloneCodeEditor>internalEditor.getInternalEditor();
  33. updateEditorPrefs();
  34. // give the language extensions the opportunity to configure the editor based upon the file type
  35. serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
  36. fileExt: fileExt,
  37. filename: filename,
  38. editor: monacoEditor
  39. });
  40. // Override CMD/CTRL+I since that is going to be used for Format Code and in the editor it is assigned to something else
  41. const noOpCommand: monaco.editor.ICommandHandler = () => { };
  42. monacoEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_I, noOpCommand, null);
  43. updateEditorPrefs();
  44. }
  45. /**
  46. * Update the editor prefs
  47. */
  48. export function updateEditorPrefs() {
  49. // converter to handle new version of the renderWhitespace setting
  50. const renderWhitespaceAdapter = (setting): "none" | "boundary" | "all" => {
  51. switch (setting.toLowerCase()) {
  52. case "true": return "all";
  53. case "false": return "none";
  54. default: return setting;
  55. }
  56. };
  57. let monacoEditor = <monaco.editor.IStandaloneCodeEditor>internalEditor.getInternalEditor();
  58. monacoEditor.updateOptions({
  59. theme: serviceLocator.clientServices.getApplicationPreference("codeEditor", "theme", "vs-dark"),
  60. renderWhitespace: renderWhitespaceAdapter(serviceLocator.clientServices.getApplicationPreference("codeEditor", "showInvisibles", "none")),
  61. mouseWheelScrollSensitivity: 2,
  62. fontSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontSize", 12),
  63. fontFamily: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontFamily", "")
  64. });
  65. }
  66. /**
  67. * Returns the text in the editor instance
  68. * @return {string}
  69. */
  70. export function getSourceText(): string {
  71. return internalEditor.getInternalEditor().getModel().getValue();
  72. }
  73. /**
  74. * Loads a file of code into the editor and wires up the change events
  75. * @param {string} code
  76. * @param {string} filename
  77. * @param {string} fileExt
  78. */
  79. export function loadCodeIntoEditor(code: string, filename: string, fileExt: string) {
  80. let monacoEditor = internalEditor.getInternalEditor();
  81. let model = monaco.editor.createModel(code, null, monaco.Uri.parse(filename));
  82. model.updateOptions({
  83. insertSpaces: serviceLocator.clientServices.getApplicationPreference("codeEditor", "useSoftTabs", true),
  84. tabSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "tabSize", 4)
  85. });
  86. monacoEditor.setModel(model);
  87. serviceLocator.sendEvent(ClientExtensionEventNames.CodeLoadedEvent, {
  88. code: code,
  89. filename: filename,
  90. fileExt: fileExt,
  91. editor: monacoEditor
  92. });
  93. monacoEditor.onDidChangeModelContent((listener) => {
  94. HostInterop.getInstance().notifyEditorChange();
  95. });
  96. }
  97. /**
  98. * Called when a resource is getting renamed
  99. * @param {string} path
  100. * @param {string} newPath
  101. */
  102. export function resourceRenamed(path: string, newPath: string) {
  103. let data: Editor.ClientExtensions.RenameResourceEvent = {
  104. path: path,
  105. newPath: newPath
  106. };
  107. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  108. }
  109. /**
  110. * Called when a resource is getting deleted
  111. * @param {string} path
  112. */
  113. export function resourceDeleted(path: string) {
  114. let data: Editor.ClientExtensions.DeleteResourceEvent = {
  115. path: path
  116. };
  117. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  118. }
  119. /**
  120. * Called when a resource is saved
  121. * @param {string} path
  122. * @param {string} fileExt
  123. * @param {string} contents
  124. */
  125. export function codeSaved(path: string, fileExt: string, contents: string) {
  126. let data: Editor.ClientExtensions.CodeSavedEvent = {
  127. filename: path,
  128. fileExt: fileExt,
  129. editor: internalEditor.getInternalEditor(),
  130. code: contents
  131. };
  132. serviceLocator.sendEvent(ClientExtensionEventNames.CodeSavedEvent, data);
  133. }
  134. /**
  135. * Called when the editor has finished it's initial load
  136. */
  137. export function editorLoaded() {
  138. // let's grab the prefs and seed the service locator
  139. serviceLocator.clientServices.setPreferences(JSON.parse(window.HOST_Preferences.ProjectPreferences), JSON.parse(window.HOST_Preferences.ApplicationPreferences));
  140. }
  141. /**
  142. * Called when new preferences are available (or initially with current prefs)
  143. */
  144. export function preferencesChanged(prefs: Editor.ClientExtensions.PreferencesChangedEventData) {
  145. serviceLocator.clientServices.setPreferences(prefs.projectPreferences, prefs.applicationPreferences);
  146. updateEditorPrefs();
  147. serviceLocator.sendEvent(ClientExtensionEventNames.PreferencesChangedEvent, prefs);
  148. }
  149. export function setEditor(editor: any) {
  150. internalEditor.setInternalEditor(editor);
  151. }
  152. export function setDebuggerEditor(editor: any) {
  153. internalEditor.setInternalEditor(editor);
  154. const dbg = new DuktapeDebugger();
  155. dbg.initialize(editor);
  156. }
  157. /**
  158. * Called when a resource is getting deleted
  159. * @param {string} path
  160. */
  161. export function formatCode() {
  162. serviceLocator.sendEvent(ClientExtensionEventNames.FormatCodeEvent, null);
  163. }
  164. /**
  165. * Called when the editor should respond to a host shortcut command
  166. */
  167. export function invokeShortcut(shortcut: Editor.EditorShortcutType) {
  168. const ed = internalEditor.getInternalEditor();
  169. ed.focus();
  170. switch (shortcut) {
  171. case "cut":
  172. case "copy":
  173. case "paste":
  174. window.document.execCommand(shortcut);
  175. break;
  176. case "selectall":
  177. ed.setSelection(ed.getModel().getFullModelRange());
  178. break;
  179. }
  180. }
  181. /**
  182. * Selects the provided line number
  183. */
  184. export function gotoLineNumber(lineNumber:number) {
  185. const ed = internalEditor.getInternalEditor();
  186. ed.revealLineInCenterIfOutsideViewport(lineNumber);
  187. ed.setPosition(new monaco.Position(lineNumber, 0));
  188. }
  189. /**
  190. * Selects the provided position
  191. */
  192. export function gotoTokenPos(tokenPos:number) {
  193. const ed = internalEditor.getInternalEditor();
  194. const pos = ed.getModel().getPositionAt(tokenPos);
  195. ed.revealPositionInCenterIfOutsideViewport(pos);
  196. ed.setPosition(pos);
  197. }