@atlaskit/editor-plugin-paste
Version:
Paste plugin for @atlaskit/editor-core
123 lines (121 loc) • 5.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPlugin = void 0;
var _analytics = require("@atlaskit/editor-common/analytics");
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _commands = require("./commands");
var _pluginFactory = require("./plugin-factory");
var _pluginKey = require("./plugin-key");
var _types = require("./types");
var _utils = require("./utils");
// This plugin exists only in FullPage/FullWidth Editor and is used to register an event that tells us
// that a user cut and than pasted a node. This order of actions could be considered an alternative
// to new Drag and Drop functionality. The event (document moved) is not accurate, but should be enough to be
// used during DnD roll out. After DnD release this plugin must be removed.
var createPlugin = exports.createPlugin = function createPlugin(dispatch, editorAnalyticsAPI) {
// This variable is used to distinguish between copy and cut events in transformCopied.
var isCutEvent = false;
return new _safePlugin.SafePlugin({
key: _pluginKey.pluginKey,
state: (0, _pluginFactory.createPluginState)(dispatch, _types.defaultState),
props: {
handleDOMEvents: {
cut: function cut() {
isCutEvent = true;
}
},
handlePaste: function handlePaste(_ref, _event, slice) {
var _content$firstChild;
var state = _ref.state,
dispatch = _ref.dispatch;
// The state was cleaned after previous paste. We don't need to update plugin state
// with 'contentPasted' if currentActions array doesn't have 'copiedOrCut'.
var _getPluginState = (0, _pluginFactory.getPluginState)(state),
contentMoved = _getPluginState.contentMoved;
var hasCutAction = contentMoved.currentActions.includes('contentCut');
if (!hasCutAction) {
return;
}
var content = slice.content;
var nodeName = (_content$firstChild = content.firstChild) === null || _content$firstChild === void 0 ? void 0 : _content$firstChild.type.name;
if (!nodeName || !(contentMoved !== null && contentMoved !== void 0 && contentMoved.nodeTypes) || !(0, _utils.isCursorSelectionAtTopLevel)(state.selection)) {
return;
}
var tr = state.tr;
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
action: _analytics.ACTION.MOVED,
actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.NODE,
eventType: _analytics.EVENT_TYPE.TRACK,
attributes: {
nodeDepth: contentMoved === null || contentMoved === void 0 ? void 0 : contentMoved.nodeDepth,
destinationNodeDepth: (0, _utils.getParentNodeDepth)(state.selection),
nodeTypes: contentMoved === null || contentMoved === void 0 ? void 0 : contentMoved.nodeTypes,
hasSelectedMultipleNodes: contentMoved === null || contentMoved === void 0 ? void 0 : contentMoved.hasSelectedMultipleNodes
}
})(tr);
// reset to default state
var updatedTr = (0, _commands.resetContentMovedTransform)()(tr);
dispatch(updatedTr);
},
transformCopied: function transformCopied(slice, _ref2) {
var _content$firstChild2;
var state = _ref2.state,
dispatch = _ref2.dispatch;
// We want to listen only to 'cut' events
if (!isCutEvent) {
return slice;
}
var resetState = false;
var content = slice.content,
size = slice.size;
var selection = state.selection;
var nodeName = ((_content$firstChild2 = content.firstChild) === null || _content$firstChild2 === void 0 ? void 0 : _content$firstChild2.type.name) || '';
var nodeTypes,
hasSelectedMultipleNodes = false;
if (content.childCount > 1) {
if ((0, _utils.containsExcludedNode)(content)) {
resetState = true;
} else {
var attributes = (0, _utils.getMultipleSelectionAttributes)(content);
nodeTypes = attributes.nodeTypes;
hasSelectedMultipleNodes = attributes.hasSelectedMultipleNodes;
}
} else if (content.childCount === 1) {
// Some nodes are not relevant as they are parts of nodes, not whole nodes (like tableCell, tableHeader instead of table node)
// Some nodes like lists, taskList(item), decisionList(item) requires tricky checks that we want to avoid doing.
// These nodes were added to excludedNodes array.
if (!resetState && (0, _utils.isExcludedNode)(nodeName)) {
resetState = true;
}
if (!resetState && !(0, _utils.isEntireNestedParagraphOrHeadingSelected)(selection)) {
resetState = true;
}
if (!resetState && (0, _utils.isInlineNode)(nodeName) && (0, _utils.isNestedInlineNode)(selection)) {
resetState = true;
}
if (!resetState && (0, _utils.isNestedInTable)(state)) {
resetState = true;
}
} else {
resetState = true;
}
if (resetState) {
(0, _commands.resetContentMoved)()(state, dispatch);
} else {
var newState = {
size: size,
nodeTypes: nodeTypes !== null && nodeTypes !== void 0 ? nodeTypes : nodeName,
nodeDepth: (0, _utils.getParentNodeDepth)(selection),
hasSelectedMultipleNodes: hasSelectedMultipleNodes
};
(0, _commands.updateContentMoved)(newState, 'contentCut')(state, dispatch);
}
isCutEvent = false;
return slice;
}
}
});
};