| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.UrlStringTransferHandler = function VeUiUrlStringTransferHandler() { |
| 19 |
|
| 20 |
ve.ui.UrlStringTransferHandler.super.apply( this, arguments ); |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
OO.inheritClass( ve.ui.UrlStringTransferHandler, ve.ui.PlainTextStringTransferHandler ); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
ve.ui.UrlStringTransferHandler.static.name = 'urlString'; |
| 30 |
|
| 31 |
ve.ui.UrlStringTransferHandler.static.types = [ |
| 32 |
|
| 33 |
'text/uri-list', |
| 34 |
|
| 35 |
|
| 36 |
'text/x-moz-url', |
| 37 |
|
| 38 |
'text/x-uri', |
| 39 |
|
| 40 |
...ve.ui.UrlStringTransferHandler.super.static.types |
| 41 |
]; |
| 42 |
|
| 43 |
ve.ui.UrlStringTransferHandler.static.handlesPaste = true; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
ve.ui.UrlStringTransferHandler.static.urlRegExp = null; |
| 52 |
|
| 53 |
ve.init.Platform.static.initializedPromise.then( () => { |
| 54 |
ve.ui.UrlStringTransferHandler.static.urlRegExp = |
| 55 |
|
| 56 |
new RegExp( |
| 57 |
ve.init.platform.getExternalLinkUrlProtocolsRegExp().source + |
| 58 |
'\\S+$', |
| 59 |
'i' |
| 60 |
); |
| 61 |
} ); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.ui.UrlStringTransferHandler.static.matchFunction = function ( item ) { |
| 66 |
|
| 67 |
if ( ve.ui.UrlStringTransferHandler.static.types.indexOf( item.type ) >= 0 && |
| 68 |
item.type !== 'text/plain' ) { |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
return ve.ui.UrlStringTransferHandler.static.urlRegExp.test( |
| 75 |
item.getAsString() |
| 76 |
); |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
ve.ui.UrlStringTransferHandler.prototype.process = function () { |
| 83 |
const surface = this.surface, |
| 84 |
store = surface.getModel().getDocument().getStore(), |
| 85 |
linkAction = ve.ui.actionFactory.create( 'link', surface ), |
| 86 |
data = this.item.getAsString(); |
| 87 |
|
| 88 |
let links; |
| 89 |
switch ( this.item.type ) { |
| 90 |
case 'text/uri-list': |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
links = data.replace( /^#.*(\r\n?|\n|$)/mg, '' ).trim() |
| 98 |
.split( /[\r\n]+/g ).map( ( line ) => ( { href: line } ) ); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if ( this.item.data.htmlStringData ) { |
| 104 |
const html = this.item.data.htmlStringData; |
| 105 |
const doc = ve.createDocumentFromHtml( html ); |
| 106 |
links = $.makeArray( doc.querySelectorAll( 'a[href]' ) ) |
| 107 |
.map( ( a ) => ( { href: a.href, title: a.textContent } ) ); |
| 108 |
} |
| 109 |
break; |
| 110 |
|
| 111 |
case 'text/x-moz-url': |
| 112 |
|
| 113 |
links = data.match( /^(.*)(\r\n?|\n)(.*)$/mg ).map( ( item ) => { |
| 114 |
item = item.split( /[\r\n]+/ ); |
| 115 |
return { href: item[ 0 ], title: item[ 1 ] }; |
| 116 |
} ); |
| 117 |
break; |
| 118 |
|
| 119 |
default: |
| 120 |
|
| 121 |
links = [ { href: data.trim() } ]; |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
const result = []; |
| 127 |
links.forEach( ( link ) => { |
| 128 |
const annotation = linkAction.getLinkAnnotation( link.href ), |
| 129 |
annotationSet = new ve.dm.AnnotationSet( store, store.hashAll( [ |
| 130 |
annotation |
| 131 |
] ) ), |
| 132 |
title = link.title || annotation.getDisplayTitle(), |
| 133 |
content = title.split( '' ); |
| 134 |
|
| 135 |
|
| 136 |
if ( result.length ) { |
| 137 |
result.push( ' ' ); |
| 138 |
} |
| 139 |
|
| 140 |
ve.dm.Document.static.addAnnotationsToData( content, annotationSet ); |
| 141 |
for ( let i = 0; i < content.length; i++ ) { |
| 142 |
result.push( content[ i ] ); |
| 143 |
} |
| 144 |
} ); |
| 145 |
this.resolve( result ); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
ve.ui.dataTransferHandlerFactory.register( ve.ui.UrlStringTransferHandler ); |