-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#172 Added popup hiding when the annotation's target is actively updated #174
Open
oleksandr-danylchenko
wants to merge
13
commits into
recogito:main
Choose a base branch
from
oleksandr-danylchenko:#172-fix-popup-flickering-selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5905be0
Moved `TextAnnotatorPopup` under the `TextAnnotator`
oleksandr-danylchenko c07d8a2
Added not annotatable class constant usage
oleksandr-danylchenko 16a3660
Added `isIdling` state to the `SelectionHandler`
oleksandr-danylchenko 1912741
Revert "Added `isIdling` state to the `SelectionHandler`"
oleksandr-danylchenko dd37d12
Formatted contextmenu comment
oleksandr-danylchenko bc1c352
Added annotation idling timeout before showing the popup
oleksandr-danylchenko 718ebfb
Added hooks re-export
oleksandr-danylchenko dabed48
Merge branch 'main' into #172-fix-popup-flickering-selection
oleksandr-danylchenko 6b19bbc
Added quote comparison for the idling
oleksandr-danylchenko e4af5e5
Added ts warning explanation
oleksandr-danylchenko 40fb398
Added `hasTargetQuoteChanged` utility
oleksandr-danylchenko b854356
Merge branch 'main' into #172-fix-popup-flickering-selection
oleksandr-danylchenko 3e92bd0
Removed redundant `expect-error`
oleksandr-danylchenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'; |
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
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; | ||
|
||
// 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) { | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inspired by the A9S's
hasTargetChange
method, source