@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
129 lines (127 loc) • 5.91 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buildAnchorDecorationKey = exports.DiffDecorationKey = exports.DecorationFamily = exports.AnchorTypeKey = exports.AnchorDocMarginKey = void 0;
exports.buildAnchorDecorationSpec = buildAnchorDecorationSpec;
exports.isDiffDecorationSpec = exports.isDiffDecoration = exports.isAnchorDecorationSpec = exports.extractDiffDescriptors = exports.buildDiffDecorationSpec = exports.buildDiffDecorationKey = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _isExtendedEnabled = require("../isExtendedEnabled");
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; }
/**
* Decoration families produced by the show-diff plugin.
* Each family owns its own key space and spec shape.
*/
var DecorationFamily = exports.DecorationFamily = {
diff: 'diff',
anchor: 'anchor'
};
var AnchorTypeKey = exports.AnchorTypeKey = {
from: 'from',
to: 'to',
left: 'left',
docMargin: 'doc-margin'
};
/**
* The diff-decoration kinds produced by the show-diff plugin. Each value is
* the leading segment of the generated key, so a diff decoration's kind can be
* matched with `key?.startsWith(DiffDecorationKey.inline)` etc.
*/
var DiffDecorationKey = exports.DiffDecorationKey = {
inline: "".concat(DecorationFamily.diff, "-inline"),
block: "".concat(DecorationFamily.diff, "-block"),
widget: "".concat(DecorationFamily.diff, "-widget")
};
var AnchorDocMarginKey = exports.AnchorDocMarginKey = "".concat(DecorationFamily.anchor, "-").concat(AnchorTypeKey.docMargin);
var buildAnchorDecorationKey = exports.buildAnchorDecorationKey = function buildAnchorDecorationKey(_ref) {
var diffId = _ref.diffId,
anchorType = _ref.anchorType;
return "".concat(DecorationFamily.anchor, "-").concat(diffId).concat(anchorType ? "-".concat(anchorType) : '');
};
/**
* Builds a diff decoration key. The extended experience includes the `diffId`
* in the key so independently rendered decorations for the same type remain
* distinguishable.
*/
var buildDiffDecorationKey = exports.buildDiffDecorationKey = function buildDiffDecorationKey(_ref2) {
var decorationKeyPrefix = _ref2.decorationKeyPrefix,
isActive = _ref2.isActive,
diffId = _ref2.diffId,
diffType = _ref2.diffType;
if ((0, _isExtendedEnabled.isExtendedEnabled)(diffType)) {
return "".concat(decorationKeyPrefix, "-").concat(diffId, "-").concat(isActive ? 'active' : 'inactive');
}
return isActive !== undefined ? "".concat(decorationKeyPrefix, "-").concat(isActive ? 'active' : 'inactive') : decorationKeyPrefix;
};
var buildDiffDecorationSpec = exports.buildDiffDecorationSpec = function buildDiffDecorationSpec(_ref3) {
var decorationType = _ref3.decorationType,
diffId = _ref3.diffId,
isActive = _ref3.isActive,
nodeName = _ref3.nodeName,
side = _ref3.side,
diffType = _ref3.diffType;
return _objectSpread(_objectSpread({
decorationFamily: DecorationFamily.diff,
decorationType: decorationType,
diffId: diffId,
key: buildDiffDecorationKey({
decorationKeyPrefix: DiffDecorationKey[decorationType],
diffId: diffId,
isActive: isActive,
diffType: diffType
})
}, nodeName ? {
nodeName: nodeName
} : {}), side !== undefined ? {
side: side
} : {});
};
function buildAnchorDecorationSpec(_ref4) {
var anchorType = _ref4.anchorType,
diffId = _ref4.diffId,
side = _ref4.side;
if (anchorType === AnchorTypeKey.docMargin) {
return _objectSpread({
decorationFamily: DecorationFamily.anchor,
anchorType: anchorType,
key: AnchorDocMarginKey
}, side !== undefined ? {
side: side
} : {});
}
if (!diffId) {
throw new Error("diffId is required for anchor type \"".concat(anchorType, "\""));
}
return _objectSpread({
decorationFamily: DecorationFamily.anchor,
anchorType: anchorType,
diffId: diffId,
key: buildAnchorDecorationKey({
diffId: diffId,
anchorType: anchorType
})
}, side !== undefined ? {
side: side
} : {});
}
var isDiffDecorationSpec = exports.isDiffDecorationSpec = function isDiffDecorationSpec(spec) {
return Boolean(spec && (0, _typeof2.default)(spec) === 'object' && 'decorationFamily' in spec && spec.decorationFamily === DecorationFamily.diff);
};
var isAnchorDecorationSpec = exports.isAnchorDecorationSpec = function isAnchorDecorationSpec(spec) {
return Boolean(spec && (0, _typeof2.default)(spec) === 'object' && 'decorationFamily' in spec && spec.decorationFamily === DecorationFamily.anchor);
};
var isDiffDecoration = exports.isDiffDecoration = function isDiffDecoration(decoration) {
return isDiffDecorationSpec(decoration.spec);
};
var extractDiffDescriptors = exports.extractDiffDescriptors = function extractDiffDescriptors(decorations) {
return decorations.find(undefined, undefined, isDiffDecorationSpec).filter(isDiffDecoration).map(function (_ref5) {
var spec = _ref5.spec;
return {
id: spec.diffId,
type: spec.decorationType
};
});
};