UNPKG

@atlaskit/editor-plugin-code-block-advanced

Version:

CodeBlockAdvanced plugin for @atlaskit/editor-core

61 lines (59 loc) 2.17 kB
import { TextSelection } from '@atlaskit/editor-prosemirror/state'; export const shiftArrowUpWorkaround = (view, event) => { var _doc$resolve$nodeBefo; const { doc, selection: { $head, $anchor }, tr, schema: { nodes: { codeBlock } } } = view.state; // Position we want to check (directly after our current position) const pos = Math.max($head.pos - 1, 1); const isNodeBefore = ((_doc$resolve$nodeBefo = doc.resolve(pos).nodeBefore) === null || _doc$resolve$nodeBefo === void 0 ? void 0 : _doc$resolve$nodeBefo.type) === codeBlock; const maybeProblematicNode = isNodeBefore ? doc.resolve(pos).nodeBefore : doc.resolve(pos).node(); if ((maybeProblematicNode === null || maybeProblematicNode === void 0 ? void 0 : maybeProblematicNode.type) === codeBlock) { const nodeSize = maybeProblematicNode.nodeSize; const startPos = isNodeBefore ? pos : $head.pos; tr.setSelection(TextSelection.create(doc, $anchor.pos, Math.max(startPos - nodeSize, 0))); view.dispatch(tr); event.preventDefault(); return true; } return false; }; export const shiftArrowDownWorkaround = (view, event) => { var _doc$resolve$nodeAfte; const { doc, selection: { $head, $anchor }, tr, schema: { nodes: { codeBlock } } } = view.state; // Position we want to check (directly after our current position) const pos = $head.pos + 1; const isNodeAfter = ((_doc$resolve$nodeAfte = doc.resolve(pos).nodeAfter) === null || _doc$resolve$nodeAfte === void 0 ? void 0 : _doc$resolve$nodeAfte.type) === codeBlock; const maybeProblematicNode = isNodeAfter ? doc.resolve(pos).nodeAfter : doc.resolve(pos).node(); if ((maybeProblematicNode === null || maybeProblematicNode === void 0 ? void 0 : maybeProblematicNode.type) === codeBlock) { const nodeSize = maybeProblematicNode.nodeSize; const startPos = isNodeAfter ? pos : $head.pos; tr.setSelection(TextSelection.create(doc, $anchor.pos, Math.min(startPos + nodeSize, doc.content.size))); view.dispatch(tr); event.preventDefault(); return true; } return false; };