Skip to content

Commit e6ef046

Browse files
Added cleanable debounce function
1 parent 2c9fb93 commit e6ef046

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/text-annotator/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
"dependencies": {
4040
"@annotorious/core": "^3.0.0-rc.31",
4141
"colord": "^2.9.3",
42+
"debounce": "^2.1.0",
4243
"dequal": "^2.0.3",
4344
"rbush": "^4.0.0",
4445
"uuid": "^10.0.0"
4546
}
46-
}
47+
}

packages/text-annotator/src/SelectionHandler.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Filter, Origin, type User } from '@annotorious/core';
22
import { v4 as uuidv4 } from 'uuid';
3+
import debounce from 'debounce';
4+
35
import type { TextAnnotatorState } from './state';
46
import type { TextAnnotationTarget } from './model';
57
import {
6-
debounce,
78
splitAnnotatableRanges,
89
rangeToSelector,
910
isWhitespaceOrEmpty,
@@ -82,13 +83,13 @@ export const createSelectionHandler = (
8283

8384
const selectionRange = sel.getRangeAt(0);
8485
if (isWhitespaceOrEmpty(selectionRange)) return;
85-
86+
8687
const annotatableRanges = splitAnnotatableRanges(selectionRange.cloneRange());
8788

8889
const hasChanged =
8990
annotatableRanges.length !== currentTarget.selector.length ||
9091
annotatableRanges.some((r, i) => r.toString() !== currentTarget.selector[i]?.quote);
91-
92+
9293
if (!hasChanged) return;
9394

9495
currentTarget = {
@@ -102,7 +103,7 @@ export const createSelectionHandler = (
102103
} else {
103104
// Proper lifecycle management: clear selection first...
104105
selection.clear();
105-
106+
106107
// ...then add annotation to store...
107108
store.addAnnotation({
108109
id: currentTarget.annotation,
@@ -165,9 +166,14 @@ export const createSelectionHandler = (
165166
document.addEventListener('pointerup', onPointerUp);
166167

167168
const destroy = () => {
169+
currentTarget = undefined;
170+
lastPointerDown = undefined;
171+
172+
onSelectionChange.clear();
173+
168174
container.removeEventListener('selectstart', onSelectStart);
169175
document.removeEventListener('selectionchange', onSelectionChange);
170-
176+
171177
container.removeEventListener('pointerdown', onPointerDown);
172178
document.removeEventListener('pointerup', onPointerUp);
173179
}

packages/text-annotator/src/highlight/baseRenderer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import debounce from 'debounce';
12
import type { Filter, ViewportState } from '@annotorious/core';
3+
24
import type { TextAnnotatorState } from '../state';
3-
import { debounce } from '../utils';
45
import { ViewportBounds, getViewportBounds, trackViewport } from './viewport';
56
import type { HighlightPainter } from './HighlightPainter';
67
import type { Highlight } from './Highlight';
@@ -169,6 +170,7 @@ export const createBaseRenderer = (
169170

170171
document.removeEventListener('scroll', onScroll);
171172

173+
onResize.clear();
172174
window.removeEventListener('resize', onResize);
173175
resizeObserver.disconnect();
174176

packages/text-annotator/src/highlight/canvas/canvasRenderer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import debounce from 'debounce';
12
import type { ViewportState } from '@annotorious/core';
3+
24
import type { TextAnnotatorState } from '../../state';
3-
import { debounce } from '../../utils';
45
import type { ViewportBounds } from '../viewport';
56
import type { HighlightStyle } from '../HighlightStyle';
67
import { DEFAULT_SELECTED_STYLE, DEFAULT_STYLE, HighlightStyleExpression } from '../HighlightStyle';
@@ -116,9 +117,7 @@ const createRenderer = (container: HTMLElement): RendererImplementation => {
116117
});
117118
});
118119

119-
const onResize = debounce(() => {
120-
resetCanvas(canvas);
121-
});
120+
const onResize = debounce(() => resetCanvas(canvas));
122121

123122
window.addEventListener('resize', onResize);
124123

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

131+
onResize.clear();
132132
window.removeEventListener('resize', onResize);
133133
}
134134

packages/text-annotator/src/utils/debounce.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/text-annotator/src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './cancelSingleClickEvents';
2-
export * from './debounce';
32
export * from './getAnnotatableFragment';
43
export * from './getClientRectsPonyfill';
54
export * from './getQuoteContext';

0 commit comments

Comments
 (0)