@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
40 lines (39 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fixBlockMenuPositionAndScroll = void 0;
var _browserApis = require("@atlaskit/browser-apis");
var _blockMenu = require("@atlaskit/editor-common/block-menu");
var fixBlockMenuPositionAndScroll = exports.fixBlockMenuPositionAndScroll = function fixBlockMenuPositionAndScroll(firstSelectedNode) {
var doc = (0, _browserApis.getDocument)();
if (!doc) {
return;
}
var blockMenuEl = doc.querySelector("[data-testid=\"".concat(_blockMenu.BLOCK_MENU_TEST_ID, "\"]"));
if (!(blockMenuEl !== null && blockMenuEl !== void 0 && blockMenuEl.parentElement)) {
return;
}
var scrollableContainer = doc.querySelector('[data-testid="editor-content-container"]');
if (!firstSelectedNode || !scrollableContainer) {
return;
}
var parentElement = blockMenuEl.parentElement;
var currentTop = parentElement.getBoundingClientRect().top;
var distance = firstSelectedNode.getBoundingClientRect().top - blockMenuEl.getBoundingClientRect().top;
scrollableContainer.scrollBy({
behavior: 'instant',
top: distance
});
var newTop = parentElement.getBoundingClientRect().top;
var topDifference = currentTop - newTop;
var hasTopProperty = parentElement.style.top !== '';
var hasBottomProperty = parentElement.style.bottom !== '';
if (hasBottomProperty && !hasTopProperty) {
var currentBottomValue = parseFloat(parentElement.style.bottom || '0');
parentElement.style.bottom = "".concat(currentBottomValue - topDifference, "px");
} else {
var currentTopValue = parseFloat(parentElement.style.top || '0');
parentElement.style.top = "".concat(currentTopValue + topDifference, "px");
}
};