Add feature for moving the current block up and down

This commit is contained in:
wolimst 2024-02-20 23:25:27 +09:00
parent 387ef33f94
commit 81bbc2ed47
2 changed files with 58 additions and 0 deletions

View File

@ -313,3 +313,58 @@ export function triggerCurrenciesLoaded(state, dispatch) {
annotations: [heynoteEvent.of(CURRENCIES_LOADED)], annotations: [heynoteEvent.of(CURRENCIES_LOADED)],
})) }))
} }
export function moveCurrentBlockUp({state, dispatch}) {
return moveCurrentBlock(state, dispatch, true)
}
export function moveCurrentBlockDown({state, dispatch}) {
return moveCurrentBlock(state, dispatch, false)
}
function moveCurrentBlock(state, dispatch, up) {
if (state.readOnly) {
return false
}
const blocks = state.facet(blockState)
const currentBlock = getActiveNoteBlock(state)
const blockIndex = blocks.indexOf(currentBlock)
if ((up && blockIndex === 0) || (!up && blockIndex === blocks.length - 1)) {
return false
}
const dir = up ? -1 : 1
const neighborBlock = blocks[blockIndex + dir]
const currentBlockContent = state.sliceDoc(currentBlock.delimiter.from, currentBlock.content.to)
const neighborBlockContent = state.sliceDoc(neighborBlock.delimiter.from, neighborBlock.content.to)
const newContent = up ? currentBlockContent + neighborBlockContent : neighborBlockContent + currentBlockContent
const selectionRange = state.selection.asSingle().ranges[0]
let newSelectionRange
if (up) {
newSelectionRange = EditorSelection.range(
selectionRange.anchor - currentBlock.delimiter.from + neighborBlock.delimiter.from,
selectionRange.head - currentBlock.delimiter.from + neighborBlock.delimiter.from,
)
} else {
newSelectionRange = EditorSelection.range(
selectionRange.anchor + neighborBlock.content.to - neighborBlock.delimiter.from,
selectionRange.head + neighborBlock.content.to - neighborBlock.delimiter.from,
)
}
dispatch(state.update({
changes: {
from: up ? neighborBlock.delimiter.from : currentBlock.delimiter.from,
to: up ? currentBlock.content.to : neighborBlock.content.to,
insert: newContent,
},
selection: newSelectionRange,
}, {
scrollIntoView: true,
userEvent: "input",
}))
return true
}

View File

@ -15,6 +15,7 @@ import {
gotoPreviousParagraph, gotoNextParagraph, gotoPreviousParagraph, gotoNextParagraph,
selectNextParagraph, selectPreviousParagraph, selectNextParagraph, selectPreviousParagraph,
newCursorBelow, newCursorAbove, newCursorBelow, newCursorAbove,
moveCurrentBlockUp, moveCurrentBlockDown,
} from "./block/commands.js" } from "./block/commands.js"
import { pasteCommand, copyCommand, cutCommand } from "./copy-paste.js" import { pasteCommand, copyCommand, cutCommand } from "./copy-paste.js"
@ -65,5 +66,7 @@ export function heynoteKeymap(editor) {
{key:"Mod-ArrowDown", run:gotoNextBlock, shift:selectNextBlock}, {key:"Mod-ArrowDown", run:gotoNextBlock, shift:selectNextBlock},
{key:"Ctrl-ArrowUp", run:gotoPreviousParagraph, shift:selectPreviousParagraph}, {key:"Ctrl-ArrowUp", run:gotoPreviousParagraph, shift:selectPreviousParagraph},
{key:"Ctrl-ArrowDown", run:gotoNextParagraph, shift:selectNextParagraph}, {key:"Ctrl-ArrowDown", run:gotoNextParagraph, shift:selectNextParagraph},
["Mod-Shift-Alt-ArrowUp", moveCurrentBlockUp],
["Mod-Shift-Alt-ArrowDown", moveCurrentBlockDown],
]) ])
} }