Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add theme support to monaco #1439

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/perspective-viewer/src/themes/material.dark.less
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ perspective-viewer, .perspective-viewer-material-dark {
--expression--function-color: @lightblue600;
--expression--error-color: rgb(255, 136, 136);
--input-dragover--background: linear-gradient(to top, rgbs(38, 112, 169, 0.5), transparent 50%);
--monaco-theme: vs-dark;

table {
color: white;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ perspective-viewer.workspace-master-widget {
--plugin--background: @grey800;
}

perspective-column-style {
perspective-column-style, perspective-expression-editor {
background-color: @grey700;
color: #FFFFFF;
border: 1px solid @grey500;
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective-workspace/src/theme/material.less
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ perspective-viewer.workspace-master-widget {
}
}

perspective-column-style {
perspective-column-style, perspective-expression-editor {
background-color: #FFFFFF;
color: #161616;
border: 1px solid #E0E4E9;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ExpressionEditorProps {
pub on_init_callback: Rc<dyn Fn()>,
pub on_validate_callback: Rc<dyn Fn(bool)>,
pub session: Session,
pub monaco_theme: String,
}

/// A label widget which displays a row count and a "projection" count, the number of
Expand Down Expand Up @@ -144,7 +145,7 @@ impl Component for ExpressionEditor {
if first_render {
let this = self.clone();
let _ = future_to_promise(async move {
let editor = init_monaco().await.unwrap();
let editor = init_monaco(&this.props.monaco_theme).await.unwrap();
let args = EditorArgs {
theme: "exprtk-theme",
value: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ impl PerspectiveExpressionEditorElement {
on_save_callback: Rc<dyn Fn(JsValue)>,
on_init_callback: Rc<dyn Fn()>,
on_validate_callback: Rc<dyn Fn(bool)>,
monaco_theme: String
) -> PerspectiveExpressionEditorElement {
let props = ExpressionEditorProps {
on_save_callback,
on_init_callback,
on_validate_callback,
monaco_theme,
session,
};

Expand Down
12 changes: 12 additions & 0 deletions rust/perspective-vieux/src/rust/custom_elements/vieux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ use wasm_bindgen_futures::future_to_promise;
use web_sys::*;
use yew::prelude::*;

fn get_theme(elem: &HtmlElement) -> String {
let styles = window().unwrap().get_computed_style(elem).unwrap().unwrap();
match &styles.get_property_value("--monaco-theme") {
Err(_) => "vs",
Ok(ref s) if s.trim() == "" => "vs",
Ok(x) => x.trim(),
}
.to_owned()
}

/// A `customElements` external API.
#[wasm_bindgen]
#[derive(Clone)]
Expand Down Expand Up @@ -119,12 +129,14 @@ impl PerspectiveVieuxElement {
.unwrap();
});

let monaco_theme = get_theme(&target);
let mut element = PerspectiveExpressionEditorElement::new(
editor,
self.session.clone(),
on_save,
on_init,
on_validate,
monaco_theme,
);

element.open(target).unwrap();
Expand Down
44 changes: 16 additions & 28 deletions rust/perspective-vieux/src/rust/exprtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use js_intern::*;
use js_sys::Reflect;
use serde_json::error;
use std::cell::Cell;
use wasm_bindgen::{JsCast, prelude::*};
use wasm_bindgen::{prelude::*, JsCast};

use crate::js_object;
use crate::utils::monaco::*;
Expand Down Expand Up @@ -444,28 +444,6 @@ thread_local! {
},
]
};

static THEME: DefineThemeArgs<'static> = DefineThemeArgs {
base: "vs",
inherit: true,
rules: vec![
// DefineThemeToken {
// token: "exprtk-comment",
// foreground: "#007700",
// font_style: None,
// },
// DefineThemeToken {
// token: "exprtk-symbol",
// foreground: "#0000ff",
// font_style: None,
// },
// DefineThemeToken {
// token: "exprtk-column",
// foreground: "#990000",
// font_style: None,
// },
],
}
}

/// This helper _must_ create the `JsValue` anew on each call, or it causes strange
Expand All @@ -477,7 +455,7 @@ fn get_suggestions() -> JsValue {

/// Initializes the `plang` language definition using Monaco's `Languages`
/// module.
async fn init_language() -> Result<Editor, error::Error> {
async fn init_language(base: &str) -> Result<Editor, error::Error> {
let exts = monaco_exts();
let module = monaco_module().await.unchecked_into::<MonacoModule>();
let languages = module.languages();
Expand All @@ -489,7 +467,14 @@ async fn init_language() -> Result<Editor, error::Error> {
let items = js_object!("provideCompletionItems", provider.as_ref()).into();
provider.forget();
languages.register_completion_item_provider("exprtk", items);
editor.define_theme("exprtk-theme", THEME.with(|x| JsValue::from_serde(&x))?);
editor.define_theme(
"exprtk-theme",
JsValue::from_serde(&DefineThemeArgs {
base,
inherit: true,
rules: vec![],
})?,
);
exts.await;
Ok(editor)
}
Expand All @@ -504,13 +489,16 @@ fn init_environment() -> Result<(), error::Error> {
}

/// Initialize the `ExprTK` language in `monaco`. This should only be done once.
pub async fn init_monaco() -> Result<Editor, error::Error> {
pub async fn init_monaco(base: &str) -> Result<Editor, error::Error> {
if IS_REGISTERED.with(|x| !x.get()) {
let editor = init_language().await?;
let editor = init_language(base).await?;
init_environment()?;
IS_REGISTERED.with(|x| x.set(true));
Ok(editor)
} else {
Ok(monaco_module().await.unchecked_into::<MonacoModule>().editor())
Ok(monaco_module()
.await
.unchecked_into::<MonacoModule>()
.editor())
}
}