Notes: keep TOC/layout intact, place notes in page margins with adaptive width

This commit is contained in:
2026-11-15 10:14:44 +08:00
parent cfe85f9b16
commit cbe7417fbf
4 changed files with 68 additions and 46 deletions
+9 -20
View File
@@ -255,53 +255,42 @@ html.dark #post-list .post-card:hover {
color: #9ca3af; color: #9ca3af;
} }
/* 宽屏:由 notes.js 接管,放进正文两侧的留白 */ /* 宽屏:由 notes.js 接管,放进页面两侧的空白处(不占用正文布局) */
@media (min-width: 80rem) { @media (min-width: 80rem) {
/* 去掉 TOC / 侧栏占位后,把正文栏收回原来的阅读宽度,两侧留给注释 */
main.notes-page {
max-width: 52rem;
margin-left: auto;
margin-right: auto;
}
html.js .post-note { html.js .post-note {
display: none; display: none;
} }
html.js main.notes-page.notes-active { html.js main#content.notes-active {
position: relative; position: relative;
} }
html.js main.notes-page.notes-active .post-note.placed { html.js main#content.notes-active .post-note.placed {
display: block; display: block;
position: absolute; position: absolute;
width: 12rem;
margin: 0; margin: 0;
padding: 0 0.75rem; padding: 0 0.75rem;
border: 0 solid #e5e7eb; border: 0 solid #e5e7eb;
border-radius: 0; border-radius: 0;
background: none; background: none;
font-size: 0.8125rem; font-size: 0.875rem;
line-height: 1.65; line-height: 1.7;
color: #6b7280; color: #6b7280;
} }
html.dark.js main.notes-page.notes-active .post-note.placed { html.dark.js main#content.notes-active .post-note.placed {
color: #9ca3af; color: #9ca3af;
border-color: #333; border-color: #333;
} }
/* 注释靠在正文哪一侧,就在朝向正文的那边加一条细线 */ /* 注释靠在正文哪一侧,就在朝向正文的那边加一条细线 */
html.js main.notes-page.notes-active .post-note.placed[data-side="right"] { html.js main#content.notes-active .post-note.placed[data-side="right"] {
left: calc(100% + 1rem);
right: auto;
border-left-width: 2px; border-left-width: 2px;
} }
html.js main.notes-page.notes-active .post-note.placed[data-side="left"] { html.js main#content.notes-active .post-note.placed[data-side="left"] {
right: calc(100% + 1rem);
left: auto;
border-right-width: 2px; border-right-width: 2px;
text-align: left;
} }
} }
+1 -1
View File
@@ -74,7 +74,7 @@ pokemon(wartortle).
pokemon(blastoise). pokemon(blastoise).
``` ```
在 Prolog 中,我们声明"谓词"(Predicate)。谓词定义关系:`bulbasaur` 是一个 `pokemon`,`charmander` 是一个 `pokemon`,以此类推。我们称这个谓词为 `pokemon/1`,因为它有一个参数。{{< note >}}斜杠后面的数字是**元数**(arity),表示参数个数。`pokemon/1` 和 `pokemon/2` 在 Prolog 里是两个完全不同的谓词,可以同时存在。{{< /note >}} 在 Prolog 中,我们声明"谓词"(Predicate)。谓词定义关系:`bulbasaur` 是一个 `pokemon`,`charmander` 是一个 `pokemon`,以此类推。我们称这个谓词为 `pokemon/1`,因为它有一个参数。{{< note >}}斜杠后面的数字是**元数**([arity](https://en.wikipedia.org/wiki/Arity)),表示参数个数。`pokemon/1` 和 `pokemon/2` 在 Prolog 里是两个完全不同的谓词,可以同时存在。{{< /note >}}
这些事实被加载到一个交互式提示符——"顶层"(top-level)中。你输入一个语句到提示符,Prolog 试图找出所有使该语句为真的方式: 这些事实被加载到一个交互式提示符——"顶层"(top-level)中。你输入一个语句到提示符,Prolog 试图找出所有使该语句为真的方式:
+3 -6
View File
@@ -1,16 +1,13 @@
{{ define "main" }} {{ define "main" }}
{{- /* 有边注的页面:宽屏两侧需要留白,因此去掉 TOC 与侧栏占位 */ -}} <div class="hx:mx-auto hx:flex hextra-max-page-width">
{{- $hasNotes := .HasShortcode "note" }}
<div class="hx:mx-auto hx:flex hextra-max-page-width{{ if $hasNotes }} has-notes{{ end }}">
{{ partial "sidebar.html" (dict "context" . "disableSidebar" true {{ partial "sidebar.html" (dict "context" . "disableSidebar" true
"displayPlaceholder" (not $hasNotes)) }} {{ if not $hasNotes }}{{ partial "displayPlaceholder" true) }} {{ partial "toc.html" . }}
"toc.html" . }}{{ end }}
<article <article
class="hx:w-full hx:break-words hx:flex hx:min-h-[calc(100vh-var(--navbar-height))] hx:min-w-0 hx:justify-center hx:pb-8 hx:pr-[calc(env(safe-area-inset-right)-1.5rem)]" class="hx:w-full hx:break-words hx:flex hx:min-h-[calc(100vh-var(--navbar-height))] hx:min-w-0 hx:justify-center hx:pb-8 hx:pr-[calc(env(safe-area-inset-right)-1.5rem)]"
> >
<main <main
id="content" id="content"
class="hx:w-full hx:min-w-0 hextra-max-content-width hx:px-6 hx:pt-0 hx:md:px-12{{ if $hasNotes }} notes-page{{ end }}" class="hx:w-full hx:min-w-0 hextra-max-content-width hx:px-6 hx:pt-0 hx:md:px-12"
> >
{{ partial "breadcrumb.html" (dict "page" . "enable" true) }} {{ if {{ partial "breadcrumb.html" (dict "page" . "enable" true) }} {{ if
.Title }} .Title }}
+55 -19
View File
@@ -1,11 +1,30 @@
// 边注定位:宽屏时把 .post-note 放到正文左右两侧的空白处, // 边注定位:宽屏时把 .post-note 放进正文两侧的空白处(不改变正文布局),
// 窄屏时交还给 CSS(虚线框内嵌)。 // 空白不够或窄屏时交还给 CSS(虚线框内嵌)。
//
// 三种模式:
// 1. 两侧留白都够宽 → 左右交替
// 2. 只有左侧够宽 → 全部放左侧(复用空着的侧栏占位区,不动正文)
// 3. 都不够宽 / 窄屏 → 不收边注,CSS 虚线框内嵌
(function () { (function () {
var WIDE = 1280; // 与 CSS 的 @media (min-width: 80rem) 保持一致 var MIN_W = 160; // 低于这个宽度就不做边注
var MIN_GAP = 12; // 同一侧两条注释之间的最小垂直间距 var MAX_W = 240; // 注释最大宽度
var GAP = 16; // 注释与正文之间的间距
var EDGE = 8; // 注释与视口边缘的最小距离
var MIN_STACK = 12; // 同一侧两条注释之间的最小垂直间距
function reset(notes) {
document.getElementById("content").classList.remove("notes-active");
notes.forEach(function (note) {
note.classList.remove("placed");
note.removeAttribute("data-side");
note.style.top = "";
note.style.left = "";
note.style.width = "";
});
}
function layout() { function layout() {
var main = document.querySelector("main.notes-page"); var main = document.getElementById("content");
if (!main) return; if (!main) return;
var notes = Array.prototype.slice.call( var notes = Array.prototype.slice.call(
@@ -13,19 +32,31 @@
); );
if (!notes.length) return; if (!notes.length) return;
if (window.innerWidth < WIDE) { var container =
main.classList.remove("notes-active"); document.querySelector(".hextra-max-page-width") || main;
notes.forEach(function (note) { var cr = container.getBoundingClientRect();
note.classList.remove("placed"); var vw = document.documentElement.clientWidth;
note.removeAttribute("data-side");
note.style.top = ""; // 左侧可用空白 = 正文左边到视口左边(含空着的侧栏占位,它在本页没有内容)
}); 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);
return; return;
} }
main.classList.add("notes-active"); main.classList.add("notes-active");
var mainTop = main.getBoundingClientRect().top; 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 sideBottom = { left: -Infinity, right: -Infinity };
notes.forEach(function (note, i) { notes.forEach(function (note, i) {
@@ -34,19 +65,24 @@
mark = note; mark = note;
} }
var side = i % 2 === 0 ? "right" : "left"; var side = mode === "both" && i % 2 === 1 ? "left" : "right";
if (mode === "left") side = "left";
note.setAttribute("data-side", side); note.setAttribute("data-side", side);
note.classList.add("placed"); note.classList.add("placed");
note.style.width = width + "px";
var anchorTop = mark.getBoundingClientRect().top - mainTop;
var height = note.offsetHeight; var height = note.offsetHeight;
var anchorTop = mark.getBoundingClientRect().top - mr.top;
var top = Math.max(0, anchorTop - 6); var top = Math.max(0, anchorTop - 6);
if (top < sideBottom[side] + MIN_GAP) { if (top < sideBottom[side] + MIN_STACK) {
top = sideBottom[side] + MIN_GAP; top = sideBottom[side] + MIN_STACK;
} }
note.style.top = top + "px"; note.style.top = top + "px";
note.style.left =
(side === "right" ? rightX : leftX) - mr.left + "px";
sideBottom[side] = top + height; sideBottom[side] = top + height;
}); });
} }
@@ -65,8 +101,8 @@
} }
if (window.ResizeObserver) { if (window.ResizeObserver) {
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
var content = document.querySelector("main.notes-page .content"); var content = document.querySelector("#content .content");
if (content) new ResizeObserver(schedule).observe(content); if (content) new ResizeObserver(schedule).observe(content);
}); });
} }
})(); })();