MWLinkNodeInspector: Actually preserve annotations when converting to link annotation

This was implemented, but didn't work because the node was removed
before its annotations were copied.

Also fixed:
* Don't do an unnecessary transaction to change attributes of a node
  we're going to remove anyway.
* Apply the same link href fixups when converting as when not
  (previously, changing the href to 'example.com' and clicking "Add
  label" would break the link in interesting ways).
* Do a single transaction from replacement instead of removal+insertion
  when possible.

Bug: 67377
Change-Id: I0318ae62c799300fb7696506a9736b839e2c8578
This commit is contained in:
Bartosz Dziewoński 2014-07-01 21:46:23 +02:00
parent 6f54034e68
commit 5ed43c266a

View file

@ -106,40 +106,38 @@ ve.ui.MWLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
nodeRange = this.selectedNode.getOuterRange(),
value = this.targetInput.getValue(),
convert = data.action === 'convert',
remove = convert || data.action === 'remove' || !value;
remove = data.action === 'remove' || !value;
// Default to http:// if the external link doesn't already begin with a supported
// protocol - this prevents the link from being converted into literal text upon
// save and also fixes a common mistake users may make
if ( !ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
value = 'http://' + value;
}
if ( remove ) {
surfaceModel.change(
ve.dm.Transaction.newFromRemoval( doc, nodeRange )
);
} else {
// Default to http:// if the external link doesn't already begin with a supported
// protocol - this prevents the link from being converted into literal text upon
// save and also fixes a common mistake users may make
if ( !ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
value = 'http://' + value;
}
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
doc, nodeRange.start, { 'href': value }
)
);
}
if ( convert ) {
} else if ( convert ) {
annotation = new ve.dm.MWExternalLinkAnnotation( {
'type': 'link/mwExternal',
'attributes': {
'href': value
}
} );
annotations = doc.data.getAnnotationsFromOffset(
this.selectedNode.getOffset()
).clone();
annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
annotations.push( annotation );
content = ve.splitClusters( value );
ve.dm.Document.static.addAnnotationsToData( content, annotations );
surfaceModel.change(
ve.dm.Transaction.newFromInsertion( doc, nodeRange.start, content )
ve.dm.Transaction.newFromReplacement( doc, nodeRange, content )
);
} else {
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
doc, nodeRange.start, { 'href': value }
)
);
}
}, this );