Skip to content

feat: add ImagePasteAddon for clipboard image handling#143

Open
brianegan wants to merge 2 commits intocoder:mainfrom
brianegan:onImagePaste
Open

feat: add ImagePasteAddon for clipboard image handling#143
brianegan wants to merge 2 commits intocoder:mainfrom
brianegan:onImagePaste

Conversation

@brianegan
Copy link

@brianegan brianegan commented Mar 23, 2026

Summary

  • Adds ImagePasteAddon, a new addon following the ITerminalAddon pattern (same as FitAddon), that detects image data in clipboard paste events and emits them as base64-encoded payloads via an onImagePaste event
  • Updates InputHandler.handlePaste to only claim paste events that contain text, allowing image-only pastes to bubble through to addons
  • Keeps the core Terminal API strictly xterm.js-conformant — no custom events on the Terminal class

Usage

import { Terminal, ImagePasteAddon } from 'ghostty-web';

const term = new Terminal();
const imagePasteAddon = new ImagePasteAddon();
term.loadAddon(imagePasteAddon);

imagePasteAddon.onImagePaste((data) => {
  console.log(data.name);       // e.g. "clipboard_1234567890.png"
  console.log(data.dataBase64); // base64-encoded image data
});

Design decisions

  • Addon, not core API: onImagePaste doesn't exist in xterm.js. Implementing it as an addon keeps the drop-in replacement promise intact — code written against the core API remains portable between xterm.js and ghostty-web.
  • Event bubbling: InputHandler no longer calls preventDefault()/stopPropagation() at the top of paste handling. It only claims the event when there is text to process, letting image-only pastes reach addon listeners.
  • Exported types: ImagePasteAddon and IImagePasteData are exported from the package entry point.

Test plan

  • All 341 existing tests pass
  • New image-paste.test.ts covers activation, disposal, lifecycle, event subscription, image paste firing, non-image paste ignoring, and post-dispose cleanup
  • Manual test: paste an image from clipboard with the addon loaded and verify onImagePaste fires
  • Manual test: paste text with the addon loaded and verify normal onData behavior is unaffected

🤖 Generated with Claude Code

brianegan and others added 2 commits March 23, 2026 13:48
Image paste handling was baked into Terminal and InputHandler as a
custom onImagePaste event, which doesn't exist in the xterm.js API.
Extract it into an opt-in addon following the ITerminalAddon pattern
(same as FitAddon) to keep the core Terminal API xterm.js-conformant.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@brianegan
Copy link
Author

Heya, really cool project! I was building a VSCode extension with ghostty-web and ran into the issue where I needed to paste images. I created this plugin with Claude Opus to handle such a case.

Not sure if you're accepting submissions coauthored by AI, but if so, I've found this helpful and wanted to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant