Struct respo::RespoEventMark

source ·
pub struct RespoEventMark {
    pub coord: Vec<RespoCoord>,
    pub name: String,
    pub event_info: RespoEvent,
}
Expand description

marks on virtual DOM to declare that there’s an event event handler is HIDDEN from this mark.

Fields§

§coord: Vec<RespoCoord>

location of element in the tree

§name: String

TODO event type

§event_info: RespoEvent

partial copy of DOM events, that shares across threads, but being async, methods like .prevent_default() and .stop_propagation() will not work

Implementations§

Examples found in repository?
src/respo/patch.rs (line 307)
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
pub fn attach_event(element: &Element, key: &str, coord: &[RespoCoord], handle_event: RespoEventMarkFn) -> Result<(), String> {
  let coord = coord.to_owned();
  // crate::util::log!("attach event {}", key);
  match key {
    "click" => {
      let handler = Closure::wrap(Box::new(move |e: MouseEvent| {
        let wrap_event = RespoEvent::Click {
          client_x: e.client_x() as f64,
          client_y: e.client_y() as f64,
          original_event: e,
        };
        handle_event
          .run(RespoEventMark::new("click", &coord, wrap_event))
          .expect("handle click event");
      }) as Box<dyn FnMut(MouseEvent)>);
      element
        .dyn_ref::<HtmlElement>()
        .expect("convert to html element")
        .set_onclick(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }

    "dblclick" => {
      let handler = Closure::wrap(Box::new(move |e: MouseEvent| {
        let wrap_event = RespoEvent::Click {
          client_x: e.client_x() as f64,
          client_y: e.client_y() as f64,
          original_event: e,
        };
        handle_event
          .run(RespoEventMark::new("dblclick", &coord, wrap_event))
          .expect("handle dblclick event");
      }) as Box<dyn FnMut(MouseEvent)>);
      element
        .dyn_ref::<HtmlElement>()
        .expect("convert to html element")
        .set_ondblclick(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    "input" => {
      let handler = Closure::wrap(Box::new(move |e: InputEvent| {
        let target = e.target().expect("get target");
        let el = target.dyn_ref::<Element>().unwrap();
        let value = match el.tag_name().as_str() {
          "INPUT" => el.dyn_ref::<HtmlInputElement>().expect("to convert to html input element").value(),
          "TEXTAREA" => el
            .dyn_ref::<HtmlTextAreaElement>()
            .expect("to convert to html text area element")
            .value(),
          _ => {
            // TODO Error
            return;
          }
        };
        let wrap_event = RespoEvent::Input { value, original_event: e };
        handle_event
          .run(RespoEventMark::new("input", &coord, wrap_event))
          .expect("handle input event");
      }) as Box<dyn FnMut(InputEvent)>);
      match element.tag_name().as_str() {
        "INPUT" => {
          element
            .dyn_ref::<HtmlInputElement>()
            .expect("convert to html input element")
            .set_oninput(Some(handler.as_ref().unchecked_ref()));
        }
        "TEXTAREA" => {
          element
            .dyn_ref::<HtmlTextAreaElement>()
            .expect("convert to html textarea element")
            .set_oninput(Some(handler.as_ref().unchecked_ref()));
        }
        _ => {
          return Err(format!("unsupported input event: {}", element.tag_name()));
        }
      }

      handler.forget();
    }
    "change" => {
      let handler = Closure::wrap(Box::new(move |e: InputEvent| {
        let wrap_event = RespoEvent::Input {
          value: e
            .target()
            .expect("to reach event target")
            .dyn_ref::<HtmlInputElement>()
            .expect("to convert to html input element")
            .value(),
          original_event: e,
        };
        handle_event
          .run(RespoEventMark::new("change", &coord, wrap_event))
          .expect("handle change event");
      }) as Box<dyn FnMut(InputEvent)>);
      element
        .dyn_ref::<HtmlInputElement>()
        .expect("convert to html input element")
        .set_onchange(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    "keydown" => {
      let handler = Closure::wrap(Box::new(move |e: KeyboardEvent| {
        // crate::util::log!("calling handler");
        let wrap_event = RespoEvent::Keyboard {
          key: e.key(),
          key_code: e.key_code(),
          shift_key: e.shift_key(),
          ctrl_key: e.ctrl_key(),
          alt_key: e.alt_key(),
          meta_key: e.meta_key(),
          repeat: e.repeat(),
          original_event: e,
        };
        handle_event
          .run(RespoEventMark::new("keydown", &coord, wrap_event))
          .expect("handle keydown event");
      }) as Box<dyn FnMut(KeyboardEvent)>);
      element
        .dyn_ref::<HtmlInputElement>()
        .expect("convert to html input element")
        .set_onkeydown(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    "keyup" => {
      let handler = Closure::wrap(Box::new(move |e: KeyboardEvent| {
        let wrap_event = RespoEvent::Keyboard {
          key: e.key(),
          key_code: e.key_code(),
          shift_key: e.shift_key(),
          ctrl_key: e.ctrl_key(),
          alt_key: e.alt_key(),
          meta_key: e.meta_key(),
          repeat: e.repeat(),
          original_event: e,
        };
        handle_event
          .run(RespoEventMark::new("keyup", &coord, wrap_event))
          .expect("handle keyup event");
      }) as Box<dyn FnMut(KeyboardEvent)>);
      element
        .dyn_ref::<HtmlInputElement>()
        .expect("convert to html input element")
        .set_onkeyup(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    "keypress" => {
      let handler = Closure::wrap(Box::new(move |e: KeyboardEvent| {
        let wrap_event = RespoEvent::Keyboard {
          key: e.key(),
          key_code: e.key_code(),
          shift_key: e.shift_key(),
          ctrl_key: e.ctrl_key(),
          alt_key: e.alt_key(),
          meta_key: e.meta_key(),
          repeat: e.repeat(),
          original_event: e,
        };
        handle_event
          .run(RespoEventMark::new("keypress", &coord, wrap_event))
          .expect("handle keypress event");
      }) as Box<dyn FnMut(KeyboardEvent)>);
      element
        .dyn_ref::<HtmlInputElement>()
        .expect("convert to html input element")
        .set_onkeypress(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    "focus" => {
      let handler = Closure::wrap(Box::new(move |e: FocusEvent| {
        handle_event
          .run(RespoEventMark::new("focus", &coord, RespoEvent::Focus(e)))
          .expect("handle focus event");
      }) as Box<dyn FnMut(FocusEvent)>);
      element
        .dyn_ref::<HtmlInputElement>()
        .expect("convert to html input element")
        .set_onfocus(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    "blur" => {
      let handler = Closure::wrap(Box::new(move |e: FocusEvent| {
        handle_event
          .run(RespoEventMark::new("blur", &coord, RespoEvent::Focus(e)))
          .expect("handle blur event");
      }) as Box<dyn FnMut(FocusEvent)>);
      element
        .dyn_ref::<HtmlInputElement>()
        .expect("convert to html input element")
        .set_onblur(Some(handler.as_ref().unchecked_ref()));
      handler.forget();
    }
    _ => {
      warn_1(&format!("unhandled event: {}", key).into());
    }
  }
  Ok(())
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.