UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

54 lines (52 loc) • 2.53 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.shallowEqualExcept = exports.isFocusOnlyChange = exports.FocusOnlyUpdateGuard = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _isExperimentEnabled = require("@atlaskit/platform-feature-experiments/is-experiment-enabled"); var shallowEqualExcept = exports.shallowEqualExcept = function shallowEqualExcept(previousValue, currentValue, ignoredKey) { var previousKeys = Object.keys(previousValue); var currentKeys = Object.keys(currentValue); return previousKeys.length === currentKeys.length && currentKeys.every(function (key) { return key === ignoredKey || Object.prototype.hasOwnProperty.call(previousValue, key) && Object.is(previousValue[key], currentValue[key]); }); }; /** * Returns true when editor focus is the only table plugin state value that changed. * * Table reducers preserve references for unchanged values. If a reducer rebuilds a value, this * returns false and callers take the original full-work path rather than skipping incorrectly. */ var isFocusOnlyChange = exports.isFocusOnlyChange = function isFocusOnlyChange(previousState, currentState) { return previousState.editorHasFocus !== currentState.editorHasFocus && shallowEqualExcept(previousState, currentState, 'editorHasFocus'); }; var FocusOnlyUpdateGuard = exports.FocusOnlyUpdateGuard = /*#__PURE__*/function () { function FocusOnlyUpdateGuard() { (0, _classCallCheck2.default)(this, FocusOnlyUpdateGuard); } return (0, _createClass2.default)(FocusOnlyUpdateGuard, [{ key: "skipIfFocusOnly", value: /** Returns true and records the state when this is a skippable focus-only update. */ function skipIfFocusOnly(state) { var lastProcessedState = this.lastProcessedState; if (lastProcessedState !== undefined && isFocusOnlyChange(lastProcessedState, state) && (0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_improve_inp')) { this.lastProcessedState = state; return true; } return false; } }, { key: "markProcessed", value: function markProcessed(state) { this.lastProcessedState = state; } }, { key: "reset", value: function reset() { this.lastProcessedState = undefined; } }]); }();