editorCommands.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import editor from "./editor";
  8. import serviceLocator from "../clientExtensions/ServiceLocator";
  9. import HostInterop from "../interop";
  10. import ClientExtensionEventNames from "../clientExtensions/ClientExtensionEventNames";
  11. /**
  12. * Set the editor theme and configuration based upon the file extension
  13. * @param {string} fileExt
  14. */
  15. export function configure(fileExt: string, filename: string) {
  16. // set a default theme
  17. editor.setTheme("ace/theme/monokai");
  18. // set a default mode
  19. editor.session.setMode("ace/mode/javascript");
  20. // give the language extensions the opportunity to configure the editor based upon the file type
  21. serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
  22. fileExt: fileExt,
  23. filename: filename,
  24. editor: editor
  25. });
  26. }
  27. /**
  28. * Returns the text in the editor instance
  29. * @return {string}
  30. */
  31. export function getSourceText() : string {
  32. return editor.session.getValue();
  33. }
  34. /**
  35. * Loads a file of code into the editor and wires up the change events
  36. * @param {string} code
  37. * @param {string} filename
  38. * @param {string} fileExt
  39. */
  40. export function loadCodeIntoEditor(code: string, filename: string, fileExt: string) {
  41. editor.session.setValue(code);
  42. editor.gotoLine(0);
  43. editor.getSession().on("change", function(e) {
  44. HostInterop.getInstance().notifyEditorChange();
  45. });
  46. serviceLocator.sendEvent(ClientExtensionEventNames.CodeLoadedEvent, {
  47. code: code,
  48. filename: filename,
  49. fileExt: fileExt,
  50. editor: editor
  51. });
  52. }
  53. /**
  54. * Called when the project is getting unloaded
  55. */
  56. export function projectUnloaded() {
  57. serviceLocator.sendEvent(ClientExtensionEventNames.ProjectUnloadedEvent, null);
  58. }
  59. /**
  60. * Called when a resource is getting renamed
  61. * @param {string} path
  62. * @param {string} newPath
  63. */
  64. export function resourceRenamed(path: string, newPath: string) {
  65. let data:Editor.EditorEvents.RenameResourceEvent = {
  66. path: path,
  67. newPath: newPath
  68. };
  69. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceRenamedEvent, data);
  70. }
  71. /**
  72. * Called when a resource is getting deleted
  73. * @param {string} path
  74. */
  75. export function resourceDeleted(path: string) {
  76. let data:Editor.EditorEvents.DeleteResourceEvent = {
  77. path: path
  78. };
  79. serviceLocator.sendEvent(ClientExtensionEventNames.ResourceDeletedEvent, data);
  80. }