import { ChevronRightIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { CodeToEdit } from "core"; import { getMarkdownLanguageTagForFile } from "core/util"; import { getUriPathBasename } from "core/util/uri"; import { useContext, useMemo, useState } from "react"; import { useDispatch } from "react-redux"; import styled from "styled-components"; import { IdeMessengerContext } from "../../context/IdeMessenger"; import { useAppSelector } from "../../redux/hooks"; import { clearCodeToEdit } from "../../redux/slices/editModeState"; import { getMetaKeyLabel } from "../../util"; import FileIcon from "../FileIcon"; import StyledMarkdownPreview from "../StyledMarkdownPreview"; import { useFontSize } from "../ui/font"; const NoPaddingWrapper = styled.div` > * { margin: 0 !important; padding: 0 !important; } pre { margin: 0 !important; padding: 0px 0px 0px 10px !important; } `; interface CodeToEditItemProps { code: CodeToEdit; } function CodeToEditItem({ code }: CodeToEditItemProps) { const dispatch = useDispatch(); const ideMessenger = useContext(IdeMessengerContext); const [showCodeSnippet, setShowCodeSnippet] = useState(false); function onDelete() { dispatch(clearCodeToEdit()); ideMessenger.post("edit/clearDecorations", undefined); } const fileName = getUriPathBasename(code.filepath); let isInsertion = false; let title = fileName; if ("range" in code) { const start = code.range.start.line + 1; const end = code.range.end.line + 1; isInsertion = start === end; title += isInsertion ? ` - Inserting at line ${start}` : ` (${start} - ${end})`; } const source = "```" + getMarkdownLanguageTagForFile(code.filepath) + "\n" + code.contents + "\n" + "```"; async function onClickFilename() { if ("range" in code) { await ideMessenger.ide.showLines( code.filepath, code.range.start.line, code.range.end.line, ); } else { await ideMessenger.ide.openFile(code.filepath); } } const smallFont = useFontSize(-3); const iconClass = "text-lightgray hover:bg-lightgray hover:text-vsc-foreground h-3.5 w-3.5 cursor-pointer rounded-sm p-0.5 hover:bg-opacity-20"; return (
Code to edit {/* */}
{ if (!isInsertion) { setShowCodeSnippet((showCodeSnippet) => !showCodeSnippet); } }} >
{ e.stopPropagation(); void onClickFilename(); }} > {title}
{ e.stopPropagation(); setShowCodeSnippet((val) => !val); }} className={`text-lightgray hover:bg-lightgray hover:text-vsc-foreground h-3.5 w-3.5 cursor-pointer rounded-sm p-0.5 hover:bg-opacity-20 ${showCodeSnippet ? "rotate-90" : ""} ${isInsertion ? "hidden" : ""}`} />
{ e.stopPropagation(); void onDelete(); }} className={iconClass} />
{showCodeSnippet && (
)}
); } export default function CodeToEditCard() { // Array from previous multi-file edit const codeToEditArray = useAppSelector( (state) => state.editModeState.codeToEdit, ); const codeToEdit = useMemo(() => { return codeToEditArray[0]; }, [codeToEditArray]); const metaKeyLabel = useMemo(() => { return getMetaKeyLabel(); }, []); const tinyFont = useFontSize(-3); return (
{codeToEdit ? ( ) : ( <> {`To edit code, highlight it and press `} {metaKeyLabel} I )}
); }