From e00e7680ce9dbb1837c003ad2e7864f43034ae47 Mon Sep 17 00:00:00 2001 From: Vince Llauderes Date: Sat, 30 Jan 2021 18:33:03 +0800 Subject: [PATCH] prevent intersection to highlighted text --- src/TextAnnotator.tsx | 9 ++++++++- src/TokenAnnotator.tsx | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/TextAnnotator.tsx b/src/TextAnnotator.tsx index e8f7d74..5c633cc 100644 --- a/src/TextAnnotator.tsx +++ b/src/TextAnnotator.tsx @@ -27,7 +27,7 @@ type TextBaseProps = { value: T[] onChange: (value: T[]) => any getSpan?: (span: TextSpan) => T - // TODO: determine whether to overwrite or leave intersecting ranges. + isIntersectToHighlightedText?: boolean } type TextAnnotatorProps = React.HTMLAttributes & TextBaseProps @@ -57,6 +57,13 @@ const TextAnnotator = (props: TextAnnotatorProps) => { ;[start, end] = [end, start] } + if (props.isIntersectToHighlightedText) { + const splitIndex = props.value.findIndex(s => s.start === start && s.end === end) + if (splitIndex >= 0) { + return + } + } + props.onChange([...props.value, getSpan({start, end, text: content.slice(start, end)})]) window.getSelection().empty() diff --git a/src/TokenAnnotator.tsx b/src/TokenAnnotator.tsx index 5055af5..c7f8c05 100644 --- a/src/TokenAnnotator.tsx +++ b/src/TokenAnnotator.tsx @@ -26,7 +26,7 @@ export interface TokenAnnotatorProps onChange: (value: T[]) => any getSpan?: (span: TokenSpan) => T renderMark?: (props: MarkProps) => JSX.Element - // TODO: determine whether to overwrite or leave intersecting ranges. + isIntersectToHighlightedText?: boolean } const TokenAnnotator = (props: TokenAnnotatorProps) => { @@ -59,6 +59,13 @@ const TokenAnnotator = (props: TokenAnnotatorProps) => { ;[start, end] = [end, start] } + if (props.isIntersectToHighlightedText) { + const splitIndex = props.value.findIndex(s => s.start >= start && s.end <= end) + if (splitIndex >= 0) { + return + } + } + end += 1 props.onChange([...props.value, getSpan({start, end, tokens: props.tokens.slice(start, end)})])