ClientExtensionServices.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.ProjectUnloadedEvent, (ev) => this.projectUnloaded());
  86. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.ResourceDeletedEvent, (ev) => this.deleteResource(ev));
  87. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.CodeSavedEvent, (ev) => this.saveCode(ev));
  88. eventDispatcher.subscribeToEvent(ClientExtensionEventNames.PreferencesChangedEvent, (ev) => this.preferencesChanged());
  89. }
  90. /**
  91. * Called when code is loaded
  92. * @param {Editor.EditorEvents.CodeLoadedEvent} ev Event info about the file that is being loaded
  93. */
  94. codeLoaded(ev: Editor.EditorEvents.CodeLoadedEvent) {
  95. this.registeredServices.forEach((service) => {
  96. try {
  97. // Notify services that the project has just been loaded
  98. if (service.codeLoaded) {
  99. service.codeLoaded(ev);
  100. }
  101. } catch (e) {
  102. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  103. }
  104. });
  105. }
  106. /**
  107. * Called after code has been saved
  108. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  109. */
  110. saveCode(ev: Editor.EditorEvents.CodeSavedEvent) {
  111. // run through and find any services that can handle this.
  112. this.registeredServices.forEach((service) => {
  113. try {
  114. // Verify that the service contains the appropriate methods and that it can save
  115. if (service.save) {
  116. service.save(ev);
  117. }
  118. } catch (e) {
  119. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  120. }
  121. });
  122. }
  123. /**
  124. * Called when a resource has been deleted
  125. */
  126. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  127. this.registeredServices.forEach((service) => {
  128. try {
  129. // Verify that the service contains the appropriate methods and that it can delete
  130. if (service.delete) {
  131. service.delete(ev);
  132. }
  133. } catch (e) {
  134. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  135. }
  136. });
  137. }
  138. /**
  139. * Called when a resource has been renamed
  140. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  141. */
  142. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  143. this.registeredServices.forEach((service) => {
  144. try {
  145. // Verify that the service contains the appropriate methods and that it can handle the rename
  146. if (service.rename) {
  147. service.rename(ev);
  148. }
  149. } catch (e) {
  150. alert(`Error detected in extension ${service.name}\n \n ${e.stack}`);
  151. }
  152. });
  153. }
  154. /**
  155. * Called when the editor is requesting to be configured for a particular file
  156. * @param {Editor.EditorEvents.EditorFileEvent} ev
  157. */
  158. configureEditor(ev: Editor.EditorEvents.EditorFileEvent) {
  159. this.registeredServices.forEach((service) => {
  160. try {
  161. // Notify services that the project has just been loaded
  162. if (service.configureEditor) {
  163. service.configureEditor(ev);
  164. }
  165. } catch (e) {
  166. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  167. }
  168. });
  169. }
  170. /**
  171. * Called when the project is unloaded
  172. */
  173. projectUnloaded() {
  174. this.registeredServices.forEach((service) => {
  175. // Notify services that the project has been unloaded
  176. try {
  177. if (service.projectUnloaded) {
  178. service.projectUnloaded();
  179. }
  180. } catch (e) {
  181. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  182. }
  183. });
  184. }
  185. /**
  186. * Called when prefeerences changes
  187. * @param {Editor.EditorEvents.PreferencesChangedEvent} ev
  188. */
  189. preferencesChanged() {
  190. this.registeredServices.forEach((service) => {
  191. // Notify services that the project has been unloaded
  192. try {
  193. if (service.preferencesChanged) {
  194. service.preferencesChanged();
  195. }
  196. } catch (e) {
  197. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  198. }
  199. });
  200. }
  201. /**
  202. * Returns the Host Interop module
  203. * @return {Editor.ClientExtensions.HostInterop}
  204. */
  205. getHostInterop(): Editor.ClientExtensions.HostInterop {
  206. return HostInteropType.getInstance();
  207. }
  208. /**
  209. * Return a preference value or the provided default from the user settings file
  210. * @param {string} gorupName name of the group the preference lives under
  211. * @param {string} preferenceName name of the preference to retrieve
  212. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  213. * @return {number|boolean|string}
  214. */
  215. getUserPreference(groupName: string, preferenceName: string, defaultValue?: number | boolean | string): number | boolean | string {
  216. if (this.userPreferences) {
  217. let prefs = this.userPreferences[groupName];
  218. if (prefs) {
  219. return prefs[groupName][preferenceName] || defaultValue;
  220. }
  221. }
  222. // if all else fails
  223. return defaultValue;
  224. }
  225. }