UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

141 lines (139 loc) 7.46 kB
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { expandedState } from '@atlaskit/editor-common/expand'; import { logException } from '@atlaskit/editor-common/monitoring'; import { startMeasure, stopMeasure } from '@atlaskit/editor-common/performance-measures'; import { expandSelectionToBlockRange, getSourceNodesFromSelectionRange } from '@atlaskit/editor-common/selection'; import { NodeSelection } from '@atlaskit/editor-prosemirror/state'; import { Mapping, StepMap } from '@atlaskit/editor-prosemirror/transform'; import { CellSelection } from '@atlaskit/editor-tables'; import { isNestedNode } from '../ui/utils/isNestedNode'; import { convertNodesToTargetType } from './transform-node-utils/transform'; import { isListNode } from './transform-node-utils/utils'; export var transformNode = function transformNode(api) { return function (targetType, metadata) { return function (_ref) { var _api$blockControls; var tr = _ref.tr; var preservedSelection = api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 || (_api$blockControls = _api$blockControls.sharedState.currentState()) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.preservedSelection; if (!preservedSelection) { return tr; } var measureId = "transformNode_".concat(targetType.name, "_").concat(Date.now()); startMeasure(measureId); var nodes = tr.doc.type.schema.nodes; var _expandSelectionToBlo = expandSelectionToBlockRange(preservedSelection), $from = _expandSelectionToBlo.$from, $to = _expandSelectionToBlo.$to; var selectedParent = $from.parent; var isParentLayout = selectedParent.type === nodes.layoutColumn; var isNested = isNestedNode(preservedSelection, '') && !isParentLayout; var isList = isListNode(selectedParent); var sourceNodes = getSourceNodesFromSelectionRange(tr, preservedSelection); var sourceNodeTypes = {}; sourceNodes.forEach(function (node) { var typeName = node.type.name; sourceNodeTypes[typeName] = (sourceNodeTypes[typeName] || 0) + 1; }); // Check if source node is empty paragraph or heading var isEmptyLine = sourceNodes.length === 1 && (sourceNodes[0].type === nodes.paragraph || sourceNodes[0].type === nodes.heading) && (sourceNodes[0].content.size === 0 || sourceNodes[0].textContent.trim() === ''); try { var resultNodes = convertNodesToTargetType({ sourceNodes: sourceNodes, targetNodeType: targetType, schema: tr.doc.type.schema, isNested: isNested, targetAttrs: metadata === null || metadata === void 0 ? void 0 : metadata.targetAttrs, parentNode: selectedParent }); var content = resultNodes.length > 0 ? resultNodes : sourceNodes; var sliceStart = isList ? $from.pos - 1 : $from.pos; var expand = nodes.expand, nestedExpand = nodes.nestedExpand; content.forEach(function (node) { if (node.type === expand || node.type === nestedExpand) { expandedState.set(node, true); } }); if (preservedSelection instanceof NodeSelection && preservedSelection.node.type === nodes.mediaSingle) { var _api$blockControls2; // when node is media single, use tr.replaceWith freeze editor, if modify position, tr.replaceWith creates duplicats var deleteFrom = $from.pos; var deleteTo = $to.pos; tr.delete(deleteFrom, deleteTo); // After deletion, recalculate the insertion position to ensure it's valid // especially when mediaSingle with caption is at the bottom of the document var insertPos = Math.min(deleteFrom, tr.doc.content.size); tr.insert(insertPos, content); // when we replace and insert content, we need to manually map the preserved selection // through the transaction, otherwise it will treat the selection as having been deleted // and stop preserving it var oldSize = sourceNodes.reduce(function (sum, node) { return sum + node.nodeSize; }, 0); var newSize = content.reduce(function (sum, node) { return sum + node.nodeSize; }, 0); api === null || api === void 0 || (_api$blockControls2 = api.blockControls) === null || _api$blockControls2 === void 0 || _api$blockControls2.commands.mapPreservedSelection(new Mapping([new StepMap([0, oldSize, newSize])]))({ tr: tr }); } else { tr.replaceWith(sliceStart, $to.pos, content); } if (preservedSelection instanceof CellSelection) { var insertedNode = tr.doc.nodeAt($from.pos); var isSelectable = insertedNode && NodeSelection.isSelectable(insertedNode); if (isSelectable) { var _api$blockControls3; var nodeSelection = NodeSelection.create(tr.doc, $from.pos); tr.setSelection(nodeSelection); api === null || api === void 0 || (_api$blockControls3 = api.blockControls) === null || _api$blockControls3 === void 0 || _api$blockControls3.commands.startPreservingSelection()({ tr: tr }); } } stopMeasure(measureId, function (duration, startTime) { var _api$analytics; api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || (_api$analytics = _api$analytics.actions) === null || _api$analytics === void 0 || _api$analytics.attachAnalyticsEvent({ action: ACTION.TRANSFORMED, actionSubject: ACTION_SUBJECT.ELEMENT, attributes: { duration: duration, isEmptyLine: isEmptyLine, isNested: isNested, sourceNodesCount: sourceNodes.length, sourceNodesCountByType: sourceNodeTypes, sourceNodeType: sourceNodes.length === 1 ? sourceNodes[0].type.name : 'multiple', startTime: startTime, targetNodeType: targetType.name, outputNodesCount: content.length, inputMethod: INPUT_METHOD.BLOCK_MENU }, eventType: EVENT_TYPE.TRACK })(tr); }); } catch (error) { var _api$analytics2; stopMeasure(measureId); logException(error, { location: 'editor-plugin-block-menu' }); api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || (_api$analytics2 = _api$analytics2.actions) === null || _api$analytics2 === void 0 || _api$analytics2.attachAnalyticsEvent({ action: ACTION.ERRORED, actionSubject: ACTION_SUBJECT.ELEMENT, actionSubjectId: ACTION_SUBJECT_ID.TRANSFORM, eventType: EVENT_TYPE.OPERATIONAL, attributes: { docSize: tr.doc.nodeSize, error: error.message, errorStack: error.stack, from: sourceNodes.length === 1 ? sourceNodes[0].type.name : 'multiple', inputMethod: INPUT_METHOD.BLOCK_MENU, selection: preservedSelection.toJSON(), to: targetType.name } })(tr); } return tr; }; }; };