Files
shumai/packages/webui/components/viewers/pdf/text-layer.css
T
Yiling-J 1cd94a77c6 feat(webui): direct canvas PDF rendering with selectable text (#299)
* feat(webui): render pdf pages via live canvas with selectable text

Replace the rasterize-to-PNG pipeline in the PDF viewer with direct
canvas rendering that tracks the current zoom, so text stays crisp when
zoomed in, and overlay a pdf.js TextLayer that makes text natively
selectable when not in draw mode.

- pdf-viewer: render pages onto a live canvas at a zoom-proportional
  scale (debounced, memory-capped) instead of a fixed 2x PNG snapshot
- pdf-viewer: add pdf.js TextLayer overlay per page, transformed in
  lockstep with the Konva stage so spans stay aligned while panning
  and zooming
- drawing-canvas: accept an optional live canvas element and force a
  layer redraw when it is swapped
- add text-layer.css with the minimal pdf.js text layer styles
- add e2e spec verifying text selection in view mode and that draw
  mode routes drags to the canvas

* fix(webui): show pdf text selection highlight above the page canvas

The pdf.js text layer was stacked below the opaque Konva page canvas, so
the native selection highlight was painted underneath it and invisible
(selection itself worked). Move the overlay above the canvas and gate its
pointer-events on draw mode: auto when not drawing (select text), none in
draw mode so drags reach the Konva stage below (draw markup).

Verified with screenshot pixel analysis: the selected region now renders
the highlight (blue) where it was pure grayscale before.

* fix(webui): transparent selection background for pdf text layer br elements

* fix(webui): stop pdf text selection over-selecting in empty space

The text layer container was user-selectable (user-select: auto), so when
a drag's caret moved into empty space Chromium targeted the container and
extended the selection to a container-content boundary, selecting across
rows (e.g. dragging from line 1 into the gap selected lines 1-2).

Make the container user-select: none while keeping the text spans
user-select: text, so the caret clamps to the nearest selectable text and
the selection stays put when dragging into empty space.

* fix(webui): deselect pdf text when clicking empty space

With user-select: none on the text layer container, Chromium no longer
clears the selection when clicking non-selectable areas. Clear it manually
on mousedown over the container or <br> (not a text span) so clicking
empty space between lines or outside text deselects, matching native
behavior.
2026-08-12 13:58:02 +08:00

75 lines
2.4 KiB
CSS

/*
* Minimal pdf.js text-layer styles, adapted from pdfjs-dist's viewer CSS.
*
* The pdf.js `TextLayer` class lays out invisible text runs by setting inline
* styles (left/top as percentages, font-family, --font-height, --scale-x,
* --rotate) on `<span>` elements. Without the rules below nothing is
* positioned or sized, so this subset is required for text selection to work.
*
* We render the text layer at viewport scale 1 and transform the whole
* container with the same translate/scale the Konva stage applies, hence
* `--total-scale-factor: 1` (the spans stay in page coordinates).
*/
.pdf-text-layer {
position: absolute;
inset: 0;
overflow: clip;
text-align: initial;
line-height: 1;
letter-spacing: normal;
word-spacing: normal;
transform-origin: 0 0;
caret-color: CanvasText;
/* The container itself must not be selectable: when a drag ends in empty
space Chromium would otherwise target the container as the caret
position and extend the selection across rows. Only the spans are
selectable (see below), so the caret clamps to the nearest text. */
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
/* Above the page canvas (z-0) so the selection highlight is visible, but
below the loading overlay (z-20). */
z-index: 10;
--total-scale-factor: 1;
--scale-round-x: 1px;
--scale-round-y: 1px;
--min-font-size: 1;
--text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
--min-font-size-inv: calc(1 / var(--min-font-size));
}
.pdf-text-layer :is(span, br) {
color: transparent;
position: absolute;
white-space: pre;
cursor: text;
transform-origin: 0% 0%;
-webkit-user-select: text;
-moz-user-select: text;
user-select: text;
}
/* pdf.js appends a <br> after each line-ending text run; without explicit
left/top they sit at the container's top-left (static position) and would
paint a stray selection highlight when a range spans multiple rows. */
.pdf-text-layer br::-moz-selection {
background: transparent;
}
.pdf-text-layer br::selection {
background: transparent;
}
.pdf-text-layer > :not(.markedContent),
.pdf-text-layer .markedContent span:not(.markedContent) {
z-index: 1;
--font-height: 0;
font-size: calc(var(--text-scale-factor) * var(--font-height));
--scale-x: 1;
--rotate: 0deg;
transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}
.pdf-text-layer .markedContent {
display: contents;
}