From 2c682bb73bfcc48b244f808f642cc1f05bebda45 Mon Sep 17 00:00:00 2001 From: Aditya Bhatt Date: Mon, 13 Jul 2015 15:38:59 +0200 Subject: [PATCH] Update other references to be aware of getMemberIds working for all members, and not just cursors Adds a new OdtDocument.hasMember method --- programs/editor/plugins/bella/Bella.js | 6 ++++- .../editor/plugins/bella/DocumentValidator.js | 3 ++- webodf/lib/gui/TrivialUndoManager.js | 4 ++- webodf/lib/ops/Document.js | 6 +++++ webodf/lib/ops/OdtDocument.js | 7 +++++ webodf/tests/gui/TrivialUndoManagerTests.js | 26 ++++++++++++++----- 6 files changed, 43 insertions(+), 9 deletions(-) diff --git a/programs/editor/plugins/bella/Bella.js b/programs/editor/plugins/bella/Bella.js index 80b7e3d2..bd142e05 100644 --- a/programs/editor/plugins/bella/Bella.js +++ b/programs/editor/plugins/bella/Bella.js @@ -84,6 +84,10 @@ define("webodf/editor/plugins/bella/Bella", [ ops = []; members.forEach(function (memberId) { + if (!odtDocument.getCursor(memberId)) { + return; + } + var selection = odtDocument.getCursorSelection(memberId), selectionType = odtDocument.getCursor(memberId).getSelectionType(), op = new webodf.ops.OpMoveCursor(); @@ -250,4 +254,4 @@ define("webodf/editor/plugins/bella/Bella", [ } return Bella; -}); \ No newline at end of file +}); diff --git a/programs/editor/plugins/bella/DocumentValidator.js b/programs/editor/plugins/bella/DocumentValidator.js index 0a0fa4cf..38794a2b 100644 --- a/programs/editor/plugins/bella/DocumentValidator.js +++ b/programs/editor/plugins/bella/DocumentValidator.js @@ -62,6 +62,7 @@ define("webodf/editor/plugins/bella/DocumentValidator", function() { var root = odtDocument.getRootNode(), stepIterator = odtDocument.createStepIterator(root, 0, [odtDocument.getPositionFilter()], root); + // In a Bella environment each memberid has a cursor odtDocument.getMemberIds().forEach(function (memberId) { var cursor = odtDocument.getCursor(memberId); stepIterator.setPosition(cursor.getNode(), 0); @@ -159,4 +160,4 @@ define("webodf/editor/plugins/bella/DocumentValidator", function() { } return DocumentValidator; -}); \ No newline at end of file +}); diff --git a/webodf/lib/gui/TrivialUndoManager.js b/webodf/lib/gui/TrivialUndoManager.js index 721ba098..b3d4de3a 100644 --- a/webodf/lib/gui/TrivialUndoManager.js +++ b/webodf/lib/gui/TrivialUndoManager.js @@ -504,7 +504,9 @@ gui.TrivialUndoManager = function TrivialUndoManager(defaultRules) { if (moved) { // Need to reset the odt document cursor list back to nil so new cursors are correctly re-registered document.getMemberIds().forEach(function (memberid) { - document.removeCursor(memberid); + if (document.hasCursor(memberid)) { + document.removeCursor(memberid); + } }); // Only do actual work if moveBackward does something to the undo stacks document.setDocumentElement(/**@type{!Element}*/(initialDoc.cloneNode(true))); diff --git a/webodf/lib/ops/Document.js b/webodf/lib/ops/Document.js index 45b145ff..fe7b6319 100644 --- a/webodf/lib/ops/Document.js +++ b/webodf/lib/ops/Document.js @@ -91,6 +91,12 @@ ops.Document.prototype.createRootFilter = function (inputMemberId) { "use strict * @return {!core.PositionIterator} */ ops.Document.prototype.createPositionIterator = function (rootNode) { "use strict"; }; +/** + * @param {!string} memberid + * @return {!boolean} + */ +ops.Document.prototype.hasCursor = function (memberid) { "use strict"; }; + /**@const*/ ops.Document.signalCursorAdded = "cursor/added"; /**@const*/ diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index df22721c..cd33a280 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -814,6 +814,13 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { return cursors[memberid]; }; + /** + * @param {!string} memberid + * @return {!boolean} + */ + this.hasCursor = function (memberid) { + return cursors.hasOwnProperty(memberid); + }; /** * @return {!Array.} */ diff --git a/webodf/tests/gui/TrivialUndoManagerTests.js b/webodf/tests/gui/TrivialUndoManagerTests.js index 8da0b1e7..e7b654fe 100644 --- a/webodf/tests/gui/TrivialUndoManagerTests.js +++ b/webodf/tests/gui/TrivialUndoManagerTests.js @@ -32,11 +32,13 @@ gui.TrivialUndoManagerTests = function TrivialUndoManagerTests(runner) { "use strict"; var t, testarea, - r = runner; + r = runner, + member, + cursor; - function cursor(id) { - return { getMemberId: function () { return id; } }; - } + cursor = member = function (id) { + return { getMemberId: function () { return id; } }; + }; /** * @param rootElement @@ -61,11 +63,21 @@ gui.TrivialUndoManagerTests = function TrivialUndoManagerTests(runner) { this.setRootElement = noOp; this.setOdfContainer = noOp; this.cursors = [cursor("1")]; + this.members = [member("1")]; this.getMemberIds = function () { - return self.cursors.map(function (cursor) { - return cursor.getMemberId(); + return self.members.map(function (member) { + return member.getMemberId(); }); }; + this.hasCursor = function (memberid) { + var i; + for (i = 0; i < self.cursors.length; i += 1) { + if (self.cursors[i].getMemberId() === memberid) { + return true; + } + } + return false; + }; this.getDocumentElement = function () { return rootElement; }; this.getRootNode = function () { return rootElement; }; this.getDOMDocument = function () { return rootElement.ownerDocument; }; @@ -195,6 +207,7 @@ gui.TrivialUndoManagerTests = function TrivialUndoManagerTests(runner) { } function setInitialState_AllCursorsMaintainedWhenCalledMultipleTimes() { + t.mock.members.push(member(2)); t.mock.cursors.push(cursor(2)); t.manager.onOperationExecuted(create(new ops.OpMoveCursor(), {timestamp: 1, memberid: 1})); t.manager.onOperationExecuted(create(new ops.OpMoveCursor(), {timestamp: 2, memberid: 2})); @@ -362,6 +375,7 @@ gui.TrivialUndoManagerTests = function TrivialUndoManagerTests(runner) { t.manager.onOperationExecuted(create(new ops.OpMoveCursor(), {timestamp: 5, memberid: 1})); t.manager.onOperationExecuted(create(new ops.OpMoveCursor(), {timestamp: 6, memberid: 2})); + t.mock.members = [member(2)]; t.mock.cursors = [cursor(2)]; t.manager.initialize();