@atlaskit/editor-plugin-layout
Version:
Layout plugin for @atlaskit/editor-core
52 lines (48 loc) • 2.69 kB
JavaScript
import { GapCursorSelection } from '@atlaskit/editor-common/selection';
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeOfType, findSelectedNodeOfType } from '@atlaskit/editor-prosemirror/utils';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
export var getMaybeLayoutSection = function getMaybeLayoutSection(state) {
var _state$schema$nodes = state.schema.nodes,
layoutSection = _state$schema$nodes.layoutSection,
layoutColumn = _state$schema$nodes.layoutColumn,
selection = state.selection;
var isLayoutColumn = editorExperiment('advanced_layouts', true) && findSelectedNodeOfType([layoutColumn])(selection);
// When selection is on layoutColumn, we want to hide floating toolbar, hence don't return layoutSection node here
return isLayoutColumn ? undefined : findParentNodeOfType(layoutSection)(selection) || findSelectedNodeOfType([layoutSection])(selection);
};
/**
* The depth of the layout column inside the layout section.
* As per the current implementation, the layout column ALWAYS has a depth of 1.
*/
var LAYOUT_COLUMN_DEPTH = 1;
/**
* This helper function is used to select a position inside a layout section.
* @param view editor view instance
* @param posOfLayout the starting position of the layout
* @param childIndex the index of the child node in the layout section
* @returns Transaction or undefined
*/
export var selectIntoLayout = function selectIntoLayout(view, posOfLayout) {
var _$maybeLayoutSection$;
var childIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var $maybeLayoutSection = view.state.doc.resolve(posOfLayout);
if (((_$maybeLayoutSection$ = $maybeLayoutSection.nodeAfter) === null || _$maybeLayoutSection$ === void 0 ? void 0 : _$maybeLayoutSection$.type.name) === 'layoutSection') {
var _layoutSectionNode$fi;
var layoutSectionNode = $maybeLayoutSection.nodeAfter;
// check if the childIndex is valid
if (childIndex < 0 || childIndex >= layoutSectionNode.childCount) {
return;
}
var childPos = $maybeLayoutSection.posAtIndex(childIndex, LAYOUT_COLUMN_DEPTH);
var tr = view.state.tr;
var $selectionPos = tr.doc.resolve(childPos);
if (((_layoutSectionNode$fi = layoutSectionNode.firstChild) === null || _layoutSectionNode$fi === void 0 ? void 0 : _layoutSectionNode$fi.type.name) === 'paragraph') {
view.dispatch(tr.setSelection(TextSelection.near($selectionPos)));
} else {
view.dispatch(tr.setSelection(GapCursorSelection.near($selectionPos)));
}
return tr;
}
};