Skip to main content

perspective_viewer/
lib.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//! The API for the [`@perspective-dev/viewer`](https://perspective-dev.github.io)
14//! JavaScript library.
15
16// Required by yew's `html` macro.
17#![recursion_limit = "1024"]
18#![feature(const_type_name)]
19#![feature(iter_intersperse)]
20#![feature(stmt_expr_attributes)]
21#![feature(try_blocks)]
22#![allow(async_fn_in_trait)]
23#![feature(more_qualified_paths)]
24#![warn(
25    clippy::all,
26    clippy::panic_in_result_fn,
27    clippy::await_holding_refcell_ref,
28    clippy::fallible_impl_from,
29    clippy::unneeded_field_pattern
30)]
31
32pub mod components;
33pub mod config;
34pub mod custom_elements;
35mod custom_events;
36mod dragdrop;
37pub mod exprtk;
38mod js;
39mod root;
40
41pub mod engines;
42mod presentation;
43mod renderer;
44mod session;
45#[doc(hidden)]
46pub mod tasks;
47pub mod utils;
48
49#[macro_use]
50extern crate macro_rules_attribute;
51extern crate alloc;
52
53use perspective_js::utils::*;
54use wasm_bindgen::prelude::*;
55
56use crate::custom_elements::copy_dropdown::CopyDropDownMenuElement;
57use crate::custom_elements::debug_plugin::PerspectiveDebugPluginElement;
58use crate::custom_elements::export_dropdown::ExportDropDownMenuElement;
59use crate::custom_elements::viewer::PerspectiveViewerElement;
60use crate::utils::define_web_component;
61
62#[wasm_bindgen(typescript_custom_section)]
63const TS_APPEND_CONTENT: &'static str = r#"
64import type {
65    ColumnType,
66    TableInitOptions, 
67    ColumnWindow,
68    ViewWindow, 
69    OnUpdateOptions,
70    JoinOptions,
71    UpdateOptions,
72    DeleteOptions,
73    ViewConfigUpdate,
74    SystemInfo,
75} from "@perspective-dev/client";
76"#;
77
78/// Register a plugin globally.
79#[wasm_bindgen]
80#[allow(non_snake_case)]
81pub fn registerPlugin(name: &str) {
82    use crate::renderer::*;
83    PLUGIN_REGISTRY.register_plugin(name);
84}
85
86/// Register this crate's Custom Elements in the browser's current session.
87///
88/// This must occur before calling any public API methods on these Custom
89/// Elements from JavaScript, as the methods themselves won't be defined yet.
90/// By default, this crate does not register `PerspectiveViewerElement` (as to
91/// preserve backwards-compatible synchronous API).
92#[cfg(not(feature = "external-bootstrap"))]
93#[wasm_bindgen(js_name = "init")]
94pub fn js_init() {
95    console_error_panic_hook::set_once();
96    perspective_js::utils::set_global_logging();
97    define_web_components!("export * as psp from '../../perspective-viewer.js'");
98    tracing::info!("Perspective initialized.");
99}
100
101/// Register Web Components with the global registry, given a Perspective
102/// module.
103///
104/// This function shouldn't be called directly;  instead, use the
105/// `define_web_components!` macro to both call this method and hook the
106/// wasm_bindgen module object.
107pub fn bootstrap_web_components(psp: &JsValue) {
108    define_web_component::<PerspectiveViewerElement>(psp);
109    define_web_component::<PerspectiveDebugPluginElement>(psp);
110    define_web_component::<CopyDropDownMenuElement>(psp);
111    define_web_component::<ExportDropDownMenuElement>(psp);
112}
113
114/// Defining the web components needs an extern struct to reference the
115/// generated JavaSript glue. This is parameterized by an attribute macro which
116/// needs to be determined by the top-level compiled module - the JavaScript
117/// glue code emitted by `wasm-bindgen-cli`.
118#[macro_export]
119macro_rules! define_web_components {
120    ($x:expr) => {{
121        #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
122        extern "C" {
123            #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
124            #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
125            pub static PSP: wasm_bindgen::prelude::JsValue;
126        }
127
128        PSP.with(|x| $crate::bootstrap_web_components(x));
129    }};
130}