MediaWiki:Gadget-wikificator.js: различия между версиями
Перейти к навигации
Перейти к поиску
(не показано 15 промежуточных версий этого же участника) | |||
Строка 1: | Строка 1: | ||
− | + | // <nowiki> | |
− | + | mw.loader.using( 'jquery.client', function () { | |
− | |||
− | |||
+ | var clientProfile = $.client.profile(); | ||
+ | var hotkey = clientProfile.platform === 'mac' ? 'Ctrl+Shift+W' : 'Ctrl+Alt+W'; | ||
+ | var strings = { | ||
+ | name: 'Викификатор', | ||
+ | tooltip: 'Викификатор — автоматический обработчик текста (' + hotkey + ')', | ||
+ | fullText: 'Викификатор обработает ВЕСЬ текст на этой странице. Продолжить?', | ||
+ | talkPage: 'Викификатор не обрабатывает страницы обсуждения целиком.\n\nВыделите ваше сообщение — обработано будет только оно.' | ||
+ | }; | ||
− | window.Wikify = function ( | + | // Function takes an input or text as an argument. If it is absent, it uses $( '#wpTextbox1' ) |
+ | // as an input. | ||
+ | window.Wikify = function ( inputOrText ) { | ||
'use strict'; | 'use strict'; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
// FUNCTIONS | // FUNCTIONS | ||
Строка 22: | Строка 21: | ||
txt = txt.replace( r1, r2 ); | txt = txt.replace( r1, r2 ); | ||
} | } | ||
− | + | ||
function processText() { | function processText() { | ||
− | var i | + | var i; |
− | + | ||
− | + | r( / +(\n|\r)/g, '$1' ); // spaces at EOL | |
− | + | txt = '\n' + txt + '\n'; | |
− | + | ||
− | + | r( /«|»|“|”|„/g, '"' ); // temp | |
− | |||
− | |||
− | |||
− | // Hyphens to | + | // Hyphens and en dashes to pretty dashes |
+ | r( /–/g, '-' ); // – -> hyphen | ||
r( /(\s)-{1,3} /g, '$1— ' ); // hyphen -> — | r( /(\s)-{1,3} /g, '$1— ' ); // hyphen -> — | ||
− | |||
r( /(\d)--(\d)/g, '$1—$2' ); // -> — | r( /(\d)--(\d)/g, '$1—$2' ); // -> — | ||
− | |||
− | |||
− | |||
// "" → «» | // "" → «» | ||
Строка 53: | Строка 46: | ||
r( 'ёёё' , '<hr class=newtheme>\n' ); | r( 'ёёё' , '<hr class=newtheme>\n' ); | ||
+ | txt = txt.substr( 1, txt.length - 2 ); // compensation for "txt = '\n' + txt + '\n';" | ||
} | } | ||
function processAllText() { | function processAllText() { | ||
− | txt = input. | + | txt = $input ? $input.textSelection( 'getContents' ) : text; |
processText(); | processText(); | ||
− | r( /^[\n\r]+/, '' ); | + | if ( $input ) { |
− | + | r( /^[\n\r]+/, '' ); | |
− | txt | + | |
+ | $input.textSelection( 'setContents', txt ); | ||
+ | if ( caretPosition ) { | ||
+ | $input.textSelection( 'setSelection', { | ||
+ | start: caretPosition[0] > txt.length ? txt.length : caretPosition[0] | ||
+ | } ); | ||
+ | } | ||
+ | } else { | ||
+ | text = txt; | ||
+ | } | ||
} | } | ||
− | + | // MAIN CODE | |
− | // | + | |
− | + | // Check what's in the first parameter | |
− | + | var text; | |
− | } | + | var isInput; |
− | + | var $input; | |
− | + | if ( typeof inputOrText === 'string' ) { | |
− | + | text = inputOrText; | |
+ | } else { | ||
+ | isInput = ( | ||
+ | inputOrText && | ||
+ | ( | ||
+ | ( inputOrText.nodeType && inputOrText.value !== undefined ) || // node | ||
+ | ( inputOrText.prop && inputOrText.prop( 'nodeType' ) ) // jQuery object | ||
+ | ) | ||
+ | ); | ||
+ | $input = $( isInput ? inputOrText : '#wpTextbox1' ); | ||
} | } | ||
− | + | var txt = ''; | |
− | + | var hidden = []; | |
− | + | var winScroll = document.documentElement.scrollTop; | |
− | if ( | + | var caretPosition; |
− | + | if ( $input ) { | |
− | + | $input.focus(); | |
− | + | ||
− | + | caretPosition = $input.textSelection( 'getCaretPosition', { startAndEnd: true } ); | |
− | + | if ( caretPosition ) { | |
− | + | var $CodeMirrorVscrollbar = $( '.CodeMirror-vscrollbar' ); | |
− | + | var textScroll = ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input ) | |
− | + | .scrollTop(); | |
− | + | if ( caretPosition[0] === caretPosition[1] ) { | |
− | + | processAllText(); | |
− | + | } else { | |
− | + | txt = $input.textSelection( 'getSelection' ); | |
− | + | processText(); | |
− | + | // replaceSelection doesn't work with MediaWiki 1.30 in case this gadget is loaded | |
− | + | // from other wiki | |
− | + | $input.textSelection( 'encapsulateSelection', { | |
− | + | replace: true, | |
− | + | peri: txt | |
− | + | } ); | |
− | if ( | + | // In CodeMirror, the selection isn't preserved, so we do it explicitly |
+ | $input.textSelection( 'setSelection', { | ||
+ | start: caretPosition[0], | ||
+ | end: caretPosition[0] + txt.length | ||
+ | } ); | ||
+ | } | ||
+ | ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input ) | ||
+ | .scrollTop( textScroll ); | ||
+ | // If something went wrong | ||
+ | } else if ( confirm( strings.fullText ) ) { | ||
processAllText(); | processAllText(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | + | } else { | |
− | |||
− | } else | ||
processAllText(); | processAllText(); | ||
+ | return text; | ||
} | } | ||
− | document.documentElement.scrollTop = winScroll; | + | // scroll back, for 2017 wikitext editor, IE, Opera |
+ | document.documentElement.scrollTop = winScroll; | ||
}; | }; | ||
− | + | function registerWikificatorTool() { | |
− | + | registerTool( { | |
− | + | name: 'wikificator', | |
− | + | position: 100, | |
− | + | title: strings.name, | |
− | + | label: strings.tooltip, | |
− | + | callback: Wikify, | |
− | + | classic: { | |
− | + | icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Wikify-toolbutton.png', | |
− | + | }, | |
− | + | visual: { | |
− | + | icon: '//upload.wikimedia.org/wikipedia/commons/thumb/4/41/Wikificator_VE_icon.svg/20px-Wikificator_VE_icon.svg.png', | |
− | + | modes: [ 'source' ], | |
− | + | addRightAway: true, | |
− | + | }, | |
− | + | } ); | |
− | + | } | |
− | + | ||
− | + | // загрузка викификатора | |
− | + | $.when( | |
− | + | mw.loader.using( [ 'mediawiki.util', 'user.options' ] ), | |
− | + | $.getScript( 'https://ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' ) | |
− | + | ).done( registerWikificatorTool ); | |
− | // | + | |
− | + | }() ); | |
− | + | // </nowiki> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
− | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | }; |
Текущая версия на 00:06, 7 января 2023
// <nowiki> mw.loader.using( 'jquery.client', function () { var clientProfile = $.client.profile(); var hotkey = clientProfile.platform === 'mac' ? 'Ctrl+Shift+W' : 'Ctrl+Alt+W'; var strings = { name: 'Викификатор', tooltip: 'Викификатор — автоматический обработчик текста (' + hotkey + ')', fullText: 'Викификатор обработает ВЕСЬ текст на этой странице. Продолжить?', talkPage: 'Викификатор не обрабатывает страницы обсуждения целиком.\n\nВыделите ваше сообщение — обработано будет только оно.' }; // Function takes an input or text as an argument. If it is absent, it uses $( '#wpTextbox1' ) // as an input. window.Wikify = function ( inputOrText ) { 'use strict'; // FUNCTIONS function r( r1, r2 ) { txt = txt.replace( r1, r2 ); } function processText() { var i; r( / +(\n|\r)/g, '$1' ); // spaces at EOL txt = '\n' + txt + '\n'; r( /«|»|“|”|„/g, '"' ); // temp // Hyphens and en dashes to pretty dashes r( /–/g, '-' ); // – -> hyphen r( /(\s)-{1,3} /g, '$1— ' ); // hyphen -> — r( /(\d)--(\d)/g, '$1—$2' ); // -> — // "" → «» for ( i = 1; i <= 2; i++ ) { r( /([\s\x02!|#'"\/([{;+\-])"([^"]*)([^\s"([{|])"([^a-zа-яё])/ig, '$1«$2$3»$4' ); // " } while ( /«[^»]*«/.test( txt ) ) { r( /«([^»]*)«([^»]*)»/g, '«$1„$2“' ); } r( 'ёё' , '</div>\n<hr class=newtheme>\n<div class=newtheme>' ); r( 'ёёё' , '<hr class=newtheme>\n' ); txt = txt.substr( 1, txt.length - 2 ); // compensation for "txt = '\n' + txt + '\n';" } function processAllText() { txt = $input ? $input.textSelection( 'getContents' ) : text; processText(); if ( $input ) { r( /^[\n\r]+/, '' ); $input.textSelection( 'setContents', txt ); if ( caretPosition ) { $input.textSelection( 'setSelection', { start: caretPosition[0] > txt.length ? txt.length : caretPosition[0] } ); } } else { text = txt; } } // MAIN CODE // Check what's in the first parameter var text; var isInput; var $input; if ( typeof inputOrText === 'string' ) { text = inputOrText; } else { isInput = ( inputOrText && ( ( inputOrText.nodeType && inputOrText.value !== undefined ) || // node ( inputOrText.prop && inputOrText.prop( 'nodeType' ) ) // jQuery object ) ); $input = $( isInput ? inputOrText : '#wpTextbox1' ); } var txt = ''; var hidden = []; var winScroll = document.documentElement.scrollTop; var caretPosition; if ( $input ) { $input.focus(); caretPosition = $input.textSelection( 'getCaretPosition', { startAndEnd: true } ); if ( caretPosition ) { var $CodeMirrorVscrollbar = $( '.CodeMirror-vscrollbar' ); var textScroll = ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input ) .scrollTop(); if ( caretPosition[0] === caretPosition[1] ) { processAllText(); } else { txt = $input.textSelection( 'getSelection' ); processText(); // replaceSelection doesn't work with MediaWiki 1.30 in case this gadget is loaded // from other wiki $input.textSelection( 'encapsulateSelection', { replace: true, peri: txt } ); // In CodeMirror, the selection isn't preserved, so we do it explicitly $input.textSelection( 'setSelection', { start: caretPosition[0], end: caretPosition[0] + txt.length } ); } ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input ) .scrollTop( textScroll ); // If something went wrong } else if ( confirm( strings.fullText ) ) { processAllText(); } } else { processAllText(); return text; } // scroll back, for 2017 wikitext editor, IE, Opera document.documentElement.scrollTop = winScroll; }; function registerWikificatorTool() { registerTool( { name: 'wikificator', position: 100, title: strings.name, label: strings.tooltip, callback: Wikify, classic: { icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Wikify-toolbutton.png', }, visual: { icon: '//upload.wikimedia.org/wikipedia/commons/thumb/4/41/Wikificator_VE_icon.svg/20px-Wikificator_VE_icon.svg.png', modes: [ 'source' ], addRightAway: true, }, } ); } // загрузка викификатора $.when( mw.loader.using( [ 'mediawiki.util', 'user.options' ] ), $.getScript( 'https://ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' ) ).done( registerWikificatorTool ); }() ); // </nowiki>