UNPKG

@atlaskit/editor-plugin-metrics

Version:

Metrics plugin for @atlaskit/editor-core

103 lines (95 loc) 7.81 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getNewPluginState = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _checkTrActionType = require("./check-tr-actions/check-tr-action-type"); var _types = require("./check-tr-actions/types"); var _isNonTextUndo = require("./is-non-text-undo"); var _isSafeInsert = require("./is-safe-insert"); 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; } var textInputActions = [_types.ActionType.TEXT_INPUT, _types.ActionType.EMPTY_LINE_ADDED_OR_DELETED]; var listActions = [_types.ActionType.UPDATING_NEW_LIST_TYPE_ITEM, _types.ActionType.INSERTING_NEW_LIST_TYPE_NODE]; var continuousActions = [].concat(textInputActions, listActions, [_types.ActionType.UPDATING_STATUS]); var checkIsDesiredAction = function checkIsDesiredAction(trType, desiredActions) { return trType && desiredActions.includes(trType.type); }; var getNewActionTypeCount = function getNewActionTypeCount(shouldIncrement, currentCount) { return shouldIncrement ? currentCount + 1 : currentCount; }; var getNewPluginState = exports.getNewPluginState = function getNewPluginState(_ref) { var _trType$extraData, _previousTrType$extra; var now = _ref.now, intentToStartEditTime = _ref.intentToStartEditTime, shouldPersistActiveSession = _ref.shouldPersistActiveSession, tr = _ref.tr, pluginState = _ref.pluginState, oldState = _ref.oldState, newState = _ref.newState; var actionTypeCount = pluginState.actionTypeCount, timeOfLastTextInput = pluginState.timeOfLastTextInput, totalActionCount = pluginState.totalActionCount, previousTrType = pluginState.previousTrType, safeInsertCount = pluginState.safeInsertCount, contentSizeChanged = pluginState.contentSizeChanged; var newPluginState = _objectSpread(_objectSpread({}, pluginState), {}, { activeSessionTime: now - intentToStartEditTime, contentSizeChanged: contentSizeChanged + Math.abs(newState.doc.content.size - oldState.doc.content.size), intentToStartEditTime: intentToStartEditTime, shouldPersistActiveSession: shouldPersistActiveSession }); var trType = (0, _checkTrActionType.checkTrActionType)(tr); var newSafeInsertCount = getNewActionTypeCount((0, _isSafeInsert.isSafeInsert)(tr, oldState.tr.selection.from, trType === null || trType === void 0 ? void 0 : trType.type), safeInsertCount); var newUndoCount = getNewActionTypeCount((0, _isNonTextUndo.isNonTextUndo)(tr), actionTypeCount.undoCount); if (!trType) { return _objectSpread(_objectSpread({}, newPluginState), {}, { totalActionCount: totalActionCount + 1, timeOfLastTextInput: undefined, actionTypeCount: _objectSpread(_objectSpread({}, actionTypeCount), {}, { undoCount: newUndoCount }), previousTrType: trType, safeInsertCount: newSafeInsertCount }); } // Below are conditions for special cases which should not increase action count if the previous action was the same or was textInput // Check if tr is updating the same status node var isNotNewStatus = trType.type === _types.ActionType.UPDATING_STATUS && (previousTrType === null || previousTrType === void 0 ? void 0 : previousTrType.type) === _types.ActionType.UPDATING_STATUS && (trType === null || trType === void 0 || (_trType$extraData = trType.extraData) === null || _trType$extraData === void 0 ? void 0 : _trType$extraData.statusId) === (previousTrType === null || previousTrType === void 0 || (_previousTrType$extra = previousTrType.extraData) === null || _previousTrType$extra === void 0 ? void 0 : _previousTrType$extra.statusId); // Check if tr is adding text after adding a list node var isAddingTextToListNode = trType.type === _types.ActionType.TEXT_INPUT && checkIsDesiredAction(previousTrType, listActions); // Check if tr is adding new list item after text input var isAddingNewListItemAfterTextInput = (previousTrType === null || previousTrType === void 0 ? void 0 : previousTrType.type) === _types.ActionType.TEXT_INPUT && trType.type === _types.ActionType.UPDATING_NEW_LIST_TYPE_ITEM; // Check if tr is textInput var isTextInput = textInputActions.includes(trType.type); // Don't increment action count if tr is text input, not a new status, adding text to list node or adding new list item if timeOfLastTextInput is set var shouldNotIncrementActionCount = timeOfLastTextInput && (isTextInput || isNotNewStatus || isAddingTextToListNode || isAddingNewListItemAfterTextInput); var newTotalActionCount = getNewActionTypeCount(!shouldNotIncrementActionCount, totalActionCount); // Increment textInputCount if tr is text input and previous action was not text input or list action var newTextInputCount = getNewActionTypeCount(isTextInput && !(timeOfLastTextInput && checkIsDesiredAction(previousTrType, [].concat(textInputActions, listActions))), actionTypeCount.textInputCount); var newNodeAttrCount = getNewActionTypeCount((trType === null || trType === void 0 ? void 0 : trType.type) === _types.ActionType.CHANGING_ATTRS, actionTypeCount.nodeAttributeChangeCount); var isRepeatedAttrAction = (trType === null || trType === void 0 ? void 0 : trType.type) === _types.ActionType.CHANGING_ATTRS && (previousTrType === null || previousTrType === void 0 ? void 0 : previousTrType.type) === _types.ActionType.CHANGING_ATTRS && trType.extraData.attr === previousTrType.extraData.attr && trType.extraData.from === previousTrType.extraData.from && trType.extraData.to === previousTrType.extraData.to; var newRepeatedActionCount = getNewActionTypeCount(isRepeatedAttrAction, pluginState.repeatedActionCount); var contentMoved = getNewActionTypeCount((trType === null || trType === void 0 ? void 0 : trType.type) === _types.ActionType.MOVING_CONTENT, actionTypeCount.contentMovedCount); var newMarkChangeCount = getNewActionTypeCount((trType === null || trType === void 0 ? void 0 : trType.type) === _types.ActionType.CHANGING_MARK, actionTypeCount.markChangeCount); var newContentDeletedCount = getNewActionTypeCount((trType === null || trType === void 0 ? void 0 : trType.type) === _types.ActionType.DELETING_CONTENT, actionTypeCount.contentDeletedCount); // timeOfLastTextInput should be set if tr includes continuous text input on the same node var shouldSetTimeOfLastTextInput = continuousActions.includes(trType.type) || isNotNewStatus; return _objectSpread(_objectSpread({}, newPluginState), {}, { totalActionCount: newTotalActionCount, timeOfLastTextInput: shouldSetTimeOfLastTextInput ? now : undefined, actionTypeCount: _objectSpread(_objectSpread({}, actionTypeCount), {}, { textInputCount: newTextInputCount, undoCount: newUndoCount, nodeAttributeChangeCount: newNodeAttrCount, contentMovedCount: contentMoved, markChangeCount: newMarkChangeCount, contentDeletedCount: newContentDeletedCount }), previousTrType: trType, repeatedActionCount: newRepeatedActionCount, safeInsertCount: newSafeInsertCount }); };