mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(tui): open clicked transcript links, including wrapped rows (#53903)
Fixes #44442 Refs #51727, #35649
This commit is contained in:
committed by
GitHub
Unverified
parent
434f7b2a36
commit
2f1a0e72af
@@ -73,6 +73,7 @@ import { SessionTabs } from "./component/session-tabs"
|
||||
import { clampSessionTabsWidth, sessionTabsFitVertically, SESSION_SIDEBAR_WIDTH } from "./ui/layout"
|
||||
import { createPaneResize } from "./ui/pane-resize"
|
||||
import { PaneResizeHandle } from "./ui/pane-resize-handle"
|
||||
import { clickedLink } from "./ui/link"
|
||||
import { ThemeErrorToast } from "./component/theme-error-toast"
|
||||
import { createThemeSource, ThemeProvider, useTheme, useThemes } from "./context/theme"
|
||||
import { Home } from "./routes/home"
|
||||
@@ -1331,9 +1332,11 @@ function App() {
|
||||
evt.preventDefault()
|
||||
evt.stopPropagation()
|
||||
}}
|
||||
onMouseUp={
|
||||
copyOnSelectEnabled() ? (event) => Selection.copyOnSelectRelease(event, renderer, toast, clipboard) : undefined
|
||||
}
|
||||
onMouseUp={(event) => {
|
||||
const url = clickedLink(renderer, event)
|
||||
if (url) return void openUrl(url).catch(toast.error)
|
||||
if (copyOnSelectEnabled()) Selection.copyOnSelectRelease(event, renderer, toast, clipboard)
|
||||
}}
|
||||
>
|
||||
<box
|
||||
flexGrow={1}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { mermanLayoutsStory } from "./merman-layouts"
|
||||
import { sessionTabsStory } from "./session-tabs"
|
||||
import { sessionLocationMissingStory } from "./session-location-missing"
|
||||
import { oneCellSpinnerStory } from "./one-cell-spinner"
|
||||
import { linkClicksStory } from "./link-clicks"
|
||||
|
||||
/**
|
||||
* A story is a full-screen, fixture-driven simulation of a real production component. Stories own
|
||||
@@ -17,7 +18,13 @@ export type Story = {
|
||||
render: (context: Plugin.Context) => JSX.Element
|
||||
}
|
||||
|
||||
const stories: Story[] = [mermanLayoutsStory, sessionTabsStory, sessionLocationMissingStory, oneCellSpinnerStory]
|
||||
const stories: Story[] = [
|
||||
mermanLayoutsStory,
|
||||
sessionTabsStory,
|
||||
sessionLocationMissingStory,
|
||||
oneCellSpinnerStory,
|
||||
linkClicksStory,
|
||||
]
|
||||
|
||||
function Commands(props: { context: Plugin.Context }) {
|
||||
props.context.keymap.layer(() => ({
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import type { Plugin } from "@opencode/plugin/tui"
|
||||
import type { SessionMessageAssistant, SessionMessageAssistantText } from "@opencode/client"
|
||||
import { useTerminalDimensions } from "@opentui/solid"
|
||||
import { createSignal } from "solid-js"
|
||||
import { useConfig } from "../../../config"
|
||||
import { createTimelineAnchors } from "../../../routes/session/anchors"
|
||||
import { TextPart } from "../../../routes/session/message-parts"
|
||||
import { context } from "../../../routes/session/render-context"
|
||||
import { StoryFooter } from "./footer"
|
||||
import type { Story } from "./index"
|
||||
|
||||
const text = `Click, double-click, drag, right-click, or Ctrl/Shift+click the links below.
|
||||
|
||||
**Long bare URL:** https://www.rockauto.com/en/catalog/dodge,2004,ram+1500,4.7l+v8,1432463,brake+&+wheel+hub,brake+pad,1684
|
||||
|
||||
**Named link:** read [the OpenTUI terminal capabilities guide](https://github.com/anomalyco/opentui/blob/main/packages/core/docs/terminal-capabilities.mdx) for details.
|
||||
|
||||
**URL in inline code (not linked):** \`https://github.com/anomalyco/opencode/issues/51727#issuecomment-wrapped-link\`
|
||||
|
||||
**Relative link (not http):** see [link.tsx](packages/tui/src/ui/link.tsx).
|
||||
|
||||
| Issue | Link |
|
||||
| --- | --- |
|
||||
| Wrapped links | https://github.com/anomalyco/opencode/issues/51727 |
|
||||
| Ctrl+click | https://github.com/anomalyco/opencode/issues/44442 |
|
||||
|
||||
- A list item ending in https://example.com/a/very/long/path/that/keeps/going/until/it/wraps/around`
|
||||
|
||||
const part: SessionMessageAssistantText = { type: "text", text }
|
||||
const message: SessionMessageAssistant = {
|
||||
id: "link-clicks",
|
||||
type: "assistant",
|
||||
agent: "build",
|
||||
model: { providerID: "fixture", id: "fixture" },
|
||||
time: { created: 0, completed: 1 },
|
||||
content: [part],
|
||||
}
|
||||
|
||||
function LinkClicksStory(props: { context: Plugin.Context }) {
|
||||
const dimensions = useTerminalDimensions()
|
||||
const theme = props.context.theme
|
||||
const config = useConfig()
|
||||
const [narrow, setNarrow] = createSignal(true)
|
||||
const [markdownMode, setMarkdownMode] = createSignal<"source" | "rendered">("rendered")
|
||||
const width = () => (narrow() ? Math.min(48, dimensions().width) : dimensions().width)
|
||||
|
||||
props.context.keymap.layer(() => ({
|
||||
commands: [
|
||||
{
|
||||
bind: "escape",
|
||||
title: "Back to storybook",
|
||||
group: "Storybook",
|
||||
run: () => props.context.ui.router.navigate({ type: "plugin", name: "storybook" }),
|
||||
},
|
||||
{
|
||||
bind: "w",
|
||||
title: "Toggle narrow width",
|
||||
group: "Storybook",
|
||||
run: () => setNarrow((value) => !value),
|
||||
},
|
||||
{
|
||||
bind: "m",
|
||||
title: "Toggle markdown mode",
|
||||
group: "Storybook",
|
||||
run: () => setMarkdownMode((mode) => (mode === "rendered" ? "source" : "rendered")),
|
||||
},
|
||||
],
|
||||
}))
|
||||
|
||||
return (
|
||||
<box
|
||||
width={dimensions().width}
|
||||
height={dimensions().height}
|
||||
flexDirection="column"
|
||||
backgroundColor={theme.background.base}
|
||||
>
|
||||
<context.Provider
|
||||
value={{
|
||||
get width() {
|
||||
return width()
|
||||
},
|
||||
get terminal() {
|
||||
return dimensions()
|
||||
},
|
||||
sessionID: "link-clicks",
|
||||
anchors: createTimelineAnchors(),
|
||||
groupExpanded: () => true,
|
||||
setGroupExpanded: () => {},
|
||||
thinkingMode: () => "show",
|
||||
markdownMode,
|
||||
groupExploration: () => false,
|
||||
diffWrapMode: () => "word",
|
||||
models: () => [],
|
||||
messageIndex: () => undefined,
|
||||
legacyTurns: () => false,
|
||||
config: config.data,
|
||||
mutatePending: async () => false,
|
||||
pendingDelivery: () => undefined,
|
||||
}}
|
||||
>
|
||||
<scrollbox flexGrow={1} minHeight={0}>
|
||||
<box width={width()} paddingTop={1} paddingRight={2}>
|
||||
<TextPart part={part} message={message} last />
|
||||
</box>
|
||||
</scrollbox>
|
||||
</context.Provider>
|
||||
<StoryFooter
|
||||
context={props.context}
|
||||
title="storybook / Link clicks"
|
||||
details={[`${width()} cols`, markdownMode()]}
|
||||
controls={[
|
||||
{ shortcut: "w", label: "narrow/full width" },
|
||||
{ shortcut: "m", label: "rendered/source" },
|
||||
{ shortcut: "esc", label: "back" },
|
||||
]}
|
||||
/>
|
||||
</box>
|
||||
)
|
||||
}
|
||||
|
||||
export const linkClicksStory: Story = {
|
||||
id: "link-clicks",
|
||||
title: "Link clicks",
|
||||
render: (context) => <LinkClicksStory context={context} />,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { JSX } from "solid-js"
|
||||
import type { RGBA } from "@opentui/core"
|
||||
import type { CliRenderer, MouseEvent, RGBA } from "@opentui/core"
|
||||
import { openUrl } from "@opencode/util/open"
|
||||
|
||||
export interface LinkProps {
|
||||
@@ -32,3 +32,16 @@ export function Link(props: LinkProps) {
|
||||
</text>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hyperlink under a plain left click. opentui marks the release that ends a selection gesture with
|
||||
* `isDragging`, and a fresh "cell" selection means the pointer never moved; other releases, such as the end of a drag
|
||||
* that started outside text, are not clicks. Each rendered cell stores its full URL, so a click on any row of a wrapped
|
||||
* link resolves the whole link. Modified clicks are left to the terminal's own hyperlink handling.
|
||||
*/
|
||||
export function clickedLink(renderer: CliRenderer, event: MouseEvent) {
|
||||
if (!event.isDragging || event.modifiers.ctrl || event.modifiers.alt || event.modifiers.shift) return
|
||||
const selection = renderer.getSelection()
|
||||
if (!selection?.isStart || selection.behavior !== "cell") return
|
||||
return renderer.getLinkAt(event.x, event.y) ?? undefined
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/** @jsxImportSource @opentui/solid */
|
||||
import { expect, test } from "bun:test"
|
||||
import { SyntaxStyle } from "@opentui/core"
|
||||
import { ManualClock } from "@opentui/core/testing"
|
||||
import { testRender, useRenderer } from "@opentui/solid"
|
||||
import { clickedLink } from "../../src/ui/link"
|
||||
|
||||
const url = "https://example.com/catalog/moparts,2004,dodge,ram+1500,4.7l+v8,1432463,brake+pad"
|
||||
const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } })
|
||||
|
||||
test("plain clicks on any row of a wrapped Markdown link resolve the full URL", async () => {
|
||||
const opened: string[] = []
|
||||
function Transcript() {
|
||||
const renderer = useRenderer()
|
||||
return (
|
||||
<box
|
||||
onMouseUp={(event) => {
|
||||
const link = clickedLink(renderer, event)
|
||||
if (link) opened.push(link)
|
||||
}}
|
||||
>
|
||||
<box height={1} />
|
||||
<markdown syntaxStyle={syntaxStyle} content={`See ${url} now.`} />
|
||||
</box>
|
||||
)
|
||||
}
|
||||
const app = await testRender(() => <Transcript />, { width: 32, height: 6, clock: new ManualClock() })
|
||||
|
||||
try {
|
||||
for (let attempt = 0; attempt < 50 && !app.captureCharFrame().replace(/\s/g, "").includes(url); attempt++) {
|
||||
await app.renderOnce()
|
||||
await Bun.sleep(10)
|
||||
}
|
||||
const rows = app.captureCharFrame().split("\n")
|
||||
const head = rows.findIndex((row) => row.includes("https"))
|
||||
const tail = rows.findIndex((row) => row.includes("brake+pad"))
|
||||
const x = rows[head]!.indexOf("https")
|
||||
expect(tail).toBeGreaterThan(head)
|
||||
|
||||
await app.mockMouse.click(x, head)
|
||||
await app.mockMouse.click(rows[tail]!.indexOf("brake"), tail)
|
||||
expect(opened).toEqual([url, url])
|
||||
|
||||
await app.mockMouse.click(0, head)
|
||||
await app.mockMouse.click(x, head, 0, { modifiers: { ctrl: true } })
|
||||
await app.mockMouse.click(x, head, 0, { modifiers: { shift: true } })
|
||||
await app.mockMouse.click(x, head, 2)
|
||||
await app.mockMouse.drag(0, head, x + 2, head)
|
||||
await app.mockMouse.drag(0, 0, x + 2, head)
|
||||
expect(opened).toEqual([url, url])
|
||||
|
||||
await app.mockMouse.click(rows[tail]!.indexOf("brake"), tail)
|
||||
await app.mockMouse.click(rows[tail]!.indexOf("brake"), tail)
|
||||
expect(opened).toEqual([url, url, url])
|
||||
} finally {
|
||||
app.renderer.destroy()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user