UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

169 lines (164 loc) • 7.95 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.buildAnchorDecorationKey = exports.SCROLL_TARGET_MARGIN_CSS_PROPERTY = exports.DiffDecorationKey = exports.DecorationFamily = exports.AnchorTypeKey = exports.AnchorDocMarginKey = void 0; exports.buildAnchorDecorationSpec = buildAnchorDecorationSpec; exports.scrollMarginTopValue = exports.scrollMarginTopStyle = exports.isDiffDecorationSpec = exports.isDiffDecoration = exports.isAnchorDecorationSpec = exports.extractDiffDescriptors = exports.buildDiffDecorationSpec = exports.buildDiffDecorationKey = exports.buildContributorTagDecorationSpec = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _defineProperty2 = _interopRequireDefault(require("@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) { (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', 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. */ var SCROLL_TARGET_MARGIN_CSS_PROPERTY = exports.SCROLL_TARGET_MARGIN_CSS_PROPERTY = '--ak-editor-diff-additional-margin'; var scrollMarginTopValue = exports.scrollMarginTopValue = "calc(".concat("var(--ds-space-400, 32px)", " + var(", SCROLL_TARGET_MARGIN_CSS_PROPERTY, ", 0px))"); var scrollMarginTopStyle = exports.scrollMarginTopStyle = "scroll-margin-top: ".concat(scrollMarginTopValue, ";"); var AnchorTypeKey = exports.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. */ 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 key includes the `diffId` 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; return "".concat(decorationKeyPrefix, "-").concat(diffId, "-").concat(isActive ? 'active' : 'inactive'); }; var buildDiffDecorationSpec = exports.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 } : {}); }; 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 buildContributorTagDecorationSpec = exports.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) }; }; 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 _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 }); }); };