@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
69 lines (67 loc) • 3.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.expandAndUpdateSelection = void 0;
var _selection = require("@atlaskit/editor-common/selection");
var _state = require("@atlaskit/editor-prosemirror/state");
var _utils = require("@atlaskit/editor-tables/utils");
var _getSelection = require("./getSelection");
var _selection2 = require("./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
} : (0, _selection2.adjustSelectionBoundsForEdgePositions)($from, $to);
return (0, _selection.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.
*/
var expandAndUpdateSelection = exports.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) && (0, _selection.isMultiBlockRange)(expandedRange.range)) {
// Then create a selection from the start of the first node to the end of the last node
tr.setSelection(_state.TextSelection.create(tr.doc, Math.min(selection.from, expandedRange.$from.pos), Math.max(selection.to, expandedRange.$to.pos)));
} else if (nodeType === 'table') {
(0, _utils.selectTableClosestToPos)(tr, tr.doc.resolve(startPos + 1));
} else {
// Select the clicked drag handle's node only
(0, _getSelection.selectNode)(tr, startPos, nodeType, api);
}
};