MediaWiki:Qlzq-Beautiful-Smart-Law-SideNotes.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* QLZQ:SimpleTooltip 自动局部双栏批注 v5 202611240016*/
(function () {
if (mw.config.get('wgAction') !== 'view') return;
mw.loader.using('ext.SimpleTooltip').then(function () {
const root = document.querySelector('#mw-content-text .mw-parser-output');
if (!root || root.classList.contains('qlzq-note-v5')) return;
const anchors = Array.from(root.querySelectorAll(
'.simple-tooltip-inline[data-simple-tooltip]'
));
const byParagraph = new Map();
anchors.forEach(function (anchor) {
const p = anchor.closest('p');
if (p && root.contains(p)) {
if (!byParagraph.has(p)) byParagraph.set(p, []);
byParagraph.get(p).push(anchor);
}
});
if (!byParagraph.size) return;
root.classList.add('qlzq-note-v5');
const groups = [];
const SVG_NS = 'http://www.w3.org/2000/svg';
// 只有过长且收纳的批注,才启用原生悬停全文。
function syncTooltip(entry, show) {
const anchor = entry.anchor;
if (!anchor.classList.contains('tooltipstered') ||
!window.jQuery ||
typeof jQuery.fn.tooltipster !== 'function') return;
const action = show ? 'enable' : 'disable';
if (entry.tooltipAction === action) return;
try {
jQuery(anchor).tooltipster(action);
entry.tooltipAction = action;
} catch (err) {
console.warn('QLZQ Tooltipster 状态切换失败', err);
}
}
byParagraph.forEach(function (paragraphAnchors, paragraph) {
const row = document.createElement('div');
row.className = 'qlzq-balanced-row';
const side = document.createElement('aside');
side.className = 'qlzq-balanced-side';
side.setAttribute('aria-label', '本自然段的批注');
paragraph.before(row);
row.append(paragraph, side);
const entries = [];
paragraphAnchors.forEach(function (anchor) {
const template = document.createElement('template');
template.innerHTML =
anchor.getAttribute('data-simple-tooltip') || '';
const comment =
(template.content.textContent || '').trim();
const card = document.createElement('div');
card.className = 'qlzq-balanced-card';
card.tabIndex = 0;
const heading = document.createElement('strong');
heading.textContent =
'✎ ' + anchor.textContent.trim();
const body = document.createElement('div');
body.className = 'qlzq-balanced-text';
body.textContent = comment;
card.append(heading, body);
side.appendChild(card);
anchor.classList.add('qlzq-balanced-anchor');
// 保留 v4 的悬停连接线
const svg = document.createElementNS(SVG_NS, 'svg');
svg.classList.add('qlzq-balanced-svg');
svg.setAttribute('aria-hidden', 'true');
const path = document.createElementNS(SVG_NS, 'path');
path.classList.add('qlzq-balanced-path');
const dot = document.createElementNS(SVG_NS, 'circle');
dot.classList.add('qlzq-balanced-dot');
dot.setAttribute('r', '2.5');
svg.append(path, dot);
row.appendChild(svg);
const mark = function (active) {
[anchor, card, path, dot].forEach(function (el) {
el.classList.toggle(
'qlzq-balanced-active', active
);
});
};
[anchor, card].forEach(function (el) {
el.addEventListener(
'mouseenter', () => mark(true)
);
el.addEventListener(
'mouseleave', () => mark(false)
);
el.addEventListener(
'focusin', () => mark(true)
);
el.addEventListener(
'focusout', () => mark(false)
);
});
entries.push({
anchor, card, svg, path, dot,
tooltipAction: null
});
});
groups.push({ row, paragraph, side, entries });
});
let scheduled = false;
function schedule() {
if (scheduled) return;
scheduled = true;
requestAnimationFrame(function () {
scheduled = false;
layout();
});
}
function layout() {
groups.forEach(function ({
row, paragraph, side, entries
}) {
const available =
row.getBoundingClientRect().width;
row.classList.remove('qlzq-balanced-stack');
// 每次重新测量前,恢复批注的自然高度
entries.forEach(function (entry) {
entry.card.style.maxHeight = '';
entry.card.style.overflowY = '';
entry.card.classList.remove(
'qlzq-balanced-clipped'
);
});
// 屏幕窄时不强制双栏
if (available < 650) {
row.classList.add('qlzq-balanced-stack');
side.style.width = '';
entries.forEach(entry =>
syncTooltip(entry, false)
);
return;
}
// 继承 v4:尝试多种批注宽度
const maxWidth = Math.min(
300,
Math.floor(available * 0.34)
);
const choices = [120, 150, 180, 215, 255, maxWidth]
.filter((w, i, arr) =>
w <= maxWidth && arr.indexOf(w) === i
);
let best = null;
choices.forEach(function (width) {
side.style.width = width + 'px';
const pHeight =
paragraph.getBoundingClientRect().height;
const noteHeight =
side.getBoundingClientRect().height;
const score =
Math.abs(pHeight - noteHeight)
+ Math.max(pHeight, noteHeight) * 0.10
+ Math.abs(width - 170) * 0.02;
if (!best || score < best.score) {
best = {
width,
pHeight,
noteHeight,
score
};
}
});
if (!best) return;
side.style.width = best.width + 'px';
/*
* v5 核心变化:
* 原文短、批注长,不再改成段后排列。
* 保持双栏、垂直居中,必要时限制右栏高度。
*/
const excess =
best.noteHeight - best.pHeight;
if (
excess > 70 &&
best.noteHeight > best.pHeight * 1.9
) {
const count = entries.length;
const cardLimit = count === 1
? Math.max(
96,
Math.min(190, best.pHeight + 52)
)
: Math.max(
76,
Math.min(
150,
(best.pHeight + 95) / count
)
);
entries.forEach(function (entry) {
entry.card.style.maxHeight =
Math.round(cardLimit) + 'px';
entry.card.style.overflowY = 'auto';
const clipped =
entry.card.scrollHeight >
entry.card.clientHeight + 2;
entry.card.classList.toggle(
'qlzq-balanced-clipped',
clipped
);
// 内容被收纳时,悬停原文可阅读全文
syncTooltip(entry, clipped);
});
} else {
entries.forEach(entry =>
syncTooltip(entry, false)
);
}
});
// 保留 v4 的对应关系连线
groups.forEach(function ({ row, entries }) {
if (row.classList.contains(
'qlzq-balanced-stack'
)) return;
const rr = row.getBoundingClientRect();
entries.forEach(function ({
anchor, card, svg, path, dot
}) {
const fragments = anchor.getClientRects();
const ar = fragments.length
? fragments[fragments.length - 1]
: anchor.getBoundingClientRect();
const cr = card.getBoundingClientRect();
const x1 = ar.right - rr.left + 2;
const y1 = ar.bottom - rr.top;
const x2 = cr.left - rr.left;
const y2 =
cr.top + cr.height / 2 - rr.top;
const curve = Math.max(
14,
Math.abs(x2 - x1) / 2
);
const mid1 = x1 + (
x2 >= x1 ? curve : 12
);
const mid2 = x2 - (
x2 >= x1 ? curve : 12
);
const width = Math.max(1, rr.width);
const height = Math.max(1, rr.height);
svg.setAttribute(
'viewBox',
`0 0 ${width} ${height}`
);
path.setAttribute(
'd',
`M ${x1} ${y1} C ${mid1} ${y1}, ` +
`${mid2} ${y2}, ${x2} ${y2}`
);
dot.setAttribute('cx', x1);
dot.setAttribute('cy', y1);
});
});
}
window.addEventListener('resize', schedule);
window.addEventListener('load', schedule);
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(schedule);
}
if (typeof ResizeObserver !== 'undefined') {
let previousWidth = root.clientWidth;
const observer = new ResizeObserver(function () {
if (root.clientWidth !== previousWidth) {
previousWidth = root.clientWidth;
schedule();
}
});
observer.observe(root);
}
schedule();
}).catch(function (error) {
console.warn('QLZQ 自动旁注加载失败:', error);
});
})();