Skip to content

Commit

Permalink
Added cleanable debounce function
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-danylchenko committed Aug 28, 2024
1 parent 2c9fb93 commit e6ef046
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/text-annotator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
"dependencies": {
"@annotorious/core": "^3.0.0-rc.31",
"colord": "^2.9.3",
"debounce": "^2.1.0",
"dequal": "^2.0.3",
"rbush": "^4.0.0",
"uuid": "^10.0.0"
}
}
}
16 changes: 11 additions & 5 deletions packages/text-annotator/src/SelectionHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Filter, Origin, type User } from '@annotorious/core';
import { v4 as uuidv4 } from 'uuid';
import debounce from 'debounce';

import type { TextAnnotatorState } from './state';
import type { TextAnnotationTarget } from './model';
import {
debounce,
splitAnnotatableRanges,
rangeToSelector,
isWhitespaceOrEmpty,
Expand Down Expand Up @@ -82,13 +83,13 @@ export const createSelectionHandler = (

const selectionRange = sel.getRangeAt(0);
if (isWhitespaceOrEmpty(selectionRange)) return;

const annotatableRanges = splitAnnotatableRanges(selectionRange.cloneRange());

const hasChanged =
annotatableRanges.length !== currentTarget.selector.length ||
annotatableRanges.some((r, i) => r.toString() !== currentTarget.selector[i]?.quote);

if (!hasChanged) return;

currentTarget = {
Expand All @@ -102,7 +103,7 @@ export const createSelectionHandler = (
} else {
// Proper lifecycle management: clear selection first...
selection.clear();

// ...then add annotation to store...
store.addAnnotation({
id: currentTarget.annotation,
Expand Down Expand Up @@ -165,9 +166,14 @@ export const createSelectionHandler = (
document.addEventListener('pointerup', onPointerUp);

const destroy = () => {
currentTarget = undefined;
lastPointerDown = undefined;

onSelectionChange.clear();

container.removeEventListener('selectstart', onSelectStart);
document.removeEventListener('selectionchange', onSelectionChange);

container.removeEventListener('pointerdown', onPointerDown);
document.removeEventListener('pointerup', onPointerUp);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/text-annotator/src/highlight/baseRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debounce from 'debounce';
import type { Filter, ViewportState } from '@annotorious/core';

import type { TextAnnotatorState } from '../state';
import { debounce } from '../utils';
import { ViewportBounds, getViewportBounds, trackViewport } from './viewport';
import type { HighlightPainter } from './HighlightPainter';
import type { Highlight } from './Highlight';
Expand Down Expand Up @@ -169,6 +170,7 @@ export const createBaseRenderer = (

document.removeEventListener('scroll', onScroll);

onResize.clear();
window.removeEventListener('resize', onResize);
resizeObserver.disconnect();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debounce from 'debounce';
import type { ViewportState } from '@annotorious/core';

import type { TextAnnotatorState } from '../../state';
import { debounce } from '../../utils';
import type { ViewportBounds } from '../viewport';
import type { HighlightStyle } from '../HighlightStyle';
import { DEFAULT_SELECTED_STYLE, DEFAULT_STYLE, HighlightStyleExpression } from '../HighlightStyle';
Expand Down Expand Up @@ -116,9 +117,7 @@ const createRenderer = (container: HTMLElement): RendererImplementation => {
});
});

const onResize = debounce(() => {
resetCanvas(canvas);
});
const onResize = debounce(() => resetCanvas(canvas));

window.addEventListener('resize', onResize);

Expand All @@ -129,6 +128,7 @@ const createRenderer = (container: HTMLElement): RendererImplementation => {
const destroy = () => {
canvas.remove();

onResize.clear();
window.removeEventListener('resize', onResize);
}

Expand Down
8 changes: 0 additions & 8 deletions packages/text-annotator/src/utils/debounce.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/text-annotator/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './cancelSingleClickEvents';
export * from './debounce';
export * from './getAnnotatableFragment';
export * from './getClientRectsPonyfill';
export * from './getQuoteContext';
Expand Down

0 comments on commit e6ef046

Please sign in to comment.