UNPKG

@atlaskit/editor-plugin-list

Version:

List plugin for @atlaskit/editor-core

113 lines (107 loc) 4.93 kB
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD, OUTDENT_SCENARIOS } 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'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { moveSelectedListItems } from '../actions/move-selected-list-items'; import { outdentListItemsSelected as outdentListAction } from '../actions/outdent-list-items-selected'; import { getRestartListsAttributes } from '../utils/analytics'; import { findFirstParentListNode } from '../utils/find'; import { isInsideListItem, isInsideTableCell } from '../utils/selection'; /** * Handler for flexible list outdentation. * Lifts items independently and cleans up wrapper structures. */ const handleOutdentListItems = (tr, editorAnalyticsAPI, inputMethod) => { var _findFirstParentListN; moveSelectedListItems(tr, -1); // If no changes were made, handle based on context if (!tr.docChanged) { // If inside table cell and can't outdent list, then let it handle by table keymap return !isInsideTableCell(tr) ? new PassiveTransaction() : null; } // Determine the action subject ID from the parent list type const { selection: { $from } } = tr; const currentListNode = (_findFirstParentListN = findFirstParentListNode($from)) === null || _findFirstParentListN === void 0 ? void 0 : _findFirstParentListN.node; const actionSubjectId = currentListNode && isBulletList(currentListNode) ? ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER; // Get restart list attributes for analytics const restartListsAttributes = {}; const { outdentScenario, splitListStartNumber } = getRestartListsAttributes(tr); if (outdentScenario === OUTDENT_SCENARIOS.SPLIT_LIST) { restartListsAttributes.outdentScenario = outdentScenario; restartListsAttributes.splitListStartNumber = splitListStartNumber; } // Attach analytics event with flexibleIndentation attribute editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({ action: ACTION.OUTDENTED, actionSubject: ACTION_SUBJECT.LIST, actionSubjectId, eventType: EVENT_TYPE.TRACK, attributes: { ...getCommonListAnalyticsAttributes(tr), ...restartListsAttributes, inputMethod } })(tr); return tr; }; export const outdentList = editorAnalyticsAPI => (inputMethod = INPUT_METHOD.KEYBOARD) => { return function ({ tr }) { if (!isInsideListItem(tr)) { return null; } const { $from } = tr.selection; const parentListNode = findFirstParentListNode($from); if (!parentListNode) { // 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 PassiveTransaction(); } // Save the history, so it could undo/revert to the same state before the outdent, see https://product-fabric.atlassian.net/browse/ED-14753 closeHistory(tr); // Route to new or original implementation based on feature flag if (expValEquals('platform_editor_flexible_list_indentation', 'isEnabled', true)) { return handleOutdentListItems(tr, editorAnalyticsAPI, inputMethod); } const actionSubjectId = isBulletList(parentListNode.node) ? ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER; const customTr = tr; outdentListAction(customTr); if (!customTr || !customTr.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 // If inside table cell and can't outdent list, then let it handle by table keymap return !isInsideTableCell(customTr) ? new PassiveTransaction() : null; } const restartListsAttributes = {}; const { outdentScenario, splitListStartNumber } = getRestartListsAttributes(customTr); if (outdentScenario === OUTDENT_SCENARIOS.SPLIT_LIST) { restartListsAttributes.outdentScenario = outdentScenario; restartListsAttributes.splitListStartNumber = splitListStartNumber; } editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({ action: ACTION.OUTDENTED, actionSubject: ACTION_SUBJECT.LIST, actionSubjectId, eventType: EVENT_TYPE.TRACK, attributes: { ...getCommonListAnalyticsAttributes(customTr), ...restartListsAttributes, inputMethod } })(customTr); return customTr; }; };