Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Improve PDF annotation visibility on dark backgrounds https://github.com/siyuan-note/siyuan/issues/11797

Daniele89f5765aba8
diff --git a/app/src/asset/anno.ts b/app/src/asset/anno.ts
index ec210daf56..d7dd0656d1 100644
--- a/app/src/asset/anno.ts
+++ b/app/src/asset/anno.ts
@@ -100,7 +100,7 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
newHeight = y - newTop;
}
rectResizeElement.setAttribute("style",
- `top:${newTop}px;height:${newHeight}px;left:${newLeft}px;width:${newWidth}px;background-color:${moveEvent.altKey ? "var(--b3-pdf-background1)" : ""}`);
+ `top:${newTop}px;height:${newHeight}px;left:${newLeft}px;width:${newWidth}px;background-color:${moveEvent.altKey ? "color-mix(in srgb, var(--b3-pdf-background1) 35%, transparent)" : ""}`);
};
documentSelf.onmouseup = () => {
documentSelf.onmousemove = null;
@@ -161,14 +161,7 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
const annoItem = config[rectElement.getAttribute("data-node-id")];
annoItem.color = color;
element.querySelectorAll(`.pdf__rect[data-node-id="${rectElement.getAttribute("data-node-id")}"]`).forEach(rectItem => {
- Array.from(rectItem.children).forEach((item: HTMLElement) => {
- item.style.border = "2px solid " + color;
- if (annoItem.type === "text") {
- item.style.backgroundColor = color;
- } else {
- item.style.backgroundColor = "transparent";
- }
- });
+ (rectItem as HTMLElement).style.setProperty("--pdf-annotation-color", color);
});
fetchPost("/api/asset/setFileAnnotation", {
path: pdf.appConfig.file.replace(location.origin, "").substr(1) + ".sya",
@@ -239,13 +232,7 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
annoItem.type = "border";
}
element.querySelectorAll(`.pdf__rect[data-node-id="${rectElement.getAttribute("data-node-id")}"]`).forEach(rectItem => {
- Array.from(rectItem.children).forEach((item: HTMLElement) => {
- if (annoItem.type === "text") {
- item.style.backgroundColor = item.style.border.replace("2px solid ", "");
- } else {
- item.style.backgroundColor = "";
- }
- });
+ (rectItem as HTMLElement).dataset.type = annoItem.type;
});
fetchPost("/api/asset/setFileAnnotation", {
path: pdf.appConfig.file.replace(location.origin, "").substr(1) + ".sya",
@@ -640,6 +627,7 @@ export const getHighlight = (element: HTMLElement) => {
if (!pdfInstance) {
return;
}
+ element.parentElement.querySelector(":scope > .pdf__rects")?.remove();
const pageIndex = parseInt(
element.parentElement.getAttribute("data-page-number")) - 1;
const config = getConfig(pdfInstance);
@@ -675,24 +663,30 @@ const showHighlight = (selected: IPdfAnno, pdf: any, hl?: boolean) => {
}
const viewport = page.viewport.clone({rotation: 0}); // rotation https://github.com/siyuan-note/siyuan/issues/9831
- let rectsElement = textLayerElement.querySelector(".pdf__rects");
+ const pageElement = textLayerElement.parentElement;
+ let rectsElement = pageElement.querySelector(":scope > .pdf__rects") as HTMLElement;
if (!rectsElement) {
- textLayerElement.insertAdjacentHTML("beforeend", "<div class='pdf__rects'></div>");
- rectsElement = textLayerElement.querySelector(".pdf__rects");
+ rectsElement = document.createElement("div");
+ rectsElement.className = "pdf__rects";
+ pageElement.append(rectsElement);
}
- let html = `<div class="pdf__rect popover__block" data-node-id="${selected.id}" data-relations="${selected.ids || ""}" data-mode="${selected.mode}">`;
+ rectsElement.style.width = textLayerElement.style.width;
+ rectsElement.style.height = textLayerElement.style.height;
+ rectsElement.style.transform = textLayerElement.style.transform;
+ const mainRotation = textLayerElement.getAttribute("data-main-rotation");
+ if (mainRotation) {
+ rectsElement.setAttribute("data-main-rotation", mainRotation);
+ } else {
+ rectsElement.removeAttribute("data-main-rotation");
+ }
+ let html = `<div class="pdf__rect popover__block" data-node-id="${selected.id}" data-relations="${selected.ids || ""}" data-mode="${selected.mode}" data-type="${selected.type}" style="--pdf-annotation-color: ${selected.color}">`;
selected.coords.forEach((rect) => {
const bounds = viewport.convertToViewportRectangle(rect);
const width = Math.abs(bounds[0] - bounds[2]);
if (width <= 0) {
return;
}
- let style = `border: 2px solid ${selected.color};background-color: ${selected.color};`;
- if (selected.type === "border") {
- style = `border: 2px solid ${selected.color};`;
- }
- html += `<div style="${style}
-left:${Math.min(bounds[0], bounds[2])}px;
+ html += `<div style="left:${Math.min(bounds[0], bounds[2])}px;
top:${Math.min(bounds[1], bounds[3])}px;
width:${width}px;
height: ${Math.abs(bounds[1] - bounds[3])}px"></div>`;
@@ -702,7 +696,7 @@ height: ${Math.abs(bounds[1] - bounds[3])}px"></div>`;
if (hl) {
hlPDFRect(rectsElement, selected.id);
}
- return rectsElement.lastElementChild;
+ return rectsElement.lastElementChild as HTMLElement;
};
export const hlPDFRect = (element: HTMLElement, id: string) => {
diff --git a/app/src/asset/pdf/text_layer_builder.js b/app/src/asset/pdf/text_layer_builder.js
index 981fc05863..e7733f6af6 100644
--- a/app/src/asset/pdf/text_layer_builder.js
+++ b/app/src/asset/pdf/text_layer_builder.js
@@ -82,8 +82,7 @@ class TextLayerBuilder {
});
this.show();
// NOTE
- this.div.querySelector(".pdf__rects")?.remove()
- getHighlight(this.div)
+ getHighlight(this.div);
return;
}
@@ -116,7 +115,7 @@ class TextLayerBuilder {
this.#onAppend?.(this.div);
this.highlighter?.enable();
// NOTE
- getHighlight(this.div)
+ getHighlight(this.div);
this.accessibilityManager?.enable();
}
diff --git a/app/src/assets/scss/pdf/_pdf.scss b/app/src/assets/scss/pdf/_pdf.scss
index a3d206c6cb..95b545fae7 100644
--- a/app/src/assets/scss/pdf/_pdf.scss
+++ b/app/src/assets/scss/pdf/_pdf.scss
@@ -30,8 +30,7 @@
height: 100%;
position: relative;
- .textLayer,
- .pdf__resize {
+ .textLayer {
/* dark 蓝色、紫色背景显示不清楚,文档位于 https://github.com/siyuan-note/siyuan/issues/5351 */
mix-blend-mode: multiply;
}
@@ -51,7 +50,10 @@
}
&__rect {
+ --pdf-annotation-color: var(--b3-pdf-background1);
+
cursor: pointer;
+ pointer-events: none;
&--hl > div {
transition: opacity 1500ms linear;
@@ -64,6 +66,8 @@
box-sizing: border-box;
position: absolute;
z-index: 1;
+ border: 2px solid var(--pdf-annotation-color);
+ pointer-events: auto;
&:first-child {
border-bottom-left-radius: var(--b3-border-radius);
@@ -75,16 +79,34 @@
border-bottom-right-radius: var(--b3-border-radius);
}
}
+
+ &:not([data-type="border"]) > div {
+ background-color: color-mix(in srgb, var(--pdf-annotation-color) 35%, transparent);
+ }
}
&__resize {
position: fixed;
+ z-index: 3;
border: 2px solid var(--b3-pdf-background1);
box-sizing: border-box;
border-radius: var(--b3-border-radius);
}
}
+.pdf__rects {
+ position: absolute;
+ inset: 0;
+ z-index: 3;
+ overflow: clip;
+ pointer-events: none;
+ transform-origin: 0 0;
+}
+
+.textLayer[hidden] ~ .pdf__rects {
+ display: none;
+}
+
.pdf__outer--dark {
.pdfViewer .page canvas,
.thumbnailImage {
@@ -95,8 +117,7 @@
background-color: var(--b3-pdf-dark);
}
- .textLayer,
- .pdf__resize {
+ .textLayer {
z-index: 2; // pdf 缩放后 canvas 会出现在末尾,导致无法选中和查看标注
mix-blend-mode: screen;
}