| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.MWMagicLinkNodeInspector = function VeUiMWMagicLinkNodeInspector() { |
| 18 |
|
| 19 |
ve.ui.MWMagicLinkNodeInspector.super.apply( this, arguments ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.inheritClass( ve.ui.MWMagicLinkNodeInspector, ve.ui.NodeInspector ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.ui.MWMagicLinkNodeInspector.static.name = 'linkMagicNode'; |
| 29 |
|
| 30 |
ve.ui.MWMagicLinkNodeInspector.static.title = null; |
| 31 |
|
| 32 |
ve.ui.MWMagicLinkNodeInspector.static.modelClasses = [ ve.dm.MWMagicLinkNode ]; |
| 33 |
|
| 34 |
ve.ui.MWMagicLinkNodeInspector.static.actions = [ |
| 35 |
...ve.ui.MWMagicLinkNodeInspector.super.static.actions, |
| 36 |
{ |
| 37 |
action: 'convert', |
| 38 |
label: OO.ui.deferMsg( 'visualeditor-magiclinknodeinspector-convert-link' ), |
| 39 |
modes: [ 'edit' ] |
| 40 |
} |
| 41 |
]; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ve.ui.MWMagicLinkNodeInspector.prototype.initialize = function () { |
| 49 |
|
| 50 |
ve.ui.MWMagicLinkNodeInspector.super.prototype.initialize.call( this ); |
| 51 |
|
| 52 |
|
| 53 |
this.targetInput = new OO.ui.TextInputWidget( { |
| 54 |
validate: this.validate.bind( this ) |
| 55 |
} ); |
| 56 |
this.targetInput.on( 'change', this.onChange.bind( this ) ); |
| 57 |
|
| 58 |
|
| 59 |
this.form.$element.append( this.targetInput.$element ); |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
ve.ui.MWMagicLinkNodeInspector.prototype.validate = function ( str ) { |
| 71 |
const node = this.getFragment().getSelectedNode(); |
| 72 |
return node.constructor.static.validateContent( str, node.getMagicType() ); |
| 73 |
}; |
| 74 |
|
| 75 |
ve.ui.MWMagicLinkNodeInspector.prototype.onChange = function ( value ) { |
| 76 |
|
| 77 |
const isValid = this.validate( value ); |
| 78 |
this.actions.forEach( null, ( action ) => { |
| 79 |
if ( !action.hasFlag( 'safe' ) ) { |
| 80 |
action.setDisabled( !isValid ); |
| 81 |
} |
| 82 |
} ); |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
ve.ui.MWMagicLinkNodeInspector.prototype.getActionProcess = function ( action ) { |
| 89 |
if ( ( action === 'done' || action === 'convert' ) && |
| 90 |
!this.validate( this.targetInput.getValue() ) ) { |
| 91 |
|
| 92 |
return new OO.ui.Process( 0 ); |
| 93 |
} |
| 94 |
if ( action === 'convert' ) { |
| 95 |
return new OO.ui.Process( () => { |
| 96 |
this.close( { action: action } ); |
| 97 |
} ); |
| 98 |
} |
| 99 |
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getActionProcess.call( this, action ); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ve.ui.MWMagicLinkNodeInspector.prototype.getSetupProcess = function ( data ) { |
| 106 |
|
| 107 |
const fragment = data.fragment, |
| 108 |
node = fragment instanceof ve.dm.SurfaceFragment ? |
| 109 |
fragment.getSelectedNode() : null, |
| 110 |
type = node instanceof ve.dm.MWMagicLinkNode ? |
| 111 |
node.getMagicType() : null, |
| 112 |
msg = type ? |
| 113 |
'visualeditor-magiclinknodeinspector-title-' + type.toLowerCase() : |
| 114 |
null; |
| 115 |
|
| 116 |
data = ve.extendObject( { |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
title: msg ? OO.ui.deferMsg( msg ) : null |
| 122 |
}, data ); |
| 123 |
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getSetupProcess.call( this, data ) |
| 124 |
.next( () => { |
| 125 |
|
| 126 |
this.targetInput.setValue( |
| 127 |
this.selectedNode ? this.selectedNode.getAttribute( 'content' ) : '' |
| 128 |
).setReadOnly( this.isReadOnly() ); |
| 129 |
} ); |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
ve.ui.MWMagicLinkNodeInspector.prototype.getReadyProcess = function ( data ) { |
| 136 |
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getReadyProcess.call( this, data ) |
| 137 |
.next( () => { |
| 138 |
this.targetInput.focus().select(); |
| 139 |
} ); |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
ve.ui.MWMagicLinkNodeInspector.prototype.getTeardownProcess = function ( data ) { |
| 146 |
data = data || {}; |
| 147 |
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data ) |
| 148 |
.first( () => { |
| 149 |
const surfaceView = this.manager.getSurface().getView(), |
| 150 |
surfaceModel = this.getFragment().getSurface(), |
| 151 |
doc = surfaceModel.getDocument(), |
| 152 |
nodeRange = this.selectedNode.getOuterRange(), |
| 153 |
value = this.targetInput.getValue(), |
| 154 |
done = data.action === 'done', |
| 155 |
convert = data.action === 'convert', |
| 156 |
remove = data.action === 'remove' || ( done && !value ); |
| 157 |
|
| 158 |
if ( remove ) { |
| 159 |
surfaceModel.change( |
| 160 |
ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange ) |
| 161 |
); |
| 162 |
} else if ( convert ) { |
| 163 |
const annotation = ve.dm.MWMagicLinkNode.static.annotationFromContent( |
| 164 |
value |
| 165 |
); |
| 166 |
if ( annotation ) { |
| 167 |
const annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone(); |
| 168 |
annotations.push( annotation ); |
| 169 |
const content = value.split( '' ); |
| 170 |
ve.dm.Document.static.addAnnotationsToData( content, annotations ); |
| 171 |
surfaceModel.change( |
| 172 |
ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content ) |
| 173 |
); |
| 174 |
setTimeout( () => { |
| 175 |
surfaceView.selectAnnotation( ( view ) => view.model instanceof ve.dm.LinkAnnotation ); |
| 176 |
} ); |
| 177 |
} |
| 178 |
} else if ( done && this.validate( value ) ) { |
| 179 |
surfaceModel.change( |
| 180 |
ve.dm.TransactionBuilder.static.newFromAttributeChanges( |
| 181 |
doc, nodeRange.start, { content: value } |
| 182 |
) |
| 183 |
); |
| 184 |
} |
| 185 |
} ); |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
ve.ui.windowFactory.register( ve.ui.MWMagicLinkNodeInspector ); |