| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.MWLinkNodeInspector = function VeUiMWLinkNodeInspector() { |
| 18 |
|
| 19 |
ve.ui.MWLinkNodeInspector.super.apply( this, arguments ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.inheritClass( ve.ui.MWLinkNodeInspector, ve.ui.NodeInspector ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.ui.MWLinkNodeInspector.static.name = 'linkNode'; |
| 29 |
|
| 30 |
ve.ui.MWLinkNodeInspector.static.title = OO.ui.deferMsg( 'visualeditor-linknodeinspector-title' ); |
| 31 |
|
| 32 |
ve.ui.MWLinkNodeInspector.static.modelClasses = [ ve.dm.MWNumberedExternalLinkNode ]; |
| 33 |
|
| 34 |
ve.ui.MWLinkNodeInspector.static.actions = [ |
| 35 |
...ve.ui.MWLinkNodeInspector.super.static.actions, |
| 36 |
{ |
| 37 |
action: 'convert', |
| 38 |
label: OO.ui.deferMsg( 'visualeditor-linknodeinspector-add-label' ), |
| 39 |
modes: [ 'edit' ] |
| 40 |
} |
| 41 |
]; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ve.ui.MWLinkNodeInspector.prototype.initialize = function () { |
| 49 |
|
| 50 |
ve.ui.MWLinkNodeInspector.super.prototype.initialize.call( this ); |
| 51 |
|
| 52 |
|
| 53 |
this.targetInput = new OO.ui.TextInputWidget( { |
| 54 |
validate: ve.init.platform.getExternalLinkUrlProtocolsRegExp() |
| 55 |
} ); |
| 56 |
|
| 57 |
|
| 58 |
this.form.$element.append( this.targetInput.$element ); |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
ve.ui.MWLinkNodeInspector.prototype.getActionProcess = function ( action ) { |
| 65 |
if ( action === 'convert' ) { |
| 66 |
return new OO.ui.Process( () => { |
| 67 |
this.close( { action: action } ); |
| 68 |
} ); |
| 69 |
} |
| 70 |
return ve.ui.MWLinkNodeInspector.super.prototype.getActionProcess.call( this, action ); |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ve.ui.MWLinkNodeInspector.prototype.getSetupProcess = function ( data ) { |
| 77 |
return ve.ui.MWLinkNodeInspector.super.prototype.getSetupProcess.call( this, data ) |
| 78 |
.next( () => { |
| 79 |
|
| 80 |
this.targetInput.setValue( |
| 81 |
this.selectedNode ? this.selectedNode.getAttribute( 'href' ) : '' |
| 82 |
); |
| 83 |
this.targetInput.setReadOnly( this.isReadOnly() ); |
| 84 |
} ); |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
ve.ui.MWLinkNodeInspector.prototype.getReadyProcess = function ( data ) { |
| 91 |
return ve.ui.MWLinkNodeInspector.super.prototype.getReadyProcess.call( this, data ) |
| 92 |
.next( () => { |
| 93 |
this.targetInput.focus().select(); |
| 94 |
} ); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
ve.ui.MWLinkNodeInspector.prototype.getTeardownProcess = function ( data ) { |
| 101 |
data = data || {}; |
| 102 |
return ve.ui.MWLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data ) |
| 103 |
.first( () => { |
| 104 |
let value = this.targetInput.getValue(); |
| 105 |
const surfaceView = this.manager.getSurface().getView(), |
| 106 |
surfaceModel = this.getFragment().getSurface(), |
| 107 |
doc = surfaceModel.getDocument(), |
| 108 |
nodeRange = this.selectedNode.getOuterRange(), |
| 109 |
convert = data.action === 'convert', |
| 110 |
remove = data.action === 'remove' || !value; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if ( !ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) { |
| 116 |
value = 'http://' + value; |
| 117 |
} |
| 118 |
|
| 119 |
if ( remove ) { |
| 120 |
surfaceModel.change( |
| 121 |
ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange ) |
| 122 |
); |
| 123 |
} else if ( convert ) { |
| 124 |
const annotation = new ve.dm.MWExternalLinkAnnotation( { |
| 125 |
type: 'link/mwExternal', |
| 126 |
attributes: { |
| 127 |
href: value |
| 128 |
} |
| 129 |
} ); |
| 130 |
const annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone(); |
| 131 |
annotations.push( annotation ); |
| 132 |
const content = value.split( '' ); |
| 133 |
ve.dm.Document.static.addAnnotationsToData( content, annotations ); |
| 134 |
surfaceModel.change( |
| 135 |
ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content ) |
| 136 |
); |
| 137 |
setTimeout( () => { |
| 138 |
surfaceView.selectAnnotation( ( view ) => view.model instanceof ve.dm.MWExternalLinkAnnotation ); |
| 139 |
} ); |
| 140 |
} else { |
| 141 |
surfaceModel.change( |
| 142 |
ve.dm.TransactionBuilder.static.newFromAttributeChanges( |
| 143 |
doc, nodeRange.start, { href: value } |
| 144 |
) |
| 145 |
); |
| 146 |
} |
| 147 |
} ); |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ve.ui.windowFactory.register( ve.ui.MWLinkNodeInspector ); |