editorCommands.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  54. /**
  55. * Returns the text in the editor instance
  56. * @return {string}
  57. */
  58. export function getSourceText(): string {
  59. return internalEditor.getInternalEditor().getModel().getValue();
  60. }
  61. /**
  62. * Loads a file of code into the editor and wires up the change events
  63. * @param {string} code
  64. * @param {string} filename
  65. * @param {string} fileExt
  66. */
  67. export function loadCodeIntoEditor(code: string, filename: string, fileExt: string) {
  68. let monacoEditor = internalEditor.getInternalEditor();
  69. let model = monaco.editor.createModel(code, null, monaco.Uri.parse(filename));
  70. model.updateOptions({
  71. insertSpaces: serviceLocator.clientServices.getApplicationPreference("codeEditor", "useSoftTabs", true),
  72. tabSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "tabSize", 4)
  73. });
  74. monacoEditor.setModel(model);
  75. serviceLocator.sendEvent(ClientExtensionEventNames.CodeLoadedEvent, {
  76. code: code,
  77. filename: filename,
  78. fileExt: fileExt,
  79. editor: monacoEditor
  80. });
  81. monacoEditor.onDidChangeModelContent((listener) => {
  82. HostInterop.getInstance().notifyEditorChange();
  83. });
  84. }
  85. /**
  86. * Called when a resource is getting renamed
  87. * @param {string} path
  88. * @param {string} newPath
  89. */
  90. export function resourceRenamed(path: string, newPath: string) {
  91. let data: Editor.EditorEvents.RenameResourceEvent = {
  92. path: path,
  93. newPath: newPath
  94. };
  95. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  96. }
  97. /**
  98. * Called when a resource is getting deleted
  99. * @param {string} path
  100. */
  101. export function resourceDeleted(path: string) {
  102. let data: Editor.EditorEvents.DeleteResourceEvent = {
  103. path: path
  104. };
  105. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  106. }
  107. /**
  108. * Called when a resource is saved
  109. * @param {string} path
  110. * @param {string} fileExt
  111. * @param {string} contents
  112. */
  113. export function codeSaved(path: string, fileExt: string, contents: string) {
  114. let data: Editor.EditorEvents.CodeSavedEvent = {
  115. filename: path,
  116. fileExt: fileExt,
  117. editor: internalEditor.getInternalEditor(),
  118. code: contents
  119. };
  120. serviceLocator.sendEvent(ClientExtensionEventNames.CodeSavedEvent, data);
  121. }
  122. /**
  123. * Called when the editor has finished it's initial load
  124. */
  125. export function editorLoaded() {
  126. // let's grab the prefs and seed the service locator
  127. serviceLocator.clientServices.setPreferences(JSON.parse(window.HOST_Preferences.ProjectPreferences), JSON.parse(window.HOST_Preferences.ApplicationPreferences));
  128. }
  129. /**
  130. * Called when new preferences are available (or initially with current prefs)
  131. */
  132. export function preferencesChanged(prefs: Editor.ClientExtensions.PreferencesChangedEventData) {
  133. serviceLocator.clientServices.setPreferences(prefs.projectPreferences, prefs.applicationPreferences);
  134. serviceLocator.sendEvent(ClientExtensionEventNames.PreferencesChangedEvent, prefs);
  135. }
  136. export function setEditor(editor: any) {
  137. internalEditor.setInternalEditor(editor);
  138. }