Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getValidPos = (tr: Transaction, pos: number) => {
const resolvedPos = tr.doc.resolve(pos);
const backwardSelection = Selection.findFrom(resolvedPos, -1, true);
// if there's no correct cursor position before the `pos`, we try to find it after the `pos`
const forwardSelection = Selection.findFrom(resolvedPos, 1, true);
return backwardSelection
? backwardSelection.from
: forwardSelection
? forwardSelection.from
: pos;
};
let $cut = findCutAfter($cursor)
// If there is no node after this, there's nothing to do
if (!$cut) return false
let after = $cut.nodeAfter
// Try the joining algorithm
if (deleteBarrier(state, $cut, dispatch)) return true
// If the node above has no content and the node below is
// selectable, delete the node above and select the one below.
if ($cursor.parent.content.size == 0 &&
(textblockAt(after, "start") || NodeSelection.isSelectable(after))) {
if (dispatch) {
let tr = state.tr.deleteRange($cursor.before(), $cursor.after())
tr.setSelection(textblockAt(after, "start") ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos)), 1)
: NodeSelection.create(tr.doc, tr.mapping.map($cut.pos)))
dispatch(tr.scrollIntoView())
}
return true
}
// If the next node is an atom, delete it
if (after.isAtom && $cut.depth == $cursor.depth - 1) {
if (dispatch) dispatch(state.tr.delete($cut.pos, $cut.pos + after.nodeSize).scrollIntoView())
return true
}
return false
}
);
if (codeBlock instanceof Node) {
const codeBlockPos = ContentEditorService.findNodePosition(state, codeBlock);
// Are we at the beginning of our code block?
if (codeBlockPos === state.selection.from - 1) {
const tr = state.tr;
// Create paragraph node and insert
const newNode = state.schema.nodes.paragraph.create();
tr.insert(codeBlockPos, newNode);
// Resolve its position and set the selection.
const resolvedPos = tr.doc.resolve(state.selection.from - 1);
const selection = Selection.findFrom(resolvedPos, 1);
if (selection instanceof Selection) {
tr.setSelection(selection).scrollIntoView();
}
dispatch(tr);
return true;
}
}
return false;
};
}
const getValidPos = (tr: Transaction, pos: number) => {
const resolvedPos = tr.doc.resolve(pos);
const backwardSelection = Selection.findFrom(resolvedPos, -1, true);
// if there's no correct cursor position before the `pos`, we try to find it after the `pos`
const forwardSelection = Selection.findFrom(resolvedPos, 1, true);
return backwardSelection
? backwardSelection.from
: forwardSelection
? forwardSelection.from
: pos;
};
export const findPositionOfNodeAfter = (
value: Selection | ResolvedPos | EditorState,
): FindProsemirrorNodeResult | undefined => {
const $pos = isResolvedPos(value) ? value : isSelection(value) ? value.$from : value.selection.$from;
if (isNullOrUndefined($pos)) {
throw new Error('Invalid value passed in.');
}
const { nodeAfter } = $pos;
const selection = PMSelection.findFrom($pos, 1);
if (!selection || !nodeAfter) {
return;
}
const parent = findParentNodeOfType({ types: nodeAfter.type, selection });
return parent
? parent
: {
node: nodeAfter,
pos: nodeAfter.isLeaf ? selection.from : selection.from - 1,
end: selection.from + nodeAfter.nodeSize,
start: selection.from,
};
};
function selectVertically(view, dir, mods) {
let sel = view.state.selection
if (sel instanceof TextSelection && !sel.empty || mods.indexOf("s") > -1) return false
let {$from, $to} = sel
if (!$from.parent.inlineContent || view.endOfTextblock(dir < 0 ? "up" : "down")) {
let next = moveSelectionBlock(view.state, dir)
if (next && (next instanceof NodeSelection))
return apply(view, next)
}
if (!$from.parent.inlineContent) {
let beyond = Selection.findFrom(dir < 0 ? $from : $to, dir)
return beyond ? apply(view, beyond) : true
}
return false
}
export const findPositionOfNodeBefore = (
value: Selection | ResolvedPos | EditorState | Transaction,
): FindProsemirrorNodeResult | undefined => {
const $pos = isResolvedPos(value) ? value : isSelection(value) ? value.$from : value.selection.$from;
if (isNullOrUndefined($pos)) {
throw new Error('Invalid value passed in.');
}
const { nodeBefore } = $pos;
const selection = PMSelection.findFrom($pos, -1);
if (!selection || !nodeBefore) {
return;
}
const parent = findParentNodeOfType({ types: nodeBefore.type, selection });
return parent
? parent
: {
node: nodeBefore,
pos: nodeBefore.isLeaf ? selection.from : selection.from - 1,
end: selection.from + nodeBefore.nodeSize,
start: selection.from,
};
};
function moveSelectionBlock(state, dir) {
let {$anchor, $head} = state.selection
let $side = dir > 0 ? $anchor.max($head) : $anchor.min($head)
let $start = !$side.parent.inlineContent ? $side : $side.depth ? state.doc.resolve(dir > 0 ? $side.after() : $side.before()) : null
return $start && Selection.findFrom($start, dir)
}