@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
54 lines (52 loc) • 3.13 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getActiveDiffAnnouncement = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _decorationKeys = require("../../pm-plugins/decorations/decorationKeys");
var _contributorLabel = require("../ContributorTag/contributorLabel");
var _messages = require("./messages");
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) { (0, _defineProperty2.default)(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; }
/**
* 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) || !(0, _decorationKeys.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".
*/
var getActiveDiffAnnouncement = exports.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(_messages.diffNavigationMessages.activeChangeWithContributor, _objectSpread(_objectSpread({}, position), {}, {
contributor: (0, _contributorLabel.formatContributorLabel)(tag, intl.formatMessage).fullLabel
})) : intl.formatMessage(_messages.diffNavigationMessages.activeChange, position);
};