MediaWiki:Gadget-SelectionCounter.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 更健壮的选区获取与事件触发 |
Maintenance script(留言 | 贡献) 记录最近选区并延迟显示 标签:清空 |
||
| 第1行: | 第1行: | ||
/* ================================================================ |
|||
MediaWiki Gadget: SelectionCounter |
|||
功能: |
|||
- 选中文本时显示字数统计 |
|||
- 与 RightToolbar 风格一致 |
|||
================================================================= */ |
|||
mw.loader.using(['mediawiki.util']).then(function () { |
|||
if (window.SelectionCounterLoaded) { |
|||
return; |
|||
} |
|||
window.SelectionCounterLoaded = true; |
|||
$(function () { |
|||
var $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'); |
|||
} |
|||
var rafId = null; |
|||
var pollTimer = null; |
|||
var lastText = null; |
|||
function getSelectionText() { |
|||
var selection = window.getSelection(); |
|||
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) { |
|||
return ''; |
|||
} |
|||
return selection.toString(); |
|||
} |
|||
function updateCounter() { |
|||
rafId = null; |
|||
var text = getSelectionText(); |
|||
if (!text) { |
|||
if (lastText !== null) { |
|||
lastText = null; |
|||
$counter.hide(); |
|||
} |
|||
return; |
|||
} |
|||
var compact = text.replace(/\s+/g, ''); |
|||
if (!compact) { |
|||
if (lastText !== null) { |
|||
lastText = null; |
|||
$counter.hide(); |
|||
} |
|||
return; |
|||
} |
|||
if (text === lastText) { |
|||
return; |
|||
} |
|||
lastText = text; |
|||
var count = Array.from(compact).length; |
|||
$counter.text('已选 ' + count + ' 字').show(); |
|||
} |
|||
function scheduleUpdate() { |
|||
if (rafId) { |
|||
return; |
|||
} |
|||
rafId = window.requestAnimationFrame(updateCounter); |
|||
} |
|||
function scheduleUpdateDelayed() { |
|||
window.setTimeout(scheduleUpdate, 0); |
|||
} |
|||
function startPolling() { |
|||
if (pollTimer) { |
|||
return; |
|||
} |
|||
pollTimer = window.setInterval(updateCounter, 500); |
|||
} |
|||
function stopPolling() { |
|||
if (!pollTimer) { |
|||
return; |
|||
} |
|||
window.clearInterval(pollTimer); |
|||
pollTimer = null; |
|||
} |
|||
document.addEventListener('selectionchange', scheduleUpdate); |
|||
document.addEventListener('mouseup', scheduleUpdateDelayed, true); |
|||
document.addEventListener('mouseup', scheduleUpdateDelayed); |
|||
document.addEventListener('keyup', scheduleUpdate); |
|||
document.addEventListener('touchend', scheduleUpdateDelayed, { passive: true, capture: true }); |
|||
document.addEventListener('pointerup', scheduleUpdateDelayed, true); |
|||
window.addEventListener('focus', startPolling); |
|||
window.addEventListener('blur', stopPolling); |
|||
document.addEventListener('visibilitychange', function () { |
|||
if (document.hidden) { |
|||
stopPolling(); |
|||
return; |
|||
} |
|||
startPolling(); |
|||
}); |
|||
startPolling(); |
|||
}); |
|||
}); |
|||