Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/app/src/components/session/session-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { messageAgentColor } from "@/utils/agent"
import { decode64 } from "@/utils/base64"
import { Persist, persisted } from "@/utils/persist"
import { StatusPopover } from "../status-popover"
import { copyToClipboard } from "@opencode-ai/ui/clipboard"

const OPEN_APPS = [
"vscode",
Expand Down Expand Up @@ -246,20 +247,18 @@ export function SessionHeader() {
})
}

const copyPath = () => {
const copyPath = async () => {
const directory = projectDirectory()
if (!directory) return
navigator.clipboard
.writeText(directory)
.then(() => {
showToast({
variant: "success",
icon: "circle-check",
title: language.t("session.share.copy.copied"),
description: directory,
})
const ok = await copyToClipboard(directory)
if (ok) {
showToast({
variant: "success",
icon: "circle-check",
title: language.t("session.share.copy.copied"),
description: directory,
})
.catch((err: unknown) => showRequestError(language, err))
}
}

const centerMount = createMemo(() => document.getElementById("opencode-titlebar-center"))
Expand Down
26 changes: 26 additions & 0 deletions packages/ui/src/components/clipboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export async function copyToClipboard(text: string): Promise<boolean> {
const isSecure =
typeof window !== "undefined" &&
(window.isSecureContext ?? (location.protocol === "https:" || location.hostname === "localhost"))
if (isSecure && navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text)
return true
}
const textarea = document.createElement("textarea")
textarea.value = text
textarea.style.position = "fixed"
textarea.style.left = "-9999px"
textarea.style.top = "-9999px"
textarea.style.opacity = "0"
document.body.appendChild(textarea)
textarea.focus()
textarea.select()
try {
const ok = document.execCommand("copy")
document.body.removeChild(textarea)
return ok
} catch {
document.body.removeChild(textarea)
return false
}
}
6 changes: 3 additions & 3 deletions packages/ui/src/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checksum } from "@opencode-ai/util/encode"
import { ComponentProps, createEffect, createResource, createSignal, onCleanup, splitProps } from "solid-js"
import { isServer } from "solid-js/web"
import { stream } from "./markdown-stream"
import { copyToClipboard } from "./clipboard"

type Entry = {
hash: string
Expand Down Expand Up @@ -199,9 +200,8 @@ function setupCodeCopy(root: HTMLDivElement, getLabels: () => CopyLabels) {
const code = button.closest('[data-component="markdown-code"]')?.querySelector("code")
const content = code?.textContent ?? ""
if (!content) return
const clipboard = navigator?.clipboard
if (!clipboard) return
await clipboard.writeText(content)
const ok = await copyToClipboard(content)
if (!ok) return
const labels = getLabels()
setCopyState(button, labels, true)
const existing = timeouts.get(button)
Expand Down
7 changes: 4 additions & 3 deletions packages/ui/src/components/message-part.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { ToolStatusTitle } from "./tool-status-title"
import { animate } from "motion"
import { useLocation } from "@solidjs/router"
import { attached, inline, kind } from "./message-file"
import { copyToClipboard } from "./clipboard"

function ShellSubmessage(props: { text: string; animate?: boolean }) {
let widthRef: HTMLSpanElement | undefined
Expand Down Expand Up @@ -984,7 +985,7 @@ export function UserMessageDisplay(props: { message: UserMessage; parts: PartTyp
const handleCopy = async () => {
const content = text()
if (!content) return
await navigator.clipboard.writeText(content)
await copyToClipboard(content)
setState("copied", true)
setTimeout(() => setState("copied", false), 2000)
}
Expand Down Expand Up @@ -1404,7 +1405,7 @@ PART_MAPPING["text"] = function TextPartDisplay(props) {
const handleCopy = async () => {
const content = text()
if (!content) return
await navigator.clipboard.writeText(content)
await copyToClipboard(content)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
Expand Down Expand Up @@ -1743,7 +1744,7 @@ ToolRegistry.register({
const handleCopy = async () => {
const content = text()
if (!content) return
await navigator.clipboard.writeText(content)
await copyToClipboard(content)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ComponentProps } from "solid-js"
import { useI18n } from "../context/i18n"
import { IconButton } from "./icon-button"
import { Tooltip } from "./tooltip"
import { copyToClipboard } from "./clipboard"

export interface TextFieldProps
extends ComponentProps<typeof Kobalte.Input>,
Expand Down Expand Up @@ -69,7 +70,7 @@ export function TextField(props: TextFieldProps) {

async function handleCopy() {
const value = local.value ?? local.defaultValue ?? ""
await navigator.clipboard.writeText(value)
await copyToClipboard(value)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/tool-error-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Icon } from "./icon"
import { IconButton } from "./icon-button"
import { Tooltip } from "./tooltip"
import { useI18n } from "../context/i18n"
import { copyToClipboard } from "./clipboard"

export interface ToolErrorCardProps extends Omit<ComponentProps<typeof Card>, "children" | "variant"> {
tool: string
Expand Down Expand Up @@ -69,7 +70,7 @@ export function ToolErrorCard(props: ToolErrorCardProps) {
const copy = async () => {
const text = cleaned()
if (!text) return
await navigator.clipboard.writeText(text)
await copyToClipboard(text)
setState("copied", true)
setTimeout(() => setState("copied", false), 2000)
}
Expand Down
Loading