diff --git a/gui/src/components/FileIcon.tsx b/gui/src/components/FileIcon.tsx index 21c17a010..42609bdf3 100644 --- a/gui/src/components/FileIcon.tsx +++ b/gui/src/components/FileIcon.tsx @@ -2,6 +2,10 @@ import DOMPurify from "dompurify"; import { themeIcons } from "seti-file-icons"; const FileIcon = ({ filename, height, width }) => { + const filenameParts = filename.includes(" (") + ? filename.split(" ") + : [filename, ""]; + filenameParts.pop(); const getIcon = themeIcons({ blue: "#268bd2", grey: "#657b83", @@ -16,7 +20,7 @@ const FileIcon = ({ filename, height, width }) => { ignore: "#586e75", }); // Sanitize the SVG string before rendering it - const { svg, color } = getIcon(filename); + const { svg, color } = getIcon(filenameParts.join(" ")); const sanitizedSVG = DOMPurify.sanitize(svg); return ( diff --git a/gui/src/components/index.ts b/gui/src/components/index.ts index 08e546658..7e85e0887 100644 --- a/gui/src/components/index.ts +++ b/gui/src/components/index.ts @@ -2,40 +2,38 @@ import { Tooltip } from "react-tooltip"; import styled, { keyframes } from "styled-components"; import { getFontSize } from "../util"; +export const VSC_SECONDARY_DARK_VAR = "--vscode-input-background"; +export const VSC_BACKGROUND_VAR = "--vscode-sideBar-background"; +export const VSC_FOREGROUND_VAR = "--vscode-editor-foreground"; +export const VSC_BUTTON_BACKGROUND_VAR = "--vscode-button-background"; +export const VSC_EDITOR_BACKGROUND_VAR = "--vscode-editor-background"; + +export const VSC_THEME_COLOR_VARS = [ + VSC_SECONDARY_DARK_VAR, + VSC_BACKGROUND_VAR, + VSC_FOREGROUND_VAR, + VSC_BUTTON_BACKGROUND_VAR, + VSC_EDITOR_BACKGROUND_VAR, +]; + export const defaultBorderRadius = "5px"; export const lightGray = "#646464"; export const buttonColor = "#1bbe84"; export const buttonColorHover = "#1bbe84a8"; -export const secondaryDark = "var(--vscode-input-background, rgb(45 45 45))"; +export const secondaryDark = `var(${VSC_SECONDARY_DARK_VAR}, rgb(45 45 45))`; // export const vscBackground = "var(--vscode-editor-background, rgb(30 30 30))"; -export const vscBackground = "var(--vscode-sideBar-background, rgb(30 30 30))"; -export const vscForeground = "var(--vscode-editor-foreground, #fff)"; -export const vscButtonBackground = "var(--vscode-button-background, #1bbe84)"; +export const vscBackground = `var(${VSC_BACKGROUND_VAR}, rgb(30 30 30))`; +export const vscForeground = `var(${VSC_FOREGROUND_VAR}, #fff)`; +export const vscButtonBackground = `var(${VSC_BUTTON_BACKGROUND_VAR}, #1bbe84)`; +export const vscEditorBackground = `var(${VSC_EDITOR_BACKGROUND_VAR}, rgb(30 30 30))`; if (typeof document !== "undefined") { - const cachedForeground = localStorage.getItem("--vscode-editor-foreground"); - const cachedBackground = localStorage.getItem("--vscode-editor-background"); - const cachedSecondaryDark = localStorage.getItem( - "--vscode-list-hoverBackground" - ); - if (cachedForeground) { - document.body.style.setProperty( - "--vscode-editor-foreground", - cachedForeground - ); - } - if (cachedBackground) { - document.body.style.setProperty( - "--vscode-editor-background", - cachedBackground - ); - } - if (cachedSecondaryDark) { - document.body.style.setProperty( - "--vscode-list-hoverBackground", - cachedSecondaryDark - ); + for (const colorVar of VSC_THEME_COLOR_VARS) { + const cached = localStorage.getItem(colorVar); + if (cached) { + document.body.style.setProperty(colorVar, cached); + } } } diff --git a/gui/src/components/mainInput/CodeBlockExtension.tsx b/gui/src/components/mainInput/CodeBlockExtension.tsx new file mode 100644 index 000000000..351333715 --- /dev/null +++ b/gui/src/components/mainInput/CodeBlockExtension.tsx @@ -0,0 +1,54 @@ +import { mergeAttributes, Node } from "@tiptap/core"; +import { NodeViewWrapper, ReactNodeViewRenderer } from "@tiptap/react"; +import { ContextItemWithId } from "core"; +import CodeSnippetPreview from "../markdown/CodeSnippetPreview"; + +const CodeBlockComponent = ({ node, deleteNode }) => { + const item: ContextItemWithId = node.attrs.item; + return ( + + { + deleteNode(); + }} + /> + + ); +}; + +export default Node.create({ + name: "codeBlock", + + group: "block", + + content: "inline*", + + atom: true, + + selectable: true, + + parseHTML() { + return [ + { + tag: "code-block", + }, + ]; + }, + + addAttributes() { + return { + item: { + default: "", + }, + }; + }, + + renderHTML({ HTMLAttributes }) { + return ["code-block", mergeAttributes(HTMLAttributes), 0]; + }, + + addNodeView() { + return ReactNodeViewRenderer(CodeBlockComponent); + }, +}); diff --git a/gui/src/components/mainInput/ComboBox.tsx b/gui/src/components/mainInput/ComboBox.tsx index 998d4e6c8..a6252fe50 100644 --- a/gui/src/components/mainInput/ComboBox.tsx +++ b/gui/src/components/mainInput/ComboBox.tsx @@ -1151,10 +1151,7 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { {showContextToggleOn && selectedContextItems.length > 0 && (
{selectedContextItems.map((item) => ( - + ))}
)} diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index 9e6326246..4a5ed8d0c 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -4,6 +4,7 @@ import styled, { keyframes } from "styled-components"; import { defaultBorderRadius, vscBackground } from ".."; import { RootStore } from "../../redux/store"; import TipTapEditor from "./TipTapEditor"; +import { ComboBoxItemType } from "./types"; const gradient = keyframes` 0% { @@ -62,6 +63,7 @@ function ContinueInputBox(props: ContinueInputBoxProps) { return { title: `/${cmd.name}`, description: cmd.description, + type: "slashCommand" as ComboBoxItemType, }; }) || [] ); diff --git a/gui/src/components/mainInput/InlineSpan.tsx b/gui/src/components/mainInput/InlineSpan.tsx deleted file mode 100644 index 5b36e2a87..000000000 --- a/gui/src/components/mainInput/InlineSpan.tsx +++ /dev/null @@ -1,21 +0,0 @@ -interface InlineSpanProps { - color?: string; - children: React.ReactNode; -} - -function InlineSpan(props: InlineSpanProps) { - return ( - - {props.children} - - ); -} - -export default InlineSpan; diff --git a/gui/src/components/mainInput/InputToolbar.tsx b/gui/src/components/mainInput/InputToolbar.tsx index 9654d46f1..091ca9b81 100644 --- a/gui/src/components/mainInput/InputToolbar.tsx +++ b/gui/src/components/mainInput/InputToolbar.tsx @@ -1,7 +1,7 @@ import styled from "styled-components"; import { defaultBorderRadius, lightGray, secondaryDark } from ".."; -const StyledDiv = styled.div` +const StyledDiv = styled.div<{ hidden?: boolean }>` position: absolute; display: flex; gap: 4px; @@ -10,6 +10,8 @@ const StyledDiv = styled.div` width: calc(100% - 10px); background-color: ${secondaryDark}; + ${(props) => (props.hidden ? "display: none;" : "")} + align-items: center; z-index: 50; font-size: 10px; diff --git a/gui/src/components/mainInput/MentionList.tsx b/gui/src/components/mainInput/MentionList.tsx index fc1caa400..59a1b6957 100644 --- a/gui/src/components/mainInput/MentionList.tsx +++ b/gui/src/components/mainInput/MentionList.tsx @@ -2,6 +2,7 @@ import { ArrowUpOnSquareIcon, AtSymbolIcon, BeakerIcon, + ChevronDoubleRightIcon, Cog6ToothIcon, CommandLineIcon, ExclamationCircleIcon, @@ -24,7 +25,7 @@ import { vscForeground, } from ".."; import FileIcon from "../FileIcon"; -import { ComboBoxItem } from "./types"; +import { ComboBoxItem, ComboBoxItemType } from "./types"; const ICONS_FOR_DROPDOWN: { [key: string]: any } = { file: FolderIcon, @@ -45,11 +46,21 @@ const ICONS_FOR_DROPDOWN: { [key: string]: any } = { "/issue": ExclamationCircleIcon, }; -function DropdownIcon(props: { provider: string; className?: string }) { +function DropdownIcon(props: { + provider: string; + className?: string; + type: ComboBoxItemType; +}) { const Icon = ICONS_FOR_DROPDOWN[props.provider]; if (!Icon) { - return ( + return props.type === "contextProvider" ? ( + ) : ( + ); } return ; @@ -89,15 +100,26 @@ const ItemDiv = styled.div` interface MentionListProps { items: ComboBoxItem[]; command: (item: ComboBoxItem) => void; - subMenuTitle?: string; + + enterSubmenu?: () => void; } const MentionList = forwardRef((props: MentionListProps, ref) => { const [selectedIndex, setSelectedIndex] = useState(0); + const [subMenuTitle, setSubMenuTitle] = useState( + undefined + ); + const selectItem = (index) => { const item = props.items[index]; + if (item.id === "file") { + setSubMenuTitle("Files - Type to search"); + props.enterSubmenu(); // TODO: how to pass this prop? + return; + } + if (item) { props.command(item); } @@ -142,6 +164,7 @@ const MentionList = forwardRef((props: MentionListProps, ref) => { return ( + {subMenuTitle && {subMenuTitle}} {props.items.length ? ( props.items.map((item, index) => ( { >
- {props.subMenuTitle && ( + {item.type === "file" && ( )} - {/* */} - + {item.type !== "file" && ( + + )} {item.title} {" "}
diff --git a/gui/src/components/mainInput/TipTapEditor.css b/gui/src/components/mainInput/TipTapEditor.css index e2ba77ecb..b5fbd1a7c 100644 --- a/gui/src/components/mainInput/TipTapEditor.css +++ b/gui/src/components/mainInput/TipTapEditor.css @@ -23,3 +23,7 @@ height: 0; pointer-events: none; } + +.gap-cursor { + border-top: 1px solid white; +} diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index 6b261a01a..f0a3d0eff 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -5,8 +5,8 @@ import Paragraph from "@tiptap/extension-paragraph"; import Placeholder from "@tiptap/extension-placeholder"; import Text from "@tiptap/extension-text"; import { EditorContent, JSONContent, useEditor } from "@tiptap/react"; -import { IContextProvider } from "core"; -import { useEffect, useState } from "react"; +import { ContextItemWithId, IContextProvider, RangeInFile } from "core"; +import { useContext, useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import styled from "styled-components"; import { @@ -15,10 +15,13 @@ import { secondaryDark, vscForeground, } from ".."; +import { SearchContext } from "../../App"; import useHistory from "../../hooks/useHistory"; import { setTakenActionTrue } from "../../redux/slices/miscSlice"; import { RootStore } from "../../redux/store"; -import { postToIde } from "../../util/ide"; +import { isMetaEquivalentKeyPressed } from "../../util"; +import { isJetBrains, postToIde } from "../../util/ide"; +import CodeBlockExtension from "./CodeBlockExtension"; import { SlashCommand } from "./CommandsExtension"; import InputToolbar from "./InputToolbar"; import "./TipTapEditor.css"; @@ -34,7 +37,7 @@ const InputBoxDiv = styled.div` border-radius: ${defaultBorderRadius}; margin: 0; height: auto; - width: 100%; + width: calc(100% - 18px); background-color: ${secondaryDark}; color: ${vscForeground}; z-index: 1; @@ -66,13 +69,13 @@ interface TipTapEditorProps { function TipTapEditor(props: TipTapEditorProps) { const dispatch = useDispatch(); + const [miniSearch, firstResults] = useContext(SearchContext); + const historyLength = useSelector( (store: RootStore) => store.state.history.length ); - const contextProviders = useSelector( - (state: RootStore) => state.state.config.contextProviders || [] - ); + const [inputFocused, setInputFocused] = useState(false); const { saveSession } = useHistory(dispatch); @@ -93,10 +96,11 @@ function TipTapEditor(props: TipTapEditorProps) { // Enter: () => // this.editor.commands.first(({ commands }) => [ // () => { - // props.onEnter(collectInput(editor)); + // props.onEnter(this.editor.getJSON()); // return true; // }, // ]), + // Enter: () => true, "Shift-Enter": () => this.editor.commands.first(({ commands }) => [ @@ -124,7 +128,22 @@ function TipTapEditor(props: TipTapEditorProps) { HTMLAttributes: { class: "mention", }, - suggestion: getMentionSuggestion(props.availableContextProviders), + suggestion: getMentionSuggestion( + props.availableContextProviders, + miniSearch, + firstResults, + () => { + const contents = editor.getText(); + const indexOfAt = contents.lastIndexOf("@"); + + editor.commands.deleteRange({ + from: indexOfAt, + to: contents.length, + }); + + editor.commands.focus(); + } + ), renderLabel: (props) => { return `@${props.node.attrs.label || props.node.attrs.id}`; }, @@ -138,6 +157,7 @@ function TipTapEditor(props: TipTapEditorProps) { return props.node.attrs.label; }, }), + CodeBlockExtension, ], editorProps: { attributes: { @@ -147,18 +167,39 @@ function TipTapEditor(props: TipTapEditorProps) { }, content: props.editorState || props.content || "", }, - [props.availableContextProviders, historyLength] + [props.availableContextProviders, historyLength, miniSearch, firstResults] ); - const [inputFocused, setInputFocused] = useState(false); + // This is a mechanism for overriding the IDE keyboard shortcut when inside of the webview + const [ignoreHighlightedCode, setIgnoreHighlightedCode] = useState(false); useEffect(() => { - if (!editor) return; + const handleKeyDown = (event: any) => { + if ( + isMetaEquivalentKeyPressed(event) && + (isJetBrains() ? event.code === "KeyJ" : event.code === "KeyM") + ) { + setIgnoreHighlightedCode(true); + setTimeout(() => { + setIgnoreHighlightedCode(false); + }, 100); + } + }; + window.addEventListener("keydown", handleKeyDown); + + return () => { + window.removeEventListener("keydown", handleKeyDown); + }; + }, []); + + // IDE event listeners + useEffect(() => { if (props.isMainInput) { - editor.commands.focus(); + editor?.commands.focus(); } - const handler = (event: any) => { + const handler = async (event: any) => { + if (!editor) return; if (event.data.type === "focusContinueInput") { editor.commands.focus(); if (historyLength > 0) { @@ -178,13 +219,39 @@ function TipTapEditor(props: TipTapEditorProps) { } else if (event.data.type === "focusContinueInputWithNewSession") { saveSession(); dispatch(setTakenActionTrue(null)); + } else if (event.data.type === "highlightedCode") { + if (!ignoreHighlightedCode) { + const rif: RangeInFile & { contents: string } = + event.data.rangeInFileWithContents; + const basename = rif.filepath.split(/[\\/]/).pop(); + const item: ContextItemWithId = { + content: rif.contents, + name: `${basename} (${rif.range.start.line + 1}-${ + rif.range.end.line + 1 + })`, + description: rif.filepath, + id: { + providerTitle: "file", + itemId: rif.filepath, + }, + }; + editor.commands.insertContentAt(0, { + type: "codeBlock", + attrs: { + item, + }, + }); + await new Promise((resolve) => setTimeout(resolve, 100)); + editor.commands.focus("end"); + } + setIgnoreHighlightedCode(false); } }; window.addEventListener("message", handler); return () => { window.removeEventListener("message", handler); }; - }, [editor, props.isMainInput, historyLength]); + }, [editor, props.isMainInput, historyLength, ignoreHighlightedCode]); return ( @@ -193,9 +260,14 @@ function TipTapEditor(props: TipTapEditorProps) { onFocus={() => { setInputFocused(true); }} - onBlur={(e) => { + onBlur={() => { setInputFocused(false); }} + // onKeyDown={(e) => { + // if (e.key === "Enter") { + // props.onEnter(editor.getJSON()); + // } + // }} />