Make caret and avatar overlays rather than inline content

Having the caret and avatar display inside the cursor node causes text
wrapping and other element shifts to occur in some browsers (mainly FF)
due to there being layout impacts from the almost-but-not-quite 0-width
cursor element.

Moving to non-inline overlays for these bits of chrome allows the cursor
to be hidden completely.

TODO
This commit introduces a regression when composing text using an IME
as the overlay doesn't advance properly during composition.

Fixes #86
This commit is contained in:
Philip Peitsch
2014-05-17 00:43:42 +10:00
parent 3c481db677
commit c0332ed1f9
5 changed files with 101 additions and 87 deletions
+23
View File
@@ -691,6 +691,29 @@
}
this.adaptRangeDifferenceToZoomLevel = adaptRangeDifferenceToZoomLevel;
/**
* Translate a given child client rectangle to be relative to the parent's rectangle.
* Adapt to the provided zoom level as per adaptRangeDifferenceToZoomLevel.
*
* IMPORTANT: due to browser quirks, any element bounding client rect used with this function
* MUST be retrieved using DomUtils.getBoundingClientRect.
*
* @param {!ClientRect|!Object.<!string, !number>} child
* @param {!ClientRect|!Object.<!string, !number>} parent
* @param {!number} zoomLevel
* @return {!ClientRect|{top: !number, left: !number, bottom: !number, right: !number, width: !number, height: !number}}
*/
this.translateRect = function(child, parent, zoomLevel) {
return {
top: adaptRangeDifferenceToZoomLevel(child.top - parent.top, zoomLevel),
left: adaptRangeDifferenceToZoomLevel(child.left - parent.left, zoomLevel),
bottom: adaptRangeDifferenceToZoomLevel(child.bottom - parent.top, zoomLevel),
right: adaptRangeDifferenceToZoomLevel(child.right - parent.left, zoomLevel),
width: adaptRangeDifferenceToZoomLevel(child.width, zoomLevel),
height: adaptRangeDifferenceToZoomLevel(child.height, zoomLevel)
};
};
/**
* Get the bounding client rect for the specified node.
* This function attempts to cope with various browser quirks, ideally
+67 -44
View File
@@ -33,7 +33,7 @@
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
*/
/*global core, gui, ops, runtime, Node*/
/*global core, gui, odf, ops, runtime, Node*/
/**
@@ -60,10 +60,10 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
caretElement,
/**@type{!gui.Avatar}*/
avatar,
/**@type{!Element}*/
cursorNode,
/**@type{?Element}*/
overlayElement,
canvas = cursor.getDocument().getCanvas(),
odfUtils = new odf.OdfUtils(),
domUtils = new core.DomUtils(),
/**@type{!core.ScheduledTask}*/
redrawTask,
@@ -132,7 +132,7 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
* @return {!number}
*/
function verticalOverlap(cursorNode, rangeRect) {
var cursorRect = cursorNode.getBoundingClientRect(),
var cursorRect = domUtils.getBoundingClientRect(cursorNode),
intersectTop = 0,
intersectBottom = 0;
@@ -148,15 +148,21 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
* Get the client rectangle for the nearest selection point to the caret.
* This works on the assumption that the next or previous sibling is likely to
* be a text node that will provide an accurate rectangle for the caret's desired
* height and vertical position.
* @return {?ClientRect}
* position. The horizontal position of the caret is specified in the "right" property
* as a caret generally appears to the right of the character or object is represents.
*
* @return {!{height: !number, top: !number, right: !number}}
*/
function getSelectionRect() {
var range = cursor.getSelectedRange().cloneRange(),
node = cursor.getNode(),
caretRectangle,
nextRectangle,
selectionRectangle = null,
nodeLength;
selectionRectangle,
nodeLength,
paragraph,
rootRect = /**@type{!ClientRect}*/(domUtils.getBoundingClientRect(canvas.getSizer())),
useLeftEdge = false;
// TODO this might be able to use OdfUtils.scanLeft & scanRight behaviours to find the next odf element
// By default, assume the selection height should reflect the height of the previousSibling's last client rect
@@ -185,11 +191,45 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
// line, rather than the line the cursor element is now actually on.
if (!selectionRectangle || verticalOverlap(node, nextRectangle) > verticalOverlap(node, selectionRectangle)) {
selectionRectangle = nextRectangle;
useLeftEdge = true;
}
}
}
return selectionRectangle;
if (!selectionRectangle) {
// Handle the case where a cursor ends up inside an empty paragraph. There are no nearby text elements
// to get a rect from, so use the paragraph BCR instead, as it's better than nothing
paragraph = odfUtils.getParagraphElement(node);
if (paragraph) {
selectionRectangle = domUtils.getBoundingClientRect(paragraph);
useLeftEdge = true;
}
}
if (!selectionRectangle) {
// Finally, if there is still no selection rectangle, crawl up the DOM hierarchy the cursor node is in
// and try and find something visible to use. Less ideal than actually having a visible rect... better than
// crashing or hiding the caret entirely though :)
runtime.log("WARN: No suitable client rectangle found for visual caret for " + cursor.getMemberId());
// TODO are the better fallbacks than this?
while (node) {
if (/**@type{!Element}*/(node).getClientRects().length > 0) {
selectionRectangle = domUtils.getBoundingClientRect(node);
useLeftEdge = true;
break;
}
node = node.parentNode;
}
}
selectionRectangle = domUtils.translateRect(/**@type{!ClientRect}*/(selectionRectangle), rootRect, canvas.getZoomLevel());
caretRectangle = {
top: selectionRectangle.top,
height: selectionRectangle.height,
right: useLeftEdge ? selectionRectangle.left : selectionRectangle.right
};
return caretRectangle;
}
/**
@@ -206,43 +246,23 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
*/
function updateOverlayHeightAndPosition() {
var selectionRect = getSelectionRect(),
canvas = /**@type{!odf.OdfCanvas}*/(cursor.getDocument().getCanvas()),
zoomLevel = canvas.getZoomLevel(),
rootRect = domUtils.getBoundingClientRect(canvas.getSizer()),
overlayRect,
caretStyle;
if (selectionRect) {
// Reset the top back to 0 so that the new client rect calculations are simple
// If this isn't done, the existing span's top setting would need to be taken into
// account (and converted if not in pixels) when calculating the new top value
caretOverlay.style.top = "0";
overlayRect = domUtils.getBoundingClientRect(caretOverlay);
if (selectionRect.height < MIN_OVERLAY_HEIGHT_PX) {
// ClientRect's are read-only, so a whole new object is necessary to modify these values
selectionRect = {
top: selectionRect.top - ((MIN_OVERLAY_HEIGHT_PX - selectionRect.height) / 2),
height: MIN_OVERLAY_HEIGHT_PX
};
}
caretOverlay.style.height = domUtils.adaptRangeDifferenceToZoomLevel(selectionRect.height, zoomLevel) + 'px';
caretOverlay.style.top = domUtils.adaptRangeDifferenceToZoomLevel(selectionRect.top - overlayRect.top, zoomLevel) + 'px';
} else {
// fallback to a relatively safe set of values
// This can happen if the caret is not currently visible, or is in the middle
// of a collection of nodes that have no client rects. In this case, the caret
// will fall back to the defaults defined in webodf.css
caretOverlay.style.removeProperty("height");
caretOverlay.style.removeProperty("top");
if (selectionRect.height < MIN_OVERLAY_HEIGHT_PX) {
// ClientRect's are read-only, so a whole new object is necessary to modify these values
selectionRect = {
top: selectionRect.top - ((MIN_OVERLAY_HEIGHT_PX - selectionRect.height) / 2),
height: MIN_OVERLAY_HEIGHT_PX,
right: selectionRect.right
};
}
caretOverlay.style.height = selectionRect.height + "px";
caretOverlay.style.top = selectionRect.top + "px";
caretOverlay.style.left = selectionRect.right + "px";
// Update the overlay element
if (overlayElement) {
caretStyle = runtime.getWindow().getComputedStyle(caretElement, null);
overlayRect = domUtils.getBoundingClientRect(caretOverlay);
overlayElement.style.bottom = domUtils.adaptRangeDifferenceToZoomLevel(rootRect.bottom - overlayRect.bottom, zoomLevel) + 'px';
overlayElement.style.left = domUtils.adaptRangeDifferenceToZoomLevel(overlayRect.right - rootRect.left, zoomLevel) + 'px';
if (caretStyle.font) {
overlayElement.style.font = caretStyle.font;
} else {
@@ -504,6 +524,7 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
*/
this.setOverlayElement = function (element) {
overlayElement = element;
caretOverlay.appendChild(element);
shouldUpdateCaretSize = true;
redrawTask.trigger();
};
@@ -525,7 +546,7 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
* @return {undefined}
*/
function destroy(callback) {
cursorNode.removeChild(caretOverlay);
canvas.getSizer().removeChild(caretOverlay);
callback();
}
@@ -539,8 +560,11 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
};
function init() {
var dom = cursor.getDocument().getDOMDocument();
caretOverlay = /**@type{!HTMLElement}*/(dom.createElement("span"));
var dom = cursor.getDocument().getDOMDocument(),
editinfons = "urn:webodf:names:editinfo";
caretOverlay = /**@type{!HTMLElement}*/(dom.createElement("div"));
caretOverlay.setAttributeNS(editinfons, "editinfo:memberid", cursor.getMemberId());
caretOverlay.className = "webodf-caretOverlay";
caretElement = /**@type{!HTMLElement}*/(dom.createElement("div"));
@@ -549,8 +573,7 @@ gui.Caret = function Caret(cursor, avatarInitiallyVisible, blinkOnRangeSelect) {
avatar = new gui.Avatar(caretOverlay, avatarInitiallyVisible);
cursorNode = cursor.getNode();
cursorNode.appendChild(caretOverlay);
canvas.getSizer().appendChild(caretOverlay);
redrawTask = core.Task.createRedrawTask(updateCaret);
blinkTask = core.Task.createTimeoutTask(blinkCaret, BLINK_PERIOD_MS);
+6 -26
View File
@@ -101,28 +101,6 @@ gui.SvgSelectionView = function SvgSelectionView(cursor) {
}
}
/**
* Takes a rect that is relative to the main viewport, and translates
* it to be relative to the 'root' rect of the document.
* Also takes the zoom level into account.
* @param {!ClientRect|!{top: !number, left: !number, bottom: !number, right: !number, width: !number, height: !number}} rect
* @return {!{top: !number, left: !number, bottom: !number, right: !number, width: !number, height: !number}}
*/
function translateRect(rect) {
var rootRect = domUtils.getBoundingClientRect(sizer),
zoomLevel = zoomHelper.getZoomLevel(),
resultRect = {};
resultRect.top = domUtils.adaptRangeDifferenceToZoomLevel(rect.top - rootRect.top, zoomLevel);
resultRect.left = domUtils.adaptRangeDifferenceToZoomLevel(rect.left - rootRect.left, zoomLevel);
resultRect.bottom = domUtils.adaptRangeDifferenceToZoomLevel(rect.bottom - rootRect.top, zoomLevel);
resultRect.right = domUtils.adaptRangeDifferenceToZoomLevel(rect.right - rootRect.left, zoomLevel);
resultRect.width = domUtils.adaptRangeDifferenceToZoomLevel(rect.width, zoomLevel);
resultRect.height = domUtils.adaptRangeDifferenceToZoomLevel(rect.height, zoomLevel);
return resultRect;
}
/**
* Returns true if the supplied range has 1 or more visible client rectangles.
* A range might not be visible if it:
@@ -529,7 +507,9 @@ gui.SvgSelectionView = function SvgSelectionView(cursor) {
* width are non-zero), otherwise returns false
*/
function repositionOverlays(selectedRange) {
var extremes = getExtremeRanges(selectedRange),
var rootRect = /**@type{!ClientRect}*/(domUtils.getBoundingClientRect(sizer)),
zoomLevel = zoomHelper.getZoomLevel(),
extremes = getExtremeRanges(selectedRange),
firstRange,
lastRange,
fillerRange,
@@ -548,14 +528,14 @@ gui.SvgSelectionView = function SvgSelectionView(cursor) {
lastRange = extremes.lastRange;
fillerRange = extremes.fillerRange;
firstRect = translateRect(getCollapsedRectOfTextRange(firstRange, false));
lastRect = translateRect(getCollapsedRectOfTextRange(lastRange, true));
firstRect = domUtils.translateRect(getCollapsedRectOfTextRange(firstRange, false), rootRect, zoomLevel);
lastRect = domUtils.translateRect(getCollapsedRectOfTextRange(lastRange, true), rootRect, zoomLevel);
fillerRect = getFillerRect(fillerRange);
if (!fillerRect) {
fillerRect = getBoundingRect(firstRect, lastRect);
} else {
fillerRect = translateRect(fillerRect);
fillerRect = domUtils.translateRect(fillerRect, rootRect, zoomLevel);
}
// These are the absolute bounding left, right, top, and bottom coordinates of the
+2 -1
View File
@@ -62,8 +62,9 @@
"core.Destroyable"
],
"gui.Caret": [
"core.Async",
"core.Task",
"gui.Avatar",
"odf.OdfCanvas",
"ops.OdtCursor"
],
"gui.CaretManager": [
+3 -16
View File
@@ -233,20 +233,15 @@ cursor|anchor {
}
cursor|cursor {
display: inline;
width: 0;
height: 1em;
/* making the position relative enables the avatar to use
the cursor as reference for its absolute position */
position: relative;
z-index: 1;
pointer-events: none;
display: none;
}
.webodf-caretOverlay {
position: absolute;
top: 5%; /* push down the caret; 0px can do the job, 5% looks better, 10% is a bit over */
height: 1em;
margin-left: -1px;
z-index: 10;
}
.webodf-caretOverlay .caret {
@@ -332,14 +327,6 @@ cursor|cursor[cursor|composing="true"] {
height: inherit;
}
cursor|cursor[cursor|composing="true"] .webodf-caretOverlay {
/* during composition, the caret should be pushed along by the composition text, inline with the text */
position: relative;
/* as it is now part of an inline-block, it will no longer need correct to top or height values to align properly */
height: auto !important;
top: auto !important;
}
editinfo|editinfo {
/* Empty or invisible display:inline elements respond very badly to mouse selection.
Inline blocks are much more reliably selectable in Chrome & friends */