@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
101 lines (96 loc) • 5.91 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.indentList = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _analytics = require("@atlaskit/editor-common/analytics");
var _lists = require("@atlaskit/editor-common/lists");
var _preset = require("@atlaskit/editor-common/preset");
var _utils = require("@atlaskit/editor-common/utils");
var _prosemirrorHistory = require("@atlaskit/prosemirror-history");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _types = require("../../types");
var _indentListItemsSelected = require("../actions/indent-list-items-selected");
var _moveSelectedListItems = require("../actions/move-selected-list-items");
var _find = require("../utils/find");
var _selection = require("../utils/selection");
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) { (0, _defineProperty2.default)(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; }
/**
* Handler for flexible list indentation.
* Allows indenting the first item by creating wrapper structures.
*/
var handleIndentListItems = function handleIndentListItems(tr, editorAnalyticsAPI, inputMethod) {
var _findFirstParentListN;
(0, _moveSelectedListItems.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 _preset.PassiveTransaction();
}
var $from = tr.selection.$from;
var currentListNode = (_findFirstParentListN = (0, _find.findFirstParentListNode)($from)) === null || _findFirstParentListN === void 0 ? void 0 : _findFirstParentListN.node;
var actionSubjectId = currentListNode && (0, _utils.isBulletList)(currentListNode) ? _analytics.ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : _analytics.ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
action: _analytics.ACTION.INDENTED,
actionSubject: _analytics.ACTION_SUBJECT.LIST,
actionSubjectId: actionSubjectId,
eventType: _analytics.EVENT_TYPE.TRACK,
attributes: _objectSpread(_objectSpread({}, (0, _lists.getCommonListAnalyticsAttributes)(tr)), {}, {
inputMethod: inputMethod
})
})(tr);
return tr;
};
var indentList = exports.indentList = function indentList(editorAnalyticsAPI) {
return function () {
var inputMethod = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _analytics.INPUT_METHOD.KEYBOARD;
return function (_ref) {
var tr = _ref.tr;
var $from = tr.selection.$from;
// don't indent if selection is not inside a list
if (!(0, _selection.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
(0, _prosemirrorHistory.closeHistory)(tr);
// Route to new or original implementation based on feature flag
if ((0, _expValEquals.expValEquals)('platform_editor_flexible_list_indentation', 'isEnabled', true)) {
return handleIndentListItems(tr, editorAnalyticsAPI, inputMethod);
}
var firstListItemSelectedAttributes = (0, _lists.getListItemAttributes)($from);
var parentListNode = (0, _find.findFirstParentListNode)($from);
if (!parentListNode || firstListItemSelectedAttributes && firstListItemSelectedAttributes.indentLevel === 0 && firstListItemSelectedAttributes.itemIndex === 0) {
if ((0, _selection.isInsideTableCell)(tr)) {
// dont consume tab, as table-keymap should move cursor to next cell
return null;
} else {
// Even though this is a non-operation, we don't want to send this event to the browser. Because if we return false, the browser will move the focus to another place
return new _preset.PassiveTransaction();
}
}
var currentListNode = parentListNode.node;
var actionSubjectId = (0, _utils.isBulletList)(currentListNode) ? _analytics.ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : _analytics.ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
(0, _indentListItemsSelected.indentListItemsSelected)(tr);
var maximimunNestedLevelReached = !(0, _lists.hasValidListIndentationLevel)({
tr: tr,
maxIndentation: _types.MAX_NESTED_LIST_INDENTATION
});
if (maximimunNestedLevelReached || !tr.docChanged) {
// Even though this is a non-operation, we don't want to send this event to the browser. Because if we return false, the browser will move the focus to another place
return new _preset.PassiveTransaction();
}
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
action: _analytics.ACTION.INDENTED,
actionSubject: _analytics.ACTION_SUBJECT.LIST,
actionSubjectId: actionSubjectId,
eventType: _analytics.EVENT_TYPE.TRACK,
attributes: _objectSpread(_objectSpread({}, (0, _lists.getCommonListAnalyticsAttributes)(tr)), {}, {
inputMethod: inputMethod
})
})(tr);
return tr;
};
};
};