AbstractTextResourceEditorBuilder.ts 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.DeleteResourceNotification, (data) => {
  57. const webClient = editor.webView.webClient;
  58. webClient.executeJavaScript(`HOST_resourceDeleted("atomic://${this.getNormalizedPath(data.path)}");`);
  59. });
  60. editor.subscribeToEvent(EditorEvents.UserPreferencesChangedNotification, (data) => {
  61. this.getUserPrefs(editor);
  62. });
  63. return editor;
  64. }
  65. /**
  66. * Send the url of the user prefs to the web view so that it can request it
  67. */
  68. getUserPrefs(editor: Editor.JSResourceEditor) {
  69. let prefsPath = ToolCore.toolSystem.project.userPrefsFullPath;
  70. if (Atomic.fileSystem.fileExists(prefsPath)) {
  71. // Get a reference to the web client so we can call the load preferences method
  72. const webClient = editor.webView.webClient;
  73. webClient.executeJavaScript(`HOST_loadPreferences("atomic://${prefsPath}");`);
  74. }
  75. }
  76. /**
  77. * Send the url of the code to the web view so that it can request it
  78. */
  79. loadCode(editor: Editor.JSResourceEditor, resourcePath: string) {
  80. const webClient = editor.webView.webClient;
  81. webClient.executeJavaScript(`HOST_loadCode("atomic://${this.getNormalizedPath(editor.fullPath)}");`);
  82. }
  83. }