fix offset issue

This commit is contained in:
Peng Xiao
2022-05-31 23:44:59 +08:00
parent 4f0eaf0b7e
commit 49cd96f05b
3 changed files with 16 additions and 6 deletions

View File

@@ -38,5 +38,5 @@
[:div.absolute.w-full.h-full
;; makes sure the whiteboard will not cover the borders
{:key name
:style {:padding "0.5px"}}
:style {:padding "0.5px" :z-index 0}}
(tldraw-app {:file tldr-name})]))

View File

@@ -35,6 +35,15 @@ export function throttle<T extends (...args: any) => any>(
}
}
export function debounce<T extends (...args: any[]) => void>(fn: T, ms = 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let timeoutId: number | any
return function (...args: Parameters<T>) {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => fn.apply(args), ms)
}
}
/** Linear interpolate between two values. */
export function lerp(a: number, b: number, t: number) {
return a + (b - a) * t

View File

@@ -1,5 +1,5 @@
import * as React from 'react'
import type { TLViewport, TLBounds } from '@tldraw/core'
import { TLViewport, TLBounds, debounce } from '@tldraw/core'
const getNearestScrollableContainer = (element: HTMLElement): HTMLElement | Document => {
let parent = element.parentElement
@@ -54,11 +54,12 @@ export function useResizeObserver<T extends HTMLElement>(
React.useEffect(() => {
const scrollingAnchor = ref.current ? getNearestScrollableContainer(ref.current) : document
scrollingAnchor.addEventListener('scroll', updateBounds)
scrollingAnchor.addEventListener('resize', updateBounds)
const debouncedupdateBounds = debounce(updateBounds, 100)
scrollingAnchor.addEventListener('scroll', debouncedupdateBounds)
scrollingAnchor.addEventListener('resize', debouncedupdateBounds)
return () => {
scrollingAnchor.removeEventListener('scroll', updateBounds)
scrollingAnchor.removeEventListener('resize', updateBounds)
scrollingAnchor.removeEventListener('scroll', debouncedupdateBounds)
scrollingAnchor.removeEventListener('resize', debouncedupdateBounds)
}
}, [])