跳转到内容

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

来自陋室
删除的内容 添加的内容
Maintenance script​(留言 | 贡献)
为未加载 CSS 提供可见性兜底
Maintenance script​(留言 | 贡献)
维护 SelectionCounter:显式依赖和本地化文本
第1行: 第1行:
/* MediaWiki Gadget: SelectionCounter */
/* ================================================================
( function () {
MediaWiki Gadget: SelectionCounter
'use strict';
功能:
- 选中文本时显示字数统计
- 与 RightToolbar 风格一致
================================================================= */


mw.loader.using(['mediawiki.util']).then(function () {
mw.loader.using( [ 'jquery', 'mediawiki.util' ] ).then( function () {
var $counter;
if (window.SelectionCounterLoaded) {
var rafId = null;
return;
var pollTimer = null;
}
var lastText = '';
window.SelectionCounterLoaded = true;
var lastTime = 0;
var isSelecting = false;


if ( window.SelectionCounterLoaded ) {
$(function () {
return;
var $counter = $('#mw-selection-counter');
}
window.SelectionCounterLoaded = true;


function label( count ) {
if (!$counter.length) {
var lang = mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn';
$counter = $('<div id="mw-selection-counter" class="mw-selection-counter" role="status" aria-live="polite"></div>')
return lang.toLowerCase().indexOf( 'en' ) === 0 ? 'Selected ' + count + ' chars' : '已选 ' + count + ' 字';
.hide()
}
.appendTo('body');
}


function ensureVisibleStyles() {
function getSelectionText() {
var selection = window.getSelection();
var el = $counter[0];
if ( !selection || selection.rangeCount === 0 || selection.isCollapsed ) {
if (!el) {
return '';
return;
}
}
return selection.toString();
var computed = window.getComputedStyle(el);
}
if (computed.position !== 'static') {
return;
}
$counter.css({
position: 'fixed',
bottom: '24px',
right: '24px',
padding: '6px 10px',
fontSize: '12px',
borderRadius: '4px',
background: 'var(--color-surface-0, rgba(255, 255, 255, 0.85))',
border: '1px solid var(--border-color-base, #aaa)',
color: 'var(--color-base, #333)',
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.2)',
backdropFilter: 'blur(4px)',
zIndex: 2147483647,
pointerEvents: 'none'
});
}


function showCount( text ) {
ensureVisibleStyles();
var compact = text.replace( /\s+/g, '' );
var count;
if ( !compact ) {
return false;
}
count = Array.from( compact ).length;
$counter.text( label( count ) ).show();
return true;
}


function updateCounter() {
var rafId = null;
var text;
var pollTimer = null;
rafId = null;
var lastText = '';
text = getSelectionText();
var lastTime = 0;
var isSelecting = false;


if ( text ) {
function getSelectionText() {
lastText = text;
var selection = window.getSelection();
lastTime = Date.now();
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
showCount( text );
return '';
return;
}
}
return selection.toString();
}


if ( lastText && Date.now() - lastTime < 1200 ) {
function compactText(text) {
showCount( lastText );
return text.replace(/\s+/g, '');
return;
}
}


$counter.hide();
function showCount(text) {
}
var compact = compactText(text);
if (!compact) {
return false;
}
var count = Array.from(compact).length;
$counter.text('已选 ' + count + ' 字').show();
return true;
}


function updateCounter() {
function scheduleUpdate() {
rafId = null;
if ( !rafId ) {
rafId = window.requestAnimationFrame( updateCounter );
var text = getSelectionText();
}
if (text) {
}
lastText = text;
lastTime = Date.now();
showCount(text);
return;
}


function scheduleUpdateDelayed() {
if (lastText && Date.now() - lastTime < 1200) {
window.setTimeout( scheduleUpdate, 0 );
showCount(lastText);
}
return;
}


function startPolling() {
$counter.hide();
if ( !pollTimer ) {
}
pollTimer = window.setInterval( updateCounter, 500 );
}
}


function scheduleUpdate() {
function stopPolling() {
if ( pollTimer ) {
if (rafId) {
window.clearInterval( pollTimer );
return;
pollTimer = null;
}
}
rafId = window.requestAnimationFrame(updateCounter);
}
}


function scheduleUpdateDelayed() {
function onSelectMove() {
if ( isSelecting ) {
window.setTimeout(scheduleUpdate, 0);
scheduleUpdate();
}
}
}


function startPolling() {
$( function () {
$counter = $( '#mw-selection-counter' );
if (pollTimer) {
if ( !$counter.length ) {
return;
$counter = $( '<div id="mw-selection-counter" class="mw-selection-counter" role="status" aria-live="polite"></div>' )
}
.hide()
pollTimer = window.setInterval(updateCounter, 500);
.appendTo( 'body' );
}
}


function stopPolling() {
document.addEventListener( 'selectionchange', function () {
var text = getSelectionText();
if (!pollTimer) {
if ( text ) {
return;
lastText = text;
}
lastTime = Date.now();
window.clearInterval(pollTimer);
}
pollTimer = null;
scheduleUpdate();
}
} );
document.addEventListener( 'mousedown', function () {
isSelecting = true;
}, true );
document.addEventListener( 'mouseup', function () {
isSelecting = false;
scheduleUpdateDelayed();
}, 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;
}
startPolling();
} );


startPolling();
function onSelectMove() {
} );
if (!isSelecting) {
} );
return;
}() );
}
scheduleUpdate();
}

document.addEventListener('selectionchange', function () {
var text = getSelectionText();
if (text) {
lastText = text;
lastTime = Date.now();
}
scheduleUpdate();
});
document.addEventListener('mousedown', function () {
isSelecting = true;
}, true);
document.addEventListener('mouseup', function () {
isSelecting = false;
scheduleUpdateDelayed();
}, 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;
}
startPolling();
});

startPolling();
});
});

2026年7月16日 (四) 16:59的版本

/* MediaWiki Gadget: SelectionCounter */
( function () {
	'use strict';

	mw.loader.using( [ 'jquery', 'mediawiki.util' ] ).then( function () {
		var $counter;
		var rafId = null;
		var pollTimer = null;
		var lastText = '';
		var lastTime = 0;
		var isSelecting = false;

		if ( window.SelectionCounterLoaded ) {
			return;
		}
		window.SelectionCounterLoaded = true;

		function label( count ) {
			var lang = mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn';
			return lang.toLowerCase().indexOf( 'en' ) === 0 ? 'Selected ' + count + ' chars' : '已选 ' + count + ' 字';
		}

		function getSelectionText() {
			var selection = window.getSelection();
			if ( !selection || selection.rangeCount === 0 || selection.isCollapsed ) {
				return '';
			}
			return selection.toString();
		}

		function showCount( text ) {
			var compact = text.replace( /\s+/g, '' );
			var count;
			if ( !compact ) {
				return false;
			}
			count = Array.from( compact ).length;
			$counter.text( label( count ) ).show();
			return true;
		}

		function updateCounter() {
			var text;
			rafId = null;
			text = getSelectionText();

			if ( text ) {
				lastText = text;
				lastTime = Date.now();
				showCount( text );
				return;
			}

			if ( lastText && Date.now() - lastTime < 1200 ) {
				showCount( lastText );
				return;
			}

			$counter.hide();
		}

		function scheduleUpdate() {
			if ( !rafId ) {
				rafId = window.requestAnimationFrame( updateCounter );
			}
		}

		function scheduleUpdateDelayed() {
			window.setTimeout( scheduleUpdate, 0 );
		}

		function startPolling() {
			if ( !pollTimer ) {
				pollTimer = window.setInterval( updateCounter, 500 );
			}
		}

		function stopPolling() {
			if ( pollTimer ) {
				window.clearInterval( pollTimer );
				pollTimer = null;
			}
		}

		function onSelectMove() {
			if ( isSelecting ) {
				scheduleUpdate();
			}
		}

		$( function () {
			$counter = $( '#mw-selection-counter' );
			if ( !$counter.length ) {
				$counter = $( '<div id="mw-selection-counter" class="mw-selection-counter" role="status" aria-live="polite"></div>' )
					.hide()
					.appendTo( 'body' );
			}

			document.addEventListener( 'selectionchange', function () {
				var text = getSelectionText();
				if ( text ) {
					lastText = text;
					lastTime = Date.now();
				}
				scheduleUpdate();
			} );
			document.addEventListener( 'mousedown', function () {
				isSelecting = true;
			}, true );
			document.addEventListener( 'mouseup', function () {
				isSelecting = false;
				scheduleUpdateDelayed();
			}, 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;
				}
				startPolling();
			} );

			startPolling();
		} );
	} );
}() );