summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam <[email protected]>2025-12-16 13:46:07 -0600
committerAdam <[email protected]>2025-12-16 15:10:43 -0600
commit7e682a95c4c77ba187b07a90d8ed2eac5238d06b (patch)
tree5bf71427252f464b31b3788851f8a32dae3b34b5
parent5eeba76bc52668e7d184249d5a2fbd7ad36c8ea7 (diff)
downloadopencode-7e682a95c4c77ba187b07a90d8ed2eac5238d06b.tar.gz
opencode-7e682a95c4c77ba187b07a90d8ed2eac5238d06b.zip
fix: prompt input multi line input
-rw-r--r--packages/desktop/src/components/prompt-input.tsx64
1 files changed, 40 insertions, 24 deletions
diff --git a/packages/desktop/src/components/prompt-input.tsx b/packages/desktop/src/components/prompt-input.tsx
index 6e147242d..6bd3127be 100644
--- a/packages/desktop/src/components/prompt-input.tsx
+++ b/packages/desktop/src/components/prompt-input.tsx
@@ -380,31 +380,47 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
const parseFromDOM = (): Prompt => {
const newParts: Prompt = []
let position = 0
- editorRef.childNodes.forEach((node) => {
- if (node.nodeType === Node.TEXT_NODE) {
- if (node.textContent) {
- const content = node.textContent
- newParts.push({ type: "text", content, start: position, end: position + content.length })
- position += content.length
- }
- } else if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).dataset.type) {
- switch ((node as HTMLElement).dataset.type) {
- case "file":
- const content = node.textContent!
- newParts.push({
- type: "file",
- path: (node as HTMLElement).dataset.path!,
- content,
- start: position,
- end: position + content.length,
- })
- position += content.length
- break
- default:
- break
- }
- }
+
+ const pushText = (content: string) => {
+ if (!content) return
+ newParts.push({ type: "text", content, start: position, end: position + content.length })
+ position += content.length
+ }
+
+ const rangeText = (range: Range) => {
+ const fragment = range.cloneContents()
+ const container = document.createElement("div")
+ container.append(fragment)
+ return container.innerText
+ }
+
+ const files = Array.from(editorRef.querySelectorAll<HTMLElement>("[data-type=file]"))
+ let last: HTMLElement | undefined
+
+ files.forEach((file) => {
+ const before = document.createRange()
+ before.selectNodeContents(editorRef)
+ if (last) before.setStartAfter(last)
+ before.setEndBefore(file)
+ pushText(rangeText(before))
+
+ const content = file.textContent ?? ""
+ newParts.push({
+ type: "file",
+ path: file.dataset.path!,
+ content,
+ start: position,
+ end: position + content.length,
+ })
+ position += content.length
+ last = file
})
+
+ const after = document.createRange()
+ after.selectNodeContents(editorRef)
+ if (last) after.setStartAfter(last)
+ pushText(rangeText(after))
+
if (newParts.length === 0) newParts.push(...DEFAULT_PROMPT)
return newParts
}