Skip to content

Commit 41078f7

Browse files
author
Friedrich W. H. Kossebau
committed
Merge methods only used by ops.ODtDocument.executeOperation
Also merge creator metadata update signal into any existing metadata update signal
1 parent 7c461a9 commit 41078f7

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

webodf/lib/ops/OdtDocument.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -432,31 +432,27 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
432432
return {textNode: lastTextNode, offset: nodeOffset };
433433
}
434434

435-
/**
436-
* @param {!ops.Operation} op
437-
* @return {undefined}
438-
*/
439-
function prepareOperationExecution(op) {
440-
eventNotifier.emit(ops.OdtDocument.signalOperationStart, op);
441-
}
442-
443435
/**
444436
* Called after an operation is executed, this
445437
* function will check if the operation is an
446438
* 'edit', and in that case will update the
447439
* document's metadata, such as dc:creator,
448440
* meta:editing-cycles, and dc:creator.
449441
* @param {!ops.Operation} op
442+
* @param {!Array.<!ops.Operation.Event>} events
450443
* @return {undefined}
451444
*/
452-
function finishOperationExecution(op) {
445+
function finishOperationExecution(op, events) {
453446
var opspec = op.spec(),
454447
memberId = opspec.memberid,
455448
date = new Date(opspec.timestamp).toISOString(),
456449
odfContainer = odfCanvas.odfContainer(),
457-
/**@type{!{setProperties: !Object, removedProperties: ?Array.<!string>}}*/
450+
/**@type{!ops.Operation.Event}*/
451+
changedMetadataEvent,
452+
/**@type{!{setProperties: !Object, removedProperties: !Array.<!string>}}*/
458453
changedMetadata,
459-
fullName;
454+
fullName,
455+
i;
460456

461457
// If the operation is an edit (that changes the
462458
// ODF that will be saved), then update metadata.
@@ -467,13 +463,25 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
467463
"dc:date": date
468464
}, null);
469465

470-
changedMetadata = {
471-
setProperties: {
472-
"dc:creator": fullName,
473-
"dc:date": date
474-
},
475-
removedProperties: []
476-
};
466+
// find any existing metadataupdated event or create one
467+
for (i = 0; i < events.length; i += 1) {
468+
if (events[i].eventid === ops.OdtDocument.signalMetadataUpdated) {
469+
changedMetadataEvent = events[i];
470+
changedMetadata = /**@type{!{setProperties: !Object, removedProperties: !Array.<!string>}}*/(changedMetadataEvent.args);
471+
break;
472+
}
473+
}
474+
if (!changedMetadataEvent) {
475+
changedMetadata = { setProperties: {}, removedProperties: [] };
476+
changedMetadataEvent = {
477+
eventid: ops.OdtDocument.signalMetadataUpdated,
478+
args: changedMetadata
479+
};
480+
events.push(changedMetadataEvent);
481+
}
482+
483+
changedMetadata.setProperties["dc:creator"] = fullName;
484+
changedMetadata.setProperties["dc:date"] = date;
477485

478486
// If no previous op was found in this session,
479487
// then increment meta:editing-cycles by 1.
@@ -495,16 +503,6 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
495503

496504
eventNotifier.emit(ops.OdtDocument.signalOperationEnd, op);
497505

498-
if (op.isEdit) {
499-
eventNotifier.emit(ops.OdtDocument.signalMetadataUpdated, changedMetadata);
500-
}
501-
}
502-
503-
/**
504-
* @param {!Array.<!ops.Operation.Event>} events
505-
* @return {undefined}
506-
*/
507-
function emitEvents(events) {
508506
events.forEach(function(event) {
509507
eventNotifier.emit(event.eventid, event.args);
510508
});
@@ -517,14 +515,15 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
517515
this.executeOperation = function(op) {
518516
var events;
519517

520-
prepareOperationExecution(op);
518+
eventNotifier.emit(ops.OdtDocument.signalOperationStart, op);
519+
521520
events = op.execute(self);
522-
if (events !== null) {
523-
finishOperationExecution(op);
524-
emitEvents(events);
525-
return true;
521+
if (events === null) {
522+
return false;
526523
}
527-
return false;
524+
525+
finishOperationExecution(op, events);
526+
return true;
528527
};
529528

530529
/**

0 commit comments

Comments
 (0)