Skip to content

Commit 4d07034

Browse files
committed
fix: svg paste bug
1 parent 9803117 commit 4d07034

1 file changed

Lines changed: 45 additions & 36 deletions

File tree

  • packages/opencode/src/cli/cmd/tui/component/prompt

‎packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx‎

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,40 @@ export function Prompt(props: PromptProps) {
496496
}
497497
const exit = useExit()
498498

499+
function pasteText(text: string, virtualText: string) {
500+
const currentOffset = input.visualCursor.offset
501+
const extmarkStart = currentOffset
502+
const extmarkEnd = extmarkStart + virtualText.length
503+
504+
input.insertText(virtualText + " ")
505+
506+
const extmarkId = input.extmarks.create({
507+
start: extmarkStart,
508+
end: extmarkEnd,
509+
virtual: true,
510+
styleId: pasteStyleId,
511+
typeId: promptPartTypeId,
512+
})
513+
514+
setStore(
515+
produce((draft) => {
516+
const partIndex = draft.prompt.parts.length
517+
draft.prompt.parts.push({
518+
type: "text" as const,
519+
text,
520+
source: {
521+
text: {
522+
start: extmarkStart,
523+
end: extmarkEnd,
524+
value: virtualText,
525+
},
526+
},
527+
})
528+
draft.extmarkToPartIndex.set(extmarkId, partIndex)
529+
}),
530+
)
531+
}
532+
499533
async function pasteImage(file: { filename?: string; content: string; mime: string }) {
500534
const currentOffset = input.visualCursor.offset
501535
const extmarkStart = currentOffset
@@ -709,12 +743,21 @@ export function Prompt(props: PromptProps) {
709743
if (!isUrl) {
710744
try {
711745
const file = Bun.file(filepath)
746+
// Handle SVG as raw text content, not as base64 image
747+
if (file.type === "image/svg+xml") {
748+
event.preventDefault()
749+
const content = await file.text().catch(() => {})
750+
if (content) {
751+
pasteText(content, `[SVG: ${file.name ?? "image"}]`)
752+
return
753+
}
754+
}
712755
if (file.type.startsWith("image/")) {
713756
event.preventDefault()
714757
const content = await file
715758
.arrayBuffer()
716759
.then((buffer) => Buffer.from(buffer).toString("base64"))
717-
.catch(console.error)
760+
.catch(() => {})
718761
if (content) {
719762
await pasteImage({
720763
filename: file.name,
@@ -733,41 +776,7 @@ export function Prompt(props: PromptProps) {
733776
!sync.data.config.experimental?.disable_paste_summary
734777
) {
735778
event.preventDefault()
736-
const currentOffset = input.visualCursor.offset
737-
const virtualText = `[Pasted ~${lineCount} lines]`
738-
const textToInsert = virtualText + " "
739-
const extmarkStart = currentOffset
740-
const extmarkEnd = extmarkStart + virtualText.length
741-
742-
input.insertText(textToInsert)
743-
744-
const extmarkId = input.extmarks.create({
745-
start: extmarkStart,
746-
end: extmarkEnd,
747-
virtual: true,
748-
styleId: pasteStyleId,
749-
typeId: promptPartTypeId,
750-
})
751-
752-
const part = {
753-
type: "text" as const,
754-
text: pastedContent,
755-
source: {
756-
text: {
757-
start: extmarkStart,
758-
end: extmarkEnd,
759-
value: virtualText,
760-
},
761-
},
762-
}
763-
764-
setStore(
765-
produce((draft) => {
766-
const partIndex = draft.prompt.parts.length
767-
draft.prompt.parts.push(part)
768-
draft.extmarkToPartIndex.set(extmarkId, partIndex)
769-
}),
770-
)
779+
pasteText(pastedContent, `[Pasted ~${lineCount} lines]`)
771780
return
772781
}
773782
}}

0 commit comments

Comments
 (0)