Sure! Let's build a minimalist note-taking app using Tauri v1 and Vanilla JavaScript. The app will:
- Prompt the user to select a folder upon the first launch to store and access files (similar to Obsidian vaults).
- Display a blank page where clicking anywhere creates a new
.txt note.
- Provide a clean API for creating, modifying, and deleting files.
We'll use npm instead of cargo, so we'll avoid Rust code as per your request.
Prerequisites
-
Node.js and npm installed.
-
Tauri CLI installed globally:
npm install -g @tauri-apps/cli
Step 1: Initialize the Project
Create a new directory for your app and initialize a new npm project.
mkdir minimalist-note-app
cd minimalist-note-app
npm init -y
Step 2: Install Tauri Dependencies
Install Tauri and its API package.
npm install @tauri-apps/cli @tauri-apps/api
Step 3: Initialize Tauri
Initialize Tauri in your project.
npx tauri init
- When prompted, select:
- Package Name: (your choice)
- Frontend Framework: None
- Vanilla instead of Rust: Yes
Step 4: Project Structure
Your project should now have the following structure:
minimalist-note-app
├── package.json
├── src
│ ├── index.html
│ └── main.js
└── src-tauri
├── Cargo.toml
├── src
│ └── main.rs
└── tauri.conf.json
Step 5: Configure Tauri
Modify tauri.conf.json to point to your src directory.
"build": {
"beforeBuildCommand": "",
"beforeDevCommand": "",
"devPath": "../src",
"distDir": "../src"
},
Step 6: Create the Frontend
6.1. index.html
In the src directory, create or modify index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Minimalist Note App</title>
<style>
html, body {
margin: 0;
padding: 0;
cursor: pointer;
height: 100%;
width: 100%;
background-color: #f5f5f5;
}
</style>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
6.2. main.js
Create main.js in the src directory:
// main.js
const { appDir } = window.__TAURI__.path;
const { readTextFile, writeFile, removeFile, createDir, exists } = window.__TAURI__.fs;
const { open } = window.__TAURI__.dialog;
const { invoke } = window.__TAURI__.tauri;
let vaultPath = null;
async function selectVault() {
try {
const selected = await open({
directory: true,
multiple: false,
title: 'Select a folder to store your notes'
});
if (selected === null) {
// User canceled the dialog
await invoke('close_splashscreen'); // Close the app
} else {
vaultPath = selected;
localStorage.setItem('vaultPath', vaultPath);
}
} catch (error) {
console.error('Error selecting vault:', error);
}
}
async function getVaultPath() {
vaultPath = localStorage.getItem('vaultPath');
if (!vaultPath) {
await selectVault();
}
}
function generateFileName() {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
return `note-${timestamp}.txt`;
}
async function createNote() {
const fileName = generateFileName();
const filePath = `${vaultPath}/${fileName}`;
const content = prompt('Enter your note:');
if (content !== null) {
try {
await writeFile({ path: filePath, contents: content });
alert('Note saved!');
} catch (error) {
console.error('Error saving note:', error);
}
}
}
async function init() {
await getVaultPath();
document.body.addEventListener('click', (event) => {
createNote();
});
}
init();
Step 7: Handle File Operations
Since Tauri uses Rust internally for file operations, but we're avoiding writing Rust code, we'll use the Tauri JS APIs provided by @tauri-apps/api.
- Select Folder: We use
window.__TAURI__.dialog.open to open a folder selection dialog.
- File Operations: We use
window.__TAURI__.fs methods for reading, writing, and deleting files.
- Path Handling: Combine paths using normal string concatenation.
Step 8: Modify Rust Code (Optional)
Even though you requested to avoid Rust code, we might need to ensure the app closes properly if the user cancels the vault selection.
In src-tauri/src/main.rs, ensure the splashscreen closes correctly:
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![close_splashscreen])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
#[tauri::command]
fn close_splashscreen(window: tauri::Window) {
window.close().unwrap();
}
However, if you're strictly avoiding Rust code, you can skip this step, but the app might not close smoothly if the vault selection is canceled.
Step 9: Run the App
Start the Tauri development server:
npm run tauri dev
Step 10: Test the App
-
First Launch:
- The app prompts you to select a folder to store your notes.
- If you cancel, the app will close (may require the optional Rust code from Step 8).
-
Creating Notes:
- Click anywhere on the blank page.
- A prompt appears to enter your note.
- After entering the content, the note is saved as a
.txt file in the selected folder.
-
Subsequent Launches:
- The app remembers the selected folder using
localStorage.
- Clicking creates new notes in the same folder.
Additional Features
To expand the app, consider adding:
Conclusion
You've created a minimalist desktop note-taking app using Tauri and Vanilla JavaScript! By clicking anywhere, users can create new notes, and the app efficiently manages files within the selected folder.
Full Code Listing
src/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Minimalist Note App</title>
<style>
html, body {
margin: 0;
padding: 0;
cursor: pointer;
height: 100%;
width: 100%;
background-color: #f5f5f5;
}
</style>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
src/main.js
// main.js
const { readTextFile, writeFile } = window.__TAURI__.fs;
const { open } = window.__TAURI__.dialog;
let vaultPath = null;
async function selectVault() {
try {
const selected = await open({
directory: true,
multiple: false,
title: 'Select a folder to store your notes'
});
if (selected === null) {
// User canceled the dialog
window.close();
} else {
vaultPath = selected;
localStorage.setItem('vaultPath', vaultPath);
}
} catch (error) {
console.error('Error selecting vault:', error);
}
}
async function getVaultPath() {
vaultPath = localStorage.getItem('vaultPath');
if (!vaultPath) {
await selectVault();
}
}
function generateFileName() {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
return `note-${timestamp}.txt`;
}
async function createNote() {
const fileName = generateFileName();
const filePath = `${vaultPath}/${fileName}`;
const content = prompt('Enter your note:');
if (content !== null) {
try {
await writeFile({ path: filePath, contents: content });
alert('Note saved!');
} catch (error) {
console.error('Error saving note:', error);
}
}
}
async function init() {
await getVaultPath();
document.body.addEventListener('click', (event) => {
createNote();
});
}
init();
Remember, Tauri allows you to write powerful desktop applications using web technologies. Feel free to customize and expand this app to suit your needs!
If you have any questions or need further assistance, feel free to ask.