From f36aa2f79bd176ece8be82bc061c0bebee4f8f5c Mon Sep 17 00:00:00 2001 From: Aditya Bhatt Date: Wed, 4 Jun 2014 17:43:40 +0200 Subject: [PATCH] Make text editing respect review mode constraints. --- webodf/lib/gui/SessionController.js | 3 +- webodf/lib/gui/TextController.js | 77 ++++++++++++++++++++++++- webodf/lib/manifest.json | 3 +- webodf/tests/gui/TextControllerTests.js | 4 +- webodf/tests/manifest.json | 2 + 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/webodf/lib/gui/SessionController.js b/webodf/lib/gui/SessionController.js index 373e3b5b..80a1c028 100644 --- a/webodf/lib/gui/SessionController.js +++ b/webodf/lib/gui/SessionController.js @@ -89,7 +89,7 @@ gui.SessionControllerOptions = function () { args.directTextStylingEnabled, args.directParagraphStylingEnabled), createCursorStyleOp = /**@type {function (!number, !number, !boolean):ops.Operation}*/ (directFormattingController.createCursorStyleOp), createParagraphStyleOps = /**@type {function (!number):!Array.}*/ (directFormattingController.createParagraphStyleOps), - textController = new gui.TextController(session, inputMemberId, createCursorStyleOp, createParagraphStyleOps), + textController = new gui.TextController(session, sessionConstraints, sessionContextCache, inputMemberId, createCursorStyleOp, createParagraphStyleOps), imageController = new gui.ImageController(session, sessionConstraints, sessionContextCache, inputMemberId, objectNameGenerator), imageSelector = new gui.ImageSelector(odtDocument.getOdfCanvas()), shadowCursorIterator = gui.SelectionMover.createPositionIterator(odtDocument.getRootNode()), @@ -1112,6 +1112,7 @@ gui.SessionControllerOptions = function () { hyperlinkClickHandler.destroy, hyperlinkController.destroy, metadataController.destroy, + textController.destroy, destroy ]; diff --git a/webodf/lib/gui/TextController.js b/webodf/lib/gui/TextController.js index 1413d07d..fc580cb5 100644 --- a/webodf/lib/gui/TextController.js +++ b/webodf/lib/gui/TextController.js @@ -26,12 +26,22 @@ /** * @constructor + * @implements {core.Destroyable} * @param {!ops.Session} session + * @param {!gui.SessionConstraints} sessionConstraints + * @param {!gui.SessionContextCache} sessionContextCache * @param {!string} inputMemberId * @param {function(!number, !number, !boolean):ops.Operation} directStyleOp * @param {function(!number):!Array.} paragraphStyleOps */ -gui.TextController = function TextController(session, inputMemberId, directStyleOp, paragraphStyleOps) { +gui.TextController = function TextController( + session, + sessionConstraints, + sessionContextCache, + inputMemberId, + directStyleOp, + paragraphStyleOps + ) { "use strict"; var odtDocument = session.getOdtDocument(), @@ -45,7 +55,37 @@ gui.TextController = function TextController(session, inputMemberId, directStyle * @const * @type {!boolean} */ - FORWARD = true; + FORWARD = true, + isEnabled = false; + + /** + * @return {undefined} + */ + function updateEnabledState() { + if (sessionConstraints.getState("edit.reviewMode") === true) { + isEnabled = /**@type{!boolean}*/(sessionContextCache.isLocalCursorWithinOwnAnnotation()); + } else { + isEnabled = true; + } + } + + /** + * @param {!ops.OdtCursor} cursor + * @return {undefined} + */ + function onCursorEvent(cursor) { + if (cursor.getMemberId() === inputMemberId) { + updateEnabledState(); + } + } + + /** + * @return {!boolean} + */ + this.isEnabled = function () { + return isEnabled; + }; + /** * Creates operations to remove the provided selection and update the destination @@ -115,6 +155,10 @@ gui.TextController = function TextController(session, inputMemberId, directStyle * @return {!boolean} */ this.enqueueParagraphSplittingOps = function() { + if (!isEnabled) { + return false; + } + var range = odtDocument.getCursor(inputMemberId).getSelectedRange(), selection = toForwardSelection(odtDocument.getCursorSelection(inputMemberId)), op, operations = [], styleOps; @@ -180,6 +224,10 @@ gui.TextController = function TextController(session, inputMemberId, directStyle * @return {!boolean} */ function removeTextInDirection(isForward) { + if (!isEnabled) { + return false; + } + var cursorNode, // Take a clone of the range as it will be modified if the selection length is 0 range = /**@type{!Range}*/(odtDocument.getCursor(inputMemberId).getSelectedRange().cloneRange()), @@ -239,6 +287,10 @@ gui.TextController = function TextController(session, inputMemberId, directStyle * @return {!boolean} */ this.removeCurrentSelection = function () { + if (!isEnabled) { + return false; + } + var range = odtDocument.getCursor(inputMemberId).getSelectedRange(), selection = toForwardSelection(odtDocument.getCursorSelection(inputMemberId)); if (selection.length !== 0) { @@ -253,6 +305,10 @@ gui.TextController = function TextController(session, inputMemberId, directStyle * @return {undefined} */ function insertText(text) { + if (!isEnabled) { + return; + } + var range = odtDocument.getCursor(inputMemberId).getSelectedRange(), selection = toForwardSelection(odtDocument.getCursorSelection(inputMemberId)), op, stylingOp, operations = [], useCachedStyle = false; @@ -279,4 +335,21 @@ gui.TextController = function TextController(session, inputMemberId, directStyle session.enqueue(operations); } this.insertText = insertText; + + /** + * @param {!function(!Error=)} callback, passing an error object in case of error + * @return {undefined} + */ + this.destroy = function (callback) { + odtDocument.unsubscribe(ops.Document.signalCursorMoved, onCursorEvent); + sessionConstraints.unsubscribe("edit.reviewMode", updateEnabledState); + callback(); + }; + + function init() { + odtDocument.subscribe(ops.Document.signalCursorMoved, onCursorEvent); + sessionConstraints.subscribe("edit.reviewMode", updateEnabledState); + updateEnabledState(); + } + init(); }; diff --git a/webodf/lib/manifest.json b/webodf/lib/manifest.json index 488a5e8b..0ce9e94a 100644 --- a/webodf/lib/manifest.json +++ b/webodf/lib/manifest.json @@ -189,7 +189,8 @@ "odf.OdfNodeFilter" ], "gui.TextController": [ - "ops.Session" + "gui.SessionConstraints", + "gui.SessionContextCache" ], "gui.TrivialUndoManager": [ "core.DomUtils", diff --git a/webodf/tests/gui/TextControllerTests.js b/webodf/tests/gui/TextControllerTests.js index f0cd6c66..558aba3d 100644 --- a/webodf/tests/gui/TextControllerTests.js +++ b/webodf/tests/gui/TextControllerTests.js @@ -120,7 +120,9 @@ gui.TextControllerTests = function TextControllerTests(runner) { t.odtDocument = new ops.OdtDocument(new MockOdfCanvas(node)); t.session = new MockSession(t.odtDocument); - t.textController = new gui.TextController(t.session, inputMemberId, directStyleOp, paragraphStyleOps); + t.sessionConstraints = new gui.SessionConstraints(); + t.sessionContextCache = new gui.SessionContextCache(t.session, inputMemberId); + t.textController = new gui.TextController(t.session, t.sessionConstraints, t.sessionContextCache, inputMemberId, directStyleOp, paragraphStyleOps); t.cursor = new ops.OdtCursor(inputMemberId, t.odtDocument); t.odtDocument.addCursor(t.cursor); diff --git a/webodf/tests/manifest.json b/webodf/tests/manifest.json index 72bb41c5..69bfe287 100644 --- a/webodf/tests/manifest.json +++ b/webodf/tests/manifest.json @@ -62,6 +62,8 @@ "gui.TextControllerTests": [ "core.UnitTester", "core.Utils", + "gui.SessionConstraints", + "gui.SessionContextCache", "gui.TextController", "odf.Namespaces", "odf.OdfCanvas",