editorCommands.ts 5.3 KB

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