* Add an additional flag moveCursor to OpInsertText,

and obey it to explicitly move the cursor to the
desired resultant position.
When doing this, take care to specify the correct
textnode position for text insertion (i.e. before
or after the cursor).
* Adapt the rest of the code that uses OpInsertText
to pass the correct moveCursor flag (true).
* Remove the 'return null' from the transform of
InsertText vs InsertText.
This commit is contained in:
Aditya Bhatt
2013-12-05 17:26:19 +01:00
parent 81c8820e35
commit 47db07fb99
5 changed files with 57 additions and 9 deletions
+3 -2
View File
@@ -72,7 +72,8 @@ gui.PlainTextPasteboard = function PlainTextPasteboard(odtDocument, inputMemberI
operations.push(createOp(new ops.OpInsertText(), {
memberid: inputMemberId,
position: cursorPosition,
text: text
text: text,
moveCursor: true
}));
cursorPosition += text.length;
});
@@ -86,4 +87,4 @@ gui.PlainTextPasteboard = function PlainTextPasteboard(odtDocument, inputMemberI
return operations;
};
};
};
+2 -1
View File
@@ -241,7 +241,8 @@ gui.TextManipulator = function TextManipulator(session, inputMemberId, directSty
op.init({
memberid: inputMemberId,
position: selection.position,
text: text
text: text,
moveCursor: true
});
operations.push(op);
if (directStyleOp) {
+21
View File
@@ -722,6 +722,27 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
return false;
};
/**
* Moves the cursor/selection of a given memberid to the
* given position+length combination and adopts the given
* selectionType.
* It is the caller's responsibility to decide if and when
* to subsequently fire signalCursorMoved.
* @param {!string} memberid
* @param {!number} position
* @param {!number} length
* @param {!string} selectionType
* @return {undefined}
*/
this.moveCursor = function (memberid, position, length, selectionType) {
var cursor = cursors[memberid],
selectionRange = self.convertCursorToDomRange(position, length);
if (cursor && selectionRange) {
cursor.setSelectedRange(selectionRange, length >= 0);
cursor.setSelectionType(selectionType || ops.OdtCursor.RangeSelection);
}
};
/**
* @return {!odf.Formatting}
*/
+31 -4
View File
@@ -39,6 +39,13 @@
/*global ops*/
/**
* This operation inserts the given text
* at the specified position, and if
* the moveCursor flag is specified and
* is set as true, moves the cursor to
* the end of the inserted text.
* Otherwise, the cursor remains at the
* same position as before.
* @constructor
* @implements ops.Operation
*/
@@ -47,13 +54,14 @@ ops.OpInsertText = function OpInsertText() {
var space = " ",
tab = "\t",
memberid, timestamp, position, text;
memberid, timestamp, position, text, moveCursor;
this.init = function (data) {
memberid = data.memberid;
timestamp = data.timestamp;
position = data.position;
text = data.text;
moveCursor = data.moveCursor === 'true' || data.moveCursor === true;
};
this.isEdit = true;
@@ -96,6 +104,8 @@ ops.OpInsertText = function OpInsertText() {
toInsertIndex = 0,
spaceTag,
spaceElement,
cursor = odtDocument.getCursor(memberid),
originalCursorPosition = odtDocument.getCursorPosition(memberid),
i;
function insertTextNode(toInsertText) {
@@ -158,6 +168,22 @@ ops.OpInsertText = function OpInsertText() {
}
odtDocument.emit(ops.OdtDocument.signalStepsInserted, {position: position, length: text.length});
if (cursor) {
// Explicitly place the cursor in the desired position after insertion
if (moveCursor) {
// TODO: At the moment the inserted text already appears before the
// cursor, so the cursor is effectively at position + text.length
// already. So this ought to be optimized, by perhaps removing
// the textnode + cursor reordering logic from OdtDocument's
// getTextNodeAtStep.
odtDocument.moveCursor(memberid, position + text.length, 0);
odtDocument.emit(ops.OdtDocument.signalCursorMoved, cursor);
} else {
odtDocument.moveCursor(memberid, originalCursorPosition, 0);
}
}
if (position > 0) {
// Necessary to match upgradeWhitespaces behaviour which searches the preceding positions as well
if (position > 1) {
@@ -172,7 +198,6 @@ ops.OpInsertText = function OpInsertText() {
// Try and downgrade the next position just *after* the added text
odtDocument.downgradeWhitespacesAtPosition(position + text.length);
odtDocument.getOdfCanvas().refreshSize();
odtDocument.emit(ops.OdtDocument.signalParagraphChanged, {
paragraphElement: paragraphElement,
@@ -192,7 +217,8 @@ ops.OpInsertText = function OpInsertText() {
memberid: memberid,
timestamp: timestamp,
position: position,
text: text
text: text,
moveCursor: moveCursor
};
};
};
@@ -201,6 +227,7 @@ ops.OpInsertText = function OpInsertText() {
memberid:string,
timestamp:number,
position:number,
text:string
text:string,
moveCursor:boolean
}}*/
ops.OpInsertText.Spec;
@@ -500,8 +500,6 @@ ops.OperationTransformMatrix = function OperationTransformMatrix() {
} else {
insertTextSpecA.position += insertTextSpecB.text.length;
}
// TODO: cursors get out of sync, so for now have OT fail
return null;
}
return {