Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/115: overflow charmap popver #130

Merged
merged 4 commits into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,42 @@ registerFormatType( type, {
};
// Pin the Popover to the caret position.
const anchorRect = () => {
return getRectangleFromRange( anchorRange );
/*
* @see https://github.com/10up/insert-special-characters/issues/115
* to know more about reasons behind the following computation.
*/
const rangeSelectionRect = getRectangleFromRange( anchorRange );
const rangeSelectionRectCenterX =
rangeSelectionRect.x + rangeSelectionRect.width / 2;

const editorViewportEl = document.querySelector(
'.interface-interface-skeleton__content'
);
const editorViewportRect = editorViewportEl.getBoundingClientRect();

/*
* The width of the charmap is 550px, so we use 550/2 = 275
* as the popover opens from the horizontal center.
*/
const canCharMapOverflowOnLeft =
275 > rangeSelectionRectCenterX - editorViewportRect.x;
const canCharMapOverflowOnRight =
275 > editorViewportRect.right - rangeSelectionRectCenterX;

/*
* The value 30 is an additional offset.
* For some reason, Popover uses Rect.x - 24px instead of the
* exact value. The value 30 is provided to add 24 + some empty space.
*/
if ( canCharMapOverflowOnLeft ) {
rangeSelectionRect.x = editorViewportRect.x + 30;
}

if ( canCharMapOverflowOnRight ) {
rangeSelectionRect.x = editorViewportRect.right - 275 - 30;
}

return rangeSelectionRect;
};
const characters = applyFilters( `${ name }-characters`, Chars );
// Display the character map when it is active.
Expand Down