3 Commits

17 changed files with 576 additions and 813 deletions
+2 -2
View File
@@ -6,8 +6,8 @@
"files.trimFinalNewlines": true, "files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true, "files.trimTrailingWhitespace": true,
"rust-analyzer.cargo.features": [ "rust-analyzer.cargo.features": [
"llm", "native",
"native" "llm"
], ],
"rust-analyzer.cargo.noDefaultFeatures": true, "rust-analyzer.cargo.noDefaultFeatures": true,
"rust-analyzer.cargo.allFeatures": false "rust-analyzer.cargo.allFeatures": false
+10 -7
View File
@@ -1,6 +1,10 @@
use egui::{TextEdit, vec2}; use egui::{TextEdit, vec2};
use crate::{PROJECT_FOLDER, filesystem::Id, util}; use crate::{
PROJECT_FOLDER,
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
util,
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Asset { pub struct Asset {
@@ -25,15 +29,14 @@ impl Asset {
println!("old_path: {old_path:?}"); println!("old_path: {old_path:?}");
println!("new_path: {new_path:?}"); println!("new_path: {new_path:?}");
// move from src dir to name path if let Err(FsError::Io(err)) = FILESYSTEM.rename(&old_path, &new_path) {
if let Err(err) = std::fs::rename(&old_path, &new_path) {
match err.kind() { match err.kind() {
std::io::ErrorKind::NotFound => { std::io::ErrorKind::NotFound => {
let dir = new_path.parent().unwrap(); let dir = new_path.parent().unwrap();
if !dir.exists() { if !dir.exists() {
std::fs::create_dir_all(dir).unwrap(); FILESYSTEM.mkdir(dir).unwrap();
} }
std::fs::rename(&old_path, &new_path).unwrap(); FILESYSTEM.rename(&old_path, &new_path).unwrap();
} }
_ => panic!("Failed to rename file: {err}"), _ => panic!("Failed to rename file: {err}"),
} }
@@ -48,7 +51,7 @@ impl Asset {
pub fn ui(&mut self, ui: &mut egui::Ui) { pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical(|ui| { ui.vertical(|ui| {
util::saved_status(ui, self.saved, &Id::new(), &self.new_name); util::saved_status(ui, self.saved, &self.name, &self.new_name);
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl)
|| ui.button("Save").clicked() || ui.button("Save").clicked()
@@ -73,7 +76,7 @@ impl Asset {
ui.separator(); ui.separator();
if let Ok(bytes) = std::fs::read(Self::path(&self.name)) { if let Ok(bytes) = FILESYSTEM.read_bytes(&Self::path(&self.name)) {
let image_source = egui::ImageSource::Bytes { let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(self.name.clone()), uri: std::borrow::Cow::Owned(self.name.clone()),
bytes: bytes.into(), bytes: bytes.into(),
+40 -53
View File
@@ -1,11 +1,12 @@
use std::path::Path;
use egui::TextEdit; use egui::TextEdit;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer}; use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize}; use serde::{self, Deserialize, Serialize};
use crate::{ use crate::{
FILESYSTEM, PROJECT_FOLDER,
editors::{settings_editor::ProjectSettings, tags::Tag}, editors::{settings_editor::ProjectSettings, tags::Tag},
filesystem::{FileSystem, Id}, filesystem::{FILESYSTEM, LegacyFileSystem},
util, util,
}; };
@@ -44,23 +45,21 @@ impl Clone for MainEditor {
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ContentSection { pub struct ContentSection {
#[serde(default)]
pub title: String, pub title: String,
pub id: String,
pub id: Id,
#[serde(default)] #[serde(default)]
pub description: String, pub description: String,
#[serde(default)] #[serde(default)]
pub tags: Vec<Id>, pub tags: Vec<String>,
#[serde(default)] #[serde(default)]
pub content: String, pub content: String,
// parent id // parent id
#[serde(default)] #[serde(default)]
pub parent: Option<Id>, pub parent: Option<String>,
#[serde(skip)] #[serde(skip)]
pub saved: bool, pub saved: bool,
@@ -70,7 +69,7 @@ impl ContentSection {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
title: String::new(), title: String::new(),
id: Id::new(), id: uuid::Uuid::new_v4().to_string(),
description: String::new(), description: String::new(),
tags: Vec::new(), tags: Vec::new(),
content: String::new(), content: String::new(),
@@ -79,29 +78,19 @@ impl ContentSection {
} }
} }
pub fn save<F: FileSystem>( pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
&mut self, FILESYSTEM.write(
filesystem: &F, Path::new(&format!("documents/{id}.json", id = &self.id)),
) -> Result<(), Box<dyn std::error::Error>> { self.clone(),
let documents_dir = PROJECT_FOLDER.join("documents"); )?;
if filesystem.exists(&self.id) {
filesystem.write(&self.id, self.clone())?;
} else {
let _new_id = filesystem.create(&documents_dir, self.clone())?;
// Note: The filesystem creates its own ID, but we keep our existing ID for consistency
}
self.saved = true; self.saved = true;
Ok(()) Ok(())
} }
pub fn load<F: FileSystem>( pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
filesystem: &F, let mut section: Self = FILESYSTEM.read(Path::new(&format!("documents/{id}.json")))?;
id: &Id,
) -> Result<Self, Box<dyn std::error::Error>> {
let mut section: Self = filesystem.read(id)?;
section.saved = true; section.saved = true;
section.id = id.clone(); section.id = id.to_string();
Ok(section) Ok(section)
} }
@@ -144,16 +133,11 @@ impl MainEditor {
} }
} }
pub fn render_ui<F: FileSystem>( pub fn render_ui(&mut self, project: &mut ProjectSettings, ui: &mut egui::Ui) {
&mut self,
project: &mut ProjectSettings,
filesystem: &F,
ui: &mut egui::Ui,
) {
ui.vertical(|ui| { ui.vertical(|ui| {
// check for Ctrl+S to save // check for Ctrl+S to save
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) { if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.content.save(filesystem) { if let Err(e) = self.content.save() {
eprintln!("Failed to save: {e}"); eprintln!("Failed to save: {e}");
} }
} }
@@ -170,7 +154,7 @@ impl MainEditor {
ui.horizontal(|ui| { ui.horizontal(|ui| {
// save button // save button
if ui.button("Save").clicked() { if ui.button("Save").clicked() {
if let Err(e) = self.content.save(filesystem) { if let Err(e) = self.content.save() {
eprintln!("Failed to save: {e}"); eprintln!("Failed to save: {e}");
} }
} }
@@ -178,23 +162,26 @@ impl MainEditor {
// create copy button // create copy button
if ui.button("Create Copy").clicked() { if ui.button("Create Copy").clicked() {
let mut copy = self.clone(); let mut copy = self.clone();
copy.content.id = Id::new(); copy.content.id = uuid::Uuid::new_v4().to_string();
copy.content.title = format!("{} (Copy)", self.content.title); copy.content.title = format!("{} (Copy)", self.content.title);
copy.content.save().unwrap();
FILESYSTEM.clone(&self.content.id, &copy.content.id);
// TODO: Fix save call to pass filesystem
// copy.content.save().unwrap();
} }
// delete button // delete button
if ui.button("Delete").clicked() { if ui.button("Delete").clicked() {
filesystem.delete(&self.content.id).unwrap(); FILESYSTEM
.delete(Path::new(&format!(
"documents/{id}.json",
id = &self.content.id
)))
.unwrap();
*self = Self::new(); *self = Self::new();
} }
// revert changes button // revert changes button
if ui.button("Revert changes").clicked() { if ui.button("Revert changes").clicked() {
self.content = ContentSection::load(filesystem, &self.content.id).unwrap(); self.content = ContentSection::load(&self.content.id).unwrap();
} }
// preview toggle // preview toggle
@@ -202,7 +189,9 @@ impl MainEditor {
// assistant toggle // assistant toggle
#[cfg(feature = "llm")] #[cfg(feature = "llm")]
ui.checkbox(&mut self.show_ai, "AI Assistant"); if project.ai_enabled() {
ui.checkbox(&mut self.show_ai, "AI Assistant");
}
// editor toggle // editor toggle
ui.checkbox(&mut self.editor_separate_window, "Pop out editor"); ui.checkbox(&mut self.editor_separate_window, "Pop out editor");
@@ -250,7 +239,7 @@ impl MainEditor {
ui.separator(); ui.separator();
#[cfg(feature = "llm")] #[cfg(feature = "llm")]
if self.show_ai { if self.show_ai && project.ai_enabled() {
let dialog = &mut self.dialog; let dialog = &mut self.dialog;
dialog.content = self.content.content.clone(); dialog.content = self.content.content.clone();
@@ -274,12 +263,7 @@ impl MainEditor {
self.editor_ui(ui, project); self.editor_ui(ui, project);
} }
pub fn ui<F: FileSystem>( pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectSettings) {
&mut self,
ctx: &egui::Context,
project: &mut ProjectSettings,
filesystem: &F,
) {
// Show the editor window if enabled // Show the editor window if enabled
let mut show = self.show_editor; let mut show = self.show_editor;
if show { if show {
@@ -290,11 +274,11 @@ impl MainEditor {
.default_height(800.0) .default_height(800.0)
.open(&mut show) .open(&mut show)
.show(ctx, |ui| { .show(ctx, |ui| {
self.render_ui(project, filesystem, ui); self.render_ui(project, ui);
}); });
} else { } else {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
self.render_ui(project, filesystem, ui); self.render_ui(project, ui);
}); });
} }
} }
@@ -336,7 +320,7 @@ impl MainEditor {
}); });
} }
fn editor_ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) { fn editor_ui(&mut self, ui: &mut egui::Ui, _project: &mut ProjectSettings) {
let _response = egui::ScrollArea::both() let _response = egui::ScrollArea::both()
.auto_shrink([false, false]) .auto_shrink([false, false])
.id_salt("editor_scroll") .id_salt("editor_scroll")
@@ -354,7 +338,7 @@ impl MainEditor {
ui.set_min_width(max_width as f32); ui.set_min_width(max_width as f32);
ui.add( let response = ui.add(
TextEdit::multiline(&mut self.content.content) TextEdit::multiline(&mut self.content.content)
.id_source("MainEditor_editor") .id_source("MainEditor_editor")
.font(egui::TextStyle::Monospace) .font(egui::TextStyle::Monospace)
@@ -364,6 +348,9 @@ impl MainEditor {
.hint_text("Type here...") .hint_text("Type here...")
.desired_width(max_width as f32), .desired_width(max_width as f32),
); );
if response.changed() {
self.content.saved = false;
}
}); });
}); });
}); });
+42 -20
View File
@@ -1,36 +1,40 @@
use std::fs; use std::path::Path;
use egui::TextEdit; use egui::TextEdit;
use serde::{Deserialize, Serialize, de::DeserializeOwned}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
FILESYSTEM, PROJECT_FOLDER,
editors::tags::Tag, editors::tags::Tag,
filesystem::{FileSystem, Id}, filesystem::{FILESYSTEM, LegacyFileSystem},
util, util,
}; };
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Note { pub struct Note {
pub id: String,
pub name: String, pub name: String,
#[serde(default)] #[serde(default)]
pub content: String, pub content: String,
#[serde(default)] #[serde(default)]
pub subject: String, pub subject: String,
#[serde(default)] #[serde(default)]
pub tags: Vec<Id>, pub tags: Vec<String>,
pub id: Id,
#[serde(skip)] #[serde(skip)]
#[serde(default = "default_saved")]
pub saved: bool, pub saved: bool,
} }
pub fn default_saved() -> bool {
true
}
impl Default for Note { impl Default for Note {
fn default() -> Self { fn default() -> Self {
Self { Self {
id: Id::new(), id: uuid::Uuid::new_v4().to_string(),
name: "New Note".to_string(), name: "New Note".to_string(),
subject: "".to_string(), subject: "".to_string(),
content: "".to_string(), content: "".to_string(),
@@ -43,7 +47,7 @@ impl Default for Note {
impl Note { impl Note {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
id: Id::new(), id: uuid::Uuid::new_v4().to_string(),
name: "New Note".to_string(), name: "New Note".to_string(),
subject: "".to_string(), subject: "".to_string(),
content: "".to_string(), content: "".to_string(),
@@ -52,18 +56,16 @@ impl Note {
} }
} }
pub fn save(&mut self) -> std::io::Result<()> { pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let id = &self.id; let id = &self.id;
let data = serde_json::to_string(&self)?; FILESYSTEM.write(Path::new(&format!("notes/{id}.json")), self.clone())?;
FILESYSTEM.write(id, data).unwrap();
self.saved = true; self.saved = true;
Ok(()) Ok(())
} }
pub fn load(id: &Id) -> std::io::Result<Self> { pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut note: Note = FILESYSTEM.read(id).unwrap(); let mut note: Self = FILESYSTEM.read(Path::new(&format!("notes/{id}.json")))?;
note.id = id.clone(); note.id = id.to_string();
note.saved = true; note.saved = true;
Ok(note) Ok(note)
} }
@@ -77,11 +79,31 @@ impl Note {
util::saved_status(ui, self.saved, &self.id, &self.name); util::saved_status(ui, self.saved, &self.id, &self.name);
if ui.button("Save").clicked() { ui.horizontal(|ui| {
if let Err(e) = self.save() { if ui.button("Save").clicked() {
eprintln!("Failed to save: {e}"); if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
} }
}
if ui.button("Create Copy").clicked() {
let new_id = uuid::Uuid::new_v4().to_string();
let mut new_note = self.clone();
new_note.id = new_id;
new_note.name = format!("{} (Copy)", self.name);
if let Err(e) = new_note.save() {
eprintln!("Failed to save copy: {e}");
}
}
if ui.button("Delete").clicked() {
if let Err(e) =
FILESYSTEM.delete(Path::new(&format!("notes/{id}.json", id = self.id)))
{
eprintln!("Failed to delete: {e}");
}
}
});
let id = ui.make_persistent_id("note_name"); let id = ui.make_persistent_id("note_name");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true) egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
+36 -26
View File
@@ -4,27 +4,29 @@ use serde::{Deserialize, Serialize};
use std::path::Path; use std::path::Path;
use crate::{ use crate::{
FILESYSTEM, PROJECT_FOLDER, RightPanelContent, PROJECT_FOLDER, RightPanelContent,
editors::{ editors::{
tags::Tag, tags::Tag,
template_editor::{FieldValue, Template}, template_editor::{FieldValue, Template},
}, },
filesystem::{FileSystem, Id}, filesystem::{FILESYSTEM, LegacyFileSystem},
util, util,
}; };
pub type ObjectId = String;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct ObjectInstance { pub struct ObjectInstance {
// template info // template info
pub id: Id, pub id: ObjectId,
pub template_id: Id, pub template_id: String,
// instance info // instance info
pub name: String, pub name: String,
pub fields: std::collections::HashMap<String, FieldValue>, pub fields: std::collections::HashMap<String, FieldValue>,
#[serde(default)] #[serde(default)]
pub tags: Vec<Id>, pub tags: Vec<String>,
#[serde(skip)] #[serde(skip)]
pub saved: bool, pub saved: bool,
@@ -50,8 +52,8 @@ impl Clone for ObjectInstance {
impl Default for ObjectInstance { impl Default for ObjectInstance {
fn default() -> Self { fn default() -> Self {
Self { Self {
id: Id::new(), id: uuid::Uuid::new_v4().to_string(),
template_id: Id::new(), template_id: "new_template_instance".to_string(),
name: "new_object".to_string(), name: "new_object".to_string(),
fields: std::collections::HashMap::new(), fields: std::collections::HashMap::new(),
tags: Vec::new(), tags: Vec::new(),
@@ -69,25 +71,26 @@ impl ObjectInstance {
fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type)); fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type));
} }
let instance = Self { Self {
fields, id: uuid::Uuid::new_v4().to_string(),
template_id: template.id.clone(), template_id: template.id.clone(),
..Default::default() name: "new_object".to_string(),
}; fields,
tags: Vec::new(),
let _ = FILESYSTEM.create(Path::new("./objects"), instance.clone()); saved: false,
dialog: None,
instance }
} }
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
FILESYSTEM.write(&self.id, self.clone())?; let id = &self.id;
FILESYSTEM.write(Path::new(&format!("objects/{id}.json")), self.clone())?;
self.saved = true; self.saved = true;
Ok(()) Ok(())
} }
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> { pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut instance: ObjectInstance = FILESYSTEM.read(id)?; let mut instance: Self = FILESYSTEM.read(Path::new(&format!("objects/{id}.json")))?;
instance.saved = true; instance.saved = true;
Ok(instance) Ok(instance)
} }
@@ -119,14 +122,21 @@ impl ObjectInstance {
} }
if ui.button("Create Copy").clicked() { if ui.button("Create Copy").clicked() {
let new_id = Id::new(); let mut copy = self.clone();
FILESYSTEM.clone(&self.id, &new_id).unwrap(); copy.id = uuid::Uuid::new_v4().to_string();
let copy = Self::load(&new_id).unwrap(); copy.dialog = None;
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
*right_panel = Some(RightPanelContent::Object(Box::new(copy))); *right_panel = Some(RightPanelContent::Object(Box::new(copy)));
} }
if ui.button("Delete").clicked() { if ui.button("Delete").clicked() {
FILESYSTEM.delete(&self.id).unwrap(); let id = &self.id;
FILESYSTEM
.delete(Path::new(&format!("objects/{id}.json")))
.expect("Failed to delete object");
*right_panel = Some(RightPanelContent::None); *right_panel = Some(RightPanelContent::None);
} }
}); });
@@ -257,7 +267,7 @@ impl ObjectInstance {
if !value.is_empty() { if !value.is_empty() {
let path = PROJECT_FOLDER.join("assets").join(&value); let path = PROJECT_FOLDER.join("assets").join(&value);
if let Ok(bytes) = std::fs::read(&path) { if let Ok(bytes) = FILESYSTEM.read_bytes(&path) {
let image_source = egui::ImageSource::Bytes { let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(path.to_str().unwrap().to_string()), uri: std::borrow::Cow::Owned(path.to_str().unwrap().to_string()),
bytes: bytes.into(), bytes: bytes.into(),
@@ -282,12 +292,12 @@ impl ObjectInstance {
} }
fn selector_ui( fn selector_ui(
selected: &mut Id, selected: &mut ObjectId,
objects: &mut [ObjectInstance], objects: &mut [ObjectInstance],
ui: &mut egui::Ui, ui: &mut egui::Ui,
saved: &mut bool, saved: &mut bool,
) { ) {
if !selected.to_string().is_empty() { if !selected.is_empty() {
if let Ok(object) = ObjectInstance::load(selected) { if let Ok(object) = ObjectInstance::load(selected) {
ui.strong(&object.name); ui.strong(&object.name);
} }
@@ -323,7 +333,7 @@ impl ObjectInstance {
} }
if ui.button("Remove").clicked() { if ui.button("Remove").clicked() {
*selected = Id::default(); *selected = String::new();
*saved = false; *saved = false;
} }
}); });
+197 -128
View File
@@ -2,9 +2,12 @@ use chrono::NaiveDate;
use egui::TextEdit; use egui::TextEdit;
use egui_extras::DatePickerButton; use egui_extras::DatePickerButton;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{io::Read, path::PathBuf, sync::LazyLock}; use std::path::{Path, PathBuf};
use crate::{PROJECT_FOLDER, filesystem::Id, util::saved_status}; use crate::{
filesystem::{FILESYSTEM, LegacyFileSystem},
util::saved_status,
};
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct ProjectSettings { pub struct ProjectSettings {
@@ -14,6 +17,7 @@ pub struct ProjectSettings {
project_description: String, project_description: String,
// AI settings // AI settings
#[cfg(feature = "llm")]
pub ai_context: String, pub ai_context: String,
// settings // settings
@@ -29,17 +33,6 @@ pub struct ProjectSettings {
pub saved: bool, pub saved: bool,
} }
static GLOBAL_SETTINGS_PATH: LazyLock<String> =
LazyLock::new(|| match std::env::var("XDG_CONFIG_HOME") {
Ok(path) => path + "/worldcoder/settings.json",
Err(_) => {
eprintln!(
"XDG_CONFIG_HOME not set, using default path of ~/.config/worldcoder/settings.json"
);
"~/.config/worldcoder/settings.json".to_string()
}
});
impl ProjectSettings { impl ProjectSettings {
#[allow(dead_code)] #[allow(dead_code)]
pub fn new() -> Self { pub fn new() -> Self {
@@ -47,25 +40,10 @@ impl ProjectSettings {
} }
pub fn load() -> Self { pub fn load() -> Self {
let project_path = PROJECT_FOLDER.join("project.json"); if let Ok(mut proj) = FILESYSTEM.read::<Self>(Path::new("project.json")) {
let mut file = if let Ok(file) = std::fs::File::open(project_path) {
file
} else {
return Self::default();
};
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
if let Ok(mut proj) = serde_json::from_str::<Self>(&contents) {
proj.saved = true; proj.saved = true;
// load global settings
proj.global_settings = EditorSettings::load_global(); proj.global_settings = EditorSettings::load_global();
// load local overrides
proj.local_overrides = EditorSettings::load(); proj.local_overrides = EditorSettings::load();
proj proj
} else { } else {
Self::default() Self::default()
@@ -73,9 +51,9 @@ impl ProjectSettings {
} }
pub fn save(&mut self) { pub fn save(&mut self) {
let project_path = PROJECT_FOLDER.join("project.json"); FILESYSTEM
let content = serde_json::to_string_pretty(self).unwrap(); .write(Path::new("project.json"), self.clone())
std::fs::write(project_path, content).unwrap(); .unwrap();
self.global_settings.save(); self.global_settings.save();
self.local_overrides.save(); self.local_overrides.save();
@@ -83,14 +61,104 @@ impl ProjectSettings {
self.saved = true; self.saved = true;
} }
#[allow(dead_code)] #[allow(unused)]
fn config_str_override(
label: &str,
field: &mut Option<String>,
default: &str,
ui: &mut egui::Ui,
) -> bool {
let mut changed = false;
ui.label(label);
if let Some(value) = field {
if ui.text_edit_singleline(value).changed() {
changed = true;
};
if ui.button("Remove Override").clicked() {
*field = None;
changed = true;
}
} else if ui.button("Override").clicked() {
*field = Some(default.to_string());
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_bool_override(
label: &str,
field: &mut Option<bool>,
default: bool,
ui: &mut egui::Ui,
) -> bool {
let mut changed = false;
ui.label(label);
if let Some(value) = field {
if ui.checkbox(value, "Enable AI").changed() {
changed = true;
};
if ui.button("Remove Override").clicked() {
*field = None;
changed = true;
}
} else if ui.button("Override").clicked() {
*field = Some(default);
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_str(field: &mut String, label: &str, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label(label);
if ui.text_edit_singleline(field).changed() {
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_bool(label: &str, field: &mut bool, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label(label);
if ui.checkbox(field, "Enable AI").changed() {
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
pub fn ui(&mut self, ui: &mut egui::Ui) { pub fn ui(&mut self, ui: &mut egui::Ui) {
// save state // save state
saved_status(ui, self.saved, &Id::default(), "Project Settings"); saved_status(ui, self.saved, "N/A", "Project Settings");
if ui.button("Save").clicked() { if ui.button("Save").clicked() {
self.save(); self.save();
} }
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
self.save();
}
ui.separator(); ui.separator();
// project settings // project settings
@@ -99,80 +167,65 @@ impl ProjectSettings {
.striped(true) .striped(true)
.num_columns(2) .num_columns(2)
.show(ui, |ui| { .show(ui, |ui| {
ui.label("Project Name"); if Self::config_str(&mut self.project_name, "Project Name", ui) { self.saved = false };
ui.text_edit_singleline(&mut self.project_name); if Self::config_str(&mut self.project_author, "Project Author", ui) { self.saved = false };
if Self::config_str(&mut self.project_description, "Project Description", ui) { self.saved = false };
ui.end_row();
ui.label("Project Author");
ui.text_edit_singleline(&mut self.project_author);
ui.end_row();
ui.label("Project Description");
ui.text_edit_singleline(&mut self.project_description);
ui.end_row();
ui.label("Date"); ui.label("Date");
ui.add(DatePickerButton::new(&mut self.date)); if ui.add(DatePickerButton::new(&mut self.date)).changed() { self.saved = false };
ui.end_row(); ui.end_row();
ui.label("AI Context Prompt"); #[cfg(feature = "llm")]
ui.add(TextEdit::multiline(&mut self.ai_context) {
.font(egui::TextStyle::Monospace) ui.label("AI Context Prompt");
.interactive(true) if ui.add(TextEdit::multiline(&mut self.ai_context)
.frame(false) .font(egui::TextStyle::Monospace)
.lock_focus(true) .interactive(true)
.hint_text("What is this project about? what should the LLM know when generating content for this project?")); .frame(false)
.lock_focus(true)
ui.end_row(); .hint_text("What is this project about? what should the LLM know when generating content for this project?")).changed() { self.saved = false };
ui.end_row();
}
}); });
ui.separator(); ui.separator();
// local settings overrides for editor // local settings overrides for editor
ui.heading("Local Overrides"); ui.heading("Local Overrides");
egui::Grid::new("local overrides") egui::Grid::new("local overrides")
.striped(true) .striped(true)
.num_columns(2) .num_columns(2)
.show(ui, |ui| { .show(ui, |ui| {
ui.label("Enable AI"); #[cfg(feature = "llm")]
if let Some(ai_enabled) = &mut self.local_overrides.ai_enabled { if ProjectSettings::config_str_override(
ui.checkbox(ai_enabled, "Enable AI"); "LLM API URI",
if ui.button("Remove Override").clicked() { &mut self.local_overrides.llm_api_uri,
self.local_overrides.ai_enabled = None; "http://localhost:1234",
} ui,
} else if ui.button("Override").clicked() { ) {
self.local_overrides.ai_enabled = Some(true); self.saved = false;
} }
ui.end_row(); #[cfg(feature = "llm")]
if ProjectSettings::config_str_override(
ui.label("LLM API URI"); "LLM API Key",
if let Some(llm_api_uri) = &mut self.local_overrides.llm_api_uri { &mut self.local_overrides.llm_api_key,
ui.text_edit_singleline(llm_api_uri); "1234",
if ui.button("Remove Override").clicked() { ui,
self.local_overrides.llm_api_uri = None; ) {
} self.saved = false;
} else if ui.button("Override").clicked() {
self.local_overrides.llm_api_uri = Some("http://localhost:1234".to_string());
} }
ui.end_row(); #[cfg(feature = "llm")]
if ProjectSettings::config_bool_override(
ui.label("LLM API Key"); "Enable AI",
if let Some(llm_api_key) = &mut self.local_overrides.llm_api_key { &mut self.local_overrides.ai_enabled,
ui.text_edit_singleline(llm_api_key); true,
if ui.button("Remove Override").clicked() { ui,
self.local_overrides.llm_api_key = None; ) {
} self.saved = false;
} else if ui.button("Override").clicked() {
self.local_overrides.llm_api_key = Some("1234".to_string());
} }
ui.end_row();
}); });
ui.separator(); ui.separator();
@@ -183,23 +236,37 @@ impl ProjectSettings {
.striped(true) .striped(true)
.num_columns(2) .num_columns(2)
.show(ui, |ui| { .show(ui, |ui| {
ui.label("Enable AI"); #[cfg(feature = "llm")]
ui.checkbox(&mut self.global_settings.ai_enabled.unwrap(), "Enable AI"); if Self::config_bool(
"Enable AI",
self.global_settings.ai_enabled.as_mut().unwrap(),
ui,
) {
self.saved = false;
}
ui.end_row(); #[cfg(feature = "llm")]
if Self::config_str(
self.global_settings.llm_api_uri.as_mut().unwrap(),
"LLM API URI",
ui,
) {
self.saved = false
};
ui.label("LLM API URI"); #[cfg(feature = "llm")]
ui.text_edit_singleline(self.global_settings.llm_api_uri.as_mut().unwrap()); if Self::config_str(
self.global_settings.llm_api_key.as_mut().unwrap(),
ui.end_row(); "LLM API Key",
ui,
ui.label("LLM API Key"); ) {
ui.text_edit_singleline(self.global_settings.llm_api_key.as_mut().unwrap()); self.saved = false
};
ui.end_row();
}); });
} }
#[cfg(feature = "llm")]
#[allow(unused)]
pub fn ai_enabled(&mut self) -> bool { pub fn ai_enabled(&mut self) -> bool {
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
@@ -236,8 +303,9 @@ impl Default for ProjectSettings {
project_author: "Your Name".to_string(), project_author: "Your Name".to_string(),
project_description: "Description of your project".to_string(), project_description: "Description of your project".to_string(),
#[cfg(feature = "llm")]
ai_context: "".to_string(), ai_context: "".to_string(),
global_settings: EditorSettings::new(), global_settings: EditorSettings::default(),
local_overrides: EditorSettings::new(), local_overrides: EditorSettings::new(),
// window state // window state
@@ -249,11 +317,15 @@ impl Default for ProjectSettings {
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct EditorSettings { pub struct EditorSettings {
pub llm_api_uri: Option<String>,
pub llm_api_key: Option<String>,
pub ai_enabled: Option<bool>,
pub dark_theme: Option<bool>, pub dark_theme: Option<bool>,
#[cfg(feature = "llm")]
pub llm_api_uri: Option<String>,
#[cfg(feature = "llm")]
pub llm_api_key: Option<String>,
#[cfg(feature = "llm")]
pub ai_enabled: Option<bool>,
#[serde(skip)] #[serde(skip)]
is_global: bool, is_global: bool,
} }
@@ -261,8 +333,11 @@ pub struct EditorSettings {
impl Default for EditorSettings { impl Default for EditorSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
#[cfg(feature = "llm")]
llm_api_uri: Some("http://localhost:1234".to_string()), llm_api_uri: Some("http://localhost:1234".to_string()),
#[cfg(feature = "llm")]
llm_api_key: Some("".to_string()), llm_api_key: Some("".to_string()),
#[cfg(feature = "llm")]
ai_enabled: Some(true), ai_enabled: Some(true),
dark_theme: Some(true), dark_theme: Some(true),
@@ -275,8 +350,11 @@ impl Default for EditorSettings {
impl EditorSettings { impl EditorSettings {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
#[cfg(feature = "llm")]
llm_api_uri: None, llm_api_uri: None,
#[cfg(feature = "llm")]
llm_api_key: None, llm_api_key: None,
#[cfg(feature = "llm")]
ai_enabled: None, ai_enabled: None,
dark_theme: None, dark_theme: None,
@@ -285,43 +363,34 @@ impl EditorSettings {
} }
pub fn load() -> Self { pub fn load() -> Self {
let path = PROJECT_FOLDER.join("settings.json"); if let Ok(mut settings) = FILESYSTEM.read::<Self>(Path::new("settings.json")) {
let mut file = if let Ok(file) = std::fs::File::open(path) { settings.is_global = false;
file return settings;
} else { }
return Self::default();
};
let mut contents = String::new(); Self::new()
file.read_to_string(&mut contents).unwrap();
serde_json::from_str(&contents).unwrap()
} }
pub fn save(&self) { pub fn save(&self) {
let content = serde_json::to_string_pretty(self).unwrap();
let path = if self.is_global { let path = if self.is_global {
PathBuf::from(GLOBAL_SETTINGS_PATH.clone()) FILESYSTEM.config_path()
} else { } else {
PROJECT_FOLDER.join("settings.json") PathBuf::from("settings.json")
}; };
std::fs::write(path, content).unwrap(); FILESYSTEM.write(path.as_path(), self.clone()).unwrap()
} }
pub fn load_global() -> Self { pub fn load_global() -> Self {
let path = PathBuf::from(GLOBAL_SETTINGS_PATH.clone()); let path = FILESYSTEM.config_path();
if !path.exists() { if !path.exists() {
std::fs::create_dir_all(path.parent().unwrap()).unwrap(); FILESYSTEM.mkdir(path.parent().unwrap()).unwrap();
let content = serde_json::to_string_pretty(&Self::default()).unwrap(); FILESYSTEM.write(path.as_path(), Self::default()).unwrap();
std::fs::write(&path, content).unwrap();
// return a default config
return Self::default();
} }
let content = std::fs::read_to_string(path).unwrap(); let mut settings = FILESYSTEM.read::<Self>(path.as_path()).unwrap();
serde_json::from_str(&content).unwrap() settings.is_global = true;
settings
} }
} }
+7 -13
View File
@@ -1,15 +1,11 @@
use egui::{Response, RichText, TextEdit}; use egui::{Response, RichText, TextEdit};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{PROJECT_FOLDER, util};
FILESYSTEM, PROJECT_FOLDER,
filesystem::{FileSystem, Id},
util,
};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Tag { pub struct Tag {
pub id: Id, pub id: String,
pub name: String, pub name: String,
pub description: String, pub description: String,
pub color: egui::Color32, pub color: egui::Color32,
@@ -24,7 +20,7 @@ pub struct Tag {
impl Default for Tag { impl Default for Tag {
fn default() -> Self { fn default() -> Self {
Self { Self {
id: Id::new(), id: uuid::Uuid::new_v4().to_string(),
name: String::new(), name: String::new(),
description: String::new(), description: String::new(),
color: egui::Color32::from_rgb(20, 20, 20), color: egui::Color32::from_rgb(20, 20, 20),
@@ -132,7 +128,7 @@ impl Tag {
}); });
} }
pub fn selector_ui(tag_ids: &mut Vec<Id>, ui: &mut egui::Ui, saved: Option<&mut bool>) { pub fn selector_ui(tag_ids: &mut Vec<String>, ui: &mut egui::Ui, saved: Option<&mut bool>) {
// remove duplicate tag ids // remove duplicate tag ids
tag_ids.sort(); tag_ids.sort();
tag_ids.dedup(); tag_ids.dedup();
@@ -206,11 +202,9 @@ impl Tag {
}); });
} }
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> { pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut tag: Self = FILESYSTEM.read(id)?; let path = PROJECT_FOLDER.join("tags").join(format!("{id}.json"));
tag.saved = true; Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
tag.id = id.clone();
Ok(tag)
} }
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
+18 -30
View File
@@ -2,11 +2,12 @@ use chrono::NaiveDate;
use core::fmt; use core::fmt;
use egui::ScrollArea; use egui::ScrollArea;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::{ use crate::{
FILESYSTEM, PROJECT_FOLDER, RightPanelContent, RightPanelContent,
editors::object_editor::ObjectInstance, editors::object_editor::ObjectInstance,
filesystem::{self, FileSystem, Id}, filesystem::{FILESYSTEM, LegacyFileSystem},
util::{self, Error}, util::{self, Error},
}; };
@@ -17,7 +18,7 @@ pub enum FieldType {
MultiLine, MultiLine,
Date, Date,
Number, Number,
Link { template_id: Option<Id> }, Link { template_id: Option<String> },
Links, Links,
} }
@@ -48,8 +49,8 @@ pub enum FieldValue {
MultiLine(String), MultiLine(String),
Date(NaiveDate), Date(NaiveDate),
Number(f64), Number(f64),
Link(Id), Link(String),
Links(Vec<Id>), Links(Vec<String>),
} }
impl FieldValue { impl FieldValue {
@@ -60,7 +61,7 @@ impl FieldValue {
FieldType::MultiLine => Self::MultiLine(String::new()), FieldType::MultiLine => Self::MultiLine(String::new()),
FieldType::Date => Self::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()), FieldType::Date => Self::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()),
FieldType::Number => Self::Number(0.0), FieldType::Number => Self::Number(0.0),
FieldType::Link { template_id: None } => Self::Link(Id::default()), FieldType::Link { template_id: None } => Self::Link(String::new()),
FieldType::Link { FieldType::Link {
template_id: Some(template_id), template_id: Some(template_id),
} => Self::Link(template_id.clone()), } => Self::Link(template_id.clone()),
@@ -89,7 +90,7 @@ pub struct FieldDefinition {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Template { pub struct Template {
pub name: String, pub name: String,
pub id: Id, pub id: String,
pub description: Option<String>, pub description: Option<String>,
pub fields: Vec<FieldDefinition>, pub fields: Vec<FieldDefinition>,
@@ -155,7 +156,7 @@ impl Default for Template {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: "New Template".to_string(), name: "New Template".to_string(),
id: Id::new(), id: uuid::Uuid::new_v4().to_string(),
description: Some(String::from("Placeholder description")), description: Some(String::from("Placeholder description")),
fields: Vec::new(), fields: Vec::new(),
saved: false, saved: false,
@@ -171,15 +172,15 @@ impl Default for Template {
} }
impl Template { impl Template {
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> { pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut template: Self = FILESYSTEM.read(id)?; let mut template = FILESYSTEM.read::<Self>(Path::new(&format!("templates/{id}.json")))?;
template.saved = true; template.saved = true;
template.id = id.clone();
Ok(template) Ok(template)
} }
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
FILESYSTEM.write(&self.id, self.clone())?; let id = &self.id;
FILESYSTEM.write(Path::new(&format!("templates/{id}.json")), self.clone())?;
self.saved = true; self.saved = true;
Ok(()) Ok(())
} }
@@ -197,16 +198,6 @@ impl Template {
ScrollArea::vertical().show(ui, |ui| { ScrollArea::vertical().show(ui, |ui| {
ui.vertical(|ui| { ui.vertical(|ui| {
// ui.group(|ui| {
// ui.horizontal(|ui| {
// if self.saved {
// ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
// } else {
// ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
// }
// ui.label(format!("id: {}", self.id));
// });
// });
util::saved_status(ui, self.saved, &self.id, &self.name); util::saved_status(ui, self.saved, &self.id, &self.name);
// Save/Cancel buttons // Save/Cancel buttons
@@ -219,19 +210,16 @@ impl Template {
if ui.button("Create Copy").clicked() { if ui.button("Create Copy").clicked() {
let mut copy = self.clone(); let mut copy = self.clone();
copy.id = Id::new(); copy.id = uuid::Uuid::new_v4().to_string();
copy.name = format!("{} (Copy)", self.name); copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap(); copy.save().unwrap();
} }
if ui.button("Delete").clicked() { if ui.button("Delete").clicked() {
std::fs::remove_file( let id = &self.id;
PROJECT_FOLDER FILESYSTEM
.join("templates") .delete(Path::new(&format!("templates/{id}.json")))
.join(format!("{}.json", self.id)), .unwrap();
)
.unwrap();
*new_instance = Some(RightPanelContent::None); *new_instance = Some(RightPanelContent::None);
} }
+63 -115
View File
@@ -1,7 +1,4 @@
use itertools::Itertools; use std::path::Path;
use std::fs::{self, DirEntry};
// use walkdir::{DirEntry, WalkDir};
use crate::{ use crate::{
PROJECT_FOLDER, RightPanelContent, PROJECT_FOLDER, RightPanelContent,
@@ -10,7 +7,7 @@ use crate::{
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance, asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
tags::Tag, template_editor::Template, tags::Tag, template_editor::Template,
}, },
filesystem::Id, filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
note_editor::Note, note_editor::Note,
}; };
@@ -166,14 +163,6 @@ impl Explorer {
}); });
} }
/// Recursively renders a tree of documents.
///
/// Each document is represented by a single element in the `documents` array.
/// The `parent_id` parameter is used to filter out documents that do not have the current
/// parent. If `parent_id` is `None`, all documents are rendered.
///
/// `load_doc` is a mutable reference to a `MainEditor`. When a document is clicked, it
/// is loaded into the `MainEditor` and returned as `Some`.
fn render_doc_branch( fn render_doc_branch(
ui: &mut egui::Ui, ui: &mut egui::Ui,
documents: &[ContentSection], documents: &[ContentSection],
@@ -183,7 +172,7 @@ impl Explorer {
// Filter documents that have the current parent (or no parent if this is the root) // Filter documents that have the current parent (or no parent if this is the root)
let child_docs: Vec<&ContentSection> = documents let child_docs: Vec<&ContentSection> = documents
.iter() .iter()
.filter(|doc| doc.parent.as_ref().map(|id| id.as_str()) == parent_id) .filter(|doc| doc.parent.as_deref() == parent_id)
.collect(); .collect();
for doc in child_docs { for doc in child_docs {
@@ -208,7 +197,7 @@ impl Explorer {
}) })
.body(|ui| { .body(|ui| {
// recursive call to render the next level of documents // recursive call to render the next level of documents
Self::render_doc_branch(ui, documents, Some(doc.id.as_str()), load_doc); Self::render_doc_branch(ui, documents, Some(&doc.id), load_doc);
}); });
} }
} }
@@ -242,95 +231,64 @@ impl Explorer {
} }
fn render_assets(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) { fn render_assets(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) {
Self::render_asset_dir(ui, to_load, Path::new("assets"));
}
fn render_asset_dir(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
let files = FILESYSTEM.lsfiles(path).unwrap();
let dirs = FILESYSTEM.lsdirs(path).unwrap();
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
egui::collapsing_header::CollapsingState::load_with_default_open( egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(), ui.ctx(),
ui.make_persistent_id("assets"), ui.make_persistent_id(&file_name),
true, false,
) )
.show_header(ui, |ui| { .show_header(ui, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Assets"); ui.label(file_name);
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
}); });
}) })
.body(|ui| { .body(|ui| {
let entries = fs::read_dir(PROJECT_FOLDER.join("assets")) // recursive call to render the next level of documents
.unwrap() for dir in dirs.iter() {
.filter_map(Result::ok) Self::render_asset_dir(ui, to_load, dir);
.sorted_by(|a, b| { }
// Directories first, then files
let a_is_dir = a.file_type().unwrap().is_dir();
let b_is_dir = b.file_type().unwrap().is_dir();
if a_is_dir == b_is_dir {
a.file_name().cmp(&b.file_name())
} else if a_is_dir {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
})
.collect::<Vec<_>>();
for entry in entries { for file in files.iter() {
Self::render_entry(ui, to_load, &entry); Self::render_asset(ui, to_load, file);
} }
}); });
} }
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) { fn render_asset(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
let file_type = entry.file_type().unwrap(); let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
let is_dir = file_type.is_dir();
let file_name = entry.file_name().to_str().unwrap().to_string();
let path = entry.path();
if is_dir { if ui
let entries = fs::read_dir(path) .selectable_label(false, format!("📄 {file_name}"))
.unwrap() .clicked()
.filter_map(Result::ok) {
.collect::<Vec<_>>(); // use asset::load to get the file at the path
let asset_path = path.strip_prefix(PROJECT_FOLDER.join("assets")).unwrap();
egui::collapsing_header::CollapsingState::load_with_default_open( let asset = Asset::open(asset_path.to_string_lossy().to_string());
ui.ctx(), *to_load = Some(RightPanelContent::Asset(Box::new(asset)));
ui.make_persistent_id(&file_name),
false,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label(file_name);
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
});
})
.body(|ui| {
// recursive call to render the next level of documents
for entry in entries {
Self::render_entry(ui, to_load, &entry);
}
});
} else {
// Handle file
if ui
.selectable_label(false, format!("📄 {file_name}"))
.clicked()
{
// use asset::load to get the file at the path
let asset_path = path.strip_prefix(PROJECT_FOLDER.join("assets")).unwrap();
let asset = Asset::open(asset_path.to_string_lossy().to_string());
*to_load = Some(RightPanelContent::Asset(Box::new(asset)));
}
} }
} }
// load templates from the templates folder // load templates from the templates folder
fn load_templates(&mut self) -> std::io::Result<()> { fn load_templates(&mut self) -> Result<(), FsError> {
let templates_folder = PROJECT_FOLDER.join("templates"); let path = Path::new("templates");
if !templates_folder.exists() {
std::fs::create_dir_all(&templates_folder)?; if !FILESYSTEM.exists(path) {
FILESYSTEM.mkdir(path)?;
} }
let mut templates = Vec::new(); let mut templates = Vec::new();
for entry in std::fs::read_dir(&templates_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<Template>(&entry) {
match Template::load(&Id::from_path(&path)) {
Ok(t) => templates.push(t), Ok(t) => templates.push(t),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
} }
} }
self.templates = templates; self.templates = templates;
@@ -339,17 +297,16 @@ impl Explorer {
} }
// load objects from the objects folder // load objects from the objects folder
fn load_objects(&mut self) -> std::io::Result<()> { fn load_objects(&mut self) -> Result<(), FsError> {
let objects_folder = PROJECT_FOLDER.join("objects"); let path = Path::new("objects");
if !objects_folder.exists() { if !FILESYSTEM.exists(path) {
std::fs::create_dir_all(&objects_folder)?; FILESYSTEM.mkdir(path)?;
} }
let mut objects = Vec::new(); let mut objects = Vec::new();
for entry in std::fs::read_dir(&objects_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<ObjectInstance>(&entry) {
match ObjectInstance::load(&Id::from_path(&path)) {
Ok(o) => objects.push(o), Ok(o) => objects.push(o),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
} }
} }
self.objects = objects; self.objects = objects;
@@ -358,16 +315,15 @@ impl Explorer {
} }
// load notes from the notes folder // load notes from the notes folder
fn load_notes(&mut self) -> std::io::Result<()> { fn load_notes(&mut self) -> Result<(), FsError> {
let notes_folder = PROJECT_FOLDER.join("notes"); let path = Path::new("notes");
if !notes_folder.exists() { if !FILESYSTEM.exists(path) {
std::fs::create_dir_all(&notes_folder)?; FILESYSTEM.mkdir(path)?;
} }
let mut notes = Vec::new(); let mut notes = Vec::new();
for entry in std::fs::read_dir(&notes_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<Note>(&entry) {
match Note::load(&Id::from_path(&path)) {
Ok(note) => notes.push(note), Ok(note) => notes.push(note),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"), Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
} }
@@ -379,25 +335,17 @@ impl Explorer {
} }
// load documents from the documents folder // load documents from the documents folder
fn load_documents(&mut self) -> std::io::Result<()> { fn load_documents(&mut self) -> Result<(), FsError> {
let documents_folder = PROJECT_FOLDER.join("documents"); let path = Path::new("documents");
if !documents_folder.exists() { if !FILESYSTEM.exists(path) {
std::fs::create_dir_all(&documents_folder)?; FILESYSTEM.mkdir(path)?;
} }
let mut documents = Vec::new(); let mut documents = Vec::new();
for entry in std::fs::read_dir(&documents_folder).unwrap() { for entry in FILESYSTEM.lsfiles(path)? {
let path = entry.unwrap().path(); match FILESYSTEM.read::<ContentSection>(&entry) {
// TODO: Update to use FileSystem API Ok(document) => documents.push(MainEditor::open(document)),
// For now, read files directly until we refactor the loading system Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
if let Ok(content) = std::fs::read_to_string(&path) {
if let Ok(document) =
serde_json::from_str::<crate::editors::content_editor::ContentSection>(&content)
{
documents.push(MainEditor::open(document));
} else {
eprintln!("Could not parse file {path:?}");
}
} }
} }
@@ -406,7 +354,7 @@ impl Explorer {
Ok(()) Ok(())
} }
fn load_tags(&mut self) -> std::io::Result<()> { fn load_tags(&mut self) -> Result<(), FsError> {
self.tags = Tag::load_all(); self.tags = Tag::load_all();
Ok(()) Ok(())
} }
+73 -107
View File
@@ -1,11 +1,15 @@
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::{ use std::{
fmt, collections::HashMap,
ops::{Deref, DerefMut}, io,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::LazyLock,
}; };
use crate::FILESYSTEM; use serde::{Serialize, de::DeserializeOwned};
#[cfg(feature = "native")]
use crate::PROJECT_FOLDER;
use crate::filesystem::native::NativeFileSystem;
#[cfg(feature = "native")] #[cfg(feature = "native")]
pub mod native; pub mod native;
@@ -13,125 +17,87 @@ pub mod native;
#[cfg(feature = "web")] #[cfg(feature = "web")]
pub mod web; pub mod web;
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize)] pub static FILESYSTEM: LazyLock<NativeFileSystem> = LazyLock::new(|| {
pub struct Id(String); #[cfg(feature = "native")]
return NativeFileSystem::new(PROJECT_FOLDER.clone());
#[cfg(feature = "web")]
return Box::new(web::WebFileSystem::new());
});
impl Id { pub trait LegacyFileSystem {
pub fn new() -> Self { fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError>;
Self(uuid::Uuid::new_v4().to_string()) fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError>;
} fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError>;
} fn delete(&self, path: &Path) -> Result<(), FsError>;
fn lsfiles(&self, path: &Path) -> Result<Vec<PathBuf>, FsError>;
impl AsRef<str> for Id { fn lsdirs(&self, path: &Path) -> Result<Vec<PathBuf>, FsError>;
fn as_ref(&self) -> &str { fn mkdir(&self, path: &Path) -> Result<(), FsError>;
&self.0 fn rename(&self, path: &Path, new_path: &Path) -> Result<(), FsError>;
} #[allow(unused)]
} fn exists(&self, path: &Path) -> bool;
fn config_path(&self) -> PathBuf;
impl Id {
pub fn as_str(&self) -> &str {
&self.0
}
pub fn from_path(path: &Path) -> Self {
Self(path.file_name().unwrap().to_str().unwrap().to_string())
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Default for Id {
fn default() -> Self {
Id(String::new())
}
} }
// ────────────────────────────────────────────────────────────────
// Custom error type
// ────────────────────────────────────────────────────────────────
#[derive(Debug)] #[derive(Debug)]
pub enum FileSystemError { pub enum FsError {
FileNotFound(Id, String), Io(io::Error),
DirectoryNotFound(PathBuf, String), Serde(serde_json::Error),
} }
impl fmt::Display for FileSystemError { impl std::fmt::Display for FsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
FileSystemError::FileNotFound(id, message) => { FsError::Io(e) => write!(f, "IO error: {e}"),
write!(f, "File not found: {} - {}", id, message) FsError::Serde(e) => write!(f, "Serialization error: {e}"),
}
FileSystemError::DirectoryNotFound(id, message) => {
write!(f, "Directory not found: {} - {}", id.display(), message)
}
} }
} }
} }
impl std::error::Error for FileSystemError {} impl std::error::Error for FsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
#[derive(Debug, Clone, Serialize, Deserialize)] match self {
pub struct FileTree { FsError::Io(e) => Some(e),
pub name: String, FsError::Serde(e) => Some(e),
pub path: PathBuf, }
pub is_directory: bool, }
pub id: Option<Id>,
pub children: Vec<FileTree>,
} }
// Convert the two underlying error types into our own
impl From<io::Error> for FsError {
fn from(err: io::Error) -> Self {
FsError::Io(err)
}
}
impl From<serde_json::Error> for FsError {
fn from(err: serde_json::Error) -> Self {
FsError::Serde(err)
}
}
#[allow(dead_code)]
pub struct Id(String);
#[allow(dead_code)]
pub trait FileSystem { pub trait FileSystem {
fn new(root: impl AsRef<Path>) -> Self; fn load<T: DeserializeOwned>(&self, id: Id) -> Result<T, FsError>;
fn save<T: Serialize>(&self, id: Id, data: T) -> Result<(), FsError>;
fn create(&self, directory: &Path, data: impl Serialize) -> Result<Id, FileSystemError>; fn mkdir(&self, path: Path) -> Result<(), FsError>;
fn exists(&self, path: Path) -> bool;
fn read<T: DeserializeOwned>(&self, id: &Id) -> Result<T, FileSystemError>;
fn write(&self, id: &Id, data: impl Serialize) -> Result<(), FileSystemError>;
fn borrow<T: DeserializeOwned + Serialize>(
&self,
id: &Id,
) -> Result<FileBorrow<T>, FileSystemError> {
let file = self.read(id)?;
Ok(FileBorrow {
id: id.clone(),
file,
})
}
fn delete(&self, id: &Id) -> Result<(), FileSystemError>;
fn clone(&self, id: &Id, new_id: &Id) -> Result<(), FileSystemError>;
fn exists(&self, id: &Id) -> bool;
fn lsdir(&self, id: &Id) -> Result<Vec<String>, FileSystemError>;
fn file_tree(&self, root_path: &Path) -> Result<FileTree, FileSystemError>;
} }
pub struct FileBorrow<T: DeserializeOwned + Serialize> { #[allow(dead_code)]
pub id: Id, pub struct Index {
pub file: T, file_cache: HashMap<Id, PathBuf>,
project_root: Directory,
} }
impl<T: DeserializeOwned + Serialize> Deref for FileBorrow<T> { #[allow(dead_code)]
type Target = T; pub struct Directory {
name: String,
fn deref(&self) -> &Self::Target { id: Id,
&self.file children: HashMap<Id, Directory>,
} files: Vec<Id>,
}
impl<T: DeserializeOwned + Serialize> DerefMut for FileBorrow<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.file
}
}
impl<T: DeserializeOwned + Serialize> Drop for FileBorrow<T> {
fn drop(&mut self) {
FILESYSTEM.write(&self.id, &self.file).unwrap();
}
} }
+83 -294
View File
@@ -1,327 +1,116 @@
use std::collections::HashMap; use std::fs;
use std::fs::{self, File}; use std::io::{ErrorKind, Read};
use std::io::{Read, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use image::EncodableLayout; use serde::Serialize;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::to_string;
use crate::filesystem::FileSystemError; use crate::filesystem::{FsError, LegacyFileSystem};
use super::FileSystem;
pub struct NativeFileSystem { pub struct NativeFileSystem {
root: PathBuf, project_root: PathBuf,
index: Arc<RwLock<HashMap<super::Id, PathBuf>>>,
} }
impl NativeFileSystem { impl NativeFileSystem {
/// Rebuild the entire index by scanning the filesystem /// Create a new instance.
fn rebuild_index(&self) -> Result<(), std::io::Error> { pub fn new(root: impl Into<PathBuf>) -> Self {
let mut index = HashMap::new(); Self {
Self::scan_directory(&self.root, &mut index)?; project_root: root.into(),
if let Ok(mut idx) = self.index.write() {
*idx = index;
}
Ok(())
}
/// Recursively scan a directory and populate the index
fn scan_directory(
dir: &Path,
index: &mut HashMap<super::Id, PathBuf>,
) -> Result<(), std::io::Error> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
// Try to parse the filename as a UUID (our Id format)
if let Ok(uuid) = uuid::Uuid::parse_str(name) {
let id = super::Id(uuid.to_string());
index.insert(id, path.clone());
}
}
if path.is_dir() {
Self::scan_directory(&path, index)?;
}
}
Ok(())
}
/// Add an entry to the index
fn add_to_index(&self, id: super::Id, path: PathBuf) {
if let Ok(mut index) = self.index.write() {
index.insert(id, path);
} }
} }
/// Remove an entry from the index /// Resolve the user supplied *relative* path against the project root.
fn remove_from_index(&self, id: &super::Id) { #[inline]
if let Ok(mut index) = self.index.write() { fn full_path(&self, path: &Path) -> PathBuf {
index.remove(id); self.project_root.join(path)
}
}
/// Get path from index, with fallback to filesystem scan if not found
fn find_path_by_id(&self, id: &super::Id) -> Option<PathBuf> {
// First try the index
if let Ok(index) = self.index.read() {
if let Some(path) = index.get(id) {
// Verify the file still exists
if path.exists() {
return Some(path.clone());
}
}
}
// Fallback: scan filesystem and update index
let target_name = id.to_string();
let mut stack = vec![self.root.clone()];
while let Some(dir) = stack.pop() {
let entries = match fs::read_dir(&dir) {
Ok(e) => e,
Err(_) => continue,
};
for entry in entries.flatten() {
let path = entry.path();
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if name == target_name {
// Update index with found path
self.add_to_index(id.clone(), path.clone());
return Some(path);
}
}
if path.is_dir() {
stack.push(path);
}
}
}
None
}
/// Build a file tree recursively from a given path
fn build_file_tree(&self, path: &Path) -> Result<super::FileTree, FileSystemError> {
let name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
.to_string();
let is_directory = path.is_dir();
// Check if this path corresponds to an ID in our index
let id = if let Ok(index) = self.index.read() {
index
.iter()
.find(|(_, indexed_path)| indexed_path.as_path() == path)
.map(|(id, _)| id.clone())
} else {
None
};
let mut children = Vec::new();
if is_directory {
for entry in fs::read_dir(path).map_err(|e| {
FileSystemError::DirectoryNotFound(path.to_path_buf(), e.to_string())
})? {
let entry = entry.map_err(|e| {
FileSystemError::DirectoryNotFound(path.to_path_buf(), e.to_string())
})?;
let child_path = entry.path();
match self.build_file_tree(&child_path) {
Ok(child_tree) => children.push(child_tree),
Err(e) => eprintln!(
"Warning: Failed to build tree for {}: {}",
child_path.display(),
e
),
}
}
}
Ok(super::FileTree {
name,
path: path.to_path_buf(),
is_directory,
id,
children,
})
} }
} }
impl FileSystem for NativeFileSystem { impl LegacyFileSystem for NativeFileSystem {
fn new(root: impl AsRef<Path>) -> Self { fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> {
let fs = Self { let full_path = self.full_path(path);
root: root.as_ref().to_path_buf(), let file = fs::File::open(full_path).map_err(FsError::Io)?;
index: Arc::new(RwLock::new(HashMap::new())), serde_json::from_reader(file).map_err(FsError::Serde)
};
fs.rebuild_index().unwrap_or_else(|e| {
eprintln!("Warning: Failed to build initial index: {e}");
});
fs
} }
fn create(&self, directory: &Path, data: impl Serialize) -> Result<super::Id, FileSystemError> { fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError> {
let dir = if directory.is_absolute() { let full_path = self.full_path(path);
directory.to_path_buf() let mut contents = Vec::new();
} else { fs::File::open(full_path)?.read_to_end(&mut contents)?;
self.root.join(directory) Ok(contents)
}; }
if !dir.exists() { fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError> {
fs::create_dir_all(&dir).map_err(|e| { let full_path = self.full_path(path);
FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string())
})?; // Ensure the parent directory exists.
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent)?;
} }
if !dir.is_dir() { let file = fs::File::create(full_path)?;
return Err(FileSystemError::DirectoryNotFound( serde_json::to_writer(file, &data).map_err(FsError::Serde)
dir.to_path_buf(),
"".to_string(),
));
}
let id = super::Id::new();
let file_path = dir.join(id.to_string());
let mut file = File::create(&file_path)
.map_err(|e| FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string()))?;
file.write_all(serde_json::to_string(&data).unwrap().as_bytes())
.map_err(|e| FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string()))?;
// Add to index
self.add_to_index(id.clone(), file_path);
Ok(id)
} }
fn read<T: DeserializeOwned>(&self, id: &super::Id) -> Result<T, FileSystemError> { fn delete(&self, path: &Path) -> Result<(), FsError> {
let path = self.find_path_by_id(id).ok_or_else(|| { let full_path = self.full_path(path);
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string()) match fs::remove_file(&full_path) {
})?; Ok(()) => Ok(()),
Err(e) if e.kind() == ErrorKind::IsADirectory => {
let val = serde_json::from_reader( // Remove a directory tree.
File::open(path) fs::remove_dir_all(full_path).map_err(FsError::Io)
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?,
)
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?;
Ok(val)
}
fn write(&self, id: &super::Id, data: impl Serialize) -> Result<(), FileSystemError> {
let path = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let mut file = File::create(path)
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?;
file.write_all(serde_json::to_string(&data).unwrap().as_bytes())
.map_err(|_| FileSystemError::FileNotFound(id.clone(), "".to_string()))?;
Ok(())
}
fn delete(&self, id: &super::Id) -> Result<(), FileSystemError> {
let path = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let result = if path.is_dir() {
fs::remove_dir_all(path)
} else {
fs::remove_file(path)
};
// Remove from index if deletion was successful
if result.is_ok() {
self.remove_from_index(id);
}
result.map_err(|e| FileSystemError::FileNotFound(id.clone(), e.to_string()))
}
fn clone(&self, id: &super::Id, new_id: &super::Id) -> Result<(), FileSystemError> {
let src = self.find_path_by_id(id).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
})?;
let parent = src.parent().map(Path::to_path_buf).ok_or_else(|| {
FileSystemError::FileNotFound(id.clone(), "No parent found!".to_string())
})?;
let dst = parent.join(new_id.to_string());
let result = if src.is_dir() {
// Simple recursive dir copy
fn copy_dir(src: &Path, dst: &Path) -> std::io::Result<()> {
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
let sp = entry.path();
let dp = dst.join(entry.file_name());
if ty.is_dir() {
copy_dir(&sp, &dp)?;
} else if ty.is_file() {
fs::copy(&sp, &dp)?;
}
}
Ok(())
} }
copy_dir(&src, &dst) Err(e) => Err(FsError::Io(e)),
} else {
fs::copy(&src, &dst).map(|_| ())
};
// Add cloned file/directory to index if copy was successful
if result.is_ok() {
self.add_to_index(new_id.clone(), dst);
} }
result.map_err(|e| FileSystemError::FileNotFound(id.clone(), e.to_string()))
} }
fn exists(&self, id: &super::Id) -> bool { fn mkdir(&self, path: &Path) -> Result<(), FsError> {
self.find_path_by_id(id).is_some() let full_path = self.full_path(path);
fs::create_dir_all(full_path).map_err(FsError::Io)
} }
fn lsdir(&self, id: &super::Id) -> Result<Vec<String>, FileSystemError> { fn lsfiles(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
let path = match self.find_path_by_id(id) { let full_path = self.full_path(path);
Some(p) if p.is_dir() => p, let paths = fs::read_dir(full_path)?
Some(p) => p.parent().map(Path::to_path_buf).ok_or_else(|| { .filter_map(|res| res.ok())
FileSystemError::DirectoryNotFound(PathBuf::from(id.to_string()), "".to_string()) .filter(|entry| entry.file_type().unwrap().is_file())
})?, .map(|entry| entry.path())
None => { .collect();
return Err(FileSystemError::DirectoryNotFound(
PathBuf::from(id.to_string()),
id.to_string(),
));
}
};
let mut entries = Vec::new(); Ok(paths)
for entry in fs::read_dir(&path) }
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?
{ fn lsdirs(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
let entry = entry let full_path = self.full_path(path);
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?; let paths = fs::read_dir(full_path)?
if let Some(name) = entry.file_name().to_str() { .filter_map(|res| res.ok())
entries.push(name.to_string()); .filter(|entry| entry.file_type().unwrap().is_dir())
.map(|entry| entry.path())
.collect();
Ok(paths)
}
fn exists(&self, path: &Path) -> bool {
let full_path = self.full_path(path);
full_path.exists()
}
fn rename(&self, path: &Path, other: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
let full_other = self.full_path(other);
fs::rename(full_path, full_other).map_err(FsError::Io)
}
fn config_path(&self) -> PathBuf {
match std::env::var("HOME") {
Ok(path) => PathBuf::from(path + "/.config/worldcoder/settings.json"),
Err(_) => {
eprintln!(
"XDG_CONFIG_HOME not set, using default path of ~/.config/worldcoder/settings.json"
);
"~/.config/worldcoder/settings.json".into()
} }
} }
Ok(entries)
}
fn file_tree(&self, root_path: &Path) -> Result<super::FileTree, FileSystemError> {
self.build_file_tree(root_path)
} }
} }
View File
+1
View File
@@ -440,6 +440,7 @@ pub struct AIInput {
pub system_prompt: String, pub system_prompt: String,
pub user_prompt: String, pub user_prompt: String,
pub previous_content: String, pub previous_content: String,
#[allow(unused)]
pub structure: Option<String>, pub structure: Option<String>,
} }
+3 -9
View File
@@ -10,17 +10,16 @@ mod explorer;
#[cfg(feature = "llm")] #[cfg(feature = "llm")]
mod llm_integration; mod llm_integration;
mod filesystem;
mod util; mod util;
mod filesystem;
use crate::{ use crate::{
editors::{ editors::{
asset_editor::Asset, content_editor, note_editor, object_editor::ObjectInstance, asset_editor::Asset, content_editor, note_editor, object_editor::ObjectInstance,
settings_editor::ProjectSettings, tags::Tag, template_editor::Template, settings_editor::ProjectSettings, tags::Tag, template_editor::Template,
}, },
explorer::Explorer, explorer::Explorer,
filesystem::{FileSystem, native::NativeFileSystem},
}; };
static VERSION: &str = "0.1.0"; static VERSION: &str = "0.1.0";
@@ -30,9 +29,6 @@ static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
path path
}); });
pub static FILESYSTEM: LazyLock<NativeFileSystem> =
LazyLock::new(|| NativeFileSystem::new(&*PROJECT_FOLDER));
fn main() { fn main() {
let app = Interface::new(); let app = Interface::new();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
@@ -52,7 +48,6 @@ pub struct Interface {
editor: content_editor::MainEditor, editor: content_editor::MainEditor,
explorer: Explorer, explorer: Explorer,
project: ProjectSettings, project: ProjectSettings,
filesystem: NativeFileSystem,
} }
impl eframe::App for Interface { impl eframe::App for Interface {
@@ -98,7 +93,6 @@ impl Interface {
editor: content_editor::MainEditor::new(), editor: content_editor::MainEditor::new(),
explorer: Explorer::new(), explorer: Explorer::new(),
project: ProjectSettings::load(), project: ProjectSettings::load(),
filesystem: NativeFileSystem::new(&*PROJECT_FOLDER),
} }
} }
@@ -212,7 +206,7 @@ impl Interface {
// render main content area // render main content area
fn render_main_content(&mut self, ctx: &egui::Context) { fn render_main_content(&mut self, ctx: &egui::Context) {
self.editor.ui(ctx, &mut self.project, &self.filesystem); self.editor.ui(ctx, &mut self.project);
} }
// configure appearance of UI elements // configure appearance of UI elements
View File
+1 -3
View File
@@ -3,8 +3,6 @@ use egui::{
scroll_area::{ScrollBarVisibility, ScrollSource}, scroll_area::{ScrollBarVisibility, ScrollSource},
}; };
use crate::filesystem::Id;
pub struct Error { pub struct Error {
message: String, message: String,
visible: bool, visible: bool,
@@ -28,7 +26,7 @@ impl Error {
} }
} }
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &Id, name: &str) { pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &str, name: &str) {
ui.group(|ui| { ui.group(|ui| {
ui.set_max_width(ui.available_width()); ui.set_max_width(ui.available_width());
-6
View File
@@ -1,6 +0,0 @@
{
"llm_api_uri": "http://localhost:1234",
"llm_api_key": "",
"ai_enabled": true,
"dark_theme": true
}