editorCommands.ts 5.2 KB

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