@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
48 lines (46 loc) • 2.81 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _defineProperty(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; }
import { isDiffDecoration } from '../../pm-plugins/decorations/decorationKeys';
import { formatContributorLabel } from '../ContributorTag/contributorLabel';
import { diffNavigationMessages } from './messages';
/**
* The tag that captions the given decoration, if any. `linkedDiffIds` is checked as well as
* `diffId`, because a replacement's two decorations share one tag and navigation can land on either.
*/
var findTagFor = function findTagFor(decoration, contributorTags) {
if (!(contributorTags !== null && contributorTags !== void 0 && contributorTags.length) || !isDiffDecoration(decoration)) {
return undefined;
}
var diffId = decoration.spec.diffId;
return contributorTags.find(function (tag) {
var _tag$linkedDiffIds;
return tag.diffId === diffId || ((_tag$linkedDiffIds = tag.linkedDiffIds) === null || _tag$linkedDiffIds === void 0 ? void 0 : _tag$linkedDiffIds.includes(diffId));
});
};
/**
* What a screen reader should say once the reader has stepped onto a change: where they are in the
* diff, and — when the plugin can credit the change — who made it. Stepping otherwise only scrolls,
* which is silent.
*
* Returns `undefined` for an index that resolves no change, so the caller says nothing at all
* rather than "Change 4 of 3".
*/
export var getActiveDiffAnnouncement = function getActiveDiffAnnouncement(_ref) {
var activeIndex = _ref.activeIndex,
contributorTags = _ref.contributorTags,
decorations = _ref.decorations,
intl = _ref.intl;
var activeDecoration = decorations[activeIndex];
if (!activeDecoration) {
return undefined;
}
var position = {
index: activeIndex + 1,
total: decorations.length
};
var tag = findTagFor(activeDecoration, contributorTags);
return tag ? intl.formatMessage(diffNavigationMessages.activeChangeWithContributor, _objectSpread(_objectSpread({}, position), {}, {
contributor: formatContributorLabel(tag, intl.formatMessage).fullLabel
})) : intl.formatMessage(diffNavigationMessages.activeChange, position);
};