-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvx-runtime-browser.voyd
More file actions
138 lines (128 loc) · 4.28 KB
/
Copy pathvx-runtime-browser.voyd
File metadata and controls
138 lines (128 loc) · 4.28 KB
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
137
138
use std::enums::{ enum }
use std::msgpack::self as msgpack
use std::string::type::String
use std::vx::all
obj Model {
api status: String,
api key: String,
api code: String,
api ctrl: bool
}
enum Msg
KeyDown { key: String, code: String, ctrl: bool }
ClipboardRead { value: String }
LocationChanged { href: String }
WindowEvent { event: String }
Frame { timestamp: f64 }
Media { matches: bool }
Storage { key: String }
Broadcast { value: String }
Changed
pub fn app() -> Program<Model, Msg>
program({ init, step, view, subscriptions })
fn init() -> Program<Model, Msg>
next<Model, Msg>(
model: Model { status: "ready", key: "none", code: "none", ctrl: false },
cmd: Cmd<Msg>::copy_to_clipboard("Copied from Voyd")
)
fn step(model: Model, msg: Msg) -> Program<Model, Msg>
match(msg)
Msg::KeyDown { key, code, ctrl }:
next<Model, Msg>(Model { status: "key", key: key, code: code, ctrl: ctrl })
else:
next<Model, Msg>(model)
fn view(model: Model) -> Html<Msg>
<div>
<p class="status">{model.status}</p>
<p class="key">{model.key}</p>
<p class="code">{model.code}</p>
<p class="ctrl">{ctrl_label(model.ctrl)}</p>
</div>
fn subscriptions(model: Model) -> Sub<Msg>
keyboard_on_key_down(
key: "s",
handler: (event: KeyboardEvent) -> Msg =>
Msg::KeyDown { key: event.key, code: event.code, ctrl: event.ctrl_key }
)
pub fn standard_commands() -> Cmd<Msg>
let editor = Ref<DomElement> { id: "editor" }
Cmd::batch([
Cmd<Msg>::copy_to_clipboard("Copied from Voyd"),
Cmd<Msg>::read_clipboard((value: String) -> Msg => Msg::ClipboardRead { value: value }),
Cmd<Msg>::set_document_title("VX"),
Cmd<Msg>::push_url("/next"),
Cmd<Msg>::replace_url("/next?replace=1"),
Cmd<Msg>::set_hash("section"),
Cmd<Msg>::navigate_back(),
Cmd<Msg>::navigate_forward(),
Cmd<Msg>::open_url("/external"),
Cmd<Msg>::open_url(url: "/external", target: "_self"),
Cmd<Msg>::scroll_window_to(x: 0.0, y: 10.0),
Cmd<Msg>::scroll_window_by(x: 1.0, y: 2.0),
Cmd<Msg>::local_storage_set(key: "draft", value: "yes"),
Cmd<Msg>::local_storage_remove("draft"),
Cmd<Msg>::local_storage_clear(),
Cmd<Msg>::session_storage_set(key: "draft", value: "yes"),
Cmd<Msg>::session_storage_remove("draft"),
Cmd<Msg>::session_storage_clear(),
Cmd<Msg>::focus(editor),
Cmd<Msg>::scroll_into_view(editor),
Cmd<Msg>::select_text(editor)
])
pub fn standard_subscriptions() -> Sub<Msg>
Sub::batch([
keyboard_on_key_down(key: "s", value: Msg::Changed {}),
keyboard_on_key_up(
key: "s",
handler: (event: KeyboardEvent) -> Msg =>
Msg::KeyDown { key: event.key, code: event.code, ctrl: event.ctrl_key }
),
online_status(key: "network", value: Msg::Changed {}),
online_status(
key: "network-payload",
handler: (_online: bool) -> Msg => Msg::Changed {}
),
window_on_resize(
key: "viewport",
handler: (_size: WindowSize) -> Msg => Msg::Changed {}
),
document_on_visibility_change(
key: "visibility",
handler: (_visibility: DocumentVisibility) -> Msg => Msg::Changed {}
),
location_on_change(
key: "location",
handler: (location: Location) -> Msg => Msg::LocationChanged { href: location.href }
),
window_on_focus(
key: "focus",
handler: (event: GenericEvent) -> Msg => Msg::WindowEvent { event: event.event }
),
window_on_blur(key: "blur", value: Msg::Changed {}),
animation_frame(
key: "frame",
handler: (frame: AnimationFrame) -> Msg => Msg::Frame { timestamp: frame.timestamp }
),
media_query(
query: "(prefers-reduced-motion: reduce)",
handler: (query: MediaQuery) -> Msg => Msg::Media { matches: query.matches }
),
storage_on_change(
key: "storage",
handler: (_change: StorageChange) -> Msg => Msg::Changed {}
),
Sub<Msg>::runtime_configured<String>(
kind: "websocket",
key: "socket",
value: msgpack::make_string("updates"),
handler: (value: String) -> Msg => Msg::Broadcast { value: value }
),
broadcast_channel<String, Msg>(
name: "updates",
handler: (value: String) -> Msg => Msg::Broadcast { value: value }
)
])
fn ctrl_label(value: bool) -> String
if
value: "ctrl"
else: "no-ctrl"