From 7de8ff00e77ef76ee7e5689bde0c5efa2ae626bc Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 13 Jun 2014 17:12:45 +1000 Subject: [PATCH] Group up style information changes and report at the end of an op batch Operation batches that modify a large number of paragraphs (or repeatedly modify the same paragraph) can cause numerous unnecessary style change events to be triggered. Rather than reporting these instantly, track them as they occur and emit the style change summary on batch end. Related to #610. --- webodf/lib/core/LazyProperty.js | 61 +++++++++++++ webodf/lib/gui/DirectFormattingController.js | 96 +++++++++++--------- webodf/lib/manifest.json | 3 + webodf/tools/karma.conf.js | 1 + 4 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 webodf/lib/core/LazyProperty.js diff --git a/webodf/lib/core/LazyProperty.js b/webodf/lib/core/LazyProperty.js new file mode 100644 index 00000000..33ff803e --- /dev/null +++ b/webodf/lib/core/LazyProperty.js @@ -0,0 +1,61 @@ +/** + * Copyright (C) 2010-2014 KO GmbH + * + * @licstart + * This file is part of WebODF. + * + * WebODF is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License (GNU AGPL) + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * WebODF is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with WebODF. If not, see . + * @licend + * + * @source: http://www.webodf.org/ + * @source: https://github.com/kogmbh/WebODF/ + */ + +/*global core*/ + +// TODO Apparently newer closure compilers do a better job with generics + +/** + * Lazily loaded property. The value is loaded using the valueLoader and cached + * the first time it's requested. Subsequent requests will return the cached value. + * Calling reset will clear the cached value, causing the next value request + * to load a new value via the valueLoader. + * + * @constructor + * @template T + * @param {!function():Object} valueLoader Property value loader + */ +core.LazyProperty = function (valueLoader) { + "use strict"; + var cachedValue, + valueLoaded = false; + + /** + * @return {T} + */ + this.value = function() { + if (!valueLoaded) { + cachedValue = valueLoader(); + valueLoaded = true; + } + return cachedValue; + }; + + /** + * @return {undefined} + */ + this.reset = function() { + valueLoaded = false; + }; +}; diff --git a/webodf/lib/gui/DirectFormattingController.js b/webodf/lib/gui/DirectFormattingController.js index f1131706..4d28e5d2 100644 --- a/webodf/lib/gui/DirectFormattingController.js +++ b/webodf/lib/gui/DirectFormattingController.js @@ -62,12 +62,20 @@ gui.DirectFormattingController = function DirectFormattingController( /**@type{Object}*/ directCursorStyleProperties, // cached text settings - /**@type{!Array.>}*/ - selectionAppliedStyles = [], /**@type{!gui.StyleSummary}*/ - selectionStylesSummary = new gui.StyleSummary(selectionAppliedStyles), + lastSignalledStyleSummary, + /**@type {!core.LazyProperty., styleSummary: !gui.StyleSummary}>} */ + selectionInfoCache, isEnabled = false; + /** + * Gets the current selection information style summary + * @return {!gui.StyleSummary} + */ + function getCachedStyleSummary() { + return selectionInfoCache.value().styleSummary; + } + /** * Fetch all the character elements and text nodes in the specified range, or if the range is collapsed, the node just to * the left of the cursor. @@ -99,9 +107,9 @@ gui.DirectFormattingController = function DirectFormattingController( /** * Get all styles currently applied to the selected range. If the range is collapsed, * this will return the style the next inserted character will have - * @return {!Array.} + * @return {!{appliedStyles: !Array., styleSummary: !gui.StyleSummary}} */ - function getSelectionAppliedStyles() { + function getSelectionInfo() { var cursor = odtDocument.getCursor(inputMemberId), range = cursor && cursor.getSelectedRange(), nodes = range ? getNodes(range) : [], @@ -112,7 +120,11 @@ gui.DirectFormattingController = function DirectFormattingController( selectionStyles[0] = utils.mergeObjects(selectionStyles[0], /**@type {!Object}*/(directCursorStyleProperties)); } - return selectionStyles; + + return { + appliedStyles: selectionStyles, + styleSummary: new gui.StyleSummary(selectionStyles) + }; } /** @@ -138,18 +150,15 @@ gui.DirectFormattingController = function DirectFormattingController( /** * @return {undefined} */ - function updateSelectionStylesInfo() { + function emitStylingChanges() { var textStyleDiff, paragraphStyleDiff, - newSelectionStylesSummary; + newSelectionStylesSummary = getCachedStyleSummary(); - selectionAppliedStyles = getSelectionAppliedStyles(); - newSelectionStylesSummary = new gui.StyleSummary(selectionAppliedStyles); + textStyleDiff = createDiff(lastSignalledStyleSummary.text, newSelectionStylesSummary.text); + paragraphStyleDiff = createDiff(lastSignalledStyleSummary.paragraph, newSelectionStylesSummary.paragraph); - textStyleDiff = createDiff(selectionStylesSummary.text, newSelectionStylesSummary.text); - paragraphStyleDiff = createDiff(selectionStylesSummary.paragraph, newSelectionStylesSummary.paragraph); - - selectionStylesSummary = newSelectionStylesSummary; + lastSignalledStyleSummary = newSelectionStylesSummary; if (Object.keys(textStyleDiff).length > 0) { eventNotifier.emit(gui.DirectFormattingController.textStylingChanged, textStyleDiff); @@ -191,10 +200,7 @@ gui.DirectFormattingController = function DirectFormattingController( var cursorMemberId = (typeof cursorOrId === "string") ? cursorOrId : cursorOrId.getMemberId(); if (cursorMemberId === inputMemberId) { - // FIXME: Update internal state before firing any signals, - // otherwise it may pose a problem when a subscriber tries - // to query the intermediate state. - updateSelectionStylesInfo(); + selectionInfoCache.reset(); updateEnabledState(); } } @@ -204,7 +210,7 @@ gui.DirectFormattingController = function DirectFormattingController( */ function onParagraphStyleModified() { // TODO: check if the cursor (selection) is actually affected - updateSelectionStylesInfo(); + selectionInfoCache.reset(); } /** @@ -216,7 +222,7 @@ gui.DirectFormattingController = function DirectFormattingController( p = args.paragraphElement; if (cursor && odtDocument.getParagraphElement(cursor.getNode()) === p) { - updateSelectionStylesInfo(); + selectionInfoCache.reset(); } } @@ -258,7 +264,7 @@ gui.DirectFormattingController = function DirectFormattingController( // Direct styling is additive. E.g., if the user selects bold and then italic, the intent is to produce // bold & italic text directCursorStyleProperties = utils.mergeObjects(directCursorStyleProperties || {}, properties); - updateSelectionStylesInfo(); + selectionInfoCache.reset(); } } this.formatTextSelection = formatTextSelection; @@ -284,8 +290,8 @@ gui.DirectFormattingController = function DirectFormattingController( */ this.createCursorStyleOp = function (position, length, useCachedStyle) { var styleOp = null, - /**@type{Object.}*/ - properties = useCachedStyle ? selectionAppliedStyles[0] : directCursorStyleProperties; + /**@type{Object.}*/ + properties = useCachedStyle ? selectionInfoCache.value().appliedStyles[0] : directCursorStyleProperties; if (properties && properties['style:text-properties']) { styleOp = new ops.OpApplyDirectStyling(); @@ -296,7 +302,7 @@ gui.DirectFormattingController = function DirectFormattingController( setProperties: {'style:text-properties': properties['style:text-properties']} }); directCursorStyleProperties = null; - updateSelectionStylesInfo(); + selectionInfoCache.reset(); } return styleOp; }; @@ -314,7 +320,7 @@ gui.DirectFormattingController = function DirectFormattingController( // added to apply the style to. Even after a split, the cursor should still style the next inserted // character directCursorStyleProperties = null; - updateSelectionStylesInfo(); + selectionInfoCache.reset(); } } } @@ -385,97 +391,97 @@ gui.DirectFormattingController = function DirectFormattingController( * @return {!Array.} */ this.getAppliedStyles = function () { - return selectionAppliedStyles; + return selectionInfoCache.value().appliedStyles; }; /** * @return {!boolean} */ - this.toggleBold = toggle.bind(self, function () { return selectionStylesSummary.isBold(); }, setBold); + this.toggleBold = toggle.bind(self, function () { return getCachedStyleSummary().isBold(); }, setBold); /** * @return {!boolean} */ - this.toggleItalic = toggle.bind(self, function () { return selectionStylesSummary.isItalic(); }, setItalic); + this.toggleItalic = toggle.bind(self, function () { return getCachedStyleSummary().isItalic(); }, setItalic); /** * @return {!boolean} */ - this.toggleUnderline = toggle.bind(self, function () { return selectionStylesSummary.hasUnderline(); }, setHasUnderline); + this.toggleUnderline = toggle.bind(self, function () { return getCachedStyleSummary().hasUnderline(); }, setHasUnderline); /** * @return {!boolean} */ - this.toggleStrikethrough = toggle.bind(self, function () { return selectionStylesSummary.hasStrikeThrough(); }, setHasStrikethrough); + this.toggleStrikethrough = toggle.bind(self, function () { return getCachedStyleSummary().hasStrikeThrough(); }, setHasStrikethrough); /** * @return {!boolean} */ this.isBold = function () { - return selectionStylesSummary.isBold(); + return getCachedStyleSummary().isBold(); }; /** * @return {!boolean} */ this.isItalic = function () { - return selectionStylesSummary.isItalic(); + return getCachedStyleSummary().isItalic(); }; /** * @return {!boolean} */ this.hasUnderline = function () { - return selectionStylesSummary.hasUnderline(); + return getCachedStyleSummary().hasUnderline(); }; /** * @return {!boolean} */ this.hasStrikeThrough = function () { - return selectionStylesSummary.hasStrikeThrough(); + return getCachedStyleSummary().hasStrikeThrough(); }; /** * @return {number|undefined} */ this.fontSize = function () { - return selectionStylesSummary.fontSize(); + return getCachedStyleSummary().fontSize(); }; /** * @return {string|undefined} */ this.fontName = function () { - return selectionStylesSummary.fontName(); + return getCachedStyleSummary().fontName(); }; /** * @return {!boolean} */ this.isAlignedLeft = function () { - return selectionStylesSummary.isAlignedLeft(); + return getCachedStyleSummary().isAlignedLeft(); }; /** * @return {!boolean} */ this.isAlignedCenter = function () { - return selectionStylesSummary.isAlignedCenter(); + return getCachedStyleSummary().isAlignedCenter(); }; /** * @return {!boolean} */ this.isAlignedRight = function () { - return selectionStylesSummary.isAlignedRight(); + return getCachedStyleSummary().isAlignedRight(); }; /** * @return {!boolean} */ this.isAlignedJustified = function () { - return selectionStylesSummary.isAlignedJustified(); + return getCachedStyleSummary().isAlignedJustified(); }; /** @@ -741,7 +747,7 @@ gui.DirectFormattingController = function DirectFormattingController( return operations; } - properties = selectionAppliedStyles[0]; + properties = selectionInfoCache.value().appliedStyles[0]; if (!properties) { return operations; } @@ -795,7 +801,7 @@ gui.DirectFormattingController = function DirectFormattingController( }; /** - * @param {!function(!Error=)} callback, passing an error object in case of error + * @param {!function(!Error=)} callback passing an error object in case of error * @return {undefined} */ this.destroy = function (callback) { @@ -805,6 +811,7 @@ gui.DirectFormattingController = function DirectFormattingController( odtDocument.unsubscribe(ops.OdtDocument.signalParagraphStyleModified, onParagraphStyleModified); odtDocument.unsubscribe(ops.OdtDocument.signalParagraphChanged, onParagraphChanged); odtDocument.unsubscribe(ops.OdtDocument.signalOperationEnd, clearCursorStyle); + odtDocument.unsubscribe(ops.OdtDocument.signalProcessingBatchEnd, emitStylingChanges); sessionConstraints.unsubscribe(gui.CommonConstraints.EDIT.REVIEW_MODE, updateEnabledState); callback(); }; @@ -830,8 +837,11 @@ gui.DirectFormattingController = function DirectFormattingController( odtDocument.subscribe(ops.OdtDocument.signalParagraphStyleModified, onParagraphStyleModified); odtDocument.subscribe(ops.OdtDocument.signalParagraphChanged, onParagraphChanged); odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, clearCursorStyle); + odtDocument.subscribe(ops.OdtDocument.signalProcessingBatchEnd, emitStylingChanges); + sessionConstraints.subscribe(gui.CommonConstraints.EDIT.REVIEW_MODE, updateEnabledState); - updateSelectionStylesInfo(); + selectionInfoCache = new core.LazyProperty(getSelectionInfo); + lastSignalledStyleSummary = getCachedStyleSummary(); updateEnabledState(); if (!directTextStylingEnabled) { diff --git a/webodf/lib/manifest.json b/webodf/lib/manifest.json index 6c6be8f3..2b46c6a0 100644 --- a/webodf/lib/manifest.json +++ b/webodf/lib/manifest.json @@ -18,6 +18,8 @@ ], "core.EventNotifier": [ ], + "core.LazyProperty": [ + ], "core.LoopWatchDog": [ ], "core.PositionFilter": [ @@ -81,6 +83,7 @@ "gui.CommonConstraints": [ ], "gui.DirectFormattingController": [ + "core.LazyProperty", "gui.CommonConstraints", "gui.SessionConstraints", "gui.SessionContext", diff --git a/webodf/tools/karma.conf.js b/webodf/tools/karma.conf.js index b4b76807..a423164d 100644 --- a/webodf/tools/karma.conf.js +++ b/webodf/tools/karma.conf.js @@ -29,6 +29,7 @@ module.exports = function (config) { 'lib/core/Cursor.js', 'lib/core/Destroyable.js', 'lib/core/EventNotifier.js', + 'lib/core/LazyProperty.js', 'lib/core/LoopWatchDog.js', 'lib/core/PositionIterator.js', 'lib/core/PositionFilter.js',