Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shortcut to insert current date #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions src/editor/block/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export { moveLineDown, moveLineUp, selectAll }
export const insertNewBlockAtCursor = ({ state, dispatch }) => {
if (state.readOnly)
return false

const currentBlock = getActiveNoteBlock(state)
let delimText;
if (currentBlock) {
delimText = `\n∞∞∞${currentBlock.language.name}${currentBlock.language.auto ? "-a" : ""}\n`
} else {
delimText = "\n∞∞∞text-a\n"
}
dispatch(state.replaceSelection(delimText),
dispatch(state.replaceSelection(delimText),
{
scrollIntoView: true,
scrollIntoView: true,
userEvent: "input",
}
)
Expand All @@ -41,7 +41,7 @@ export const addNewBlockAfterCurrent = ({ state, dispatch }) => {
},
selection: EditorSelection.cursor(block.content.to + delimText.length)
}, {
scrollIntoView: true,
scrollIntoView: true,
userEvent: "input",
}))
return true;
Expand Down Expand Up @@ -119,16 +119,16 @@ function nextBlock(state, range) {
}
}

export function gotoNextBlock({state, dispatch}) {
export function gotoNextBlock({ state, dispatch }) {
return moveSel(state, dispatch, range => nextBlock(state, range))
}
export function selectNextBlock({state, dispatch}) {
export function selectNextBlock({ state, dispatch }) {
return extendSel(state, dispatch, range => nextBlock(state, range))
}
export function gotoPreviousBlock({state, dispatch}) {
export function gotoPreviousBlock({ state, dispatch }) {
return moveSel(state, dispatch, range => previousBlock(state, range))
}
export function selectPreviousBlock({state, dispatch}) {
export function selectPreviousBlock({ state, dispatch }) {
return extendSel(state, dispatch, range => previousBlock(state, range))
}

Expand Down Expand Up @@ -194,19 +194,19 @@ function nextParagraph(state, range) {
return EditorSelection.cursor(block.content.to)
}

export function gotoNextParagraph({state, dispatch}) {
export function gotoNextParagraph({ state, dispatch }) {
return moveSel(state, dispatch, range => nextParagraph(state, range))
}

export function selectNextParagraph({state, dispatch}) {
export function selectNextParagraph({ state, dispatch }) {
return extendSel(state, dispatch, range => nextParagraph(state, range))
}

export function gotoPreviousParagraph({state, dispatch}) {
export function gotoPreviousParagraph({ state, dispatch }) {
return moveSel(state, dispatch, range => previousParagraph(state, range))
}

export function selectPreviousParagraph({state, dispatch}) {
export function selectPreviousParagraph({ state, dispatch }) {
return extendSel(state, dispatch, range => previousParagraph(state, range))
}

Expand All @@ -220,7 +220,7 @@ function newCursor(view, below) {
let range = ranges[i]
let newRange = view.moveVertically(range, below)
let exists = false
for (let j=0; j < ranges.length; j++) {
for (let j = 0; j < ranges.length; j++) {
if (newRange.eq(ranges[j])) {
exists = true
break
Expand All @@ -231,7 +231,7 @@ function newCursor(view, below) {
}
}
const newSelection = EditorSelection.create(newRanges, sel.mainIndex)
view.dispatch({selection: newSelection})
view.dispatch({ selection: newSelection })
}

export function newCursorBelow(view) {
Expand All @@ -246,7 +246,26 @@ export function triggerCurrenciesLoaded(state, dispatch) {
// Trigger empty change transaction that is annotated with CURRENCIES_LOADED
// This will make Math blocks re-render so that currency conversions are applied
dispatch(state.update({
changes:{from: 0, to: 0, insert:""},
changes: { from: 0, to: 0, insert: "" },
annotations: [heynoteEvent.of(CURRENCIES_LOADED)],
}))
}

export const insertDate = ({ state, dispatch }) => {
if (state.readOnly)
return false

const currentBlock = getActiveNoteBlock(state)
let dateText;
if (currentBlock) {
dateText = new Date().toLocaleDateString();
}
dispatch(state.replaceSelection(dateText),
{
scrollIntoView: true,
userEvent: "input",
}
)

return true;
}
28 changes: 15 additions & 13 deletions src/editor/keymap.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { keymap } from "@codemirror/view"
//import { EditorSelection, EditorState } from "@codemirror/state"
import {
indentLess, indentMore,
indentLess, indentMore,
} from "@codemirror/commands"

import {
insertNewBlockAtCursor,
addNewBlockAfterCurrent,
moveLineUp, moveLineDown,
selectAll,
gotoPreviousBlock, gotoNextBlock,
import {
insertNewBlockAtCursor,
addNewBlockAfterCurrent,
moveLineUp, moveLineDown,
selectAll,
gotoPreviousBlock, gotoNextBlock,
selectNextBlock, selectPreviousBlock,
gotoPreviousParagraph, gotoNextParagraph,
gotoPreviousParagraph, gotoNextParagraph,
selectNextParagraph, selectPreviousParagraph,
newCursorBelow, newCursorAbove,
insertDate,
} from "./block/commands.js"

import { formatBlockContent } from "./block/format-code.js"
Expand All @@ -22,7 +23,7 @@ import { formatBlockContent } from "./block/format-code.js"
export function keymapFromSpec(specs) {
return keymap.of(specs.map((spec) => {
if (spec.run) {
return {...spec, preventDefault: true}
return { ...spec, preventDefault: true }
} else {
const [key, run] = spec
return {
Expand All @@ -47,9 +48,10 @@ export function heynoteKeymap(editor) {
["Alt-Shift-f", formatBlockContent],
["Mod-Alt-ArrowDown", newCursorBelow],
["Mod-Alt-ArrowUp", newCursorAbove],
{key:"Mod-ArrowUp", run:gotoPreviousBlock, shift:selectPreviousBlock},
{key:"Mod-ArrowDown", run:gotoNextBlock, shift:selectNextBlock},
{key:"Ctrl-ArrowUp", run:gotoPreviousParagraph, shift:selectPreviousParagraph},
{key:"Ctrl-ArrowDown", run:gotoNextParagraph, shift:selectNextParagraph},
["F5", insertDate],
{ key: "Mod-ArrowUp", run: gotoPreviousBlock, shift: selectPreviousBlock },
{ key: "Mod-ArrowDown", run: gotoNextBlock, shift: selectNextBlock },
{ key: "Ctrl-ArrowUp", run: gotoPreviousParagraph, shift: selectPreviousParagraph },
{ key: "Ctrl-ArrowDown", run: gotoNextParagraph, shift: selectNextParagraph },
])
}