diff --git a/src/common/constants.js b/src/common/constants.js index 42c1766..a1359aa 100644 --- a/src/common/constants.js +++ b/src/common/constants.js @@ -1 +1,2 @@ export const SCRATCH_FILE_NAME = "scratch.txt" +export const AUTO_SAVE_INTERVAL = 2000 diff --git a/src/editor/block/commands.js b/src/editor/block/commands.js index c59ce51..fe9bf3b 100644 --- a/src/editor/block/commands.js +++ b/src/editor/block/commands.js @@ -1,4 +1,5 @@ import { EditorSelection, Transaction } from "@codemirror/state" + import { heynoteEvent, LANGUAGE_CHANGE, CURRENCIES_LOADED, ADD_NEW_BLOCK, DELETE_BLOCK } from "../annotation.js"; import { blockState, getActiveNoteBlock, getFirstNoteBlock, getLastNoteBlock, getNoteBlockFromPos } from "./block" import { moveLineDown, moveLineUp } from "./move-lines.js"; diff --git a/src/editor/editor.js b/src/editor/editor.js index 4a3f3ac..0199c14 100644 --- a/src/editor/editor.js +++ b/src/editor/editor.js @@ -22,6 +22,7 @@ import { autoSaveContent } from "./save.js" import { todoCheckboxPlugin} from "./todo-checkbox.ts" import { links } from "./links.js" import { NoteFormat } from "../common/note-format.js" +import { AUTO_SAVE_INTERVAL } from "../common/constants.js" import { useNotesStore } from "../stores/notes-store.js"; import { useErrorStore } from "../stores/error-store.js"; @@ -105,7 +106,7 @@ export class HeynoteEditor { return {class: view.state.facet(EditorView.darkTheme) ? "dark-theme" : "light-theme"} }), - autoSaveContent(this, 2000), + autoSaveContent(this, AUTO_SAVE_INTERVAL), todoCheckboxPlugin, markdown(), diff --git a/tests/buffer-creation.spec.js b/tests/buffer-creation.spec.js new file mode 100644 index 0000000..a42705c --- /dev/null +++ b/tests/buffer-creation.spec.js @@ -0,0 +1,69 @@ +import {expect, test} from "@playwright/test"; +import {HeynotePage} from "./test-utils.js"; + +import { AUTO_SAVE_INTERVAL } from "../src/common/constants.js" +import { NoteFormat } from "../src/common/note-format.js" +import exp from "constants"; + +let heynotePage + +test.beforeEach(async ({page}) => { + heynotePage = new HeynotePage(page) + await heynotePage.goto() + + expect((await heynotePage.getBlocks()).length).toBe(1) + await heynotePage.setContent(` +∞∞∞text +Block A +∞∞∞text +Block B +∞∞∞text +Block C`) + await page.waitForTimeout(100); + // check that blocks are created + expect((await heynotePage.getBlocks()).length).toBe(3) + + // check that visual block layers are created + await expect(page.locator("css=.heynote-blocks-layer > div")).toHaveCount(3) +}); + + +test("default buffer saved", async ({page}) => { + // make some change and make sure content is auto saved in default scratch buffer + await page.locator("body").pressSequentially("YAY") + await page.waitForTimeout(AUTO_SAVE_INTERVAL + 50); + const bufferList = await heynotePage.getStoredBufferList() + expect(Object.keys(bufferList).length).toBe(1) + expect(bufferList["scratch.txt"]).toBeTruthy() +}) + +test("create new buffer from block", async ({page}) => { + await page.locator("body").press(heynotePage.agnosticKey("Mod+S")) + await page.waitForTimeout(50) + await page.locator("body").pressSequentially("My New Buffer") + await page.locator("body").press("Enter") + await page.waitForTimeout(50) + await page.locator("body").press("Enter") + await page.locator("body").pressSequentially("New buffer content") + await page.waitForTimeout(AUTO_SAVE_INTERVAL + 50); + + const buffers = Object.keys(await heynotePage.getStoredBufferList()) + expect(buffers).toContain("scratch.txt") + expect(buffers).toContain("my-new-buffer.txt") + + 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 +∞∞∞text +Block B`) + + expect(newBuffer.content).toBe(` +∞∞∞text +Block C +New buffer content`) + +}) diff --git a/tests/test-utils.js b/tests/test-utils.js index aa59fb6..209701a 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -56,4 +56,16 @@ export class HeynotePage { async getStoredSettings() { return await this.page.evaluate(() => JSON.parse(window.localStorage.getItem("settings"))) } + + async getStoredBufferList() { + return await this.page.evaluate(() => window.heynote.buffer.getList()) + } + + async getStoredBuffer(path) { + return await this.page.evaluate((path) => window.heynote.buffer.load(path), path) + } + + agnosticKey(key) { + return key.replace("Mod", this.isMac ? "Meta" : "Control") + } } diff --git a/webapp/bridge.js b/webapp/bridge.js index 0d8adb3..48bf02e 100644 --- a/webapp/bridge.js +++ b/webapp/bridge.js @@ -2,6 +2,8 @@ import { Exception } from "sass"; import { SETTINGS_CHANGE_EVENT, OPEN_SETTINGS_EVENT } from "../electron/constants"; import { NoteFormat } from "../src/common/note-format"; +const NOTE_KEY_PREFIX = "heynote-library__" + const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)') let themeCallback = null mediaMatch.addEventListener("change", async (event) => { @@ -75,9 +77,6 @@ if (settingsData !== null) { initialSettings = Object.assign(initialSettings, JSON.parse(settingsData)) } - -const NOTE_KEY_PREFIX = "heynote-library__" - function noteKey(path) { return NOTE_KEY_PREFIX + path } @@ -161,7 +160,7 @@ const Heynote = { return localStorage.getItem(noteKey(path)) !== null }, - async getList(path) { + async getList() { //return {"scratch.txt": {name:"Scratch"}} const notes = {} for (let [key, content] of Object.entries(localStorage)) {