Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/141384
Change subject: Inserted links should work as internal wiki links ...................................................................... Inserted links should work as internal wiki links With document.execCommand('CreateLink',..) it will be simple external links. Parsoid converts the links to internal link when there is an attribtue rel="mw:WikiLink" To add that attribute, we need to create the whole link html and insert/replace at curser/selection. We can not use InsertHTML command since it is not supported in IE <a href='Link' rel="mw:WikiLink">LinkText</a> becomes [[LinkText|Link]] Without the rel attribute parsoid gives [Link LinkText] Change-Id: Iab240509925fd278111fff492b414a65314afb8f --- M modules/tools/ext.cx.tools.link.js 1 file changed, 61 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation refs/changes/84/141384/1 diff --git a/modules/tools/ext.cx.tools.link.js b/modules/tools/ext.cx.tools.link.js index 6ee6f44..9210159 100644 --- a/modules/tools/ext.cx.tools.link.js +++ b/modules/tools/ext.cx.tools.link.js @@ -163,6 +163,66 @@ } } + /** + * Paste a given html string at the selection. + * It replaces the selected text if any. Otherwise it insert + * at caret position. + * Tries to do it in a cross browser compatible way. + * See http://stackoverflow.com/a/6691294/337907, Credits: Tim Down + * @param {string} html The html string + */ + function pasteHtmlAtSelection( html ) { + var sel, range; + if ( window.getSelection ) { + // IE9 and non-IE + sel = window.getSelection(); + if ( sel.getRangeAt && sel.rangeCount ) { + range = sel.getRangeAt( 0 ); + range.deleteContents(); + + // Range.createContextualFragment() would be useful here but is + // only relatively recently standardized and is not supported in + // some browsers (IE9, for one) + var el = document.createElement( 'div' ); + el.innerHTML = html; + var frag = document.createDocumentFragment(), + node, lastNode; + while ( ( node = el.firstChild ) ) { + lastNode = frag.appendChild( node ); + } + range.insertNode( frag ); + + // Preserve the selection + if ( lastNode ) { + range = range.cloneRange(); + range.setStartAfter( lastNode ); + range.collapse( true ); + sel.removeAllRanges(); + sel.addRange( range ); + } + } + } else if ( document.selection && document.selection.type !== 'Control' ) { + // IE < 9 + document.selection.createRange().pasteHTML( html ); + } + } + + /** + * Create a wiki internal link with given link text and target + * @param {string} linkText The link Text + * @param {string} title The link target + */ + LinkCard.prototype.createInternalLink = function ( linkText, title ) { + var $link = $( '<a>' ) + .addClass( 'cx-link' ) + .text( linkText ) + .attr( { + href: title, + rel: 'mw:WikiLink' + } ); + pasteHtmlAtSelection( $link[ 0 ].outerHTML ); + }; + LinkCard.prototype.start = function ( link, language ) { var word, linkCard = this; @@ -211,7 +271,7 @@ } ); linkCard.$addLink.click( function () { restoreSelection( range ); - document.execCommand( 'CreateLink', false, page.title ); + linkCard.createInternalLink( word, page.title ); } ); if ( page.thumbnail ) { imgSrc = page.thumbnail.source; -- To view, visit https://gerrit.wikimedia.org/r/141384 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iab240509925fd278111fff492b414a65314afb8f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ContentTranslation Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits