feat: poll to detect if position of excalidraw was updated and allow consumer to disable it

This commit is contained in:
Aakansha Doshi 2021-04-10 03:09:16 +05:30
parent c19c8ecd27
commit bfc8415554
5 changed files with 23 additions and 2 deletions

View file

@ -46,6 +46,7 @@ import {
CURSOR_TYPE,
DEFAULT_UI_OPTIONS,
DEFAULT_VERTICAL_ALIGN,
DETECT_POSITION_CHANGE_INTERVAL,
DRAGGING_THRESHOLD,
ELEMENT_SHIFT_TRANSLATE_AMOUNT,
ELEMENT_TRANSLATE_AMOUNT,
@ -305,6 +306,8 @@ class App extends React.Component<AppProps, AppState> {
private scene: Scene;
private resizeObserver: ResizeObserver | undefined;
private nearestScrollableContainer: HTMLElement | Document | undefined;
private detectPositionChangeIntervalId: NodeJS.Timeout | undefined;
constructor(props: AppProps) {
super(props);
const defaultAppState = getDefaultAppState();
@ -788,6 +791,13 @@ class App extends React.Component<AppProps, AppState> {
this.scene.addCallback(this.onSceneUpdated);
this.addEventListeners();
if (this.props.detectPositionChange) {
this.detectPositionChangeIntervalId = setInterval(
this.updateOffsetsIfChanged,
DETECT_POSITION_CHANGE_INTERVAL,
);
}
if ("ResizeObserver" in window && this.excalidrawContainerRef?.current) {
this.resizeObserver = new ResizeObserver(() => {
// compute isMobile state
@ -829,6 +839,9 @@ class App extends React.Component<AppProps, AppState> {
this.removeEventListeners();
this.scene.destroy();
clearTimeout(touchTimeout);
if (this.detectPositionChangeIntervalId) {
clearInterval(this.detectPositionChangeIntervalId);
}
touchTimeout = 0;
}
@ -1091,7 +1104,7 @@ class App extends React.Component<AppProps, AppState> {
}
}
private onScroll = debounce(() => {
private updateOffsetsIfChanged = () => {
const { offsetTop, offsetLeft } = this.getCanvasOffsets();
this.setState((state) => {
if (state.offsetLeft === offsetLeft && state.offsetTop === offsetTop) {
@ -1099,7 +1112,9 @@ class App extends React.Component<AppProps, AppState> {
}
return { offsetTop, offsetLeft };
});
}, SCROLL_TIMEOUT);
};
private onScroll = debounce(this.updateOffsetsIfChanged, SCROLL_TIMEOUT);
// Copy/paste