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, [ ...mapState(useNotesStore, [
"notes", "notes",
"currentNotePath", "currentNotePath",
"createNoteMode",
]), ]),
currentNoteDirectory() { currentNoteDirectory() {
@ -78,12 +79,17 @@
"name-input": true, "name-input": true,
"error": this.errors.name, "error": this.errors.name,
} }
} },
dialogTitle() {
return this.createNoteMode === "currentBlock" ? "New Note from Block" : "New Note"
},
}, },
methods: { methods: {
...mapActions(useNotesStore, [ ...mapActions(useNotesStore, [
"updateNotes", "updateNotes",
"createNewNote",
"createNewNoteFromActiveBlock", "createNewNoteFromActiveBlock",
]), ]),
@ -141,7 +147,14 @@
return return
} }
console.log("Creating note", path) console.log("Creating note", path)
if (this.createNoteMode === "currentBlock") {
this.createNewNoteFromActiveBlock(path, this.name) 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("close")
//this.$emit("create", this.$refs.input.value) //this.$emit("create", this.$refs.input.value)
}, },
@ -153,7 +166,7 @@
<div class="fader" @keydown="onKeydown" tabindex="-1"> <div class="fader" @keydown="onKeydown" tabindex="-1">
<form class="new-note" tabindex="-1" @focusout="onFocusOut" ref="container" @submit.prevent="submit"> <form class="new-note" tabindex="-1" @focusout="onFocusOut" ref="container" @submit.prevent="submit">
<div class="container"> <div class="container">
<h1>New Note from Block</h1> <h1>{{ dialogTitle }}</h1>
<input <input
placeholder="Name" placeholder="Name"
type="text" type="text"

View File

@ -273,8 +273,20 @@ export class HeynoteEditor {
this.notesStore.openNoteSelector() this.notesStore.openNoteSelector()
} }
openCreateNote() { openCreateNote(createMode) {
this.notesStore.openCreateNote(this) 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) { async createNewNoteFromActiveBlock(path, name) {

View File

@ -59,7 +59,8 @@ export function heynoteKeymap(editor) {
["Alt-ArrowDown", moveLineDown], ["Alt-ArrowDown", moveLineDown],
["Mod-l", () => editor.openLanguageSelector()], ["Mod-l", () => editor.openLanguageSelector()],
["Mod-p", () => editor.openNoteSelector()], ["Mod-p", () => editor.openNoteSelector()],
["Mod-s", () => editor.openCreateNote()], ["Mod-s", () => editor.openCreateNote("currentBlock")],
["Mod-n", () => editor.openCreateNote("new")],
["Mod-Shift-d", deleteBlock(editor)], ["Mod-Shift-d", deleteBlock(editor)],
["Alt-Shift-f", formatBlockContent], ["Alt-Shift-f", formatBlockContent],
["Mod-Alt-ArrowDown", newCursorBelow], ["Mod-Alt-ArrowDown", newCursorBelow],

View File

@ -18,6 +18,7 @@ export const useNotesStore = defineStore("notes", {
currentCursorLine: null, currentCursorLine: null,
currentSelectionSize: null, currentSelectionSize: null,
libraryId: 0, libraryId: 0,
createNoteMode: "new",
showNoteSelector: false, showNoteSelector: false,
showLanguageSelector: false, showLanguageSelector: false,
@ -51,8 +52,10 @@ export const useNotesStore = defineStore("notes", {
this.closeDialog() this.closeDialog()
this.showNoteSelector = true this.showNoteSelector = true
}, },
openCreateNote() { openCreateNote(createMode) {
createMode = createMode || "new"
this.closeDialog() this.closeDialog()
this.createNoteMode = createMode
this.showCreateNote = true this.showCreateNote = true
}, },
closeDialog() { 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) { async createNewNoteFromActiveBlock(path, name) {
await toRaw(this.currentEditor).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 * Create a new note file at path, with name `name`, and content content
* @param {*} path: File path relative to Heynote root * @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 defaultBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("scratch.txt"))
const newBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("my-new-buffer.txt")) const newBuffer = NoteFormat.load(await heynotePage.getStoredBuffer("my-new-buffer.txt"))
expect(defaultBuffer.content).toBe(` expect(defaultBuffer.content).toBe(`
text text
Block A Block A
@ -67,3 +66,32 @@ Block C
New buffer content`) 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
`)
})