@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
31 lines (30 loc) • 1.55 kB
JavaScript
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
export const shallowEqualExcept = (previousValue, currentValue, ignoredKey) => {
const previousKeys = Object.keys(previousValue);
const currentKeys = Object.keys(currentValue);
return previousKeys.length === currentKeys.length && currentKeys.every(key => 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.
*/
export const isFocusOnlyChange = (previousState, currentState) => previousState.editorHasFocus !== currentState.editorHasFocus && shallowEqualExcept(previousState, currentState, 'editorHasFocus');
export class FocusOnlyUpdateGuard {
/** Returns true and records the state when this is a skippable focus-only update. */
skipIfFocusOnly(state) {
const lastProcessedState = this.lastProcessedState;
if (lastProcessedState !== undefined && isFocusOnlyChange(lastProcessedState, state) && isExperimentEnabled('platform_editor_improve_inp')) {
this.lastProcessedState = state;
return true;
}
return false;
}
markProcessed(state) {
this.lastProcessedState = state;
}
reset() {
this.lastProcessedState = undefined;
}
}