Files
shumai/packages/webui/components/drawing-canvas.tsx
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

511 lines
15 KiB
TypeScript

import Konva from 'konva'
import type { KonvaEventObject } from 'konva/lib/Node'
import type {
Annotation,
BoxAnnotation,
LineAnnotation,
ArrowAnnotation,
FreehandAnnotation,
} from '@/ui/types'
import { useEffect, useRef } from 'react'
import { Stage, Layer, Image as KonvaImage, Rect, Arrow, Line, Group } from 'react-konva'
import useImage from 'use-image'
import type { DrawingTool } from '@/ui/stores/annotation-store'
interface DrawingCanvasProps {
width: number
height: number
mediaDimensions: { width: number; height: number }
imageUrl?: string
/** A live canvas to draw instead of an image URL (e.g. a PDF.js page render). */
canvasElement?: HTMLCanvasElement | null
videoElement?: HTMLVideoElement
annotations: Annotation[]
scale: number
offset: { x: number; y: number }
onPan?: (newOffset: { x: number; y: number }) => void
onClick?: () => void
className?: string
// Drawing Props
isDrawing?: boolean
currentTool?: DrawingTool
currentColor?: string
onAddAnnotation?: (annotation: Annotation) => void
}
const DrawingCanvas = ({
width,
height,
mediaDimensions,
imageUrl,
canvasElement,
videoElement,
annotations,
scale,
offset,
onPan,
onClick,
className,
isDrawing = false,
currentTool = 'arrow',
currentColor = '#ef4444',
onAddAnnotation,
}: DrawingCanvasProps) => {
const [loadedImage] = useImage(imageUrl || '', 'anonymous')
const imageRef = useRef<Konva.Image>(null)
const isDragging = useRef(false)
const lastPos = useRef({ x: 0, y: 0 })
const startPointRef = useRef<{ x: number; y: number } | null>(null)
// Draft shapes for drawing
const currentLineRef = useRef<Konva.Line>(null)
const currentRectRef = useRef<Konva.Rect>(null)
const currentArrowRef = useRef<Konva.Arrow>(null)
// Force a redraw whenever the live canvas source is swapped, so the layer
// picks up the new page raster (Konva redraws the source each batchDraw).
useEffect(() => {
if (canvasElement && imageRef.current) {
imageRef.current.getLayer()?.batchDraw()
}
}, [canvasElement])
// Handle Video Redraw
useEffect(() => {
if (videoElement && imageRef.current) {
const layer = imageRef.current.getLayer()
if (!layer) return
const anim = new Konva.Animation(() => {
// Force redraw to update video frame
}, layer)
anim.start()
return () => {
anim.stop()
}
}
}, [videoElement])
// -- Helpers --
const getRelativePointerPosition = (
node: Konva.Node,
clamp = false,
customPos?: { x: number; y: number },
) => {
// the function will return pointer position relative to the passed node
const transform = node.getAbsoluteTransform().copy()
// to detect relative position we need to invert transform
transform.invert()
// get pointer (say mouse or touch) position
const pos = customPos || node.getStage()?.getPointerPosition()
if (pos) {
const point = transform.point(pos)
if (clamp) {
return {
x: Math.max(0, Math.min(mediaDimensions.width, point.x)),
y: Math.max(0, Math.min(mediaDimensions.height, point.y)),
}
}
return point
}
return null
}
// Ref to hold current props for global listeners to avoid stale closures
const stateRef = useRef({
isDrawing,
currentTool,
currentColor,
onPan,
offset,
scale,
mediaDimensions,
onAddAnnotation,
})
stateRef.current = {
isDrawing,
currentTool,
currentColor,
onPan,
offset,
scale,
mediaDimensions,
onAddAnnotation,
}
const finalizeDrawing = () => {
if (!isDragging.current) return
isDragging.current = false
const {
isDrawing: activeDrawing,
onAddAnnotation: addAnn,
mediaDimensions: dims,
currentTool: tool,
currentColor: color,
} = stateRef.current
if (activeDrawing && addAnn) {
const w = dims.width
const h = dims.height
// Helper to normalize
const norm = (val: number, dim: number) => val / dim
// Capture shape
if (tool === 'freehand' && currentLineRef.current) {
currentLineRef.current.visible(false)
const rawPoints = currentLineRef.current.points()
const points: [number, number][] = []
for (let i = 0; i < rawPoints.length; i += 2) {
points.push([norm(rawPoints[i], w), norm(rawPoints[i + 1], h)])
}
if (points.length > 1) {
const ann: FreehandAnnotation = {
type: 'freehand',
color,
points,
}
addAnn(ann)
}
} else if (tool === 'line' && currentLineRef.current) {
currentLineRef.current.visible(false)
const pts = currentLineRef.current.points()
const ann: LineAnnotation = {
type: 'line',
color,
points: [
[norm(pts[0], w), norm(pts[1], h)],
[norm(pts[2], w), norm(pts[3], h)],
],
}
addAnn(ann)
} else if (tool === 'box' && currentRectRef.current) {
currentRectRef.current.visible(false)
const x = currentRectRef.current.x()
const y = currentRectRef.current.y()
const boxW = currentRectRef.current.width()
const boxH = currentRectRef.current.height()
// Convert to start/end points
const start: [number, number] = [norm(x, w), norm(y, h)]
const end: [number, number] = [norm(x + boxW, w), norm(y + boxH, h)]
const ann: BoxAnnotation = {
type: 'box',
color,
points: [start, end],
}
addAnn(ann)
} else if (tool === 'arrow' && currentArrowRef.current) {
currentArrowRef.current.visible(false)
const pts = currentArrowRef.current.points()
const ann: ArrowAnnotation = {
type: 'arrow',
color,
points: [
[norm(pts[0], w), norm(pts[1], h)],
[norm(pts[2], w), norm(pts[3], h)],
],
}
addAnn(ann)
}
}
}
useEffect(() => {
const handleGlobalMove = (e: MouseEvent | TouchEvent) => {
if (!isDragging.current) return
const stage = imageRef.current?.getStage()
if (!stage) return
const container = stage.container()
const rect = container.getBoundingClientRect()
let clientX, clientY
if ('clientX' in e) {
clientX = e.clientX
clientY = e.clientY
} else {
clientX = e.touches[0].clientX
clientY = e.touches[0].clientY
}
const stagePos = {
x: clientX - rect.left,
y: clientY - rect.top,
}
// Multi-touch gestures (pinch / two-finger pan) are handled by the
// viewer's gesture layer. Don't apply single-pointer pan/draw while they
// are active, but keep lastPos in sync so a resumed single-finger pan
// doesn't jump.
if ('touches' in e && e.touches.length > 1) {
lastPos.current = stagePos
return
}
const {
isDrawing: activeDrawing,
currentTool: tool,
onPan: panFn,
offset: currentOffset,
} = stateRef.current
if (activeDrawing) {
if (tool === 'select') return
const pos = getRelativePointerPosition(imageRef.current!, true, stagePos)
if (!pos || !startPointRef.current) return
const start = startPointRef.current
const layer = imageRef.current?.getLayer()
if (tool === 'freehand' && currentLineRef.current) {
const newPoints = currentLineRef.current.points().concat([pos.x, pos.y])
currentLineRef.current.points(newPoints)
} else if (tool === 'line' && currentLineRef.current) {
currentLineRef.current.points([start.x, start.y, pos.x, pos.y])
} else if (tool === 'box' && currentRectRef.current) {
currentRectRef.current.width(pos.x - start.x)
currentRectRef.current.height(pos.y - start.y)
} else if (tool === 'arrow' && currentArrowRef.current) {
currentArrowRef.current.points([start.x, start.y, pos.x, pos.y])
}
layer?.batchDraw()
} else {
// Panning
if (!panFn) return
const dx = stagePos.x - lastPos.current.x
const dy = stagePos.y - lastPos.current.y
lastPos.current = stagePos
panFn({
x: currentOffset.x + dx,
y: currentOffset.y + dy,
})
}
}
const handleGlobalUp = (e: MouseEvent | TouchEvent) => {
// Keep dragging while fingers remain down (e.g. a pinch releasing one
// finger); only finalize once every pointer is up.
if ('touches' in e && e.touches.length > 0) return
finalizeDrawing()
}
window.addEventListener('mousemove', handleGlobalMove)
window.addEventListener('mouseup', handleGlobalUp)
window.addEventListener('touchmove', handleGlobalMove)
window.addEventListener('touchend', handleGlobalUp)
return () => {
window.removeEventListener('mousemove', handleGlobalMove)
window.removeEventListener('mouseup', handleGlobalUp)
window.removeEventListener('touchmove', handleGlobalMove)
window.removeEventListener('touchend', handleGlobalUp)
}
}, [])
const handleMouseDown = (e: KonvaEventObject<MouseEvent | TouchEvent>) => {
if (isDrawing) {
// Drawing Logic
if (currentTool === 'select') return
const stage = e.target.getStage()
if (!stage) return
// Use imageRef to get coordinates relative to the image (handling zoom/pan)
const pos = getRelativePointerPosition(imageRef.current!)
if (!pos) return
// Only start drawing if within media bounds
if (
pos.x < 0 ||
pos.x > mediaDimensions.width ||
pos.y < 0 ||
pos.y > mediaDimensions.height
) {
return
}
isDragging.current = true
startPointRef.current = pos
const layer = imageRef.current?.getLayer()
if (!layer) return
if (currentTool === 'freehand' && currentLineRef.current) {
currentLineRef.current.points([pos.x, pos.y])
currentLineRef.current.stroke(currentColor)
currentLineRef.current.visible(true)
} else if (currentTool === 'line' && currentLineRef.current) {
currentLineRef.current.points([pos.x, pos.y, pos.x, pos.y])
currentLineRef.current.stroke(currentColor)
currentLineRef.current.visible(true)
} else if (currentTool === 'box' && currentRectRef.current) {
currentRectRef.current.position(pos)
currentRectRef.current.width(0)
currentRectRef.current.height(0)
currentRectRef.current.stroke(currentColor)
currentRectRef.current.visible(true)
} else if (currentTool === 'arrow' && currentArrowRef.current) {
currentArrowRef.current.points([pos.x, pos.y, pos.x, pos.y])
currentArrowRef.current.stroke(currentColor)
currentArrowRef.current.fill(currentColor)
currentArrowRef.current.visible(true)
}
layer.batchDraw()
} else {
// Panning Logic
if (!onPan) return
isDragging.current = true
const stage = e.target.getStage()
if (stage) {
const pointer = stage.getPointerPosition()
if (pointer) {
lastPos.current = pointer
}
}
}
}
const renderAnnotation = (annotation: Annotation, i: number) => {
const w = mediaDimensions.width
const h = mediaDimensions.height
// We add a key suffix to force re-render if updated? No, index is fine for this list.
// Actually, we might want stable IDs if we were editing existing ones.
// Stroke Width Logic:
// We want the stroke to be visible regardless of zoom.
// If we use `strokeScaleEnabled={false}`, Konva handles it!
// Wait, `strokeScaleEnabled` defaults to true.
// If false, stroke width is in screen pixels.
// Let's try `strokeScaleEnabled={false}` and `strokeWidth={2}`.
switch (annotation.type) {
case 'box': {
const [start, end] = annotation.points
const x = Math.min(start[0], end[0]) * w
const y = Math.min(start[1], end[1]) * h
const boxWidth = Math.abs(end[0] - start[0]) * w
const boxHeight = Math.abs(end[1] - start[1]) * h
return (
<Rect
key={i}
x={x}
y={y}
width={boxWidth}
height={boxHeight}
stroke={annotation.color}
strokeWidth={2.6 / scale}
listening={false}
/>
)
}
case 'arrow': {
const points = annotation.points.flat().map((p, idx) => (idx % 2 === 0 ? p * w : p * h))
return (
<Arrow
key={i}
points={points}
stroke={annotation.color}
strokeWidth={2.6 / scale}
fill={annotation.color}
pointerLength={10 / scale}
pointerWidth={10 / scale}
listening={false}
/>
)
}
case 'line':
case 'freehand': {
const points = annotation.points.flat().map((p, idx) => (idx % 2 === 0 ? p * w : p * h))
return (
<Line
key={i}
points={points}
stroke={annotation.color}
strokeWidth={2.6 / scale}
tension={0.5}
lineCap="round"
lineJoin="round"
listening={false}
/>
)
}
default:
return null
}
}
return (
<div
className={className}
style={{
width,
height,
overflow: 'hidden',
pointerEvents: !isDrawing && !onPan ? 'none' : 'auto',
cursor:
isDrawing && currentTool !== 'select'
? 'crosshair'
: onPan
? isDragging.current
? 'grabbing'
: 'grab'
: 'default',
}}
>
<Stage
width={width}
height={height}
scaleX={scale}
scaleY={scale}
x={offset.x}
y={offset.y}
onMouseDown={handleMouseDown}
onClick={!isDrawing ? onClick : undefined}
onTap={!isDrawing ? onClick : undefined}
onTouchStart={handleMouseDown}
>
<Layer>
<KonvaImage
ref={imageRef}
image={videoElement || canvasElement || loadedImage}
width={mediaDimensions.width}
height={mediaDimensions.height}
/>
{annotations.map(renderAnnotation)}
{/* Draft Shapes */}
<Group listening={false}>
<Line
ref={currentLineRef}
strokeWidth={2.6 / scale}
lineCap="round"
lineJoin="round"
visible={false}
/>
<Rect ref={currentRectRef} strokeWidth={2.6 / scale} visible={false} />
<Arrow
ref={currentArrowRef}
points={[]}
strokeWidth={2.6 / scale}
pointerLength={10 / scale}
pointerWidth={10 / scale}
visible={false}
/>
</Group>
</Layer>
</Stage>
</div>
)
}
export default DrawingCanvas