@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
65 lines (61 loc) • 2.99 kB
JavaScript
/** Document range containing a heading and all content in its section. */
/** Returns the full section range when the heading at this position is collapsed. */
export const getCollapsedHeadingRange = (api, headingPos) => {
var _api$blockCollapse, _api$blockCollapse$sh;
const sectionEnd = api === null || api === void 0 ? void 0 : (_api$blockCollapse = api.blockCollapse) === null || _api$blockCollapse === void 0 ? void 0 : (_api$blockCollapse$sh = _api$blockCollapse.sharedState.currentState()) === null || _api$blockCollapse$sh === void 0 ? void 0 : _api$blockCollapse$sh.collapsedSectionEnds.get(headingPos);
return sectionEnd === undefined ? undefined : {
from: headingPos,
to: sectionEnd
};
};
/** Returns whether the heading at this position has a collapsed section. */
export const isCollapsedHeading = (api, headingPos) => {
var _api$blockCollapse$sh2, _api$blockCollapse2, _api$blockCollapse2$s;
return (_api$blockCollapse$sh2 = api === null || api === void 0 ? void 0 : (_api$blockCollapse2 = api.blockCollapse) === null || _api$blockCollapse2 === void 0 ? void 0 : (_api$blockCollapse2$s = _api$blockCollapse2.sharedState.currentState()) === null || _api$blockCollapse2$s === void 0 ? void 0 : _api$blockCollapse2$s.collapsedSectionEnds.has(headingPos)) !== null && _api$blockCollapse$sh2 !== void 0 ? _api$blockCollapse$sh2 : false;
};
/** Selects a collapsed section for drag or block-menu operations, optionally expanding it. */
export const prepareCollapsedHeadingSelection = ({
api,
expand,
headingPos,
tr
}) => {
var _api$blockControls;
const range = getCollapsedHeadingRange(api, headingPos);
if (!range) {
return false;
}
if (expand) {
var _api$blockCollapse3;
api === null || api === void 0 ? void 0 : (_api$blockCollapse3 = api.blockCollapse) === null || _api$blockCollapse3 === void 0 ? void 0 : _api$blockCollapse3.commands.expandHeading(headingPos)({
tr
});
}
api === null || api === void 0 ? void 0 : (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.commands.setMultiSelectPositions(range.from, range.to)({
tr
});
return true;
};
/** Preserves collapse for root moves and expands headings moved into nested containers. */
export const updateCollapsedHeadingAfterMove = ({
api,
destinationPos,
isNested,
tr,
wasCollapsed
}) => {
var _api$blockCollapse5;
if (!wasCollapsed) {
return;
}
if (isNested) {
var _api$blockCollapse4;
api === null || api === void 0 ? void 0 : (_api$blockCollapse4 = api.blockCollapse) === null || _api$blockCollapse4 === void 0 ? void 0 : _api$blockCollapse4.commands.expandHeading(destinationPos)({
tr
});
return;
}
api === null || api === void 0 ? void 0 : (_api$blockCollapse5 = api.blockCollapse) === null || _api$blockCollapse5 === void 0 ? void 0 : _api$blockCollapse5.commands.collapseHeadingsAtPositions([destinationPos])({
tr
});
};