Merge pull request #326 from heyman/feature-insert-current-datetime

Add functionality for inserting the current date and time
This commit is contained in:
Jonatan Heyman 2025-04-22 18:38:24 +02:00 committed by GitHub
commit 33e65a6127
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 0 deletions

View File

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

View File

@ -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"),

20
src/editor/date-time.js Normal file
View File

@ -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;
}

View File

@ -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"),

View File

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