Merge pull request #343 from peitschie/align-perf

Improve performance of direct paragraph styling
This commit is contained in:
Philip Peitsch
2014-02-06 00:16:18 +11:00
11 changed files with 208 additions and 33 deletions
@@ -0,0 +1,65 @@
/**
* @license
* Copyright (C) 2014 KO GmbH <copyright@kogmbh.com>
*
* @licstart
* The JavaScript code in this page 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. The code is distributed
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this code. If not, see <http://www.gnu.org/licenses/>.
*
* As additional permission under GNU AGPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* As a special exception to the AGPL, any HTML file which merely makes function
* calls to this code, and for that purpose includes it by reference shall be
* deemed a separate work for copyright law purposes. In addition, the copyright
* holders of this code give you permission to combine this code with free
* software libraries that are released under the GNU LGPL. You may copy and
* distribute such a system following the terms of the GNU AGPL for this code
* and the LGPL for the libraries. If you modify this code, you may extend this
* exception to your version of the code, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your
* version.
*
* This license applies to this entire compilation.
* @licend
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
*/
define(["BenchmarkAction"], function(BenchmarkAction) {
"use strict";
/**
* Align the current selection 'justify'
* @constructor
*/
function AlignCurrentSelectionJustified() {
var state = {description: "Align the current selection 'justify'"},
action = new BenchmarkAction(state);
this.subscribe = action.subscribe;
this.state = state;
/**
* @param {!SharedState} sharedState
* @return {undefined}
*/
this.start = function(sharedState) {
action.start();
sharedState.sessionController.getDirectFormattingController().alignParagraphJustified();
action.complete(true);
}
}
return AlignCurrentSelectionJustified;
});
+1 -1
View File
@@ -58,7 +58,7 @@ define(["BenchmarkAction"], function(BenchmarkAction) {
action = new BenchmarkAction(state),
localMemberId = "localmember",
sessionControllerOptions = {
directStylingEnabled: true
directParagraphStylingEnabled: true
},
viewOptions = {
editInfoMarkersInitiallyVisible: false,
+4 -1
View File
@@ -49,10 +49,12 @@ define([
"RemoveCurrentSelection",
"PreloadDocument",
"BoldCurrentSelection",
"AlignCurrentSelectionJustified",
"MoveCursorToEnd"
], function (Benchmark, HTMLResultsRenderer,
OpenDocument, EnterEditMode, MoveCursorToEndDirect,InsertLetterA, RemovePositions, MoveCursorLeft,
SelectEntireDocument, RemoveCurrentSelection, PreloadDocument, BoldCurrentSelection, MoveCursorToEnd) {
SelectEntireDocument, RemoveCurrentSelection, PreloadDocument, BoldCurrentSelection,
AlignCurrentSelectionJustified, MoveCursorToEnd) {
"use strict";
/**
@@ -119,6 +121,7 @@ define([
benchmark.actions.push(new RemovePositions(100, true));
benchmark.actions.push(new SelectEntireDocument());
benchmark.actions.push(new BoldCurrentSelection());
benchmark.actions.push(new AlignCurrentSelectionJustified());
benchmark.actions.push(new RemoveCurrentSelection());
this.start = benchmark.start;
+42 -13
View File
@@ -460,6 +460,15 @@ gui.DirectFormattingController = function DirectFormattingController(session, in
return step === ops.StepsTranslator.NEXT_STEP;
}
/**
* @param {!Object.<!string, *>} obj
* @param {!string} key
* @return {*}
*/
function getOwnProperty(obj, key) {
return obj.hasOwnProperty(key) ? obj[key] : undefined;
}
/**
* @param {!function(!Object) : !Object} applyDirectStyling
* @return {undefined}
@@ -468,29 +477,49 @@ gui.DirectFormattingController = function DirectFormattingController(session, in
var range = odtDocument.getCursor(inputMemberId).getSelectedRange(),
paragraphs = odfUtils.getParagraphElements(range),
formatting = odtDocument.getFormatting(),
operations = [];
operations = [],
derivedStyleNames = {},
defaultStyleName;
paragraphs.forEach(function(paragraph) {
var paragraphStartPoint = odtDocument.convertDomPointToCursorStep(paragraph, 0, roundUp),
paragraphStyleName = paragraph.getAttributeNS(odf.Namespaces.textns, "style-name"),
newParagraphStyleName = objectNameGenerator.generateStyleName(),
newParagraphStyleName,
opAddStyle,
opSetParagraphStyle,
paragraphProperties;
// Try and reuse an existing paragraph style if possible
if (paragraphStyleName) {
paragraphProperties = formatting.createDerivedStyleObject(paragraphStyleName, "paragraph", {});
newParagraphStyleName = getOwnProperty(derivedStyleNames, paragraphStyleName);
} else {
newParagraphStyleName = defaultStyleName;
}
paragraphProperties = applyDirectStyling(paragraphProperties || {});
opAddStyle = new ops.OpAddStyle();
opAddStyle.init({
memberid: inputMemberId,
styleName: newParagraphStyleName,
styleFamily: 'paragraph',
isAutomaticStyle: true,
setProperties: paragraphProperties
});
operations.push(opAddStyle);
if (!newParagraphStyleName) {
newParagraphStyleName = objectNameGenerator.generateStyleName();
if (paragraphStyleName) {
derivedStyleNames[paragraphStyleName] = newParagraphStyleName;
paragraphProperties = formatting.createDerivedStyleObject(paragraphStyleName, "paragraph", {});
} else {
defaultStyleName = newParagraphStyleName;
paragraphProperties = {};
}
// The assumption is that applyDirectStyling will return the same transform given the same
// paragraph properties (e.g., there is nothing dependent on whether this is the 10th paragraph)
paragraphProperties = applyDirectStyling(paragraphProperties);
opAddStyle = new ops.OpAddStyle();
opAddStyle.init({
memberid: inputMemberId,
styleName: newParagraphStyleName,
styleFamily: 'paragraph',
isAutomaticStyle: true,
setProperties: paragraphProperties
});
operations.push(opAddStyle);
}
opSetParagraphStyle = new ops.OpSetParagraphStyle();
opSetParagraphStyle.init({
+2 -2
View File
@@ -924,9 +924,9 @@ gui.SessionController = (function () {
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, redrawRegionSelectionTask.trigger);
odtDocument.subscribe(ops.OdtDocument.signalCursorAdded, inputMethodEditor.registerCursor);
odtDocument.subscribe(ops.OdtDocument.signalCursorRemoved, inputMethodEditor.removeCursor);
odtDocument.subscribe(ops.OdtDocument.signalOperationStart, saveFocus);
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, restoreFocus);
odtDocument.subscribe(ops.OdtDocument.signalOperationEnd, updateUndoStack);
odtDocument.subscribe(ops.OdtDocument.signalProcessingBatchStart, saveFocus);
odtDocument.subscribe(ops.OdtDocument.signalProcessingBatchEnd, restoreFocus);
}
init();
+3
View File
@@ -305,7 +305,9 @@
"odf/OdfContainer.js"
],
"odf/OdfCanvas.js": [
"core/Async.js",
"core/DomUtils.js",
"core/ScheduledTask.js",
"gui/AnnotationViewManager.js",
"odf/FontLoader.js",
"odf/Formatting.js",
@@ -574,6 +576,7 @@
"odf/OdfUtils.js"
],
"ops/TrivialOperationRouter.js": [
"core/EventNotifier.js",
"ops/Operation.js",
"ops/OperationFactory.js",
"ops/OperationRouter.js"
+35 -8
View File
@@ -39,7 +39,9 @@
/*jslint sub: true*/
/*global runtime, odf, xmldom, webodf_css, core, gui */
runtime.loadClass("core.Async");
runtime.loadClass("core.DomUtils");
runtime.loadClass("core.ScheduledTask");
runtime.loadClass("odf.OdfContainer");
runtime.loadClass("odf.Formatting");
runtime.loadClass("xmldom.XPath");
@@ -944,6 +946,7 @@ runtime.loadClass("gui.AnnotationViewManager");
"odf.OdfCanvas constructor needs DOM");
var self = this,
doc = /**@type{!Document}*/(element.ownerDocument),
async = new core.Async(),
/**@type{!odf.OdfContainer}*/
odfcontainer,
/**@type{!odf.Formatting}*/
@@ -969,6 +972,9 @@ runtime.loadClass("gui.AnnotationViewManager");
/**@type{!Object.<string,!Array.<!Function>>}*/
eventHandlers = {},
waitingForDoneTimeoutId,
/**@type{!core.ScheduledTask}*/redrawContainerTask,
shouldRefreshCss = false,
shouldRerenderAnnotations = false,
loadingQueue = new LoadingQueue();
/**
@@ -1112,6 +1118,25 @@ runtime.loadClass("gui.AnnotationViewManager");
element.style.width = Math.round(zoomLevel * sizer.offsetWidth) + "px";
element.style.height = Math.round(zoomLevel * sizer.offsetHeight) + "px";
}
/**
* @return {undefined}
*/
function redrawContainer() {
if (shouldRefreshCss) {
handleStyles(odfcontainer, formatting, stylesxmlcss);
shouldRefreshCss = false;
// different styles means different layout, thus different sizes
}
if (shouldRerenderAnnotations) {
if (annotationViewManager) {
annotationViewManager.rerenderAnnotations();
}
shouldRerenderAnnotations = false;
}
fixContainerSize();
}
/**
* A new content.xml has been loaded. Update the live document with it.
* @param {!odf.OdfContainer} container
@@ -1266,9 +1291,8 @@ runtime.loadClass("gui.AnnotationViewManager");
* @return {undefined}
*/
this.refreshCSS = function () {
handleStyles(odfcontainer, formatting, stylesxmlcss);
// different styles means different layout, thus different sizes:
fixContainerSize();
shouldRefreshCss = true;
redrawContainerTask.trigger();
};
/**
@@ -1277,7 +1301,7 @@ runtime.loadClass("gui.AnnotationViewManager");
* @return {undefined}
*/
this.refreshSize = function () {
fixContainerSize();
redrawContainerTask.trigger();
};
/**
* @return {!odf.OdfContainer}
@@ -1368,8 +1392,8 @@ runtime.loadClass("gui.AnnotationViewManager");
*/
this.rerenderAnnotations = function () {
if (annotationViewManager) {
annotationViewManager.rerenderAnnotations();
fixContainerSize();
shouldRerenderAnnotations = true;
redrawContainerTask.trigger();
}
};
@@ -1543,7 +1567,9 @@ runtime.loadClass("gui.AnnotationViewManager");
* @return {undefined}
*/
this.destroy = function(callback) {
var head = /**@type{!HTMLHeadElement}*/(doc.getElementsByTagName('head')[0]);
var head = /**@type{!HTMLHeadElement}*/(doc.getElementsByTagName('head')[0]),
cleanup = [pageSwitcher.destroy, redrawContainerTask.destroy];
runtime.clearTimeout(waitingForDoneTimeoutId);
// TODO: anything to clean with annotationViewManager?
if (annotationsPane && annotationsPane.parentNode) {
@@ -1560,7 +1586,7 @@ runtime.loadClass("gui.AnnotationViewManager");
head.removeChild(positioncss);
// TODO: loadingQueue, make sure it is empty
pageSwitcher.destroy(callback);
async.destroyAll(cleanup, callback);
};
function init() {
@@ -1569,6 +1595,7 @@ runtime.loadClass("gui.AnnotationViewManager");
fontcss = addStyleSheet(doc);
stylesxmlcss = addStyleSheet(doc);
positioncss = addStyleSheet(doc);
redrawContainerTask = new core.ScheduledTask(redrawContainer, 0);
}
init();
+4
View File
@@ -79,6 +79,8 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
ops.OdtDocument.signalTableAdded,
ops.OdtDocument.signalOperationStart,
ops.OdtDocument.signalOperationEnd,
ops.OdtDocument.signalProcessingBatchStart,
ops.OdtDocument.signalProcessingBatchEnd,
ops.OdtDocument.signalUndoStackChanged,
ops.OdtDocument.signalStepsInserted,
ops.OdtDocument.signalStepsRemoved
@@ -908,6 +910,8 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
/**@const*/ops.OdtDocument.signalParagraphStyleModified = "paragraphstyle/modified";
/**@const*/ops.OdtDocument.signalOperationStart = "operation/start";
/**@const*/ops.OdtDocument.signalOperationEnd = "operation/end";
/**@const*/ops.OdtDocument.signalProcessingBatchStart = "router/batchstart";
/**@const*/ops.OdtDocument.signalProcessingBatchEnd = "router/batchend";
/**@const*/ops.OdtDocument.signalUndoStackChanged = "undo/changed";
/**@const*/ops.OdtDocument.signalStepsInserted = "steps/inserted";
/**@const*/ops.OdtDocument.signalStepsRemoved = "steps/removed";
+14
View File
@@ -115,3 +115,17 @@ ops.OperationRouter.prototype.hasLocalUnsyncedOps = function () {"use strict"; }
* @return {!boolean}
*/
ops.OperationRouter.prototype.hasSessionHostConnection = function () {"use strict"; };
/**
* Signal that the operation router is about to process a batch of operations.
* @const
* @type {string}
*/
ops.OperationRouter.signalProcessingBatchStart = "router/batchstart";
/**
* Signal that the operation router has just completed processing a batch of operations.
* @const
* @type {string}
*/
ops.OperationRouter.signalProcessingBatchEnd = "router/batchend";
+24
View File
@@ -57,6 +57,24 @@ ops.Session = function Session(odfCanvas) {
/**@type{?ops.OperationRouter}*/
operationRouter = null;
/**
* Forward the router's batch start signal on to the document
* @param {*} args
* @return {undefined}
*/
function forwardBatchStart(args) {
odtDocument.emit(ops.OdtDocument.signalProcessingBatchStart, args);
}
/**
* Forward the router's batch end signal on to the document
* @param {*} args
* @return {undefined}
*/
function forwardBatchEnd(args) {
odtDocument.emit(ops.OdtDocument.signalProcessingBatchEnd, args);
}
/**
* @param {!ops.OperationFactory} opFactory
*/
@@ -72,7 +90,13 @@ ops.Session = function Session(odfCanvas) {
* @return {undefined}
*/
this.setOperationRouter = function (opRouter) {
if (operationRouter) {
operationRouter.unsubscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart);
operationRouter.unsubscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd);
}
operationRouter = opRouter;
operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart);
operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd);
opRouter.setPlaybackFunction(function (op) {
odtDocument.emit(ops.OdtDocument.signalOperationStart, op);
if (op.execute(odtDocument)) {
+14 -8
View File
@@ -35,7 +35,9 @@
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
*/
/*global ops*/
/*global ops, runtime, core*/
runtime.loadClass("core.EventNotifier");
/*
* route the operations.
@@ -52,7 +54,11 @@
ops.TrivialOperationRouter = function TrivialOperationRouter() {
"use strict";
var operationFactory,
var events = new core.EventNotifier([
ops.OperationRouter.signalProcessingBatchStart,
ops.OperationRouter.signalProcessingBatchEnd
]),
operationFactory,
playbackFunction,
groupIdentifier = 0;
@@ -89,6 +95,7 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() {
// The current implementation is only designed for a localeditor instance & the TrivialUndoManager.
// TODO redesign this concept to work with collaborative editing
groupIdentifier += 1;
events.emit(ops.OperationRouter.signalProcessingBatchStart, {});
operations.forEach(function(op) {
var timedOp,
opspec = op.spec();
@@ -100,6 +107,7 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() {
// TODO: handle return flag in error case
playbackFunction(timedOp);
});
events.emit(ops.OperationRouter.signalProcessingBatchEnd, {});
};
this.close = function (cb) {
@@ -111,20 +119,18 @@ ops.TrivialOperationRouter = function TrivialOperationRouter() {
* @param {!Function} cb
* @return {undefined}
*/
/*jslint emptyblock: true, unparam: true*/
this.subscribe = function (eventId, cb) {
this.subscribe = function(eventId, cb) {
events.subscribe(eventId, cb);
};
/*jslint emptyblock: false, unparam: false*/
/**
* @param {!string} eventId
* @param {!Function} cb
* @return {undefined}
*/
/*jslint emptyblock: true, unparam: true*/
this.unsubscribe = function (eventId, cb) {
this.unsubscribe = function(eventId, cb) {
events.unsubscribe(eventId, cb);
};
/*jslint emptyblock: false, unparam: false*/
/**
* @return {!boolean}