├── .gitignore
├── .npmignore
├── .prettierrc
├── LICENSE
├── README.md
├── docs
├── Annotators.mdx
└── Examples.tsx
├── doczrc.js
├── example
├── index.html
├── src
│ ├── App.tsx
│ └── index.tsx
├── tsconfig.json
└── webpack.config.js
├── jest.config.js
├── package.json
├── src
├── Mark.tsx
├── TextAnnotator.tsx
├── TokenAnnotator.tsx
├── __tests__
│ ├── TextAnnotator.test.tsx
│ └── TokenAnnotator.test.tsx
├── index.ts
├── span.ts
└── utils.ts
├── tsconfig.json
└── yarn.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/ignore-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 |
6 | # testing
7 | /coverage
8 |
9 | # production
10 | /dist
11 | /lib
12 | /example/dist
13 |
14 | # misc
15 | .DS_Store
16 | .env
17 | npm-debug.log*
18 | yarn-debug.log*
19 | yarn-error.log*
20 |
21 | .docz
22 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | !/lib
2 | .docz
3 | .vscode
4 | docs
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "semi": false,
4 | "tabWidth": 2,
5 | "trailingComma": "es5",
6 | "printWidth": 100,
7 | "bracketSpacing": false
8 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Martin Camacho
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-text-annotate
2 | [](https://www.npmjs.com/package/react-text-annotate)
3 |
4 | A React component for interactively highlighting parts of text.
5 |
6 | ## Usage
7 |
8 | React `16.8.0` or higher is required as a peer dependency of this package.
9 |
10 | ```
11 | npm install --save react-text-annotate
12 | ```
13 |
14 | [Docs](https://mcamac.github.io/react-text-annotate/)
15 |
16 | ## Examples
17 |
18 | A simple controlled annotation.
19 |
20 | ```tsx
21 | import {TokenAnnotator, TextAnnotator} from 'react-text-annotate'
22 |
23 |
27 | ```
28 |
--------------------------------------------------------------------------------
/docs/Annotators.mdx:
--------------------------------------------------------------------------------
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 |
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 |
33 |
34 | {({state, setState}) => (
35 |
36 |
40 | setState({value})}
48 | getSpan={span => ({
49 | ...span,
50 | tag: state.tag,
51 | color: TAG_COLORS[state.tag],
52 | })}
53 | />
54 |
55 | {JSON.stringify(state, null, 2)}
56 |
57 |
58 | )}
59 |
60 |
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 |
72 |
73 | {({state, setState}) => (
74 |
75 |
79 | setState({value})}
87 | getSpan={span => ({
88 | ...span,
89 | tag: state.tag,
90 | color: TAG_COLORS[state.tag],
91 | })}
92 | />
93 |
94 | {JSON.stringify(state, null, 2)}
95 |
96 |
97 | )}
98 |
99 |
100 |
--------------------------------------------------------------------------------
/docs/Examples.tsx:
--------------------------------------------------------------------------------
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 |
21 | {children}
22 |
23 | )
24 |
--------------------------------------------------------------------------------
/doczrc.js:
--------------------------------------------------------------------------------
1 | export default {
2 | typescript: true,
3 | ignore: ['README.md'],
4 | base: '/react-text-annotate',
5 | }
6 |
--------------------------------------------------------------------------------
/example/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | react-text-annotate
6 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/example/src/App.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import {hot} from 'react-hot-loader'
3 |
4 | import {TextAnnotator, TokenAnnotator} from '../../src'
5 |
6 | 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 . "`
7 |
8 | const TAG_COLORS = {
9 | ORG: '#00ffa2',
10 | PERSON: '#84d2ff',
11 | }
12 |
13 | const Card = ({children}) => (
14 |