editorCommands.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /**
  27. * Set the editor theme and configuration based upon the file extension
  28. * @param {string} fileExt
  29. */
  30. export function configure(fileExt: string, filename: string) {
  31. // converter to handle new version of the renderWhitespace setting
  32. const renderWhitespaceAdapter = (setting): "none" | "boundary" | "all" => {
  33. switch (setting.toLowerCase()) {
  34. case "true": return "all";
  35. case "false": return "none";
  36. default: return setting;
  37. }
  38. };
  39. let monacoEditor = <monaco.editor.IStandaloneCodeEditor>internalEditor.getInternalEditor();
  40. monacoEditor.updateOptions({
  41. theme: serviceLocator.clientServices.getApplicationPreference("codeEditor", "theme", "vs-dark"),
  42. renderWhitespace: renderWhitespaceAdapter(serviceLocator.clientServices.getApplicationPreference("codeEditor", "showInvisibles", "none")),
  43. mouseWheelScrollSensitivity: 2,
  44. fontSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontSize", 12),
  45. fontFamily: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontFamily", "")
  46. });
  47. // give the language extensions the opportunity to configure the editor based upon the file type
  48. serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
  49. fileExt: fileExt,
  50. filename: filename,
  51. editor: monacoEditor
  52. });
  53. // Override CMD/CTRL+I since that is going to be used for Format Code and in the editor it is assigned to something else
  54. const noOpCommand: monaco.editor.ICommandHandler = () => { };
  55. monacoEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_I, noOpCommand, null);
  56. }
  57. /**
  58. * Returns the text in the editor instance
  59. * @return {string}
  60. */
  61. export function getSourceText(): string {
  62. return internalEditor.getInternalEditor().getModel().getValue();
  63. }
  64. /**
  65. * Loads a file of code into the editor and wires up the change events
  66. * @param {string} code
  67. * @param {string} filename
  68. * @param {string} fileExt
  69. */
  70. export function loadCodeIntoEditor(code: string, filename: string, fileExt: string) {
  71. let monacoEditor = internalEditor.getInternalEditor();
  72. let model = monaco.editor.createModel(code, null, monaco.Uri.parse(filename));
  73. model.updateOptions({
  74. insertSpaces: serviceLocator.clientServices.getApplicationPreference("codeEditor", "useSoftTabs", true),
  75. tabSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "tabSize", 4)
  76. });
  77. monacoEditor.setModel(model);
  78. serviceLocator.sendEvent(ClientExtensionEventNames.CodeLoadedEvent, {
  79. code: code,
  80. filename: filename,
  81. fileExt: fileExt,
  82. editor: monacoEditor
  83. });
  84. monacoEditor.onDidChangeModelContent((listener) => {
  85. HostInterop.getInstance().notifyEditorChange();
  86. });
  87. }
  88. /**
  89. * Called when a resource is getting renamed
  90. * @param {string} path
  91. * @param {string} newPath
  92. */
  93. export function resourceRenamed(path: string, newPath: string) {
  94. let data: Editor.ClientExtensions.RenameResourceEvent = {
  95. path: path,
  96. newPath: newPath
  97. };
  98. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  99. }
  100. /**
  101. * Called when a resource is getting deleted
  102. * @param {string} path
  103. */
  104. export function resourceDeleted(path: string) {
  105. let data: Editor.ClientExtensions.DeleteResourceEvent = {
  106. path: path
  107. };
  108. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  109. }
  110. /**
  111. * Called when a resource is saved
  112. * @param {string} path
  113. * @param {string} fileExt
  114. * @param {string} contents
  115. */
  116. export function codeSaved(path: string, fileExt: string, contents: string) {
  117. let data: Editor.ClientExtensions.CodeSavedEvent = {
  118. filename: path,
  119. fileExt: fileExt,
  120. editor: internalEditor.getInternalEditor(),
  121. code: contents
  122. };
  123. serviceLocator.sendEvent(ClientExtensionEventNames.CodeSavedEvent, data);
  124. }
  125. /**
  126. * Called when the editor has finished it's initial load
  127. */
  128. export function editorLoaded() {
  129. // let's grab the prefs and seed the service locator
  130. serviceLocator.clientServices.setPreferences(JSON.parse(window.HOST_Preferences.ProjectPreferences), JSON.parse(window.HOST_Preferences.ApplicationPreferences));
  131. }
  132. /**
  133. * Called when new preferences are available (or initially with current prefs)
  134. */
  135. export function preferencesChanged(prefs: Editor.ClientExtensions.PreferencesChangedEventData) {
  136. serviceLocator.clientServices.setPreferences(prefs.projectPreferences, prefs.applicationPreferences);
  137. serviceLocator.sendEvent(ClientExtensionEventNames.PreferencesChangedEvent, prefs);
  138. }
  139. export function setEditor(editor: any) {
  140. internalEditor.setInternalEditor(editor);
  141. }
  142. /**
  143. * Called when a resource is getting deleted
  144. * @param {string} path
  145. */
  146. export function formatCode() {
  147. serviceLocator.sendEvent(ClientExtensionEventNames.FormatCodeEvent, null);
  148. }
  149. /**
  150. * Called when the editor should respond to a host shortcut command
  151. */
  152. export function invokeShortcut(shortcut: Editor.EditorShortcutType) {
  153. const ed = internalEditor.getInternalEditor();
  154. ed.focus();
  155. switch (shortcut) {
  156. case "cut":
  157. case "copy":
  158. case "paste":
  159. window.document.execCommand(shortcut);
  160. break;
  161. case "selectall":
  162. ed.setSelection(ed.getModel().getFullModelRange());
  163. break;
  164. }
  165. }