Skip to content

Commit

Permalink
Merge branch 'recogito#127-annotating-enabled-reactive' into staging
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/text-annotator/src/SelectionHandler.ts
#	packages/text-annotator/src/utils/index.ts
  • Loading branch information
oleksandr-danylchenko committed Aug 30, 2024
2 parents 16793eb + c0dc156 commit 7ced549
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/text-annotator/src/SelectionHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Filter, Origin, type Selection, type User } from '@annotorious/core';
import { v4 as uuidv4 } from 'uuid';
import debounce from 'debounce';
import hotkeys from 'hotkeys-js';

import type { TextAnnotatorState } from './state';
import type { TextAnnotationTarget } from './model';
import {
debounce,
clonePointerEvent,
cloneKeyboardEvent,
splitAnnotatableRanges,
Expand Down Expand Up @@ -147,7 +147,7 @@ export const createSelectionHandler = (
// ...then make the new annotation the current selection
selection.userSelect(currentTarget.annotation, lastDownEvent);
}
}, 10).bind(undefined);
});

document.addEventListener('selectionchange', onSelectionChange);

Expand Down
7 changes: 3 additions & 4 deletions packages/text-annotator/src/highlight/baseRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import debounce from 'debounce';
import type { Filter, ViewportState } from '@annotorious/core';

import type { TextAnnotatorState } from '../state';
import { ViewportBounds, getViewportBounds, trackViewport } from './viewport';
import type { HighlightPainter } from './HighlightPainter';
import type { Highlight } from './Highlight';
import type { HighlightStyleExpression } from './HighlightStyle';
import { debounce } from '../utils';

export interface RendererImplementation {

Expand Down Expand Up @@ -136,11 +136,10 @@ export const createBaseRenderer = (
const onResize = debounce(() => {
store.recalculatePositions();

if (currentPainter)
currentPainter.reset();
currentPainter?.reset();

redraw();
}, 10).bind(undefined);
});

window.addEventListener('resize', onResize);

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

import type { TextAnnotatorState } from '../../state';
Expand All @@ -8,6 +7,7 @@ import { DEFAULT_SELECTED_STYLE, DEFAULT_STYLE, HighlightStyleExpression } from
import type { HighlightPainter } from '../HighlightPainter';
import { createBaseRenderer, type RendererImplementation } from '../baseRenderer';
import type { Highlight } from '../Highlight';
import { debounce } from '../../utils';

import './canvasRenderer.css';

Expand Down Expand Up @@ -117,7 +117,7 @@ const createRenderer = (container: HTMLElement): RendererImplementation => {
});
});

const onResize = debounce(() => resetCanvas(canvas), 10).bind(undefined);
const onResize = debounce(() => resetCanvas(canvas));

window.addEventListener('resize', onResize);

Expand Down
22 changes: 22 additions & 0 deletions packages/text-annotator/src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import libDebounce from 'debounce';

/**
* Wraps the `debounce` function from the `debounce` package
* to make it context-agnostic.
* Otherwise, we won't be able to use it in multiple event listeners simultaneously,
* like `window.onresize` and `ResizeObserver`.
* @see https://github.com/sindresorhus/debounce/issues/8#issuecomment-2321341074
*/
export const debounce: typeof libDebounce = (function_, wait = 10, options) => {
const fn = libDebounce(function_, wait, options);

const boundFn = fn.bind(undefined);

Object.getOwnPropertyNames(fn).forEach(
prop => Object.defineProperty(boundFn, prop, Object.getOwnPropertyDescriptor(fn, prop))
);
const proto = Object.getPrototypeOf(fn);
Object.setPrototypeOf(boundFn, proto);

return boundFn;
}
1 change: 1 addition & 0 deletions packages/text-annotator/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './reviveSelector';
export * from './reviveTarget';
export * from './splitAnnotatableRanges';
export * from './trimRangeToContainer';
export * from './debounce';
export * from './normalizeRects';
export * from './cloneEvents';

0 comments on commit 7ced549

Please sign in to comment.