Notes: all-left, wider, lighter, numbered cards, connector polyline

This commit is contained in:
2026-11-15 10:33:27 +08:00
parent 210483763a
commit 9b56433db6
3 changed files with 140 additions and 66 deletions
+56 -20
View File
@@ -224,13 +224,13 @@ html.dark #post-list .post-card:hover {
margin-bottom: 1rem;
}
/* ===== 注释(边注):宽屏显示在两侧留白,窄屏虚线框内嵌 ===== */
/* ===== 注释(边注):宽屏显示在左侧留白并用折线连回原文,窄屏虚线框内嵌 ===== */
.post-note-mark {
font-size: 0.7em;
line-height: 0;
vertical-align: super;
margin-left: 0.1em;
color: #2563eb;
margin-left: 0.15em;
color: #3b82f6;
font-weight: 600;
}
@@ -243,19 +243,33 @@ html.dark #post-list .post-card:hover {
display: block;
margin: 0.5rem 0;
padding: 0.5rem 0.75rem;
border: 1px dashed #cbd5e1;
border: 1px dashed #dbe0e6;
border-radius: 0.375rem;
font-size: 0.85rem;
line-height: 1.65;
color: #6b7280;
line-height: 1.7;
color: #9aa1ab;
}
.dark .post-note {
border-color: #404040;
color: #9ca3af;
border-color: #333;
color: #7e848c;
}
/* 宽屏:由 notes.js 接管,放进页面两侧的空白处(不占用正文布局) */
/* 注释卡片上的序号 */
.post-note-num {
font-size: 0.72em;
font-weight: 600;
line-height: 0;
vertical-align: super;
margin-right: 0.45em;
color: #b6bcc5;
}
.dark .post-note-num {
color: #6b7078;
}
/* 宽屏:由 notes.js 接管,放进正文左侧的空白处 */
@media (min-width: 80rem) {
html.js .post-note {
display: none;
@@ -268,33 +282,55 @@ html.dark #post-list .post-card:hover {
html.js main#content.notes-active .post-note.placed {
display: block;
position: absolute;
z-index: 1;
margin: 0;
padding: 0 0.75rem;
border: 0 solid #e5e7eb;
border: 0 solid #e4e8ee;
border-right-width: 2px;
border-radius: 0;
background: none;
font-size: 0.875rem;
line-height: 1.7;
color: #6b7280;
line-height: 1.75;
color: #9aa1ab;
}
html.dark.js main#content.notes-active .post-note.placed {
color: #9ca3af;
border-color: #333;
color: #7e848c;
border-color: #2a2a2a;
}
/* 注释靠在正文哪一侧,就在朝向正文的那边加一条细线 */
html.js main#content.notes-active .post-note.placed[data-side="right"] {
border-left-width: 2px;
/* 连接原文与注释卡片的折线 */
.post-notes-lines {
position: absolute;
z-index: 0;
pointer-events: none;
overflow: visible;
}
html.js main#content.notes-active .post-note.placed[data-side="left"] {
border-right-width: 2px;
text-align: left;
.post-notes-lines path {
fill: none;
stroke: #d7dce2;
stroke-width: 1;
}
.post-notes-lines circle {
fill: #c4cad3;
}
html.dark .post-notes-lines path {
stroke: #353535;
}
html.dark .post-notes-lines circle {
fill: #4a4a4a;
}
}
@media print {
.post-notes-lines {
display: none !important;
}
.post-note {
display: block !important;
position: static !important;
+2 -2
View File
@@ -1,9 +1,9 @@
{{- /*
边注 / 注释。
宽屏:显示在正文两侧的空白处(由 static/js/notes.js 定位)。
宽屏:显示在正文左侧的空白处,并用一条折线与原文所在行相连(由 static/js/notes.js 定位)。
窄屏:以虚线框内嵌在文章中。
用法:{{< note >}}注释内容{{< /note >}}
*/ -}}
{{- $n := add (.Page.Store.Get "noteCount" | default 0) 1 -}}
{{- .Page.Store.Set "noteCount" $n -}}
<sup class="post-note-mark">{{ $n }}</sup><span class="post-note" data-note="{{ $n }}">{{- .Inner | .Page.RenderString (dict "display" "inline") -}}</span>
<sup class="post-note-mark">{{ $n }}</sup><span class="post-note"><span class="post-note-num">{{ $n }}</span>{{- .Inner | .Page.RenderString (dict "display" "inline") -}}</span>
+82 -44
View File
@@ -1,22 +1,21 @@
// 边注定位:宽屏时把 .post-note 放进正文两侧的空白处(不改变正文布局),
// 空白不够或窄屏时交还给 CSS(虚线框内嵌)。
//
// 三种模式:
// 1. 两侧留白都够宽 → 左右交替
// 2. 只有左侧够宽 → 全部放左侧(复用空着的侧栏占位区,不动正文)
// 3. 都不够宽 / 窄屏 → 不收边注,CSS 虚线框内嵌
// 边注定位:宽屏时把 .post-note 放到正文左侧的空白处(不改变正文布局),
// 并用折线把「原文所在行左侧的节点」连到「注释卡片右边中点」。
// 窄屏 / 空白不够时交还给 CSS(虚线框内嵌)。
(function () {
var MIN_W = 160; // 低于这个宽度就不做边注
var MAX_W = 288; // 注释最大宽度(18rem)
var GAP = 16; // 注释与正文之间的间距
var MAX_W = 320; // 注释最大宽度
var GAP = 24; // 注释卡片与正文文字之间的间距(折线走这里)
var EDGE = 8; // 注释与视口边缘的最小距离
var MIN_STACK = 12; // 同一侧两条注释之间的最小垂直间距
var STUB = 10; // 折线从原文节点向左先走的一小段
var SVG_NS = "http://www.w3.org/2000/svg";
function reset(notes) {
document.getElementById("content").classList.remove("notes-active");
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.removeAttribute("data-side");
note.style.top = "";
note.style.left = "";
note.style.width = "";
@@ -32,58 +31,97 @@
);
if (!notes.length) return;
var container =
document.querySelector(".hextra-max-page-width") || main;
var cr = container.getBoundingClientRect();
var vw = document.documentElement.clientWidth;
// 左侧可用空白 = 正文文字左边到视口左边(含本页空着的侧栏占位区)
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);
// 左侧可用空白 = 正文左边到视口左边(含空着的侧栏占位,它在本页没有内容)
var leftAvail = main.getBoundingClientRect().left;
// 右侧可用空白 = 视口右边到内容容器右边(TOC 之后)
var rightAvail = vw - cr.right;
var bothW = Math.min(MAX_W, Math.min(leftAvail - GAP - EDGE, rightAvail - GAP - EDGE));
var leftW = Math.min(MAX_W, leftAvail - GAP - EDGE);
var mode = bothW >= MIN_W ? "both" : leftW >= MIN_W ? "left" : "none";
if (mode === "none") {
reset(notes);
if (!(width >= MIN_W)) {
reset(main, notes);
return;
}
main.classList.add("notes-active");
var mr = main.getBoundingClientRect();
var width = mode === "both" ? bothW : leftW;
var leftX = leftAvail - GAP - width;
var rightX = cr.right + GAP;
var sideBottom = { left: -Infinity, right: -Infinity };
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 side = mode === "both" && i % 2 === 1 ? "left" : "right";
if (mode === "left") side = "left";
note.setAttribute("data-side", side);
var markRect = mark.getBoundingClientRect();
note.classList.add("placed");
note.style.width = width + "px";
var height = note.offsetHeight;
var anchorTop = mark.getBoundingClientRect().top - mr.top;
var lineY = markRect.top - mr.top + markRect.height * 0.75;
var top = Math.max(0, anchorTop - 6);
if (top < sideBottom[side] + MIN_STACK) {
top = sideBottom[side] + MIN_STACK;
}
var top = Math.max(0, lineY - 6);
if (top < bottom + MIN_STACK) top = bottom + MIN_STACK;
note.style.top = top + "px";
note.style.left =
(side === "right" ? rightX : leftX) - mr.left + "px";
sideBottom[side] = top + height;
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) {
var x0 = textLeft - 6; // 原文所在行左侧的节点
var x2 = note.offsetLeft + note.offsetWidth; // 卡片右边中点
// 折线拐点:先向左一小段,再纵向落到卡片中线,最后横向接上卡片
var x1 = x0 - STUB;
if (x1 <= x2 + 2) x1 = (x0 + x2) / 2;
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);
[ [x0, y0], [x2, y1] ].forEach(function (p) {
var dot = document.createElementNS(SVG_NS, "circle");
dot.setAttribute("cx", p[0]);
dot.setAttribute("cy", p[1]);
dot.setAttribute("r", "2.5");
svg.appendChild(dot);
});
});
}