UNPKG

@atlaskit/editor-plugin-metrics

Version:

Metrics plugin for @atlaskit/editor-core

38 lines 1.92 kB
import { InsertTypeAheadStep, LinkMetaStep } from '@atlaskit/adf-schema/steps'; import { ReplaceStep } from '@atlaskit/editor-prosemirror/transform'; const isResolvingLink = tr => { var _linkStep$getMetadata; // When links are added, there are two transactions that are fired and we want to ignore the last one where the link is resolved const linkStep = tr.steps.find(step => step instanceof LinkMetaStep); const hasReplaceStep = tr.steps.some(step => step instanceof ReplaceStep); return Boolean(hasReplaceStep && linkStep instanceof LinkMetaStep && ((_linkStep$getMetadata = linkStep.getMetadata()) === null || _linkStep$getMetadata === void 0 ? void 0 : _linkStep$getMetadata.cardAction) === 'RESOLVE'); }; const checkTypeAheadStepStage = tr => { var _tr$getMeta; // When nodes are inserted from typeahead, there are multiple transactions that are fired that changes the document // We want to ignore all but the last transaction where the node is inserted if (((_tr$getMeta = tr.getMeta('typeAheadPlugin$')) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.action) === 'INSERT_RAW_QUERY') { return 'INSERT_RAW_QUERY'; } if (!tr.getMeta('appendedTransaction')) { return false; } const insertTypeAheadStep = tr.steps.find(step => step instanceof InsertTypeAheadStep); const replaceStep = tr.steps.find(step => step instanceof ReplaceStep); if (!insertTypeAheadStep || !replaceStep) { return false; } return insertTypeAheadStep instanceof InsertTypeAheadStep && insertTypeAheadStep.stage; }; export const shouldSkipTr = tr => { if (isResolvingLink(tr)) { return true; } const typeAheadStepStage = checkTypeAheadStepStage(tr); if (typeAheadStepStage) { return typeAheadStepStage !== 'INSERTING_ITEM'; } else { // Ignore all appended transactions apart from when typeahead is inserting an item return tr.getMeta('appendedTransaction'); } };