HostExtensionServices.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 * as EditorEvents from "../editor/EditorEvents";
  23. import * as EditorUI from "../ui/EditorUI";
  24. import MainFrame = require("../ui/frames/MainFrame");
  25. import InspectorFrame = require("../ui/frames/inspector/InspectorFrame");
  26. import ModalOps = require("../ui/modal/ModalOps");
  27. import ResourceOps = require("../resources/ResourceOps");
  28. import Editor = require("../editor/Editor");
  29. /**
  30. * Generic registry for storing Editor Extension Services
  31. */
  32. export class ServicesProvider<T extends Editor.Extensions.ServiceEventListener> implements Editor.Extensions.ServicesProvider<T> {
  33. registeredServices: T[] = [];
  34. /**
  35. * Adds a service to the registered services list for this type of service
  36. * @param {T} service the service to register
  37. */
  38. register(service: T) {
  39. this.registeredServices.push(service);
  40. }
  41. unregister(service: T) {
  42. var index = this.registeredServices.indexOf(service, 0);
  43. if (index > -1) {
  44. this.registeredServices.splice(index, 1);
  45. }
  46. }
  47. }
  48. export interface ServiceEventSubscriber {
  49. /**
  50. * Allow this service registry to subscribe to events that it is interested in
  51. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  52. */
  53. subscribeToEvents(topLevelWindow: Atomic.UIWidget);
  54. }
  55. /**
  56. * Registry for service extensions that are concerned about project events
  57. */
  58. export class ProjectServicesProvider extends ServicesProvider<Editor.HostExtensions.ProjectServicesEventListener> implements Editor.HostExtensions.ProjectServicesProvider {
  59. constructor() {
  60. super();
  61. }
  62. /**
  63. * Allow this service registry to subscribe to events that it is interested in
  64. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  65. */
  66. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  67. eventDispatcher.subscribeToEvent(EditorEvents.LoadProjectNotification, (ev) => this.projectLoaded(ev));
  68. eventDispatcher.subscribeToEvent(EditorEvents.CloseProject, (ev) => this.projectUnloaded(ev));
  69. eventDispatcher.subscribeToEvent(EditorEvents.PlayerStartRequest, () => this.playerStarted());
  70. }
  71. /**
  72. * Called when the project is unloaded
  73. * @param {[type]} data Event info from the project unloaded event
  74. */
  75. projectUnloaded(data) {
  76. // Need to use a for loop for length down to 0 because extensions *could* delete themselves from the list on projectUnloaded
  77. for (let i = this.registeredServices.length - 1; i >= 0; i--) {
  78. let service = this.registeredServices[i];
  79. // Notify services that the project has been unloaded
  80. try {
  81. if (service.projectUnloaded) {
  82. service.projectUnloaded();
  83. }
  84. } catch (e) {
  85. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e} \n\n ${e.stack}`);
  86. }
  87. };
  88. }
  89. /**
  90. * Called when the project is loaded
  91. * @param {[type]} data Event info from the project unloaded event
  92. */
  93. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  94. // Need to use a for loop and don't cache the length because the list of services *may* change while processing. Extensions could be appended to the end
  95. for (let i = 0; i < this.registeredServices.length; i++) {
  96. let service = this.registeredServices[i];
  97. try {
  98. // Notify services that the project has just been loaded
  99. if (service.projectLoaded) {
  100. service.projectLoaded(ev);
  101. }
  102. } catch (e) {
  103. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  104. }
  105. };
  106. }
  107. playerStarted() {
  108. this.registeredServices.forEach((service) => {
  109. try {
  110. // Notify services that the project has just been loaded
  111. if (service.playerStarted) {
  112. service.playerStarted();
  113. }
  114. } catch (e) {
  115. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  116. }
  117. });
  118. }
  119. /**
  120. * Return a preference value or the provided default from the user settings file
  121. * @param {string} extensionName name of the extension the preference lives under
  122. * @param {string} preferenceName name of the preference to retrieve
  123. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  124. * @return {number|boolean|string}
  125. */
  126. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
  127. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
  128. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
  129. getUserPreference(extensionName: string, preferenceName: string, defaultValue?: any): any {
  130. return EditorUI.getEditor().getUserPreference(extensionName, preferenceName, defaultValue);
  131. }
  132. /**
  133. * Sets a user preference value in the user settings file
  134. * @param {string} extensionName name of the extension the preference lives under
  135. * @param {string} preferenceName name of the preference to set
  136. * @param {number | boolean | string} value value to set
  137. */
  138. setUserPreference(extensionName: string, preferenceName: string, value: number | boolean | string) {
  139. EditorUI.getEditor().setUserPreference(extensionName, preferenceName, value);
  140. }
  141. /**
  142. * Sets a group of user preference values in the user settings file located in the project. Elements in the
  143. * group will merge in with existing group preferences. Use this method if setting a bunch of settings
  144. * at once.
  145. * @param {string} extensionName name of the group the preference lives under
  146. * @param {string} groupPreferenceValues an object literal containing all of the preferences for the group.
  147. */
  148. setUserPreferenceGroup(extensionName: string, groupPreferenceValues: Object) {
  149. EditorUI.getEditor().setUserPreferenceGroup(extensionName, groupPreferenceValues);
  150. }
  151. }
  152. /**
  153. * Registry for service extensions that are concerned about Resources
  154. */
  155. export class ResourceServicesProvider extends ServicesProvider<Editor.HostExtensions.ResourceServicesEventListener> implements Editor.HostExtensions.ResourceServicesProvider {
  156. constructor() {
  157. super();
  158. }
  159. /**
  160. * Allow this service registry to subscribe to events that it is interested in
  161. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  162. */
  163. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  164. eventDispatcher.subscribeToEvent(EditorEvents.SaveResourceNotification, (ev) => this.saveResource(ev));
  165. eventDispatcher.subscribeToEvent(EditorEvents.DeleteResourceNotification, (ev) => this.deleteResource(ev));
  166. eventDispatcher.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev) => this.renameResource(ev));
  167. eventDispatcher.subscribeToEvent(EditorEvents.EditResource, (ev) => this.editResource(ev));
  168. }
  169. /**
  170. * Called after a resource has been saved
  171. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  172. */
  173. saveResource(ev: Editor.EditorEvents.SaveResourceEvent) {
  174. // run through and find any services that can handle this.
  175. this.registeredServices.forEach((service) => {
  176. try {
  177. // Verify that the service contains the appropriate methods and that it can save
  178. if (service.save) {
  179. service.save(ev);
  180. }
  181. } catch (e) {
  182. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  183. }
  184. });
  185. }
  186. /**
  187. * Called when a resource has been deleted
  188. */
  189. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  190. this.registeredServices.forEach((service) => {
  191. try {
  192. // Verify that the service contains the appropriate methods and that it can delete
  193. if (service.delete) {
  194. service.delete(ev);
  195. }
  196. } catch (e) {
  197. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  198. }
  199. });
  200. }
  201. /**
  202. * Called when a resource has been renamed
  203. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  204. */
  205. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  206. this.registeredServices.forEach((service) => {
  207. try {
  208. // Verify that the service contains the appropriate methods and that it can handle the rename
  209. if (service.rename) {
  210. service.rename(ev);
  211. }
  212. } catch (e) {
  213. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  214. }
  215. });
  216. }
  217. /**
  218. * Called when a resource is about to be edited
  219. * @param {Editor.EditorEvents.EditResourceEvent} ev
  220. */
  221. editResource(ev: Editor.EditorEvents.EditResourceEvent) {
  222. this.registeredServices.forEach((service) => {
  223. try {
  224. // Verify that the service contains the appropriate methods and that it can handle the edit
  225. if (service.edit) {
  226. service.edit(ev);
  227. }
  228. } catch (e) {
  229. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  230. }
  231. });
  232. }
  233. /**
  234. * Create New Material
  235. * @param {string} resourcePath
  236. * @param {string} materialName
  237. * @param {boolean} reportError
  238. */
  239. createMaterial(resourcePath: string, materialName: string, reportError: boolean): boolean {
  240. return ResourceOps.CreateNewMaterial(resourcePath, materialName, reportError);
  241. }
  242. }
  243. /**
  244. * Registry for service extensions that are concerned about Scenes
  245. */
  246. export class SceneServicesProvider extends ServicesProvider<Editor.HostExtensions.SceneServicesEventListener> implements Editor.HostExtensions.SceneServicesProvider {
  247. constructor() {
  248. super();
  249. }
  250. /**
  251. * Allow this service registry to subscribe to events that it is interested in
  252. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  253. */
  254. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  255. eventDispatcher.subscribeToEvent(EditorEvents.ActiveSceneEditorChange, (ev) => this.activeSceneEditorChange(ev));
  256. eventDispatcher.subscribeToEvent(EditorEvents.SceneClosed, (ev) => this.sceneClosed(ev));
  257. }
  258. /**
  259. * Called after an active scene editor change
  260. * @param {Editor.EditorEvents.ActiveSceneEditorChangeEvent} ev
  261. */
  262. activeSceneEditorChange(ev: Editor.EditorEvents.ActiveSceneEditorChangeEvent) {
  263. // run through and find any services that can handle this.
  264. this.registeredServices.forEach((service) => {
  265. try {
  266. // Verify that the service contains the appropriate methods and that it can save
  267. if (service.activeSceneEditorChanged) {
  268. service.activeSceneEditorChanged(ev);
  269. }
  270. } catch (e) {
  271. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  272. }
  273. });
  274. }
  275. /**
  276. * Called after a scene is closed
  277. * @param {Editor.EditorEvents.SceneClosedEvent} ev
  278. */
  279. sceneClosed(ev: Editor.EditorEvents.SceneClosedEvent) {
  280. // run through and find any services that can handle this.
  281. this.registeredServices.forEach((service) => {
  282. try {
  283. // Verify that the service contains the appropriate methods and that it can save
  284. if (service.editorSceneClosed) {
  285. service.editorSceneClosed(ev);
  286. }
  287. } catch (e) {
  288. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  289. }
  290. });
  291. }
  292. }
  293. /**
  294. * Registry for service extensions that are concerned about and need access to parts of the editor user interface
  295. * Note: we may want to move this out into it's own file since it has a bunch of editor dependencies
  296. */
  297. export class UIServicesProvider extends ServicesProvider<Editor.HostExtensions.UIServicesEventListener> implements Editor.HostExtensions.UIServicesProvider {
  298. constructor() {
  299. super();
  300. }
  301. private mainFrame: MainFrame = null;
  302. private inspectorFrame: InspectorFrame = null;
  303. private modalOps: ModalOps;
  304. init(mainFrame: MainFrame, modalOps: ModalOps) {
  305. // Only set these once
  306. if (this.mainFrame == null) {
  307. this.mainFrame = mainFrame;
  308. this.inspectorFrame = this.mainFrame.inspectorframe;
  309. }
  310. if (this.modalOps == null) {
  311. this.modalOps = modalOps;
  312. }
  313. }
  314. /**
  315. * Adds a new menu to the plugin menu
  316. * @param {string} id
  317. * @param {any} items
  318. * @return {Atomic.UIMenuItemSource}
  319. */
  320. createPluginMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  321. return this.mainFrame.menu.createPluginMenuItemSource(id, items);
  322. }
  323. /**
  324. * Removes a previously added menu from the plugin menu
  325. * @param {string} id
  326. */
  327. removePluginMenuItemSource(id: string) {
  328. this.mainFrame.menu.removePluginMenuItemSource(id);
  329. }
  330. /**
  331. * Returns the currently active resource editor or null
  332. * @return {Editor.ResourceEditor}
  333. */
  334. getCurrentResourceEditor(): Editor.ResourceEditor {
  335. return this.mainFrame.resourceframe.currentResourceEditor;
  336. }
  337. /**
  338. * Will load a resource editor or navigate to an already loaded resource editor by path
  339. * @return {Editor.ResourceEditor}
  340. */
  341. loadResourceEditor(resourcePath: string): Editor.ResourceEditor {
  342. this.mainFrame.resourceframe.sendEvent(EditorEvents.EditResource, {path: resourcePath} );
  343. return this.mainFrame.resourceframe.currentResourceEditor;
  344. }
  345. /**
  346. * Adds a new menu to the hierarchy context menu
  347. * @param {string} id
  348. * @param {any} items
  349. * @return {Atomic.UIMenuItemSource}
  350. */
  351. createHierarchyContextMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  352. return this.mainFrame.hierarchyFrame.menu.createPluginItemSource(id, items);
  353. }
  354. /**
  355. * Removes a previously added menu from the hierarchy context menu
  356. * @param {string} id
  357. */
  358. removeHierarchyContextMenuItemSource(id: string) {
  359. this.mainFrame.hierarchyFrame.menu.removePluginItemSource(id);
  360. }
  361. /**
  362. * Adds a new menu to the project context menu
  363. * @param {string} id
  364. * @param {any} items
  365. * @return {Atomic.UIMenuItemSource}
  366. */
  367. createProjectContextMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  368. return this.mainFrame.projectframe.menu.createPluginItemSource(id, items);
  369. }
  370. /**
  371. * Removes a previously added menu from the project context menu
  372. * @param {string} id
  373. */
  374. removeProjectContextMenuItemSource(id: string) {
  375. this.mainFrame.projectframe.menu.removePluginItemSource(id);
  376. }
  377. /**
  378. * Refreshes the hierarchy frame
  379. */
  380. refreshHierarchyFrame() {
  381. this.mainFrame.hierarchyFrame.populate();
  382. }
  383. /**
  384. * Loads Custom Inspector Widget
  385. * @param {Atomic.UIWidget} customInspector
  386. */
  387. loadCustomInspector(customInspector: Atomic.UIWidget) {
  388. if (this.inspectorFrame) {
  389. this.inspectorFrame.loadCustomInspectorWidget(customInspector);
  390. }
  391. }
  392. /**
  393. * Disaplays a modal window
  394. * @param {Editor.Modal.ModalWindow} window
  395. */
  396. showModalWindow(windowText: string, uifilename: string, handleWidgetEventCB: (ev: Atomic.UIWidgetEvent) => void): Editor.Modal.ExtensionWindow {
  397. return this.modalOps.showExtensionWindow(windowText, uifilename, handleWidgetEventCB, true);
  398. }
  399. /**
  400. * Disaplays a modal window
  401. * @param {Editor.Modal.ModalWindow} window
  402. */
  403. showNonModalWindow(windowText: string, uifilename: string, handleWidgetEventCB: (ev: Atomic.UIWidgetEvent) => void): Editor.Modal.ExtensionWindow {
  404. return this.modalOps.showExtensionWindow(windowText, uifilename, handleWidgetEventCB, false);
  405. }
  406. /**
  407. * Disaplays a modal error window
  408. * @param {string} windowText
  409. * @param {string} message
  410. */
  411. showModalError(windowText: string, message: string): Atomic.UIMessageWindow {
  412. return this.modalOps.showError(windowText, message);
  413. }
  414. /**
  415. * Displays a resource slection window
  416. * @param {string} windowText
  417. * @param {string} importerType
  418. * @param {string} resourceType
  419. * @param {function} callback
  420. * @param {any} retObject
  421. * @param {any} args
  422. */
  423. showResourceSelection(windowText: string, importerType: string, resourceType: string, callback: (retObject: any, args: any) => void, args: any = undefined) {
  424. this.modalOps.showResourceSelection(windowText, importerType, resourceType, callback);
  425. }
  426. /**
  427. * Will register a custom editor for a particular file type.
  428. * @param {Editor.Extensions.ResourceEditorBuilder} editorBuilder
  429. */
  430. registerCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
  431. this.mainFrame.resourceframe.resourceEditorProvider.registerCustomEditor(editorBuilder);
  432. }
  433. /**
  434. * Will unregister a previously registered editor builder
  435. * @param {Editor.Extensions.ResourceEditorBuilder} editorBuilder
  436. */
  437. unregisterCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
  438. this.mainFrame.resourceframe.resourceEditorProvider.unregisterCustomEditor(editorBuilder);
  439. }
  440. /**
  441. * Called when a menu item has been clicked
  442. * @param {string} refId
  443. * @type {boolean} return true if handled
  444. */
  445. menuItemClicked(refid: string): boolean {
  446. // run through and find any services that can handle this.
  447. return this.registeredServices.some((service) => {
  448. try {
  449. // Verify that the service contains the appropriate methods and that it can handle it
  450. if (service.menuItemClicked) {
  451. if (service.menuItemClicked(refid)) {
  452. return true;
  453. }
  454. }
  455. } catch (e) {
  456. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  457. }
  458. });
  459. }
  460. /**
  461. * Called when a context menu item in the hierarchy pane has been clicked
  462. * @param {Atomic.Node} node
  463. * @param {string} refId
  464. * @type {boolean} return true if handled
  465. */
  466. hierarchyContextItemClicked(node: Atomic.Node, refid: string): boolean {
  467. if (!node)
  468. return false;
  469. // run through and find any services that can handle this.
  470. return this.registeredServices.some((service) => {
  471. try {
  472. // Verify that the service contains the appropriate methods and that it can handle it
  473. if (service.hierarchyContextItemClicked) {
  474. if (service.hierarchyContextItemClicked(node, refid)) {
  475. return true;
  476. }
  477. }
  478. } catch (e) {
  479. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  480. }
  481. });
  482. }
  483. /**
  484. * Called when a context menu item in the hierarchy pane has been clicked
  485. * @param {ToolCore.Asset} asset
  486. * @param {string} refId
  487. * @type {boolean} return true if handled
  488. */
  489. projectContextItemClicked(asset: ToolCore.Asset, refid: string): boolean {
  490. if (!asset)
  491. return false;
  492. // run through and find any services that can handle this.
  493. return this.registeredServices.some((service) => {
  494. try {
  495. // Verify that the service contains the appropriate methods and that it can handle it
  496. if (service.projectContextItemClicked) {
  497. if (service.projectContextItemClicked(asset, refid)) {
  498. return true;
  499. }
  500. }
  501. } catch (e) {
  502. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  503. }
  504. });
  505. }
  506. /**
  507. * Called when a project asset in the hierarchy pane has been clicked
  508. * @param {ToolCore.Asset} asset
  509. * @type {boolean} return true if handled
  510. */
  511. projectAssetClicked(asset: ToolCore.Asset):boolean {
  512. if (!asset) {
  513. return false;
  514. }
  515. // run through and find any services that can handle this.
  516. return this.registeredServices.some((service) => {
  517. try {
  518. // Verify that the service contains the appropriate methods and that it can handle it
  519. if (service.projectAssetClicked) {
  520. if (service.projectAssetClicked(asset)) {
  521. return true;
  522. }
  523. }
  524. } catch (e) {
  525. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  526. }
  527. });
  528. }
  529. /**
  530. * Hooks into web messages coming in from web views
  531. * @param {[String|Object]} data
  532. */
  533. handleWebMessage(data) {
  534. let messageType;
  535. let messageObject;
  536. try {
  537. messageObject = JSON.parse(data.request);
  538. messageType = messageObject.message;
  539. } catch (e) {
  540. // not JSON, we are just getting a notification message of some sort
  541. messageType = data.request;
  542. }
  543. // run through and find any services that can handle this.
  544. this.registeredServices.forEach((service) => {
  545. try {
  546. // Verify that the service contains the appropriate methods and that it can save
  547. if (service.handleWebMessage) {
  548. service.handleWebMessage(messageType, messageObject);
  549. }
  550. } catch (e) {
  551. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  552. }
  553. });
  554. }
  555. /**
  556. * Allow this service registry to subscribe to events that it is interested in
  557. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  558. */
  559. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  560. // Placeholder for when UI events published by the editor need to be listened for
  561. eventDispatcher.subscribeToEvent(EditorEvents.WebMessage, (ev) => this.handleWebMessage(ev));
  562. }
  563. }