propagated use of config home path to other systems. file loading now works using rust instead of js

This commit is contained in:
Iaphetes 2025-03-09 09:03:40 +01:00
parent 62918416ee
commit 11bd003298
11 changed files with 1007 additions and 1000 deletions

1460
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -21,6 +21,7 @@ tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = ["protocol-asset", "unstable"] }
tauri-plugin-shell = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

View file

@ -6,13 +6,17 @@ use std::{
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
use crate::get_basepath;
async fn populate_file_db(db: &SqlitePool) {}
#[tauri::command]
pub async fn initialize_database(
app_handle: tauri::AppHandle,
basepath: String,
pathtemplate: String,
) {
if let Some(basepath) = get_basepath(app_handle.clone()) {
let db_path = Path::new("Documents")
.join(Path::new("Knowledgebase"))
.join("db.sqlite");
@ -61,7 +65,10 @@ pub async fn initialize_database(
.execute(&db)
.await
.unwrap();
}
}
#[tauri::command]
async fn store_diff(diff: String) {}
async fn store_diff(diff: String) {
}

View file

@ -0,0 +1,19 @@
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
use std::fs::{read_to_string};
use std::path::Path;
use crate::get_basepath;
#[tauri::command]
pub async fn save(
app_handle: tauri::AppHandle,
path: String, content: String) {}
#[tauri::command]
pub async fn load(
path: String) -> Result<String, String> {
println!("loading file");
read_to_string(path).map_err(|e| e.to_string())
}

View file

@ -1,3 +1,4 @@
use crate::get_basepath;
use html_tag::HtmlTag;
use shellexpand;
use std::{
@ -5,11 +6,11 @@ use std::{
path::Path,
};
#[tauri::command]
pub fn dir_tree_html(basepath: &str, filter: Vec<String>) -> String {
match shellexpand::full(basepath) {
Ok(path) => add_dir_tree_node(&Path::new(&path.into_owned()), &filter).to_html(),
Err(_) => String::new(),
pub fn dir_tree_html(app_handle: tauri::AppHandle, filter: Vec<String>) -> String {
if let Some(basepath) = get_basepath(app_handle) {
add_dir_tree_node(&Path::new(&basepath), &filter).to_html()
} else {
String::new()
}
}
fn add_dir_tree_node(path: &Path, filter: &Vec<String>) -> HtmlTag {

View file

@ -1,6 +1,7 @@
mod config;
mod database;
mod file_access;
mod file_handler;
mod file_tree;
mod markdown_parser;
mod search;
@ -14,7 +15,7 @@ use std::env;
use std::sync::Mutex;
use tauri::Manager;
use tauri_plugin_fs::FsExt;
use file_handler::load;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
@ -40,6 +41,7 @@ pub fn run() {
search_files,
get_basepath,
set_basepath,
load
])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View file

@ -1,2 +0,0 @@
#[tauri::command]
pub async fn save(path: String, content: String) {}

View file

@ -2,30 +2,29 @@ const { convertFileSrc, invoke } = window.__TAURI__.core;
import { handle_file_select } from "./filesystem.js";
var search_input = document.getElementById("file-search-dialog-input");
search_input.addEventListener('input', () => {
search_input.addEventListener("input", () => {
search_files();
});
function search_files(){
function search_files() {
var text = search_input.innerText;
invoke("search_files", { searchstring: text, basepath: "$HOME/Documents/Knowledgebase", filter: ["md"]}).then(
(ret) => {
var tag_id = document.getElementById('file-search-results');
invoke("search_files", {
searchstring: text,
basepath: "$HOME/Documents/Knowledgebase",
filter: ["md"],
}).then((ret) => {
var tag_id = document.getElementById("file-search-results");
var result_div = "";
console.log(ret);
ret.forEach(element => {
ret.forEach((element) => {
result_div += element;
});
console.log(result_div);
tag_id.innerHTML = result_div;
// tag_id.innerHTML = assetUrl.concat(" ", ' \n <img src="'.concat("", assetUrl).concat("", '" alt="Girl in a jacket" width="500" height="600">'))
}
);
});
}
let filetree = document.getElementById('file-search-results');
let filetree = document.getElementById("file-search-results");
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
@ -35,11 +34,10 @@ const callback = (mutationList, observer) => {
var anchors = document.getElementsByClassName("file-search-button");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function() {
anchor.onclick = function () {
handle_file_select(this.parentElement.id);
};
};
}
};
const observer = new MutationObserver(callback);

View file

@ -5,14 +5,12 @@ import { render_markdown } from "./main.js";
var selected_file = "";
export function handle_file_select(filename) {
if (filename.endsWith("md")) {
readTextFile(convertFileSrc(filename)).then(
(ret) => {
var tag_id = document.getElementById('markdown_input');
invoke("load", { path: filename }).then((ret) => {
var tag_id = document.getElementById("markdown_input");
tag_id.innerHTML = "<pre>".concat("", ret).concat("", "</pre>");
render_markdown();
selected_file = filename;
}
);
});
}
}
@ -20,17 +18,15 @@ export function save_file() {
console.log(selected_file);
}
document.getElementById("save-file").onclick = function () {
save_file();
}
save_file();
};
function dropdown(id) {
var dropdown_element = document.getElementById(id);
var dropdown_children = dropdown_element.children;
console.log(dropdown_element.getAttribute("expanded"));
if (dropdown_element.getAttribute("expanded") == "false") {
dropdown_element.setAttribute("expanded", "true");
}
else {
} else {
dropdown_element.setAttribute("expanded", "false");
}
for (var i = 0; i < dropdown_children.length; i++) {
@ -46,17 +42,17 @@ function dropdown(id) {
}
}
}
}
window.onload = function() {
invoke("dir_tree_html", { basepath: "~/Documents/Knowledgebase", filter: ["*"] }).then(
(ret) => {
var tag_id = document.getElementById('filetree');
window.onload = function () {
invoke("dir_tree_html", {
basepath: "~/Documents/Knowledgebase",
filter: ["*"],
}).then((ret) => {
var tag_id = document.getElementById("filetree");
tag_id.innerHTML = ret;
}
)
}
let filetree = document.getElementById('filetree');
});
};
let filetree = document.getElementById("filetree");
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
@ -65,18 +61,17 @@ const callback = (mutationList, observer) => {
var anchors = document.getElementsByClassName("filetree-directory-button");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function() {
anchor.onclick = function () {
dropdown(this.parentElement.id);
};
};
}
var anchors = document.getElementsByClassName("filetree-file-button");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function() {
anchor.onclick = function () {
handle_file_select(this.parentElement.id);
};
};
}
};
// Create an observer instance linked to the callback function
@ -84,4 +79,3 @@ const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(filetree, config);

View file

@ -5,24 +5,21 @@ let text = "";
let placeholder_path = "FILEPATH";
let path_template = convertFileSrc(placeholder_path);
let textarea = document.getElementById('markdown_input');
let textarea = document.getElementById("markdown_input");
// TODO move to place, where the knowledgebase is created...
invoke("initialize_database", { basepath: "~/Documents/Knowledgebase", pathtemplate: path_template }).then(
(ret) => {
}
);
invoke("get_basepath").then(
(ret) => {
if (!ret){
invoke("initialize_database", {
basepath: "~/Documents/Knowledgebase",
pathtemplate: path_template,
}).then((ret) => {});
invoke("get_basepath").then((ret) => {
if (!ret) {
const setup_dialog = document.getElementById("setup-dialog");
setup_dialog.show();
}else{
} else {
console.log(ret);
}
}
)
});
document.getElementById("knowledgebase-button").onclick = async function () {
const file = await open({
@ -32,33 +29,31 @@ document.getElementById("knowledgebase-button").onclick = async function () {
const path_text = document.getElementById("knowledgebase-path");
path_text.innerText = file;
invoke("set_basepath", {path: file}).then(
(ret) => {
if (ret){
invoke("set_basepath", { path: file }).then((ret) => {
if (ret) {
const setup_dialog = document.getElementById("setup-dialog");
console.log("hiding");
setup_dialog.close();
location.reload();
}
}
);
});
console.log(file);
};
//
export function render_markdown() {
text = textarea.innerText;
invoke("parse_markdown", { document: text, pathtemplate: path_template, basepath: "$HOME/Documents/Knowledgebase" }).then(
(ret) => {
var tag_id = document.getElementById('rendered_markdown');
invoke("parse_markdown", {
document: text,
pathtemplate: path_template,
}).then((ret) => {
var tag_id = document.getElementById("rendered_markdown");
tag_id.innerHTML = "<pre>".concat("", ret).concat("", "</pre>");
// tag_id.innerHTML = assetUrl.concat(" ", ' \n <img src="'.concat("", assetUrl).concat("", '" alt="Girl in a jacket" width="500" height="600">'))
}
);
});
}
textarea.addEventListener('input', () => {
textarea.addEventListener("input", () => {
render_markdown();
});
// Random tree
// const N = 300;
// const gData = {

View file

@ -59,7 +59,7 @@
width: 50%;
padding: 0.5em;
/* margin: .1em; */
margin-left: .1em;
margin-left: 0.1em;
border: solid;
border-color: var(--highlight-color);
/* border-radius: .5em; */
@ -90,7 +90,6 @@
overflow-y: scroll;
}
h1 {
text-align: left;
line-height: 18px;
@ -117,7 +116,6 @@ li {
}
.top-bar {
display: flex;
flex-direction: row;
}
@ -138,10 +136,8 @@ li {
color: #f6f6f6;
background-color: var(--main-bg-color);
}
}
.main {
display: flex;
height: 95vh;
@ -182,7 +178,6 @@ li {
overflow: hidden;
color: var(--text-color);
text-overflow: ellipsis;
}
.filetree-node {
@ -196,7 +191,6 @@ li {
margin-right: 5px;
}
.filetree_expand {
background-color: transparent;
border: none;
@ -216,7 +210,10 @@ dialog {
background-color: var(--highlight-color);
border: none;
border-radius: 10px;
box-shadow: 0 0 #0000, 0 0 #0000, 0 25px 50px -12px rgba(0, 0, 0, 0.25);
box-shadow:
0 0 #0000,
0 0 #0000,
0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 50%;
height: 20%;
margin: auto;
@ -224,7 +221,7 @@ dialog {
/* flex-direction: column; */
overflow: hidden;
color: var(--text-color);
font-size: .5cm;
font-size: 0.5cm;
}
.row {
@ -246,7 +243,6 @@ dialog {
}
.button:hover {
border: solid;
border-color: var(--bright-highlight-color);
}