Files
upstream-tldraw/packages/tldraw/src/lib/overlays/CollaboratorHintOverlayUtil.ts
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

97 lines
2.8 KiB
TypeScript

import { OverlayUtil, TLOverlay, Vec, clamp, isCursorInViewport } from '@tldraw/editor'
/** @public */
export interface TLCollaboratorHintOverlay extends TLOverlay {
props: {
/** Clamped point on viewport edge, in page coordinates */
x: number
y: number
/** Rotation angle pointing toward the collaborator's actual cursor */
rotation: number
color: string
}
}
// Lazy-init a Path2D for the hint arrow shape to avoid per-frame path building
let _arrowPath: Path2D | null = null
function getArrowPath() {
if (!_arrowPath) {
// Matches the path drawn via: M -2,-5 2,0 -2,5 Z
_arrowPath = new Path2D('M -2 -5 L 2 0 L -2 5 Z')
}
return _arrowPath!
}
/**
* Overlay util for off-screen collaborator cursor hints.
* Shows a small directional arrow at the viewport edge pointing toward the collaborator.
*
* @public
*/
export class CollaboratorHintOverlayUtil extends OverlayUtil<TLCollaboratorHintOverlay> {
static override type = 'collaborator_hint'
override options = { zIndex: 900, lineWidth: 3, viewportPadding: 5 }
override isActive(): boolean {
const viewport = this.editor.getViewportPageBounds()
const zoom = this.editor.getZoomLevel()
return this.editor.getVisibleCollaboratorsOnCurrentPage().some((presence) => {
const { cursor } = presence
if (!cursor) return false
return !isCursorInViewport(cursor, viewport, zoom)
})
}
override getOverlays(): TLCollaboratorHintOverlay[] {
const overlays: TLCollaboratorHintOverlay[] = []
const viewport = this.editor.getViewportPageBounds()
const zoom = this.editor.getZoomLevel()
for (const presence of this.editor.getVisibleCollaboratorsOnCurrentPage()) {
const { cursor, color, userId } = presence
if (!cursor) continue
if (isCursorInViewport(cursor, viewport, zoom)) continue
const pad = this.options.viewportPadding / zoom
const x = clamp(cursor.x, viewport.minX + pad, viewport.maxX - pad)
const y = clamp(cursor.y, viewport.minY + pad, viewport.maxY - pad)
const rotation = Vec.Angle(viewport.center, cursor)
overlays.push({
id: `collaborator_hint:${userId}`,
type: 'collaborator_hint',
props: { x, y, rotation, color },
})
}
return overlays
}
override render(ctx: CanvasRenderingContext2D, overlays: TLCollaboratorHintOverlay[]): void {
const zoom = this.editor.getZoomLevel()
const scale = 1 / zoom
for (const overlay of overlays) {
const { x, y, rotation, color } = overlay.props
ctx.save()
ctx.translate(x, y)
ctx.scale(scale, scale)
ctx.rotate(rotation)
const path = getArrowPath()
// Outline stroke (white background for contrast)
ctx.lineWidth = this.options.lineWidth
// Canvas doesn't support CSS vars in strokeStyle; use white
ctx.strokeStyle = '#ffffff'
ctx.stroke(path)
// Fill with collaborator color
ctx.fillStyle = color
ctx.fill(path)
ctx.restore()
}
}
}