AbstractTextResourceEditorBuilder.ts 4.3 KB

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