editorCommands.ts 5.7 KB

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