🚧 inline TipTapEditor
This commit is contained in:
parent
de73f6b5c3
commit
a916cc1dde
5 changed files with 107 additions and 14 deletions
|
|
@ -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("<html><body><input type='text'/></body></html>")
|
||||
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<Disposable>()
|
||||
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<Disposable>): JPanel {
|
||||
fun makePanel(editor: EditorTextField, inlayRef: Ref<Disposable>, 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?) {
|
||||
|
|
|
|||
13
gui/editorInset/index.html
Normal file
13
gui/editorInset/index.html
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/play_button.png" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Continue</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="../src/editorInset/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
19
gui/editorInset/vite.config.ts
Normal file
19
gui/editorInset/vite.config.ts
Normal file
|
|
@ -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]`,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
33
gui/src/editorInset/EditorInset.tsx
Normal file
33
gui/src/editorInset/EditorInset.tsx
Normal file
|
|
@ -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 (
|
||||
<EditorInsetDiv>
|
||||
<div className="flex px-2 relative">
|
||||
<TipTapEditor
|
||||
availableContextProviders={availableContextProviders}
|
||||
availableSlashCommands={availableSlashCommands}
|
||||
isMainInput={true}
|
||||
onEnter={(e) => {
|
||||
console.log("Enter: ", e);
|
||||
}}
|
||||
></TipTapEditor>
|
||||
</div>
|
||||
</EditorInsetDiv>
|
||||
);
|
||||
}
|
||||
|
||||
export default EditorInset;
|
||||
18
gui/src/editorInset/main.tsx
Normal file
18
gui/src/editorInset/main.tsx
Normal file
|
|
@ -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(
|
||||
<React.StrictMode>
|
||||
<Provider store={store}>
|
||||
<CustomPostHogProvider>
|
||||
<EditorInset />
|
||||
</CustomPostHogProvider>
|
||||
</Provider>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
Loading…
Add table
Reference in a new issue