AbstractTextResourceEditorBuilder.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 EditorEvents = require("../../editor/EditorEvents");
  23. export abstract class AbstractTextResourceEditorBuilder implements Editor.Extensions.ResourceEditorBuilder {
  24. abstract canHandleResource(resourcePath: string): boolean;
  25. /**
  26. * Full path is the fully qualified path from the root of the filesystem. In order to take advantage
  27. * of the resource caching system, let's trim it down to just the path inside the resources directory
  28. * including the Resources directory so that the casing is correct
  29. */
  30. private getNormalizedPath(path: string) {
  31. const RESOURCES_MARKER = "resources/";
  32. return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
  33. }
  34. getEditor(resourceFrame: Atomic.UIWidget, resourcePath: string, tabContainer: Atomic.UITabContainer): Editor.ResourceEditor {
  35. const editor = new Editor.JSResourceEditor(resourcePath, tabContainer);
  36. // one time subscriptions waiting for the web view to finish loading. This event
  37. // actually hits the editor instance before we can hook it, so listen to it on the
  38. // frame and then unhook it
  39. editor.subscribeToEvent(EditorEvents.WebViewLoadEnd, (data) => {
  40. editor.unsubscribeFromEvent(EditorEvents.WebViewLoadEnd);
  41. this.loadCode(<Editor.JSResourceEditor>editor, resourcePath);
  42. });
  43. // Cannot subscribe to WebMessage until the .ts side can return a Handler.Success() message
  44. // editor.subscribeToEvent(EditorEvents.WebMessage, (data) => {
  45. // console.log("editor Got a web message: " + data.request);
  46. // for (let x in data) {
  47. // console.log(x + ":" + data[x]);
  48. // }
  49. // const request = JSON.parse(data.request);
  50. // switch (request.message) {
  51. // case "editorGetUserPrefs":
  52. // this.getUserPrefs(editor);
  53. // return true;
  54. // }
  55. // });
  56. editor.subscribeToEvent(EditorEvents.ProjectUnloadedNotification, (data) => {
  57. const webClient = editor.webView.webClient;
  58. webClient.executeJavaScript("HOST_projectUnloaded();");
  59. });
  60. editor.subscribeToEvent(EditorEvents.DeleteResourceNotification, (data) => {
  61. const webClient = editor.webView.webClient;
  62. webClient.executeJavaScript(`HOST_resourceDeleted("atomic://${this.getNormalizedPath(editor.fullPath)}");`);
  63. });
  64. editor.subscribeToEvent(EditorEvents.UserPreferencesChangedNotification, (data) => {
  65. this.getUserPrefs(editor);
  66. });
  67. return editor;
  68. }
  69. /**
  70. * Send the url of the user prefs to the web view so that it can request it
  71. */
  72. getUserPrefs(editor: Editor.JSResourceEditor) {
  73. let prefsPath = ToolCore.toolSystem.project.userPrefsFullPath;
  74. if (Atomic.fileSystem.fileExists(prefsPath)) {
  75. // Get a reference to the web client so we can call the load preferences method
  76. const webClient = editor.webView.webClient;
  77. webClient.executeJavaScript(`HOST_loadPreferences("atomic://${prefsPath}");`);
  78. }
  79. }
  80. /**
  81. * Send the url of the code to the web view so that it can request it
  82. */
  83. loadCode(editor: Editor.JSResourceEditor, resourcePath: string) {
  84. const webClient = editor.webView.webClient;
  85. webClient.executeJavaScript(`HOST_loadCode("atomic://${this.getNormalizedPath(editor.fullPath)}");`);
  86. }
  87. }