Skip to content
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

Begin refactoring mark #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# react-text-annotate

![NPM Version](https://img.shields.io/npm/v/react-text-annotate)

A React component for interactively highlighting parts of text.

## Usage
Expand All @@ -18,9 +20,8 @@ A simple controlled annotation.

```tsx
import {TokenAnnotator, TextAnnotator} from 'react-text-annotate'

<TokenAnnotator
;<TokenAnnotator
tokens={['My', 'text', 'needs', 'annotating', 'for', 'NLP', 'training']}
value={[{start: 5, end: 6, tag: 'TOPIC', color: '#EEE'}]}
/>
```
```
46 changes: 46 additions & 0 deletions docs/Customizing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
name: Customizing Styles
route: /customizing
---

# Customizing Styles

import {Playground, Props} from 'docz'
import {State} from 'react-powerplug'
import {TextAnnotator, TokenAnnotator} from '../src'
import {Card, TEXT, TAG_COLORS} from './Examples'

Each annotator takes a `renderMark` prop which can be used to customize how highlights are rendered.

In the example below, drag or double click to select text. Click on a selected "mark" to deselect.

<Playground>
<State initial={{value: [{start: 18, end: 28, tag: 'PERSON'}], tag: 'PERSON'}}>
{({state, setState}) => (
<Card>
<select onChange={e => setState({tag: e.target.value})} value={state.tag}>
<option value="ORG">ORG</option>
<option value="PERSON">PERSON</option>
</select>
<TextAnnotator
style={{
maxWidth: 500,
lineHeight: 1.5,
}}
content={TEXT}
value={state.value}
onChange={value => setState({value})}
getSpan={span => ({
...span,
tag: state.tag,
color: TAG_COLORS[state.tag],
})}
renderMark={props => <span style={{backgroundColor: 'red'}}>{props.content} [{props.tag}]</span>}
/>
<pre style={{fontSize: 12, lineHeight: 1.2}}>
{JSON.stringify(state, null, 2)}
</pre>
</Card>
)}
</State>
</Playground>
17 changes: 11 additions & 6 deletions src/Mark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ export interface MarkProps {
onClick: (any) => any
}

const Mark: React.SFC<MarkProps> = props => (
export const Mark: React.SFC<MarkProps> = props => (
<span style={{backgroundColor: props.color || '#84d2ff', padding: '0 4px'}}>
{props.content}
{props.tag && (
<span style={{fontSize: '0.7em', fontWeight: 500, marginLeft: 6}}>{props.tag}</span>
)}
</span>
)

export const MarkWrapper = props => (
<mark
style={{backgroundColor: props.color || '#84d2ff', padding: '0 4px'}}
data-start={props.start}
data-end={props.end}
onClick={() => props.onClick({start: props.start, end: props.end})}
>
{props.content}
{props.tag && (
<span style={{fontSize: '0.7em', fontWeight: 500, marginLeft: 6}}>{props.tag}</span>
)}
{props.renderMark ? props.renderMark(props) : <Mark {...props} />}
</mark>
)

Expand Down
19 changes: 15 additions & 4 deletions src/TextAnnotator.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as React from 'react'

import Mark from './Mark'
import Mark, {MarkProps, MarkWrapper} from './Mark'
import {selectionIsEmpty, selectionIsBackwards, splitWithOffsets} from './utils'

const Split = props => {
if (props.mark) return <Mark {...props} />
if (props.mark)
return (
<MarkWrapper {...props}>
{props.renderMark ? props.renderMark(props) : <Mark {...props} />}
</MarkWrapper>
)

return (
<span
Expand All @@ -29,6 +34,7 @@ export interface TextAnnotatorProps extends Omit<React.HTMLAttributes<HTMLDivEle
value: TextSpan[]
onChange: (value: TextSpan[]) => any
getSpan?: (span: TextSpan) => TextSpan
renderMark?: (props: MarkProps) => JSX.Element
// TODO: determine whether to overwrite or leave intersecting ranges.
}

Expand Down Expand Up @@ -92,12 +98,17 @@ class TextAnnotator extends React.Component<TextAnnotatorProps, {}> {
}

render() {
const {content, value, style} = this.props
const {content, value, style, renderMark} = this.props
const splits = splitWithOffsets(content, value)
return (
<div style={style} ref={this.rootRef}>
{splits.map(split => (
<Split key={`${split.start}-${split.end}`} {...split} onClick={this.handleSplitClick} />
<Split
key={`${split.start}-${split.end}`}
{...split}
onClick={this.handleSplitClick}
renderMark={renderMark}
/>
))}
</div>
)
Expand Down
4 changes: 2 additions & 2 deletions src/TokenAnnotator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'

import Mark, {MarkProps} from './Mark'
import {MarkWrapper, MarkProps} from './Mark'
import {selectionIsEmpty, selectionIsBackwards, splitTokensWithOffsets} from './utils'

interface TokenProps {
Expand Down Expand Up @@ -31,7 +31,7 @@ export interface TokenAnnotatorProps
// TODO: When React 16.3 types are ready, remove casts.
class TokenAnnotator extends React.Component<TokenAnnotatorProps, {}> {
static defaultProps = {
renderMark: props => <Mark {...props} />,
renderMark: props => <MarkWrapper {...props} />,
}

rootRef: React.RefObject<HTMLDivElement>
Expand Down