chore: refactor packages/app files (#13236)
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Frank <frank@anoma.ly>
This commit is contained in:
@@ -277,6 +277,47 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
|
||||
const isFocused = createFocusSignal(() => editorRef)
|
||||
|
||||
const closePopover = () => setStore("popover", null)
|
||||
|
||||
const resetHistoryNavigation = (force = false) => {
|
||||
if (!force && (store.historyIndex < 0 || store.applyingHistory)) return
|
||||
setStore("historyIndex", -1)
|
||||
setStore("savedPrompt", null)
|
||||
}
|
||||
|
||||
const clearEditor = () => {
|
||||
editorRef.innerHTML = ""
|
||||
}
|
||||
|
||||
const setEditorText = (text: string) => {
|
||||
clearEditor()
|
||||
editorRef.textContent = text
|
||||
}
|
||||
|
||||
const focusEditorEnd = () => {
|
||||
requestAnimationFrame(() => {
|
||||
editorRef.focus()
|
||||
const range = document.createRange()
|
||||
const selection = window.getSelection()
|
||||
range.selectNodeContents(editorRef)
|
||||
range.collapse(false)
|
||||
selection?.removeAllRanges()
|
||||
selection?.addRange(range)
|
||||
})
|
||||
}
|
||||
|
||||
const currentCursor = () => {
|
||||
const selection = window.getSelection()
|
||||
if (!selection || selection.rangeCount === 0 || !editorRef.contains(selection.anchorNode)) return null
|
||||
return getCursorPosition(editorRef)
|
||||
}
|
||||
|
||||
const renderEditorWithCursor = (parts: Prompt) => {
|
||||
const cursor = currentCursor()
|
||||
renderEditor(parts)
|
||||
if (cursor !== null) setCursorPosition(editorRef, cursor)
|
||||
}
|
||||
|
||||
createEffect(() => {
|
||||
params.id
|
||||
if (params.id) return
|
||||
@@ -290,7 +331,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
const isImeComposing = (event: KeyboardEvent) => event.isComposing || composing() || event.keyCode === 229
|
||||
|
||||
createEffect(() => {
|
||||
if (!isFocused()) setStore("popover", null)
|
||||
if (!isFocused()) closePopover()
|
||||
})
|
||||
|
||||
// Safety: reset composing state on focus change to prevent stuck state
|
||||
@@ -381,26 +422,17 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
|
||||
const handleSlashSelect = (cmd: SlashCommand | undefined) => {
|
||||
if (!cmd) return
|
||||
setStore("popover", null)
|
||||
closePopover()
|
||||
|
||||
if (cmd.type === "custom") {
|
||||
const text = `/${cmd.trigger} `
|
||||
editorRef.innerHTML = ""
|
||||
editorRef.textContent = text
|
||||
setEditorText(text)
|
||||
prompt.set([{ type: "text", content: text, start: 0, end: text.length }], text.length)
|
||||
requestAnimationFrame(() => {
|
||||
editorRef.focus()
|
||||
const range = document.createRange()
|
||||
const sel = window.getSelection()
|
||||
range.selectNodeContents(editorRef)
|
||||
range.collapse(false)
|
||||
sel?.removeAllRanges()
|
||||
sel?.addRange(range)
|
||||
})
|
||||
focusEditorEnd()
|
||||
return
|
||||
}
|
||||
|
||||
editorRef.innerHTML = ""
|
||||
clearEditor()
|
||||
prompt.set([{ type: "text", content: "", start: 0, end: 0 }], 0)
|
||||
command.trigger(cmd.id, "slash")
|
||||
}
|
||||
@@ -454,7 +486,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
})
|
||||
|
||||
const renderEditor = (parts: Prompt) => {
|
||||
editorRef.innerHTML = ""
|
||||
clearEditor()
|
||||
for (const part of parts) {
|
||||
if (part.type === "text") {
|
||||
editorRef.appendChild(createTextFragment(part.content))
|
||||
@@ -514,34 +546,14 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
mirror.input = false
|
||||
if (isNormalizedEditor()) return
|
||||
|
||||
const selection = window.getSelection()
|
||||
let cursorPosition: number | null = null
|
||||
if (selection && selection.rangeCount > 0 && editorRef.contains(selection.anchorNode)) {
|
||||
cursorPosition = getCursorPosition(editorRef)
|
||||
}
|
||||
|
||||
renderEditor(inputParts)
|
||||
|
||||
if (cursorPosition !== null) {
|
||||
setCursorPosition(editorRef, cursorPosition)
|
||||
}
|
||||
renderEditorWithCursor(inputParts)
|
||||
return
|
||||
}
|
||||
|
||||
const domParts = parseFromDOM()
|
||||
if (isNormalizedEditor() && isPromptEqual(inputParts, domParts)) return
|
||||
|
||||
const selection = window.getSelection()
|
||||
let cursorPosition: number | null = null
|
||||
if (selection && selection.rangeCount > 0 && editorRef.contains(selection.anchorNode)) {
|
||||
cursorPosition = getCursorPosition(editorRef)
|
||||
}
|
||||
|
||||
renderEditor(inputParts)
|
||||
|
||||
if (cursorPosition !== null) {
|
||||
setCursorPosition(editorRef, cursorPosition)
|
||||
}
|
||||
renderEditorWithCursor(inputParts)
|
||||
},
|
||||
),
|
||||
)
|
||||
@@ -636,11 +648,8 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
const shouldReset = trimmed.length === 0 && !hasNonText && images.length === 0
|
||||
|
||||
if (shouldReset) {
|
||||
setStore("popover", null)
|
||||
if (store.historyIndex >= 0 && !store.applyingHistory) {
|
||||
setStore("historyIndex", -1)
|
||||
setStore("savedPrompt", null)
|
||||
}
|
||||
closePopover()
|
||||
resetHistoryNavigation()
|
||||
if (prompt.dirty()) {
|
||||
mirror.input = true
|
||||
prompt.set(DEFAULT_PROMPT, 0)
|
||||
@@ -662,16 +671,13 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
slashOnInput(slashMatch[1])
|
||||
setStore("popover", "slash")
|
||||
} else {
|
||||
setStore("popover", null)
|
||||
closePopover()
|
||||
}
|
||||
} else {
|
||||
setStore("popover", null)
|
||||
closePopover()
|
||||
}
|
||||
|
||||
if (store.historyIndex >= 0 && !store.applyingHistory) {
|
||||
setStore("historyIndex", -1)
|
||||
setStore("savedPrompt", null)
|
||||
}
|
||||
resetHistoryNavigation()
|
||||
|
||||
mirror.input = true
|
||||
prompt.set([...rawParts, ...images], cursorPosition)
|
||||
@@ -732,7 +738,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
}
|
||||
|
||||
handleInput()
|
||||
setStore("popover", null)
|
||||
closePopover()
|
||||
}
|
||||
|
||||
const addToHistory = (prompt: Prompt, mode: "normal" | "shell") => {
|
||||
@@ -782,8 +788,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
promptLength,
|
||||
addToHistory,
|
||||
resetHistoryNavigation: () => {
|
||||
setStore("historyIndex", -1)
|
||||
setStore("savedPrompt", null)
|
||||
resetHistoryNavigation(true)
|
||||
},
|
||||
setMode: (mode) => setStore("mode", mode),
|
||||
setPopover: (popover) => setStore("popover", popover),
|
||||
@@ -872,7 +877,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
|
||||
if (ctrl && event.code === "KeyG") {
|
||||
if (store.popover) {
|
||||
setStore("popover", null)
|
||||
closePopover()
|
||||
event.preventDefault()
|
||||
return
|
||||
}
|
||||
@@ -923,7 +928,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
|
||||
}
|
||||
if (event.key === "Escape") {
|
||||
if (store.popover) {
|
||||
setStore("popover", null)
|
||||
closePopover()
|
||||
} else if (working()) {
|
||||
abort()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user