Anchor margin-note connectors to the line's text center
This commit is contained in:
@@ -17,7 +17,9 @@ tags:
|
|||||||
|
|
||||||
人是长着两副面孔的。
|
人是长着两副面孔的。
|
||||||
|
|
||||||
一副朝着别人。父母、朋友、同事、路上撞了一下的陌生人,人和人的牵连织成一个人,抽掉哪一根都不成样子。这副面孔归社会生活,马克思在《关于费尔巴哈的提纲》第六条里写:「人的本质不是单个人所固有的抽象物,在其现实性上,它是一切社会关系的总和。」
|
一副朝着别人。父母、朋友、同事、路上撞了一下的陌生人,人和人的牵连织成一个人,抽掉哪一根都不成样子。这副面孔归社会生活,马克思在《关于费尔巴哈的提纲》第六条里写:
|
||||||
|
|
||||||
|
>「人的本质不是单个人所固有的抽象物,在其现实性上,它是一切社会关系的总和。」
|
||||||
|
|
||||||
{{< note cite >}}Marx, Karl. "Thesen über Feuerbach"(关于费尔巴哈的提纲), 第六条. 1845.{{< /note >}}
|
{{< note cite >}}Marx, Karl. "Thesen über Feuerbach"(关于费尔巴哈的提纲), 第六条. 1845.{{< /note >}}
|
||||||
|
|
||||||
|
|||||||
+22
-48
@@ -24,66 +24,40 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 取注释所在「行」的垂直中点。
|
// 取注释所在「行」的垂直中点。
|
||||||
// 做法:用 Range 从段落开头量到 .post-note-anchor(零尺寸基准点,贴在行基线上),
|
// 首选量「标记前最后一个字符」的字体盒(浏览器给的就是该行的文字盒,取中点即行中线);
|
||||||
// 它的最后一个 rect 就是标记所在那一行的文字片段,取其中点即可。
|
// 标记在行首、前面没有文字时,退回 .post-note-anchor(0 宽、1em 高、基线对齐的行内块)的中点。
|
||||||
// 这样既不受上标(sup)抬高的影响,也不会被行内代码撑高的行框带偏。
|
// 两种方式都不受上标(sup)抬高、行内代码撑高行框的影响。
|
||||||
function lineCenter(mark, mr) {
|
function lineCenter(mark, mr) {
|
||||||
var probe = mark.previousElementSibling;
|
var probe = mark.previousElementSibling;
|
||||||
if (!probe || !probe.classList.contains("post-note-anchor")) {
|
if (!probe || !probe.classList.contains("post-note-anchor")) {
|
||||||
probe = mark;
|
probe = mark;
|
||||||
}
|
}
|
||||||
|
|
||||||
var block =
|
var probeRect = probe.getBoundingClientRect();
|
||||||
mark.closest("p, li, td, th, dd, dt, blockquote, h1, h2, h3, h4, h5, h6") ||
|
var baseline = probeRect.bottom;
|
||||||
mark.parentElement ||
|
var lineHeight = parseFloat(
|
||||||
mark;
|
getComputedStyle(probe.parentElement || mark).lineHeight,
|
||||||
|
|
||||||
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 (!lineHeight || isNaN(lineHeight)) lineHeight = 28;
|
||||||
|
|
||||||
if (!lineRect || !lineRect.height) {
|
var prev = probe.previousSibling;
|
||||||
// 标记正好在行首时,改用「从标记到段落结尾」的第一个 rect
|
if (prev && prev.nodeType === 3 && prev.nodeValue.length) {
|
||||||
lineRect = pick(
|
var range = document.createRange();
|
||||||
rectsOf(function (r) {
|
range.setStart(prev, prev.nodeValue.length - 1);
|
||||||
r.setStartBefore(probe);
|
range.setEnd(prev, prev.nodeValue.length);
|
||||||
r.setEnd(block, block.childNodes.length);
|
var charRect = range.getBoundingClientRect();
|
||||||
}),
|
// 最后一个字符若在上一行,charRect 底边会明显高于本行基线
|
||||||
false,
|
if (charRect.height && charRect.bottom - baseline < lineHeight / 2) {
|
||||||
);
|
return charRect.top + charRect.height / 2 - mr.top;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lineRect || !lineRect.height) {
|
if (probeRect.height) {
|
||||||
return probe.getBoundingClientRect().top - mr.top;
|
return probeRect.top + probeRect.height / 2 - mr.top;
|
||||||
}
|
}
|
||||||
|
|
||||||
return lineRect.top + lineRect.height / 2 - mr.top;
|
var r = mark.getBoundingClientRect();
|
||||||
|
return r.top + r.height / 2 - mr.top;
|
||||||
}
|
}
|
||||||
|
|
||||||
function layout() {
|
function layout() {
|
||||||
|
|||||||
Reference in New Issue
Block a user