Skip to content

Instantly share code, notes, and snippets.

@levvsha
Created June 24, 2018 10:17
Show Gist options
  • Select an option

  • Save levvsha/c1b20a0a86a7072bd86ff4a923337f2e to your computer and use it in GitHub Desktop.

Select an option

Save levvsha/c1b20a0a86a7072bd86ff4a923337f2e to your computer and use it in GitHub Desktop.
export const getSelectionCoords = (selectionRange) => {
const editorBounds = document.getElementById('editor-container').getBoundingClientRect();
const rangeBounds = selectionRange.getBoundingClientRect();
const rangeWidth = rangeBounds.right - rangeBounds.left;
// 107px is width of inline toolbar
const offsetLeft = (rangeBounds.left - editorBounds.left) + (rangeWidth / 2) - (107 / 2);
// 42px is height of inline toolbar
const offsetTop = rangeBounds.top - editorBounds.top - 42;
return { offsetLeft, offsetTop };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment