Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b051208f3 |
Vendored
+2
-2
@@ -6,8 +6,8 @@
|
|||||||
"files.trimFinalNewlines": true,
|
"files.trimFinalNewlines": true,
|
||||||
"files.trimTrailingWhitespace": true,
|
"files.trimTrailingWhitespace": true,
|
||||||
"rust-analyzer.cargo.features": [
|
"rust-analyzer.cargo.features": [
|
||||||
"native",
|
"llm",
|
||||||
"llm"
|
"native"
|
||||||
],
|
],
|
||||||
"rust-analyzer.cargo.noDefaultFeatures": true,
|
"rust-analyzer.cargo.noDefaultFeatures": true,
|
||||||
"rust-analyzer.cargo.allFeatures": false
|
"rust-analyzer.cargo.allFeatures": false
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
use egui::{TextEdit, vec2};
|
use egui::{TextEdit, vec2};
|
||||||
|
|
||||||
use crate::{
|
use crate::{PROJECT_FOLDER, filesystem::Id, util};
|
||||||
PROJECT_FOLDER,
|
|
||||||
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
|
|
||||||
util,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Asset {
|
pub struct Asset {
|
||||||
@@ -29,14 +25,15 @@ impl Asset {
|
|||||||
println!("old_path: {old_path:?}");
|
println!("old_path: {old_path:?}");
|
||||||
println!("new_path: {new_path:?}");
|
println!("new_path: {new_path:?}");
|
||||||
|
|
||||||
if let Err(FsError::Io(err)) = FILESYSTEM.rename(&old_path, &new_path) {
|
// move from src dir to name 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() {
|
||||||
FILESYSTEM.mkdir(dir).unwrap();
|
std::fs::create_dir_all(dir).unwrap();
|
||||||
}
|
}
|
||||||
FILESYSTEM.rename(&old_path, &new_path).unwrap();
|
std::fs::rename(&old_path, &new_path).unwrap();
|
||||||
}
|
}
|
||||||
_ => panic!("Failed to rename file: {err}"),
|
_ => panic!("Failed to rename file: {err}"),
|
||||||
}
|
}
|
||||||
@@ -51,7 +48,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, &self.name, &self.new_name);
|
util::saved_status(ui, self.saved, &Id::new(), &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()
|
||||||
@@ -76,7 +73,7 @@ impl Asset {
|
|||||||
|
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
if let Ok(bytes) = FILESYSTEM.read_bytes(&Self::path(&self.name)) {
|
if let Ok(bytes) = std::fs::read(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(),
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
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, LegacyFileSystem},
|
filesystem::{FileSystem, Id},
|
||||||
util,
|
util,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,21 +44,23 @@ 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<String>,
|
pub tags: Vec<Id>,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub content: String,
|
pub content: String,
|
||||||
|
|
||||||
// parent id
|
// parent id
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub parent: Option<String>,
|
pub parent: Option<Id>,
|
||||||
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub saved: bool,
|
pub saved: bool,
|
||||||
@@ -69,7 +70,7 @@ impl ContentSection {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
title: String::new(),
|
title: String::new(),
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
id: Id::new(),
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
content: String::new(),
|
content: String::new(),
|
||||||
@@ -78,19 +79,29 @@ impl ContentSection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn save<F: FileSystem>(
|
||||||
FILESYSTEM.write(
|
&mut self,
|
||||||
Path::new(&format!("documents/{id}.json", id = &self.id)),
|
filesystem: &F,
|
||||||
self.clone(),
|
) -> 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
|
||||||
|
}
|
||||||
|
|
||||||
self.saved = true;
|
self.saved = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn load<F: FileSystem>(
|
||||||
let mut section: Self = FILESYSTEM.read(Path::new(&format!("documents/{id}.json")))?;
|
filesystem: &F,
|
||||||
|
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.to_string();
|
section.id = id.clone();
|
||||||
Ok(section)
|
Ok(section)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,11 +144,16 @@ impl MainEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_ui(&mut self, project: &mut ProjectSettings, ui: &mut egui::Ui) {
|
pub fn render_ui<F: FileSystem>(
|
||||||
|
&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() {
|
if let Err(e) = self.content.save(filesystem) {
|
||||||
eprintln!("Failed to save: {e}");
|
eprintln!("Failed to save: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,7 +170,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() {
|
if let Err(e) = self.content.save(filesystem) {
|
||||||
eprintln!("Failed to save: {e}");
|
eprintln!("Failed to save: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,26 +178,23 @@ 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 = uuid::Uuid::new_v4().to_string();
|
copy.content.id = Id::new();
|
||||||
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, ©.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
|
filesystem.delete(&self.content.id).unwrap();
|
||||||
.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(&self.content.id).unwrap();
|
self.content = ContentSection::load(filesystem, &self.content.id).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// preview toggle
|
// preview toggle
|
||||||
@@ -189,9 +202,7 @@ impl MainEditor {
|
|||||||
|
|
||||||
// assistant toggle
|
// assistant toggle
|
||||||
#[cfg(feature = "llm")]
|
#[cfg(feature = "llm")]
|
||||||
if project.ai_enabled() {
|
ui.checkbox(&mut self.show_ai, "AI Assistant");
|
||||||
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");
|
||||||
@@ -239,7 +250,7 @@ impl MainEditor {
|
|||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
#[cfg(feature = "llm")]
|
||||||
if self.show_ai && project.ai_enabled() {
|
if self.show_ai {
|
||||||
let dialog = &mut self.dialog;
|
let dialog = &mut self.dialog;
|
||||||
|
|
||||||
dialog.content = self.content.content.clone();
|
dialog.content = self.content.content.clone();
|
||||||
@@ -263,7 +274,12 @@ impl MainEditor {
|
|||||||
self.editor_ui(ui, project);
|
self.editor_ui(ui, project);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectSettings) {
|
pub fn ui<F: FileSystem>(
|
||||||
|
&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 {
|
||||||
@@ -274,11 +290,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, ui);
|
self.render_ui(project, filesystem, ui);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
self.render_ui(project, ui);
|
self.render_ui(project, filesystem, ui);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -320,7 +336,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")
|
||||||
@@ -338,7 +354,7 @@ impl MainEditor {
|
|||||||
|
|
||||||
ui.set_min_width(max_width as f32);
|
ui.set_min_width(max_width as f32);
|
||||||
|
|
||||||
let response = ui.add(
|
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)
|
||||||
@@ -348,9 +364,6 @@ 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;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+20
-42
@@ -1,40 +1,36 @@
|
|||||||
use std::path::Path;
|
use std::fs;
|
||||||
|
|
||||||
use egui::TextEdit;
|
use egui::TextEdit;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
FILESYSTEM, PROJECT_FOLDER,
|
||||||
editors::tags::Tag,
|
editors::tags::Tag,
|
||||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
filesystem::{FileSystem, Id},
|
||||||
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<String>,
|
pub tags: Vec<Id>,
|
||||||
|
|
||||||
|
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: uuid::Uuid::new_v4().to_string(),
|
id: Id::new(),
|
||||||
name: "New Note".to_string(),
|
name: "New Note".to_string(),
|
||||||
subject: "".to_string(),
|
subject: "".to_string(),
|
||||||
content: "".to_string(),
|
content: "".to_string(),
|
||||||
@@ -47,7 +43,7 @@ impl Default for Note {
|
|||||||
impl Note {
|
impl Note {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
id: Id::new(),
|
||||||
name: "New Note".to_string(),
|
name: "New Note".to_string(),
|
||||||
subject: "".to_string(),
|
subject: "".to_string(),
|
||||||
content: "".to_string(),
|
content: "".to_string(),
|
||||||
@@ -56,16 +52,18 @@ impl Note {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn save(&mut self) -> std::io::Result<()> {
|
||||||
let id = &self.id;
|
let id = &self.id;
|
||||||
FILESYSTEM.write(Path::new(&format!("notes/{id}.json")), self.clone())?;
|
let data = serde_json::to_string(&self)?;
|
||||||
|
FILESYSTEM.write(id, data).unwrap();
|
||||||
|
|
||||||
self.saved = true;
|
self.saved = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn load(id: &Id) -> std::io::Result<Self> {
|
||||||
let mut note: Self = FILESYSTEM.read(Path::new(&format!("notes/{id}.json")))?;
|
let mut note: Note = FILESYSTEM.read(id).unwrap();
|
||||||
note.id = id.to_string();
|
note.id = id.clone();
|
||||||
note.saved = true;
|
note.saved = true;
|
||||||
Ok(note)
|
Ok(note)
|
||||||
}
|
}
|
||||||
@@ -79,31 +77,11 @@ impl Note {
|
|||||||
|
|
||||||
util::saved_status(ui, self.saved, &self.id, &self.name);
|
util::saved_status(ui, self.saved, &self.id, &self.name);
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
if ui.button("Save").clicked() {
|
||||||
if ui.button("Save").clicked() {
|
if let Err(e) = self.save() {
|
||||||
if let Err(e) = self.save() {
|
eprintln!("Failed to save: {e}");
|
||||||
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)
|
||||||
|
|||||||
@@ -4,29 +4,27 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
PROJECT_FOLDER, RightPanelContent,
|
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
|
||||||
editors::{
|
editors::{
|
||||||
tags::Tag,
|
tags::Tag,
|
||||||
template_editor::{FieldValue, Template},
|
template_editor::{FieldValue, Template},
|
||||||
},
|
},
|
||||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
filesystem::{FileSystem, Id},
|
||||||
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: ObjectId,
|
pub id: Id,
|
||||||
pub template_id: String,
|
pub template_id: Id,
|
||||||
|
|
||||||
// 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<String>,
|
pub tags: Vec<Id>,
|
||||||
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub saved: bool,
|
pub saved: bool,
|
||||||
@@ -52,8 +50,8 @@ impl Clone for ObjectInstance {
|
|||||||
impl Default for ObjectInstance {
|
impl Default for ObjectInstance {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
id: Id::new(),
|
||||||
template_id: "new_template_instance".to_string(),
|
template_id: Id::new(),
|
||||||
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(),
|
||||||
@@ -71,26 +69,25 @@ impl ObjectInstance {
|
|||||||
fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type));
|
fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
let instance = Self {
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
|
||||||
template_id: template.id.clone(),
|
|
||||||
name: "new_object".to_string(),
|
|
||||||
fields,
|
fields,
|
||||||
tags: Vec::new(),
|
template_id: template.id.clone(),
|
||||||
saved: false,
|
..Default::default()
|
||||||
dialog: None,
|
};
|
||||||
}
|
|
||||||
|
let _ = FILESYSTEM.create(Path::new("./objects"), instance.clone());
|
||||||
|
|
||||||
|
instance
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let id = &self.id;
|
FILESYSTEM.write(&self.id, self.clone())?;
|
||||||
FILESYSTEM.write(Path::new(&format!("objects/{id}.json")), self.clone())?;
|
|
||||||
self.saved = true;
|
self.saved = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let mut instance: Self = FILESYSTEM.read(Path::new(&format!("objects/{id}.json")))?;
|
let mut instance: ObjectInstance = FILESYSTEM.read(id)?;
|
||||||
instance.saved = true;
|
instance.saved = true;
|
||||||
Ok(instance)
|
Ok(instance)
|
||||||
}
|
}
|
||||||
@@ -122,21 +119,14 @@ impl ObjectInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ui.button("Create Copy").clicked() {
|
if ui.button("Create Copy").clicked() {
|
||||||
let mut copy = self.clone();
|
let new_id = Id::new();
|
||||||
copy.id = uuid::Uuid::new_v4().to_string();
|
FILESYSTEM.clone(&self.id, &new_id).unwrap();
|
||||||
copy.dialog = None;
|
let copy = Self::load(&new_id).unwrap();
|
||||||
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() {
|
||||||
let id = &self.id;
|
FILESYSTEM.delete(&self.id).unwrap();
|
||||||
FILESYSTEM
|
|
||||||
.delete(Path::new(&format!("objects/{id}.json")))
|
|
||||||
.expect("Failed to delete object");
|
|
||||||
|
|
||||||
*right_panel = Some(RightPanelContent::None);
|
*right_panel = Some(RightPanelContent::None);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -267,7 +257,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) = FILESYSTEM.read_bytes(&path) {
|
if let Ok(bytes) = std::fs::read(&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(),
|
||||||
@@ -292,12 +282,12 @@ impl ObjectInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn selector_ui(
|
fn selector_ui(
|
||||||
selected: &mut ObjectId,
|
selected: &mut Id,
|
||||||
objects: &mut [ObjectInstance],
|
objects: &mut [ObjectInstance],
|
||||||
ui: &mut egui::Ui,
|
ui: &mut egui::Ui,
|
||||||
saved: &mut bool,
|
saved: &mut bool,
|
||||||
) {
|
) {
|
||||||
if !selected.is_empty() {
|
if !selected.to_string().is_empty() {
|
||||||
if let Ok(object) = ObjectInstance::load(selected) {
|
if let Ok(object) = ObjectInstance::load(selected) {
|
||||||
ui.strong(&object.name);
|
ui.strong(&object.name);
|
||||||
}
|
}
|
||||||
@@ -333,7 +323,7 @@ impl ObjectInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ui.button("Remove").clicked() {
|
if ui.button("Remove").clicked() {
|
||||||
*selected = String::new();
|
*selected = Id::default();
|
||||||
*saved = false;
|
*saved = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+127
-196
@@ -2,12 +2,9 @@ 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::path::{Path, PathBuf};
|
use std::{io::Read, path::PathBuf, sync::LazyLock};
|
||||||
|
|
||||||
use crate::{
|
use crate::{PROJECT_FOLDER, filesystem::Id, util::saved_status};
|
||||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
|
||||||
util::saved_status,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub struct ProjectSettings {
|
pub struct ProjectSettings {
|
||||||
@@ -17,7 +14,6 @@ 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
|
||||||
@@ -33,6 +29,17 @@ 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 {
|
||||||
@@ -40,10 +47,25 @@ impl ProjectSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn load() -> Self {
|
pub fn load() -> Self {
|
||||||
if let Ok(mut proj) = FILESYSTEM.read::<Self>(Path::new("project.json")) {
|
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) {
|
||||||
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()
|
||||||
@@ -51,9 +73,9 @@ impl ProjectSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&mut self) {
|
pub fn save(&mut self) {
|
||||||
FILESYSTEM
|
let project_path = PROJECT_FOLDER.join("project.json");
|
||||||
.write(Path::new("project.json"), self.clone())
|
let content = serde_json::to_string_pretty(self).unwrap();
|
||||||
.unwrap();
|
std::fs::write(project_path, content).unwrap();
|
||||||
|
|
||||||
self.global_settings.save();
|
self.global_settings.save();
|
||||||
self.local_overrides.save();
|
self.local_overrides.save();
|
||||||
@@ -61,104 +83,14 @@ impl ProjectSettings {
|
|||||||
self.saved = true;
|
self.saved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(dead_code)]
|
||||||
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, "N/A", "Project Settings");
|
saved_status(ui, self.saved, &Id::default(), "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
|
||||||
@@ -167,65 +99,80 @@ impl ProjectSettings {
|
|||||||
.striped(true)
|
.striped(true)
|
||||||
.num_columns(2)
|
.num_columns(2)
|
||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
if Self::config_str(&mut self.project_name, "Project Name", ui) { self.saved = false };
|
ui.label("Project Name");
|
||||||
if Self::config_str(&mut self.project_author, "Project Author", ui) { self.saved = false };
|
ui.text_edit_singleline(&mut self.project_name);
|
||||||
if Self::config_str(&mut self.project_description, "Project Description", ui) { self.saved = false };
|
|
||||||
|
|
||||||
ui.label("Date");
|
|
||||||
if ui.add(DatePickerButton::new(&mut self.date)).changed() { self.saved = false };
|
|
||||||
ui.end_row();
|
ui.end_row();
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
ui.label("Project Author");
|
||||||
{
|
ui.text_edit_singleline(&mut self.project_author);
|
||||||
ui.label("AI Context Prompt");
|
|
||||||
if ui.add(TextEdit::multiline(&mut self.ai_context)
|
ui.end_row();
|
||||||
.font(egui::TextStyle::Monospace)
|
|
||||||
.interactive(true)
|
ui.label("Project Description");
|
||||||
.frame(false)
|
ui.text_edit_singleline(&mut self.project_description);
|
||||||
.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.end_row();
|
|
||||||
}
|
ui.label("Date");
|
||||||
|
ui.add(DatePickerButton::new(&mut self.date));
|
||||||
|
|
||||||
|
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();
|
||||||
});
|
});
|
||||||
|
|
||||||
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| {
|
||||||
#[cfg(feature = "llm")]
|
ui.label("Enable AI");
|
||||||
if ProjectSettings::config_str_override(
|
if let Some(ai_enabled) = &mut self.local_overrides.ai_enabled {
|
||||||
"LLM API URI",
|
ui.checkbox(ai_enabled, "Enable AI");
|
||||||
&mut self.local_overrides.llm_api_uri,
|
if ui.button("Remove Override").clicked() {
|
||||||
"http://localhost:1234",
|
self.local_overrides.ai_enabled = None;
|
||||||
ui,
|
}
|
||||||
) {
|
} else if ui.button("Override").clicked() {
|
||||||
self.saved = false;
|
self.local_overrides.ai_enabled = Some(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
ui.end_row();
|
||||||
if ProjectSettings::config_str_override(
|
|
||||||
"LLM API Key",
|
ui.label("LLM API URI");
|
||||||
&mut self.local_overrides.llm_api_key,
|
if let Some(llm_api_uri) = &mut self.local_overrides.llm_api_uri {
|
||||||
"1234",
|
ui.text_edit_singleline(llm_api_uri);
|
||||||
ui,
|
if ui.button("Remove Override").clicked() {
|
||||||
) {
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
ui.end_row();
|
||||||
if ProjectSettings::config_bool_override(
|
|
||||||
"Enable AI",
|
ui.label("LLM API Key");
|
||||||
&mut self.local_overrides.ai_enabled,
|
if let Some(llm_api_key) = &mut self.local_overrides.llm_api_key {
|
||||||
true,
|
ui.text_edit_singleline(llm_api_key);
|
||||||
ui,
|
if ui.button("Remove Override").clicked() {
|
||||||
) {
|
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();
|
||||||
@@ -236,37 +183,23 @@ impl ProjectSettings {
|
|||||||
.striped(true)
|
.striped(true)
|
||||||
.num_columns(2)
|
.num_columns(2)
|
||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
#[cfg(feature = "llm")]
|
ui.label("Enable AI");
|
||||||
if Self::config_bool(
|
ui.checkbox(&mut self.global_settings.ai_enabled.unwrap(), "Enable AI");
|
||||||
"Enable AI",
|
|
||||||
self.global_settings.ai_enabled.as_mut().unwrap(),
|
|
||||||
ui,
|
|
||||||
) {
|
|
||||||
self.saved = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
ui.end_row();
|
||||||
if Self::config_str(
|
|
||||||
self.global_settings.llm_api_uri.as_mut().unwrap(),
|
|
||||||
"LLM API URI",
|
|
||||||
ui,
|
|
||||||
) {
|
|
||||||
self.saved = false
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
ui.label("LLM API URI");
|
||||||
if Self::config_str(
|
ui.text_edit_singleline(self.global_settings.llm_api_uri.as_mut().unwrap());
|
||||||
self.global_settings.llm_api_key.as_mut().unwrap(),
|
|
||||||
"LLM API Key",
|
ui.end_row();
|
||||||
ui,
|
|
||||||
) {
|
ui.label("LLM API Key");
|
||||||
self.saved = false
|
ui.text_edit_singleline(self.global_settings.llm_api_key.as_mut().unwrap());
|
||||||
};
|
|
||||||
|
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();
|
||||||
|
|
||||||
@@ -303,9 +236,8 @@ 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::default(),
|
global_settings: EditorSettings::new(),
|
||||||
local_overrides: EditorSettings::new(),
|
local_overrides: EditorSettings::new(),
|
||||||
|
|
||||||
// window state
|
// window state
|
||||||
@@ -317,14 +249,10 @@ impl Default for ProjectSettings {
|
|||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub struct EditorSettings {
|
pub struct EditorSettings {
|
||||||
pub dark_theme: Option<bool>,
|
|
||||||
|
|
||||||
#[cfg(feature = "llm")]
|
|
||||||
pub llm_api_uri: Option<String>,
|
pub llm_api_uri: Option<String>,
|
||||||
#[cfg(feature = "llm")]
|
|
||||||
pub llm_api_key: Option<String>,
|
pub llm_api_key: Option<String>,
|
||||||
#[cfg(feature = "llm")]
|
|
||||||
pub ai_enabled: Option<bool>,
|
pub ai_enabled: Option<bool>,
|
||||||
|
pub dark_theme: Option<bool>,
|
||||||
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
is_global: bool,
|
is_global: bool,
|
||||||
@@ -333,11 +261,8 @@ 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),
|
||||||
|
|
||||||
@@ -350,11 +275,8 @@ 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,
|
||||||
|
|
||||||
@@ -363,34 +285,43 @@ impl EditorSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn load() -> Self {
|
pub fn load() -> Self {
|
||||||
if let Ok(mut settings) = FILESYSTEM.read::<Self>(Path::new("settings.json")) {
|
let path = PROJECT_FOLDER.join("settings.json");
|
||||||
settings.is_global = false;
|
let mut file = if let Ok(file) = std::fs::File::open(path) {
|
||||||
return settings;
|
file
|
||||||
}
|
} else {
|
||||||
|
return Self::default();
|
||||||
|
};
|
||||||
|
|
||||||
Self::new()
|
let mut contents = String::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 {
|
||||||
FILESYSTEM.config_path()
|
PathBuf::from(GLOBAL_SETTINGS_PATH.clone())
|
||||||
} else {
|
} else {
|
||||||
PathBuf::from("settings.json")
|
PROJECT_FOLDER.join("settings.json")
|
||||||
};
|
};
|
||||||
|
|
||||||
FILESYSTEM.write(path.as_path(), self.clone()).unwrap()
|
std::fs::write(path, content).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_global() -> Self {
|
pub fn load_global() -> Self {
|
||||||
let path = FILESYSTEM.config_path();
|
let path = PathBuf::from(GLOBAL_SETTINGS_PATH.clone());
|
||||||
|
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
FILESYSTEM.mkdir(path.parent().unwrap()).unwrap();
|
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||||
FILESYSTEM.write(path.as_path(), Self::default()).unwrap();
|
let content = serde_json::to_string_pretty(&Self::default()).unwrap();
|
||||||
|
std::fs::write(&path, content).unwrap();
|
||||||
|
|
||||||
|
// return a default config
|
||||||
|
return Self::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut settings = FILESYSTEM.read::<Self>(path.as_path()).unwrap();
|
let content = std::fs::read_to_string(path).unwrap();
|
||||||
settings.is_global = true;
|
serde_json::from_str(&content).unwrap()
|
||||||
settings
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-7
@@ -1,11 +1,15 @@
|
|||||||
use egui::{Response, RichText, TextEdit};
|
use egui::{Response, RichText, TextEdit};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{PROJECT_FOLDER, util};
|
use crate::{
|
||||||
|
FILESYSTEM, PROJECT_FOLDER,
|
||||||
|
filesystem::{FileSystem, Id},
|
||||||
|
util,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Tag {
|
pub struct Tag {
|
||||||
pub id: String,
|
pub id: Id,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
pub color: egui::Color32,
|
pub color: egui::Color32,
|
||||||
@@ -20,7 +24,7 @@ pub struct Tag {
|
|||||||
impl Default for Tag {
|
impl Default for Tag {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
id: Id::new(),
|
||||||
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),
|
||||||
@@ -128,7 +132,7 @@ impl Tag {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn selector_ui(tag_ids: &mut Vec<String>, ui: &mut egui::Ui, saved: Option<&mut bool>) {
|
pub fn selector_ui(tag_ids: &mut Vec<Id>, 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();
|
||||||
@@ -202,9 +206,11 @@ impl Tag {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let path = PROJECT_FOLDER.join("tags").join(format!("{id}.json"));
|
let mut tag: Self = FILESYSTEM.read(id)?;
|
||||||
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
|
tag.saved = true;
|
||||||
|
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>> {
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ 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::{
|
||||||
RightPanelContent,
|
FILESYSTEM, PROJECT_FOLDER, RightPanelContent,
|
||||||
editors::object_editor::ObjectInstance,
|
editors::object_editor::ObjectInstance,
|
||||||
filesystem::{FILESYSTEM, LegacyFileSystem},
|
filesystem::{self, FileSystem, Id},
|
||||||
util::{self, Error},
|
util::{self, Error},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -18,7 +17,7 @@ pub enum FieldType {
|
|||||||
MultiLine,
|
MultiLine,
|
||||||
Date,
|
Date,
|
||||||
Number,
|
Number,
|
||||||
Link { template_id: Option<String> },
|
Link { template_id: Option<Id> },
|
||||||
Links,
|
Links,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,8 +48,8 @@ pub enum FieldValue {
|
|||||||
MultiLine(String),
|
MultiLine(String),
|
||||||
Date(NaiveDate),
|
Date(NaiveDate),
|
||||||
Number(f64),
|
Number(f64),
|
||||||
Link(String),
|
Link(Id),
|
||||||
Links(Vec<String>),
|
Links(Vec<Id>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FieldValue {
|
impl FieldValue {
|
||||||
@@ -61,7 +60,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(String::new()),
|
FieldType::Link { template_id: None } => Self::Link(Id::default()),
|
||||||
FieldType::Link {
|
FieldType::Link {
|
||||||
template_id: Some(template_id),
|
template_id: Some(template_id),
|
||||||
} => Self::Link(template_id.clone()),
|
} => Self::Link(template_id.clone()),
|
||||||
@@ -90,7 +89,7 @@ pub struct FieldDefinition {
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Template {
|
pub struct Template {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub id: String,
|
pub id: Id,
|
||||||
|
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub fields: Vec<FieldDefinition>,
|
pub fields: Vec<FieldDefinition>,
|
||||||
@@ -156,7 +155,7 @@ impl Default for Template {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: "New Template".to_string(),
|
name: "New Template".to_string(),
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
id: Id::new(),
|
||||||
description: Some(String::from("Placeholder description")),
|
description: Some(String::from("Placeholder description")),
|
||||||
fields: Vec::new(),
|
fields: Vec::new(),
|
||||||
saved: false,
|
saved: false,
|
||||||
@@ -172,15 +171,15 @@ impl Default for Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Template {
|
impl Template {
|
||||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn load(id: &Id) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let mut template = FILESYSTEM.read::<Self>(Path::new(&format!("templates/{id}.json")))?;
|
let mut template: Self = FILESYSTEM.read(id)?;
|
||||||
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>> {
|
||||||
let id = &self.id;
|
FILESYSTEM.write(&self.id, self.clone())?;
|
||||||
FILESYSTEM.write(Path::new(&format!("templates/{id}.json")), self.clone())?;
|
|
||||||
self.saved = true;
|
self.saved = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -198,6 +197,16 @@ 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
|
||||||
@@ -210,16 +219,19 @@ 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 = uuid::Uuid::new_v4().to_string();
|
copy.id = Id::new();
|
||||||
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() {
|
||||||
let id = &self.id;
|
std::fs::remove_file(
|
||||||
FILESYSTEM
|
PROJECT_FOLDER
|
||||||
.delete(Path::new(&format!("templates/{id}.json")))
|
.join("templates")
|
||||||
.unwrap();
|
.join(format!("{}.json", self.id)),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
*new_instance = Some(RightPanelContent::None);
|
*new_instance = Some(RightPanelContent::None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+115
-63
@@ -1,4 +1,7 @@
|
|||||||
use std::path::Path;
|
use itertools::Itertools;
|
||||||
|
use std::fs::{self, DirEntry};
|
||||||
|
|
||||||
|
// use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
PROJECT_FOLDER, RightPanelContent,
|
PROJECT_FOLDER, RightPanelContent,
|
||||||
@@ -7,7 +10,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::{FILESYSTEM, FsError, LegacyFileSystem},
|
filesystem::Id,
|
||||||
note_editor::Note,
|
note_editor::Note,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -163,6 +166,14 @@ 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],
|
||||||
@@ -172,7 +183,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_deref() == parent_id)
|
.filter(|doc| doc.parent.as_ref().map(|id| id.as_str()) == parent_id)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for doc in child_docs {
|
for doc in child_docs {
|
||||||
@@ -197,7 +208,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), load_doc);
|
Self::render_doc_branch(ui, documents, Some(doc.id.as_str()), load_doc);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,64 +242,95 @@ 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(&file_name),
|
ui.make_persistent_id("assets"),
|
||||||
false,
|
true,
|
||||||
)
|
)
|
||||||
.show_header(ui, |ui| {
|
.show_header(ui, |ui| {
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.label(file_name);
|
ui.label("Assets");
|
||||||
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.body(|ui| {
|
.body(|ui| {
|
||||||
// recursive call to render the next level of documents
|
let entries = fs::read_dir(PROJECT_FOLDER.join("assets"))
|
||||||
for dir in dirs.iter() {
|
.unwrap()
|
||||||
Self::render_asset_dir(ui, to_load, dir);
|
.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<_>>();
|
||||||
|
|
||||||
for file in files.iter() {
|
for entry in entries {
|
||||||
Self::render_asset(ui, to_load, file);
|
Self::render_entry(ui, to_load, &entry);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_asset(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, path: &Path) {
|
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) {
|
||||||
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
|
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();
|
||||||
|
|
||||||
if ui
|
if is_dir {
|
||||||
.selectable_label(false, format!("📄 {file_name}"))
|
let entries = fs::read_dir(path)
|
||||||
.clicked()
|
.unwrap()
|
||||||
{
|
.filter_map(Result::ok)
|
||||||
// use asset::load to get the file at the path
|
.collect::<Vec<_>>();
|
||||||
let asset_path = path.strip_prefix(PROJECT_FOLDER.join("assets")).unwrap();
|
|
||||||
let asset = Asset::open(asset_path.to_string_lossy().to_string());
|
egui::collapsing_header::CollapsingState::load_with_default_open(
|
||||||
*to_load = Some(RightPanelContent::Asset(Box::new(asset)));
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load templates from the templates folder
|
// load templates from the templates folder
|
||||||
fn load_templates(&mut self) -> Result<(), FsError> {
|
fn load_templates(&mut self) -> std::io::Result<()> {
|
||||||
let path = Path::new("templates");
|
let templates_folder = PROJECT_FOLDER.join("templates");
|
||||||
|
if !templates_folder.exists() {
|
||||||
if !FILESYSTEM.exists(path) {
|
std::fs::create_dir_all(&templates_folder)?;
|
||||||
FILESYSTEM.mkdir(path)?;
|
|
||||||
}
|
}
|
||||||
let mut templates = Vec::new();
|
let mut templates = Vec::new();
|
||||||
for entry in FILESYSTEM.lsfiles(path)? {
|
for entry in std::fs::read_dir(&templates_folder).unwrap() {
|
||||||
match FILESYSTEM.read::<Template>(&entry) {
|
let path = entry.unwrap().path();
|
||||||
|
match Template::load(&Id::from_path(&path)) {
|
||||||
Ok(t) => templates.push(t),
|
Ok(t) => templates.push(t),
|
||||||
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
|
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.templates = templates;
|
self.templates = templates;
|
||||||
@@ -297,16 +339,17 @@ impl Explorer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load objects from the objects folder
|
// load objects from the objects folder
|
||||||
fn load_objects(&mut self) -> Result<(), FsError> {
|
fn load_objects(&mut self) -> std::io::Result<()> {
|
||||||
let path = Path::new("objects");
|
let objects_folder = PROJECT_FOLDER.join("objects");
|
||||||
if !FILESYSTEM.exists(path) {
|
if !objects_folder.exists() {
|
||||||
FILESYSTEM.mkdir(path)?;
|
std::fs::create_dir_all(&objects_folder)?;
|
||||||
}
|
}
|
||||||
let mut objects = Vec::new();
|
let mut objects = Vec::new();
|
||||||
for entry in FILESYSTEM.lsfiles(path)? {
|
for entry in std::fs::read_dir(&objects_folder).unwrap() {
|
||||||
match FILESYSTEM.read::<ObjectInstance>(&entry) {
|
let path = entry.unwrap().path();
|
||||||
|
match ObjectInstance::load(&Id::from_path(&path)) {
|
||||||
Ok(o) => objects.push(o),
|
Ok(o) => objects.push(o),
|
||||||
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
|
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.objects = objects;
|
self.objects = objects;
|
||||||
@@ -315,15 +358,16 @@ impl Explorer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load notes from the notes folder
|
// load notes from the notes folder
|
||||||
fn load_notes(&mut self) -> Result<(), FsError> {
|
fn load_notes(&mut self) -> std::io::Result<()> {
|
||||||
let path = Path::new("notes");
|
let notes_folder = PROJECT_FOLDER.join("notes");
|
||||||
if !FILESYSTEM.exists(path) {
|
if !notes_folder.exists() {
|
||||||
FILESYSTEM.mkdir(path)?;
|
std::fs::create_dir_all(¬es_folder)?;
|
||||||
}
|
}
|
||||||
let mut notes = Vec::new();
|
let mut notes = Vec::new();
|
||||||
|
|
||||||
for entry in FILESYSTEM.lsfiles(path)? {
|
for entry in std::fs::read_dir(¬es_folder).unwrap() {
|
||||||
match FILESYSTEM.read::<Note>(&entry) {
|
let path = entry.unwrap().path();
|
||||||
|
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}"),
|
||||||
}
|
}
|
||||||
@@ -335,17 +379,25 @@ impl Explorer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load documents from the documents folder
|
// load documents from the documents folder
|
||||||
fn load_documents(&mut self) -> Result<(), FsError> {
|
fn load_documents(&mut self) -> std::io::Result<()> {
|
||||||
let path = Path::new("documents");
|
let documents_folder = PROJECT_FOLDER.join("documents");
|
||||||
if !FILESYSTEM.exists(path) {
|
if !documents_folder.exists() {
|
||||||
FILESYSTEM.mkdir(path)?;
|
std::fs::create_dir_all(&documents_folder)?;
|
||||||
}
|
}
|
||||||
let mut documents = Vec::new();
|
let mut documents = Vec::new();
|
||||||
|
|
||||||
for entry in FILESYSTEM.lsfiles(path)? {
|
for entry in std::fs::read_dir(&documents_folder).unwrap() {
|
||||||
match FILESYSTEM.read::<ContentSection>(&entry) {
|
let path = entry.unwrap().path();
|
||||||
Ok(document) => documents.push(MainEditor::open(document)),
|
// TODO: Update to use FileSystem API
|
||||||
Err(err) => eprintln!("Could not parse file {entry:?}: {err}"),
|
// 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:?}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,7 +406,7 @@ impl Explorer {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_tags(&mut self) -> Result<(), FsError> {
|
fn load_tags(&mut self) -> std::io::Result<()> {
|
||||||
self.tags = Tag::load_all();
|
self.tags = Tag::load_all();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+118
-84
@@ -1,15 +1,11 @@
|
|||||||
|
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
fmt,
|
||||||
io,
|
ops::{Deref, DerefMut},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::LazyLock,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use serde::{Serialize, de::DeserializeOwned};
|
use crate::FILESYSTEM;
|
||||||
|
|
||||||
#[cfg(feature = "native")]
|
|
||||||
use crate::PROJECT_FOLDER;
|
|
||||||
use crate::filesystem::native::NativeFileSystem;
|
|
||||||
|
|
||||||
#[cfg(feature = "native")]
|
#[cfg(feature = "native")]
|
||||||
pub mod native;
|
pub mod native;
|
||||||
@@ -17,87 +13,125 @@ pub mod native;
|
|||||||
#[cfg(feature = "web")]
|
#[cfg(feature = "web")]
|
||||||
pub mod web;
|
pub mod web;
|
||||||
|
|
||||||
pub static FILESYSTEM: LazyLock<NativeFileSystem> = LazyLock::new(|| {
|
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize)]
|
||||||
#[cfg(feature = "native")]
|
|
||||||
return NativeFileSystem::new(PROJECT_FOLDER.clone());
|
|
||||||
#[cfg(feature = "web")]
|
|
||||||
return Box::new(web::WebFileSystem::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 FsError {
|
|
||||||
Io(io::Error),
|
|
||||||
Serde(serde_json::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for FsError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
FsError::Io(e) => write!(f, "IO error: {e}"),
|
|
||||||
FsError::Serde(e) => write!(f, "Serialization error: {e}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
pub struct Id(String);
|
||||||
|
|
||||||
#[allow(dead_code)]
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum FileSystemError {
|
||||||
|
FileNotFound(Id, String),
|
||||||
|
DirectoryNotFound(PathBuf, String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for FileSystemError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>,
|
||||||
|
}
|
||||||
|
|
||||||
pub trait FileSystem {
|
pub trait FileSystem {
|
||||||
fn load<T: DeserializeOwned>(&self, id: Id) -> Result<T, FsError>;
|
fn new(root: impl AsRef<Path>) -> Self;
|
||||||
fn save<T: Serialize>(&self, id: Id, data: T) -> Result<(), FsError>;
|
|
||||||
fn mkdir(&self, path: Path) -> Result<(), FsError>;
|
fn create(&self, directory: &Path, data: impl Serialize) -> Result<Id, FileSystemError>;
|
||||||
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>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
pub struct FileBorrow<T: DeserializeOwned + Serialize> {
|
||||||
pub struct Index {
|
pub id: Id,
|
||||||
file_cache: HashMap<Id, PathBuf>,
|
pub file: T,
|
||||||
project_root: Directory,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
impl<T: DeserializeOwned + Serialize> Deref for FileBorrow<T> {
|
||||||
pub struct Directory {
|
type Target = T;
|
||||||
name: String,
|
|
||||||
id: Id,
|
fn deref(&self) -> &Self::Target {
|
||||||
children: HashMap<Id, Directory>,
|
&self.file
|
||||||
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+294
-83
@@ -1,116 +1,327 @@
|
|||||||
use std::fs;
|
use std::collections::HashMap;
|
||||||
use std::io::{ErrorKind, Read};
|
use std::fs::{self, File};
|
||||||
|
use std::io::{Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use serde::Serialize;
|
use image::EncodableLayout;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::to_string;
|
||||||
|
|
||||||
use crate::filesystem::{FsError, LegacyFileSystem};
|
use crate::filesystem::FileSystemError;
|
||||||
|
|
||||||
|
use super::FileSystem;
|
||||||
|
|
||||||
pub struct NativeFileSystem {
|
pub struct NativeFileSystem {
|
||||||
project_root: PathBuf,
|
root: PathBuf,
|
||||||
|
index: Arc<RwLock<HashMap<super::Id, PathBuf>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NativeFileSystem {
|
impl NativeFileSystem {
|
||||||
/// Create a new instance.
|
/// Rebuild the entire index by scanning the filesystem
|
||||||
pub fn new(root: impl Into<PathBuf>) -> Self {
|
fn rebuild_index(&self) -> Result<(), std::io::Error> {
|
||||||
Self {
|
let mut index = HashMap::new();
|
||||||
project_root: root.into(),
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve the user supplied *relative* path against the project root.
|
/// Remove an entry from the index
|
||||||
#[inline]
|
fn remove_from_index(&self, id: &super::Id) {
|
||||||
fn full_path(&self, path: &Path) -> PathBuf {
|
if let Ok(mut index) = self.index.write() {
|
||||||
self.project_root.join(path)
|
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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LegacyFileSystem for NativeFileSystem {
|
impl FileSystem for NativeFileSystem {
|
||||||
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> {
|
fn new(root: impl AsRef<Path>) -> Self {
|
||||||
let full_path = self.full_path(path);
|
let fs = Self {
|
||||||
let file = fs::File::open(full_path).map_err(FsError::Io)?;
|
root: root.as_ref().to_path_buf(),
|
||||||
serde_json::from_reader(file).map_err(FsError::Serde)
|
index: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
};
|
||||||
|
fs.rebuild_index().unwrap_or_else(|e| {
|
||||||
|
eprintln!("Warning: Failed to build initial index: {e}");
|
||||||
|
});
|
||||||
|
fs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError> {
|
fn create(&self, directory: &Path, data: impl Serialize) -> Result<super::Id, FileSystemError> {
|
||||||
let full_path = self.full_path(path);
|
let dir = if directory.is_absolute() {
|
||||||
let mut contents = Vec::new();
|
directory.to_path_buf()
|
||||||
fs::File::open(full_path)?.read_to_end(&mut contents)?;
|
} else {
|
||||||
Ok(contents)
|
self.root.join(directory)
|
||||||
}
|
};
|
||||||
|
|
||||||
fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError> {
|
if !dir.exists() {
|
||||||
let full_path = self.full_path(path);
|
fs::create_dir_all(&dir).map_err(|e| {
|
||||||
|
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)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let file = fs::File::create(full_path)?;
|
if !dir.is_dir() {
|
||||||
serde_json::to_writer(file, &data).map_err(FsError::Serde)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete(&self, path: &Path) -> Result<(), FsError> {
|
fn read<T: DeserializeOwned>(&self, id: &super::Id) -> Result<T, FileSystemError> {
|
||||||
let full_path = self.full_path(path);
|
let path = self.find_path_by_id(id).ok_or_else(|| {
|
||||||
match fs::remove_file(&full_path) {
|
FileSystemError::FileNotFound(id.clone(), "No path found!".to_string())
|
||||||
Ok(()) => Ok(()),
|
})?;
|
||||||
Err(e) if e.kind() == ErrorKind::IsADirectory => {
|
|
||||||
// Remove a directory tree.
|
let val = serde_json::from_reader(
|
||||||
fs::remove_dir_all(full_path).map_err(FsError::Io)
|
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(())
|
||||||
}
|
}
|
||||||
Err(e) => Err(FsError::Io(e)),
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.map_err(|e| FileSystemError::FileNotFound(id.clone(), e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mkdir(&self, path: &Path) -> Result<(), FsError> {
|
fn exists(&self, id: &super::Id) -> bool {
|
||||||
let full_path = self.full_path(path);
|
self.find_path_by_id(id).is_some()
|
||||||
fs::create_dir_all(full_path).map_err(FsError::Io)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lsfiles(&self, path: &Path) -> Result<Vec<PathBuf>, FsError> {
|
fn lsdir(&self, id: &super::Id) -> Result<Vec<String>, FileSystemError> {
|
||||||
let full_path = self.full_path(path);
|
let path = match self.find_path_by_id(id) {
|
||||||
let paths = fs::read_dir(full_path)?
|
Some(p) if p.is_dir() => p,
|
||||||
.filter_map(|res| res.ok())
|
Some(p) => p.parent().map(Path::to_path_buf).ok_or_else(|| {
|
||||||
.filter(|entry| entry.file_type().unwrap().is_file())
|
FileSystemError::DirectoryNotFound(PathBuf::from(id.to_string()), "".to_string())
|
||||||
.map(|entry| entry.path())
|
})?,
|
||||||
.collect();
|
None => {
|
||||||
|
return Err(FileSystemError::DirectoryNotFound(
|
||||||
|
PathBuf::from(id.to_string()),
|
||||||
|
id.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Ok(paths)
|
let mut entries = Vec::new();
|
||||||
}
|
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 full_path = self.full_path(path);
|
let entry = entry
|
||||||
let paths = fs::read_dir(full_path)?
|
.map_err(|e| FileSystemError::DirectoryNotFound(path.clone(), e.to_string()))?;
|
||||||
.filter_map(|res| res.ok())
|
if let Some(name) = entry.file_name().to_str() {
|
||||||
.filter(|entry| entry.file_type().unwrap().is_dir())
|
entries.push(name.to_string());
|
||||||
.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,7 +440,6 @@ 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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-3
@@ -10,16 +10,17 @@ mod explorer;
|
|||||||
#[cfg(feature = "llm")]
|
#[cfg(feature = "llm")]
|
||||||
mod llm_integration;
|
mod llm_integration;
|
||||||
|
|
||||||
mod util;
|
|
||||||
|
|
||||||
mod filesystem;
|
mod filesystem;
|
||||||
|
|
||||||
|
mod util;
|
||||||
|
|
||||||
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";
|
||||||
@@ -29,6 +30,9 @@ 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 {
|
||||||
@@ -48,6 +52,7 @@ 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 {
|
||||||
@@ -93,6 +98,7 @@ 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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +212,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.editor.ui(ctx, &mut self.project, &self.filesystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// configure appearance of UI elements
|
// configure appearance of UI elements
|
||||||
|
|||||||
+3
-1
@@ -3,6 +3,8 @@ 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,
|
||||||
@@ -26,7 +28,7 @@ impl Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &str, name: &str) {
|
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &Id, name: &str) {
|
||||||
ui.group(|ui| {
|
ui.group(|ui| {
|
||||||
ui.set_max_width(ui.available_width());
|
ui.set_max_width(ui.available_width());
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"llm_api_uri": "http://localhost:1234",
|
||||||
|
"llm_api_key": "",
|
||||||
|
"ai_enabled": true,
|
||||||
|
"dark_theme": true
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user