perspective_viewer/utils/custom_element.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13//! Utilities for building JavaScript Custom Elements with Rust.
14
15use wasm_bindgen::prelude::*;
16
17#[wasm_bindgen(inline_js = r#"
18 export function bootstrap(psp, name, clsname, statics) {
19 if (customElements.get(name)) {
20 console.warn(`Custom element "${name}" is already registered; skipping duplicate registration (Perspective was loaded more than once on this page).`);
21 return;
22 }
23
24 const cls = psp[clsname];
25 const proto = cls.prototype;
26 class x extends HTMLElement {
27 constructor() {
28 super();
29 this._instance = new cls(this);
30 }
31 }
32
33 const names = Object.getOwnPropertyNames(proto);
34 for (const key of names) {
35 if ('get' in Object.getOwnPropertyDescriptor(proto, key)) {
36 Object.defineProperty(x.prototype, key, {
37 get: function() {
38 return this._instance[key];
39 }
40 });
41 } else {
42 Object.defineProperty(x.prototype, key, {
43 value: function(...args) {
44 return this._instance[key].call(this._instance, ...args);
45 }
46 });
47 }
48 }
49
50 for (const key of statics) {
51 Object.defineProperty(x, key, {
52 value: function(...args) {
53 return psp[key].call(psp, ...args);
54 }
55 });
56 }
57
58 customElements.define(name, x);
59 }
60"#)]
61extern "C" {
62 #[wasm_bindgen(js_name = "bootstrap")]
63 fn js_bootstrap(psp: &JsValue, name: &str, cls: &str, statics: js_sys::Array) -> JsValue;
64}
65
66/// A trait which allows the [`define_web_component`] method to create a
67/// Custom Element (which must by definition inherit from `HTMLElement`) from
68/// a `[wasm_bindgen]` annotated Rust struct (for which this trait is
69/// implemented).
70pub trait CustomElementMetadata {
71 /// The name of the element to register.
72 const CUSTOM_ELEMENT_NAME: &'static str;
73
74 /// [optional] The names of the methods which should be static on the
75 /// element.
76 const STATICS: &'static [&'static str] = [].as_slice();
77
78 /// [optional] The name of the Rust struct.
79 const TYPE_NAME: &'static str = std::any::type_name::<Self>();
80
81 /// [optional] A custom implementation of struct name to class name.
82 #[must_use]
83 fn struct_name() -> &'static str {
84 match &Self::TYPE_NAME.rfind(':') {
85 Some(pos) => &Self::TYPE_NAME[pos + 1..],
86 None => Self::TYPE_NAME,
87 }
88 }
89}
90
91/// Register the Custom Element globally.
92pub fn define_web_component<T: CustomElementMetadata>(module: &JsValue) {
93 js_bootstrap(
94 module,
95 T::CUSTOM_ELEMENT_NAME,
96 T::struct_name(),
97 T::STATICS.iter().cloned().map(JsValue::from).collect(),
98 );
99}