This commit is contained in:
2025-07-17 02:18:29 +01:00
parent fe8bbae68e
commit 224300f3ea
29 changed files with 1880 additions and 1439 deletions
+306
View File
@@ -0,0 +1,306 @@
use egui::TextEdit;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::{PROJECT_FOLDER, editors::tags::Tag, util};
pub struct MainEditor {
pub content: ContentSection,
pub show_editor: bool,
pub show_preview: bool,
preview_cache: CommonMarkCache,
}
impl Clone for MainEditor {
fn clone(&self) -> Self {
Self {
content: self.content.clone(),
show_editor: self.show_editor,
show_preview: self.show_preview,
preview_cache: CommonMarkCache::default(),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ContentSection {
#[serde(default)]
pub title: String,
#[serde(default)]
pub id: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub content: String,
// parent id
#[serde(default)]
pub parent: Option<String>,
#[serde(skip)]
pub saved: bool,
}
impl ContentSection {
pub fn new() -> Self {
Self {
title: String::new(),
id: uuid::Uuid::new_v4().to_string(),
description: String::new(),
tags: Vec::new(),
content: String::new(),
parent: None,
saved: false,
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("documents").join(format!("{id}.json"));
let content = std::fs::read_to_string(&path)?;
let mut section: Self = serde_json::from_str(&content)?;
section.saved = true;
section.id = id.to_string();
Ok(section)
}
pub fn create_child(&self) -> Self {
let mut child = Self::new();
child.title = format!("{} (Child)", self.title);
child.parent = Some(self.id.clone());
child
}
}
impl MainEditor {
pub fn new() -> Self {
Self {
content: ContentSection::new(),
show_editor: false, // Start with editor hidden
show_preview: true,
preview_cache: CommonMarkCache::default(),
}
}
pub fn open(content: ContentSection) -> Self {
Self {
content,
show_editor: true,
show_preview: true,
preview_cache: CommonMarkCache::default(),
}
}
pub fn ui(&mut self, ctx: &egui::Context) {
// Show the editor window if enabled
let mut show = self.show_editor;
if show {
egui::Window::new("Markdown Editor")
.resizable(true)
.default_width(1000.0)
.default_height(800.0)
.open(&mut show)
.show(ctx, |ui| {
ui.vertical(|ui| {
// check for Ctrl+S to save
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.content.save() {
eprintln!("Failed to save: {e}");
}
}
// display save state
util::saved_status(
ui,
self.content.saved,
&self.content.id,
&self.content.title,
);
// Save/Cancel buttons
ui.horizontal(|ui| {
// save button
if ui.button("Save").clicked() {
if let Err(e) = self.content.save() {
eprintln!("Failed to save: {e}");
}
}
// create copy button
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.content.id = uuid::Uuid::new_v4().to_string();
copy.content.title = format!("{} (Copy)", self.content.title);
copy.content.save().unwrap();
}
// delete button
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", self.content.id)),
)
.unwrap();
*self = Self::new();
}
// revert changes button
if ui.button("Revert changes").clicked() {
self.content = ContentSection::load(&self.content.id).unwrap();
}
// preview toggle
ui.checkbox(&mut self.show_preview, "Preview");
});
});
ui.separator();
// Name and description grid
egui::Grid::new("top_grid")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
ui.strong("Name");
if ui
.add(
TextEdit::singleline(&mut self.content.title)
.desired_width(f32::INFINITY)
.frame(false),
)
.changed()
{
self.content.saved = false;
}
ui.end_row();
ui.strong("Description");
if ui
.add(
TextEdit::singleline(&mut self.content.description)
.desired_width(f32::INFINITY)
.frame(false),
)
.changed()
{
self.content.saved = false;
}
ui.end_row();
ui.strong("Tags");
Tag::selector_ui(
&mut self.content.tags,
ui,
Some(&mut self.content.saved),
);
ui.end_row();
});
ui.separator();
if self.show_preview {
self.preview_ui(ui);
}
self.editor_ui(ui);
});
}
self.show_editor = show;
}
fn preview_ui(&mut self, ui: &mut egui::Ui) {
// 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.content.content);
});
});
});
});
}
fn editor_ui(&mut self, ui: &mut egui::Ui) {
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).max(30.0) / 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.content.content)
.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);
if ui
.add_sized(
egui::vec2(max_width as f32 - 30.0, ui.available_height()),
text_edit,
)
.changed()
{
self.content.saved = false;
}
});
});
});
}
}