Create an app using Tauri and Vanilla JavaScript, with a clean API for creating, modifying, and deleting files. The frontend should be minimalistic, just a blank page that allows you to create a new note (.txt file) by clicking anywhere on the page. Upon the first launch, the app will prompt you to select a folder to store and access its files, similar to how Obsidian handles vaults. Consider we are using npm instead of cargo, and using the v2 of tauri.
Assistant
Certainly! Let's create a minimalistic Tauri app using Vanilla JavaScript that allows users to select a folder upon first launch and create, modify, and delete .txt files within that folder by interacting with the app's UI.
Prerequisites
Node.js and npm installed on your system.
Tauri CLI v2 installed globally via npm.
Step 1: Install Tauri CLI v2
bash
npm install -g @tauri-apps/cli@v2
Step 2: Create a New Tauri Project
Create a new directory for your app and initialize a Tauri project:
bash
mkdir tauri-vanilla-app
cd tauri-vanilla-app
tauri init
During the initialization, select the following options:
Frontend Framework: Vanilla HTML/CSS/JS
Package Manager: npm
Step 3: Project Structure
Your project should now have the following structure:
State Management: We're using Tauri's State to manage the folder path across commands.
Folder Selection: On first launch, get_folder_path returns None, prompting the frontend to open a folder selection dialog.
Creating Notes: Clicking anywhere on the page triggers create_note, which creates a new .txt file in the selected folder.
Concurrency: Mutex is used to safely share mutable state (folder_path) across threads.
Further Improvements
Modifying and Deleting Files: Implement additional commands in main.rs for modifying and deleting files as needed.
Persistent Storage: Save the folder_path to a configuration file to persist across app restarts.
Adding Persistent Storage
To save the folder path between sessions, you can use the tauri::api::path::app_dir to store a configuration file.
Update set_folder_path Function
rust
use tauri::api::path::app_dir;
#[tauri::command]
fn set_folder_path(path: String, folder_path: State<FolderPath>) {
*folder_path.0.lock().unwrap() = Some(path.clone());
if let Some(dir) = app_dir() {
let config_path = dir.join("config.txt");
fs::write(config_path, &path).unwrap();
}
}
Update get_folder_path Function
rust
#[tauri::command]
fn get_folder_path(folder_path: State<FolderPath>) -> Option<String> {
let mut path = folder_path.0.lock().unwrap();
if path.is_none() {
if let Some(dir) = app_dir() {
let config_path = dir.join("config.txt");
if let Ok(saved_path) = fs::read_to_string(config_path) {
*path = Some(saved_path);
}
}
}
path.clone()
}
Conclusion
You've now created a minimalistic Tauri application using Vanilla JavaScript that allows users to select a folder and create notes by clicking on the page. You can extend this application by adding more functionalities such as displaying the list of notes, editing, and deleting them.
Note: Always ensure that you handle errors and edge cases in your actual implementation to create a robust application.