Skip to content

Commit 8c4cc1b

Browse files
authored
deps: Remove all webpack/babel and begin switch to docz (#5)
* Remove all webpack/babel and begin switch to docz * Better types for token/text annotator * Bump gh-pages and improve README * Fix docz and add yarn
1 parent 8baa30a commit 8c4cc1b

File tree

12 files changed

+13732
-11728
lines changed

12 files changed

+13732
-11728
lines changed

.babelrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ npm-debug.log*
1818
yarn-debug.log*
1919
yarn-error.log*
2020

21+
.docz

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"singleQuote": true,
33
"semi": false,
4+
"tabWidth": 2,
45
"trailingComma": "es5",
56
"printWidth": 100,
67
"bracketSpacing": false
7-
}
8+
}

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,25 @@
22

33
A React component for interactively highlighting parts of text.
44

5-
[Demo](https://mcamac.github.io/react-text-annotate/)
5+
## Usage
6+
7+
React `16.8.0` or higher is required as a peer dependency of this package.
8+
9+
```
10+
npm install --save react-text-annotate
11+
```
12+
13+
[Docs](https://mcamac.github.io/react-text-annotate/)
14+
15+
## Examples
16+
17+
A simple controlled annotation.
18+
19+
```tsx
20+
import {TokenAnnotator, TextAnnotator} from 'react-text-annotate'
21+
22+
<TokenAnnotator
23+
tokens={['My', 'text', 'needs', 'annotating', 'for', 'NLP', 'training']}
24+
value={[{start: 5, end: 6, tag: 'TOPIC', color: '#EEE'}]}
25+
/>
26+
```

docs/Annotators.mdx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
name: Annotators
3+
route: /annotators
4+
---
5+
6+
# Annotators
7+
8+
import {Playground, Props} from 'docz'
9+
import {State} from 'react-powerplug'
10+
import {TextAnnotator, TokenAnnotator} from '../src'
11+
import {Card, TEXT, TAG_COLORS} from './Examples'
12+
13+
14+
```tsx
15+
import {TokenAnnotator, TextAnnotator} from 'react-text-annotate'
16+
17+
<TokenAnnotator
18+
tokens={['My', 'text', 'needs', 'annotating', 'for', 'NLP', 'training']}
19+
value={[{start: 5, end: 6, tag: 'TOPIC'}]}
20+
/>
21+
```
22+
23+
24+
Note: the examples below use the `State` component from [react-powerplug](https://github.com/renatorib/react-powerplug).
25+
26+
## `TokenAnnotator`
27+
28+
Token annotators take a list of tokens, and allow selecting subranges of them.
29+
30+
In the example below, drag or double click to select text. Click on a selected "mark" to deselect.
31+
32+
<Playground>
33+
<State initial={{value: [{start: 17, end: 19, tag: 'PERSON'}], tag: 'PERSON'}}>
34+
{({state, setState}) => (
35+
<Card>
36+
<select onChange={e => setState({tag: e.target.value})} value={state.tag}>
37+
<option value="ORG">ORG</option>
38+
<option value="PERSON">PERSON</option>
39+
</select>
40+
<TokenAnnotator
41+
style={{
42+
maxWidth: 500,
43+
lineHeight: 1.5,
44+
}}
45+
tokens={TEXT.split(' ')}
46+
value={state.value}
47+
onChange={value => setState({value})}
48+
getSpan={span => ({
49+
...span,
50+
tag: state.tag,
51+
color: TAG_COLORS[state.tag],
52+
})}
53+
/>
54+
<pre style={{fontSize: 12, lineHeight: 1.2}}>
55+
{JSON.stringify(state, null, 2)}
56+
</pre>
57+
</Card>
58+
)}
59+
</State>
60+
</Playground>
61+
62+
63+
64+
## `TextAnnotator`
65+
66+
Text annotators take a string `content` prop, and allow the selecting of substrings.
67+
This can be useful for fine-grained text annotation tasks.
68+
69+
In the example below, drag or double click to select text. Click on a selected "mark" to deselect.
70+
71+
<Playground>
72+
<State initial={{value: [{start: 18, end: 28, tag: 'PERSON'}], tag: 'PERSON'}}>
73+
{({state, setState}) => (
74+
<Card>
75+
<select onChange={e => setState({tag: e.target.value})} value={state.tag}>
76+
<option value="ORG">ORG</option>
77+
<option value="PERSON">PERSON</option>
78+
</select>
79+
<TextAnnotator
80+
style={{
81+
maxWidth: 500,
82+
lineHeight: 1.5,
83+
}}
84+
content={TEXT}
85+
value={state.value}
86+
onChange={value => setState({value})}
87+
getSpan={span => ({
88+
...span,
89+
tag: state.tag,
90+
color: TAG_COLORS[state.tag],
91+
})}
92+
/>
93+
<pre style={{fontSize: 12, lineHeight: 1.2}}>
94+
{JSON.stringify(state, null, 2)}
95+
</pre>
96+
</Card>
97+
)}
98+
</State>
99+
</Playground>

docs/Examples.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, {useState} from 'react'
2+
3+
import {TextAnnotator, TokenAnnotator} from '../src'
4+
5+
export const TEXT = `On Monday night , Mr. Fallon will have a co-host for the first time : The rapper Cardi B , who just released her first album, " Invasion of Privacy . "`
6+
7+
export const TAG_COLORS = {
8+
ORG: '#00ffa2',
9+
PERSON: '#84d2ff',
10+
}
11+
12+
export const Card = ({children}) => (
13+
<div
14+
style={{
15+
boxShadow: '0 2px 4px rgba(0,0,0,.1)',
16+
margin: 6,
17+
maxWidth: 500,
18+
padding: 16,
19+
}}
20+
>
21+
{children}
22+
</div>
23+
)

doczrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
typescript: true,
3+
ignore: ['README.md'],
4+
base: '/react-text-annotate',
5+
}

0 commit comments

Comments
 (0)