ClientExtensionServices.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 ClientExtensionEventNames from "./ClientExtensionEventNames";
  23. import HostInteropType from "../interop";
  24. // Entry point for web view extensions -- extensions that live inside the web view
  25. interface EventSubscription {
  26. eventName: string;
  27. callback: (data: any) => any;
  28. }
  29. /**
  30. * Implements an event dispatcher for the client services
  31. */
  32. export class EventDispatcher implements Editor.Extensions.EventDispatcher {
  33. private subscriptions: EventSubscription[] = [];
  34. sendEvent(eventType: string, data: any) {
  35. this.subscriptions.forEach(sub => {
  36. if (sub.eventName == eventType) {
  37. sub.callback(data);
  38. }
  39. });
  40. }
  41. subscribeToEvent(eventType, callback) {
  42. this.subscriptions.push({
  43. eventName: eventType,
  44. callback: callback
  45. });
  46. }
  47. }
  48. /**
  49. * Generic registry for storing Editor Extension Services
  50. */
  51. class ServicesProvider<T extends Editor.Extensions.ServiceEventListener> implements Editor.Extensions.ServicesProvider<T> {
  52. registeredServices: T[] = [];
  53. /**
  54. * Adds a service to the registered services list for this type of service
  55. * @param {T} service the service to register
  56. */
  57. register(service: T) {
  58. this.registeredServices.push(service);
  59. }
  60. unregister(service: T) {
  61. var index = this.registeredServices.indexOf(service, 0);
  62. if (index > -1) {
  63. this.registeredServices.splice(index, 1);
  64. }
  65. }
  66. }
  67. export class WebViewServicesProvider extends ServicesProvider<Editor.ClientExtensions.WebViewServiceEventListener> {
  68. private userPreferences = {};
  69. /**
  70. * Sets the preferences for the service locator
  71. * @param {any} prefs
  72. * @return {[type]}
  73. */
  74. setPreferences(prefs : any) {
  75. this.userPreferences = prefs;
  76. }
  77. /**
  78. * Allow this service registry to subscribe to events that it is interested in
  79. * @param {EventDispatcher} eventDispatcher The global event dispatcher
  80. */
  81. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  82. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.CodeLoadedEvent, (ev) => this.codeLoaded(ev));
  83. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ConfigureEditorEvent, (ev) => this.configureEditor(ev));
  84. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceRenamedEvent, (ev) => this.renameResource(ev));
  85. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceDeletedEvent, (ev) => this.deleteResource(ev));
  86. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.CodeSavedEvent, (ev) => this.saveCode(ev));
  87. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.PreferencesChangedEvent, (ev) => this.preferencesChanged());
  88. }
  89. /**
  90. * Called when code is loaded
  91. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  92. */
  93. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  94. this.registeredServices.forEach((service) => {
  95. try {
  96. // Notify services that the project has just been loaded
  97. if (service.codeLoaded) {
  98. service.codeLoaded(ev);
  99. }
  100. } catch (e) {
  101. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  102. }
  103. });
  104. }
  105. /**
  106. * Called after code has been saved
  107. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  108. */
  109. saveCode(ev: Editor.EditorEvents.CodeSavedEvent) {
  110. // run through and find any services that can handle this.
  111. this.registeredServices.forEach((service) => {
  112. try {
  113. // Verify that the service contains the appropriate methods and that it can save
  114. if (service.save) {
  115. service.save(ev);
  116. }
  117. } catch (e) {
  118. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  119. }
  120. });
  121. }
  122. /**
  123. * Called when a resource has been deleted
  124. */
  125. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  126. this.registeredServices.forEach((service) => {
  127. try {
  128. // Verify that the service contains the appropriate methods and that it can delete
  129. if (service.delete) {
  130. service.delete(ev);
  131. }
  132. } catch (e) {
  133. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  134. }
  135. });
  136. }
  137. /**
  138. * Called when a resource has been renamed
  139. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  140. */
  141. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  142. this.registeredServices.forEach((service) => {
  143. try {
  144. // Verify that the service contains the appropriate methods and that it can handle the rename
  145. if (service.rename) {
  146. service.rename(ev);
  147. }
  148. } catch (e) {
  149. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  150. }
  151. });
  152. }
  153. /**
  154. * Called when the editor is requesting to be configured for a particular file
  155. * @param {Editor.EditorEvents.EditorFileEvent} ev
  156. */
  157. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  158. this.registeredServices.forEach((service) => {
  159. try {
  160. // Notify services that the project has just been loaded
  161. if (service.configureEditor) {
  162. service.configureEditor(ev);
  163. }
  164. } catch (e) {
  165. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  166. }
  167. });
  168. }
  169. /**
  170. * Called when preferences changes
  171. * @param {Editor.EditorEvents.PreferencesChangedEvent} ev
  172. */
  173. preferencesChanged() {
  174. this.registeredServices.forEach((service) => {
  175. // Notify services that the project has been unloaded
  176. try {
  177. if (service.preferencesChanged) {
  178. service.preferencesChanged();
  179. }
  180. } catch (e) {
  181. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  182. }
  183. });
  184. }
  185. /**
  186. * Returns the Host Interop module
  187. * @return {Editor.ClientExtensions.HostInterop}
  188. */
  189. getHostInterop(): Editor.ClientExtensions.HostInterop {
  190. return HostInteropType.getInstance();
  191. }
  192. /**
  193. * Return a preference value or the provided default from the user settings file
  194. * @param {string} gorupName name of the group the preference lives under
  195. * @param {string} preferenceName name of the preference to retrieve
  196. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  197. * @return {number|boolean|string}
  198. */
  199. getUserPreference(groupName: string, preferenceName: string, defaultValue?: number | boolean | string): number | boolean | string {
  200. if (this.userPreferences) {
  201. let prefs = this.userPreferences[groupName];
  202. if (prefs) {
  203. return prefs[groupName][preferenceName] || defaultValue;
  204. }
  205. }
  206. // if all else fails
  207. return defaultValue;
  208. }
  209. }