This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
/
clipboard_impl.go
85 lines (71 loc) · 1.8 KB
/
clipboard_impl.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// +build js,wasm
package wasm
// -------------8<---------------------------------------
type clipboardImpl struct {
*eventTargetImpl
}
func wrapClipboard(v Value) Clipboard {
if v.valid() {
return &clipboardImpl{
eventTargetImpl: newEventTargetImpl(v),
}
}
return nil
}
func (p *clipboardImpl) Read() func() (DataTransfer, error) {
return func() (DataTransfer, error) {
result, ok := await(p.call("read"))
if ok {
return wrapDataTransfer(result), nil
}
return nil, wrapDOMException(result)
}
}
func (p *clipboardImpl) ReadText() func() (string, bool) {
return func() (string, bool) {
result, ok := await(p.call("readText"))
if ok {
return result.toString(), true
}
return "", false
}
}
func (p *clipboardImpl) Write(data DataTransfer) func() bool {
return func() bool {
_, ok := await(p.call("write", JSValueOf(data)))
return ok
}
}
func (p *clipboardImpl) WriteText(data string) func() bool {
return func() bool {
_, ok := await(p.call("writeText", data))
return ok
}
}
// -------------8<---------------------------------------
type clipboardEventImpl struct {
*eventImpl
}
func wrapClipboardEvent(v Value) ClipboardEvent {
if v.valid() {
return &clipboardEventImpl{
eventImpl: newEventImpl(v),
}
}
return nil
}
func (p *clipboardEventImpl) ClipboardData() DataTransfer {
return wrapDataTransfer(p.get("clipboardData"))
}
// -------------8<---------------------------------------
func NewClipboardEvent(typ string, eventInitDict ...ClipboardEventInit) ClipboardEvent {
if jsClipboardEvent := jsGlobal.get("ClipboardEvent"); jsClipboardEvent.valid() {
switch len(eventInitDict) {
case 0:
return wrapClipboardEvent(jsClipboardEvent.jsNew(typ))
default:
return wrapClipboardEvent(jsClipboardEvent.jsNew(typ, eventInitDict[0].JSValue()))
}
}
return nil
}