Skip to content

Commit cce2827

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 3e47df7 commit cce2827

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
@@ -434,31 +434,27 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
434434
return {textNode: lastTextNode, offset: nodeOffset };
435435
}
436436

437-
/**
438-
* @param {!ops.Operation} op
439-
* @return {undefined}
440-
*/
441-
function prepareOperationExecution(op) {
442-
eventNotifier.emit(ops.OdtDocument.signalOperationStart, op);
443-
}
444-
445437
/**
446438
* Called after an operation is executed, this
447439
* function will check if the operation is an
448440
* 'edit', and in that case will update the
449441
* document's metadata, such as dc:creator,
450442
* meta:editing-cycles, and dc:creator.
451443
* @param {!ops.Operation} op
444+
* @param {!Array.<!ops.Operation.Event>} events
452445
* @return {undefined}
453446
*/
454-
function finishOperationExecution(op) {
447+
function finishOperationExecution(op, events) {
455448
var opspec = op.spec(),
456449
memberId = opspec.memberid,
457450
date = new Date(opspec.timestamp).toISOString(),
458451
odfContainer = odfCanvas.odfContainer(),
459-
/**@type{!{setProperties: !Object, removedProperties: ?Array.<!string>}}*/
452+
/**@type{!ops.Operation.Event}*/
453+
changedMetadataEvent,
454+
/**@type{!{setProperties: !Object, removedProperties: !Array.<!string>}}*/
460455
changedMetadata,
461-
fullName;
456+
fullName,
457+
i;
462458

463459
// If the operation is an edit (that changes the
464460
// ODF that will be saved), then update metadata.
@@ -469,13 +465,25 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
469465
"dc:date": date
470466
}, null);
471467

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

480488
// If no previous op was found in this session,
481489
// then increment meta:editing-cycles by 1.
@@ -497,16 +505,6 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
497505

498506
eventNotifier.emit(ops.OdtDocument.signalOperationEnd, op);
499507

500-
if (op.isEdit) {
501-
eventNotifier.emit(ops.OdtDocument.signalMetadataUpdated, changedMetadata);
502-
}
503-
}
504-
505-
/**
506-
* @param {!Array.<!ops.Operation.Event>} events
507-
* @return {undefined}
508-
*/
509-
function emitEvents(events) {
510508
events.forEach(function(event) {
511509
eventNotifier.emit(event.eventid, event.args);
512510
});
@@ -519,14 +517,15 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
519517
this.executeOperation = function(op) {
520518
var events;
521519

522-
prepareOperationExecution(op);
520+
eventNotifier.emit(ops.OdtDocument.signalOperationStart, op);
521+
523522
events = op.execute(self);
524-
if (events !== null) {
525-
finishOperationExecution(op);
526-
emitEvents(events);
527-
return true;
523+
if (events === null) {
524+
return false;
528525
}
529-
return false;
526+
527+
finishOperationExecution(op, events);
528+
return true;
530529
};
531530

532531
/**

0 commit comments

Comments
 (0)