UNPKG

@atlaskit/editor-plugin-placeholder-text

Version:

placeholder text plugin for @atlaskit/editor-core

70 lines (67 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handlePositionCalculatedWith = exports.getOffsetParent = exports.getNearestNonTextNode = void 0; var getCursorHeightFrom = function getCursorHeightFrom(node) { return parseFloat(window.getComputedStyle(node, undefined).lineHeight || ''); }; var getOffsetParent = exports.getOffsetParent = function getOffsetParent(editorViewDom, popupsMountPoint) { return popupsMountPoint ? // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting popupsMountPoint.offsetParent : // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting editorViewDom.offsetParent; }; var getNearestNonTextNode = exports.getNearestNonTextNode = function getNearestNonTextNode(node) { return ( // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting node.nodeType === Node.TEXT_NODE ? node.parentNode : node ); }; /** * We need to translate the co-ordinates because `coordsAtPos` returns co-ordinates * relative to `window`. And, also need to adjust the cursor container height. * (0, 0) * +--------------------- [window] ---------------------+ * | (left, top) +-------- [Offset Parent] --------+ | * | {coordsAtPos} | [Cursor] <- cursorHeight | | * | | [FloatingToolbar] | | */ var convertFixedCoordinatesToAbsolutePositioning = function convertFixedCoordinatesToAbsolutePositioning(coordinates, offsetParent, cursorHeight) { var _coordinates$left, _coordinates$right, _coordinates$top, _coordinates$top2; var _offsetParent$getBoun = offsetParent.getBoundingClientRect(), offsetParentLeft = _offsetParent$getBoun.left, offsetParentTop = _offsetParent$getBoun.top, offsetParentHeight = _offsetParent$getBoun.height; return { left: ((_coordinates$left = coordinates.left) !== null && _coordinates$left !== void 0 ? _coordinates$left : 0) - offsetParentLeft, right: ((_coordinates$right = coordinates.right) !== null && _coordinates$right !== void 0 ? _coordinates$right : 0) - offsetParentLeft, top: ((_coordinates$top = coordinates.top) !== null && _coordinates$top !== void 0 ? _coordinates$top : 0) - (offsetParentTop - cursorHeight) + offsetParent.scrollTop, bottom: offsetParentHeight - (((_coordinates$top2 = coordinates.top) !== null && _coordinates$top2 !== void 0 ? _coordinates$top2 : 0) - (offsetParentTop - cursorHeight) - offsetParent.scrollTop) }; }; var handlePositionCalculatedWith = exports.handlePositionCalculatedWith = function handlePositionCalculatedWith(offsetParent, node, getCurrentFixedCoordinates) { return function (position) { if (!offsetParent) { return position; } // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var target = getNearestNonTextNode(node); var cursorHeight = getCursorHeightFrom(target); var fixedCoordinates = getCurrentFixedCoordinates(); var absoluteCoordinates = convertFixedCoordinatesToAbsolutePositioning(fixedCoordinates, offsetParent, cursorHeight); return { left: position.left ? absoluteCoordinates.left : undefined, right: position.right ? absoluteCoordinates.right : undefined, top: position.top ? absoluteCoordinates.top : undefined, bottom: position.bottom ? absoluteCoordinates.bottom : undefined }; }; };