From 40fb398fc16149ceffcac1b1b601394cc0f86ebf Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Mon, 18 Nov 2024 15:28:40 +0200 Subject: [PATCH] Added `hasTargetQuoteChanged` utility --- .../src/hooks/useAnnotationQuoteIdling.ts | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/text-annotator-react/src/hooks/useAnnotationQuoteIdling.ts b/packages/text-annotator-react/src/hooks/useAnnotationQuoteIdling.ts index 090f31cd..62898c9a 100644 --- a/packages/text-annotator-react/src/hooks/useAnnotationQuoteIdling.ts +++ b/packages/text-annotator-react/src/hooks/useAnnotationQuoteIdling.ts @@ -3,7 +3,7 @@ import { dequal } from 'dequal/lite'; import { Origin, useAnnotationStore } from '@annotorious/react'; import { Ignore, type StoreChangeEvent } from '@annotorious/core'; -import type { TextAnnotation, TextAnnotationStore } from '@recogito/text-annotator'; +import type { TextAnnotation, TextAnnotationStore, TextAnnotationTarget } from '@recogito/text-annotator'; export const useAnnotationQuoteIdling = ( annotationId: string | undefined, @@ -23,29 +23,21 @@ export const useAnnotationQuoteIdling = ( const hasChanged = updated?.some((update) => { const { targetUpdated } = update; - if (!targetUpdated) return false; + if (targetUpdated) { + const { oldTarget, newTarget } = targetUpdated; - const { - oldTarget: { selector: oldSelector }, - newTarget: { selector: newSelector } - } = targetUpdated; - - // The generic type support in the `Update` was added in https://github.com/annotorious/annotorious/pull/476 - - // @ts-expect-error: requires generic `TextSelector` type support - const oldQuotes = oldSelector.map(({ quote }) => quote); - // @ts-expect-error: requires generic `TextSelector` type support - const newQuotes = newSelector.map(({ quote }) => quote); - - return dequal(oldQuotes, newQuotes); + // The generic type support in the `Update` was added in https://github.com/annotorious/annotorious/pull/476 + // @ts-expect-error: requires generic `TextAnnotationTarget` type support + return hasTargetQuoteChanged(oldTarget, newTarget); + } }); - if (!hasChanged) return; + if (hasChanged) { + setIdling(false); - setIdling(false); - - clearTimeout(idlingTimeout); - idlingTimeout = setTimeout(() => setIdling(true), options.timeout); + clearTimeout(idlingTimeout); + idlingTimeout = setTimeout(() => setIdling(true), options.timeout); + } }; store.observe(scheduleSetIdling, { @@ -62,3 +54,13 @@ export const useAnnotationQuoteIdling = ( return isIdling; }; + +const hasTargetQuoteChanged = (oldValue: TextAnnotationTarget, newValue: TextAnnotationTarget) => { + const { selector: oldSelector } = oldValue; + const oldQuotes = oldSelector.map(({ quote }) => quote); + + const { selector: newSelector } = newValue; + const newQuotes = newSelector.map(({ quote }) => quote); + + return !dequal(oldQuotes, newQuotes); +};