tui: fix prompt ref initialization to prevent undefined reference errors
This commit is contained in:
@@ -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,47 +310,44 @@ export function Prompt(props: PromptProps) {
|
|||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const ref: PromptRef = {
|
||||||
|
get focused() {
|
||||||
|
return input.focused
|
||||||
|
},
|
||||||
|
get current() {
|
||||||
|
return store.prompt
|
||||||
|
},
|
||||||
|
focus() {
|
||||||
|
input.focus()
|
||||||
|
},
|
||||||
|
blur() {
|
||||||
|
input.blur()
|
||||||
|
},
|
||||||
|
set(prompt) {
|
||||||
|
input.setText(prompt.input)
|
||||||
|
setStore("prompt", prompt)
|
||||||
|
restoreExtmarksFromParts(prompt.parts)
|
||||||
|
input.gotoBufferEnd()
|
||||||
|
},
|
||||||
|
reset() {
|
||||||
|
input.clear()
|
||||||
|
input.extmarks.clear()
|
||||||
|
setStore("prompt", {
|
||||||
|
input: "",
|
||||||
|
parts: [],
|
||||||
|
})
|
||||||
|
setStore("extmarkToPartIndex", new Map())
|
||||||
|
},
|
||||||
|
submit() {
|
||||||
|
submit()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
if (props.visible !== false) input?.focus()
|
if (props.visible !== false) input?.focus()
|
||||||
if (props.visible === false) input?.blur()
|
if (props.visible === false) input?.blur()
|
||||||
})
|
})
|
||||||
|
|
||||||
onMount(() => {
|
|
||||||
promptPartTypeId = input.extmarks.registerType("prompt-part")
|
|
||||||
props.ref?.({
|
|
||||||
get focused() {
|
|
||||||
return input.focused
|
|
||||||
},
|
|
||||||
get current() {
|
|
||||||
return store.prompt
|
|
||||||
},
|
|
||||||
focus() {
|
|
||||||
input.focus()
|
|
||||||
},
|
|
||||||
blur() {
|
|
||||||
input.blur()
|
|
||||||
},
|
|
||||||
set(prompt) {
|
|
||||||
input.setText(prompt.input)
|
|
||||||
setStore("prompt", prompt)
|
|
||||||
restoreExtmarksFromParts(prompt.parts)
|
|
||||||
input.gotoBufferEnd()
|
|
||||||
},
|
|
||||||
reset() {
|
|
||||||
input.clear()
|
|
||||||
input.extmarks.clear()
|
|
||||||
setStore("prompt", {
|
|
||||||
input: "",
|
|
||||||
parts: [],
|
|
||||||
})
|
|
||||||
setStore("extmarkToPartIndex", new Map())
|
|
||||||
},
|
|
||||||
submit() {
|
|
||||||
submit()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
function restoreExtmarksFromParts(parts: PromptInfo["parts"]) {
|
function restoreExtmarksFromParts(parts: PromptInfo["parts"]) {
|
||||||
input.extmarks.clear()
|
input.extmarks.clear()
|
||||||
setStore("extmarkToPartIndex", new Map())
|
setStore("extmarkToPartIndex", new Map())
|
||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user