Skip to content

Commit

Permalink
Add support for creating new empty note buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Dec 4, 2024
1 parent e51d094 commit 84b4fce
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/components/NewNote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
...mapState(useNotesStore, [
"notes",
"currentNotePath",
"createNoteMode",
]),
currentNoteDirectory() {
Expand All @@ -78,12 +79,17 @@
"name-input": true,
"error": this.errors.name,
}
}
},
dialogTitle() {
return this.createNoteMode === "currentBlock" ? "New Note from Block" : "New Note"
},
},
methods: {
...mapActions(useNotesStore, [
"updateNotes",
"createNewNote",
"createNewNoteFromActiveBlock",
]),
Expand Down Expand Up @@ -141,7 +147,14 @@
return
}
console.log("Creating note", path)
this.createNewNoteFromActiveBlock(path, this.name)
if (this.createNoteMode === "currentBlock") {
this.createNewNoteFromActiveBlock(path, this.name)
} else if (this.createNoteMode === "new") {
this.createNewNote(path, this.name)
} else {
throw new Error("Unknown createNoteMode: " + this.createNoteMode)
}
this.$emit("close")
//this.$emit("create", this.$refs.input.value)
},
Expand All @@ -153,7 +166,7 @@
<div class="fader" @keydown="onKeydown" tabindex="-1">
<form class="new-note" tabindex="-1" @focusout="onFocusOut" ref="container" @submit.prevent="submit">
<div class="container">
<h1>New Note from Block</h1>
<h1>{{ dialogTitle }}</h1>
<input
placeholder="Name"
type="text"
Expand Down
16 changes: 14 additions & 2 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,20 @@ export class HeynoteEditor {
this.notesStore.openNoteSelector()
}

openCreateNote() {
this.notesStore.openCreateNote(this)
openCreateNote(createMode) {
this.notesStore.openCreateNote(createMode)
}

async createNewNote(path, name) {
const data = getBlockDelimiter(this.defaultBlockToken, this.defaultBlockAutoDetect)
await this.notesStore.saveNewNote(path, name, data)

// by using requestAnimationFrame we avoid a race condition where rendering the block backgrounds
// would fail if we immediately opened the new note (since the block UI wouldn't have time to update
// after the block was deleted)
requestAnimationFrame(() => {
this.notesStore.openNote(path)
})
}

async createNewNoteFromActiveBlock(path, name) {
Expand Down
3 changes: 2 additions & 1 deletion src/editor/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export function heynoteKeymap(editor) {
["Alt-ArrowDown", moveLineDown],
["Mod-l", () => editor.openLanguageSelector()],
["Mod-p", () => editor.openNoteSelector()],
["Mod-s", () => editor.openCreateNote()],
["Mod-s", () => editor.openCreateNote("currentBlock")],
["Mod-n", () => editor.openCreateNote("new")],
["Mod-Shift-d", deleteBlock(editor)],
["Alt-Shift-f", formatBlockContent],
["Mod-Alt-ArrowDown", newCursorBelow],
Expand Down
15 changes: 13 additions & 2 deletions src/stores/notes-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const useNotesStore = defineStore("notes", {
currentCursorLine: null,
currentSelectionSize: null,
libraryId: 0,
createNoteMode: "new",

showNoteSelector: false,
showLanguageSelector: false,
Expand Down Expand Up @@ -51,8 +52,10 @@ export const useNotesStore = defineStore("notes", {
this.closeDialog()
this.showNoteSelector = true
},
openCreateNote() {
openCreateNote(createMode) {
createMode = createMode || "new"
this.closeDialog()
this.createNoteMode = createMode
this.showCreateNote = true
},
closeDialog() {
Expand All @@ -75,12 +78,20 @@ export const useNotesStore = defineStore("notes", {
},

/**
* Create a new note file at `path` with name `name` from the current block of the current open editor
* Create a new note file at `path` with name `name` from the current block of the current open editor,
* and switch to it
*/
async createNewNoteFromActiveBlock(path, name) {
await toRaw(this.currentEditor).createNewNoteFromActiveBlock(path, name)
},

/**
* Create a new empty note file at `path` with name `name`, and switch to it
*/
async createNewNote(path, name) {
await toRaw(this.currentEditor).createNewNote(path, name)
},

/**
* Create a new note file at path, with name `name`, and content content
* @param {*} path: File path relative to Heynote root
Expand Down
30 changes: 29 additions & 1 deletion tests/buffer-creation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ test("create new buffer from block", async ({page}) => {
const defaultBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("scratch.txt"))
const newBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("my-new-buffer.txt"))


expect(defaultBuffer.content).toBe(`
∞∞∞text
Block A
Expand All @@ -67,3 +66,32 @@ Block C
New buffer content`)

})


test("create new empty note", async ({page}) => {
await page.locator("body").press("Enter")
await page.locator("body").press("Backspace")
await page.locator("body").press(heynotePage.agnosticKey("Mod+N"))
await page.locator("body").pressSequentially("New Empty Buffer")
await page.locator("body").press("Enter")
await page.waitForTimeout(AUTO_SAVE_INTERVAL + 50);

const buffers = Object.keys(await heynotePage.getStoredBufferList())
expect(buffers).toContain("scratch.txt")
expect(buffers).toContain("new-empty-buffer.txt")

const defaultBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("scratch.txt"))
const newBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("new-empty-buffer.txt"))

expect(defaultBuffer.content).toBe(`
∞∞∞text
Block A
∞∞∞text
Block B
∞∞∞text
Block C`)

expect(newBuffer.content).toBe(`
∞∞∞text-a
`)
})

0 comments on commit 84b4fce

Please sign in to comment.