@atlaskit/editor-plugin-placeholder-text
Version:
placeholder text plugin for @atlaskit/editor-core
56 lines (54 loc) • 2.91 kB
JavaScript
const getCursorHeightFrom = node => parseFloat(window.getComputedStyle(node, undefined).lineHeight || '');
export const getOffsetParent = (editorViewDom, popupsMountPoint) => 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;
export const getNearestNonTextNode = node =>
// 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] | |
*/
const convertFixedCoordinatesToAbsolutePositioning = (coordinates, offsetParent, cursorHeight) => {
var _coordinates$left, _coordinates$right, _coordinates$top, _coordinates$top2;
const {
left: offsetParentLeft,
top: offsetParentTop,
height: offsetParentHeight
} = offsetParent.getBoundingClientRect();
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)
};
};
export const handlePositionCalculatedWith = (offsetParent, node, getCurrentFixedCoordinates) => position => {
if (!offsetParent) {
return position;
}
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const target = getNearestNonTextNode(node);
const cursorHeight = getCursorHeightFrom(target);
const fixedCoordinates = getCurrentFixedCoordinates();
const 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
};
};