MediaWiki:Gadget-SelectionCounter.js:修订间差异
外观
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 维护 SelectionCounter:显式依赖和本地化文本 |
Maintenance script(留言 | 贡献) 小 优化 SelectionCounter:事件驱动计数与低开销显示 |
||
| 第2行: | 第2行: | ||
( function () { |
( function () { |
||
'use strict'; |
'use strict'; |
||
if ( window.SelectionCounterLoaded ) { |
|||
return; |
|||
} |
|||
window.SelectionCounterLoaded = true; |
|||
mw.loader.using( [ 'jquery', 'mediawiki.util' ] ).then( function () { |
mw.loader.using( [ 'jquery', 'mediawiki.util' ] ).then( function () { |
||
var $counter; |
var $counter; |
||
var rafId = |
var rafId = 0; |
||
var |
var hideTimer = 0; |
||
var |
var lastRendered = ''; |
||
var |
var lastShownAt = 0; |
||
var |
var selecting = false; |
||
var canSegment = typeof Intl !== 'undefined' && typeof Intl.Segmenter === 'function'; |
|||
var segmenter = canSegment ? new Intl.Segmenter( mw.config.get( 'wgUserLanguage' ) || 'zh-cn', { |
|||
if ( window.SelectionCounterLoaded ) { |
|||
granularity: 'grapheme' |
|||
return; |
|||
} |
} ) : null; |
||
window.SelectionCounterLoaded = true; |
|||
function |
function getMessage( count ) { |
||
var lang = mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn'; |
var lang = ( mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn' ).toLowerCase(); |
||
return lang |
return lang.indexOf( 'en' ) === 0 ? 'Selected ' + count + ' chars' : '已选 ' + count + ' 字'; |
||
} |
} |
||
function getSelectionText() { |
function getSelectionText() { |
||
var selection = window.getSelection(); |
var selection = window.getSelection && window.getSelection(); |
||
if ( !selection || selection.rangeCount === 0 || selection.isCollapsed ) { |
if ( !selection || selection.rangeCount === 0 || selection.isCollapsed ) { |
||
return ''; |
return ''; |
||
| 第29行: | 第33行: | ||
} |
} |
||
function |
function countText( text ) { |
||
var compact = text.replace( /\s+/g, '' ); |
var compact = text.replace( /\s+/g, '' ); |
||
var count; |
|||
if ( !compact ) { |
if ( !compact ) { |
||
return |
return 0; |
||
} |
} |
||
if ( segmenter ) { |
|||
count = Array.from( compact ).length; |
|||
return Array.from( segmenter.segment( compact ) ).length; |
|||
} |
|||
return true; |
|||
return Array.from( compact ).length; |
|||
} |
|||
function hideCounter() { |
|||
lastRendered = ''; |
|||
$counter.attr( 'hidden', 'hidden' ).text( '' ); |
|||
} |
|||
function renderCounter( text ) { |
|||
var count = countText( text ); |
|||
var rendered; |
|||
if ( count < 1 ) { |
|||
hideCounter(); |
|||
return; |
|||
} |
|||
rendered = getMessage( count ); |
|||
if ( rendered !== lastRendered ) { |
|||
$counter.text( rendered ); |
|||
lastRendered = rendered; |
|||
} |
|||
lastShownAt = Date.now(); |
|||
$counter.removeAttr( 'hidden' ); |
|||
} |
} |
||
function updateCounter() { |
function updateCounter() { |
||
var text; |
var text; |
||
rafId = null; |
|||
rafId = 0; |
|||
text = getSelectionText(); |
text = getSelectionText(); |
||
if ( text ) { |
if ( text ) { |
||
renderCounter( text ); |
|||
lastTime = Date.now(); |
|||
showCount( text ); |
|||
return; |
return; |
||
} |
} |
||
if ( |
if ( !selecting && Date.now() - lastShownAt > 800 ) { |
||
hideCounter(); |
|||
return; |
|||
} |
} |
||
$counter.hide(); |
|||
} |
} |
||
function scheduleUpdate() { |
function scheduleUpdate() { |
||
if ( |
if ( rafId ) { |
||
return; |
|||
rafId = window.requestAnimationFrame( updateCounter ); |
|||
} |
} |
||
rafId = window.requestAnimationFrame( updateCounter ); |
|||
} |
} |
||
function |
function scheduleHide() { |
||
window. |
window.clearTimeout( hideTimer ); |
||
hideTimer = window.setTimeout( scheduleUpdate, 900 ); |
|||
} |
|||
function startPolling() { |
|||
if ( !pollTimer ) { |
|||
pollTimer = window.setInterval( updateCounter, 500 ); |
|||
} |
|||
} |
|||
function stopPolling() { |
|||
if ( pollTimer ) { |
|||
window.clearInterval( pollTimer ); |
|||
pollTimer = null; |
|||
} |
|||
} |
|||
function onSelectMove() { |
|||
if ( isSelecting ) { |
|||
scheduleUpdate(); |
|||
} |
|||
} |
} |
||
| 第92行: | 第98行: | ||
$counter = $( '#mw-selection-counter' ); |
$counter = $( '#mw-selection-counter' ); |
||
if ( !$counter.length ) { |
if ( !$counter.length ) { |
||
$counter = $( '< |
$counter = $( '<div>' ) |
||
. |
.attr( { |
||
id: 'mw-selection-counter', |
|||
.appendTo( 'body' ); |
|||
hidden: 'hidden', |
|||
role: 'status', |
|||
'aria-live': 'polite', |
|||
'aria-atomic': 'true' |
|||
} ) |
|||
.addClass( 'mw-selection-counter' ) |
|||
.appendTo( document.body ); |
|||
} |
} |
||
document.addEventListener( 'selectionchange', |
document.addEventListener( 'selectionchange', scheduleUpdate, { passive: true } ); |
||
document.addEventListener( 'pointerdown', function () { |
|||
var text = getSelectionText(); |
|||
selecting = true; |
|||
scheduleUpdate(); |
|||
}, true ); |
|||
document.addEventListener( 'pointermove', function () { |
|||
if ( selecting ) { |
|||
scheduleUpdate(); |
|||
} |
} |
||
}, { passive: true, capture: true } ); |
|||
document.addEventListener( 'pointerup', function () { |
|||
selecting = false; |
|||
scheduleUpdate(); |
scheduleUpdate(); |
||
scheduleHide(); |
|||
document.addEventListener( 'mousedown', function () { |
|||
isSelecting = true; |
|||
}, true ); |
}, true ); |
||
document.addEventListener( ' |
document.addEventListener( 'keyup', function ( event ) { |
||
if ( event.key === 'Escape' ) { |
|||
isSelecting = false; |
|||
hideCounter(); |
|||
}, true ); |
|||
document.addEventListener( 'mousemove', onSelectMove, true ); |
|||
document.addEventListener( 'touchstart', function () { |
|||
isSelecting = true; |
|||
}, { passive: true, capture: true } ); |
|||
document.addEventListener( 'touchmove', onSelectMove, { passive: true, capture: true } ); |
|||
document.addEventListener( 'touchend', function () { |
|||
isSelecting = false; |
|||
scheduleUpdateDelayed(); |
|||
}, { passive: true, capture: true } ); |
|||
document.addEventListener( 'keyup', scheduleUpdate ); |
|||
window.addEventListener( 'focus', startPolling ); |
|||
window.addEventListener( 'blur', stopPolling ); |
|||
document.addEventListener( 'visibilitychange', function () { |
|||
if ( document.hidden ) { |
|||
stopPolling(); |
|||
return; |
return; |
||
} |
} |
||
scheduleUpdate(); |
|||
} ); |
} ); |
||
window.addEventListener( 'blur', hideCounter ); |
|||
startPolling(); |
|||
} ); |
} ); |
||
} ); |
} ); |
||
2026年7月19日 (日) 20:11的最新版本
/* MediaWiki Gadget: SelectionCounter */
( function () {
'use strict';
if ( window.SelectionCounterLoaded ) {
return;
}
window.SelectionCounterLoaded = true;
mw.loader.using( [ 'jquery', 'mediawiki.util' ] ).then( function () {
var $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 ) {
var lang = ( mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn' ).toLowerCase();
return lang.indexOf( 'en' ) === 0 ? 'Selected ' + count + ' chars' : '已选 ' + count + ' 字';
}
function getSelectionText() {
var selection = window.getSelection && window.getSelection();
if ( !selection || selection.rangeCount === 0 || selection.isCollapsed ) {
return '';
}
return selection.toString();
}
function countText( text ) {
var compact = text.replace( /\s+/g, '' );
if ( !compact ) {
return 0;
}
if ( segmenter ) {
return Array.from( segmenter.segment( compact ) ).length;
}
return Array.from( compact ).length;
}
function hideCounter() {
lastRendered = '';
$counter.attr( 'hidden', 'hidden' ).text( '' );
}
function renderCounter( text ) {
var count = countText( text );
var rendered;
if ( count < 1 ) {
hideCounter();
return;
}
rendered = getMessage( count );
if ( rendered !== lastRendered ) {
$counter.text( rendered );
lastRendered = rendered;
}
lastShownAt = Date.now();
$counter.removeAttr( 'hidden' );
}
function updateCounter() {
var text;
rafId = 0;
text = getSelectionText();
if ( text ) {
renderCounter( text );
return;
}
if ( !selecting && Date.now() - lastShownAt > 800 ) {
hideCounter();
}
}
function scheduleUpdate() {
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 );
} );
} );
}() );