Ver código fonte

- Added new UI Extension method getCurrentResourceEditor to grab the current editor
- Added new UI Extension event handleWebMessage to allow extensions to be notified of AtomicQuery calls
- Fixed signature of getUserPreferences in the WebView extensions
- Added ability for WebView extensions to add method to the window object so that host extensions can call them

Shaddock Heath 9 anos atrás
pai
commit
a99169438a

+ 39 - 1
Script/AtomicEditor/hostExtensions/HostExtensionServices.ts

@@ -339,6 +339,15 @@ export class UIServicesProvider extends ServicesProvider<Editor.HostExtensions.U
         this.mainFrame.menu.removePluginMenuItemSource(id);
     }
 
+
+    /**
+     * Returns the currently active resource editor or null
+     * @return {Editor.ResourceEditor}
+     */
+    getCurrentResourceEditor(): Editor.ResourceEditor {
+        return this.mainFrame.resourceframe.currentResourceEditor;
+    }
+
     /**
      * Adds a new menu to the hierarchy context menu
      * @param  {string} id
@@ -501,12 +510,41 @@ export class UIServicesProvider extends ServicesProvider<Editor.HostExtensions.U
         });
     }
 
+    /**
+     * Hooks into web messages coming in from web views
+     * @param  {[String|Object]} data
+     */
+    handleWebMessage(data) {
+        let messageType;
+        let messageObject;
+
+        try {
+            messageObject = JSON.parse(data.request);
+            messageType = messageObject.message;
+        } catch (e) {
+            // not JSON, we are just getting a notification message of some sort
+            messageType = data.request;
+        }
+
+        // run through and find any services that can handle this.
+        this.registeredServices.forEach((service) => {
+            try {
+                // Verify that the service contains the appropriate methods and that it can save
+                if (service.handleWebMessage) {
+                    service.handleWebMessage(messageType, messageObject);
+                }
+            } catch (e) {
+                EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
+            }
+        });
+    }
+
     /**
      * Allow this service registry to subscribe to events that it is interested in
      * @param  {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
      */
     subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
         // Placeholder for when UI events published by the editor need to be listened for
-        //eventDispatcher.subscribeToEvent(EditorEvents.SaveResourceNotification, (ev) => this.doSomeUiMessage(ev));
+        eventDispatcher.subscribeToEvent(EditorEvents.WebMessage, (ev) => this.handleWebMessage(ev));
     }
 }

+ 6 - 3
Script/AtomicWebViewEditor/clientExtensions/ClientExtensionServices.ts

@@ -213,14 +213,17 @@ export class WebViewServicesProvider extends ServicesProvider<Editor.ClientExten
 
     /**
      * Return a preference value or the provided default from the user settings file
-     * @param  {string} gorupName name of the group the preference lives under
+     * @param  {string} settignsGroup name of the group the preference lives under
      * @param  {string} preferenceName name of the preference to retrieve
      * @param  {number | boolean | string} defaultValue value to return if pref doesn't exist
      * @return {number|boolean|string}
      */
-    getUserPreference(groupName: string, preferenceName: string, defaultValue?: number | boolean | string): number | boolean | string {
+    getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
+    getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
+    getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
+    getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: any): any {
         if (this.userPreferences) {
-            let prefs = this.userPreferences[groupName];
+            let prefs = this.userPreferences[settingsGroup];
             if (prefs) {
                 return prefs[preferenceName] || defaultValue;
             }

+ 9 - 0
Script/AtomicWebViewEditor/interop.ts

@@ -215,4 +215,13 @@ export default class HostInteropType {
             console.log("Error loading preferences: " + e.error_message);
         });
     }
+
+    /**
+     * This adds a global routine to the window object so that it can be called from the host
+     * @param  {string} routineName
+     * @param  {(} callback
+     */
+    addCustomHostRoutine(routineName: string, callback: () => void) {
+        window[routineName] = callback;
+    }
 }

+ 24 - 1
Script/TypeScript/EditorWork.d.ts

@@ -310,7 +310,15 @@ declare module Editor.HostExtensions {
         menuItemClicked?(refid: string): boolean;
         projectContextItemClicked?(asset: ToolCore.Asset, refid: string): boolean;
         hierarchyContextItemClicked?(node: Atomic.Node, refid: string): boolean;
+
+        /**
+         * Handle messages that are submitted via Atomic.Query from within a web view editor.
+         * @param message The message type that was submitted to be used to determine what the data contains if present
+         * @param data any additional data that needs to be submitted with the message
+         */
+        handleWebMessage?(messageType: string, data?: any): void;
     }
+
     export interface UIServicesProvider extends Editor.Extensions.ServicesProvider<UIServicesEventListener> {
         createPluginMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource;
         removePluginMenuItemSource(id: string);
@@ -323,6 +331,12 @@ declare module Editor.HostExtensions {
         showModalError(windowText: string, message: string);
         showResourceSelection(windowText: string, importerType: string, resourceType: string, callback: (retObject: any, args: any) => void, args?: any);
 
+        /**
+         * Returns the currently active resource editor or null
+         * @return {Editor.ResourceEditor}
+         */
+        getCurrentResourceEditor(): Editor.ResourceEditor;
+
         /**
          * Register a custom editor.  These editors will override editors in the standard editor list if
          * they both resolve the ```canHandleResource``` call.
@@ -389,7 +403,9 @@ declare module Editor.ClientExtensions {
          * @param  {number | boolean | string} defaultValue value to return if pref doesn't exist
          * @return {number|boolean|string}
          */
-        getUserPreference(extensionName: string, preferenceName: string, defaultValue?: number | boolean | string): number | boolean | string;
+        getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
+        getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
+        getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
     }
 
     export interface AtomicErrorMessage {
@@ -440,5 +456,12 @@ declare module Editor.ClientExtensions {
          * Notify the host that the contents of the editor has changed
          */
         notifyEditorChange();
+
+        /**
+         * This adds a global routine to the window object so that it can be called from the host
+         * @param  {string} routineName
+         * @param  {(} callback
+         */
+        addCustomHostRoutine(routineName: string, callback: () => void);
     }
 }