Skip to content

Commit

Permalink
WIP: Multiple notes support
Browse files Browse the repository at this point in the history
Add support for migrating old buffer file to new library.

Add support for changing location for the notes library.

Replace theme toggle in status bar with a dropdown in Appearance settings.

Improve New Note and Update Note dialogs.

Implement UI for confirming note delete (the actualal deltion is still to be implemented).
  • Loading branch information
heyman committed Sep 10, 2024
1 parent 8807223 commit c010df0
Show file tree
Hide file tree
Showing 24 changed files with 338 additions and 266 deletions.
1 change: 1 addition & 0 deletions assets/icons/arrow-right-black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
1 change: 1 addition & 0 deletions assets/icons/arrow-right-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Available for Mac, Windows, and Linux.
⌥ + Shift + Enter Add new block at the start of the buffer
⌘ + ⌥ + Enter Split the current block at cursor position
⌘ + L Change block language
⌘ + S Create a new note from the current block
⌘ + P Open note selector
⌘ + Down Goto next block
⌘ + Up Goto previous block
⌘ + A Select all text in a note block. Press again to select the whole buffer
Expand All @@ -52,6 +54,8 @@ Ctrl + Shift + Enter Add new block at the end of the buffer
Alt + Shift + Enter Add new block at the start of the buffer
Ctrl + Alt + Enter Split the current block at cursor position
Ctrl + L Change block language
Ctrl + S Create a new note from the current block
Ctrl + P Open note selector
Ctrl + Down Goto next block
Ctrl + Up Goto previous block
Ctrl + A Select all text in a note block. Press again to select the whole buffer
Expand Down
3 changes: 1 addition & 2 deletions electron/initial-content.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os from "os";
import { keyHelpStr } from "../shared-utils/key-helper";

export const eraseInitialContent = !!process.env.ERASE_INITIAL_CONTENT

export const initialContent = `
{"formatVersion":"1.0.0","name":"Scratch"}
∞∞∞markdown
Welcome to Heynote! 👋
Expand Down
172 changes: 0 additions & 172 deletions electron/main/buffer.js

This file was deleted.

92 changes: 88 additions & 4 deletions electron/main/file-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { join, dirname, basename } from "path"
import * as jetpack from "fs-jetpack";
import { app, ipcMain, dialog } from "electron"

import CONFIG from "../config"
import { SCRATCH_FILE_NAME } from "../../src/common/constants"
import { NoteFormat } from "../../src/common/note-format"
import { isDev } from '../detect-platform';
import { initialContent, initialDevContent } from '../initial-content'

export const NOTES_DIR_NAME = isDev ? "notes-dev" : "notes"


let library

const untildify = (pathWithTilde) => {
const homeDir = os.homedir()
Expand Down Expand Up @@ -42,6 +52,11 @@ export class FileLibrary {
this.watcher = null;
this.contentSaved = false
this.onChangeCallback = null

// create scratch.txt if it doesn't exist
if (!this.jetpack.exists(SCRATCH_FILE_NAME)) {
this.jetpack.write(SCRATCH_FILE_NAME, isDev ? initialDevContent : initialContent)
}
}

async exists(path) {
Expand Down Expand Up @@ -82,7 +97,7 @@ export class FileLibrary {
}

async getList() {
console.log("Loading notes")
//console.log("Listing notes")
const notes = {}
const files = await this.jetpack.findAsync(".", {
matching: "*.txt",
Expand Down Expand Up @@ -199,10 +214,13 @@ export class NoteBuffer {
}
}

export function setCurrentFileLibrary(lib) {
library = lib
}

export function setupFileLibraryEventHandlers(library, win) {
export function setupFileLibraryEventHandlers(win) {
ipcMain.handle('buffer:load', async (event, path) => {
console.log("buffer:load", path)
//console.log("buffer:load", path)
return await library.load(path)
});

Expand Down Expand Up @@ -244,5 +262,71 @@ export function setupFileLibraryEventHandlers(library, win) {
return await library.move(path, newPath)
});

library.setupWatcher(win)
ipcMain.handle("library:selectLocation", async () => {
let result = await dialog.showOpenDialog({
title: "Select directory to store buffer",
properties: [
"openDirectory",
"createDirectory",
"noResolveAliases",
],
})
if (result.canceled) {
return
}
const filePath = result.filePaths[0]
return filePath
})
}


export async function migrateBufferFileToLibrary(app) {
async function ensureBufferFileMetadata(filePath) {
const metadata = await readNoteMetadata(filePath)
//console.log("Metadata", metadata)
if (!metadata || !metadata.name) {
console.log("Adding metadata to", filePath)
const note = NoteFormat.load(jetpack.read(filePath))
note.metadata.name = "Scratch"
jetpack.write(filePath, note.serialize())
} else {
console.log("Metadata already exists for", filePath)
}
}

const defaultLibraryPath = join(app.getPath("userData"), NOTES_DIR_NAME)
const customBufferPath = CONFIG.get("settings.bufferPath")
const oldBufferFile = isDev ? "buffer-dev.txt" : "buffer.txt"
if (customBufferPath) {
// if the new buffer file exists, no need to migrate
if (jetpack.exists(join(customBufferPath, SCRATCH_FILE_NAME)) === "file") {
return
}
const oldBufferFileFullPath = join(customBufferPath, oldBufferFile)
if (jetpack.exists(oldBufferFileFullPath) === "file") {
const newFileFullPath = join(customBufferPath, SCRATCH_FILE_NAME);
console.log(`Migrating file ${oldBufferFileFullPath} to ${newFileFullPath}`)
// rename buffer file to scratch.txt
jetpack.move(oldBufferFileFullPath, newFileFullPath)
// add metadata to scratch.txt (just to be sure, we'll double check that it's needed first)
await ensureBufferFileMetadata(newFileFullPath)
}
} else {
// if the new buffer file exists, no need to migrate
if (jetpack.exists(join(defaultLibraryPath, SCRATCH_FILE_NAME)) === "file") {
return
}
// check if the old buffer file exists, while the default *library* path doesn't exist
const oldBufferFileFullPath = join(app.getPath("userData"), oldBufferFile)
if (jetpack.exists(oldBufferFileFullPath) === "file" && jetpack.exists(defaultLibraryPath) !== "dir") {
const newFileFullPath = join(defaultLibraryPath, SCRATCH_FILE_NAME);
console.log(`Migrating buffer file ${oldBufferFileFullPath} to ${newFileFullPath}`)
// create the default library path
jetpack.dir(defaultLibraryPath)
// move the buffer file to the library path
jetpack.move(oldBufferFileFullPath, newFileFullPath)
// add metadata to scratch.txt
await ensureBufferFileMetadata(newFileFullPath)
}
}
}
Loading

0 comments on commit c010df0

Please sign in to comment.