From 1a3f79375df9099c29e19dae3f4c412ce6b66fd3 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Thu, 17 Oct 2024 11:02:26 +0200 Subject: [PATCH 1/2] Refactoring --- .../src/utils/cancelSingleClickEvents.ts | 2 +- packages/text-annotator/src/utils/index.ts | 6 ++++-- .../text-annotator/src/utils/isNotAnnotatable.ts | 15 +++++++++++++++ .../text-annotator/src/utils/reviveSelector.ts | 2 +- .../src/utils/splitAnnotatableRanges.ts | 16 +--------------- 5 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 packages/text-annotator/src/utils/isNotAnnotatable.ts diff --git a/packages/text-annotator/src/utils/cancelSingleClickEvents.ts b/packages/text-annotator/src/utils/cancelSingleClickEvents.ts index 7d671988..acc4943d 100644 --- a/packages/text-annotator/src/utils/cancelSingleClickEvents.ts +++ b/packages/text-annotator/src/utils/cancelSingleClickEvents.ts @@ -1,4 +1,4 @@ -import { NOT_ANNOTATABLE_SELECTOR } from './splitAnnotatableRanges'; +import { NOT_ANNOTATABLE_SELECTOR } from './isNotAnnotatable'; /** * Calls .preventDefault() on click events in annotable areas, in order diff --git a/packages/text-annotator/src/utils/index.ts b/packages/text-annotator/src/utils/index.ts index c1390635..2105761f 100644 --- a/packages/text-annotator/src/utils/index.ts +++ b/packages/text-annotator/src/utils/index.ts @@ -1,10 +1,12 @@ export * from './cancelSingleClickEvents'; +export * from './cloneEvents'; export * from './device'; export * from './programmaticallyFocusable'; export * from './debounce'; export * from './getQuoteContext'; -export * from './isWhitespaceOrEmpty'; +export * from './isNotAnnotatable'; export * from './isRevived'; +export * from './isWhitespaceOrEmpty'; export * from './mergeClientRects'; export * from './rangeToSelector'; export * from './reviveAnnotation'; @@ -12,5 +14,5 @@ export * from './reviveSelector'; export * from './reviveTarget'; export * from './splitAnnotatableRanges'; export * from './trimRangeToContainer'; -export * from './cloneEvents'; + diff --git a/packages/text-annotator/src/utils/isNotAnnotatable.ts b/packages/text-annotator/src/utils/isNotAnnotatable.ts new file mode 100644 index 00000000..ece9d7bd --- /dev/null +++ b/packages/text-annotator/src/utils/isNotAnnotatable.ts @@ -0,0 +1,15 @@ +export const NOT_ANNOTATABLE_CLASS = 'not-annotatable'; + +export const NOT_ANNOTATABLE_SELECTOR = `.${NOT_ANNOTATABLE_CLASS}`; + +export const isNotAnnotatable = (node: Node): boolean => { + const closestNotAnnotatable = node instanceof HTMLElement + ? node.closest(NOT_ANNOTATABLE_SELECTOR) + : node.parentElement?.closest(NOT_ANNOTATABLE_SELECTOR); + return Boolean(closestNotAnnotatable); +} + +export const isRangeAnnotatable = (range: Range): boolean => { + const ancestor = range.commonAncestorContainer; + return !isNotAnnotatable(ancestor); +} \ No newline at end of file diff --git a/packages/text-annotator/src/utils/reviveSelector.ts b/packages/text-annotator/src/utils/reviveSelector.ts index b3990053..c0ee7756 100644 --- a/packages/text-annotator/src/utils/reviveSelector.ts +++ b/packages/text-annotator/src/utils/reviveSelector.ts @@ -1,5 +1,5 @@ import type { TextSelector } from '../model'; -import { NOT_ANNOTATABLE_SELECTOR } from './splitAnnotatableRanges'; +import { NOT_ANNOTATABLE_SELECTOR } from './isNotAnnotatable'; /** * Creates a new selector object with the revived DOM range from the given text annotation position diff --git a/packages/text-annotator/src/utils/splitAnnotatableRanges.ts b/packages/text-annotator/src/utils/splitAnnotatableRanges.ts index d66aefd0..4269b9e0 100644 --- a/packages/text-annotator/src/utils/splitAnnotatableRanges.ts +++ b/packages/text-annotator/src/utils/splitAnnotatableRanges.ts @@ -1,18 +1,4 @@ -export const NOT_ANNOTATABLE_CLASS = 'not-annotatable'; - -export const NOT_ANNOTATABLE_SELECTOR = `.${NOT_ANNOTATABLE_CLASS}`; - -export const isNotAnnotatable = (node: Node): boolean => { - const closestNotAnnotatable = node instanceof HTMLElement - ? node.closest(NOT_ANNOTATABLE_SELECTOR) - : node.parentElement?.closest(NOT_ANNOTATABLE_SELECTOR); - return Boolean(closestNotAnnotatable); -} - -export const isRangeAnnotatable = (range: Range): boolean => { - const ancestor = range.commonAncestorContainer; - return !isNotAnnotatable(ancestor); -} +import { isRangeAnnotatable, NOT_ANNOTATABLE_CLASS, NOT_ANNOTATABLE_SELECTOR } from './isNotAnnotatable'; const iterateNotAnnotatableElements = function*(range: Range): Generator { const notAnnotatableIterator = document.createNodeIterator( From 69be7ea1b6a8be3dcd4e02e7803179c6a37a4f6d Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Thu, 17 Oct 2024 11:27:04 +0200 Subject: [PATCH 2/2] Fixed shifted annotation offsets after reformatting --- packages/text-annotator/test/model/w3c/fixtures.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/text-annotator/test/model/w3c/fixtures.ts b/packages/text-annotator/test/model/w3c/fixtures.ts index 7ce75045..142fb168 100644 --- a/packages/text-annotator/test/model/w3c/fixtures.ts +++ b/packages/text-annotator/test/model/w3c/fixtures.ts @@ -28,8 +28,8 @@ export const textAnnotation: W3CTextAnnotation = { }, { type: 'TextPositionSelector', - start: 986, - end: 998 + start: 958, + end: 970 }, ] }