diff --git a/webodf/lib/gui/PlainTextPasteboard.js b/webodf/lib/gui/PlainTextPasteboard.js index adfb4df8..3ebd1de8 100644 --- a/webodf/lib/gui/PlainTextPasteboard.js +++ b/webodf/lib/gui/PlainTextPasteboard.js @@ -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; }; -}; \ No newline at end of file +}; diff --git a/webodf/lib/gui/TextManipulator.js b/webodf/lib/gui/TextManipulator.js index 1dcac67b..45572162 100644 --- a/webodf/lib/gui/TextManipulator.js +++ b/webodf/lib/gui/TextManipulator.js @@ -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) { diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index 06bd2ad7..7a1859a6 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -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} */ diff --git a/webodf/lib/ops/OpInsertText.js b/webodf/lib/ops/OpInsertText.js index 59c2f875..4cd490be 100644 --- a/webodf/lib/ops/OpInsertText.js +++ b/webodf/lib/ops/OpInsertText.js @@ -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; diff --git a/webodf/lib/ops/OperationTransformMatrix.js b/webodf/lib/ops/OperationTransformMatrix.js index cd9e87a8..3a906444 100644 --- a/webodf/lib/ops/OperationTransformMatrix.js +++ b/webodf/lib/ops/OperationTransformMatrix.js @@ -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 {