From 2075d6d2dc9759a65b19e71f55e31f8086f22de9 Mon Sep 17 00:00:00 2001 From: Alexander Jones Date: Tue, 2 Dec 2014 08:23:18 -0600 Subject: [PATCH] Pre-fill fields in image dialog when image code is selected in editor This commit adds a function to parse image syntax selected in the editor and uses the parsed data to automatically fill the fields of the WikiEditor image dialog. Surrounding whitespace in the selected wikitext is now included in the result. This causes an issue with the ownline option in line 840, which appends an extra line regardless of the post text. Bug: T40829 Change-Id: Ib117a2e6350400f9298d4892a00981ac41f3dbbf --- modules/jquery.wikiEditor.dialogs.config.js | 96 ++++++++++++++++++++- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/modules/jquery.wikiEditor.dialogs.config.js b/modules/jquery.wikiEditor.dialogs.config.js index b2ea837a..68f43716 100644 --- a/modules/jquery.wikiEditor.dialogs.config.js +++ b/modules/jquery.wikiEditor.dialogs.config.js @@ -562,8 +562,8 @@ width: 590, buttons: { 'wikieditor-toolbar-tool-file-insert': function () { - var fileName, caption, fileFloat, fileFormat, fileSize, fileTitle, - options, fileUse, + var fileName, caption, fileFloat, fileFormat, fileSize, whitespace, + fileTitle, options, fileUse, hasPxRgx = /.+px$/, magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' ); fileName = $( '#wikieditor-toolbar-file-target' ).val(); @@ -571,6 +571,7 @@ fileFloat = $( '#wikieditor-toolbar-file-float' ).val(); fileFormat = $( '#wikieditor-toolbar-file-format' ).val(); fileSize = $( '#wikieditor-toolbar-file-size' ).val(); + whitespace = $( '#wikieditor-toolbar-file-dialog' ).data( 'whitespace' ); // Append px to end to size if not already contains it if ( fileSize !== '' && !hasPxRgx.test( fileSize ) ) { fileSize += 'px'; @@ -598,9 +599,9 @@ { type: 'replace', options: { - pre: '[[', + pre: whitespace[ 0 ] + '[[', peri: fileUse, - post: ']]', + post: ']]' + whitespace[ 1 ], ownline: true } }, @@ -639,7 +640,94 @@ } }, open: function () { + var context, selection, parseFileSyntax, + magicWordsI18N = mw.config.get( 'wgWikiEditorMagicWords' ), + fileData = { + pre: '', + post: '', + fileName: '', + caption: '', + fileSize: '', + fileFloat: 'default', + fileFormat: magicWordsI18N.img_thumbnail + }; + + parseFileSyntax = function ( wikitext ) { + var escapedPipe = '\u0001', + result = {}, + match, params, file, i, param; + if ( wikitext.indexOf( escapedPipe ) !== -1 ) { + return false; + } + match = /^(\s*)\[\[(.*)\]\](\s*)$/.exec( wikitext ); + if ( !match ) { + return false; + } + result.pre = match[ 1 ]; + result.post = match[ 3 ]; + // Escape pipes inside links and templates, + // then split the parameters at the remaining pipes + params = match[ 2 ].replace( /\[\[[^[\]]*\]\]|\{\{[^{}]\}\}/g, function ( link ) { + return link.replace( /\|/g, escapedPipe ); + } ).split( '|' ); + file = new mw.Title( params[ 0 ] ); + if ( file.getNamespaceId() !== 6 ) { + return false; + } + result.fileName = file.getMainText(); + for ( i = 1; i < params.length; i++ ) { + param = params[ i ].toLowerCase(); + if ( param === 'right' || param === magicWordsI18N.img_right ) { + result.fileFloat = magicWordsI18N.img_right; + } else if ( param === 'left' || param === magicWordsI18N.img_left ) { + result.fileFloat = magicWordsI18N.img_left; + } else if ( param === 'none' || param === magicWordsI18N.img_none ) { + result.fileFloat = magicWordsI18N.img_none; + } else if ( param === 'center' || param === 'centre' || param === magicWordsI18N.img_center ) { + result.fileFloat = magicWordsI18N.img_center; + } else if ( param === 'thumbnail' || param === 'thumb' || param === magicWordsI18N.img_thumbnail ) { + result.fileFormat = magicWordsI18N.img_thumbnail; + } else if ( param === 'framed' || param === 'enframed' || param === 'frame' || param === magicWordsI18N.img_framed ) { + result.fileFormat = magicWordsI18N.img_framed; + } else if ( param === 'frameless' || param === magicWordsI18N.img_frameless ) { + result.fileFormat = magicWordsI18N.img_frameless; + } else if ( /.+px$/.test( param ) ) { + result.fileSize = param.replace( /px$/, '' ); + } else if ( param === '' ) { + continue; + } else if ( i === params.length - 1 ) { // Last param -> caption + result.caption = param.replace( new RegExp( mw.RegExp.escape( escapedPipe ), 'g' ), '|' ); + } else { // Unknown param + return false; + } + } + if ( !result.fileFormat ) { + result.fileFormat = 'default'; + } + return result; + }; + + // Retrieve the current selection + context = $( this ).data( 'context' ); + selection = context.$textarea.textSelection( 'getSelection' ); + + // Pre-fill the text fields based on the current selection + if ( selection !== '' ) { + fileData = $.extend( fileData, parseFileSyntax( selection ) ); + } + + // Initialize the form fields + $( '#wikieditor-toolbar-file-dialog' ) + .data( 'whitespace', [ fileData.pre, fileData.post ] ); + $( '#wikieditor-toolbar-file-target' ).val( fileData.fileName ); + $( '#wikieditor-toolbar-file-caption' ).val( fileData.caption ); + $( '#wikieditor-toolbar-file-float' ).val( fileData.fileFloat ); + $( '#wikieditor-toolbar-file-format' ).val( fileData.fileFormat ); + $( '#wikieditor-toolbar-file-size' ).val( fileData.fileSize ); + + // Set focus $( '#wikieditor-toolbar-file-target' ).trigger( 'focus' ); + if ( !( $( this ).data( 'dialogkeypressset' ) ) ) { $( this ).data( 'dialogkeypressset', true ); // Execute the action associated with the first button