@@ -43,33 +43,31 @@ moon add bikallem/webapi@0.3.0
4343A simple counter application demonstrating DOM manipulation and event handling:
4444
4545``` moonbit nocheck
46- fn main {
46+ ///|
47+ fn readme_counter() -> Unit {
4748 let mut count = 0
4849
4950 // Create count display element
50- let count_display : @webapi.HTMLDivElement = @webapi.document
51- .create_element("div")
52- .into()
51+ let count_display : HTMLDivElement = document.create_element("div").into()
5352 count_display
5453 ..set_attribute("id", "count-display")
55- .. set_attribute("style", "font-size: 3em; margin: 0.5em 0;")
56- .set_text_content("0")
54+ .set_attribute("style", "font-size: 3em; margin: 0.5em 0;")
55+ count_display .set_text_content("0")
5756
5857 let update_display = fn() {
5958 count_display.set_text_content(count.to_string())
6059 }
6160
6261 // Create increment button — closures are accepted directly
63- let increment_btn = @webapi.document.create_element("button")
64- increment_btn
65- ..set_text_content("+")
66- .add_event_listener("click", fn(_event) {
62+ let increment_btn = document.create_element("button")
63+ increment_btn.set_text_content("+")
64+ increment_btn.add_event_listener("click", fn(_event) {
6765 count = count + 1
6866 update_display()
6967 })
7068
7169 // Append to DOM
72- let app = @webapi. document.get_element_by_id("app").unwrap()
70+ let app = document.get_element_by_id("app").unwrap()
7371 app.append_child(count_display) |> ignore
7472 app.append_child(increment_btn) |> ignore
7573}
@@ -80,13 +78,12 @@ fn main {
8078Demonstrates WebSocket connections with event handler closures:
8179
8280``` moonbit nocheck
83- fn main {
84- let socket = @webapi.WebSocket::new("wss://echo.websocket.events")
81+ ///|
82+ fn readme_websocket() -> Unit {
83+ let socket = WebSocket::new("wss://echo.websocket.events")
8584
8685 // Event handlers accept closures directly
87- socket.set_onopen(fn(_e) {
88- socket.send("Hello from MoonBit!")
89- })
86+ socket.set_onopen(fn(_e) { socket.send("Hello from MoonBit!") })
9087
9188 socket.set_onmessage(fn(e) {
9289 let data : String = e.data().into()
@@ -100,30 +97,27 @@ fn main {
10097Demonstrates the Canvas 2D API with gradients, shapes, and text:
10198
10299``` moonbit nocheck
103- fn main {
104- let canvas : @webapi.HTMLCanvasElement = @webapi.document
105- .create_element("canvas")
106- .into()
107- canvas..set_width(800)..set_height(500)
108- @webapi.document.get_element_by_id("app").unwrap().append_child(canvas)
109- |> ignore
100+ ///|
101+ fn readme_canvas() -> Unit {
102+ let canvas : HTMLCanvasElement = document.create_element("canvas").into()
103+ canvas.set_width(800)
104+ canvas.set_height(500)
105+ document.get_element_by_id("app").unwrap().append_child(canvas) |> ignore
110106
111107 // Get 2D rendering context
112- let ctx : @webapi.CanvasRenderingContext2D = canvas
113- .get_context("2d")
114- .unwrap()
115- .into()
108+ let ctx : CanvasRenderingContext2D = canvas.get_context("2d").unwrap().into()
116109
117110 // Create gradient and draw
118111 let gradient = ctx.create_linear_gradient(0.0, 0.0, 0.0, 300.0)
119- gradient..add_color_stop(0.0, "#1e3c72").add_color_stop(1.0, "#87CEEB")
120- ctx..set_fill_style(gradient).fill_rect(0.0, 0.0, 800.0, 300.0)
112+ gradient.add_color_stop(0.0, "#1e3c72")
113+ gradient.add_color_stop(1.0, "#87CEEB")
114+ ctx.set_fill_style(gradient)
115+ ctx.fill_rect(0.0, 0.0, 800.0, 300.0)
121116
122117 // Draw text
123- ctx
124- ..set_font("bold 24px sans-serif")
125- ..set_fill_style("#FFFFFF")
126- .fill_text("Hello, MoonBit!", 320.0, 400.0)
118+ ctx.set_font("bold 24px sans-serif")
119+ ctx.set_fill_style("#FFFFFF")
120+ ctx.fill_text("Hello, MoonBit!", 320.0, 400.0)
127121}
128122```
129123
@@ -134,68 +128,76 @@ fn main {
134128The library provides direct access to browser global objects:
135129
136130``` moonbit nocheck
137- // Access the document object
138- @webapi.document.get_element_by_id("my-id")
131+ ///|
132+ fn readme_globals() -> Unit {
133+ // Access the document object
134+ let _ = document.get_element_by_id("my-id")
139135
140- // Access the window object
141- @webapi. window.inner_width()
136+ // Access the window object
137+ let _ = window.inner_width()
142138
143- // Access the navigator object
144- @webapi.navigator.user_agent()
139+ // Access the navigator object
140+ let _ = navigator.user_agent()
141+ }
145142```
146143
147144### Type Casting with ` into() `
148145
149146DOM elements are returned as generic ` Element ` types. Use ` into() ` to cast to specific element types:
150147
151148``` moonbit nocheck
152- let canvas : @webapi.HTMLCanvasElement = @webapi.document
153- .create_element("canvas")
154- .into()
155-
156- let ctx : @webapi.CanvasRenderingContext2D = canvas
157- .get_context("2d")
158- . unwrap()
159- .into()
149+ ///|
150+ fn readme_casting() -> Unit {
151+ // Create an element and cast to specific type
152+ let canvas : HTMLCanvasElement = document.create_element("canvas").into()
153+
154+ // Cast to access type-specific methods
155+ let _ctx : CanvasRenderingContext2D = canvas.get_context("2d"). unwrap().into ()
156+ }
160157```
161158
162159### Event Handling
163160
164161Event listeners and handlers accept closures directly:
165162
166163``` moonbit nocheck
167- // addEventListener with closure
168- element.add_event_listener("click", fn(event) {
169- println("Clicked!")
170- })
171-
172- // Event handler attributes with closure
173- element.set_onclick(fn(event) {
174- println("Clicked!")
175- })
164+ ///|
165+ fn readme_events() -> Unit {
166+ let element = document.create_element("button")
167+
168+ // addEventListener with closure
169+ element.add_event_listener("click", fn(_event) { println("Clicked!") })
170+ }
176171```
177172
178173### Method Chaining
179174
180175Most setter methods return ` Unit ` , enabling method chaining with ` .. ` (use ` . ` for the last call in the chain):
181176
182177``` moonbit nocheck
183- element
184- ..set_attribute("id", "my-element")
185- ..set_attribute("class", "container")
186- .set_text_content("Hello!")
178+ ///|
179+ fn readme_chaining() -> Unit {
180+ let element = document.create_element("div")
181+ element..set_attribute("id", "my-element").set_attribute("class", "container")
182+ element.set_text_content("Hello!")
183+ }
187184```
188185
189186### Optional Parameters
190187
191188Many methods have optional parameters using MoonBit's ` ? ` syntax:
192189
193190``` moonbit nocheck
194- // With default options
195- document.create_element("div")
196-
197- // With explicit options
198- document.create_element("div", options=ElementCreationOptions::new(is="custom-div"))
191+ fn readme_optional() -> Unit {
192+ // With default options
193+ let _ = document.create_element("div")
194+
195+ // With explicit options
196+ let _ = document.create_element(
197+ "div",
198+ options=ElementCreationOptions::new(is="custom-div"),
199+ )
200+ }
199201```
200202
201203## WebIDL to MoonBit Conversion
@@ -232,21 +234,21 @@ interface Element : Node {
232234
233235** Generated MoonBit:**
234236``` moonbit nocheck
237+ ///|
235238#external
236239pub type Element
237240
241+ ///|
238242pub trait TElement: TNode {
239243 id(self : Self) -> String = _
240244 set_id(self : Self, id : String) -> Unit = _
241245 get_attribute(self : Self, name : String) -> String? = _
242246 set_attribute(self : Self, name : String, value : String) -> Unit = _
243247}
244248
249+ ///|
245250impl TElement with get_attribute(self : Self, name : String) -> String? {
246- element_get_attribute_ffi(
247- TJsValue::to_js(self),
248- TJsValue::to_js(name),
249- ).to_option()
251+ element_get_attribute_ffi(TJsValue::to_js(self), TJsValue::to_js(name)).to_option()
250252}
251253```
252254
@@ -261,18 +263,21 @@ enum ShadowRootMode { "open", "closed" };
261263
262264** Generated MoonBit:**
263265``` moonbit nocheck
266+ ///|
264267pub(all) enum ShadowRootMode {
265268 Open
266269 Closed
267270} derive(Eq, Show)
268271
272+ ///|
269273pub impl TJsValue for ShadowRootMode with to_js(self : ShadowRootMode) -> JsValue {
270274 match self {
271275 ShadowRootMode::Open => TJsValue::to_js("open")
272276 ShadowRootMode::Closed => TJsValue::to_js("closed")
273277 }
274278}
275279
280+ ///|
276281pub fn ShadowRootMode::from(value : String) -> ShadowRootMode? {
277282 match value {
278283 "open" => Some(ShadowRootMode::Open)
@@ -296,9 +301,11 @@ dictionary EventInit {
296301
297302** Generated MoonBit:**
298303``` moonbit nocheck
304+ ///|
299305#external
300306pub type EventInit
301307
308+ ///|
302309pub fn EventInit::new(bubbles? : Bool, cancelable? : Bool) -> EventInit {
303310 event_init_ffi(opt_to_js(bubbles), opt_to_js(cancelable))
304311}
0 commit comments