Skip to content
Kite

Clipboard: choose the richest paste you can handle

A terminal app normally uses OSC 52 for plain text. That’s a useful default over SSH: the terminal and the Go process may be on different machines. For a local app that accepts images or formatted text, install the optional extras/clipboard provider on the engine:

app := kite.New(b, kite.WithClipboard(clipboard.New()))

The native provider reads the clipboard of the machine running the app. Formats vary by platform and available tools. On Linux, for example, Wayland needs wl-paste/wl-copy; X11 can use xclip, whereas xsel is text-only. Don’t assume that a listed rich format is always present.

Listen for event.EventPaste on the editing target. The engine loads available formats into Items before dispatch. Pick in an explicit order: a clipboard can contain both HTML and plain text, and Go map iteration doesn’t express a preference.

func handlePaste(e event.Event, attach func(string, []byte)) {
paste, ok := e.(*event.ClipboardEvent)
if !ok { return }
for _, mime := range []string{"image/png", "text/html"} {
if data, ok := paste.Items[mime]; ok {
attach(mime, data)
paste.PreventDefault()
return
}
}
}

Register it with editor.AddEventListener(event.EventPaste, func(e event.Event) { handlePaste(e, attach) }). Here attach is your application’s attachment handler. Treat HTML as untrusted data; don’t execute it. Only call PreventDefault() when taking responsibility for the paste. Otherwise a focused <input> or <textarea> inserts text/plain via paste.Text() as its normal default action. Canceling the earlier paste keydown instead can prevent the paste event entirely.

For a Copy button use WriteText(text). For an attachment workflow use ListFormats(), Read(mime), and Write(mime, data) on the provider; all return promises. Handle failures separately from a successful empty text result. If rich content is optional, keep plain text as the fallback rather than failing the whole edit.