mirror of
https://github.com/webodf/WebODF.git
synced 2026-12-01 16:56:05 +01:00
Re-implement the selection commands using new StepsTranslator interfaces
This re-implementation also addresses problems when navigating passed annotations as identified in issue #65 & #65. This also fixes a large performance issue with extending or moving the cursor via short-cut keys.
This commit is contained in:
@@ -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));
|
||||
|
||||
+3
-3
@@ -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;
|
||||
});
|
||||
@@ -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}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user