Skip to content

Commit

Permalink
Add functionality for creating new cursors using Cmd/Ctrl + Alt + Up/…
Browse files Browse the repository at this point in the history
…Down
  • Loading branch information
heyman committed Mar 13, 2023
1 parent 9015986 commit 1c78aca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/editor/block/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,36 @@ export function gotoPreviousParagraph({state, dispatch}) {

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


function newCursor(view, below) {
const sel = view.state.selection
const ranges = sel.ranges

const newRanges = [...ranges]
for (let i = 0; i < ranges.length; i++) {
let range = ranges[i]
let newRange = view.moveVertically(range, below)
let exists = false
for (let j=0; j < ranges.length; j++) {
if (newRange.eq(ranges[j])) {
exists = true
break
}
}
if (!exists) {
newRanges.push(newRange)
}
}
const newSelection = EditorSelection.create(newRanges, sel.mainIndex)
view.dispatch({selection: newSelection})
}

export function newCursorBelow(view) {
newCursor(view, true)
}

export function newCursorAbove(view) {
newCursor(view, false)
}
3 changes: 3 additions & 0 deletions src/editor/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
selectNextBlock, selectPreviousBlock,
gotoPreviousParagraph, gotoNextParagraph,
selectNextParagraph, selectPreviousParagraph,
newCursorBelow, newCursorAbove,
} from "./block/commands.js"

import { formatBlockContent } from "./block/format-code.js"
Expand Down Expand Up @@ -44,6 +45,8 @@ export function heynoteKeymap(editor) {
["Alt-ArrowDown", moveLineDown],
["Mod-l", () => editor.openLanguageSelector()],
["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},
Expand Down

0 comments on commit 1c78aca

Please sign in to comment.