perspective_viewer/
lib.rs1#![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#![warn(
24 clippy::all,
25 clippy::panic_in_result_fn,
26 clippy::fallible_impl_from,
27 clippy::unneeded_field_pattern
28)]
29#![deny(clippy::await_holding_refcell_ref)]
33
34#[cfg(feature = "llm-agent")]
35mod agent;
36pub mod components;
37pub mod config;
38pub mod custom_elements;
39mod custom_events;
40pub mod exprtk;
41mod js;
42mod presentation;
43mod root;
44
45#[doc(hidden)]
46pub mod queries;
47mod renderer;
48mod session;
49
50#[doc(hidden)]
51pub mod tasks;
52pub mod utils;
53mod workspace;
54
55#[macro_use]
56extern crate macro_rules_attribute;
57extern crate alloc;
58
59use std::cell::RefCell;
60
61use perspective_js::utils::*;
62use wasm_bindgen::prelude::*;
63
64use crate::custom_elements::copy_dropdown::CopyDropDownMenuElement;
65use crate::custom_elements::debug_plugin::PerspectiveDebugPluginElement;
66use crate::custom_elements::export_dropdown::ExportDropDownMenuElement;
67use crate::custom_elements::viewer::PerspectiveViewerElement;
68use crate::utils::define_web_component;
69
70#[wasm_bindgen(typescript_custom_section)]
71const TS_APPEND_CONTENT: &'static str = r#"
72import type {
73 ColumnType,
74 TableInitOptions,
75 ColumnWindow,
76 ViewWindow,
77 TypedArrayWindow,
78 OnUpdateOptions,
79 JoinOptions,
80 UpdateOptions,
81 DeleteOptions,
82 ViewConfig,
83 ViewConfigUpdate,
84 SystemInfo,
85 Scalar,
86 Features,
87} from "@perspective-dev/client";
88
89export type * from "../../src/ts/ts-rs/ViewerConfig.d.ts";
90export type * from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
91export type * from "../../src/ts/ts-rs/ViewerConfigInitial.d.ts";
92export type * from "../../src/ts/ts-rs/PluginStaticConfig.d.ts";
93export type * from "../../src/ts/ts-rs/WorkspaceConfig.d.ts";
94export type * from "../../src/ts/ts-rs/WorkspaceConfigUpdate.d.ts";
95export type * from "../../src/ts/ts-rs/ExportMethod.d.ts";
96export type * from "../../src/ts/ts-rs/PanelOptions.d.ts";
97export type * from "../../src/ts/ts-rs/RestoreOptions.d.ts";
98export type * from "../../src/ts/ts-rs/SaveWorkspaceOptions.d.ts";
99export type * from "../../src/ts/ts-rs/ClientOptions.d.ts";
100export type * from "../../src/ts/ts-rs/ExportOptions.d.ts";
101export type * from "../../src/ts/ts-rs/GetTableOptions.d.ts";
102export type * from "../../src/ts/ts-rs/GetClientOptions.d.ts";
103export type * from "../../src/ts/ts-rs/CustomNumberFormatConfig.d.ts";
104export type * from "../../src/ts/ts-rs/NumberFormatStyle.d.ts";
105export type * from "../../src/ts/ts-rs/Notation.d.ts";
106export type * from "../../src/ts/ts-rs/DatetimeFormatType.d.ts";
107export type * from "../../src/ts/ts-rs/StringColorMode.d.ts";
108export type * from "../../src/ts/ts-rs/DatetimeColorMode.d.ts";
109export type * from "../../src/ts/ts-rs/FormatMode.d.ts";
110import type {GetTableOptions} from "../../src/ts/ts-rs/GetTableOptions.d.ts";
111import type {PanelOptions} from "../../src/ts/ts-rs/PanelOptions.d.ts";
112import type {RestoreOptions} from "../../src/ts/ts-rs/RestoreOptions.d.ts";
113import type {SaveWorkspaceOptions} from "../../src/ts/ts-rs/SaveWorkspaceOptions.d.ts";
114import type {ExportOptions} from "../../src/ts/ts-rs/ExportOptions.d.ts";
115import type {GetClientOptions} from "../../src/ts/ts-rs/GetClientOptions.d.ts";
116import type {ClientOptions} from "../../src/ts/ts-rs/ClientOptions.d.ts";
117import type {ViewerConfig} from "../../src/ts/ts-rs/ViewerConfig.d.ts";
118import type {ViewerConfigUpdate} from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
119import type {ViewerConfigInitial} from "../../src/ts/ts-rs/ViewerConfigInitial.d.ts";
120import type {WorkspaceConfig} from "../../src/ts/ts-rs/WorkspaceConfig.d.ts";
121import type {WorkspaceConfigUpdate} from "../../src/ts/ts-rs/WorkspaceConfigUpdate.d.ts";
122"#;
123
124#[wasm_bindgen]
126#[allow(non_snake_case)]
127pub fn registerPlugin(name: &str) {
128 use crate::renderer::*;
129 PLUGIN_REGISTRY.register_plugin(name);
130}
131
132#[cfg(not(feature = "external-bootstrap"))]
139#[wasm_bindgen(js_name = "init")]
140pub fn js_init(module: js_sys::WebAssembly::Module, url: web_sys::Url) {
141 console_error_panic_hook::set_once();
142 perspective_js::utils::set_global_logging();
143 define_web_components!("export * as psp from '../../perspective-viewer.js'");
144 MODULE.with_borrow_mut(|f| {
145 *f = Some((module, url));
146 });
147
148 tracing::info!("Perspective initialized.");
149}
150
151thread_local! {
152 static MODULE: RefCell<Option<(js_sys::WebAssembly::Module, web_sys::Url)>> = RefCell::default();
153}
154
155#[cfg(not(feature = "external-bootstrap"))]
156#[wasm_bindgen(js_name = "get_wasm_module")]
157pub fn js_get_module() -> Result<js_sys::WebAssembly::Module, JsValue> {
158 MODULE
159 .with_borrow(|f| f.clone().map(|x| x.0))
160 .ok_or_else(|| "Uninited module".into())
161}
162
163#[cfg(not(feature = "external-bootstrap"))]
164#[wasm_bindgen(js_name = "get_worker_url")]
165pub fn js_get_worker_url() -> Result<web_sys::Url, JsValue> {
166 MODULE
167 .with_borrow(|f| f.clone().map(|x| x.1))
168 .ok_or_else(|| "Uninited module".into())
169}
170
171pub fn bootstrap_web_components(psp: &JsValue) {
178 define_web_component::<PerspectiveViewerElement>(psp);
179 define_web_component::<PerspectiveDebugPluginElement>(psp);
180 define_web_component::<CopyDropDownMenuElement>(psp);
181 define_web_component::<ExportDropDownMenuElement>(psp);
182}
183
184#[macro_export]
189macro_rules! define_web_components {
190 ($x:expr) => {{
191 #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
192 extern "C" {
193 #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
194 #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
195 pub static PSP: wasm_bindgen::prelude::JsValue;
196 }
197
198 PSP.with(|x| $crate::bootstrap_web_components(x));
199 }};
200}