🐛 fix Enter

This commit is contained in:
Nate Sesti 2023-12-25 00:55:57 -05:00
parent 1142ce9e60
commit 55fed41099
7 changed files with 52 additions and 2007 deletions

View file

@ -3,7 +3,7 @@ import {
HandThumbUpIcon,
} from "@heroicons/react/24/outline";
import { ChatHistoryItem } from "core";
import { useEffect, useRef, useState } from "react";
import { useState } from "react";
import { useSelector } from "react-redux";
import styled from "styled-components";
import {
@ -60,24 +60,10 @@ const ContentDiv = styled.div<{ isUserInput: boolean; fontSize?: number }>`
function StepContainer(props: StepContainerProps) {
const [isHovered, setIsHovered] = useState(false);
const naturalLanguageInputRef = useRef<HTMLTextAreaElement>(null);
const userInputRef = useRef<HTMLInputElement>(null);
const isUserInput = props.item.message.role === "user";
const sessionHistory = useSelector((store: RootStore) => store.state.history);
const active = useSelector((store: RootStore) => store.state.active);
useEffect(() => {
if (userInputRef?.current) {
userInputRef.current.focus();
}
}, [userInputRef]);
useEffect(() => {
if (isHovered) {
naturalLanguageInputRef.current?.focus();
}
}, [isHovered]);
const [feedback, setFeedback] = useState<boolean | undefined>(undefined);
const sendFeedback = (feedback: boolean) => {

File diff suppressed because it is too large Load diff

View file

@ -157,6 +157,8 @@ const MentionList = forwardRef((props: MentionListProps, ref) => {
if (event.key === "Enter") {
enterHandler();
event.stopPropagation();
event.preventDefault();
return true;
}

View file

@ -1,327 +0,0 @@
import {
ExclamationTriangleIcon,
PaintBrushIcon,
TrashIcon,
} from "@heroicons/react/24/outline";
import { ContextItemWithId } from "core";
import { useCallback, useEffect, useRef, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import styled from "styled-components";
import {
StyledTooltip,
defaultBorderRadius,
lightGray,
secondaryDark,
vscForeground,
} from "..";
import { setEditingAtIds } from "../../redux/slices/stateSlice";
import { RootStore } from "../../redux/store";
import { getFontSize } from "../../util";
import { postToIde } from "../../util/ide";
import FileIcon from "../FileIcon";
import HeaderButtonWithText from "../HeaderButtonWithText";
const Button = styled.button<{ fontSize?: number }>`
border: none;
color: ${vscForeground};
background-color: ${secondaryDark};
border-radius: ${defaultBorderRadius};
padding: 3.5px;
padding-left: 3px;
padding-right: 5px;
overflow: hidden;
font-size: ${(props) => props.fontSize || getFontSize()}px;
cursor: pointer;
`;
const GridDiv = styled.div`
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
display: grid;
grid-gap: 0;
align-items: center;
border-radius: ${defaultBorderRadius};
background-color: ${secondaryDark};
`;
const ButtonDiv = styled.div<{ backgroundColor: string }>`
background-color: ${secondaryDark};
height: 100%;
display: flex;
align-items: center;
&:hover {
background-color: ${(props) => props.backgroundColor};
}
`;
const CircleDiv = styled.div`
position: absolute;
top: -10px;
right: -10px;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: red;
color: white;
display: flex;
align-items: center;
justify-content: center;
padding: 2px;
`;
interface PillButtonProps {
onHover?: (arg0: boolean) => void;
item: ContextItemWithId;
editing: boolean;
editingAny: boolean;
index: number;
inputIndex: number; // index of the ComboBox
areMultipleItems?: boolean;
onDelete?: (index?: number) => void;
onClick?: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
stepIndex?: number;
previewing?: boolean;
toggleViewContent?: () => void;
onBlur?: () => void;
focusing?: boolean;
prefixInputWithEdit?: (should: boolean) => void;
}
interface StyledButtonProps {
borderColor?: string;
editing?: boolean;
}
const Container = styled.div<{ previewing?: boolean }>`
border-radius: ${defaultBorderRadius};
background-color: ${secondaryDark};
display: flex;
align-items: center;
justify-content: center;
`;
const StyledButton = styled(Button)<StyledButtonProps>`
position: relative;
border-color: ${(props) => props.borderColor || "transparent"};
border-width: 1px;
border-style: solid;
&:focus {
outline: none;
/* border-color: ${lightGray}; */
text-decoration: underline;
}
`;
const HoverableInsidePillButton = styled(HeaderButtonWithText)<{
color: string;
}>`
&:hover {
background-color: ${(props) => props.color};
}
`;
const ClickableInsidePillButton = styled(HeaderButtonWithText)<{
color: string;
selected: boolean;
}>`
${(props) =>
props.selected &&
`
background-color: ${props.color};
&:hover {
background-color: ${props.color};
}
`}
`;
const PillButton = (props: PillButtonProps) => {
const [isHovered, setIsHovered] = useState(false);
const dispatch = useDispatch();
const [warning, setWarning] = useState<string | undefined>(undefined);
useEffect(() => {
if (props.editing && props.item.content.length > 4000) {
setWarning("Editing such a large range may be slow");
} else {
setWarning(undefined);
}
}, [props.editing, props.item]);
const setEditing = useCallback(() => {
if (!props.editing) {
dispatch(
setEditingAtIds({
ids: [props.item.id],
index: props.inputIndex,
})
);
}
if (!props.editingAny) {
props.prefixInputWithEdit?.(true);
}
if (props.editingAny && props.editing) {
props.prefixInputWithEdit?.(false);
}
}, [props.editing, props.editingAny, props.item, props.inputIndex]);
const pillContainerRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
const selectedContextItems = useSelector(
(store: RootStore) => store.state.contextItems
);
return (
<div style={{ position: "relative" }}>
<Container previewing={props.previewing} ref={pillContainerRef}>
<StyledButton
fontSize={getFontSize() - 1}
borderColor={
props.editing ? (warning ? "red" : undefined) : undefined
}
ref={buttonRef}
onMouseEnter={() => {
setIsHovered(true);
if (props.onHover) {
props.onHover(true);
}
}}
onMouseLeave={() => {
setIsHovered(false);
if (props.onHover) {
props.onHover(false);
}
}}
className={`pill-button-${props.stepIndex || "main"}`}
onKeyDown={(e) => {
if (e.key === "Backspace") {
props.onDelete?.(props.stepIndex);
} else if (e.key === "v") {
props.toggleViewContent?.();
} else if (e.key === "e") {
setEditing();
}
}}
onClick={(e) => {
props.onClick?.(e);
if (props.item.id.providerTitle === "file") {
postToIde("showFile", {
filepath: props.item.description,
});
} else if (props.item.id.providerTitle === "code") {
const lines = props.item.name
.split("(")[1]
.split(")")[0]
.split("-");
postToIde("showLines", {
filepath: props.item.description,
start: parseInt(lines[0]) - 1,
end: parseInt(lines[1]) - 1,
});
} else {
postToIde("showVirtualFile", {
name: props.item.name,
content: props.item.content,
});
}
}}
onBlur={(e) => {
if (!pillContainerRef.current?.contains(e.relatedTarget as any)) {
// props.onBlur?.();
} else {
e.preventDefault();
buttonRef.current?.focus();
}
}}
>
<span className={(isHovered ? "underline" : "") + " flex"}>
{["file", "code", "open"].includes(props.item.id.providerTitle) && (
<FileIcon
height="16px"
width="16px"
filename={props.item.name.split(" ").at(0)}
></FileIcon>
)}
{props.item.name}
</span>
</StyledButton>
{(props.editingAny ||
(props.focusing && props.item.editable) ||
(props.editing && props.editingAny)) && (
<>
<ClickableInsidePillButton
data-tooltip-id={`circle-div-${props.item.name}`}
text={
props.editing ? "Editing this range" : "Edit this range (e)"
}
onClick={setEditing}
tabIndex={-1}
color="#f0f4"
selected={props.editing}
>
<PaintBrushIcon width="1.4em" height="1.4em" />
</ClickableInsidePillButton>
<StyledTooltip id={`circle-div-${props.item.name}`}>
Editing this range
</StyledTooltip>
</>
)}
{/* {(props.focusing || props.previewing) && (
<ClickableInsidePillButton
text="View (v)"
onClick={() => props.toggleViewContent?.()}
tabIndex={-1}
color="#ff04"
selected={props.previewing || false}
>
<EyeIcon width="1.4em" height="1.4em" />
</ClickableInsidePillButton>
)} */}
{props.focusing && (
<HoverableInsidePillButton
text="Delete (⌫)"
onClick={() => props.onDelete?.(props.stepIndex)}
tabIndex={-1}
color="#f004"
>
<TrashIcon width="1.4em" height="1.4em" />
</HoverableInsidePillButton>
)}
</Container>
<StyledTooltip id={`edit-${props.index}`}>
{props.item.editing
? "Editing this section (with entire file as context)"
: "Edit this section"}
</StyledTooltip>
<StyledTooltip id={`delete-${props.index}`}>Delete</StyledTooltip>
{props.editing && warning && (
<>
<CircleDiv
data-tooltip-id={`circle-div-${props.item.name}`}
className="z-10"
>
<ExclamationTriangleIcon
style={{ margin: "auto" }}
width="1.0em"
strokeWidth={2}
/>
</CircleDiv>
<StyledTooltip id={`circle-div-${props.item.name}`}>
{warning}
</StyledTooltip>
</>
)}
</div>
);
};
export default PillButton;

View file

@ -81,6 +81,7 @@ function TipTapEditor(props: TipTapEditorProps) {
const { saveSession } = useHistory(dispatch);
const inSubmenu = useRef(false);
const inDropdown = useRef(false);
const enterSubmenu = async (editor: Editor) => {
const contents = editor.getText();
@ -105,6 +106,11 @@ function TipTapEditor(props: TipTapEditorProps) {
const onClose = () => {
inSubmenu.current = false;
inDropdown.current = false;
};
const onOpen = () => {
inDropdown.current = true;
};
const editor = useEditor(
@ -121,14 +127,19 @@ function TipTapEditor(props: TipTapEditorProps) {
Paragraph.extend({
addKeyboardShortcuts() {
return {
// Enter: () =>
// this.editor.commands.first(({ commands }) => [
// () => {
// props.onEnter(this.editor.getJSON());
// return true;
// },
// ]),
// Enter: () => true,
Enter: () => {
if (inDropdown.current) {
return false;
}
props.onEnter(this.editor.getJSON());
return true;
},
"Cmd-Enter": () => {
props.onEnter(this.editor.getJSON());
return true;
},
"Shift-Enter": () =>
this.editor.commands.first(({ commands }) => [
@ -155,6 +166,7 @@ function TipTapEditor(props: TipTapEditorProps) {
firstResults,
enterSubmenu,
onClose,
onOpen,
inSubmenu
),
renderLabel: (props) => {
@ -165,7 +177,11 @@ function TipTapEditor(props: TipTapEditorProps) {
HTMLAttributes: {
class: "mention",
},
suggestion: getCommandSuggestion(props.availableSlashCommands),
suggestion: getCommandSuggestion(
props.availableSlashCommands,
onClose,
onOpen
),
renderLabel: (props) => {
return props.node.attrs.label;
},
@ -180,7 +196,13 @@ function TipTapEditor(props: TipTapEditorProps) {
},
content: props.editorState || props.content || "",
},
[props.availableContextProviders, historyLength, miniSearch, firstResults]
[
props.availableContextProviders,
historyLength,
miniSearch,
firstResults,
inDropdown,
]
);
// This is a mechanism for overriding the IDE keyboard shortcut when inside of the webview
@ -221,17 +243,18 @@ function TipTapEditor(props: TipTapEditorProps) {
editor.commands.insertContent(input);
props.onEnter(editor.getJSON());
} else if (event.data.type === "focusContinueInput") {
editor.commands.focus();
if (historyLength > 0) {
saveSession();
}
dispatch(setTakenActionTrue(null));
editor.commands.focus("end");
} else if (event.data.type === "focusContinueInputWithoutClear") {
editor.commands.focus();
editor.commands.focus("end");
dispatch(setTakenActionTrue(null));
} else if (event.data.type === "focusContinueInputWithNewSession") {
saveSession();
dispatch(setTakenActionTrue(null));
editor.commands.focus("end");
} else if (event.data.type === "highlightedCode") {
if (!ignoreHighlightedCode) {
const rif: RangeInFile & { contents: string } =
@ -276,11 +299,6 @@ function TipTapEditor(props: TipTapEditorProps) {
onBlur={() => {
setInputFocused(false);
}}
// onKeyDown={(e) => {
// if (e.key === "Enter") {
// props.onEnter(editor.getJSON());
// }
// }}
/>
<InputToolbar
hidden={!(inputFocused || props.isMainInput)}

View file

@ -9,7 +9,8 @@ import { ComboBoxItem, ComboBoxItemType } from "./types";
function getSuggestion(
items: (props: { query: string }) => ComboBoxItem[],
enterSubmenu: (editor: Editor) => void = (editor) => {},
onClose: () => void = () => {}
onClose: () => void = () => {},
onOpen: () => void = () => {}
) {
return {
items,
@ -39,6 +40,8 @@ function getSuggestion(
trigger: "manual",
placement: "bottom-start",
});
onOpen();
},
onUpdate(props) {
@ -86,7 +89,7 @@ function getFileItems(
res = firstResults;
}
return (
res?.slice(0, 20).map((hit) => {
res?.slice(0, 10).map((hit) => {
const item = {
title: hit.basename,
description: hit.basename,
@ -106,6 +109,7 @@ export function getMentionSuggestion(
firstResults: any[],
enterSubmenu: (editor: Editor) => void,
onClose: () => void,
onOpen: () => void,
inSubmenu: MutableRefObject<boolean>
) {
const items = ({ query }) => {
@ -139,10 +143,14 @@ export function getMentionSuggestion(
}
return mainResults;
};
return getSuggestion(items, enterSubmenu, onClose);
return getSuggestion(items, enterSubmenu, onClose, onOpen);
}
export function getCommandSuggestion(availableSlashCommands: ComboBoxItem[]) {
export function getCommandSuggestion(
availableSlashCommands: ComboBoxItem[],
onClose: () => void,
onOpen: () => void
) {
const items = ({ query }) => {
return (
availableSlashCommands?.filter((slashCommand) => {
@ -159,5 +167,5 @@ export function getCommandSuggestion(availableSlashCommands: ComboBoxItem[]) {
type: "slashCommand" as ComboBoxItemType,
}));
};
return getSuggestion(items);
return getSuggestion(items, undefined, onClose, onOpen);
}

View file

@ -62,7 +62,7 @@ async function resolveEditorContent(
function resolveParagraph(p: JSONContent) {
let text = "";
const contextItems = [];
for (const child of p.content) {
for (const child of p.content || []) {
if (child.type === "text") {
text += child.text;
} else if (child.type === "mention") {