From a916cc1ddee357fc572d057eaced4549a61a536b Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 30 Mar 2024 14:12:44 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20inline=20TipTapEditor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/InlineEditAction.kt | 38 ++++++++++++------- gui/editorInset/index.html | 13 +++++++ gui/editorInset/vite.config.ts | 19 ++++++++++ gui/src/editorInset/EditorInset.tsx | 33 ++++++++++++++++ gui/src/editorInset/main.tsx | 18 +++++++++ 5 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 gui/editorInset/index.html create mode 100644 gui/editorInset/vite.config.ts create mode 100644 gui/src/editorInset/EditorInset.tsx create mode 100644 gui/src/editorInset/main.tsx 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 ce8c1e31f..8883e5f66 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 @@ -4,28 +4,28 @@ import com.github.continuedev.continueintellijextension.toolWindow.JS_QUERY_POOL import com.intellij.icons.AllIcons import com.intellij.openapi.Disposable import com.intellij.openapi.actionSystem.* -import com.intellij.openapi.actionSystem.impl.ActionButton import com.intellij.openapi.editor.EditorFactory import com.intellij.openapi.editor.actions.IncrementalFindAction +import com.intellij.openapi.editor.colors.EditorFontType import com.intellij.openapi.editor.ex.EditorEx import com.intellij.openapi.editor.impl.EditorImpl import com.intellij.openapi.fileTypes.FileTypes import com.intellij.openapi.project.DumbAware import com.intellij.openapi.project.Project -import com.intellij.openapi.util.Disposer 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 java.awt.Color import java.awt.event.ComponentAdapter import java.awt.event.ComponentEvent import java.awt.event.KeyAdapter import java.awt.event.KeyEvent -import javax.swing.JButton import javax.swing.JPanel -import javax.swing.KeyStroke /** * Adapted from https://github.com/cursive-ide/component-inlay-example/blob/master/src/main/kotlin/inlays/InlineEditAction.kt @@ -36,23 +36,32 @@ class InlineEditAction : AnAction(), DumbAware { e.presentation.isVisible = true } - private val preloadedBrowser: JBCefBrowser by lazy { - JBCefBrowser.createBuilder().setOffScreenRendering(true).build().apply { + private val preloadedBrowser: JBCefBrowser = + JBCefBrowser.createBuilder().setOffScreenRendering(false).build().apply { jbCefClient.setProperty( JBCefClient.Properties.JS_QUERY_POOL_SIZE, JS_QUERY_POOL_SIZE ) - loadHTML("") + loadURL("http://continue/editorInset/index.html") } - } + override fun actionPerformed(e: AnActionEvent) { val editor = e.getData(PlatformDataKeys.EDITOR) ?: return val project = e.getData(PlatformDataKeys.PROJECT) ?: return val manager = EditorComponentInlaysManager.from(editor) - val lineNumber = editor.document.getLineNumber(editor.caretModel.offset) - 1 + val lineNumber = editor.document.getLineNumber(editor.caretModel.offset) + + // Get indentation width in pixels + val lineStart = editor.document.getLineStartOffset(lineNumber) + val lineEnd = editor.document.getLineEndOffset(lineNumber) + val text = editor.document.getText(TextRange(lineStart, lineEnd)) + val indentation = text.takeWhile { it == ' ' }.length + val charWidth = editor.contentComponent.getFontMetrics(editor.colorsScheme.getFont(EditorFontType.PLAIN)).charWidth(' ') + val leftInset = indentation * charWidth * 2 / 3 + val inlayRef = Ref() - val panel = makePanel(makeEditor(project, inlayRef), inlayRef) + val panel = makePanel(makeEditor(project, inlayRef), inlayRef, leftInset) val inlay = manager.insertAfter(lineNumber, panel) panel.revalidate() inlayRef.set(inlay) @@ -60,7 +69,7 @@ class InlineEditAction : AnAction(), DumbAware { viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED)) } - fun makePanel(editor: EditorTextField, inlayRef: Ref): JPanel { + fun makePanel(editor: EditorTextField, inlayRef: Ref, leftInset: Int): JPanel { val action = object : AnAction({ "Close" }, AllIcons.Actions.Close) { override fun actionPerformed(e: AnActionEvent) { inlayRef.get().dispose() @@ -70,7 +79,7 @@ class InlineEditAction : AnAction(), DumbAware { val browser = preloadedBrowser // Set height of the browser to be 100px browser.component.preferredSize = browser.component.preferredSize.apply { - height = 100 + height = 60 } editor.addKeyListener(object : KeyAdapter() { @@ -81,8 +90,9 @@ class InlineEditAction : AnAction(), DumbAware { } }) - return JPanel(MigLayout("wrap 1, insets 0, gap 0!, fillx")).apply { -// add(editor, "grow, gap 0!") + 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?) { diff --git a/gui/editorInset/index.html b/gui/editorInset/index.html new file mode 100644 index 000000000..1e6607ff2 --- /dev/null +++ b/gui/editorInset/index.html @@ -0,0 +1,13 @@ + + + + + + + Continue + + +
+ + + diff --git a/gui/editorInset/vite.config.ts b/gui/editorInset/vite.config.ts new file mode 100644 index 000000000..fdc63bbdb --- /dev/null +++ b/gui/editorInset/vite.config.ts @@ -0,0 +1,19 @@ +import react from "@vitejs/plugin-react-swc"; +import tailwindcss from "tailwindcss"; +import { defineConfig } from "vite"; + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [react(), tailwindcss()], + build: { + // Change the output .js filename to not include a hash + rollupOptions: { + // external: ["vscode-webview"], + output: { + entryFileNames: `assets/[name].js`, + chunkFileNames: `assets/[name].js`, + assetFileNames: `assets/[name].[ext]`, + }, + }, + }, +}); diff --git a/gui/src/editorInset/EditorInset.tsx b/gui/src/editorInset/EditorInset.tsx new file mode 100644 index 000000000..7600bd8f4 --- /dev/null +++ b/gui/src/editorInset/EditorInset.tsx @@ -0,0 +1,33 @@ +import { useSelector } from "react-redux"; +import styled from "styled-components"; +import TipTapEditor from "../components/mainInput/TipTapEditor"; +import { selectSlashCommands } from "../redux/selectors"; +import { RootState } from "../redux/store"; + +const EditorInsetDiv = styled.div` + max-width: 500px; +`; + +function EditorInset() { + const availableSlashCommands = useSelector(selectSlashCommands); + const availableContextProviders = useSelector( + (store: RootState) => store.state.config.contextProviders, + ); + + return ( + +
+ { + console.log("Enter: ", e); + }} + > +
+
+ ); +} + +export default EditorInset; diff --git a/gui/src/editorInset/main.tsx b/gui/src/editorInset/main.tsx new file mode 100644 index 000000000..d15005ee7 --- /dev/null +++ b/gui/src/editorInset/main.tsx @@ -0,0 +1,18 @@ +import React from "react"; +import ReactDOM from "react-dom/client"; +import { Provider } from "react-redux"; +import "../index.css"; +import { store } from "../redux/store"; + +import CustomPostHogProvider from "../hooks/CustomPostHogProvider"; +import EditorInset from "./EditorInset"; + +ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( + + + + + + + , +);