MediaWiki:Gadget-SelectionCounter.js:修订间差异

删除的内容 添加的内容
Maintenance script​(留言 | 贡献)
增加轮询兜底以兼容未触发 selectionchange 的情况
Maintenance script​(留言 | 贡献)
小 优化 SelectionCounter:事件驱动计数与低开销显示
 
(未显示同一用户的6个中间版本)
第1行:
/* MediaWiki Gadget: SelectionCounter */
/* ================================================================
( function () {
MediaWiki Gadget: SelectionCounter
'use strict';
功能:
- 选中文本时显示字数统计
- 与 RightToolbar 风格一致
================================================================= */
 
if ( window.SelectionCounterLoaded ) {
mw.loader.using(['mediawiki.util']).then(function () {
return;
if (window.SelectionCounterLoaded) {
}
return;
window.SelectionCounterLoaded = true;
}
window.SelectionCounterLoaded = true;
 
mw.loader.using( [ 'jquery', 'mediawiki.util' $] ).then( function () {
var $counter = $('#mw-selection-counter');
var rafId = 0;
var hideTimer = 0;
var lastRendered = '';
var lastShownAt = 0;
var selecting = false;
var canSegment = typeof Intl !== 'undefined' && typeof Intl.Segmenter === 'function';
var segmenter = canSegment ? new Intl.Segmenter( mw.config.get( 'wgUserLanguage' ) || 'zh-cn', {
granularity: 'grapheme'
} ) : null;
 
function getMessage( count ) {
if (!$counter.length) {
var lang = ( mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn' ).toLowerCase();
$counter = $('<div id="mw-selection-counter" class="mw-selection-counter" role="status" aria-live="polite"></div>')
return lang.indexOf( 'en' ) === 0 ? 'Selected ' + count + ' chars' : '已选 ' + count + ' 字';
.hide()
}
.appendTo('body');
}
 
function getSelectionText() {
var rafId = null;
var selection = window.getSelection && window.getSelection();
var pollTimer = null;
if ( !selection || selection.rangeCount === 0 || selection.isCollapsed ) {
var lastText = null;
return '';
}
return selection.toString();
}
 
function countText( text ) {
function getSelectionText() {
var compact = text.replace( /\s+/g, '' );
var selection = window.getSelection();
if ( !compact ) {
if (!selection || selection.isCollapsed) {
return ''0;
}
}
if ( segmenter ) {
return String(selection).trim();
return Array.from( segmenter.segment( compact ) ).length;
}
}
return Array.from( compact ).length;
}
 
function updateCounterhideCounter() {
lastRendered = '';
rafId = null;
$counter.attr( 'hidden', 'hidden' ).text( '' );
var active = document.activeElement;
}
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
if (lastText !== null) {
lastText = null;
$counter.hide();
}
return;
}
 
function renderCounter( text ) {
var text = getSelectionText();
var count = countText( text );
if (!text) {
var rendered;
if (lastText !== null) {
lastText = null;
$counter.hide();
}
return;
}
 
if ( count < 1 ) {
if (text === lastText) {
hideCounter();
return;
return;
}
}
 
rendered = getMessage( count );
lastText = text;
if ( rendered !== lastRendered ) {
var count = Array.from(text).length;
$counter.text('已选 'rendered + count + ' 字').show();
lastRendered = rendered;
}
}
lastShownAt = Date.now();
$counter.removeAttr( 'hidden' );
}
 
function scheduleUpdateupdateCounter() {
var text;
if (rafId) {
return;
}
rafId = window.requestAnimationFrame(updateCounter);
}
 
rafId = 0;
function startPolling() {
text = getSelectionText();
if (pollTimer) {
return;
}
pollTimer = window.setInterval(updateCounter, 500);
}
 
if ( text ) {
function stopPolling() {
renderCounter( text );
if (!pollTimer) {
return;
}
}
window.clearInterval(pollTimer);
pollTimer = null;
}
 
if ( !selecting && Date.now() - lastShownAt > 800 ) {
document.addEventListener('selectionchange', scheduleUpdate);
hideCounter();
document.addEventListener('mouseup', scheduleUpdate);
}
document.addEventListener('keyup', scheduleUpdate);
}
document.addEventListener('touchend', scheduleUpdate, { passive: true });
window.addEventListener('focus', startPolling);
window.addEventListener('blur', stopPolling);
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
stopPolling();
return;
}
startPolling();
});
 
function scheduleUpdate() {
startPolling();
if ( rafId }); {
return;
});
}
rafId = window.requestAnimationFrame( updateCounter );
}
 
function scheduleHide() {
window.clearTimeout( hideTimer );
hideTimer = window.setTimeout( scheduleUpdate, 900 );
}
 
$( function () {
$counter = $( '#mw-selection-counter' );
if ( !$counter.length ) {
$counter = $( '<div>' )
.attr( {
id: 'mw-selection-counter',
hidden: 'hidden',
role: 'status',
'aria-live': 'polite',
'aria-atomic': 'true'
} )
.addClass( 'mw-selection-counter' )
.appendTo( document.body );
}
 
document.addEventListener( 'selectionchange', scheduleUpdate, { passive: true } );
document.addEventListener( 'pointerdown', function () {
selecting = true;
scheduleUpdate();
}, true );
document.addEventListener( 'pointermove', function () {
if ( selecting ) {
scheduleUpdate();
}
}, { passive: true, capture: true } );
document.addEventListener( 'pointerup', function () {
selecting = false;
scheduleUpdate();
scheduleHide();
}, true );
document.addEventListener( 'keyup', function ( event ) {
if ( event.key === 'Escape' ) {
hideCounter();
return;
}
scheduleUpdate();
} );
window.addEventListener( 'blur', hideCounter );
} );
} );
}() );