UNPKG

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

Version:

CodeBlockAdvanced plugin for @atlaskit/editor-core

57 lines 1.65 kB
import { RelativeSelectionPos } from '@atlaskit/editor-common/selection'; import { Selection } from '@atlaskit/editor-prosemirror/state'; export const maybeEscapeKeymap = ({ unit, dir, view, cm, getPos, getNode, onMaybeNodeSelection, selectCodeBlockNode }) => { var _getPos; if (unit === 'char') { onMaybeNodeSelection(); } const node = getNode(); const { state } = cm; let main = state.selection.main; if (!main.empty) { return false; } if (unit === 'line') { main = { ...state.doc.lineAt(main.head), head: 0, empty: false }; } if (dir < 0 ? main.from > 0 : main.to < state.doc.length) { return false; } const targetPos = ((_getPos = getPos === null || getPos === void 0 ? void 0 : getPos()) !== null && _getPos !== void 0 ? _getPos : 0) + (dir < 0 ? 0 : node.nodeSize); if (unit === 'char') { view.focus(); selectCodeBlockNode(dir === -1 ? RelativeSelectionPos.Start : RelativeSelectionPos.End); return true; } else { createParagraphIfEndOfDoc(view, targetPos); const tr = view.state.tr; const selection = Selection.near(tr.doc.resolve(targetPos), dir); tr.setSelection(selection).scrollIntoView(); view.dispatch(tr); view.focus(); return true; } }; const createParagraphIfEndOfDoc = (view, targetPos) => { if (targetPos === view.state.doc.content.size) { const paragraph = view.state.schema.nodes.paragraph.createChecked({}); const tr = view.state.tr.insert(targetPos, paragraph); // Note: we purposefully do a multi-dispatch here, otherwise we get stuck in codemirror view.dispatch(tr); } };