@atlaskit/editor-plugin-paste
Version:
Paste plugin for @atlaskit/editor-core
90 lines (88 loc) • 3.64 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import { findParentNodeClosestToPos, findParentNodeOfTypeClosestToPos } from '@atlaskit/editor-prosemirror/utils';
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
import { fg } from '@atlaskit/platform-feature-flags';
var excludedNodes = ['caption', 'layoutColumn', 'listItem', 'tableHeader', 'tableCell', 'tableRow', 'text', 'placeholder', 'unsupportedBlock', 'unsupportedInline', 'hardBreak', 'confluenceUnsupportedBlock', 'confluenceUnsupportedInline', 'taskItem', 'decisionItem'];
export var isExcludedNode = function isExcludedNode(nodeName) {
return excludedNodes.includes(nodeName);
};
export var isCursorSelectionAtTopLevel = function isCursorSelectionAtTopLevel(selection) {
var from = selection.from,
to = selection.to,
$from = selection.$from;
if (from !== to) {
return false;
}
return $from.parentOffset === 0;
};
var inlineNodes = ['emoji', 'date', 'status', 'mention', 'mediaInline', 'inlineCard', 'inlineExtension'];
export var isInlineNode = function isInlineNode(nodeName) {
return inlineNodes.includes(nodeName);
};
export var isNestedInlineNode = function isNestedInlineNode(selection) {
if (selection.$from.depth !== 1) {
return true;
}
// check if the node is a part of a larger paragraph or heading
var parentSize = selection.$from.parent.content.size;
var contentSize = selection.content().size;
var parentChildCount = selection.$from.parent.childCount;
// when the node was copied and pasted, it won't have extra space the parent has only one child
if (parentChildCount === 1) {
return false;
}
// some inline nodes (like date and emoji) when inserted have extra space that is stores as a child
if (parentChildCount === 2 && parentSize - contentSize === 1) {
return false;
}
return true;
};
export var isNestedInTable = function isNestedInTable(state) {
var schema = state.schema,
selection = state.selection;
if (selection instanceof CellSelection) {
return false;
}
var table = schema.nodes.table;
var tableNode = findParentNodeOfTypeClosestToPos(selection.$from, table);
if (!tableNode) {
return false;
}
return true;
};
export var getParentNodeDepth = function getParentNodeDepth(selection) {
var parentNode = findParentNodeClosestToPos(selection.$from, function () {
return true;
});
if (!parentNode) {
return 0;
}
return parentNode.node.type.name === 'heading' || parentNode.node.type.name === 'paragraph' ? parentNode.depth - 1 : parentNode.depth;
};
export var isEntireNestedParagraphOrHeadingSelected = function isEntireNestedParagraphOrHeadingSelected(selection) {
var $from = selection.$from,
$to = selection.$to;
return $from.textOffset === 0 && $to.textOffset === 0;
};
export var containsExcludedNode = function containsExcludedNode(content) {
for (var i = 0; i < content.childCount; i++) {
var _content$maybeChild;
var nodeName = ((_content$maybeChild = content.maybeChild(i)) === null || _content$maybeChild === void 0 ? void 0 : _content$maybeChild.type.name) || '';
if (isExcludedNode(nodeName)) {
return true;
}
}
return false;
};
export var getMultipleSelectionAttributes = function getMultipleSelectionAttributes(content) {
var nodeTypes = [];
if (content.size) {
content.forEach(function (node) {
nodeTypes.push(node.type.name);
});
}
return {
nodeTypes: fg('platform_editor_track_node_types') ? _toConsumableArray(new Set(nodeTypes)).sort().join(',') : undefined,
hasSelectedMultipleNodes: nodeTypes.length > 1
};
};