Skip to content

Commit

Permalink
Detect loaded plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
texodus committed Jul 27, 2022
1 parent 4f6729e commit cdc8322
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
20 changes: 19 additions & 1 deletion rust/perspective-viewer/src/rust/model/copy_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ use super::export_method::*;
use super::get_viewer_config::*;
use super::structural::*;
use crate::config::*;
use crate::js::JsPerspectiveViewerPlugin;
use crate::utils::*;

use futures::join;
use itertools::Itertools;
use js_intern::*;
use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;

fn tag_name_to_package(plugin: &JsPerspectiveViewerPlugin) -> String {
let tag_name = plugin.unchecked_ref::<web_sys::HtmlElement>().tag_name();
let tag_parts = tag_name.split('-').take(3).map(|x| x.to_lowercase());
Itertools::intersperse(tag_parts, "-".to_owned()).collect::<String>()
}

/// Export functionality, for downloads and copy-to-clipboard, are mostly shared
/// behavior, but require access to a few state objects depending on which
/// format is desired. The `CopyExportModel` groups this functionality in a
Expand All @@ -30,13 +39,22 @@ pub trait CopyExportModel: HasSession + HasRenderer + HasTheme + GetViewerConfig
fn html_as_jsvalue(&self) -> Pin<Box<dyn Future<Output = Result<web_sys::Blob, JsValue>>>> {
let view_config = self.get_viewer_config();
let session = self.session().clone();
let plugins = self
.renderer()
.get_all_plugins()
.iter()
.map(tag_name_to_package)
.collect::<HashSet<String>>()
.into_iter()
.collect::<Vec<_>>();

Box::pin(async move {
let (arrow, config) = join!(session.arrow_as_vec(true), view_config);
let arrow = arrow?;
let mut config = config?;
config.settings = false;
let js_config = serde_json::to_string(&config).into_jserror()?;
let html = export_app::render(&base64::encode(arrow), &js_config);
let html = export_app::render(&base64::encode(arrow), &js_config, &plugins);
js_sys::JsString::from(html.trim()).as_blob()
})
}
Expand Down
20 changes: 16 additions & 4 deletions rust/perspective-viewer/src/rust/model/export_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
// of the Apache License 2.0. The full license can be found in the LICENSE
// file.

use itertools::Itertools;

static VERSION: &str = env!("PKG_VERSION");

pub fn render(data: &str, layout: &str) -> String {
fn render_plugin(tag_name: impl AsRef<str>) -> String {
format!(
"import \"https://cdn.jsdelivr.net/npm/@finos/{0}@{1}/dist/cdn/{0}.js\";\n",
tag_name.as_ref(),
VERSION
)
}

pub fn render(data: &str, layout: &str, plugins: &[String]) -> String {
let stmts = plugins.iter().map(render_plugin);
let imports = Itertools::intersperse(stmts, " ".to_owned()).collect::<String>();

format!("
<!DOCTYPE html lang=\"en\">
<html>
Expand All @@ -18,8 +31,7 @@ pub fn render(data: &str, layout: &str) -> String {
<script type=\"module\">
import perspective from \"https://cdn.jsdelivr.net/npm/@finos/perspective@{0}/dist/cdn/perspective.js\";
import \"https://cdn.jsdelivr.net/npm/@finos/perspective-viewer@{0}/dist/cdn/perspective-viewer.js\";
import \"https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-datagrid@{0}/dist/cdn/perspective-viewer-datagrid.js\";
import \"https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-d3fc@{0}/dist/cdn/perspective-viewer-d3fc.js\";
{3}
const worker = perspective.worker();
const binary_string = window.atob(window.data.textContent);
const len = binary_string.length;
Expand All @@ -38,5 +50,5 @@ window.viewer.restore(JSON.parse(window.layout.textContent));
<perspective-viewer id='viewer'></perspective-viewer>
</body>
</html>
", VERSION, data, layout)
", VERSION, data, layout, imports)
}

0 comments on commit cdc8322

Please sign in to comment.