forked from recogito/text-annotator-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'recogito#172-fix-popup-flickering-selection' into stagi…
…ng-merge-candidate
- Loading branch information
Showing
8 changed files
with
93 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useAnnotationQuoteIdling } from './useAnnotationQuoteIdling'; |
63 changes: 63 additions & 0 deletions
63
packages/text-annotator-react/src/hooks/useAnnotationQuoteIdling.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { dequal } from 'dequal/lite'; | ||
|
||
import { Origin, useAnnotationStore } from '@annotorious/react'; | ||
import { Ignore, type StoreChangeEvent } from '@annotorious/core'; | ||
import type { TextAnnotation, TextAnnotationStore, TextAnnotationTarget } from '@recogito/text-annotator'; | ||
|
||
export const useAnnotationQuoteIdling = ( | ||
annotationId: string | undefined, | ||
options: { timeout: number } = { timeout: 500 } | ||
) => { | ||
const store = useAnnotationStore<TextAnnotationStore>(); | ||
|
||
const [isIdling, setIdling] = useState(true); | ||
|
||
useEffect(() => { | ||
if (!store || !annotationId) return; | ||
|
||
let idlingTimeout: ReturnType<typeof setTimeout>; | ||
|
||
const scheduleSetIdling = (event: StoreChangeEvent<TextAnnotation>) => { | ||
const { changes: { updated } } = event; | ||
|
||
const hasChanged = updated?.some((update) => { | ||
const { targetUpdated } = update; | ||
if (targetUpdated) { | ||
const { oldTarget, newTarget } = targetUpdated; | ||
return hasTargetQuoteChanged(oldTarget, newTarget); | ||
} | ||
}); | ||
|
||
if (hasChanged) { | ||
setIdling(false); | ||
|
||
clearTimeout(idlingTimeout); | ||
idlingTimeout = setTimeout(() => setIdling(true), options.timeout); | ||
} | ||
}; | ||
|
||
store.observe(scheduleSetIdling, { | ||
annotations: annotationId, | ||
ignore: Ignore.BODY_ONLY, | ||
origin: Origin.LOCAL | ||
}); | ||
|
||
return () => { | ||
clearTimeout(idlingTimeout); | ||
store.unobserve(scheduleSetIdling); | ||
}; | ||
}, [store, annotationId, options.timeout]); | ||
|
||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters