@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
63 lines (62 loc) • 2.98 kB
JavaScript
import { expandToBlockRange, isMultiBlockRange } from '@atlaskit/editor-common/selection';
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
import { selectTableClosestToPos } from '@atlaskit/editor-tables/utils';
import { selectNode } from './getSelection';
import { adjustSelectionBoundsForEdgePositions } from './selection';
var isPosWithinRange = function isPosWithinRange(pos, range) {
return range.start <= pos && range.end >= pos + 1;
};
/**
* Expands the current selection to encompass the full block range
* starting from the given resolved position.
*/
var getExpandedSelectionRange = function getExpandedSelectionRange(_ref) {
var selection = _ref.selection,
doc = _ref.doc,
resolvedStartPos = _ref.resolvedStartPos,
isShiftPressed = _ref.isShiftPressed;
// When not pressing shift, expand the current selection
// When shift selecting upwards, expand from start of node to selection end
// When shift selecting downwards, expand from selection start to end of node
var selectUp = resolvedStartPos.pos < selection.from;
var $from = isShiftPressed && selectUp ? resolvedStartPos : selection.$from;
var $to = isShiftPressed && !selectUp ? doc.resolve(resolvedStartPos.pos + 1) : selection.$to;
var adjusted = isShiftPressed ? {
$from: $from,
$to: $to
} : adjustSelectionBoundsForEdgePositions($from, $to);
return expandToBlockRange(adjusted.$from, adjusted.$to);
};
/**
* Updates the transaction's selection based on the clicked drag handle position.
*
* - If the clicked handle is within an existing multi-block selection range, the selection
* is expanded to cover both the existing range and the clicked node's range.
* - For tables, a table cell selection is used.
* - Otherwise, selects the single node at the clicked handle position.
*/
export var expandAndUpdateSelection = function expandAndUpdateSelection(_ref2) {
var tr = _ref2.tr,
selection = _ref2.selection,
startPos = _ref2.startPos,
isShiftPressed = _ref2.isShiftPressed,
nodeType = _ref2.nodeType,
api = _ref2.api;
var resolvedStartPos = tr.doc.resolve(startPos);
var expandedRange = getExpandedSelectionRange({
doc: tr.doc,
selection: selection,
resolvedStartPos: resolvedStartPos,
isShiftPressed: isShiftPressed
});
// Set selection to expanded selection range if it encompasses the clicked drag handle
if (expandedRange.range && isPosWithinRange(startPos, expandedRange.range) && isMultiBlockRange(expandedRange.range)) {
// Then create a selection from the start of the first node to the end of the last node
tr.setSelection(TextSelection.create(tr.doc, Math.min(selection.from, expandedRange.$from.pos), Math.max(selection.to, expandedRange.$to.pos)));
} else if (nodeType === 'table') {
selectTableClosestToPos(tr, tr.doc.resolve(startPos + 1));
} else {
// Select the clicked drag handle's node only
selectNode(tr, startPos, nodeType, api);
}
};