From 1154432924ddb9941cb19f8042bc7b78de5d5053 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 5 Dec 2024 18:58:24 -0500 Subject: [PATCH] Adds count of occurences and index to find and replace (#8070) * Adds count of occurences and index to find and replace * Disable replace buttons also --- app/editor/components/FindAndReplace.tsx | 48 +++++++++++++++++++++--- app/editor/extensions/FindAndReplace.tsx | 6 +++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/app/editor/components/FindAndReplace.tsx b/app/editor/components/FindAndReplace.tsx index 85bd410c94..10ab1e4e5b 100644 --- a/app/editor/components/FindAndReplace.tsx +++ b/app/editor/components/FindAndReplace.tsx @@ -25,10 +25,18 @@ import { altDisplay, isModKey, metaDisplay } from "~/utils/keyboard"; import { useEditor } from "./EditorContext"; type Props = { + /** Whether the find and replace popover is open */ open: boolean; + /** Callback when the find and replace popover is opened */ onOpen: () => void; + /** Callback when the find and replace popover is closed */ onClose: () => void; + /** Whether the editor is in read-only mode */ readOnly?: boolean; + /** The current highlighted index in the search results */ + currentIndex: number; + /** The total number of search results */ + totalResults: number; }; export default function FindAndReplace({ @@ -36,6 +44,8 @@ export default function FindAndReplace({ open, onOpen, onClose, + currentIndex, + totalResults, }: Props) { const editor = useEditor(); const finalFocusRef = React.useRef( @@ -270,6 +280,7 @@ export default function FindAndReplace({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [popover.visible]); + const disabled = totalResults === 0; const navigation = ( <> - editor.commands.prevSearchMatch()}> + editor.commands.prevSearchMatch()} + > @@ -288,7 +302,10 @@ export default function FindAndReplace({ delay={500} placement="bottom" > - editor.commands.nextSearchMatch()}> + editor.commands.nextSearchMatch()} + > @@ -306,7 +323,7 @@ export default function FindAndReplace({ width={420} > - + )} + + {totalResults > 0 ? currentIndex + 1 : 0} / {totalResults} + {showReplace && !readOnly && ( @@ -367,10 +387,10 @@ export default function FindAndReplace({ onRequestSubmit={handleReplaceAll} onChange={(ev) => setReplaceTerm(ev.currentTarget.value)} /> - - @@ -396,6 +416,12 @@ const ButtonSmall = styled(NudeButton)` &[aria-expanded="true"] { background: ${s("sidebarControlHoverBackground")}; } + + &:disabled { + color: ${s("textTertiary")}; + background: none; + cursor: default; + } `; const ButtonLarge = styled(ButtonSmall)` @@ -408,3 +434,15 @@ const Content = styled(Flex)` margin-bottom: -16px; position: static; `; + +const Results = styled.span` + color: ${s("textSecondary")}; + font-size: 12px; + font-weight: 500; + font-variant-numeric: tabular-nums; + line-height: 32px; + min-width: 32px; + letter-spacing: -0.5px; + text-align: right; + user-select: none; +`; diff --git a/app/editor/extensions/FindAndReplace.tsx b/app/editor/extensions/FindAndReplace.tsx index 258cf823c0..844dce80f5 100644 --- a/app/editor/extensions/FindAndReplace.tsx +++ b/app/editor/extensions/FindAndReplace.tsx @@ -332,6 +332,8 @@ export default class FindAndReplaceExtension extends Extension { public widget = ({ readOnly }: WidgetProps) => ( { @@ -346,7 +348,11 @@ export default class FindAndReplaceExtension extends Extension { @observable private open = false; + @observable private results: { from: number; to: number }[] = []; + + @observable private currentResultIndex = 0; + private searchTerm = ""; }