Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/TextAnnotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type TextBaseProps<T> = {
value: T[]
onChange: (value: T[]) => any
getSpan?: (span: TextSpan) => T
// TODO: determine whether to overwrite or leave intersecting ranges.
isIntersectToHighlightedText?: boolean
}

type TextAnnotatorProps<T> = React.HTMLAttributes<HTMLDivElement> & TextBaseProps<T>
Expand Down Expand Up @@ -57,6 +57,13 @@ const TextAnnotator = <T extends Span>(props: TextAnnotatorProps<T>) => {
;[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()
Expand Down
9 changes: 8 additions & 1 deletion src/TokenAnnotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface TokenAnnotatorProps<T>
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 = <T extends Span>(props: TokenAnnotatorProps<T>) => {
Expand Down Expand Up @@ -59,6 +59,13 @@ const TokenAnnotator = <T extends Span>(props: TokenAnnotatorProps<T>) => {
;[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)})])
Expand Down