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
77export type * from "../../src/ts/ts-rs/ViewerConfig.d.ts";
78export type * from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
79import type {ViewerConfig} from "../../src/ts/ts-rs/ViewerConfig.d.ts";
80import type {ViewerConfigUpdate} from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
81"#;
82
83/// Register a plugin globally.
84#[wasm_bindgen]
85#[allow(non_snake_case)]
86pub fn registerPlugin(name: &str) {
87    use crate::renderer::*;
88    PLUGIN_REGISTRY.register_plugin(name);
89}
90
91/// Register this crate's Custom Elements in the browser's current session.
92///
93/// This must occur before calling any public API methods on these Custom
94/// Elements from JavaScript, as the methods themselves won't be defined yet.
95/// By default, this crate does not register `PerspectiveViewerElement` (as to
96/// preserve backwards-compatible synchronous API).
97#[cfg(not(feature = "external-bootstrap"))]
98#[wasm_bindgen(js_name = "init")]
99pub fn js_init() {
100    console_error_panic_hook::set_once();
101    perspective_js::utils::set_global_logging();
102    define_web_components!("export * as psp from '../../perspective-viewer.js'");
103    tracing::info!("Perspective initialized.");
104}
105
106/// Register Web Components with the global registry, given a Perspective
107/// module.
108///
109/// This function shouldn't be called directly;  instead, use the
110/// `define_web_components!` macro to both call this method and hook the
111/// wasm_bindgen module object.
112pub fn bootstrap_web_components(psp: &JsValue) {
113    define_web_component::<PerspectiveViewerElement>(psp);
114    define_web_component::<PerspectiveDebugPluginElement>(psp);
115    define_web_component::<CopyDropDownMenuElement>(psp);
116    define_web_component::<ExportDropDownMenuElement>(psp);
117}
118
119/// Defining the web components needs an extern struct to reference the
120/// generated JavaSript glue. This is parameterized by an attribute macro which
121/// needs to be determined by the top-level compiled module - the JavaScript
122/// glue code emitted by `wasm-bindgen-cli`.
123#[macro_export]
124macro_rules! define_web_components {
125    ($x:expr) => {{
126        #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
127        extern "C" {
128            #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
129            #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
130            pub static PSP: wasm_bindgen::prelude::JsValue;
131        }
132
133        PSP.with(|x| $crate::bootstrap_web_components(x));
134    }};
135}