| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.MWWikitextStringTransferHandler = function VeUiMWWikitextStringTransferHandler() { |
| 19 |
|
| 20 |
ve.ui.MWWikitextStringTransferHandler.super.apply( this, arguments ); |
| 21 |
|
| 22 |
|
| 23 |
this.parsoidRequest = null; |
| 24 |
}; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
OO.inheritClass( ve.ui.MWWikitextStringTransferHandler, ve.ui.PlainTextStringTransferHandler ); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ve.ui.MWWikitextStringTransferHandler.static.name = 'wikitextString'; |
| 33 |
|
| 34 |
ve.ui.MWWikitextStringTransferHandler.static.types = [ |
| 35 |
...ve.ui.MWWikitextStringTransferHandler.super.static.types, |
| 36 |
'text/x-wiki' |
| 37 |
]; |
| 38 |
|
| 39 |
ve.ui.MWWikitextStringTransferHandler.static.handlesPaste = true; |
| 40 |
|
| 41 |
ve.ui.MWWikitextStringTransferHandler.static.matchFunction = function ( item ) { |
| 42 |
const text = item.getAsString(), |
| 43 |
registry = ve.ui.mwWikitextTransferRegistry; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
if ( item.type === 'text/x-wiki' ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if ( ve.dm.MWMagicLinkNode.static.validateContent( text.trim() ) ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
for ( const i in registry.registry ) { |
| 60 |
const rule = registry.registry[ i ]; |
| 61 |
if ( rule instanceof RegExp ) { |
| 62 |
if ( registry.registry[ i ].test( text ) ) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
} else if ( text.indexOf( rule ) !== -1 ) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
return false; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
ve.ui.MWWikitextStringTransferHandler.static.createDocumentFromParsoidHtml = function ( html, targetDoc ) { |
| 80 |
const htmlDoc = ve.createDocumentFromHtml( html ); |
| 81 |
|
| 82 |
|
| 83 |
mw.libs.ve.stripRestbaseIds( htmlDoc ); |
| 84 |
|
| 85 |
|
| 86 |
mw.libs.ve.stripParsoidFallbackIds( htmlDoc.body ); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
const doc = targetDoc.newFromHtml( htmlDoc, {} ); |
| 91 |
const data = doc.data.data; |
| 92 |
const surface = new ve.dm.Surface( doc ); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
for ( let i = data.length - 1; i >= 0; i-- ) { |
| 98 |
if ( ve.getProp( data[ i ], 'attributes', 'mw', 'autoGenerated' ) ) { |
| 99 |
surface.change( |
| 100 |
ve.dm.TransactionBuilder.static.newFromRemoval( |
| 101 |
doc, |
| 102 |
surface.getDocument().getDocumentNode().getNodeFromOffset( i + 1 ).getOuterRange() |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
doc.data.cloneElements( true ); |
| 110 |
|
| 111 |
return doc; |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
ve.ui.MWWikitextStringTransferHandler.prototype.process = function () { |
| 120 |
const wikitext = this.item.getAsString(); |
| 121 |
|
| 122 |
|
| 123 |
if ( ve.dm.MWMagicLinkNode.static.validateContent( wikitext.trim() ) ) { |
| 124 |
this.resolve( [ |
| 125 |
{ |
| 126 |
type: 'link/mwMagic', |
| 127 |
attributes: { |
| 128 |
content: wikitext.trim() |
| 129 |
} |
| 130 |
}, |
| 131 |
{ |
| 132 |
type: '/link/mwMagic' |
| 133 |
} |
| 134 |
] ); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
const failure = () => { |
| 139 |
|
| 140 |
|
| 141 |
this.resolve( wikitext ); |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
this.parsoidRequest = ve.init.target.parseWikitextFragment( wikitext, false, this.surface.getModel().getDocument() ); |
| 146 |
|
| 147 |
|
| 148 |
this.parsoidRequest.then( ( response ) => { |
| 149 |
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) { |
| 150 |
return failure(); |
| 151 |
} |
| 152 |
|
| 153 |
const doc = this.constructor.static.createDocumentFromParsoidHtml( |
| 154 |
response.visualeditor.content, |
| 155 |
this.surface.getModel().getDocument() |
| 156 |
); |
| 157 |
|
| 158 |
if ( !doc.data.hasContent() ) { |
| 159 |
return failure(); |
| 160 |
} |
| 161 |
|
| 162 |
this.resolve( doc ); |
| 163 |
}, failure ); |
| 164 |
|
| 165 |
this.createProgress( this.parsoidRequest, ve.msg( 'visualeditor-wikitext-progress' ) ); |
| 166 |
|
| 167 |
this.setProgress( null ); |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
ve.ui.MWWikitextStringTransferHandler.prototype.abort = function () { |
| 174 |
|
| 175 |
ve.ui.MWWikitextStringTransferHandler.super.prototype.abort.apply( this, arguments ); |
| 176 |
|
| 177 |
if ( this.parsoidRequest ) { |
| 178 |
this.parsoidRequest.abort(); |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
ve.ui.dataTransferHandlerFactory.register( ve.ui.MWWikitextStringTransferHandler ); |