@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
46 lines (43 loc) • 2.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.updateSelection = exports.getInsertLayoutStep = void 0;
var _selection = require("@atlaskit/editor-common/selection");
var _state = require("@atlaskit/editor-prosemirror/state");
var _transform = require("@atlaskit/editor-prosemirror/transform");
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
var getInsertLayoutStep = exports.getInsertLayoutStep = function getInsertLayoutStep(tr) {
return tr.steps.find(function (step) {
var _step$slice$content$f;
return step instanceof _transform.ReplaceStep && ['layoutSection', 'layoutColumn'].includes(((_step$slice$content$f = step.slice.content.firstChild) === null || _step$slice$content$f === void 0 ? void 0 : _step$slice$content$f.type.name) || '');
});
};
var updateSelection = exports.updateSelection = function updateSelection(tr, to, insertAtRight) {
var _lastNode;
var $to = tr.doc.resolve(to);
var toNode = $to.nodeAfter;
var lastNode = toNode === null || toNode === void 0 ? void 0 : toNode.content.lastChild;
if (!toNode || !lastNode) {
return tr;
}
var offset = toNode.nodeSize;
// drop at the start of the layoutSection or to create a new layoutSection
if (toNode.type.name === 'layoutSection') {
if (insertAtRight) {
var _toNode$content$lastC;
lastNode = (_toNode$content$lastC = toNode.content.lastChild) === null || _toNode$content$lastC === void 0 ? void 0 : _toNode$content$lastC.lastChild;
offset = toNode.nodeSize - 1;
} else {
var _toNode$content$first, _toNode$content$first2;
lastNode = (_toNode$content$first = toNode.content.firstChild) === null || _toNode$content$first === void 0 ? void 0 : _toNode$content$first.lastChild;
offset = 1 + (((_toNode$content$first2 = toNode.content.firstChild) === null || _toNode$content$first2 === void 0 ? void 0 : _toNode$content$first2.nodeSize) || 0);
}
}
if (['paragraph', 'heading'].includes(((_lastNode = lastNode) === null || _lastNode === void 0 ? void 0 : _lastNode.type.name) || '')) {
tr.setSelection(_state.TextSelection.near(tr.doc.resolve(to + offset - 2)));
} else if (lastNode) {
tr.setSelection(new _selection.GapCursorSelection(tr.doc.resolve(to + offset - 1), _selection.Side.RIGHT));
}
return tr;
};