LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-autocomplete-core.js”的版本间差异
跳转到导航
跳转到搜索
小 |
小 |
||
(未显示同一用户的17个中间版本) | |||
第1行: | 第1行: | ||
//<nowiki> |
|||
$(function() { |
$(function() { |
||
⚫ | |||
if(!['edit','submit'].includes( mw.config.get('wgAction') ) || |
|||
mw.config.get('wgPageContentModel') != 'wikitext') { |
|||
⚫ | |||
⚫ | |||
mw.util.addCSS('.wikiEditor-ui-text { position:relative; } #autocompleteHints { display:inline-block; position:absolute; left:50%; top:0; cursor:move; padding:0.1em 0.3em; } #autocompleteHints ul { list-style:none; margin:0; } #autocompleteHints li { cursor:pointer; color:#0645ad; } #autocompleteHints li:hover { text-decoration:underline; }'); |
|||
⚫ | |||
api = new mw.Api(), |
api = new mw.Api(), |
||
autocomplete = false; |
autocomplete = false; |
||
const regexp1 = /\[\[((?:(?!(\[|]|\|)).)*)$/, |
const regexp1 = /\[\[((?:(?!(\[|]|\|)).)*)$/, |
||
regexp2 = /{{((?:(?!({|}|\||:|#)).)*)$/, |
regexp2 = /{{((?:(?!({|}|\||:|#)).)*)$/, |
||
regexp3 = /{{#invoke:((?:(?!({|}|\|)).)*)$/, |
regexp3 = /{{#invoke:((?:(?!({|}|\|)).)*)$/i, |
||
regexp4 = /{{#widget:((?:(?!({|}|\|)).)*)$/; |
regexp4 = /{{#widget:((?:(?!({|}|\|)).)*)$/i; |
||
var getPrefix = function() { |
var getPrefix = function() { |
||
var position = editbox.prop('selectionEnd'), |
var position = editbox.prop('selectionEnd'), |
||
left = editbox.val().substring(0, position), |
left = editbox.val().substring(0, position), |
||
result = null; |
result = null; |
||
if(result = left.match(regexp1)) { |
if(result = left.match(regexp1)) { |
||
if( /^(.+)#/.test(result[1]) ) { |
|||
var section = result[1].match(/^(.+)#(.*)/); |
|||
return ['sec', section[1], section[2]]; |
|||
⚫ | |||
if( /^(.+):/.test(result[1]) ) { return ['*', result[1]]; } |
|||
if( /^:/.test(result[1]) ) { return ['6|14', result[1]]; } |
|||
return ['0', result[1]]; |
|||
⚫ | |||
if(result = left.match(regexp2)) { return ['10', result[1]]; } |
if(result = left.match(regexp2)) { return ['10', result[1]]; } |
||
if(result = left.match(regexp3)) { return ['828', result[1]]; } |
if(result = left.match(regexp3)) { return ['828', result[1]]; } |
||
第23行: | 第27行: | ||
}; |
}; |
||
var showHints = function() { |
var showHints = function() { |
||
console.log('Autocompletion begins.'); |
//console.log('Autocompletion begins.'); |
||
autocomplete = true; |
autocomplete = true; |
||
if(prefix[1]) { updateHints(); } |
|||
⚫ | |||
editbox.on('input', function() { |
editbox.on('input', function() { |
||
prefix = getPrefix(); |
|||
if(!prefix) { |
if(!prefix) { |
||
hideHints(); |
hideHints(); |
||
第34行: | 第37行: | ||
else { |
else { |
||
setTimeout(function() { |
setTimeout(function() { |
||
updateHints( |
updateHints(); |
||
}, 500); |
}, 500); |
||
} |
} |
||
第40行: | 第43行: | ||
}; |
}; |
||
var hideHints = function() { |
var hideHints = function() { |
||
console.log('Autocompletion ends.'); |
//console.log('Autocompletion ends.'); |
||
autocomplete = false; |
autocomplete = false; |
||
editbox.off('input'); |
editbox.off('input'); |
||
$('#autocompleteHints').remove(); |
$('#autocompleteHints').remove(); |
||
}; |
}; |
||
var updateHints = function( |
var updateHints = function() { |
||
var hints; |
var hints; |
||
if( $('#autocompleteHints').length ) { |
if( $('#autocompleteHints').length ) { |
||
hints = $('#autocompleteHints').eq(0); |
hints = $('#autocompleteHints').eq(0); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
hints.on('click', 'li', function(e) { |
hints.on('click', 'li', function(e) { |
||
var ele = $(e.target), |
var ele = $(e.target), |
||
第54行: | 第61行: | ||
left = editbox.val().substring(0, position), |
left = editbox.val().substring(0, position), |
||
right = editbox.val().substring(position); |
right = editbox.val().substring(position); |
||
switch(prefix[0]) { |
|||
case 'sec': |
|||
left = left.replace(regexp1,'[[') + prefix[1] + '#' + ele.text() + ']]'; |
|||
⚫ | |||
break; |
|||
case '*': |
|||
case '0': |
|||
left = left.replace(regexp1,'[[') + ele.text() + ']]'; |
|||
break; |
|||
case '6|14': |
|||
left = left.replace(regexp1,'[[:') + ele.text() + ']]'; |
|||
break; |
|||
case '10': |
|||
left = left.replace(regexp2,'{{') + ele.text().substring(9) + '}}'; |
|||
break; |
|||
case '828': |
|||
left = left.replace(regexp3,'{{#invoke') + ele.text().substring(2) + '}}'; |
|||
break; |
|||
case '274': |
|||
left = left.replace(regexp4,'{{#') + ele.text() + '}}'; |
|||
} |
} |
||
editbox.val(left + right); |
|||
editbox.prop('selectionStart', left.length); |
|||
editbox.prop('selectionEnd', left.length); |
|||
hideHints(); |
hideHints(); |
||
}); |
}); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
if(!prefix[1]) { |
if(!prefix[1]) { |
||
第77行: | 第91行: | ||
return; |
return; |
||
} |
} |
||
⚫ | |||
⚫ | |||
api.get({action:'parse', page:prefix[1], prop:'sections', formatversion:2}) |
|||
.then(function(data) { |
|||
var section = data.parse.sections.map((ele) => ele.anchor), |
|||
secHints = [], |
|||
regexp = new RegExp('^' + prefix[2]); |
|||
section.forEach(function(ele) { |
|||
if(regexp.test(ele)) { secHints.push(ele); } |
|||
}); |
|||
if(secHints.length === 0) { hints.html('<span class="error">' + wgUCS("该页面没有对应章节", "該頁面沒有對應章節") + '</span>'); } |
|||
else { hints.html('<ul><li>' + secHints.join('</li><li>') + '</li></ul>'); } |
|||
}, function() { |
|||
hints.html('<span class="error">' + wgUCS("没有对应页面", "沒有對應頁面") + '</span>'); |
|||
} |
|||
); |
|||
⚫ | |||
} |
|||
⚫ | |||
.then(function(data) { |
.then(function(data) { |
||
var titles = data.query.prefixsearch.map( (ele) => '<li>' + ele.title + '</li>' ); |
var titles = data.query.prefixsearch.map( (ele) => '<li>' + ele.title + '</li>' ); |
||
第89行: | 第120行: | ||
}; |
}; |
||
$('body').on('keydown', '#wpTextbox1, #Wikiplus-Quickedit', function(e) { |
|||
editbox = $(this); |
|||
if(e.keyCode == 9) { |
if(e.keyCode == 9) { |
||
prefix = getPrefix(); |
|||
e.preventDefault(); |
e.preventDefault(); |
||
if(prefix && !autocomplete) { |
if(prefix && !autocomplete) { |
||
第105行: | 第137行: | ||
}); |
}); |
||
}); |
}); |
||
//</nowiki> |
2022年1月1日 (六) 04:02的最新版本
//<nowiki> $(function() { var editbox, prefix, api = new mw.Api(), autocomplete = false; const regexp1 = /\[\[((?:(?!(\[|]|\|)).)*)$/, regexp2 = /{{((?:(?!({|}|\||:|#)).)*)$/, regexp3 = /{{#invoke:((?:(?!({|}|\|)).)*)$/i, regexp4 = /{{#widget:((?:(?!({|}|\|)).)*)$/i; var getPrefix = function() { var position = editbox.prop('selectionEnd'), left = editbox.val().substring(0, position), result = null; if(result = left.match(regexp1)) { if( /^(.+)#/.test(result[1]) ) { var section = result[1].match(/^(.+)#(.*)/); return ['sec', section[1], section[2]]; } if( /^(.+):/.test(result[1]) ) { return ['*', result[1]]; } if( /^:/.test(result[1]) ) { return ['6|14', result[1]]; } return ['0', result[1]]; } if(result = left.match(regexp2)) { return ['10', result[1]]; } if(result = left.match(regexp3)) { return ['828', result[1]]; } if(result = left.match(regexp4)) { return ['274', result[1]]; } return null; }; var showHints = function() { //console.log('Autocompletion begins.'); autocomplete = true; if(prefix[1]) { updateHints(); } editbox.on('input', function() { prefix = getPrefix(); if(!prefix) { hideHints(); } else { setTimeout(function() { updateHints(); }, 500); } }); }; var hideHints = function() { //console.log('Autocompletion ends.'); autocomplete = false; editbox.off('input'); $('#autocompleteHints').remove(); }; var updateHints = function() { var hints; if( $('#autocompleteHints').length ) { hints = $('#autocompleteHints').eq(0); } else { hints = $('<div id="autocompleteHints" class="ui-widget-content">').insertAfter(editbox); hints.draggable(); hints.on('click', 'li', function(e) { var ele = $(e.target), position = editbox.prop('selectionEnd'), left = editbox.val().substring(0, position), right = editbox.val().substring(position); switch(prefix[0]) { case 'sec': left = left.replace(regexp1,'[[') + prefix[1] + '#' + ele.text() + ']]'; break; case '*': case '0': left = left.replace(regexp1,'[[') + ele.text() + ']]'; break; case '6|14': left = left.replace(regexp1,'[[:') + ele.text() + ']]'; break; case '10': left = left.replace(regexp2,'{{') + ele.text().substring(9) + '}}'; break; case '828': left = left.replace(regexp3,'{{#invoke') + ele.text().substring(2) + '}}'; break; case '274': left = left.replace(regexp4,'{{#') + ele.text() + '}}'; } editbox.val(left + right); editbox.prop('selectionStart', left.length); editbox.prop('selectionEnd', left.length); hideHints(); }); } if(!prefix[1]) { hints.html('<span class="error">' + wgUCS("请输入页面部分标题", "請輸入頁面部分標題") + '</span>'); return; } if(prefix[0] == 'sec') { api.get({action:'parse', page:prefix[1], prop:'sections', formatversion:2}) .then(function(data) { var section = data.parse.sections.map((ele) => ele.anchor), secHints = [], regexp = new RegExp('^' + prefix[2]); section.forEach(function(ele) { if(regexp.test(ele)) { secHints.push(ele); } }); if(secHints.length === 0) { hints.html('<span class="error">' + wgUCS("该页面没有对应章节", "該頁面沒有對應章節") + '</span>'); } else { hints.html('<ul><li>' + secHints.join('</li><li>') + '</li></ul>'); } }, function() { hints.html('<span class="error">' + wgUCS("没有对应页面", "沒有對應頁面") + '</span>'); } ); return; } api.get({action:'query', list:'prefixsearch', pssearch:prefix[1].replace(/^:/, ''), psnamespace:prefix[0], formatversion:2}) .then(function(data) { var titles = data.query.prefixsearch.map( (ele) => '<li>' + ele.title + '</li>' ); if(titles.length) { hints.html('<ul>' + titles.join('') + '</ul>'); } else { hints.html('<span class="error">' + wgUCS("没有对应页面", "沒有對應頁面") + '</span>'); } }); }; $('body').on('keydown', '#wpTextbox1, #Wikiplus-Quickedit', function(e) { editbox = $(this); if(e.keyCode == 9) { prefix = getPrefix(); e.preventDefault(); if(prefix && !autocomplete) { showHints(); } } if(e.keyCode == 27) { e.preventDefault(); if(autocomplete) { hideHints(); } } }); }); //</nowiki>