|
5 | 5 |
|
6 | 6 | package com.microsoft.azure.toolkit.intellij.common.feedback; |
7 | 7 |
|
| 8 | +import com.intellij.openapi.fileEditor.FileEditorManager; |
8 | 9 | import com.intellij.openapi.project.DumbAware; |
9 | 10 | import com.intellij.openapi.project.Project; |
| 11 | +import com.intellij.openapi.util.Disposer; |
10 | 12 | import com.intellij.openapi.vfs.VirtualFile; |
11 | 13 | import com.intellij.ui.jcef.JBCefBrowser; |
| 14 | +import com.intellij.ui.jcef.JBCefBrowserBase; |
| 15 | +import com.intellij.ui.jcef.JBCefJSQuery; |
12 | 16 | import com.intellij.uiDesigner.core.GridConstraints; |
13 | 17 | import com.microsoft.azure.toolkit.intellij.common.BaseEditor; |
| 18 | +import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager; |
| 19 | +import org.cef.browser.CefBrowser; |
| 20 | +import org.cef.browser.CefFrame; |
| 21 | +import org.cef.handler.CefLoadHandlerAdapter; |
14 | 22 | import org.jetbrains.annotations.Nls; |
15 | | -import org.jetbrains.annotations.NotNull; |
16 | 23 |
|
| 24 | +import javax.annotation.Nonnull; |
17 | 25 | import javax.swing.*; |
18 | 26 |
|
19 | 27 | public class ProvideFeedbackEditor extends BaseEditor implements DumbAware { |
| 28 | + private final JBCefJSQuery myJSQueryOpenInBrowser; |
20 | 29 | private JPanel pnlRoot; |
21 | 30 |
|
22 | 31 | public ProvideFeedbackEditor(final Project project, VirtualFile virtualFile) { |
23 | 32 | super(virtualFile); |
| 33 | + final JBCefBrowser jbCefBrowser = new JBCefBrowser("https://www.surveymonkey.com/r/PNB5NBL?mode=simple"); |
| 34 | + final CefBrowser browser = jbCefBrowser.getCefBrowser(); |
| 35 | + pnlRoot.add(jbCefBrowser.getComponent(), new GridConstraints(0, 0, 1, 1, 0, GridConstraints.FILL_BOTH, 3, 3, null, null, null, 0)); |
| 36 | + // Create a JS query instance |
| 37 | + this.myJSQueryOpenInBrowser = JBCefJSQuery.create((JBCefBrowserBase) jbCefBrowser); |
| 38 | + myJSQueryOpenInBrowser.addHandler((e) -> { |
| 39 | + AzureTaskManager.getInstance().runLater(() -> { |
| 40 | + final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); |
| 41 | + fileEditorManager.closeFile(virtualFile); |
| 42 | + }); |
| 43 | + return new JBCefJSQuery.Response("ok!"); |
| 44 | + }); |
24 | 45 |
|
25 | | - final JBCefBrowser browser = new JBCefBrowser("https://www.surveymonkey.com/r/PNB5NBL?mode=simple"); |
26 | | - pnlRoot.add(browser.getComponent(), new GridConstraints(0, 0, 1, 1, 0, GridConstraints.FILL_BOTH, 3, 3, null, null, null, 0)); |
27 | | - |
| 46 | + jbCefBrowser.getJBCefClient().addLoadHandler(new CefLoadHandlerAdapter() { |
| 47 | + @Override |
| 48 | + public void onLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode) { |
| 49 | + if (!browser.getURL().contains("https://www.surveymonkey.com/r/PNB5NBL")) { |
| 50 | + return; |
| 51 | + } |
| 52 | + final String js = "window.JavaPanelBridge = {\n" + |
| 53 | + " closeTab: function (e) {\n" + |
| 54 | + " " + myJSQueryOpenInBrowser.inject("e") + |
| 55 | + " }\n" + |
| 56 | + "};\n" + |
| 57 | + "var azToolkitCloseBtn = document.querySelector('.thank-you-page-container.survey-submit-actions a.survey-page-button.btn');\n" + |
| 58 | + "if (azToolkitCloseBtn) {\n" + |
| 59 | + " azToolkitCloseBtn.innerHTML='Close';" + |
| 60 | + " azToolkitCloseBtn.addEventListener('click', () => {\n" + |
| 61 | + " window.JavaPanelBridge.closeTab();\n" + |
| 62 | + " });\n" + |
| 63 | + "}"; |
| 64 | + browser.executeJavaScript(js, browser.getURL(), 0); |
| 65 | + } |
| 66 | + }, browser); |
28 | 67 | } |
29 | 68 |
|
30 | 69 | @Override |
31 | | - public @NotNull JComponent getComponent() { |
| 70 | + public JComponent getComponent() { |
32 | 71 | return pnlRoot; |
33 | 72 | } |
34 | 73 |
|
35 | 74 | @Override |
36 | | - public @Nls(capitalization = Nls.Capitalization.Title) @NotNull String getName() { |
| 75 | + public @Nls(capitalization = Nls.Capitalization.Title) @Nonnull String getName() { |
37 | 76 | return "Provide Feedback"; |
38 | 77 | } |
39 | 78 |
|
40 | 79 | @Override |
41 | 80 | public void dispose() { |
42 | | - |
| 81 | + Disposer.dispose(myJSQueryOpenInBrowser); |
43 | 82 | } |
44 | 83 | } |
0 commit comments