tui: fix prompt ref initialization to prevent undefined reference errors

This commit is contained in:
Dax Raad
2026-01-11 19:40:16 -05:00
parent 3a30773874
commit 68ed664a3f

View File

@@ -89,7 +89,7 @@ export function Prompt(props: PromptProps) {
const fileStyleId = syntax().getStyleId("extmark.file")! const fileStyleId = syntax().getStyleId("extmark.file")!
const agentStyleId = syntax().getStyleId("extmark.agent")! const agentStyleId = syntax().getStyleId("extmark.agent")!
const pasteStyleId = syntax().getStyleId("extmark.paste")! const pasteStyleId = syntax().getStyleId("extmark.paste")!
let promptPartTypeId: number let promptPartTypeId = 0
sdk.event.on(TuiEvent.PromptAppend.type, (evt) => { sdk.event.on(TuiEvent.PromptAppend.type, (evt) => {
input.insertText(evt.properties.text) input.insertText(evt.properties.text)
@@ -310,14 +310,7 @@ export function Prompt(props: PromptProps) {
] ]
}) })
createEffect(() => { const ref: PromptRef = {
if (props.visible !== false) input?.focus()
if (props.visible === false) input?.blur()
})
onMount(() => {
promptPartTypeId = input.extmarks.registerType("prompt-part")
props.ref?.({
get focused() { get focused() {
return input.focused return input.focused
}, },
@@ -348,7 +341,11 @@ export function Prompt(props: PromptProps) {
submit() { submit() {
submit() submit()
}, },
}) }
createEffect(() => {
if (props.visible !== false) input?.focus()
if (props.visible === false) input?.blur()
}) })
function restoreExtmarksFromParts(parts: PromptInfo["parts"]) { function restoreExtmarksFromParts(parts: PromptInfo["parts"]) {
@@ -920,6 +917,10 @@ export function Prompt(props: PromptProps) {
}} }}
ref={(r: TextareaRenderable) => { ref={(r: TextareaRenderable) => {
input = r input = r
if (promptPartTypeId === 0) {
promptPartTypeId = input.extmarks.registerType("prompt-part")
}
props.ref?.(ref)
setTimeout(() => { setTimeout(() => {
input.cursorColor = theme.text input.cursorColor = theme.text
}, 0) }, 0)