-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): support preview grounding annotations
- Loading branch information
Showing
8 changed files
with
178 additions
and
10 deletions.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
packages/components/src/Annotator/components/HighlightText/index.less
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,5 @@ | ||
.dds-annotator-highlight-text .ant-tag { | ||
margin-inline-end: 0px !important; | ||
font-size: 14px; | ||
cursor: pointer; | ||
} |
73 changes: 73 additions & 0 deletions
73
packages/components/src/Annotator/components/HighlightText/index.tsx
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,73 @@ | ||
import React, { useMemo } from 'react'; | ||
import { escapeRegExp } from 'lodash'; | ||
import { Tag } from 'antd'; | ||
|
||
import './index.less'; | ||
|
||
interface IHighlight { | ||
text: string; | ||
color: string; | ||
} | ||
|
||
interface IProps { | ||
text: string; | ||
highlights: IHighlight[]; | ||
onHoverHighlightWord: (text: string) => void; | ||
onLeaveHighlightWord: () => void; | ||
} | ||
|
||
const HighlightText: React.FC<IProps> = ({ | ||
text, | ||
highlights, | ||
onHoverHighlightWord, | ||
onLeaveHighlightWord, | ||
}) => { | ||
|
||
const segments = useMemo(() => { | ||
const computedSegments: React.ReactNode[] = []; | ||
const pattern = new RegExp( | ||
highlights.map(h => `\\b(${escapeRegExp(h.text)})\\b`).join('|'), | ||
'g' | ||
); | ||
|
||
const matches = Array.from(text.matchAll(pattern)); | ||
let lastIndex = 0; | ||
|
||
matches.forEach(match => { | ||
const matchText = match[0]; | ||
const index = match.index ?? 0; | ||
|
||
if (index > lastIndex) { | ||
computedSegments.push(text.substring(lastIndex, index)); | ||
} | ||
|
||
const highlightConfig = highlights.find(h => h.text === matchText); | ||
|
||
if (highlightConfig) { | ||
computedSegments.push( | ||
<Tag | ||
key={`${index}-${matchText}`} | ||
color={highlightConfig.color} | ||
bordered={false} | ||
onMouseEnter={() => onHoverHighlightWord(matchText)} | ||
onMouseLeave={onLeaveHighlightWord} | ||
> | ||
{matchText} | ||
</Tag> | ||
); | ||
} | ||
|
||
lastIndex = index + matchText.length; | ||
}); | ||
|
||
if (lastIndex < text.length) { | ||
computedSegments.push(text.substring(lastIndex)); | ||
} | ||
|
||
return computedSegments; | ||
}, [text, highlights, onHoverHighlightWord, onLeaveHighlightWord]); | ||
|
||
return <div className={'dds-annotator-highlight-text'}>{segments}</div>; | ||
}; | ||
|
||
export default HighlightText; |
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
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