diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index c36aa553..381a84e9 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -49,10 +49,10 @@ define([ "RemoveCurrentSelection", "PreloadDocument", "BoldCurrentSelection", - "MoveCursorToEndViaCtrlEnd" + "MoveCursorToEnd" ], function (Benchmark, HTMLResultsRenderer, OpenDocument, EnterEditMode, MoveCursorToEndDirect,InsertLetterA, Remove1Position, MoveCursor1StepLeft, - SelectEntireDocument, RemoveCurrentSelection, PreloadDocument, BoldCurrentSelection, MoveCursorToEndViaCtrlEnd) { + SelectEntireDocument, RemoveCurrentSelection, PreloadDocument, BoldCurrentSelection, MoveCursorToEnd) { "use strict"; /** @@ -105,9 +105,7 @@ define([ benchmark.actions.push(new PreloadDocument(config.fileUrl)); benchmark.actions.push(new OpenDocument(config.fileUrl)); benchmark.actions.push(new EnterEditMode()); - if (config.includeSlow) { - benchmark.actions.push(new MoveCursorToEndViaCtrlEnd()); - } + benchmark.actions.push(new MoveCursorToEnd()); benchmark.actions.push(new MoveCursorToEndDirect()); benchmark.actions.push(new InsertLetterA()); benchmark.actions.push(new Remove1Position(true)); diff --git a/programs/benchmark/js/MoveCursorToEndViaCtrlEnd.js b/programs/benchmark/js/MoveCursorToEnd.js similarity index 93% rename from programs/benchmark/js/MoveCursorToEndViaCtrlEnd.js rename to programs/benchmark/js/MoveCursorToEnd.js index e20b8d6b..dd920ad5 100644 --- a/programs/benchmark/js/MoveCursorToEndViaCtrlEnd.js +++ b/programs/benchmark/js/MoveCursorToEnd.js @@ -43,7 +43,7 @@ define(["BenchmarkAction"], function(BenchmarkAction) { * Move the cursor to the end of the document using the same interface as ctrl/cmd+end * @constructor */ - function MoveCursorToEndViaCtrlEnd() { + function MoveCursorToEnd() { var state = {description: "Move to document end (ctrl+end)"}, action = new BenchmarkAction(state); @@ -55,10 +55,10 @@ define(["BenchmarkAction"], function(BenchmarkAction) { */ this.start = function(sharedState) { action.start(); - sharedState.sessionController.moveCursorToDocumentBoundary(1, false); + sharedState.sessionController.moveCursorToDocumentEnd(); action.complete(true); } } - return MoveCursorToEndViaCtrlEnd; + return MoveCursorToEnd; }); diff --git a/webodf/lib/gui/SessionController.js b/webodf/lib/gui/SessionController.js index eeae711f..f36462ec 100644 --- a/webodf/lib/gui/SessionController.js +++ b/webodf/lib/gui/SessionController.js @@ -45,6 +45,7 @@ runtime.loadClass("odf.ObjectNameGenerator"); runtime.loadClass("ops.OdtCursor"); runtime.loadClass("ops.OpAddCursor"); runtime.loadClass("ops.OpRemoveCursor"); +runtime.loadClass("ops.StepsTranslator"); runtime.loadClass("gui.Clipboard"); runtime.loadClass("gui.DirectTextStyler"); runtime.loadClass("gui.DirectParagraphStyler"); @@ -539,28 +540,35 @@ gui.SessionController = (function () { return true; } + /** + * @param {!number} direction -1 for beginning, 1 for end + * @param {!function(!Node):Node} getContainmentNode Returns a node container for the supplied node. + * Usually this will be something like the parent paragraph or root the supplied node is within + * @return {undefined} + */ + function extendCursorToNodeBoundary(direction, getContainmentNode) { + var cursor = odtDocument.getCursor(inputMemberId), + node = getContainmentNode(cursor.getNode()), + selection = rangeToSelection(cursor.getSelectedRange(), cursor.hasForwardSelection()), + newCursorSelection; + + runtime.assert(Boolean(node), "SessionController: Cursor outside root"); + if (direction < 0) { + selection.focusNode = /**@type{!Node}*/(node); + selection.focusOffset = 0; + } else { + selection.focusNode = /**@type{!Node}*/(node); + selection.focusOffset = node.childNodes.length; + } + newCursorSelection = odtDocument.convertDomToCursorRange(selection, constrain(getContainmentNode)); + session.enqueue([createOpMoveCursor(newCursorSelection.position, newCursorSelection.length)]); + } + /** * @return {!boolean} */ function extendSelectionToParagraphStart() { - var paragraphNode = /**@type{!Node}*/(odtDocument.getParagraphElement(odtDocument.getCursor(inputMemberId).getNode())), - iterator, - node, - steps; - - runtime.assert(Boolean(paragraphNode), "SessionController: Cursor outside paragraph"); - steps = odtDocument.getDistanceFromCursor(inputMemberId, paragraphNode, 0); - iterator = gui.SelectionMover.createPositionIterator(odtDocument.getRootNode()); - iterator.setUnfilteredPosition(paragraphNode, 0); - - while (steps === 0 && iterator.previousPosition()) { - node = iterator.getCurrentNode(); - - if (odfUtils.isParagraph(node)) { - steps = odtDocument.getDistanceFromCursor(inputMemberId, node, 0); - } - } - extendCursorByAdjustment(steps); + extendCursorToNodeBoundary(-1, odtDocument.getParagraphElement); return true; } @@ -568,93 +576,75 @@ gui.SessionController = (function () { * @return {!boolean} */ function extendSelectionToParagraphEnd() { - var paragraphNode = /**@type{!Node}*/(odtDocument.getParagraphElement(odtDocument.getCursor(inputMemberId).getNode())), - iterator, - node, - steps; - - runtime.assert(Boolean(paragraphNode), "SessionController: Cursor outside paragraph"); - iterator = gui.SelectionMover.createPositionIterator(odtDocument.getRootNode()); - iterator.moveToEndOfNode(paragraphNode); - steps = odtDocument.getDistanceFromCursor( - inputMemberId, - iterator.container(), - iterator.unfilteredDomOffset() - ); - - while (steps === 0 && iterator.nextPosition()) { - node = iterator.getCurrentNode(); - - if (odfUtils.isParagraph(node)) { - iterator.moveToEndOfNode(node); - steps = odtDocument.getDistanceFromCursor( - inputMemberId, - iterator.container(), - iterator.unfilteredDomOffset() - ); - } - } - extendCursorByAdjustment(steps); + extendCursorToNodeBoundary(1, odtDocument.getParagraphElement); return true; } /** * @param {!number} direction -1 for beginning, 1 for end - * @param {!boolean=} extend - * @return {undefined} + * @return {!boolean} */ - function moveCursorToDocumentBoundary(direction, extend) { - var iterator = gui.SelectionMover.createPositionIterator(odtDocument.getRootNode()), - steps; - if (direction > 0) { - iterator.moveToEnd(); - } + function moveCursorToRootBoundary(direction) { + var cursor = odtDocument.getCursor(inputMemberId), + root = odtDocument.getRootElement(cursor.getNode()), + newPosition; - steps = odtDocument.getDistanceFromCursor( - inputMemberId, - iterator.container(), - iterator.unfilteredDomOffset() - ); - if (extend) { - extendCursorByAdjustment(steps); + runtime.assert(Boolean(root), "SessionController: Cursor outside root"); + if (direction < 0) { + // The anchor node will already be in a walkable position having just been retrieved from the cursor + // The rounding will only impact the new focus node + // Need to round up as (p, 0) is potentially before the first walkable position in the paragraph + newPosition = odtDocument.convertDomPointToCursorStep(root, 0, function (step) { + return step === ops.StepsTranslator.NEXT_STEP; + }); } else { - moveCursorByAdjustment(steps); + // Default behaviour is to round down to the previous walkable step if (p, p.childNodes.length) isn't + // walkable. Either way, this still equates to moving to the last walkable step in the paragraph + newPosition = odtDocument.convertDomPointToCursorStep(root, root.childNodes.length); } + session.enqueue([createOpMoveCursor(newPosition, 0)]); + return true; } - // TODO Extract selection functions into a standalone SelectionManipulator - this.moveCursorToDocumentBoundary = moveCursorToDocumentBoundary; /** * @return {!boolean} */ function moveCursorToDocumentStart() { - moveCursorToDocumentBoundary(-1, false); + moveCursorToRootBoundary(-1); return true; } + // TODO Extract selection functions into a standalone SelectionManipulator + this.moveCursorToDocumentStart = moveCursorToDocumentStart; /** * @return {!boolean} */ function moveCursorToDocumentEnd() { - moveCursorToDocumentBoundary(1, false); + moveCursorToRootBoundary(1); return true; } + // TODO Extract selection functions into a standalone SelectionManipulator + this.moveCursorToDocumentEnd = moveCursorToDocumentEnd; /** * @return {!boolean} */ function extendSelectionToDocumentStart() { - moveCursorToDocumentBoundary(-1, true); + extendCursorToNodeBoundary(-1, odtDocument.getRootElement); return true; } + // TODO Extract selection functions into a standalone SelectionManipulator + this.extendSelectionToDocumentStart = extendSelectionToDocumentStart; /** * @return {!boolean} */ function extendSelectionToDocumentEnd() { - moveCursorToDocumentBoundary(1, true); + extendCursorToNodeBoundary(1, odtDocument.getRootElement); return true; } + // TODO Extract selection functions into a standalone SelectionManipulator + this.extendSelectionToDocumentEnd = extendSelectionToDocumentEnd; /** * @return {!boolean} diff --git a/webodf/lib/manifest.json b/webodf/lib/manifest.json index f4dfb56d..c1ed9519 100644 --- a/webodf/lib/manifest.json +++ b/webodf/lib/manifest.json @@ -180,7 +180,8 @@ "ops/OpMoveCursor.js", "ops/OpRemoveCursor.js", "ops/Operation.js", - "ops/Session.js" + "ops/Session.js", + "ops/StepsTranslator.js" ], "gui/SessionView.js": [ "gui/Caret.js", diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index 87ceca49..114633e9 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -199,7 +199,7 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { this.convertDomToCursorRange = function (selection, constraint) { var point1, point2, - anchorConstraint = constraint(selection.anchorNode, selection.anchorOffset), + anchorConstraint = constraint && constraint(selection.anchorNode, selection.anchorOffset), focusConstraint; point1 = stepsTranslator.convertDomPointToSteps(selection.anchorNode, selection.anchorOffset, anchorConstraint); @@ -208,7 +208,7 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { // In this case, it's safest to just look up the next point again. point2 = point1; } else { - focusConstraint = constraint(selection.focusNode, selection.focusOffset); + focusConstraint = constraint && constraint(selection.focusNode, selection.focusOffset); point2 = stepsTranslator.convertDomPointToSteps(selection.focusNode, selection.focusOffset, focusConstraint); }