Skip to content

Commit f0df3d4

Browse files
committed
fix: avoid registering text click listeners multiple times
1 parent c4631e8 commit f0df3d4

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/components/ContentView.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ const contentStyle = computed(() => ({
5656
fontSize: `${props.fontSize}px`,
5757
}));
5858
59+
let clickListener = null
60+
5961
watch(
6062
() => props.url,
6163
loadContent,
6264
{ immediate: true },
6365
);
66+
6467
async function loadContent(url) {
6568
const annotationStore = useAnnotationsStore();
6669
content.value = '';
@@ -82,7 +85,9 @@ async function loadContent(url) {
8285
8386
const root = textContainer.value;
8487
annotationStore.addHighlightAttributesToText(root);
85-
await annotationStore.addHighlightClickListeners(root);
88+
89+
if (clickListener) root.removeEventListener('click', clickListener);
90+
clickListener = annotationStore.addHighlightClickListeners(root);
8691
8792
// In case of multiple text panels, we need to pass the panel index to track changes on the content in the annotation panel.
8893
contentStore.setActiveContentMap(props.url, props.panelIndex);

src/stores/annotations.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ export const useAnnotationsStore = defineStore('annotations', () => {
328328

329329
const addHighlightClickListeners = (root: HTMLElement) => {
330330
if (!root) return;
331-
root.addEventListener('click', ({target}) => {
331+
332+
const listener = ({target}) => {
332333
// The click event handler works like this:
333334
// When clicking on the text we pick the whole part of the text which belongs to the highest parent annotation.
334335
// Since the annotations can be nested we avoid handling each of them separately
@@ -346,7 +347,11 @@ export const useAnnotationsStore = defineStore('annotations', () => {
346347
return;
347348
}
348349
TextEventBus.emit('click', { target })
349-
});
350+
}
351+
352+
root.addEventListener('click', listener);
353+
354+
return listener
350355
};
351356

352357
function getNearestParentAnnotation(element) {

0 commit comments

Comments
 (0)