201 lines
7.0 KiB
JavaScript
201 lines
7.0 KiB
JavaScript
// 边注定位:宽屏时把 .post-note 放到正文左侧的空白处(不改变正文布局),
|
|
// 并用折线把「原文所在行左侧的节点」连到「注释卡片右边中点」。
|
|
// 窄屏 / 空白不够时交还给 CSS(虚线框内嵌)。
|
|
(function () {
|
|
var MIN_W = 160; // 低于这个宽度就不做边注
|
|
var MAX_W = 360; // 注释最大宽度
|
|
var GAP = 32; // 注释卡片与正文文字之间的间距(折线走这里)
|
|
var EDGE = 8; // 注释与视口边缘的最小距离
|
|
var MIN_STACK = 12; // 同一侧两条注释之间的最小垂直间距
|
|
var CHANNELS = 3; // 折线在间距里分几条竖向通道,避免互相重叠
|
|
var CHANNEL_GAP = 6; // 相邻通道的间距
|
|
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
|
|
function reset(main, notes) {
|
|
main.classList.remove("notes-active");
|
|
var svg = main.querySelector(".post-notes-lines");
|
|
if (svg) svg.remove();
|
|
notes.forEach(function (note) {
|
|
note.classList.remove("placed");
|
|
note.style.top = "";
|
|
note.style.left = "";
|
|
note.style.width = "";
|
|
});
|
|
}
|
|
|
|
// 取注释所在「行」的垂直中点。
|
|
// 做法:用 Range 从段落开头量到 .post-note-anchor(零尺寸基准点,贴在行基线上),
|
|
// 它的最后一个 rect 就是标记所在那一行的文字片段,取其中点即可。
|
|
// 这样既不受上标(sup)抬高的影响,也不会被行内代码撑高的行框带偏。
|
|
function lineCenter(mark, mr) {
|
|
var probe = mark.previousElementSibling;
|
|
if (!probe || !probe.classList.contains("post-note-anchor")) {
|
|
probe = mark;
|
|
}
|
|
|
|
var block =
|
|
mark.closest("p, li, td, th, dd, dt, blockquote, h1, h2, h3, h4, h5, h6") ||
|
|
mark.parentElement ||
|
|
mark;
|
|
|
|
function rectsOf(setup) {
|
|
var range = document.createRange();
|
|
setup(range);
|
|
return range.getClientRects();
|
|
}
|
|
|
|
function pick(list, fromEnd) {
|
|
var i, r;
|
|
if (fromEnd) {
|
|
for (i = list.length - 1; i >= 0; i--) {
|
|
r = list[i];
|
|
if (r.height > 0) return r;
|
|
}
|
|
} else {
|
|
for (i = 0; i < list.length; i++) {
|
|
r = list[i];
|
|
if (r.height > 0) return r;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
var lineRect = pick(
|
|
rectsOf(function (r) {
|
|
r.setStart(block, 0);
|
|
r.setEndAfter(probe);
|
|
}),
|
|
true,
|
|
);
|
|
|
|
if (!lineRect || !lineRect.height) {
|
|
// 标记正好在行首时,改用「从标记到段落结尾」的第一个 rect
|
|
lineRect = pick(
|
|
rectsOf(function (r) {
|
|
r.setStartBefore(probe);
|
|
r.setEnd(block, block.childNodes.length);
|
|
}),
|
|
false,
|
|
);
|
|
}
|
|
|
|
if (!lineRect || !lineRect.height) {
|
|
return probe.getBoundingClientRect().top - mr.top;
|
|
}
|
|
|
|
return lineRect.top + lineRect.height / 2 - mr.top;
|
|
}
|
|
|
|
function layout() {
|
|
var main = document.getElementById("content");
|
|
if (!main) return;
|
|
|
|
var notes = Array.prototype.slice.call(
|
|
main.querySelectorAll(".post-note"),
|
|
);
|
|
if (!notes.length) return;
|
|
|
|
// 左侧可用空白 = 正文文字左边到视口左边(含本页空着的侧栏占位区)
|
|
var mr = main.getBoundingClientRect();
|
|
var content = main.querySelector(".content");
|
|
var textLeftPage = content
|
|
? content.getBoundingClientRect().left
|
|
: mr.left;
|
|
var width = Math.min(MAX_W, textLeftPage - GAP - EDGE);
|
|
|
|
if (!(width >= MIN_W)) {
|
|
reset(main, notes);
|
|
return;
|
|
}
|
|
|
|
main.classList.add("notes-active");
|
|
|
|
var textLeft = textLeftPage - mr.left;
|
|
var leftX = textLeftPage - GAP - width; // 卡片在页面坐标里的左边界
|
|
|
|
var bottom = 0;
|
|
notes.forEach(function (note, i) {
|
|
var mark = note.previousElementSibling;
|
|
if (!mark || !mark.classList.contains("post-note-mark")) {
|
|
mark = note;
|
|
}
|
|
|
|
var markRect = mark.getBoundingClientRect();
|
|
note.classList.add("placed");
|
|
note.style.width = width + "px";
|
|
|
|
var height = note.offsetHeight;
|
|
var lineY = lineCenter(mark, mr);
|
|
|
|
var top = Math.max(0, lineY - 6);
|
|
if (top < bottom + MIN_STACK) top = bottom + MIN_STACK;
|
|
|
|
note.style.top = top + "px";
|
|
note.style.left = leftX - mr.left + "px";
|
|
|
|
note._lineY = lineY;
|
|
note._midY = top + height / 2;
|
|
bottom = top + height;
|
|
});
|
|
|
|
drawLines(main, notes, mr, textLeft, width);
|
|
}
|
|
|
|
function drawLines(main, notes, mr, textLeft, width) {
|
|
var svg = main.querySelector(".post-notes-lines");
|
|
if (!svg) {
|
|
svg = document.createElementNS(SVG_NS, "svg");
|
|
svg.setAttribute("class", "post-notes-lines");
|
|
main.appendChild(svg);
|
|
}
|
|
|
|
var minX = -GAP - 12;
|
|
var svgWidth = main.offsetWidth + Math.abs(minX);
|
|
var svgHeight = Math.max(main.scrollHeight, main.offsetHeight);
|
|
|
|
svg.setAttribute("width", svgWidth);
|
|
svg.setAttribute("height", svgHeight);
|
|
svg.setAttribute("viewBox", minX + " 0 " + svgWidth + " " + svgHeight);
|
|
svg.style.left = minX + "px";
|
|
svg.style.top = "0px";
|
|
svg.style.width = svgWidth + "px";
|
|
svg.style.height = svgHeight + "px";
|
|
|
|
while (svg.firstChild) svg.removeChild(svg.firstChild);
|
|
|
|
notes.forEach(function (note, noteIndex) {
|
|
var x0 = textLeft - 2; // 原文所在行左侧的起点
|
|
var x2 = note.offsetLeft + note.offsetWidth; // 卡片右边中点
|
|
// 竖向通道:按序号错开,避免几条折线叠在同一根竖线上
|
|
var x1 = x2 + 8 + (noteIndex % CHANNELS) * CHANNEL_GAP;
|
|
var y0 = note._lineY;
|
|
var y1 = note._midY;
|
|
|
|
var path = document.createElementNS(SVG_NS, "path");
|
|
path.setAttribute(
|
|
"d",
|
|
"M" + x0 + " " + y0 + " H" + x1 + " V" + y1 + " H" + x2,
|
|
);
|
|
svg.appendChild(path);
|
|
});
|
|
}
|
|
|
|
var timer;
|
|
function schedule() {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(layout, 100);
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", layout);
|
|
window.addEventListener("load", layout);
|
|
window.addEventListener("resize", schedule);
|
|
if (document.fonts && document.fonts.ready) {
|
|
document.fonts.ready.then(layout);
|
|
}
|
|
if (window.ResizeObserver) {
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
var content = document.querySelector("#content .content");
|
|
if (content) new ResizeObserver(schedule).observe(content);
|
|
});
|
|
}
|
|
})(); |