Files
worldcoder/src/main_editor.rs
T
2025-07-14 01:30:33 +01:00

110 lines
4.7 KiB
Rust

use egui::{TextEdit, Ui};
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
pub struct MainEditor {
pub show_editor: bool,
pub show_preview: bool,
text: String,
preview_cache: CommonMarkCache,
}
impl MainEditor {
pub fn new() -> Self {
Self {
show_editor: false, // Start with editor hidden
show_preview: true,
text: String::new(),
preview_cache: CommonMarkCache::default(),
}
}
pub fn ui(&mut self, ctx: &egui::Context) {
// Show the editor window if enabled
if self.show_editor {
egui::Window::new("Markdown Editor")
.resizable(true)
.default_width(1000.0)
.default_height(800.0)
.max_height(800.0)
.open(&mut self.show_editor)
.show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label("Content Editor");
ui.checkbox(&mut self.show_preview, "Preview");
});
ui.separator();
if self.show_preview {
// Preview area
egui::SidePanel::right("preview_panel")
.resizable(true)
.default_width(ui.available_width() / 2.0)
.show_inside(ui, |ui| {
// Preview area with centered content and max width
egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("preview_scroll")
.show(ui, |ui| {
let max_width = 600;
let available_width = ui.available_width();
let content_width = (max_width as f32).min(available_width);
let padding = (available_width - content_width) / 2.0;
ui.horizontal(|ui| {
ui.add_space(padding);
ui.vertical(|ui| {
ui.set_width(content_width);
ui.add_space(15.0);
ui.set_min_width(max_width as f32);
CommonMarkViewer::new()
.default_width(Some(max_width))
.max_image_width(Some(512))
.show(ui, &mut self.preview_cache, &self.text);
});
});
});
});
}
// Editor area with centered content and max width
egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("editor_scroll")
.show(ui, |ui| {
let max_width = 600;
let available_width = ui.available_width();
let content_width = (max_width as f32).min(available_width);
let padding = (available_width - content_width) / 2.0;
ui.horizontal(|ui| {
ui.add_space(padding);
ui.vertical(|ui| {
ui.set_width(content_width);
ui.add_space(15.0);
ui.set_min_width(max_width as f32);
let text_edit = TextEdit::multiline(&mut self.text)
.id_source("MainEditor_editor")
.font(egui::TextStyle::Monospace)
.interactive(true)
.frame(false)
.lock_focus(true)
.hint_text("Type here...")
.desired_width(max_width as f32);
ui.add_sized(
egui::vec2(max_width as f32, ui.available_height()),
text_edit,
);
});
});
});
});
}
}
}