@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
68 lines (64 loc) • 2.8 kB
JavaScript
import { isDiffDecoration } from './decorations/decorationKeys';
/**
* Extra space above the scrolled-to element so it does not sit flush under the
* viewport edge (helps with sticky table headers, toolbars, etc.).
*
* Implemented with `scroll-margin-top` so we still use the browser’s native
* `scrollIntoView`, which scrolls every relevant scrollport (nested containers
* and the window). A single manual `scrollTop` on one ancestor often misses
* outer scroll or mis-identifies the active scroll container.
*/
const SCROLL_TOP_MARGIN_PX = 100;
/**
* Returns the resolved HTMLElement for a given DOM node, walking up to the
* parent element if the node itself is not an Element (e.g. a text node).
*/
function scrollToSelection(node) {
const element = node instanceof Element ? node : (node === null || node === void 0 ? void 0 : node.parentElement) instanceof Element ? node.parentElement : null;
if (!(element instanceof HTMLElement)) {
return;
}
// scroll-margin is included in scroll-into-view math; it does not change layout.
const previousScrollMarginTop = element.style.scrollMarginTop;
element.style.scrollMarginTop = `${SCROLL_TOP_MARGIN_PX}px`;
try {
element.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
} finally {
element.style.scrollMarginTop = previousScrollMarginTop;
}
}
/**
* Schedules scrolling to the decoration at the given index after the next frame. Defaults to the
* first decoration when no index is provided.
*
* @returns A function that cancels the scheduled `requestAnimationFrame` if it has not run yet.
*/
export const scrollToDecoration = (view, decorations, activeIndex = 0) => {
const decoration = decorations[activeIndex];
if (!decoration) {
return () => {};
}
let rafId = requestAnimationFrame(() => {
rafId = null;
if (isDiffDecoration(decoration) && decoration.spec.decorationType === 'widget') {
var _decoration$type;
// @ts-expect-error - decoration.type is not typed public API
const widgetDom = decoration === null || decoration === void 0 ? void 0 : (_decoration$type = decoration.type) === null || _decoration$type === void 0 ? void 0 : _decoration$type.toDOM;
scrollToSelection(widgetDom);
} else {
var _view$domAtPos;
const targetNode = view.nodeDOM(decoration === null || decoration === void 0 ? void 0 : decoration.from);
const node = targetNode instanceof Element ? targetNode : (_view$domAtPos = view.domAtPos(decoration === null || decoration === void 0 ? void 0 : decoration.from)) === null || _view$domAtPos === void 0 ? void 0 : _view$domAtPos.node;
scrollToSelection(node);
}
});
return () => {
if (rafId !== null) {
cancelAnimationFrame(rafId);
rafId = null;
}
};
};