ResourceEditorProvider.ts 3.9 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 JavascriptResourceEditorBuilder from "./resourceEditors/JavascriptResourceEditorBuilder";
  23. import JsonResourceEditorBuilder from "./resourceEditors/JsonResourceEditorBuilder";
  24. import Scene3dResourceEditorBuilder from "./resourceEditors/Scene3dResourceEditorBuilder";
  25. import TextFileResourceEditorBuilder from "./resourceEditors/TextFileResourceEditorBuilder";
  26. import TurboBadgerResourceEditorBuilder from "./resourceEditors/TurboBadgerResourceEditorBuilder";
  27. import TypescriptResourceEditorBuilder from "./resourceEditors/TypescriptResourceEditorBuilder";
  28. export default class ResourceEditorProvider {
  29. private standardEditorRegistry: Editor.Extensions.ResourceEditorBuilder[] = [];
  30. private customEditorRegistry: Editor.Extensions.ResourceEditorBuilder[] = [];
  31. private resourceFrame: Atomic.UIWidget;
  32. constructor(resourceFrame: Atomic.UIWidget) {
  33. this.resourceFrame = resourceFrame;
  34. }
  35. /**
  36. * Register an internal core editor.
  37. */
  38. registerStandardEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
  39. this.standardEditorRegistry.push(editorBuilder);
  40. }
  41. /**
  42. * Register a custom editor. These editors will override editors in the standard editor list if
  43. * they both resolve the ```canHandleResource``` call.
  44. */
  45. registerCustomEditor(editorBuilder) {
  46. this.customEditorRegistry.push(editorBuilder);
  47. }
  48. /**
  49. * Returns an editor for the provided resource type or null
  50. */
  51. getEditor(resourcePath: string, tabContainer) {
  52. let editorBuilder: Editor.Extensions.ResourceEditorBuilder;
  53. this.customEditorRegistry.forEach((builder) => {
  54. if (builder.canHandleResource(resourcePath)) {
  55. editorBuilder = builder;
  56. return;
  57. }
  58. });
  59. if (!editorBuilder) {
  60. this.standardEditorRegistry.forEach((builder) => {
  61. if (builder.canHandleResource(resourcePath)) {
  62. editorBuilder = builder;
  63. return;
  64. }
  65. });
  66. }
  67. if (editorBuilder) {
  68. return editorBuilder.getEditor(this.resourceFrame, resourcePath, tabContainer);
  69. } else {
  70. return null;
  71. }
  72. }
  73. /**
  74. * Loads the built-in standard editors
  75. */
  76. loadStandardEditors() {
  77. this.registerStandardEditor(new TextFileResourceEditorBuilder());
  78. this.registerStandardEditor(new JavascriptResourceEditorBuilder());
  79. this.registerStandardEditor(new JsonResourceEditorBuilder());
  80. this.registerStandardEditor(new TypescriptResourceEditorBuilder());
  81. this.registerStandardEditor(new Scene3dResourceEditorBuilder());
  82. // this overrides the test resource editor so need to put it in the custom bucket
  83. this.registerCustomEditor(new TurboBadgerResourceEditorBuilder());
  84. }
  85. }