Skip to content

Commit c14ed99

Browse files
committed
Keyboard selection
1 parent b96f04a commit c14ed99

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

packages/text-annotator/src/SelectionHandler.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,23 @@ export const SelectionHandler = (
236236
});
237237
}
238238

239+
const onKeyup = (evt: KeyboardEvent) => {
240+
if (evt.key === 'Shift' && currentTarget) {
241+
// Proper lifecycle management: clear selection first...
242+
selection.clear();
243+
244+
// ...then add annotation to store...
245+
store.addAnnotation({
246+
id: currentTarget.annotation,
247+
bodies: [],
248+
target: currentTarget
249+
});
250+
251+
// ...then make the new annotation the current selection
252+
selection.userSelect(currentTarget.annotation, cloneKeyboardEvent(evt));
253+
}
254+
}
255+
239256
hotkeys(SELECTION_KEYS.join(','), { element: container, keydown: true, keyup: false }, (evt) => {
240257
if (!evt.repeat)
241258
lastDownEvent = cloneKeyboardEvent(evt);
@@ -267,6 +284,7 @@ export const SelectionHandler = (
267284
document.addEventListener('pointerup', onPointerUp);
268285

269286
if (annotatingEnabled) {
287+
container.addEventListener('keyup', onKeyup);
270288
container.addEventListener('selectstart', onSelectStart);
271289
document.addEventListener('selectionchange', onSelectionChange);
272290
}
@@ -275,6 +293,7 @@ export const SelectionHandler = (
275293
container.removeEventListener('pointerdown', onPointerDown);
276294
document.removeEventListener('pointerup', onPointerUp);
277295

296+
container.removeEventListener('keyup', onKeyup);
278297
container.removeEventListener('selectstart', onSelectStart);
279298
document.removeEventListener('selectionchange', onSelectionChange);
280299

0 commit comments

Comments
 (0)