@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
121 lines (119 loc) • 4.96 kB
JavaScript
import _typeof from "@babel/runtime/helpers/typeof";
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 { isExtendedEnabled } from '../isExtendedEnabled';
/**
* Decoration families produced by the show-diff plugin.
* Each family owns its own key space and spec shape.
*/
export var DecorationFamily = {
diff: 'diff',
anchor: 'anchor'
};
export var 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.
*/
export var DiffDecorationKey = {
inline: "".concat(DecorationFamily.diff, "-inline"),
block: "".concat(DecorationFamily.diff, "-block"),
widget: "".concat(DecorationFamily.diff, "-widget")
};
export var AnchorDocMarginKey = "".concat(DecorationFamily.anchor, "-").concat(AnchorTypeKey.docMargin);
export var 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.
*/
export var buildDiffDecorationKey = function buildDiffDecorationKey(_ref2) {
var decorationKeyPrefix = _ref2.decorationKeyPrefix,
isActive = _ref2.isActive,
diffId = _ref2.diffId,
diffType = _ref2.diffType;
if (isExtendedEnabled(diffType)) {
return "".concat(decorationKeyPrefix, "-").concat(diffId, "-").concat(isActive ? 'active' : 'inactive');
}
return isActive !== undefined ? "".concat(decorationKeyPrefix, "-").concat(isActive ? 'active' : 'inactive') : decorationKeyPrefix;
};
export var 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
} : {});
};
export 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
} : {});
}
export var isDiffDecorationSpec = function isDiffDecorationSpec(spec) {
return Boolean(spec && _typeof(spec) === 'object' && 'decorationFamily' in spec && spec.decorationFamily === DecorationFamily.diff);
};
export var isAnchorDecorationSpec = function isAnchorDecorationSpec(spec) {
return Boolean(spec && _typeof(spec) === 'object' && 'decorationFamily' in spec && spec.decorationFamily === DecorationFamily.anchor);
};
export var isDiffDecoration = function isDiffDecoration(decoration) {
return isDiffDecorationSpec(decoration.spec);
};
export var 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
};
});
};