AbstractTextResourceEditorBuilder.ts 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /**
  35. * Returns the URL to load into the web view. Override this to return a custom url
  36. * @return {string}
  37. */
  38. getEditorUrl(): string {
  39. return `atomic://${ToolCore.toolEnvironment.toolDataDir}CodeEditor/Editor.html`;
  40. }
  41. getEditor(resourceFrame: Atomic.UIWidget, resourcePath: string, tabContainer: Atomic.UITabContainer): Editor.ResourceEditor {
  42. const editor = new Editor.JSResourceEditor(resourcePath, tabContainer, this.getEditorUrl());
  43. // one time subscriptions waiting for the web view to finish loading. This event
  44. // actually hits the editor instance before we can hook it, so listen to it on the
  45. // frame and then unhook it
  46. editor.subscribeToEvent(EditorEvents.WebViewLoadEnd, (data) => {
  47. editor.unsubscribeFromEvent(EditorEvents.WebViewLoadEnd);
  48. this.loadCode(<Editor.JSResourceEditor>editor, resourcePath);
  49. });
  50. // Cannot subscribe to WebMessage until the .ts side can return a Handler.Success() message
  51. // editor.subscribeToEvent(EditorEvents.WebMessage, (data) => {
  52. // console.log("editor Got a web message: " + data.request);
  53. // for (let x in data) {
  54. // console.log(x + ":" + data[x]);
  55. // }
  56. // const request = JSON.parse(data.request);
  57. // switch (request.message) {
  58. // case "editorGetUserPrefs":
  59. // this.getUserPrefs(editor);
  60. // return true;
  61. // }
  62. // });
  63. editor.subscribeToEvent(EditorEvents.DeleteResourceNotification, (data) => {
  64. const webClient = editor.webView.webClient;
  65. webClient.executeJavaScript(`HOST_resourceDeleted("atomic://${this.getNormalizedPath(data.path)}");`);
  66. });
  67. editor.subscribeToEvent(EditorEvents.UserPreferencesChangedNotification, (data) => {
  68. const webClient = editor.webView.webClient;
  69. webClient.executeJavaScript("HOST_preferencesChanged();");
  70. });
  71. return editor;
  72. }
  73. /**
  74. * Send the url of the code to the web view so that it can request it
  75. */
  76. loadCode(editor: Editor.JSResourceEditor, resourcePath: string) {
  77. const webClient = editor.webView.webClient;
  78. webClient.executeJavaScript(`HOST_loadCode("atomic://${this.getNormalizedPath(editor.fullPath)}");`);
  79. }
  80. }