-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
136 lines (114 loc) · 3.21 KB
/
events.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package magic
import (
"encoding/json"
"log"
)
const (
UnmountEvent = "m:unmount"
ClickEvent = "m:click"
FocusEvent = "m:focus"
ChangeEvent = "m:change"
KeydownEvent = "m:keydown"
KeypressEvent = "m:keypress"
KeyupEvent = "m:keyup"
SubmitEvent = "m:submit"
DblclickEvent = "m:dblclick"
NavigateEvent = "m:navigate"
ResetEvent = "m:reset"
BlurEvent = "m:blur"
OpenFullscreenEvent = "m:openFullscreen"
CloseFullscreenEvent = "m:closeFullscreen"
AnimateEvent = "m:animate"
ScrollIntoViewEvent = "m:scrollIntoView"
DisconnectEvent = "m:disconnect"
UpdateURLEvent = "m:updateUrl"
RefreshFileEvent = "m:refreshFile"
)
type Event struct {
Kind string `json:"k"`
Target uintptr `json:"t"`
Payload json.RawMessage `json:"p"`
}
type EventData json.RawMessage
type EventHandler func(ev string, data EventData)
type EventSender func(ev string, data EventData)
type ClickPayload struct {
Value string `json:"value"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type FocusPayload struct {
Value string `json:"value"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type ChangePayload struct {
Value string `json:"value"`
Content string `json:"content"`
Key string `json:"key"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type KeydownPayload struct {
Value string `json:"value"`
Key string `json:"key"`
Content string `json:"content"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type KeypressPayload struct {
Value string `json:"value"`
Key string `json:"key"`
Content string `json:"content"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type KeyupPayload struct {
Value string `json:"value"`
Key string `json:"key"`
Content string `json:"content"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type SubmitPayload struct {
Value string `json:"value"`
Form json.RawMessage `json:"form"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
type DblclickPayload struct {
Value string `json:"value"`
MetaKey bool `json:"metaKey"`
CtrlKey bool `json:"shiftKey"`
}
func (ev EventData) Click() ClickPayload {
return parseJsonEventData[ClickPayload](ev)
}
func (ev EventData) Focus() FocusPayload {
return parseJsonEventData[FocusPayload](ev)
}
func (ev EventData) Change() ChangePayload {
return parseJsonEventData[ChangePayload](ev)
}
func (ev EventData) Keydown() KeydownPayload {
return parseJsonEventData[KeydownPayload](ev)
}
func (ev EventData) Keypress() KeypressPayload {
return parseJsonEventData[KeypressPayload](ev)
}
func (ev EventData) Keyup() KeyupPayload {
return parseJsonEventData[KeyupPayload](ev)
}
func (ev EventData) Submit() SubmitPayload {
return parseJsonEventData[SubmitPayload](ev)
}
func (ev EventData) Dblclick() DblclickPayload {
return parseJsonEventData[DblclickPayload](ev)
}
func parseJsonEventData[T any](data EventData) T {
t := new(T)
if err := json.Unmarshal(data, &t); err != nil {
log.Println(err)
}
return *t
}