editorCommands.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. let monacoEditor = <monaco.editor.IStandaloneCodeEditor>internalEditor.getInternalEditor();
  32. monacoEditor.updateOptions({
  33. theme: serviceLocator.clientServices.getApplicationPreference("codeEditor", "theme", "vs-dark"),
  34. renderWhitespace: serviceLocator.clientServices.getApplicationPreference("codeEditor", "showInvisibles", false),
  35. mouseWheelScrollSensitivity: 2,
  36. fontSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontSize", 12),
  37. fontFamily: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontFamily", "")
  38. });
  39. // give the language extensions the opportunity to configure the editor based upon the file type
  40. serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
  41. fileExt: fileExt,
  42. filename: filename,
  43. editor: monacoEditor
  44. });
  45. }
  46. /**
  47. * Returns the text in the editor instance
  48. * @return {string}
  49. */
  50. export function getSourceText(): string {
  51. return internalEditor.getInternalEditor().getModel().getValue();
  52. }
  53. /**
  54. * Loads a file of code into the editor and wires up the change events
  55. * @param {string} code
  56. * @param {string} filename
  57. * @param {string} fileExt
  58. */
  59. export function loadCodeIntoEditor(code: string, filename: string, fileExt: string) {
  60. let monacoEditor = internalEditor.getInternalEditor();
  61. let model = monaco.editor.createModel(code, null, monaco.Uri.file(filename));
  62. model.updateOptions({
  63. insertSpaces: serviceLocator.clientServices.getApplicationPreference("codeEditor", "useSoftTabs", true),
  64. tabSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "tabSize", 4)
  65. });
  66. monacoEditor.setModel(model);
  67. serviceLocator.sendEvent(ClientExtensionEventNames.CodeLoadedEvent, {
  68. code: code,
  69. filename: filename,
  70. fileExt: fileExt,
  71. editor: monacoEditor
  72. });
  73. monacoEditor.onDidChangeModelContent((listener) => {
  74. HostInterop.getInstance().notifyEditorChange();
  75. });
  76. }
  77. /**
  78. * Called when a resource is getting renamed
  79. * @param {string} path
  80. * @param {string} newPath
  81. */
  82. export function resourceRenamed(path: string, newPath: string) {
  83. let data: Editor.EditorEvents.RenameResourceEvent = {
  84. path: path,
  85. newPath: newPath
  86. };
  87. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  88. }
  89. /**
  90. * Called when a resource is getting deleted
  91. * @param {string} path
  92. */
  93. export function resourceDeleted(path: string) {
  94. let data: Editor.EditorEvents.DeleteResourceEvent = {
  95. path: path
  96. };
  97. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  98. }
  99. /**
  100. * Called when a resource is saved
  101. * @param {string} path
  102. * @param {string} fileExt
  103. * @param {string} contents
  104. */
  105. export function codeSaved(path: string, fileExt: string, contents: string) {
  106. let data: Editor.EditorEvents.CodeSavedEvent = {
  107. filename: path,
  108. fileExt: fileExt,
  109. editor: internalEditor.getInternalEditor(),
  110. code: contents
  111. };
  112. serviceLocator.sendEvent(ClientExtensionEventNames.CodeSavedEvent, data);
  113. }
  114. /**
  115. * Called when the editor has finished it's initial load
  116. */
  117. export function editorLoaded() {
  118. // let's grab the prefs and seed the service locator
  119. serviceLocator.clientServices.setPreferences(JSON.parse(window.HOST_Preferences.ProjectPreferences), JSON.parse(window.HOST_Preferences.ApplicationPreferences));
  120. }
  121. /**
  122. * Called when new preferences are available (or initially with current prefs)
  123. */
  124. export function preferencesChanged(prefs: Editor.ClientExtensions.PreferencesChangedEventData) {
  125. serviceLocator.clientServices.setPreferences(prefs.projectPreferences, prefs.applicationPreferences);
  126. serviceLocator.sendEvent(ClientExtensionEventNames.PreferencesChangedEvent, prefs);
  127. }
  128. export function setEditor(editor: any) {
  129. internalEditor.setInternalEditor(editor);
  130. }