@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
37 lines (36 loc) • 2.07 kB
JavaScript
import { GapCursorSelection, Side } from '@atlaskit/editor-common/selection';
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
import { ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
export const getInsertLayoutStep = tr => tr.steps.find(step => {
var _step$slice$content$f;
return step instanceof 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) || '');
});
export const updateSelection = (tr, to, insertAtRight) => {
var _lastNode;
const $to = tr.doc.resolve(to);
const toNode = $to.nodeAfter;
let lastNode = toNode === null || toNode === void 0 ? void 0 : toNode.content.lastChild;
if (!toNode || !lastNode) {
return tr;
}
let 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(TextSelection.near(tr.doc.resolve(to + offset - 2)));
} else if (lastNode) {
tr.setSelection(new GapCursorSelection(tr.doc.resolve(to + offset - 1), Side.RIGHT));
}
return tr;
};