UNPKG

@atlaskit/editor-plugin-layout

Version:

Layout plugin for @atlaskit/editor-core

121 lines (119 loc) 5.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSelectedLayoutColumnsFromSelection = exports.getLayoutColumnsFromContentSelection = exports.getLayoutColumnValign = exports.getLayoutColumnMenuAnchorPos = exports.getAllLayoutColumnsFromSelection = void 0; var _state = require("@atlaskit/editor-prosemirror/state"); var _utils = require("@atlaskit/editor-prosemirror/utils"); var _consts = require("../consts"); var findLayoutSectionFromSelection = function findLayoutSectionFromSelection(selection) { var layoutSection = selection.$from.doc.type.schema.nodes.layoutSection; // NodeSelection on the layoutSection node itself if (selection instanceof _state.NodeSelection && selection.node.type === layoutSection) { return { node: selection.node, pos: selection.from }; } return (0, _utils.findParentNodeOfType)(layoutSection)(selection); }; var findLayoutColumnsFromLayoutSection = function findLayoutColumnsFromLayoutSection(layoutSectionNode) { var layoutSectionPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; return (0, _utils.findChildrenByType)(layoutSectionNode, layoutSectionNode.type.schema.nodes.layoutColumn).map(function (_ref) { var node = _ref.node, pos = _ref.pos; return { node: node, pos: pos + layoutSectionPos + 1 }; }); }; var getSelectedLayoutColumns = function getSelectedLayoutColumns(selection, isColumnSelected) { var layoutSection = findLayoutSectionFromSelection(selection); if (!layoutSection) { return undefined; } var layoutSectionNode = layoutSection.node, layoutSectionPos = layoutSection.pos; var allLayoutColumns = findLayoutColumnsFromLayoutSection(layoutSectionNode, layoutSectionPos); if (!allLayoutColumns.length) { return undefined; } var startIndex = -1; var endIndex = -1; var selectedLayoutColumns = allLayoutColumns.filter(function (column, index) { if (isColumnSelected(column, index)) { if (startIndex === -1) { startIndex = index; } endIndex = index; return true; } return false; }); return { layoutSectionNode: layoutSectionNode, layoutSectionPos: layoutSectionPos, selectedLayoutColumns: selectedLayoutColumns, startIndex: startIndex, endIndex: endIndex }; }; var getSelectedLayoutColumnsFromSelection = exports.getSelectedLayoutColumnsFromSelection = function getSelectedLayoutColumnsFromSelection(selection) { return getSelectedLayoutColumns(selection, function (_ref2) { var node = _ref2.node, pos = _ref2.pos; // NodeSelection on a layout column is clearly selected. if (selection instanceof _state.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). var nodeEndPos = pos + node.nodeSize; return !selection.empty && selection.from <= pos && selection.to >= nodeEndPos; }); }; var getLayoutColumnsFromContentSelection = exports.getLayoutColumnsFromContentSelection = function getLayoutColumnsFromContentSelection(selection) { return getSelectedLayoutColumns(selection, function (_ref3) { var node = _ref3.node, pos = _ref3.pos; if (selection instanceof _state.NodeSelection && selection.node === node) { return true; } var nodeEndPos = pos + node.nodeSize; return selection.empty ? selection.from > pos && selection.from < nodeEndPos : selection.from < nodeEndPos && selection.to > pos; }); }; var getAllLayoutColumnsFromSelection = exports.getAllLayoutColumnsFromSelection = function getAllLayoutColumnsFromSelection(selection) { var layoutSection = findLayoutSectionFromSelection(selection); if (!layoutSection) { return undefined; } var 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 }; }; var getLayoutColumnValign = exports.getLayoutColumnValign = function getLayoutColumnValign(layoutColumn) { var _ref4; return layoutColumn ? (_ref4 = layoutColumn.attrs.valign) !== null && _ref4 !== void 0 ? _ref4 : _consts.DEFAULT_LAYOUT_COLUMN_VALIGN : undefined; }; var getLayoutColumnMenuAnchorPos = exports.getLayoutColumnMenuAnchorPos = function getLayoutColumnMenuAnchorPos(selection, anchorPosFromHandle) { var _clickedSelectedColum, _selectedLayoutColumn; var selectedLayoutColumns = getSelectedLayoutColumnsFromSelection(selection); if (!selectedLayoutColumns) { return undefined; } var clickedSelectedColumn = selectedLayoutColumns.selectedLayoutColumns.find(function (_ref5) { var pos = _ref5.pos; return 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; };