From 35ab72620698f0d7e2872fc2b593e8f308ced940 Mon Sep 17 00:00:00 2001 From: zxq5 Date: Mon, 14 Jul 2025 01:30:33 +0100 Subject: [PATCH] editor has separate window now --- Cargo.lock | 7 +++ Cargo.toml | 2 +- src/main.rs | 7 +++ src/main_editor.rs | 115 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 102 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 873125c..2c38be1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -851,6 +851,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f" +[[package]] +name = "data-url" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c297a1c74b71ae29df00c3e22dd9534821d60eb9af5a0192823fa2acea70c2a" + [[package]] name = "digest" version = "0.10.7" @@ -1052,6 +1058,7 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efb41b6833a6aaa99ca5c4f8e75b2410d69a7b3e30148d413f541147404a0dfa" dependencies = [ + "data-url", "egui", "egui_extras", "pulldown-cmark", diff --git a/Cargo.toml b/Cargo.toml index 2ea24b4..ea783f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.140" chrono = { version = "0.4.41", features = ["serde"] } thiserror = "2.0.12" -egui_commonmark = "0.20.0" +egui_commonmark = { version = "0.20.0", features = ["embedded_image"] } diff --git a/src/main.rs b/src/main.rs index 3d50c3c..e2121bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,6 +182,13 @@ impl eframe::App for Interface { // Top bar with actions egui::TopBottomPanel::top("top").show(ctx, |ui| { ui.horizontal(|ui| { + // Open Markdown Editor button + if ui.button("📝 Open Editor").clicked() { + self.editor.show_editor = true; + } + + ui.separator(); + // create new template if ui.button("New Template").clicked() { self.right_panel_content = RightPanelContent::Template { diff --git a/src/main_editor.rs b/src/main_editor.rs index ed40342..bd94c85 100644 --- a/src/main_editor.rs +++ b/src/main_editor.rs @@ -1,50 +1,109 @@ +use egui::{TextEdit, Ui}; use egui_commonmark::{CommonMarkCache, CommonMarkViewer}; pub struct MainEditor { - preview: bool, + pub show_editor: bool, + pub show_preview: bool, text: String, + preview_cache: CommonMarkCache, } impl MainEditor { pub fn new() -> Self { Self { - preview: true, + 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) { - if self.preview { - egui::TopBottomPanel::bottom("bottom_panel") + // Show the editor window if enabled + if self.show_editor { + egui::Window::new("Markdown Editor") .resizable(true) - .default_height(250.0) + .default_width(1000.0) + .default_height(800.0) + .max_height(800.0) + .open(&mut self.show_editor) .show(ctx, |ui| { - egui::ScrollArea::vertical() - .stick_to_bottom(true) - .auto_shrink(false) + 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 mut cache = CommonMarkCache::default(); - CommonMarkViewer::new().show(ui, &mut cache, &self.text); + 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, + ); + }); + }); }); }); } - - egui::CentralPanel::default().show(ctx, |ui| { - egui::ScrollArea::vertical() - .enable_scrolling(true) - .auto_shrink(false) - .show(ui, |ui| { - ui.add( - egui::TextEdit::multiline(&mut self.text) - .id_source("MainEditor_numlines") - .font(egui::TextStyle::Monospace) - .interactive(true) - .frame(false) - .lock_focus(true) - .hint_text("Type here...") - .desired_width(256.0), - ); - }); - }); } }