@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
132 lines (123 loc) • 5.85 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getScrollableDecorations = void 0;
exports.isInlineDiffDecorationRenderableInDoc = isInlineDiffDecorationRenderableInDoc;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
/**
* True if `fragment` contains at least one inline node (text, hardBreak, emoji, mention, etc.).
* Block-only subtrees (e.g. empty paragraphs, block cards with no inline children) return false.
*/
function fragmentContainsInlineContent(fragment) {
for (var i = 0; i < fragment.childCount; i++) {
var node = fragment.child(i);
if (node.isInline) {
return true;
}
if (node.content.size > 0 && fragmentContainsInlineContent(node.content)) {
return true;
}
}
return false;
}
/**
* Returns true when an inline decoration's [from, to) range can actually show in the document:
* positions are valid, and the slice contains at least one inline node ProseMirror would paint
* (not only empty block wrappers or block-only structure).
*/
function isInlineDiffDecorationRenderableInDoc(doc, from, to) {
try {
var slice = doc.slice(from, to);
return fragmentContainsInlineContent(slice.content);
} catch (_unused) {
return false;
}
}
/**
* Checks if range1 is fully contained within range2
*/
function isRangeFullyInside(range1Start, range1End, range2Start, range2End) {
return range2Start <= range1Start && range1End <= range2End;
}
/**
* Gets scrollable decorations from a DecorationSet, filtering out overlapping decorations
* and applying various rules for diff visualization.
*
* Rules:
* 1. Only includes diff-inline, diff-widget-* and diff-block decorations
* 2. Excludes listItem diff-block decorations (never scrollable)
* 3. Deduplicates diff-block decorations with same from, to and nodeName
* 4. When `doc` is passed: excludes diff-inline decorations whose range has no inline content
* (invalid positions, or block-only slices with no text/atoms — e.g. empty blocks)
* 5. Excludes diff-inline decorations that are fully contained within a diff-block
* 6. Excludes diff-block decorations that are fully contained within a diff-inline
* 7. Results are sorted by from position, then by to position
*
* @param set - The DecorationSet to extract scrollable decorations from
* @param doc - Current document; when set, diff-inline ranges are validated against this doc
* @returns Array of scrollable decorations, sorted and deduplicated
*/
var getScrollableDecorations = exports.getScrollableDecorations = function getScrollableDecorations(set, doc) {
if (!set) {
return [];
}
var seenBlockKeys = new Set();
var allDecorations = set.find(undefined, undefined, function (spec) {
var _spec$key;
return spec.key === 'diff-inline' || ((_spec$key = spec.key) === null || _spec$key === void 0 ? void 0 : _spec$key.startsWith('diff-widget')) || spec.key === 'diff-block';
});
// First pass: filter out listItem blocks and deduplicates blocks
var filtered = allDecorations.filter(function (dec) {
var _dec$spec, _dec$spec$nodeName, _dec$spec3;
if (((_dec$spec = dec.spec) === null || _dec$spec === void 0 ? void 0 : _dec$spec.key) === 'diff-block') {
var _dec$spec2;
// Skip listItem blocks as they are not scrollable
if (((_dec$spec2 = dec.spec) === null || _dec$spec2 === void 0 ? void 0 : _dec$spec2.nodeName) === 'listItem') return false;
}
var key = "".concat(dec.from, "-").concat(dec.to, "-").concat((_dec$spec$nodeName = (_dec$spec3 = dec.spec) === null || _dec$spec3 === void 0 ? void 0 : _dec$spec3.nodeName) !== null && _dec$spec$nodeName !== void 0 ? _dec$spec$nodeName : '');
// Skip blocks that have already been seen
if (seenBlockKeys.has(key)) return false;
seenBlockKeys.add(key);
return true;
});
// Separate decorations by type for easier processing
var blocks = filtered.filter(function (d) {
var _d$spec;
return ((_d$spec = d.spec) === null || _d$spec === void 0 ? void 0 : _d$spec.key) === 'diff-block';
});
var rawInlines = filtered.filter(function (d) {
var _d$spec2;
return ((_d$spec2 = d.spec) === null || _d$spec2 === void 0 ? void 0 : _d$spec2.key) === 'diff-inline';
});
var inlines = doc !== undefined ? rawInlines.filter(function (d) {
return isInlineDiffDecorationRenderableInDoc(doc, d.from, d.to);
}) : rawInlines;
var widgets = filtered.filter(function (d) {
var _d$spec3;
return (_d$spec3 = d.spec) === null || _d$spec3 === void 0 || (_d$spec3 = _d$spec3.key) === null || _d$spec3 === void 0 ? void 0 : _d$spec3.startsWith('diff-widget');
});
// Second pass: exclude overlapping decorations
// Rules:
// - If an inline is fully inside a block, exclude the block (inline takes priority)
// - If a block is fully inside an inline, exclude the block (inline takes priority)
var nonOverlappingBlocks = blocks.filter(function (block) {
// Exclude block if:
// 1. It's fully contained within any inline, OR
// 2. It fully contains any inline
return !inlines.some(function (inline) {
return isRangeFullyInside(block.from, block.to, inline.from, inline.to) ||
// block inside inline
isRangeFullyInside(inline.from, inline.to, block.from, block.to);
} // inline inside block
);
});
// Combine all non-overlapping decorations
var result = [].concat((0, _toConsumableArray2.default)(nonOverlappingBlocks), (0, _toConsumableArray2.default)(inlines), (0, _toConsumableArray2.default)(widgets));
// Sort by from position, then by to position
result.sort(function (a, b) {
return a.from === b.from ? a.to - b.to : a.from - b.from;
});
return result;
};