@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
72 lines • 2.87 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, DELETE_DIRECTION, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { isEmptySelectionAtEnd, walkNextNode } from '@atlaskit/editor-common/utils';
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { calcJoinListScenario } from '../actions/join-list-items-forward';
export var joinListItemForward = function joinListItemForward(editorAnalyticsAPI) {
return function (state, dispatch) {
var tr = state.tr,
$head = state.selection.$head;
var walkNode = walkNextNode($head);
if (!isEmptySelectionAtEnd(state)) {
return false;
}
if (fg('platform_editor_blocks_patch_7')) {
var interveningSyncBlockPos;
// walkNextNode can skip leaf nodes because they have no resolvable content position.
state.doc.nodesBetween($head.pos, walkNode.$pos.pos, function (node, pos) {
if (interveningSyncBlockPos === undefined && node.type.name === 'syncBlock') {
interveningSyncBlockPos = pos;
}
return interveningSyncBlockPos === undefined;
});
if (interveningSyncBlockPos !== undefined) {
if (dispatch) {
var selection = NodeSelection.create(state.doc, interveningSyncBlockPos);
dispatch(tr.setSelection(selection).scrollIntoView());
}
return true;
}
}
var scenarios = calcJoinListScenario(walkNode, $head);
if (!scenarios) {
return false;
}
var _scenarios = _slicedToArray(scenarios, 2),
scenario = _scenarios[0],
action = _scenarios[1];
var result = action({
tr: tr,
$next: walkNode.$pos,
$head: $head
});
if (!result) {
return false;
}
var _state$schema$nodes = state.schema.nodes,
bulletList = _state$schema$nodes.bulletList,
orderedList = _state$schema$nodes.orderedList;
var listParent = findParentNodeOfType([bulletList, orderedList])(tr.selection);
var actionSubjectId = ACTION_SUBJECT_ID.FORMAT_LIST_BULLET;
if (listParent && listParent.node.type === orderedList) {
actionSubjectId = ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
}
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
action: ACTION.LIST_ITEM_JOINED,
actionSubject: ACTION_SUBJECT.LIST,
actionSubjectId: actionSubjectId,
eventType: EVENT_TYPE.TRACK,
attributes: {
inputMethod: INPUT_METHOD.KEYBOARD,
direction: DELETE_DIRECTION.FORWARD,
scenario: scenario
}
})(tr);
if (dispatch) {
dispatch(tr);
}
return true;
};
};