| 1 | import { ChevronRightIcon, XMarkIcon } from "@heroicons/react/24/outline"; |
| 2 | import { CodeToEdit } from "core"; |
| 3 | import { getMarkdownLanguageTagForFile } from "core/util"; |
| 4 | import { getUriPathBasename } from "core/util/uri"; |
| 5 | import { useContext, useMemo, useState } from "react"; |
| 6 | import { useDispatch } from "react-redux"; |
| 7 | import styled from "styled-components"; |
| 8 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 9 | import { useAppSelector } from "../../redux/hooks"; |
| 10 | import { clearCodeToEdit } from "../../redux/slices/editModeState"; |
| 11 | import { getMetaKeyLabel } from "../../util"; |
| 12 | import FileIcon from "../FileIcon"; |
| 13 | import StyledMarkdownPreview from "../StyledMarkdownPreview"; |
| 14 | import { useFontSize } from "../ui/font"; |
| 15 | |
| 16 | const NoPaddingWrapper = styled.div` |
| 17 | > * { |
| 18 | margin: 0 !important; |
| 19 | padding: 0 !important; |
| 20 | } |
| 21 | |
| 22 | pre { |
| 23 | margin: 0 !important; |
| 24 | padding: 0px 0px 0px 10px !important; |
| 25 | } |
| 26 | `; |
| 27 | |
| 28 | interface CodeToEditItemProps { |
| 29 | code: CodeToEdit; |
| 30 | } |
| 31 | |
| 32 | function CodeToEditItem({ code }: CodeToEditItemProps) { |
| 33 | const dispatch = useDispatch(); |
| 34 | const ideMessenger = useContext(IdeMessengerContext); |
| 35 | const [showCodeSnippet, setShowCodeSnippet] = useState(false); |
| 36 | |
| 37 | function onDelete() { |
| 38 | dispatch(clearCodeToEdit()); |
| 39 | ideMessenger.post("edit/clearDecorations", undefined); |
| 40 | } |
| 41 | |
| 42 | const fileName = getUriPathBasename(code.filepath); |
| 43 | |
| 44 | let isInsertion = false; |
| 45 | let title = fileName; |
| 46 | |
| 47 | if ("range" in code) { |
| 48 | const start = code.range.start.line + 1; |
| 49 | const end = code.range.end.line + 1; |
| 50 | |
| 51 | isInsertion = start === end; |
| 52 | |
| 53 | title += isInsertion |
| 54 | ? ` - Inserting at line ${start}` |
| 55 | : ` (${start} - ${end})`; |
| 56 | } |
| 57 | |
| 58 | const source = |
| 59 | "```" + |
| 60 | getMarkdownLanguageTagForFile(code.filepath) + |
| 61 | "\n" + |
| 62 | code.contents + |
| 63 | "\n" + |
| 64 | "```"; |
| 65 | |
| 66 | async function onClickFilename() { |
| 67 | if ("range" in code) { |
| 68 | await ideMessenger.ide.showLines( |
| 69 | code.filepath, |
| 70 | code.range.start.line, |
| 71 | code.range.end.line, |
| 72 | ); |
| 73 | } else { |
| 74 | await ideMessenger.ide.openFile(code.filepath); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const smallFont = useFontSize(-3); |
| 79 | |
| 80 | const iconClass = |
| 81 | "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"; |
| 82 | |
| 83 | return ( |
| 84 | <div className="flex flex-col gap-1 py-0.5"> |
| 85 | <div className="group flex flex-row items-center justify-start gap-1.5 px-1"> |
| 86 | <span |
| 87 | className="hidden flex-shrink-0 pl-2 font-semibold text-gray-400 sm:flex" |
| 88 | style={{ |
| 89 | fontSize: smallFont, |
| 90 | }} |
| 91 | > |
| 92 | Code to edit |
| 93 | </span> |
| 94 | {/* <CodeBracketIcon className="h-3 w-3 flex-shrink-0 text-gray-400 sm:hidden" /> */} |
| 95 | <div |
| 96 | className={`hover:bg-lightgray flex flex-1 cursor-pointer items-center justify-between rounded transition-colors hover:bg-opacity-20 ${showCodeSnippet && "bg-lightgray bg-opacity-20"}`} |
| 97 | onClick={() => { |
| 98 | if (!isInsertion) { |
| 99 | setShowCodeSnippet((showCodeSnippet) => !showCodeSnippet); |
| 100 | } |
| 101 | }} |
| 102 | > |
| 103 | <div className="flex items-center gap-0.5"> |
| 104 | <FileIcon filename={code.filepath} height={"18px"} width={"18px"} /> |
| 105 | <span |
| 106 | className="line-clamp-1 text-xs hover:underline" |
| 107 | onClick={(e) => { |
| 108 | e.stopPropagation(); |
| 109 | void onClickFilename(); |
| 110 | }} |
| 111 | > |
| 112 | {title} |
| 113 | </span> |
| 114 | </div> |
| 115 | |
| 116 | <div className="invisible flex items-center group-hover:visible"> |
| 117 | <ChevronRightIcon |
| 118 | onClick={(e) => { |
| 119 | e.stopPropagation(); |
| 120 | setShowCodeSnippet((val) => !val); |
| 121 | }} |
| 122 | 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" : ""}`} |
| 123 | /> |
| 124 | <div className="flex items-center"> |
| 125 | <XMarkIcon |
| 126 | onClick={(e) => { |
| 127 | e.stopPropagation(); |
| 128 | void onDelete(); |
| 129 | }} |
| 130 | className={iconClass} |
| 131 | /> |
| 132 | </div> |
| 133 | </div> |
| 134 | </div> |
| 135 | </div> |
| 136 | {showCodeSnippet && ( |
| 137 | <div className="!m-0 max-h-48 overflow-y-auto !p-0"> |
| 138 | <NoPaddingWrapper> |
| 139 | <StyledMarkdownPreview source={source} /> |
| 140 | </NoPaddingWrapper> |
| 141 | </div> |
| 142 | )} |
| 143 | </div> |
| 144 | ); |
| 145 | } |
| 146 | export default function CodeToEditCard() { |
| 147 | // Array from previous multi-file edit |
| 148 | const codeToEditArray = useAppSelector( |
| 149 | (state) => state.editModeState.codeToEdit, |
| 150 | ); |
| 151 | const codeToEdit = useMemo(() => { |
| 152 | return codeToEditArray[0]; |
| 153 | }, [codeToEditArray]); |
| 154 | |
| 155 | const metaKeyLabel = useMemo(() => { |
| 156 | return getMetaKeyLabel(); |
| 157 | }, []); |
| 158 | |
| 159 | const tinyFont = useFontSize(-3); |
| 160 | |
| 161 | return ( |
| 162 | <div className="bg-vsc-editor-background mx-3 flex flex-col rounded-t-lg py-1"> |
| 163 | {codeToEdit ? ( |
| 164 | <CodeToEditItem code={codeToEdit} /> |
| 165 | ) : ( |
| 166 | <> |
| 167 | <span |
| 168 | className="text-balance px-3 text-center font-semibold text-gray-400" |
| 169 | style={{ |
| 170 | fontSize: tinyFont, |
| 171 | }} |
| 172 | > |
| 173 | {`To edit code, highlight it and press `} |
| 174 | <kbd>{metaKeyLabel}</kbd> <kbd>I</kbd> |
| 175 | </span> |
| 176 | </> |
| 177 | )} |
| 178 | </div> |
| 179 | ); |
| 180 | } |