@atlaskit/editor-plugin-layout
Version:
Layout plugin for @atlaskit/editor-core
115 lines (114 loc) • 4.96 kB
JavaScript
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { findChildrenByType, findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
import { DEFAULT_LAYOUT_COLUMN_VALIGN } from '../consts';
var findLayoutSectionFromSelection = function findLayoutSectionFromSelection(selection) {
var layoutSection = selection.$from.doc.type.schema.nodes.layoutSection;
// 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);
};
var findLayoutColumnsFromLayoutSection = function findLayoutColumnsFromLayoutSection(layoutSectionNode) {
var layoutSectionPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return 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
};
};
export var 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 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;
});
};
export var getLayoutColumnsFromContentSelection = function getLayoutColumnsFromContentSelection(selection) {
return getSelectedLayoutColumns(selection, function (_ref3) {
var node = _ref3.node,
pos = _ref3.pos;
if (selection instanceof 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;
});
};
export var 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
};
};
export var getLayoutColumnValign = function getLayoutColumnValign(layoutColumn) {
var _ref4;
return layoutColumn ? (_ref4 = layoutColumn.attrs.valign) !== null && _ref4 !== void 0 ? _ref4 : DEFAULT_LAYOUT_COLUMN_VALIGN : undefined;
};
export var 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;
};