UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

160 lines (156 loc) • 6.75 kB
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; } /** * 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', contributorTag: 'contributor-tag' }; /** * Navigation measures the target table header and writes this variable on the * editor root immediately before scrolling. Diff decorations inherit it, so * native scrolling leaves room for the header without DOM mutation on the * ProseMirror-rendered suggestion. */ export var SCROLL_TARGET_MARGIN_CSS_PROPERTY = '--ak-editor-diff-additional-margin'; export var scrollMarginTopValue = "calc(".concat("var(--ds-space-400, 32px)", " + var(", SCROLL_TARGET_MARGIN_CSS_PROPERTY, ", 0px))"); export var scrollMarginTopStyle = "scroll-margin-top: ".concat(scrollMarginTopValue, ";"); export var AnchorTypeKey = { from: 'from', to: 'to', left: 'left', docMargin: 'doc-margin', /** The decorated block itself, which its contributor tag positions against. */ tag: 'tag' }; /** * The host widget one contributor tag renders into. Its own family, so the tag never reaches the * consumers that read diff decorations — change counting, navigation and `getDeletedWidgets`. */ /** * 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 key includes the `diffId` 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; return "".concat(decorationKeyPrefix, "-").concat(diffId, "-").concat(isActive ? 'active' : 'inactive'); }; export var buildDiffDecorationSpec = function buildDiffDecorationSpec(_ref3) { var attributionKey = _ref3.attributionKey, colorScheme = _ref3.colorScheme, decorationType = _ref3.decorationType, diffId = _ref3.diffId, isActive = _ref3.isActive, isInserted = _ref3.isInserted, leftAnchorId = _ref3.leftAnchorId, nodeName = _ref3.nodeName, side = _ref3.side; return _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({ decorationFamily: DecorationFamily.diff, decorationType: decorationType, diffId: diffId, key: buildDiffDecorationKey({ decorationKeyPrefix: DiffDecorationKey[decorationType], diffId: diffId, isActive: isActive }) }, attributionKey ? { attributionKey: attributionKey } : {}), attributionKey && isActive !== undefined ? { isActive: isActive } : {}), isInserted !== undefined ? { isInserted: isInserted } : {}), leftAnchorId ? { leftAnchorId: leftAnchorId } : {}), colorScheme ? { colorScheme: colorScheme } : {}), 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 buildContributorTagDecorationSpec = function buildContributorTagDecorationSpec(diffId) { return { decorationFamily: DecorationFamily.contributorTag, diffId: diffId, // Stable across recalculations, so ProseMirror keeps the host element the tag is portalled into. key: "".concat(DecorationFamily.contributorTag, "-").concat(diffId) }; }; 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 _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, spec.colorScheme ? { colorScheme: spec.colorScheme } : {}), {}, { id: spec.diffId }, spec.leftAnchorId ? { leftAnchorId: spec.leftAnchorId } : {}), spec.isInserted !== undefined ? { isInserted: spec.isInserted } : {}), {}, { type: spec.decorationType }); }); };