Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc7cf6b8ed | |||
| 7272d19207 | |||
| 9614d2884b |
Vendored
+2
-2
@@ -6,8 +6,8 @@
|
||||
"files.trimFinalNewlines": true,
|
||||
"files.trimTrailingWhitespace": true,
|
||||
"rust-analyzer.cargo.features": [
|
||||
"llm",
|
||||
"native"
|
||||
"native",
|
||||
"llm"
|
||||
],
|
||||
"rust-analyzer.cargo.noDefaultFeatures": true,
|
||||
"rust-analyzer.cargo.allFeatures": false
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
use egui::{TextEdit, vec2};
|
||||
|
||||
use crate::{PROJECT_FOLDER, filesystem::Id, util};
|
||||
use crate::{
|
||||
PROJECT_FOLDER,
|
||||
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
|
||||
util,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Asset {
|
||||
@@ -25,15 +29,14 @@ impl Asset {
|
||||
println!("old_path: {old_path:?}");
|
||||
println!("new_path: {new_path:?}");
|
||||
|
||||
// move from src dir to name path
|
||||
if let Err(err) = std::fs::rename(&old_path, &new_path) {
|
||||
if let Err(FsError::Io(err)) = FILESYSTEM.rename(&old_path, &new_path) {
|
||||
match err.kind() {
|
||||
std::io::ErrorKind::NotFound => {
|
||||
let dir = new_path.parent().unwrap();
|
||||
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}"),
|
||||
}
|
||||
@@ -48,7 +51,7 @@ impl Asset {
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::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)
|
||||
|| ui.button("Save").clicked()
|
||||
@@ -73,7 +76,7 @@ impl Asset {
|
||||
|
||||
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 {
|
||||
uri: std::borrow::Cow::Owned(self.name.clone()),
|
||||
bytes: bytes.into(),
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use std::path::Path;
|
||||
|
||||
use egui::TextEdit;
|
||||
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
|
||||
use serde::{self, Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
FILESYSTEM, PROJECT_FOLDER,
|
||||
editors::{settings_editor::ProjectSettings, tags::Tag},
|
||||
filesystem::{FileSystem, Id},
|
||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
||||
util,
|
||||
};
|
||||
|
||||
@@ -44,23 +45,21 @@ impl Clone for MainEditor {
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct ContentSection {
|
||||
#[serde(default)]
|
||||
pub title: String,
|
||||
|
||||
pub id: Id,
|
||||
pub id: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<Id>,
|
||||
pub tags: Vec<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub content: String,
|
||||
|
||||
// parent id
|
||||
#[serde(default)]
|
||||
pub parent: Option<Id>,
|
||||
pub parent: Option<String>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
@@ -70,7 +69,7 @@ impl ContentSection {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
title: String::new(),
|
||||
id: Id::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
description: String::new(),
|
||||
tags: Vec::new(),
|
||||
content: String::new(),
|
||||
@@ -79,29 +78,19 @@ impl ContentSection {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save<F: FileSystem>(
|
||||
&mut self,
|
||||
filesystem: &F,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
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
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
FILESYSTEM.write(
|
||||
Path::new(&format!("documents/{id}.json", id = &self.id)),
|
||||
self.clone(),
|
||||
)?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load<F: FileSystem>(
|
||||
filesystem: &F,
|
||||
id: &Id,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut section: Self = filesystem.read(id)?;
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut section: Self = FILESYSTEM.read(Path::new(&format!("documents/{id}.json")))?;
|
||||
section.saved = true;
|
||||
section.id = id.clone();
|
||||
section.id = id.to_string();
|
||||
Ok(section)
|
||||
}
|
||||
|
||||
@@ -144,16 +133,11 @@ impl MainEditor {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_ui<F: FileSystem>(
|
||||
&mut self,
|
||||
project: &mut ProjectSettings,
|
||||
filesystem: &F,
|
||||
ui: &mut egui::Ui,
|
||||
) {
|
||||
pub fn render_ui(&mut self, project: &mut ProjectSettings, ui: &mut egui::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(filesystem) {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
@@ -170,7 +154,7 @@ impl MainEditor {
|
||||
ui.horizontal(|ui| {
|
||||
// save button
|
||||
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}");
|
||||
}
|
||||
}
|
||||
@@ -178,23 +162,26 @@ impl MainEditor {
|
||||
// create copy button
|
||||
if ui.button("Create Copy").clicked() {
|
||||
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);
|
||||
|
||||
FILESYSTEM.clone(&self.content.id, ©.content.id);
|
||||
// TODO: Fix save call to pass filesystem
|
||||
// copy.content.save().unwrap();
|
||||
copy.content.save().unwrap();
|
||||
}
|
||||
|
||||
// delete button
|
||||
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();
|
||||
}
|
||||
|
||||
// revert changes button
|
||||
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
|
||||
@@ -202,7 +189,9 @@ impl MainEditor {
|
||||
|
||||
// assistant toggle
|
||||
#[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
|
||||
ui.checkbox(&mut self.editor_separate_window, "Pop out editor");
|
||||
@@ -250,7 +239,7 @@ impl MainEditor {
|
||||
ui.separator();
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
if self.show_ai {
|
||||
if self.show_ai && project.ai_enabled() {
|
||||
let dialog = &mut self.dialog;
|
||||
|
||||
dialog.content = self.content.content.clone();
|
||||
@@ -274,12 +263,7 @@ impl MainEditor {
|
||||
self.editor_ui(ui, project);
|
||||
}
|
||||
|
||||
pub fn ui<F: FileSystem>(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
project: &mut ProjectSettings,
|
||||
filesystem: &F,
|
||||
) {
|
||||
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectSettings) {
|
||||
// Show the editor window if enabled
|
||||
let mut show = self.show_editor;
|
||||
if show {
|
||||
@@ -290,11 +274,11 @@ impl MainEditor {
|
||||
.default_height(800.0)
|
||||
.open(&mut show)
|
||||
.show(ctx, |ui| {
|
||||
self.render_ui(project, filesystem, ui);
|
||||
self.render_ui(project, ui);
|
||||
});
|
||||
} else {
|
||||
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()
|
||||
.auto_shrink([false, false])
|
||||
.id_salt("editor_scroll")
|
||||
@@ -354,7 +338,7 @@ impl MainEditor {
|
||||
|
||||
ui.set_min_width(max_width as f32);
|
||||
|
||||
ui.add(
|
||||
let response = ui.add(
|
||||
TextEdit::multiline(&mut self.content.content)
|
||||
.id_source("MainEditor_editor")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
@@ -364,6 +348,9 @@ impl MainEditor {
|
||||
.hint_text("Type here...")
|
||||
.desired_width(max_width as f32),
|
||||
);
|
||||
if response.changed() {
|
||||
self.content.saved = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+42
-20
@@ -1,36 +1,40 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use egui::TextEdit;
|
||||
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
FILESYSTEM, PROJECT_FOLDER,
|
||||
editors::tags::Tag,
|
||||
filesystem::{FileSystem, Id},
|
||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
||||
util,
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Note {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub content: String,
|
||||
#[serde(default)]
|
||||
pub subject: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<Id>,
|
||||
|
||||
pub id: Id,
|
||||
pub tags: Vec<String>,
|
||||
|
||||
#[serde(skip)]
|
||||
#[serde(default = "default_saved")]
|
||||
pub saved: bool,
|
||||
}
|
||||
|
||||
pub fn default_saved() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl Default for Note {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Id::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
name: "New Note".to_string(),
|
||||
subject: "".to_string(),
|
||||
content: "".to_string(),
|
||||
@@ -43,7 +47,7 @@ impl Default for Note {
|
||||
impl Note {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
id: Id::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
name: "New Note".to_string(),
|
||||
subject: "".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 data = serde_json::to_string(&self)?;
|
||||
FILESYSTEM.write(id, data).unwrap();
|
||||
|
||||
FILESYSTEM.write(Path::new(&format!("notes/{id}.json")), self.clone())?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(id: &Id) -> std::io::Result<Self> {
|
||||
let mut note: Note = FILESYSTEM.read(id).unwrap();
|
||||
note.id = id.clone();
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut note: Self = FILESYSTEM.read(Path::new(&format!("notes/{id}.json")))?;
|
||||
note.id = id.to_string();
|
||||
note.saved = true;
|
||||
Ok(note)
|
||||
}
|
||||
@@ -77,11 +79,31 @@ impl Note {
|
||||
|
||||
util::saved_status(ui, self.saved, &self.id, &self.name);
|
||||
|
||||
if ui.button("Save").clicked() {
|
||||
if let Err(e) = self.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Save").clicked() {
|
||||
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");
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
|
||||
|
||||
@@ -4,27 +4,29 @@ use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{
|
||||
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
|
||||
PROJECT_FOLDER, RightPanelContent,
|
||||
editors::{
|
||||
tags::Tag,
|
||||
template_editor::{FieldValue, Template},
|
||||
},
|
||||
filesystem::{FileSystem, Id},
|
||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
||||
util,
|
||||
};
|
||||
|
||||
pub type ObjectId = String;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ObjectInstance {
|
||||
// template info
|
||||
pub id: Id,
|
||||
pub template_id: Id,
|
||||
pub id: ObjectId,
|
||||
pub template_id: String,
|
||||
|
||||
// instance info
|
||||
pub name: String,
|
||||
pub fields: std::collections::HashMap<String, FieldValue>,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<Id>,
|
||||
pub tags: Vec<String>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
@@ -50,8 +52,8 @@ impl Clone for ObjectInstance {
|
||||
impl Default for ObjectInstance {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Id::new(),
|
||||
template_id: Id::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
template_id: "new_template_instance".to_string(),
|
||||
name: "new_object".to_string(),
|
||||
fields: std::collections::HashMap::new(),
|
||||
tags: Vec::new(),
|
||||
@@ -69,25 +71,26 @@ impl ObjectInstance {
|
||||
fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type));
|
||||
}
|
||||
|
||||
let instance = Self {
|
||||
fields,
|
||||
Self {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
template_id: template.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let _ = FILESYSTEM.create(Path::new("./objects"), instance.clone());
|
||||
|
||||
instance
|
||||
name: "new_object".to_string(),
|
||||
fields,
|
||||
tags: Vec::new(),
|
||||
saved: false,
|
||||
dialog: None,
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut instance: ObjectInstance = FILESYSTEM.read(id)?;
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut instance: Self = FILESYSTEM.read(Path::new(&format!("objects/{id}.json")))?;
|
||||
instance.saved = true;
|
||||
Ok(instance)
|
||||
}
|
||||
@@ -119,14 +122,21 @@ impl ObjectInstance {
|
||||
}
|
||||
|
||||
if ui.button("Create Copy").clicked() {
|
||||
let new_id = Id::new();
|
||||
FILESYSTEM.clone(&self.id, &new_id).unwrap();
|
||||
let copy = Self::load(&new_id).unwrap();
|
||||
let mut copy = self.clone();
|
||||
copy.id = uuid::Uuid::new_v4().to_string();
|
||||
copy.dialog = None;
|
||||
copy.name = format!("{} (Copy)", self.name);
|
||||
copy.save().unwrap();
|
||||
|
||||
*right_panel = Some(RightPanelContent::Object(Box::new(copy)));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
@@ -257,7 +267,7 @@ impl ObjectInstance {
|
||||
if !value.is_empty() {
|
||||
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 {
|
||||
uri: std::borrow::Cow::Owned(path.to_str().unwrap().to_string()),
|
||||
bytes: bytes.into(),
|
||||
@@ -282,12 +292,12 @@ impl ObjectInstance {
|
||||
}
|
||||
|
||||
fn selector_ui(
|
||||
selected: &mut Id,
|
||||
selected: &mut ObjectId,
|
||||
objects: &mut [ObjectInstance],
|
||||
ui: &mut egui::Ui,
|
||||
saved: &mut bool,
|
||||
) {
|
||||
if !selected.to_string().is_empty() {
|
||||
if !selected.is_empty() {
|
||||
if let Ok(object) = ObjectInstance::load(selected) {
|
||||
ui.strong(&object.name);
|
||||
}
|
||||
@@ -323,7 +333,7 @@ impl ObjectInstance {
|
||||
}
|
||||
|
||||
if ui.button("Remove").clicked() {
|
||||
*selected = Id::default();
|
||||
*selected = String::new();
|
||||
*saved = false;
|
||||
}
|
||||
});
|
||||
|
||||
+197
-128
@@ -2,9 +2,12 @@ use chrono::NaiveDate;
|
||||
use egui::TextEdit;
|
||||
use egui_extras::DatePickerButton;
|
||||
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)]
|
||||
pub struct ProjectSettings {
|
||||
@@ -14,6 +17,7 @@ pub struct ProjectSettings {
|
||||
project_description: String,
|
||||
|
||||
// AI settings
|
||||
#[cfg(feature = "llm")]
|
||||
pub ai_context: String,
|
||||
|
||||
// settings
|
||||
@@ -29,17 +33,6 @@ pub struct ProjectSettings {
|
||||
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 {
|
||||
#[allow(dead_code)]
|
||||
pub fn new() -> Self {
|
||||
@@ -47,25 +40,10 @@ impl ProjectSettings {
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let project_path = PROJECT_FOLDER.join("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) {
|
||||
if let Ok(mut proj) = FILESYSTEM.read::<Self>(Path::new("project.json")) {
|
||||
proj.saved = true;
|
||||
|
||||
// load global settings
|
||||
proj.global_settings = EditorSettings::load_global();
|
||||
|
||||
// load local overrides
|
||||
proj.local_overrides = EditorSettings::load();
|
||||
|
||||
proj
|
||||
} else {
|
||||
Self::default()
|
||||
@@ -73,9 +51,9 @@ impl ProjectSettings {
|
||||
}
|
||||
|
||||
pub fn save(&mut self) {
|
||||
let project_path = PROJECT_FOLDER.join("project.json");
|
||||
let content = serde_json::to_string_pretty(self).unwrap();
|
||||
std::fs::write(project_path, content).unwrap();
|
||||
FILESYSTEM
|
||||
.write(Path::new("project.json"), self.clone())
|
||||
.unwrap();
|
||||
|
||||
self.global_settings.save();
|
||||
self.local_overrides.save();
|
||||
@@ -83,14 +61,104 @@ impl ProjectSettings {
|
||||
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) {
|
||||
// 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() {
|
||||
self.save();
|
||||
}
|
||||
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
self.save();
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
|
||||
// project settings
|
||||
@@ -99,80 +167,65 @@ impl ProjectSettings {
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Project Name");
|
||||
ui.text_edit_singleline(&mut self.project_name);
|
||||
|
||||
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();
|
||||
if Self::config_str(&mut self.project_name, "Project Name", ui) { self.saved = false };
|
||||
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.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.label("AI Context Prompt");
|
||||
ui.add(TextEdit::multiline(&mut self.ai_context)
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("What is this project about? what should the LLM know when generating content for this project?"));
|
||||
|
||||
ui.end_row();
|
||||
#[cfg(feature = "llm")]
|
||||
{
|
||||
ui.label("AI Context Prompt");
|
||||
if ui.add(TextEdit::multiline(&mut self.ai_context)
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.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();
|
||||
|
||||
// local settings overrides for editor
|
||||
|
||||
ui.heading("Local Overrides");
|
||||
egui::Grid::new("local overrides")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Enable AI");
|
||||
if let Some(ai_enabled) = &mut self.local_overrides.ai_enabled {
|
||||
ui.checkbox(ai_enabled, "Enable AI");
|
||||
if ui.button("Remove Override").clicked() {
|
||||
self.local_overrides.ai_enabled = None;
|
||||
}
|
||||
} else if ui.button("Override").clicked() {
|
||||
self.local_overrides.ai_enabled = Some(true);
|
||||
#[cfg(feature = "llm")]
|
||||
if ProjectSettings::config_str_override(
|
||||
"LLM API URI",
|
||||
&mut self.local_overrides.llm_api_uri,
|
||||
"http://localhost:1234",
|
||||
ui,
|
||||
) {
|
||||
self.saved = false;
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API URI");
|
||||
if let Some(llm_api_uri) = &mut self.local_overrides.llm_api_uri {
|
||||
ui.text_edit_singleline(llm_api_uri);
|
||||
if ui.button("Remove Override").clicked() {
|
||||
self.local_overrides.llm_api_uri = None;
|
||||
}
|
||||
} else if ui.button("Override").clicked() {
|
||||
self.local_overrides.llm_api_uri = Some("http://localhost:1234".to_string());
|
||||
#[cfg(feature = "llm")]
|
||||
if ProjectSettings::config_str_override(
|
||||
"LLM API Key",
|
||||
&mut self.local_overrides.llm_api_key,
|
||||
"1234",
|
||||
ui,
|
||||
) {
|
||||
self.saved = false;
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API Key");
|
||||
if let Some(llm_api_key) = &mut self.local_overrides.llm_api_key {
|
||||
ui.text_edit_singleline(llm_api_key);
|
||||
if ui.button("Remove Override").clicked() {
|
||||
self.local_overrides.llm_api_key = None;
|
||||
}
|
||||
} else if ui.button("Override").clicked() {
|
||||
self.local_overrides.llm_api_key = Some("1234".to_string());
|
||||
#[cfg(feature = "llm")]
|
||||
if ProjectSettings::config_bool_override(
|
||||
"Enable AI",
|
||||
&mut self.local_overrides.ai_enabled,
|
||||
true,
|
||||
ui,
|
||||
) {
|
||||
self.saved = false;
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
@@ -183,23 +236,37 @@ impl ProjectSettings {
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Enable AI");
|
||||
ui.checkbox(&mut self.global_settings.ai_enabled.unwrap(), "Enable AI");
|
||||
#[cfg(feature = "llm")]
|
||||
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");
|
||||
ui.text_edit_singleline(self.global_settings.llm_api_uri.as_mut().unwrap());
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API Key");
|
||||
ui.text_edit_singleline(self.global_settings.llm_api_key.as_mut().unwrap());
|
||||
|
||||
ui.end_row();
|
||||
#[cfg(feature = "llm")]
|
||||
if Self::config_str(
|
||||
self.global_settings.llm_api_key.as_mut().unwrap(),
|
||||
"LLM API Key",
|
||||
ui,
|
||||
) {
|
||||
self.saved = false
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
#[allow(unused)]
|
||||
pub fn ai_enabled(&mut self) -> bool {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
|
||||
@@ -236,8 +303,9 @@ impl Default for ProjectSettings {
|
||||
project_author: "Your Name".to_string(),
|
||||
project_description: "Description of your project".to_string(),
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
ai_context: "".to_string(),
|
||||
global_settings: EditorSettings::new(),
|
||||
global_settings: EditorSettings::default(),
|
||||
local_overrides: EditorSettings::new(),
|
||||
|
||||
// window state
|
||||
@@ -249,11 +317,15 @@ impl Default for ProjectSettings {
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
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>,
|
||||
|
||||
#[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)]
|
||||
is_global: bool,
|
||||
}
|
||||
@@ -261,8 +333,11 @@ pub struct EditorSettings {
|
||||
impl Default for EditorSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
#[cfg(feature = "llm")]
|
||||
llm_api_uri: Some("http://localhost:1234".to_string()),
|
||||
#[cfg(feature = "llm")]
|
||||
llm_api_key: Some("".to_string()),
|
||||
#[cfg(feature = "llm")]
|
||||
ai_enabled: Some(true),
|
||||
dark_theme: Some(true),
|
||||
|
||||
@@ -275,8 +350,11 @@ impl Default for EditorSettings {
|
||||
impl EditorSettings {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
#[cfg(feature = "llm")]
|
||||
llm_api_uri: None,
|
||||
#[cfg(feature = "llm")]
|
||||
llm_api_key: None,
|
||||
#[cfg(feature = "llm")]
|
||||
ai_enabled: None,
|
||||
dark_theme: None,
|
||||
|
||||
@@ -285,43 +363,34 @@ impl EditorSettings {
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let path = PROJECT_FOLDER.join("settings.json");
|
||||
let mut file = if let Ok(file) = std::fs::File::open(path) {
|
||||
file
|
||||
} else {
|
||||
return Self::default();
|
||||
};
|
||||
if let Ok(mut settings) = FILESYSTEM.read::<Self>(Path::new("settings.json")) {
|
||||
settings.is_global = false;
|
||||
return settings;
|
||||
}
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
serde_json::from_str(&contents).unwrap()
|
||||
Self::new()
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
let content = serde_json::to_string_pretty(self).unwrap();
|
||||
|
||||
let path = if self.is_global {
|
||||
PathBuf::from(GLOBAL_SETTINGS_PATH.clone())
|
||||
FILESYSTEM.config_path()
|
||||
} 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 {
|
||||
let path = PathBuf::from(GLOBAL_SETTINGS_PATH.clone());
|
||||
let path = FILESYSTEM.config_path();
|
||||
|
||||
if !path.exists() {
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
let content = serde_json::to_string_pretty(&Self::default()).unwrap();
|
||||
std::fs::write(&path, content).unwrap();
|
||||
|
||||
// return a default config
|
||||
return Self::default();
|
||||
FILESYSTEM.mkdir(path.parent().unwrap()).unwrap();
|
||||
FILESYSTEM.write(path.as_path(), Self::default()).unwrap();
|
||||
}
|
||||
|
||||
let content = std::fs::read_to_string(path).unwrap();
|
||||
serde_json::from_str(&content).unwrap()
|
||||
let mut settings = FILESYSTEM.read::<Self>(path.as_path()).unwrap();
|
||||
settings.is_global = true;
|
||||
settings
|
||||
}
|
||||
}
|
||||
|
||||
+7
-13
@@ -1,15 +1,11 @@
|
||||
use egui::{Response, RichText, TextEdit};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
FILESYSTEM, PROJECT_FOLDER,
|
||||
filesystem::{FileSystem, Id},
|
||||
util,
|
||||
};
|
||||
use crate::{PROJECT_FOLDER, util};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Tag {
|
||||
pub id: Id,
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub color: egui::Color32,
|
||||
@@ -24,7 +20,7 @@ pub struct Tag {
|
||||
impl Default for Tag {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Id::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
name: String::new(),
|
||||
description: String::new(),
|
||||
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
|
||||
tag_ids.sort();
|
||||
tag_ids.dedup();
|
||||
@@ -206,11 +202,9 @@ impl Tag {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut tag: Self = FILESYSTEM.read(id)?;
|
||||
tag.saved = true;
|
||||
tag.id = id.clone();
|
||||
Ok(tag)
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER.join("tags").join(format!("{id}.json"));
|
||||
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
@@ -2,11 +2,12 @@ use chrono::NaiveDate;
|
||||
use core::fmt;
|
||||
use egui::ScrollArea;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{
|
||||
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
|
||||
RightPanelContent,
|
||||
editors::object_editor::ObjectInstance,
|
||||
filesystem::{self, FileSystem, Id},
|
||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
||||
util::{self, Error},
|
||||
};
|
||||
|
||||
@@ -17,7 +18,7 @@ pub enum FieldType {
|
||||
MultiLine,
|
||||
Date,
|
||||
Number,
|
||||
Link { template_id: Option<Id> },
|
||||
Link { template_id: Option<String> },
|
||||
Links,
|
||||
}
|
||||
|
||||
@@ -48,8 +49,8 @@ pub enum FieldValue {
|
||||
MultiLine(String),
|
||||
Date(NaiveDate),
|
||||
Number(f64),
|
||||
Link(Id),
|
||||
Links(Vec<Id>),
|
||||
Link(String),
|
||||
Links(Vec<String>),
|
||||
}
|
||||
|
||||
impl FieldValue {
|
||||
@@ -60,7 +61,7 @@ impl FieldValue {
|
||||
FieldType::MultiLine => Self::MultiLine(String::new()),
|
||||
FieldType::Date => Self::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()),
|
||||
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 {
|
||||
template_id: Some(template_id),
|
||||
} => Self::Link(template_id.clone()),
|
||||
@@ -89,7 +90,7 @@ pub struct FieldDefinition {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Template {
|
||||
pub name: String,
|
||||
pub id: Id,
|
||||
pub id: String,
|
||||
|
||||
pub description: Option<String>,
|
||||
pub fields: Vec<FieldDefinition>,
|
||||
@@ -155,7 +156,7 @@ impl Default for Template {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "New Template".to_string(),
|
||||
id: Id::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
description: Some(String::from("Placeholder description")),
|
||||
fields: Vec::new(),
|
||||
saved: false,
|
||||
@@ -171,15 +172,15 @@ impl Default for Template {
|
||||
}
|
||||
|
||||
impl Template {
|
||||
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut template: Self = FILESYSTEM.read(id)?;
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let mut template = FILESYSTEM.read::<Self>(Path::new(&format!("templates/{id}.json")))?;
|
||||
template.saved = true;
|
||||
template.id = id.clone();
|
||||
Ok(template)
|
||||
}
|
||||
|
||||
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;
|
||||
Ok(())
|
||||
}
|
||||
@@ -197,16 +198,6 @@ impl Template {
|
||||
|
||||
ScrollArea::vertical().show(ui, |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);
|
||||
|
||||
// Save/Cancel buttons
|
||||
@@ -219,19 +210,16 @@ impl Template {
|
||||
|
||||
if ui.button("Create Copy").clicked() {
|
||||
let mut copy = self.clone();
|
||||
copy.id = Id::new();
|
||||
copy.id = uuid::Uuid::new_v4().to_string();
|
||||
copy.name = format!("{} (Copy)", self.name);
|
||||
copy.save().unwrap();
|
||||
}
|
||||
|
||||
if ui.button("Delete").clicked() {
|
||||
std::fs::remove_file(
|
||||
PROJECT_FOLDER
|
||||
.join("templates")
|
||||
.join(format!("{}.json", self.id)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let id = &self.id;
|
||||
FILESYSTEM
|
||||
.delete(Path::new(&format!("templates/{id}.json")))
|
||||
.unwrap();
|
||||
*new_instance = Some(RightPanelContent::None);
|
||||
}
|
||||
|
||||
|
||||
+63
-115
@@ -1,7 +1,4 @@
|
||||
use itertools::Itertools;
|
||||
use std::fs::{self, DirEntry};
|
||||
|
||||
// use walkdir::{DirEntry, WalkDir};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{
|
||||
PROJECT_FOLDER, RightPanelContent,
|
||||
@@ -10,7 +7,7 @@ use crate::{
|
||||
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
|
||||
tags::Tag, template_editor::Template,
|
||||
},
|
||||
filesystem::Id,
|
||||
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
|
||||
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(
|
||||
ui: &mut egui::Ui,
|
||||
documents: &[ContentSection],
|
||||
@@ -183,7 +172,7 @@ impl Explorer {
|
||||
// Filter documents that have the current parent (or no parent if this is the root)
|
||||
let child_docs: Vec<&ContentSection> = documents
|
||||
.iter()
|
||||
.filter(|doc| doc.parent.as_ref().map(|id| id.as_str()) == parent_id)
|
||||
.filter(|doc| doc.parent.as_deref() == parent_id)
|
||||
.collect();
|
||||
|
||||
for doc in child_docs {
|
||||
@@ -208,7 +197,7 @@ impl Explorer {
|
||||
})
|
||||
.body(|ui| {
|
||||
// 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>) {
|
||||
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(
|
||||
ui.ctx(),
|
||||
ui.make_persistent_id("assets"),
|
||||
true,
|
||||
ui.make_persistent_id(&file_name),
|
||||
false,
|
||||
)
|
||||
.show_header(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Assets");
|
||||
ui.label(file_name);
|
||||
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
|
||||
});
|
||||
})
|
||||
.body(|ui| {
|
||||
let entries = fs::read_dir(PROJECT_FOLDER.join("assets"))
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.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<_>>();
|
||||
// recursive call to render the next level of documents
|
||||
for dir in dirs.iter() {
|
||||
Self::render_asset_dir(ui, to_load, dir);
|
||||
}
|
||||
|
||||
for entry in entries {
|
||||
Self::render_entry(ui, to_load, &entry);
|
||||
for file in files.iter() {
|
||||
Self::render_asset(ui, to_load, file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) {
|
||||
let file_type = entry.file_type().unwrap();
|
||||
let is_dir = file_type.is_dir();
|
||||
let file_name = entry.file_name().to_str().unwrap().to_string();
|
||||
let path = entry.path();
|
||||
fn render_asset(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
|
||||
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
|
||||
|
||||
if is_dir {
|
||||
let entries = fs::read_dir(path)
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(
|
||||
ui.ctx(),
|
||||
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)));
|
||||
}
|
||||
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
|
||||
fn load_templates(&mut self) -> std::io::Result<()> {
|
||||
let templates_folder = PROJECT_FOLDER.join("templates");
|
||||
if !templates_folder.exists() {
|
||||
std::fs::create_dir_all(&templates_folder)?;
|
||||
fn load_templates(&mut self) -> Result<(), FsError> {
|
||||
let path = Path::new("templates");
|
||||
|
||||
if !FILESYSTEM.exists(path) {
|
||||
FILESYSTEM.mkdir(path)?;
|
||||
}
|
||||
let mut templates = Vec::new();
|
||||
for entry in std::fs::read_dir(&templates_folder).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
match Template::load(&Id::from_path(&path)) {
|
||||
for entry in FILESYSTEM.lsfiles(path)? {
|
||||
match FILESYSTEM.read::<Template>(&entry) {
|
||||
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;
|
||||
@@ -339,17 +297,16 @@ impl Explorer {
|
||||
}
|
||||
|
||||
// load objects from the objects folder
|
||||
fn load_objects(&mut self) -> std::io::Result<()> {
|
||||
let objects_folder = PROJECT_FOLDER.join("objects");
|
||||
if !objects_folder.exists() {
|
||||
std::fs::create_dir_all(&objects_folder)?;
|
||||
fn load_objects(&mut self) -> Result<(), FsError> {
|
||||
let path = Path::new("objects");
|
||||
if !FILESYSTEM.exists(path) {
|
||||
FILESYSTEM.mkdir(path)?;
|
||||
}
|
||||
let mut objects = Vec::new();
|
||||
for entry in std::fs::read_dir(&objects_folder).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
match ObjectInstance::load(&Id::from_path(&path)) {
|
||||
for entry in FILESYSTEM.lsfiles(path)? {
|
||||
match FILESYSTEM.read::<ObjectInstance>(&entry) {
|
||||
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;
|
||||
@@ -358,16 +315,15 @@ impl Explorer {
|
||||
}
|
||||
|
||||
// load notes from the notes folder
|
||||
fn load_notes(&mut self) -> std::io::Result<()> {
|
||||
let notes_folder = PROJECT_FOLDER.join("notes");
|
||||
if !notes_folder.exists() {
|
||||
std::fs::create_dir_all(¬es_folder)?;
|
||||
fn load_notes(&mut self) -> Result<(), FsError> {
|
||||
let path = Path::new("notes");
|
||||
if !FILESYSTEM.exists(path) {
|
||||
FILESYSTEM.mkdir(path)?;
|
||||
}
|
||||
let mut notes = Vec::new();
|
||||
|
||||
for entry in std::fs::read_dir(¬es_folder).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
match Note::load(&Id::from_path(&path)) {
|
||||
for entry in FILESYSTEM.lsfiles(path)? {
|
||||
match FILESYSTEM.read::<Note>(&entry) {
|
||||
Ok(note) => notes.push(note),
|
||||
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
|
||||
}
|
||||
@@ -379,25 +335,17 @@ impl Explorer {
|
||||
}
|
||||
|
||||
// load documents from the documents folder
|
||||
fn load_documents(&mut self) -> std::io::Result<()> {
|
||||
let documents_folder = PROJECT_FOLDER.join("documents");
|
||||
if !documents_folder.exists() {
|
||||
std::fs::create_dir_all(&documents_folder)?;
|
||||
fn load_documents(&mut self) -> Result<(), FsError> {
|
||||
let path = Path::new("documents");
|
||||
if !FILESYSTEM.exists(path) {
|
||||
FILESYSTEM.mkdir(path)?;
|
||||
}
|
||||
let mut documents = Vec::new();
|
||||
|
||||
for entry in std::fs::read_dir(&documents_folder).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
// TODO: Update to use FileSystem API
|
||||
// For now, read files directly until we refactor the loading system
|
||||
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:?}");
|
||||
}
|
||||
for entry in FILESYSTEM.lsfiles(path)? {
|
||||
match FILESYSTEM.read::<ContentSection>(&entry) {
|
||||
Ok(document) => documents.push(MainEditor::open(document)),
|
||||
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -406,7 +354,7 @@ impl Explorer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_tags(&mut self) -> std::io::Result<()> {
|
||||
fn load_tags(&mut self) -> Result<(), FsError> {
|
||||
self.tags = Tag::load_all();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+73
-107
@@ -1,11 +1,15 @@
|
||||
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||
use std::{
|
||||
fmt,
|
||||
ops::{Deref, DerefMut},
|
||||
collections::HashMap,
|
||||
io,
|
||||
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")]
|
||||
pub mod native;
|
||||
@@ -13,125 +17,87 @@ pub mod native;
|
||||
#[cfg(feature = "web")]
|
||||
pub mod web;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct Id(String);
|
||||
pub static FILESYSTEM: LazyLock<NativeFileSystem> = LazyLock::new(|| {
|
||||
#[cfg(feature = "native")]
|
||||
return NativeFileSystem::new(PROJECT_FOLDER.clone());
|
||||
#[cfg(feature = "web")]
|
||||
return Box::new(web::WebFileSystem::new());
|
||||
});
|
||||
|
||||
impl Id {
|
||||
pub fn new() -> Self {
|
||||
Self(uuid::Uuid::new_v4().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for Id {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
pub trait LegacyFileSystem {
|
||||
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError>;
|
||||
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>;
|
||||
fn lsdirs(&self, path: &Path) -> Result<Vec<PathBuf>, FsError>;
|
||||
fn mkdir(&self, path: &Path) -> Result<(), FsError>;
|
||||
fn rename(&self, path: &Path, new_path: &Path) -> Result<(), FsError>;
|
||||
#[allow(unused)]
|
||||
fn exists(&self, path: &Path) -> bool;
|
||||
fn config_path(&self) -> PathBuf;
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────
|
||||
// Custom error type
|
||||
// ────────────────────────────────────────────────────────────────
|
||||
#[derive(Debug)]
|
||||
pub enum FileSystemError {
|
||||
FileNotFound(Id, String),
|
||||
DirectoryNotFound(PathBuf, String),
|
||||
pub enum FsError {
|
||||
Io(io::Error),
|
||||
Serde(serde_json::Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for FileSystemError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
impl std::fmt::Display for FsError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
FileSystemError::FileNotFound(id, message) => {
|
||||
write!(f, "File not found: {} - {}", id, message)
|
||||
}
|
||||
FileSystemError::DirectoryNotFound(id, message) => {
|
||||
write!(f, "Directory not found: {} - {}", id.display(), message)
|
||||
}
|
||||
FsError::Io(e) => write!(f, "IO error: {e}"),
|
||||
FsError::Serde(e) => write!(f, "Serialization error: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for FileSystemError {}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileTree {
|
||||
pub name: String,
|
||||
pub path: PathBuf,
|
||||
pub is_directory: bool,
|
||||
pub id: Option<Id>,
|
||||
pub children: Vec<FileTree>,
|
||||
impl std::error::Error for FsError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
FsError::Io(e) => Some(e),
|
||||
FsError::Serde(e) => Some(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
fn new(root: impl AsRef<Path>) -> Self;
|
||||
|
||||
fn create(&self, directory: &Path, data: impl Serialize) -> Result<Id, FileSystemError>;
|
||||
|
||||
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>;
|
||||
fn load<T: DeserializeOwned>(&self, id: Id) -> Result<T, FsError>;
|
||||
fn save<T: Serialize>(&self, id: Id, data: T) -> Result<(), FsError>;
|
||||
fn mkdir(&self, path: Path) -> Result<(), FsError>;
|
||||
fn exists(&self, path: Path) -> bool;
|
||||
}
|
||||
|
||||
pub struct FileBorrow<T: DeserializeOwned + Serialize> {
|
||||
pub id: Id,
|
||||
pub file: T,
|
||||
#[allow(dead_code)]
|
||||
pub struct Index {
|
||||
file_cache: HashMap<Id, PathBuf>,
|
||||
project_root: Directory,
|
||||
}
|
||||
|
||||
impl<T: DeserializeOwned + Serialize> Deref for FileBorrow<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.file
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub struct Directory {
|
||||
name: String,
|
||||
id: Id,
|
||||
children: HashMap<Id, Directory>,
|
||||
files: Vec<Id>,
|
||||
}
|
||||
|
||||
+83
-294
@@ -1,327 +1,116 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Read, Write};
|
||||
use std::fs;
|
||||
use std::io::{ErrorKind, Read};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use image::EncodableLayout;
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::to_string;
|
||||
|
||||
use crate::filesystem::FileSystemError;
|
||||
|
||||
use super::FileSystem;
|
||||
use crate::filesystem::{FsError, LegacyFileSystem};
|
||||
|
||||
pub struct NativeFileSystem {
|
||||
root: PathBuf,
|
||||
index: Arc<RwLock<HashMap<super::Id, PathBuf>>>,
|
||||
project_root: PathBuf,
|
||||
}
|
||||
|
||||
impl NativeFileSystem {
|
||||
/// Rebuild the entire index by scanning the filesystem
|
||||
fn rebuild_index(&self) -> Result<(), std::io::Error> {
|
||||
let mut index = HashMap::new();
|
||||
Self::scan_directory(&self.root, &mut index)?;
|
||||
|
||||
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);
|
||||
/// Create a new instance.
|
||||
pub fn new(root: impl Into<PathBuf>) -> Self {
|
||||
Self {
|
||||
project_root: root.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove an entry from the index
|
||||
fn remove_from_index(&self, id: &super::Id) {
|
||||
if let Ok(mut index) = self.index.write() {
|
||||
index.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
})
|
||||
/// Resolve the user supplied *relative* path against the project root.
|
||||
#[inline]
|
||||
fn full_path(&self, path: &Path) -> PathBuf {
|
||||
self.project_root.join(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl FileSystem for NativeFileSystem {
|
||||
fn new(root: impl AsRef<Path>) -> Self {
|
||||
let fs = Self {
|
||||
root: root.as_ref().to_path_buf(),
|
||||
index: Arc::new(RwLock::new(HashMap::new())),
|
||||
};
|
||||
fs.rebuild_index().unwrap_or_else(|e| {
|
||||
eprintln!("Warning: Failed to build initial index: {e}");
|
||||
});
|
||||
fs
|
||||
impl LegacyFileSystem for NativeFileSystem {
|
||||
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> {
|
||||
let full_path = self.full_path(path);
|
||||
let file = fs::File::open(full_path).map_err(FsError::Io)?;
|
||||
serde_json::from_reader(file).map_err(FsError::Serde)
|
||||
}
|
||||
|
||||
fn create(&self, directory: &Path, data: impl Serialize) -> Result<super::Id, FileSystemError> {
|
||||
let dir = if directory.is_absolute() {
|
||||
directory.to_path_buf()
|
||||
} else {
|
||||
self.root.join(directory)
|
||||
};
|
||||
fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError> {
|
||||
let full_path = self.full_path(path);
|
||||
let mut contents = Vec::new();
|
||||
fs::File::open(full_path)?.read_to_end(&mut contents)?;
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
if !dir.exists() {
|
||||
fs::create_dir_all(&dir).map_err(|e| {
|
||||
FileSystemError::DirectoryNotFound(dir.to_path_buf(), e.to_string())
|
||||
})?;
|
||||
fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError> {
|
||||
let full_path = self.full_path(path);
|
||||
|
||||
// Ensure the parent directory exists.
|
||||
if let Some(parent) = full_path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
if !dir.is_dir() {
|
||||
return Err(FileSystemError::DirectoryNotFound(
|
||||
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)
|
||||
let file = fs::File::create(full_path)?;
|
||||
serde_json::to_writer(file, &data).map_err(FsError::Serde)
|
||||
}
|
||||
|
||||
fn read<T: DeserializeOwned>(&self, id: &super::Id) -> Result<T, FileSystemError> {
|
||||
let path = self.find_path_by_id(id).ok_or_else(|| {
|
||||
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
|
||||
})?;
|
||||
|
||||
let val = serde_json::from_reader(
|
||||
File::open(path)
|
||||
.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(())
|
||||
fn delete(&self, path: &Path) -> Result<(), FsError> {
|
||||
let full_path = self.full_path(path);
|
||||
match fs::remove_file(&full_path) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) if e.kind() == ErrorKind::IsADirectory => {
|
||||
// Remove a directory tree.
|
||||
fs::remove_dir_all(full_path).map_err(FsError::Io)
|
||||
}
|
||||
copy_dir(&src, &dst)
|
||||
} 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);
|
||||
Err(e) => Err(FsError::Io(e)),
|
||||
}
|
||||
|
||||
result.map_err(|e| FileSystemError::FileNotFound(id.clone(), e.to_string()))
|
||||
}
|
||||
|
||||
fn exists(&self, id: &super::Id) -> bool {
|
||||
self.find_path_by_id(id).is_some()
|
||||
fn mkdir(&self, path: &Path) -> Result<(), FsError> {
|
||||
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> {
|
||||
let path = match self.find_path_by_id(id) {
|
||||
Some(p) if p.is_dir() => p,
|
||||
Some(p) => p.parent().map(Path::to_path_buf).ok_or_else(|| {
|
||||
FileSystemError::DirectoryNotFound(PathBuf::from(id.to_string()), "".to_string())
|
||||
})?,
|
||||
None => {
|
||||
return Err(FileSystemError::DirectoryNotFound(
|
||||
PathBuf::from(id.to_string()),
|
||||
id.to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
fn lsfiles(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
|
||||
let full_path = self.full_path(path);
|
||||
let paths = fs::read_dir(full_path)?
|
||||
.filter_map(|res| res.ok())
|
||||
.filter(|entry| entry.file_type().unwrap().is_file())
|
||||
.map(|entry| entry.path())
|
||||
.collect();
|
||||
|
||||
let mut entries = Vec::new();
|
||||
for entry in fs::read_dir(&path)
|
||||
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?
|
||||
{
|
||||
let entry = entry
|
||||
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?;
|
||||
if let Some(name) = entry.file_name().to_str() {
|
||||
entries.push(name.to_string());
|
||||
Ok(paths)
|
||||
}
|
||||
|
||||
fn lsdirs(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
|
||||
let full_path = self.full_path(path);
|
||||
let paths = fs::read_dir(full_path)?
|
||||
.filter_map(|res| res.ok())
|
||||
.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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -440,6 +440,7 @@ pub struct AIInput {
|
||||
pub system_prompt: String,
|
||||
pub user_prompt: String,
|
||||
pub previous_content: String,
|
||||
#[allow(unused)]
|
||||
pub structure: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
+3
-9
@@ -10,17 +10,16 @@ mod explorer;
|
||||
#[cfg(feature = "llm")]
|
||||
mod llm_integration;
|
||||
|
||||
mod filesystem;
|
||||
|
||||
mod util;
|
||||
|
||||
mod filesystem;
|
||||
|
||||
use crate::{
|
||||
editors::{
|
||||
asset_editor::Asset, content_editor, note_editor, object_editor::ObjectInstance,
|
||||
settings_editor::ProjectSettings, tags::Tag, template_editor::Template,
|
||||
},
|
||||
explorer::Explorer,
|
||||
filesystem::{FileSystem, native::NativeFileSystem},
|
||||
};
|
||||
|
||||
static VERSION: &str = "0.1.0";
|
||||
@@ -30,9 +29,6 @@ static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
path
|
||||
});
|
||||
|
||||
pub static FILESYSTEM: LazyLock<NativeFileSystem> =
|
||||
LazyLock::new(|| NativeFileSystem::new(&*PROJECT_FOLDER));
|
||||
|
||||
fn main() {
|
||||
let app = Interface::new();
|
||||
let options = eframe::NativeOptions {
|
||||
@@ -52,7 +48,6 @@ pub struct Interface {
|
||||
editor: content_editor::MainEditor,
|
||||
explorer: Explorer,
|
||||
project: ProjectSettings,
|
||||
filesystem: NativeFileSystem,
|
||||
}
|
||||
|
||||
impl eframe::App for Interface {
|
||||
@@ -98,7 +93,6 @@ impl Interface {
|
||||
editor: content_editor::MainEditor::new(),
|
||||
explorer: Explorer::new(),
|
||||
project: ProjectSettings::load(),
|
||||
filesystem: NativeFileSystem::new(&*PROJECT_FOLDER),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +206,7 @@ impl Interface {
|
||||
|
||||
// render main content area
|
||||
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
|
||||
|
||||
+1
-3
@@ -3,8 +3,6 @@ use egui::{
|
||||
scroll_area::{ScrollBarVisibility, ScrollSource},
|
||||
};
|
||||
|
||||
use crate::filesystem::Id;
|
||||
|
||||
pub struct Error {
|
||||
message: String,
|
||||
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.set_max_width(ui.available_width());
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"llm_api_uri": "http://localhost:1234",
|
||||
"llm_api_key": "",
|
||||
"ai_enabled": true,
|
||||
"dark_theme": true
|
||||
}
|
||||
Reference in New Issue
Block a user