@atlaskit/editor-plugin-expand
Version:
Expand plugin for @atlaskit/editor-core
41 lines (40 loc) • 1.7 kB
JavaScript
import { expandedState } from '@atlaskit/editor-common/expand';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
export var TOGGLE_EXPAND_RANGE_META_KEY = 'toggleExpandRange';
export var toggleExpandRange = function toggleExpandRange(from, to) {
var open = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
return function (_ref) {
var tr = _ref.tr;
var _tr$doc$type$schema$n = tr.doc.type.schema.nodes,
expand = _tr$doc$type$schema$n.expand,
nestedExpand = _tr$doc$type$schema$n.nestedExpand;
var fromClamped = from && from >= 0 ? from : 0;
var toClamped = to && to <= tr.doc.content.size ? to : tr.doc.content.size;
var positions = [];
tr.doc.nodesBetween(fromClamped, toClamped, function (node, pos) {
if ([expand, nestedExpand].includes(node.type)) {
expandedState.set(node, open);
positions.push(pos);
}
});
if (fg('platform_editor_show_diff_scroll_navigation')) {
if (positions.length === 0) {
// No expand nodes found in the range — nothing to dispatch.
return null;
}
// Set meta so the expand PM plugin can add node decorations.
// This ensures ExpandNodeView.update() receives the decoration and visually
// opens or closes the expand, even when the experiment below is disabled.
tr.setMeta(TOGGLE_EXPAND_RANGE_META_KEY, {
positions: positions,
open: open
});
return tr;
}
if (expValEquals('platform_editor_aifc_expand_collapses_oncreate_fix', 'isEnabled', true)) {
return tr;
}
return null;
};
};