zino_dioxus/typography/
editor.rsuse dioxus::prelude::*;
use std::time::Duration;
use zino_core::{json, JsonValue, SharedString};
pub fn TuiEditor(props: TuiEditorProps) -> Element {
let mut markdown = use_signal(String::new);
let eval_editor = document::eval(
r#"
const { Editor } = toastui;
const { codeSyntaxHighlight } = Editor.plugin;
let options = await dioxus.recv();
options.el = document.getElementById(options.id);
options.plugins = [codeSyntaxHighlight];
options.events = {
change: function() {
document.getElementById("tui-editor-input").value = tuiEditor.getMarkdown();
},
};
const tuiEditor = new Editor(options);
tuiEditor.show();
"#,
);
spawn(async move {
loop {
let mut eval = document::eval(
r#"
const value = document.getElementById("tui-editor-input").value;
dioxus.send(value);
"#,
);
if let Ok(JsonValue::String(s)) = eval.recv().await {
if markdown() != s {
if let Some(handler) = props.on_change.as_ref() {
handler.call(s.clone());
}
markdown.set(s);
}
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
});
rsx! {
input {
id: "tui-editor-input",
r#type: "hidden",
}
div {
id: "{props.id}",
onmounted: move |_event| {
let options = json!({
"id": props.id,
"height": props.height,
"minHeight": props.min_height,
"initialValue": props.initial_value,
"initialEditType": props.edit_type,
"previewStyle": props.preview_style,
"language": props.locale,
"theme": props.theme,
"referenceDefinition": true,
"usageStatistics": false,
});
eval_editor.send(options).ok();
}
}
}
}
#[derive(Clone, PartialEq, Props)]
pub struct TuiEditorProps {
#[props(into, default = "editor")]
pub id: SharedString,
#[props(into, default = "auto")]
pub height: SharedString,
#[props(into, default = "300px")]
pub min_height: SharedString,
#[props(into)]
pub initial_value: SharedString,
#[props(into, default = "markdown")]
pub edit_type: SharedString,
#[props(into, default = "vertical")]
pub preview_style: SharedString,
#[props(into, default = "light")]
pub theme: SharedString,
#[props(into, default = "en-US")]
pub locale: SharedString,
pub on_change: Option<EventHandler<String>>,
}