editorCommands.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 the project is getting unloaded
  70. */
  71. export function projectUnloaded() {
  72. serviceLocator.sendEvent(ClientExtensionEventNames.ProjectUnloadedEvent, null);
  73. }
  74. /**
  75. * Called when a resource is getting renamed
  76. * @param {string} path
  77. * @param {string} newPath
  78. */
  79. export function resourceRenamed(path: string, newPath: string) {
  80. let data:Editor.EditorEvents.RenameResourceEvent = {
  81. path: path,
  82. newPath: newPath
  83. };
  84. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  85. }
  86. /**
  87. * Called when a resource is getting deleted
  88. * @param {string} path
  89. */
  90. export function resourceDeleted(path: string) {
  91. let data:Editor.EditorEvents.DeleteResourceEvent = {
  92. path: path
  93. };
  94. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  95. }