Skip to content

Commit

Permalink
#119 fix scrolled positioning
Browse files Browse the repository at this point in the history
Fix position of menu when a contenteditable is scrolled.  Not all
elements in the DOM hierarchy are considered during offsetParent
traversal during coordinate calculation, and the scrollable div is
sometimes missed and it’s scrolled state not considered.  Make sure to
consider all scrollable elements up the hierarchy.
  • Loading branch information
Jeff Collins committed Sep 22, 2015
1 parent f48371a commit e4b90f1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
26 changes: 19 additions & 7 deletions dist/mentio.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ angular.module('mentio', [])
scope.adjustScroll = function (direction) {
var menuEl = element[0];
var menuItemsList = menuEl.querySelector('ul');
var menuItem = (menuEl.querySelector('[mentio-menu-item].active') || menuEl.querySelector('[data-mentio-menu-item].active'));
var menuItem = (menuEl.querySelector('[mentio-menu-item].active') ||
menuEl.querySelector('[data-mentio-menu-item].active'));

if (scope.isFirstItemActive()) {
return menuItemsList.scrollTop = 0;
Expand Down Expand Up @@ -703,7 +704,7 @@ angular.module('mentio')
top: coordinates.top + 'px',
left: coordinates.left + 'px',
position: 'absolute',
zIndex: 100,
zIndex: 10000,
display: 'block'
});

Expand Down Expand Up @@ -1116,19 +1117,30 @@ angular.module('mentio')
var obj = element;
var iframe = ctx ? ctx.iframe : null;
while(obj) {
coordinates.left += obj.offsetLeft;
coordinates.top += obj.offsetTop;
if (obj !== getDocument().body) {
coordinates.left += obj.offsetLeft + obj.clientLeft;
coordinates.top += obj.offsetTop + obj.clientTop;
obj = obj.offsetParent;
if (!obj && iframe) {
obj = iframe;
iframe = null;
}
}
obj = element;
iframe = ctx ? ctx.iframe : null;
while(obj !== getDocument().body) {
if (obj.scrollTop && obj.scrollTop > 0) {
coordinates.top -= obj.scrollTop;
}
if (obj.scrollLeft && obj.scrollLeft > 0) {
coordinates.left -= obj.scrollLeft;
}
obj = obj.offsetParent;
obj = obj.parentNode;
if (!obj && iframe) {
obj = iframe;
iframe = null;
}
}
}
}

function getTextAreaOrInputUnderlinePosition (ctx, element, position) {
var properties = [
Expand Down
2 changes: 1 addition & 1 deletion dist/mentio.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ment.io/styles.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.editor {
height: auto;
overflow: auto;
min-height: 140px;
}

Expand Down
3 changes: 2 additions & 1 deletion src/mentio.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ angular.module('mentio', [])
scope.adjustScroll = function (direction) {
var menuEl = element[0];
var menuItemsList = menuEl.querySelector('ul');
var menuItem = (menuEl.querySelector('[mentio-menu-item].active') || menuEl.querySelector('[data-mentio-menu-item].active'));
var menuItem = (menuEl.querySelector('[mentio-menu-item].active') ||
menuEl.querySelector('[data-mentio-menu-item].active'));

if (scope.isFirstItemActive()) {
return menuItemsList.scrollTop = 0;
Expand Down
23 changes: 17 additions & 6 deletions src/mentio.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ angular.module('mentio')
top: coordinates.top + 'px',
left: coordinates.left + 'px',
position: 'absolute',
zIndex: 100,
zIndex: 10000,
display: 'block'
});

Expand Down Expand Up @@ -435,19 +435,30 @@ angular.module('mentio')
var obj = element;
var iframe = ctx ? ctx.iframe : null;
while(obj) {
coordinates.left += obj.offsetLeft;
coordinates.top += obj.offsetTop;
if (obj !== getDocument().body) {
coordinates.left += obj.offsetLeft + obj.clientLeft;
coordinates.top += obj.offsetTop + obj.clientTop;
obj = obj.offsetParent;
if (!obj && iframe) {
obj = iframe;
iframe = null;
}
}
obj = element;
iframe = ctx ? ctx.iframe : null;
while(obj !== getDocument().body) {
if (obj.scrollTop && obj.scrollTop > 0) {
coordinates.top -= obj.scrollTop;
}
if (obj.scrollLeft && obj.scrollLeft > 0) {
coordinates.left -= obj.scrollLeft;
}
obj = obj.offsetParent;
obj = obj.parentNode;
if (!obj && iframe) {
obj = iframe;
iframe = null;
}
}
}
}

function getTextAreaOrInputUnderlinePosition (ctx, element, position) {
var properties = [
Expand Down

0 comments on commit e4b90f1

Please sign in to comment.