| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.LinkContextItem = function VeUiLinkContextItem( context, model, config ) { |
| 18 |
|
| 19 |
ve.ui.LinkContextItem.super.call( this, context, model, config ); |
| 20 |
|
| 21 |
|
| 22 |
this.$element.addClass( 've-ui-linkContextItem' ); |
| 23 |
|
| 24 |
this.labelPreview = new OO.ui.LabelWidget(); |
| 25 |
if ( this.context.isMobile() ) { |
| 26 |
this.$labelLayout = $( '<div>' ).addClass( 've-ui-linkContextItem-label' ).append( |
| 27 |
$( '<div>' ).addClass( 've-ui-linkContextItem-label-body' ).append( |
| 28 |
new OO.ui.LabelWidget( { |
| 29 |
classes: [ 've-ui-linkContextItem-label-label' ], |
| 30 |
label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-label' ) |
| 31 |
} ).$element, |
| 32 |
$( '<div>' ).addClass( 've-ui-linkContextItem-label-preview' ).append( this.labelPreview.$element ) |
| 33 |
) |
| 34 |
); |
| 35 |
this.$innerBody = $( '<div>' ).addClass( 've-ui-linkContextItem-inner-body' ); |
| 36 |
this.$body.append( |
| 37 |
this.$labelLayout, |
| 38 |
new OO.ui.LabelWidget( { |
| 39 |
classes: [ 've-ui-linkContextItem-link-label' ], |
| 40 |
label: OO.ui.deferMsg( 'visualeditor-linkinspector-title' ) |
| 41 |
} ).$element, |
| 42 |
this.$innerBody |
| 43 |
); |
| 44 |
|
| 45 |
this.$body = this.$innerBody; |
| 46 |
} else { |
| 47 |
this.labelButton = new OO.ui.ButtonWidget( { |
| 48 |
label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-change' ), |
| 49 |
framed: false, |
| 50 |
flags: [ 'progressive' ] |
| 51 |
} ); |
| 52 |
this.$labelLayout = $( '<div>' ).addClass( 've-ui-linkContextItem-label' ).append( |
| 53 |
$( '<div>' ).addClass( 've-ui-linkContextItem-label-label' ).append( |
| 54 |
new OO.ui.IconWidget( { icon: 'quotes' } ).$element, |
| 55 |
new OO.ui.LabelWidget( { label: OO.ui.deferMsg( 'visualeditor-linkcontext-label-label' ) } ).$element |
| 56 |
), |
| 57 |
$( '<div>' ).addClass( 've-ui-linkContextItem-label-preview' ).append( this.labelPreview.$element ) |
| 58 |
); |
| 59 |
this.labelButton.connect( this, { click: 'onLabelButtonClick' } ); |
| 60 |
|
| 61 |
if ( !this.isReadOnly() ) { |
| 62 |
this.$labelLayout.append( $( '<div>' ).addClass( 've-ui-linkContextItem-label-action' ).append( this.labelButton.$element ) ); |
| 63 |
} |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
OO.inheritClass( ve.ui.LinkContextItem, ve.ui.AnnotationContextItem ); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ve.ui.LinkContextItem.static.name = 'link'; |
| 74 |
|
| 75 |
ve.ui.LinkContextItem.static.icon = 'link'; |
| 76 |
|
| 77 |
ve.ui.LinkContextItem.static.label = OO.ui.deferMsg( 'visualeditor-linkinspector-title' ); |
| 78 |
|
| 79 |
ve.ui.LinkContextItem.static.modelClasses = [ ve.dm.LinkAnnotation ]; |
| 80 |
|
| 81 |
ve.ui.LinkContextItem.static.embeddable = false; |
| 82 |
|
| 83 |
ve.ui.LinkContextItem.static.commandName = 'link'; |
| 84 |
|
| 85 |
ve.ui.LinkContextItem.static.clearable = true; |
| 86 |
|
| 87 |
ve.ui.LinkContextItem.static.clearMsg = OO.ui.deferMsg( 'visualeditor-linkcontext-remove' ); |
| 88 |
|
| 89 |
ve.ui.LinkContextItem.static.clearIcon = 'unLink'; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ve.ui.LinkContextItem.prototype.getDescription = function () { |
| 97 |
return this.model.getHref(); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ve.ui.LinkContextItem.prototype.renderBody = function () { |
| 104 |
const htmlDoc = this.context.getSurface().getModel().getDocument().getHtmlDocument(); |
| 105 |
const $link = $( '<a>' ) |
| 106 |
.addClass( 've-ui-linkContextItem-link' ) |
| 107 |
.text( this.getDescription() ) |
| 108 |
.attr( { |
| 109 |
target: '_blank', |
| 110 |
rel: 'noopener' |
| 111 |
} ); |
| 112 |
|
| 113 |
ve.setAttributeSafe( $link[ 0 ], 'href', ve.resolveUrl( this.model.getHref(), htmlDoc ), '#' ); |
| 114 |
this.$body.empty().append( $link ); |
| 115 |
if ( !this.context.isMobile() ) { |
| 116 |
this.$body.append( this.$labelLayout ); |
| 117 |
} |
| 118 |
this.updateLabelPreview(); |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ve.ui.LinkContextItem.prototype.updateLabelPreview = function () { |
| 127 |
const surfaceModel = this.context.getSurface().getModel(), |
| 128 |
annotationView = this.getAnnotationView(); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
let label; |
| 134 |
if ( annotationView ) { |
| 135 |
label = surfaceModel.getFragment().expandLinearSelection( 'annotation', annotationView.getModel() ).getText(); |
| 136 |
} |
| 137 |
|
| 138 |
this.labelPreview.setLabel( label || ve.msg( 'visualeditor-linkcontext-label-fallback' ) ); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
ve.ui.LinkContextItem.prototype.onLabelButtonClick = function () { |
| 149 |
const surface = this.context.getSurface().getView(), |
| 150 |
annotationView = this.getAnnotationView(); |
| 151 |
|
| 152 |
surface.selectNodeContents( |
| 153 |
annotationView.$element[ 0 ], |
| 154 |
this.context.isMobile() ? 'end' : undefined |
| 155 |
); |
| 156 |
|
| 157 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'context-label' } ); |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
ve.ui.contextItemFactory.register( ve.ui.LinkContextItem ); |