@atlaskit/editor-plugin-metrics
Version:
Metrics plugin for @atlaskit/editor-core
46 lines • 2.08 kB
JavaScript
import { InsertTypeAheadStep, LinkMetaStep } from '@atlaskit/adf-schema/steps';
import { ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
var isResolvingLink = function 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
var linkStep = tr.steps.find(function (step) {
return step instanceof LinkMetaStep;
});
var hasReplaceStep = tr.steps.some(function (step) {
return step instanceof ReplaceStep;
});
return Boolean(hasReplaceStep && linkStep instanceof LinkMetaStep && ((_linkStep$getMetadata = linkStep.getMetadata()) === null || _linkStep$getMetadata === void 0 ? void 0 : _linkStep$getMetadata.cardAction) === 'RESOLVE');
};
var checkTypeAheadStepStage = function 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;
}
var insertTypeAheadStep = tr.steps.find(function (step) {
return step instanceof InsertTypeAheadStep;
});
var replaceStep = tr.steps.find(function (step) {
return step instanceof ReplaceStep;
});
if (!insertTypeAheadStep || !replaceStep) {
return false;
}
return insertTypeAheadStep instanceof InsertTypeAheadStep && insertTypeAheadStep.stage;
};
export var shouldSkipTr = function shouldSkipTr(tr) {
if (isResolvingLink(tr)) {
return true;
}
var 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');
}
};