@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
53 lines (51 loc) • 3.23 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { getCommonListAnalyticsAttributes } from '@atlaskit/editor-common/lists';
import { PassiveTransaction } from '@atlaskit/editor-common/preset';
import { isBulletList } from '@atlaskit/editor-common/utils';
import { closeHistory } from '@atlaskit/prosemirror-history/closeHistory';
import { moveSelectedListItems } from '../actions/move-selected-list-items';
import { findFirstParentListNode } from '../utils/find';
import { isInsideListItem } from '../utils/selection';
/**
* Handler for flexible list indentation.
* Allows indenting the first item by creating wrapper structures.
*/
var handleIndentListItems = function handleIndentListItems(tr, editorAnalyticsAPI, inputMethod) {
var _findFirstParentListN;
moveSelectedListItems(tr, 1);
// If no changes were made, return PassiveTransaction to prevent browser from handling this as a tab key event (e.g. moving focus)
if (!tr.docChanged) {
return new PassiveTransaction();
}
var $from = tr.selection.$from;
var currentListNode = (_findFirstParentListN = findFirstParentListNode($from)) === null || _findFirstParentListN === void 0 ? void 0 : _findFirstParentListN.node;
var actionSubjectId = currentListNode && isBulletList(currentListNode) ? ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
action: ACTION.INDENTED,
actionSubject: ACTION_SUBJECT.LIST,
actionSubjectId: actionSubjectId,
eventType: EVENT_TYPE.TRACK,
attributes: _objectSpread(_objectSpread({}, getCommonListAnalyticsAttributes(tr)), {}, {
inputMethod: inputMethod
})
})(tr);
return tr;
};
export var indentList = function indentList(editorAnalyticsAPI) {
return function () {
var inputMethod = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : INPUT_METHOD.KEYBOARD;
return function (_ref) {
var tr = _ref.tr;
// don't indent if selection is not inside a list
if (!isInsideListItem(tr)) {
return null;
}
// Save the history, so it could undo/revert to the same state before the indent, see https://product-fabric.atlassian.net/browse/ED-14753
closeHistory(tr);
return handleIndentListItems(tr, editorAnalyticsAPI, inputMethod);
};
};
};