Add support for creating new empty note buffers

This commit is contained in:
Jonatan Heyman 2024-12-04 20:58:24 +01:00
parent d54a3ba633
commit 9c057b120a
5 changed files with 74 additions and 9 deletions

View File

@ -67,6 +67,7 @@
...mapState(useNotesStore, [
"notes",
"currentNotePath",
"createNoteMode",
]),
currentNoteDirectory() {
@ -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",
]),
@ -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)
},
@ -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"

View File

@ -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) {

View File

@ -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],

View File

@ -18,6 +18,7 @@ export const useNotesStore = defineStore("notes", {
currentCursorLine: null,
currentSelectionSize: null,
libraryId: 0,
createNoteMode: "new",
showNoteSelector: false,
showLanguageSelector: false,
@ -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() {
@ -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

View File

@ -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
@ -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
`)
})