use crate::core::types::FieldValue;
use leptos::prelude::*;
#[component]
pub fn MarkdownInput(
#[prop(into)]
name: String,
#[prop(into)]
value: Signal<FieldValue>,
#[prop(into)]
_on_change: Callback<FieldValue>,
#[prop(optional, into)]
placeholder: Option<String>,
#[prop(optional)]
required: Option<bool>,
#[prop(optional)]
disabled: Option<bool>,
#[prop(optional, into)]
class: Option<String>,
#[prop(optional, into)]
error: Option<String>,
#[prop(optional)]
has_error: Option<bool>,
#[prop(optional)]
show_preview: Option<bool>,
#[prop(optional)]
show_toolbar: Option<bool>,
#[prop(optional)]
min_height: Option<u32>,
#[prop(optional)]
max_height: Option<u32>,
) -> impl IntoView {
let _show_preview = show_preview.unwrap_or(true);
let show_toolbar = show_toolbar.unwrap_or(true);
let min_height = min_height.unwrap_or(200);
let max_height = max_height.unwrap_or(600);
let current_value = move || match value.get() {
FieldValue::String(s) => s,
_ => String::new(),
};
let toolbar_view = move || {
if show_toolbar {
Some(view! {
<div class="markdown-toolbar">
<button type="button" class="toolbar-btn">"H1"</button>
<button type="button" class="toolbar-btn">"H2"</button>
<button type="button" class="toolbar-btn">"H3"</button>
<div class="toolbar-separator"></div>
<button type="button" class="toolbar-btn">"B"</button>
<button type="button" class="toolbar-btn">"I"</button>
<div class="toolbar-separator"></div>
<button type="button" class="toolbar-btn">"•"</button>
<button type="button" class="toolbar-btn">"1."</button>
<div class="toolbar-separator"></div>
<button type="button" class="toolbar-btn">"🔗"</button>
<button type="button" class="toolbar-btn">"🖼️"</button>
<button type="button" class="toolbar-btn">"```"</button>
<button type="button" class="toolbar-btn">">"</button>
<div class="toolbar-separator"></div>
<button type="button" class="toolbar-btn">"👁️"</button>
<button type="button" class="toolbar-btn">"⫴"</button>
</div>
})
} else {
None
}
};
let error_view = move || {
error.as_ref().map(|error_msg| {
view! {
<div class="error-message">
{error_msg.clone()}
</div>
}
})
};
view! {
<div class={format!("markdown-input {}", class.unwrap_or_default())}>
{toolbar_view}
<div class="markdown-editor-container">
<textarea
name={name.clone()}
placeholder={placeholder}
required={required}
disabled={disabled}
class={move || {
let mut classes = vec!["markdown-textarea"];
if has_error.unwrap_or(false) {
classes.push("error");
}
if disabled.unwrap_or(false) {
classes.push("disabled");
}
classes.join(" ")
}}
style={format!("min-height: {}px; max-height: {}px;", min_height, max_height)}
>{current_value}</textarea>
</div>
{error_view}
</div>
}
}