+
+var scroll = function(place, textElement) {
+ var rect = textElement.getBoundingClientRect(),
+ scroll = $('#rng-module-documentCanvas-contentWrapper'),
+ border = rect.bottom - (place === 'top' ? rect.height : 0) - scroll.offset().top + scroll[0].scrollTop,
+ visible = scroll[0].scrollTop + {top: 0, bottom: scroll.height()}[place],
+ padding = 16,
+ toScroll = 0;
+
+ if(place === 'top' && (border - padding < visible)) {
+ toScroll = border - visible - padding;
+ } else if(place === 'bottom' && (border + padding > visible)) {
+ toScroll = border - visible + padding;
+ }
+ if(toScroll) {
+ scroll[0].scrollTop = scroll[0].scrollTop + toScroll;
+ }
+ return toScroll;
+};
+
+var getLastRectAbove = function(node, y) {
+ var rects = node.getClientRects(),
+ idx = 0,
+ rect, toret;
+ while((rect = rects[idx])) {
+ if(rect.bottom < y) {
+ toret = rect;
+ } else {
+ break;
+ }
+ idx++;
+ }
+ return toret;
+};
+
+var getFirstRectBelow = function(node, y) {
+ var rects = node.getClientRects(),
+ idx = 0,
+ rect, toret;
+ while((rect = rects[idx])) {
+ if(rect.top > y) {
+ toret = rect;
+ break;
+ }
+ idx++;
+ }
+ return toret;
+};
+