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.
Assistant
Creating a minimalist note-taking app using Tauri and Vanilla JavaScript involves several steps, including setting up the Tauri environment, building the frontend, and implementing the backend API for file operations. Below is a comprehensive guide to help you create this application.
Table of Contents
Prerequisites
Project Setup
Frontend Development
Backend Development (Tauri Commands)
File Operations API
Handling First Launch and Vault Selection
Building and Running the App
Complete Code Example
Prerequisites
Before you begin, ensure you have the following installed on your system:
Tauri allows you to define backend commands in Rust that can be invoked from the frontend.
src-tauri/src/main.rs
Modify your main.rs to include commands for file operations.
rust
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::Manager;
use std::fs::{self, File};
use std::path::PathBuf;
// Command to create a new file
#[tauri::command]
fn create_file(vault_path: String, file_name: String) -> Result<(), String> {
let mut path = PathBuf::from(vault_path);
path.push(file_name);
// Check if file already exists
if path.exists() {
return Err(format!("File {:?} already exists.", path));
}
// Create the file
File::create(&path).map_err(|e| e.to_string())?;
Ok(())
}
// Additional commands for modifying and deleting can be added here
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![create_file])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
File Operations API
Extend the backend to include commands for modifying and deleting files.
src-tauri/src/main.rs (continued)
rust
use tauri::command;
use std::fs;
use std::io::Write;
// Command to modify a file
#[tauri::command]
fn modify_file(vault_path: String, file_name: String, content: String) -> Result<(), String> {
let mut path = PathBuf::from(vault_path);
path.push(file_name);
fs::write(&path, content).map_err(|e| e.to_string())?;
Ok(())
}
// Command to delete a file
#[tauri::command]
fn delete_file(vault_path: String, file_name: String) -> Result<(), String> {
let mut path = PathBuf::from(vault_path);
path.push(file_name);
if path.exists() {
fs::remove_file(&path).map_err(|e| e.to_string())?;
Ok(())
} else {
Err(format!("File {:?} does not exist.", path))
}
}
Don't forget to register these commands in the invoke_handler:
rust
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![create_file, modify_file, delete_file])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expose Backend Commands to Frontend
Update your frontend to handle modify and delete operations as needed, using invoke.
javascript
// Example: Modify a file
async function modifyNote(fileName, newContent) {
await invoke('modify_file', { path: vaultPath, fileName, content: newContent });
}
// Example: Delete a file
async function deleteNote(fileName) {
await invoke('delete_file', { path: vaultPath, fileName });
}
Handling First Launch and Vault Selection
The frontend code provided earlier handles the first launch by checking if a config.json exists in the app data directory. If not, it prompts the user to select a folder, which is then saved for future use.
Ensure that the storage.js file correctly reads and writes the configuration.
Building and Running the App
Development Mode
To run the app in development mode:
Start the frontend development server:
bash
cd frontend
npm run dev
In another terminal, run Tauri:
bash
npx tauri dev
Production Build
To build the app for production:
Build the frontend:
bash
cd frontend
npm run build
Build the Tauri app:
bash
npx tauri build
The compiled application will be available in the src-tauri/target/release directory.
Complete Code Example
For convenience, here's a summary of the essential files and their contents.
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::Manager;
use std::fs::{self, File};
use std::path::PathBuf;
// Command to create a new file
#[tauri::command]
fn create_file(vault_path: String, file_name: String) -> Result<(), String> {
let mut path = PathBuf::from(vault_path);
path.push(&file_name);
if path.exists() {
return Err(format!("File {:?} already exists.", path));
}
File::create(&path).map_err(|e| e.to_string())?;
Ok(())
}
// Command to modify a file
#[tauri::command]
fn modify_file(vault_path: String, file_name: String, content: String) -> Result<(), String> {
let mut path = PathBuf::from(vault_path);
path.push(&file_name);
fs::write(&path, content).map_err(|e| e.to_string())?;
Ok(())
}
// Command to delete a file
#[tauri::command]
fn delete_file(vault_path: String, file_name: String) -> Result<(), String> {
let mut path = PathBuf::from(vault_path);
path.push(&file_name);
if path.exists() {
fs::remove_file(&path).map_err(|e| e.to_string())?;
Ok(())
} else {
Err(format!("File {:?} does not exist.", path))
}
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![create_file, modify_file, delete_file])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Conclusion
This guide provides a foundational structure for building a minimalist note-taking app using Tauri and Vanilla JavaScript. You can further enhance the app by adding features such as listing existing notes, editing note content, searching, and more sophisticated UI elements. Tauri’s powerful backend capabilities combined with a simple frontend make it a great choice for building lightweight desktop applications.
If you encounter any issues or need more advanced functionality, refer to the Tauri Documentation for more detailed information.