editorCommands.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 theme
  32. editor.setTheme("ace/theme/monokai");
  33. // set a default mode
  34. editor.session.setMode("ace/mode/javascript");
  35. // give the language extensions the opportunity to configure the editor based upon the file type
  36. serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
  37. fileExt: fileExt,
  38. filename: filename,
  39. editor: editor
  40. });
  41. }
  42. /**
  43. * Returns the text in the editor instance
  44. * @return {string}
  45. */
  46. export function getSourceText() : string {
  47. return editor.session.getValue();
  48. }
  49. /**
  50. * Loads a file of code into the editor and wires up the change events
  51. * @param {string} code
  52. * @param {string} filename
  53. * @param {string} fileExt
  54. */
  55. export function loadCodeIntoEditor(code: string, filename: string, fileExt: string) {
  56. editor.session.setValue(code);
  57. editor.gotoLine(0);
  58. editor.getSession().on("change", function(e) {
  59. HostInterop.getInstance().notifyEditorChange();
  60. });
  61. serviceLocator.sendEvent(ClientExtensionEventNames.CodeLoadedEvent, {
  62. code: code,
  63. filename: filename,
  64. fileExt: fileExt,
  65. editor: editor
  66. });
  67. }
  68. /**
  69. * Called when a resource is getting renamed
  70. * @param {string} path
  71. * @param {string} newPath
  72. */
  73. export function resourceRenamed(path: string, newPath: string) {
  74. let data:Editor.EditorEvents.RenameResourceEvent = {
  75. path: path,
  76. newPath: newPath
  77. };
  78. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  79. }
  80. /**
  81. * Called when a resource is getting deleted
  82. * @param {string} path
  83. */
  84. export function resourceDeleted(path: string) {
  85. let data:Editor.EditorEvents.DeleteResourceEvent = {
  86. path: path
  87. };
  88. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  89. }
  90. /**
  91. * Called when a resource is saved
  92. * @param {string} path
  93. * @param {string} fileExt
  94. * @param {string} contents
  95. */
  96. export function codeSaved(path: string, fileExt: string, contents: string) {
  97. let data:Editor.EditorEvents.CodeSavedEvent = {
  98. filename: path,
  99. fileExt: fileExt,
  100. editor: editor,
  101. code: contents
  102. };
  103. serviceLocator.sendEvent(ClientExtensionEventNames.CodeSavedEvent, data);
  104. }
  105. /**
  106. * Called when new preferences are available (or initially with current prefs)
  107. * @param {any} prefs
  108. */
  109. export function loadPreferences(prefs: any) {
  110. serviceLocator.clientServices.setPreferences(prefs);
  111. serviceLocator.sendEvent(ClientExtensionEventNames.PreferencesChangedEvent, null);
  112. }