forked from goxjs/glfw
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclipboard_wasm.go
38 lines (31 loc) · 972 Bytes
/
clipboard_wasm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//go:build wasm
package glfw
import (
"syscall/js"
)
// GetClipboardString returns the contents of the system clipboard, if it contains or is convertible to a UTF-8 encoded string.
//
// This function may only be called from the main thread.
func GetClipboardString() string {
text := make(chan string)
clipboard := js.Global().Get("navigator").Get("clipboard")
clipboard.Call("readText").Call("then", js.FuncOf(func(this js.Value, p []js.Value) interface{} {
content := p[0]
if !content.Truthy() {
text <- ""
return nil
}
text <- content.String()
return nil
})).Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
text <- ""
return nil
}))
return <-text
}
// SetClipboardString sets the system clipboard to the specified UTF-8 encoded string.
//
// This function may only be called from the main thread.
func SetClipboardString(str string) {
js.Global().Get("navigator").Get("clipboard").Call("writeText", str)
}