UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

118 lines (117 loc) 4.35 kB
import { NodeSelection } from '@atlaskit/editor-prosemirror/state'; import { findChildrenByType, findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils'; import { DEFAULT_LAYOUT_COLUMN_VALIGN } from '../consts'; const findLayoutSectionFromSelection = selection => { const { layoutSection } = selection.$from.doc.type.schema.nodes; // NodeSelection on the layoutSection node itself if (selection instanceof NodeSelection && selection.node.type === layoutSection) { return { node: selection.node, pos: selection.from }; } return findParentNodeOfType(layoutSection)(selection); }; const findLayoutColumnsFromLayoutSection = (layoutSectionNode, layoutSectionPos = 0) => { return findChildrenByType(layoutSectionNode, layoutSectionNode.type.schema.nodes.layoutColumn).map(({ node, pos }) => ({ node, pos: pos + layoutSectionPos + 1 })); }; const getSelectedLayoutColumns = (selection, isColumnSelected) => { const layoutSection = findLayoutSectionFromSelection(selection); if (!layoutSection) { return undefined; } const { node: layoutSectionNode, pos: layoutSectionPos } = layoutSection; const allLayoutColumns = findLayoutColumnsFromLayoutSection(layoutSectionNode, layoutSectionPos); if (!allLayoutColumns.length) { return undefined; } let startIndex = -1; let endIndex = -1; const selectedLayoutColumns = allLayoutColumns.filter((column, index) => { if (isColumnSelected(column, index)) { if (startIndex === -1) { startIndex = index; } endIndex = index; return true; } return false; }); return { layoutSectionNode, layoutSectionPos, selectedLayoutColumns, startIndex, endIndex }; }; export const getSelectedLayoutColumnsFromSelection = selection => { return getSelectedLayoutColumns(selection, ({ node, pos }) => { // NodeSelection on a layout column is clearly selected. if (selection instanceof NodeSelection && selection.node === node) { return true; } // For TextSelection, only count columns that are fully contained within the selection // (not partial text selections inside a column). const nodeEndPos = pos + node.nodeSize; return !selection.empty && selection.from <= pos && selection.to >= nodeEndPos; }); }; export const getLayoutColumnsFromContentSelection = selection => { return getSelectedLayoutColumns(selection, ({ node, pos }) => { if (selection instanceof NodeSelection && selection.node === node) { return true; } const nodeEndPos = pos + node.nodeSize; return selection.empty ? selection.from > pos && selection.from < nodeEndPos : selection.from < nodeEndPos && selection.to > pos; }); }; export const getAllLayoutColumnsFromSelection = selection => { const layoutSection = findLayoutSectionFromSelection(selection); if (!layoutSection) { return undefined; } const layoutColumns = findLayoutColumnsFromLayoutSection(layoutSection.node, layoutSection.pos); if (!(layoutColumns !== null && layoutColumns !== void 0 && layoutColumns.length)) { return undefined; } return { layoutSectionNode: layoutSection.node, layoutSectionPos: layoutSection.pos, selectedLayoutColumns: layoutColumns, startIndex: 0, endIndex: layoutColumns.length - 1 }; }; export const getLayoutColumnValign = layoutColumn => { var _ref; return layoutColumn ? (_ref = layoutColumn.attrs.valign) !== null && _ref !== void 0 ? _ref : DEFAULT_LAYOUT_COLUMN_VALIGN : undefined; }; export const getLayoutColumnMenuAnchorPos = (selection, anchorPosFromHandle) => { var _clickedSelectedColum, _selectedLayoutColumn; const selectedLayoutColumns = getSelectedLayoutColumnsFromSelection(selection); if (!selectedLayoutColumns) { return undefined; } const clickedSelectedColumn = selectedLayoutColumns.selectedLayoutColumns.find(({ pos }) => pos === anchorPosFromHandle); return (_clickedSelectedColum = clickedSelectedColumn === null || clickedSelectedColumn === void 0 ? void 0 : clickedSelectedColumn.pos) !== null && _clickedSelectedColum !== void 0 ? _clickedSelectedColum : (_selectedLayoutColumn = selectedLayoutColumns.selectedLayoutColumns[0]) === null || _selectedLayoutColumn === void 0 ? void 0 : _selectedLayoutColumn.pos; };