diff --git a/docs/changelog.md b/docs/changelog.md index a6bad58..8af5308 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -10,6 +10,7 @@ Here are the most notable changes in each release. For a more detailed list of c - Added a "command palette" that can be accessed by pressing `Ctrl/Cmd+Shift+P`, or just typing `>` in the buffer selector. The command palette allows you to discover all available commands in the app, and to quickly execute them. - Added support for configuring the tab size. - Added functionality for moving blocks up and down. Default key bindings are `Ctrl/Cmd+Alt+Shift+Up` and `Ctrl/Cmd+Alt+Shift+Down`. +- Added functionality for inserting the current date and time. Default key binding is `Alt+Shift+D`. ### Other changes diff --git a/src/editor/commands.js b/src/editor/commands.js index 1d03c8a..30d241d 100644 --- a/src/editor/commands.js +++ b/src/editor/commands.js @@ -31,6 +31,7 @@ import { transposeChars } from "./block/transpose-chars.js" import { cutCommand, copyCommand, pasteCommand } from "./copy-paste.js" import { markModeMoveCommand, toggleSelectionMarkMode, selectionMarkModeCancel } from "./mark-mode.js" +import { insertDateAndTime } from "./date-time.js" const cursorPreviousBlock = markModeMoveCommand(gotoPreviousBlock, selectPreviousBlock) @@ -116,6 +117,7 @@ const HEYNOTE_COMMANDS = { selectPreviousBlock: cmdLessContext(selectPreviousBlock, "Selection", "Select to previous block"), selectNextBlock: cmdLessContext(selectNextBlock, "Selection", "Select to next block"), nothing: cmdLessContext(nothing, "Misc", "Do nothing"), + insertDateAndTime: cmdLessContext(insertDateAndTime, "Misc", "Insert date and time"), // directly from CodeMirror undo: cmdLessContext(undo, "Edit", "Undo"), diff --git a/src/editor/date-time.js b/src/editor/date-time.js new file mode 100644 index 0000000..0457bac --- /dev/null +++ b/src/editor/date-time.js @@ -0,0 +1,20 @@ +export const insertDateAndTime = ({ state, dispatch }) => { + if (state.readOnly) { + return false + } + + const dateText = new Date().toLocaleString(undefined, { + hour: '2-digit', + minute: '2-digit', + year: 'numeric', + month: 'long', + day: 'numeric', + }) + dispatch(state.replaceSelection(dateText), + { + scrollIntoView: true, + userEvent: "input", + } + ) + return true; +} diff --git a/src/editor/keymap.js b/src/editor/keymap.js index a396dc9..29df880 100644 --- a/src/editor/keymap.js +++ b/src/editor/keymap.js @@ -42,6 +42,7 @@ export const DEFAULT_KEYMAP = [ ...cmdShift("End", "cursorLineBoundaryForward", "selectLineBoundaryForward"), cmd("Alt-Mod-Shift-ArrowUp", "moveCurrentBlockUp"), cmd("Alt-Mod-Shift-ArrowDown", "moveCurrentBlockDown"), + cmd("Alt-Shift-d", "insertDateAndTime"), cmd("Backspace", "deleteCharBackward"), cmd("Delete", "deleteCharForward"), cmd("Escape", "simplifySelection"), diff --git a/tests/basic-editing.spec.js b/tests/basic-editing.spec.js index b5bdff5..57f7ff5 100644 --- a/tests/basic-editing.spec.js +++ b/tests/basic-editing.spec.js @@ -37,3 +37,12 @@ test("first block is protected", async ({ page }) => { expect(await heynotePage.getBlockContent(0)).toBe("") expect(await heynotePage.getContent()).toBe(initialContent) }) + +test("insert current date and time", async ({ page }) => { + const expectedYear = new Date().toLocaleString(undefined, { + year: 'numeric', + }) + await page.locator("body").press("Alt+Shift+D") + expect(await heynotePage.getBlockContent(0)).toContain(expectedYear) + expect((await heynotePage.getBlockContent(0)).length).toBeGreaterThan(0) +})