File size: 4,346 Bytes
39e315a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import { c as _c } from "react-compiler-runtime";
import React from 'react';
import { renderPlaceholder } from '../hooks/renderPlaceholder.js';
import { usePasteHandler } from '../hooks/usePasteHandler.js';
import { useDeclaredCursor } from '../ink/hooks/use-declared-cursor.js';
import { Ansi, Box, Text, useInput } from '../ink.js';
import type { BaseInputState, BaseTextInputProps } from '../types/textInputTypes.js';
import type { TextHighlight } from '../utils/textHighlighting.js';
import { HighlightedInput } from './PromptInput/ShimmeredInput.js';
type BaseTextInputComponentProps = BaseTextInputProps & {
inputState: BaseInputState;
children?: React.ReactNode;
terminalFocus: boolean;
highlights?: TextHighlight[];
invert?: (text: string) => string;
hidePlaceholderText?: boolean;
};
/**
* A base component for text inputs that handles rendering and basic input
*/
export function BaseTextInput(t0) {
const $ = _c(14);
const {
inputState,
children,
terminalFocus,
invert,
hidePlaceholderText,
...props
} = t0;
const {
onInput,
value,
renderedValue,
cursorLine,
cursorColumn,
offset,
} = inputState;
const t1 = Boolean(props.focus && props.showCursor && terminalFocus);
let t2;
if ($[0] !== cursorColumn || $[1] !== cursorLine || $[2] !== t1) {
t2 = {
line: cursorLine,
column: cursorColumn,
active: t1
};
$[0] = cursorColumn;
$[1] = cursorLine;
$[2] = t1;
$[3] = t2;
} else {
t2 = $[3];
}
const cursorRef = useDeclaredCursor(t2);
const {
wrappedOnInput,
isPasting: t3
} = usePasteHandler({
onPaste: props.onPaste,
onInput: (input, key) => {
if (isPasting && key.return) {
return;
}
onInput(input, key);
},
onImagePaste: props.onImagePaste
});
const isPasting = t3;
const {
onIsPastingChange
} = props;
React.useEffect(() => {
if (onIsPastingChange) {
onIsPastingChange(isPasting);
}
}, [isPasting, onIsPastingChange]);
const {
showPlaceholder,
renderedPlaceholder
} = renderPlaceholder({
placeholder: props.placeholder,
value,
showCursor: props.showCursor,
focus: props.focus,
terminalFocus,
invert,
hidePlaceholderText
});
useInput(wrappedOnInput, {
isActive: props.focus
});
const commandWithoutArgs = value && value.trim().indexOf(" ") === -1 || value && value.endsWith(" ");
const showArgumentHint = Boolean(props.argumentHint && value && commandWithoutArgs && value.startsWith("/"));
const cursorFiltered = props.showCursor && props.highlights ? props.highlights.filter(h => h.dimColor || offset < h.start || offset >= h.end) : props.highlights;
const {
viewportCharOffset,
viewportCharEnd
} = inputState;
const filteredHighlights = cursorFiltered && viewportCharOffset > 0 ? cursorFiltered.filter(h_0 => h_0.end > viewportCharOffset && h_0.start < viewportCharEnd).map(h_1 => ({
...h_1,
start: Math.max(0, h_1.start - viewportCharOffset),
end: h_1.end - viewportCharOffset
})) : cursorFiltered;
const hasHighlights = filteredHighlights && filteredHighlights.length > 0;
if (hasHighlights) {
return <Box ref={cursorRef}><HighlightedInput text={renderedValue} highlights={filteredHighlights} />{showArgumentHint && <Text dimColor={true}>{value.endsWith(" ") ? "" : " "}{props.argumentHint}</Text>}{children}</Box>;
}
const T0 = Box;
const T1 = Text;
const t4 = "truncate-end";
const t5 = showPlaceholder && props.placeholderElement ? props.placeholderElement : showPlaceholder && renderedPlaceholder ? <Ansi>{renderedPlaceholder}</Ansi> : <Ansi>{renderedValue}</Ansi>;
const t6 = showArgumentHint && <Text dimColor={true}>{value.endsWith(" ") ? "" : " "}{props.argumentHint}</Text>;
let t7;
if ($[4] !== T1 || $[5] !== children || $[6] !== props || $[7] !== t5 || $[8] !== t6) {
t7 = <T1 wrap={t4} dimColor={props.dimColor}>{t5}{t6}{children}</T1>;
$[4] = T1;
$[5] = children;
$[6] = props;
$[7] = t5;
$[8] = t6;
$[9] = t7;
} else {
t7 = $[9];
}
let t8;
if ($[10] !== T0 || $[11] !== cursorRef || $[12] !== t7) {
t8 = <T0 ref={cursorRef}>{t7}</T0>;
$[10] = T0;
$[11] = cursorRef;
$[12] = t7;
$[13] = t8;
} else {
t8 = $[13];
}
return t8;
}
|