diff --git a/core/web/webviewProtocol.ts b/core/web/webviewProtocol.ts index ac5637278..446c9e045 100644 --- a/core/web/webviewProtocol.ts +++ b/core/web/webviewProtocol.ts @@ -87,6 +87,7 @@ export type WebviewProtocol = Protocol & "stats/getTokensPerDay": [undefined, { day: string; tokens: number }[]]; "stats/getTokensPerModel": [undefined, { model: string; tokens: number }[]]; insertAtCursor: [{ text: string }, void]; + "jetbrains/editorInsetHeight": [{ height: number }, void]; }; export type ReverseWebviewProtocol = { @@ -119,4 +120,5 @@ export type ReverseWebviewProtocol = { refreshSubmenuItems: [undefined, void]; setTheme: [{ theme: any }, void]; setColors: [{ [key: string]: string }, void]; + "jetbrains/editorInsetRefresh": [undefined, void]; }; diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt index 8883e5f66..d311670d0 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt @@ -1,11 +1,14 @@ package com.github.continuedev.continueintellijextension.editor +import com.github.continuedev.continueintellijextension.factories.CustomSchemeHandlerFactory +import com.github.continuedev.continueintellijextension.toolWindow.ContinueBrowser import com.github.continuedev.continueintellijextension.toolWindow.JS_QUERY_POOL_SIZE import com.intellij.icons.AllIcons import com.intellij.openapi.Disposable import com.intellij.openapi.actionSystem.* import com.intellij.openapi.editor.EditorFactory import com.intellij.openapi.editor.actions.IncrementalFindAction +import com.intellij.openapi.editor.colors.EditorColorsManager import com.intellij.openapi.editor.colors.EditorFontType import com.intellij.openapi.editor.ex.EditorEx import com.intellij.openapi.editor.impl.EditorImpl @@ -15,17 +18,25 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util.Ref import com.intellij.openapi.util.TextRange import com.intellij.ui.EditorTextField -import com.intellij.ui.JBColor import com.intellij.ui.jcef.JBCefBrowser import com.intellij.ui.jcef.JBCefClient import com.intellij.util.ui.UIUtil import net.miginfocom.swing.MigLayout +import org.cef.CefApp +import org.cef.browser.CefBrowser +import org.cef.handler.CefLoadHandlerAdapter import java.awt.Color +import java.awt.Graphics +import java.awt.Graphics2D +import java.awt.RenderingHints import java.awt.event.ComponentAdapter import java.awt.event.ComponentEvent import java.awt.event.KeyAdapter import java.awt.event.KeyEvent +import javax.swing.BorderFactory +import javax.swing.JFrame import javax.swing.JPanel +import javax.swing.JTextArea /** * Adapted from https://github.com/cursive-ide/component-inlay-example/blob/master/src/main/kotlin/inlays/InlineEditAction.kt @@ -36,17 +47,16 @@ class InlineEditAction : AnAction(), DumbAware { e.presentation.isVisible = true } - private val preloadedBrowser: JBCefBrowser = - JBCefBrowser.createBuilder().setOffScreenRendering(false).build().apply { - jbCefClient.setProperty( - JBCefClient.Properties.JS_QUERY_POOL_SIZE, - JS_QUERY_POOL_SIZE - ) - loadURL("http://continue/editorInset/index.html") - } - + private var preloadedBrowser: ContinueBrowser? = null override fun actionPerformed(e: AnActionEvent) { + if (e.project == null) return + if (this.preloadedBrowser == null) { + this.preloadedBrowser = ContinueBrowser(e.project!!, + "http://continue/editorInset/index.html", true) +// "http://localhost:5173/jetbrains_editorInset_index.html") + } + val editor = e.getData(PlatformDataKeys.EDITOR) ?: return val project = e.getData(PlatformDataKeys.PROJECT) ?: return val manager = EditorComponentInlaysManager.from(editor) @@ -61,28 +71,70 @@ class InlineEditAction : AnAction(), DumbAware { val leftInset = indentation * charWidth * 2 / 3 val inlayRef = Ref() - val panel = makePanel(makeEditor(project, inlayRef), inlayRef, leftInset) + val panel = makePanel(inlayRef, leftInset) val inlay = manager.insertAfter(lineNumber, panel) panel.revalidate() inlayRef.set(inlay) val viewport = (editor as? EditorImpl)?.scrollPane?.viewport viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED)) + + // Set focus to the editor's browser component + preloadedBrowser?.browser?.component?.requestFocus() + + preloadedBrowser?.onHeightChange { + viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED)) + } + +// preloadedBrowser?.sendToWebview("jetbrains/editorInsetRefresh", null) + // Set timeout +// Thread.sleep(3000) +// preloadedBrowser?.sendToWebview("jetbrains/editorInsetRefresh", null) } - fun makePanel(editor: EditorTextField, inlayRef: Ref, leftInset: Int): JPanel { + fun makePanel(inlayRef: Ref, leftInset: Int): JPanel { val action = object : AnAction({ "Close" }, AllIcons.Actions.Close) { override fun actionPerformed(e: AnActionEvent) { inlayRef.get().dispose() } } - val browser = preloadedBrowser - // Set height of the browser to be 100px + val browser = preloadedBrowser?.browser ?: return JPanel() browser.component.preferredSize = browser.component.preferredSize.apply { height = 60 } - editor.addKeyListener(object : KeyAdapter() { + val frame = JFrame("Text Area Example") + frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE + frame.setSize(400, 300) + + val textArea = CustomTextArea() + textArea.background = Color(240, 240, 240) + textArea.border = BorderFactory.createCompoundBorder( + BorderFactory.createEmptyBorder(10, 10, 10, 10), + BorderFactory.createLineBorder(Color.GRAY, 2) + ) + textArea.lineWrap = true + textArea.wrapStyleWord = true + + frame.add(textArea) + frame.isVisible = true + + val panel = JPanel(MigLayout("wrap 1, insets 0 $leftInset 0 0, gap 0!, fillx")).apply { + // Transparent background + val globalScheme = EditorColorsManager.getInstance().globalScheme + val defaultBackground = globalScheme.defaultBackground + background = defaultBackground +// add(textArea, "grow, gap 0!") + add(browser.component, "grow, gap 0!") + addComponentListener(object : ComponentAdapter() { + override fun componentShown(e: ComponentEvent?) { + browser.component.requestFocus() + } + }) + putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true) + } + panel.isOpaque = false + panel.addKeyListener(object : KeyAdapter() { override fun keyPressed(e: KeyEvent) { if (e.keyCode == KeyEvent.VK_ESCAPE) { inlayRef.get().dispose() @@ -90,47 +142,29 @@ class InlineEditAction : AnAction(), DumbAware { } }) - return JPanel(MigLayout("wrap 1, insets 0 $leftInset 0 0, gap 0!, fillx")).apply { - // Transparent background - background = Color(0, 0, 0, 0) - add(browser.component, "grow, gap 0!") - addComponentListener(object : ComponentAdapter() { - override fun componentShown(e: ComponentEvent?) { - editor.requestFocus() - } - }) + preloadedBrowser?.onHeightChange { height -> + browser.component.preferredSize = browser.component.preferredSize.apply { +// this.height = height + // Refresh +// browser.component.revalidate() +// browser.component.repaint() +// +// // Refresh the panel +// panel.revalidate() +// panel.repaint() + } } + + return panel } +} - fun makeEditor(project: Project, inlayRef: Ref): EditorTextField { - val factory = EditorFactory.getInstance() - val document = factory.createDocument("") - - return object : EditorTextField(document, project, FileTypes.PLAIN_TEXT) { - //always paint pretty border - override fun updateBorder(editor: EditorEx) = setupBorder(editor) - - override fun createEditor(): EditorEx { - // otherwise border background is painted from multiple places - return super.createEditor().apply { - //TODO: fix in editor - //com.intellij.openapi.editor.impl.EditorImpl.getComponent() == non-opaque JPanel - // which uses default panel color - component.isOpaque = false - //com.intellij.ide.ui.laf.darcula.ui.DarculaEditorTextFieldBorder.paintBorder - scrollPane.isOpaque = false - } - } - }.apply { - putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true) - setOneLineMode(false) - setPlaceholder(text) - addSettingsProvider { - it.putUserData(IncrementalFindAction.SEARCH_DISABLED, true) - it.colorsScheme.lineSpacing = 1f - it.settings.isUseSoftWraps = true - } - selectAll() - } +class CustomTextArea : JTextArea() { + override fun paintComponent(g: Graphics) { + val g2 = g as Graphics2D + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) + g2.color = background + g2.fillRoundRect(0, 0, width - 1, height - 1, 15, 15) + super.paintComponent(g) } } \ No newline at end of file diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinuePluginService.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinuePluginService.kt index 6f4989c59..550df9542 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinuePluginService.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinuePluginService.kt @@ -3,6 +3,7 @@ package com.github.continuedev.continueintellijextension.services import com.github.continuedev.continueintellijextension.`continue`.CoreMessenger import com.github.continuedev.continueintellijextension.`continue`.IdeProtocolClient import com.github.continuedev.continueintellijextension.`continue`.uuid +import com.github.continuedev.continueintellijextension.toolWindow.ContinueBrowser import com.github.continuedev.continueintellijextension.toolWindow.ContinuePluginToolWindowFactory import com.google.gson.Gson import com.intellij.openapi.Disposable @@ -41,25 +42,6 @@ class ContinuePluginService(project: Project) : Disposable, DumbAware { data: Any?, messageId: String = uuid() ) { - val jsonData = Gson().toJson( - mapOf( - "messageId" to messageId, - "messageType" to messageType, - "data" to data - ) - ) - val jsCode = buildJavaScript(jsonData) - - try { - continuePluginWindow?.webView?.executeJavaScriptAsync(jsCode) - } catch (error: IllegalStateException) { - println("Webview not initialized yet $error") - } + continuePluginWindow?.browser?.sendToWebview(messageType, data, messageId) } - - private fun buildJavaScript(jsonData: String): String { - return """window.postMessage($jsonData, "*");""" - } - - } \ No newline at end of file diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinueBrowser.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinueBrowser.kt new file mode 100644 index 000000000..c7ffad486 --- /dev/null +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinueBrowser.kt @@ -0,0 +1,233 @@ +package com.github.continuedev.continueintellijextension.toolWindow + +import com.github.continuedev.continueintellijextension.activities.showTutorial +import com.github.continuedev.continueintellijextension.constants.getConfigJsonPath +import com.github.continuedev.continueintellijextension.`continue`.* +import com.github.continuedev.continueintellijextension.factories.CustomSchemeHandlerFactory +import com.github.continuedev.continueintellijextension.services.ContinuePluginService +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.openapi.components.ServiceManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.Disposer +import com.intellij.ui.jcef.* +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import org.cef.CefApp +import org.cef.browser.CefBrowser +import org.cef.handler.CefLoadHandlerAdapter + +class ContinueBrowser(val project: Project, url: String, useOsr: Boolean = false) { + private val heightChangeListeners = mutableListOf<(Int) -> Unit>() + fun onHeightChange(listener: (Int) -> Unit) { + heightChangeListeners.add(listener) + } + + private val PASS_THROUGH_TO_CORE = listOf( + "abort", + "getContinueDir", + "history/list", + "history/save", + "history/delete", + "history/load", + "devdata/log", + "config/addModel", + "config/deleteModel", + "config/addOpenAIKey", + "llm/streamComplete", + "llm/streamChat", + "llm/complete", + "command/run", + "context/loadSubmenuItems", + "context/getContextItems", + "context/addDocs", + "config/getBrowserSerialized", + ) + + private fun registerAppSchemeHandler() { + CefApp.getInstance().registerSchemeHandlerFactory( + "http", + "continue", + CustomSchemeHandlerFactory() + ) + } + + val browser: JBCefBrowser + init { + val osName = System.getProperty("os.name").toLowerCase() + val os = when { + osName.contains("mac") || osName.contains("darwin") -> "darwin" + osName.contains("win") -> "win32" + osName.contains("nix") || osName.contains("nux") || osName.contains("aix") -> "linux" + else -> "linux" + } + + this.browser = JBCefBrowser.createBuilder().setOffScreenRendering(os == "linux" || useOsr).build() + browser.jbCefClient.setProperty( + JBCefClient.Properties.JS_QUERY_POOL_SIZE, + JS_QUERY_POOL_SIZE + ) + registerAppSchemeHandler() + browser.loadURL(url); + Disposer.register(project, browser) + + // Listen for events sent from browser + val myJSQueryOpenInBrowser = JBCefJSQuery.create((browser as JBCefBrowserBase?)!!) + myJSQueryOpenInBrowser.addHandler { msg: String? -> + val parser = JsonParser() + val json: JsonObject = parser.parse(msg).asJsonObject + val messageType = json.get("messageType").asString + val data = json.get("data") + val messageId = json.get("messageId")?.asString + + val continuePluginService = ServiceManager.getService( + project, + ContinuePluginService::class.java + ) + + val ide = continuePluginService.ideProtocolClient; + + val respond = fun(data: Any?) { + val jsonData = mutableMapOf( + "messageId" to messageId, + "data" to data, + "messageType" to messageType + ) + val jsonString = Gson().toJson(jsonData) + sendToWebview(messageType, data, messageId ?: uuid()) + } + + if (PASS_THROUGH_TO_CORE.contains(messageType)) { + continuePluginService.coreMessenger?.request(messageType, data, messageId, respond) + return@addHandler null + } + + when (messageType) { + "jetbrains/editorInsetHeight" -> { + val height = data.asJsonObject.get("height").asInt + heightChangeListeners.forEach { it(height) } + } + "onLoad" -> { + GlobalScope.launch { + // Set the colors to match Intellij theme + val colors = GetTheme().getTheme(); + sendToWebview("setColors", colors) + + val jsonData = mutableMapOf( + "windowId" to continuePluginService.windowId, + "workspacePaths" to continuePluginService.workspacePaths, + "vscMachineId" to getMachineUniqueID(), + "vscMediaUrl" to "http://continue", + ) + respond(jsonData) + } + + } + "showLines" -> { + val data = data.asJsonObject + ide?.highlightCode(RangeInFile( + data.get("filepath").asString, + Range(Position( + data.get("start").asInt, + 0 + ), Position( + data.get("end").asInt, + 0 + )), + + ),"#00ff0022") + } + "showTutorial" -> { + showTutorial(project) + } + "showVirtualFile" -> { + val data = data.asJsonObject + ide?.showVirtualFile(data.get("name").asString, data.get("content").asString) + } + "showFile" -> { + val data = data.asJsonObject + ide?.setFileOpen(data.get("filepath").asString) + } + "reloadWindow" -> {} + "openConfigJson" -> { + ide?.setFileOpen(getConfigJsonPath()) + } + "readRangeInFile" -> { + val data = data.asJsonObject + ide?.readRangeInFile(RangeInFile( + data.get("filepath").asString, + Range(Position( + data.get("start").asInt, + 0 + ), Position( + data.get("end").asInt + 1, + 0 + )), + )) + } + "focusEditor" -> {} + + // IDE // + else -> { + if (msg != null) { + ide?.handleMessage(msg, respond) + } + } + } + + + null + } + + // Listen for the page load event + browser.jbCefClient.addLoadHandler(object : CefLoadHandlerAdapter() { + override fun onLoadingStateChange( + browser: CefBrowser?, + isLoading: Boolean, + canGoBack: Boolean, + canGoForward: Boolean + ) { + if (!isLoading) { + // The page has finished loading + executeJavaScript(browser, myJSQueryOpenInBrowser) + } + } + }, browser.cefBrowser) + + } + fun executeJavaScript(browser: CefBrowser?, myJSQueryOpenInBrowser: JBCefJSQuery) { + // Execute JavaScript - you might want to handle potential exceptions here + val script = """window.postIntellijMessage = function(messageType, data, messageId) { + const msg = JSON.stringify({messageType, data, messageId}); + ${myJSQueryOpenInBrowser.inject("msg")} + }""".trimIndent() + + browser?.executeJavaScript(script, browser.url, 0) + } + + fun sendToWebview( + messageType: String, + data: Any?, + messageId: String = uuid() + ) { + val jsonData = Gson().toJson( + mapOf( + "messageId" to messageId, + "messageType" to messageType, + "data" to data + ) + ) + val jsCode = buildJavaScript(jsonData) + + try { + this.browser.executeJavaScriptAsync(jsCode) + } catch (error: IllegalStateException) { + println("Webview not initialized yet $error") + } + } + + private fun buildJavaScript(jsonData: String): String { + return """window.postMessage($jsonData, "*");""" + } +} \ No newline at end of file diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt index 873ccb911..94a498da1 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt @@ -1,33 +1,16 @@ package com.github.continuedev.continueintellijextension.toolWindow -import com.github.continuedev.continueintellijextension.activities.showTutorial -import com.github.continuedev.continueintellijextension.constants.getConfigJsonPath -import com.github.continuedev.continueintellijextension.`continue`.* -import com.github.continuedev.continueintellijextension.factories.CustomSchemeHandlerFactory import com.github.continuedev.continueintellijextension.services.ContinuePluginService -import com.google.gson.Gson -import com.google.gson.JsonObject -import com.google.gson.JsonParser import com.intellij.openapi.actionSystem.ActionManager import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.components.ServiceManager -import com.intellij.openapi.editor.colors.EditorColorsManager import com.intellij.openapi.project.DumbAware import com.intellij.openapi.project.Project -import com.intellij.openapi.util.Disposer import com.intellij.openapi.wm.ToolWindow import com.intellij.openapi.wm.ToolWindowFactory import com.intellij.ui.content.ContentFactory import com.intellij.ui.jcef.* -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch -import org.cef.CefApp -import org.cef.browser.CefBrowser -import org.cef.handler.CefLoadHandlerAdapter import javax.swing.* -import kotlin.math.max -import kotlin.math.min - const val JS_QUERY_POOL_SIZE = "200" @@ -53,200 +36,26 @@ class ContinuePluginToolWindowFactory : ToolWindowFactory, DumbAware { class ContinuePluginWindow(toolWindow: ToolWindow, project: Project) { - val PASS_THROUGH_TO_CORE = listOf( - "abort", - "getContinueDir", - "history/list", - "history/save", - "history/delete", - "history/load", - "devdata/log", - "config/addModel", - "config/deleteModel", - "config/addOpenAIKey", - "llm/streamComplete", - "llm/streamChat", - "llm/complete", - "command/run", - "context/loadSubmenuItems", - "context/getContextItems", - "context/addDocs", - "config/getBrowserSerialized", - ) - init { System.setProperty("ide.browser.jcef.jsQueryPoolSize", JS_QUERY_POOL_SIZE) System.setProperty("ide.browser.jcef.contextMenu.devTools.enabled", "true") } - val webView: JBCefBrowser by lazy { - val osName = System.getProperty("os.name").toLowerCase() - val os = when { - osName.contains("mac") || osName.contains("darwin") -> "darwin" - osName.contains("win") -> "win32" - osName.contains("nix") || osName.contains("nux") || osName.contains("aix") -> "linux" - else -> "linux" - } - - val browser = JBCefBrowser.createBuilder().setOffScreenRendering(os == "linux").build() - browser.jbCefClient.setProperty( - JBCefClient.Properties.JS_QUERY_POOL_SIZE, - JS_QUERY_POOL_SIZE - ) - registerAppSchemeHandler() - + val browser: ContinueBrowser by lazy { + val url = "http://continue/index.html"; // Use to get hot-reloading in local development - if (false) { - browser.loadURL("http://localhost:5173/jetbrains_index.html"); - } else { - browser.loadURL("http://continue/index.html") - } -// browser.loadHTML("") -// browser.loadURL("http://localhost:5173/index.html") - Disposer.register(project, browser) + // val url = "http://localhost:5173/jetbrains_index.html"; + val browser = ContinueBrowser(project, url) val continuePluginService = ServiceManager.getService( project, ContinuePluginService::class.java ) continuePluginService.continuePluginWindow = this - - // Listen for events sent from browser - val myJSQueryOpenInBrowser = JBCefJSQuery.create((browser as JBCefBrowserBase?)!!) - myJSQueryOpenInBrowser.addHandler { msg: String? -> - val parser = JsonParser() - val json: JsonObject = parser.parse(msg).asJsonObject - val messageType = json.get("messageType").asString - val data = json.get("data") - val messageId = json.get("messageId")?.asString - - val ide = continuePluginService.ideProtocolClient; - - val respond = fun(data: Any?) { - val jsonData = mutableMapOf( - "messageId" to messageId, - "data" to data, - "messageType" to messageType - ) - val jsonString = Gson().toJson(jsonData) - continuePluginService.sendToWebview(messageType, data, messageId ?: uuid()) - } - - if (PASS_THROUGH_TO_CORE.contains(messageType)) { - continuePluginService.coreMessenger?.request(messageType, data, messageId, respond) - return@addHandler null - } - - when (messageType) { - "onLoad" -> { - GlobalScope.launch { - // Set the colors to match Intellij theme - val colors = GetTheme().getTheme(); - continuePluginService.sendToWebview("setColors", colors) - - val jsonData = mutableMapOf( - "windowId" to continuePluginService.windowId, - "workspacePaths" to continuePluginService.workspacePaths, - "vscMachineId" to getMachineUniqueID(), - "vscMediaUrl" to "http://continue", - ) - respond(jsonData) - } - - } - "showLines" -> { - val data = data.asJsonObject - ide?.highlightCode(RangeInFile( - data.get("filepath").asString, - Range(Position( - data.get("start").asInt, - 0 - ), Position( - data.get("end").asInt, - 0 - )), - - ),"#00ff0022") - } - "showTutorial" -> { - showTutorial(project) - } - "showVirtualFile" -> { - val data = data.asJsonObject - ide?.showVirtualFile(data.get("name").asString, data.get("content").asString) - } - "showFile" -> { - val data = data.asJsonObject - ide?.setFileOpen(data.get("filepath").asString) - } - "reloadWindow" -> {} - "openConfigJson" -> { - ide?.setFileOpen(getConfigJsonPath()) - } - "readRangeInFile" -> { - val data = data.asJsonObject - ide?.readRangeInFile(RangeInFile( - data.get("filepath").asString, - Range(Position( - data.get("start").asInt, - 0 - ), Position( - data.get("end").asInt + 1, - 0 - )), - )) - } - "focusEditor" -> {} - - // IDE // - else -> { - if (msg != null) { - ide?.handleMessage(msg, respond) - } - } - } - - - null - } - - // Listen for the page load event - browser.jbCefClient.addLoadHandler(object : CefLoadHandlerAdapter() { - override fun onLoadingStateChange( - browser: CefBrowser?, - isLoading: Boolean, - canGoBack: Boolean, - canGoForward: Boolean - ) { - if (!isLoading) { - // The page has finished loading - executeJavaScript(browser, myJSQueryOpenInBrowser) - } - } - }, browser.cefBrowser) - browser } - fun executeJavaScript(browser: CefBrowser?, myJSQueryOpenInBrowser: JBCefJSQuery) { - // Execute JavaScript - you might want to handle potential exceptions here - val script = """window.postIntellijMessage = function(messageType, data, messageId) { - const msg = JSON.stringify({messageType, data, messageId}); - ${myJSQueryOpenInBrowser.inject("msg")} - }""".trimIndent() - - browser?.executeJavaScript(script, browser.url, 0) - } - val content: JComponent - get() = webView.component - - private fun registerAppSchemeHandler() { - CefApp.getInstance().registerSchemeHandlerFactory( - "http", - "continue", - CustomSchemeHandlerFactory() - ) - } + get() = browser.browser.component } } \ No newline at end of file diff --git a/gui/public/jetbrains_editorInset_index.html b/gui/public/jetbrains_editorInset_index.html new file mode 100644 index 000000000..1573f0bcd --- /dev/null +++ b/gui/public/jetbrains_editorInset_index.html @@ -0,0 +1,26 @@ + + + + + + + + Continue + + +
+ + + + + + + diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index 80c8142ff..bc07499c5 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -410,6 +410,10 @@ function TipTapEditor(props: TipTapEditorProps) { [editor, onEnterRef.current, props.isMainInput], ); + useWebviewListener("jetbrains/editorInsetRefresh", async () => { + editor?.chain().clearContent().focus().run(); + }); + useWebviewListener( "focusContinueInput", async (data) => { diff --git a/gui/src/editorInset/EditorInset.tsx b/gui/src/editorInset/EditorInset.tsx index 7600bd8f4..3cad91f68 100644 --- a/gui/src/editorInset/EditorInset.tsx +++ b/gui/src/editorInset/EditorInset.tsx @@ -1,31 +1,56 @@ -import { useSelector } from "react-redux"; +import { useEffect, useRef } from "react"; +import { useDispatch, useSelector } from "react-redux"; import styled from "styled-components"; +import { defaultBorderRadius } from "../components"; import TipTapEditor from "../components/mainInput/TipTapEditor"; +import useSetup from "../hooks/useSetup"; import { selectSlashCommands } from "../redux/selectors"; import { RootState } from "../redux/store"; +import { ideRequest } from "../util/ide"; const EditorInsetDiv = styled.div` max-width: 500px; + position: relative; + display: flex; + border-radius: ${defaultBorderRadius}; + // box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.4); `; function EditorInset() { + const dispatch = useDispatch(); const availableSlashCommands = useSelector(selectSlashCommands); const availableContextProviders = useSelector( (store: RootState) => store.state.config.contextProviders, ); + useSetup(dispatch); + + const elementRef = useRef(null); + + useEffect(() => { + if (!elementRef.current) return; + const resizeObserver = new ResizeObserver(() => { + if (!elementRef.current) return; + + console.log("Height: ", elementRef.current.clientHeight); + ideRequest("jetbrains/editorInsetHeight", { + height: elementRef.current.clientHeight, + }); + }); + resizeObserver.observe(elementRef.current); + return () => resizeObserver.disconnect(); + }, []); + return ( - -
- { - console.log("Enter: ", e); - }} - > -
+ + { + console.log("Enter: ", e); + }} + > ); }