UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

59 lines (53 loc) 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectIntoLayout = exports.getMaybeLayoutSection = void 0; var _selection = require("@atlaskit/editor-common/selection"); var _state = require("@atlaskit/editor-prosemirror/state"); var _utils = require("@atlaskit/editor-prosemirror/utils"); var _experiments = require("@atlaskit/tmp-editor-statsig/experiments"); // eslint-disable-next-line @typescript-eslint/consistent-type-imports var getMaybeLayoutSection = exports.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 = (0, _experiments.editorExperiment)('advanced_layouts', true) && (0, _utils.findSelectedNodeOfType)([layoutColumn])(selection); // When selection is on layoutColumn, we want to hide floating toolbar, hence don't return layoutSection node here return isLayoutColumn ? undefined : (0, _utils.findParentNodeOfType)(layoutSection)(selection) || (0, _utils.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 */ var selectIntoLayout = exports.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(_state.TextSelection.near($selectionPos))); } else { view.dispatch(tr.setSelection(_selection.GapCursorSelection.near($selectionPos))); } return tr; } };