feat: add native clipboard capture bridge
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
func (a *App) ReadClipboardText() (string, error) {
|
||||
text, err := wailsruntime.ClipboardGetText(a.ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("clipboard text is unavailable")
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (a *App) CaptureClipboardTextWithContext(contextJSON string) (*InboxNodeDTO, error) {
|
||||
text, err := a.ReadClipboardText()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kind, value := classifyClipboardText(text)
|
||||
if value == "" {
|
||||
return nil, fmt.Errorf("clipboard is empty")
|
||||
}
|
||||
if kind == "url" {
|
||||
return a.CaptureURLWithContext(value, "", "clipboard_button", contextJSON)
|
||||
}
|
||||
return a.CaptureTextWithContext(value, "clipboard_button", contextJSON)
|
||||
}
|
||||
|
||||
func classifyClipboardText(text string) (string, string) {
|
||||
value := strings.TrimSpace(text)
|
||||
if value == "" {
|
||||
return "text", ""
|
||||
}
|
||||
if isURLLike(value) {
|
||||
return "url", value
|
||||
}
|
||||
return "text", value
|
||||
}
|
||||
@@ -245,3 +245,21 @@ func TestCapturePathWithNodeContextUsesNodeIDForLocalInbox(t *testing.T) {
|
||||
t.Fatalf("local inbox B = %+v, want empty for same title different node", localB)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyClipboardTextRoutesURLBeforePlainText(t *testing.T) {
|
||||
kind, value := classifyClipboardText(" https://example.test/page ")
|
||||
if kind != "url" {
|
||||
t.Fatalf("kind = %q, want url", kind)
|
||||
}
|
||||
if value != "https://example.test/page" {
|
||||
t.Fatalf("value = %q, want trimmed URL", value)
|
||||
}
|
||||
|
||||
kind, value = classifyClipboardText("not a url\nwith more text")
|
||||
if kind != "text" {
|
||||
t.Fatalf("kind = %q, want text", kind)
|
||||
}
|
||||
if value != "not a url\nwith more text" {
|
||||
t.Fatalf("value = %q, want trimmed text", value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user