UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

56 lines (52 loc) 2.51 kB
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 const getMaybeLayoutSection = state => { const { schema: { nodes: { layoutSection, layoutColumn } }, selection } = state; const 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. */ const 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 const selectIntoLayout = (view, posOfLayout, childIndex = 0) => { var _$maybeLayoutSection$; const $maybeLayoutSection = view.state.doc.resolve(posOfLayout); if (((_$maybeLayoutSection$ = $maybeLayoutSection.nodeAfter) === null || _$maybeLayoutSection$ === void 0 ? void 0 : _$maybeLayoutSection$.type.name) === 'layoutSection') { var _layoutSectionNode$fi; const layoutSectionNode = $maybeLayoutSection.nodeAfter; // check if the childIndex is valid if (childIndex < 0 || childIndex >= layoutSectionNode.childCount) { return; } const childPos = $maybeLayoutSection.posAtIndex(childIndex, LAYOUT_COLUMN_DEPTH); const tr = view.state.tr; const $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; } };