Skip to content

Commit 92d7c78

Browse files
committed
Move example to its own folder
1 parent 09696d7 commit 92d7c78

File tree

11 files changed

+86
-35
lines changed

11 files changed

+86
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# production
1010
/dist
11+
/lib
1112

1213
# misc
1314
.DS_Store
File renamed without changes.

src/App.tsx renamed to example/src/App.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import * as React from 'react'
22
import {hot} from 'react-hot-loader'
33

4-
import TextAnnotator from './TextAnnotator'
5-
6-
const Home = () => (
7-
<div>
8-
<div>
9-
<h2>Welcome to React</h2>
10-
</div>
11-
<p>HMR works!</p>
12-
</div>
13-
)
4+
import TextAnnotator from '../../src'
145

156
const TEXT = `On Friday, former President Park Geun-hye joined their number as a court sentenced her to 24 years, more than a year after she was impeached and removed from office over an influence-peddling scandal.`
167

178
class App extends React.Component<any, any> {
189
state = {
19-
// value: [{start: 3, end: 9}],
20-
value: [],
10+
value: [{start: 28, end: 41}],
2111
}
2212

2313
handleChange = value => {

src/index.tsx renamed to example/src/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import * as ReactDOM from 'react-dom'
33

44
import App from './App'
55

6-
import './index.css'
7-
86
const render = Component => {
97
ReactDOM.render(<Component />, document.getElementById('root'))
108
}

example/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
// "noImplicitAny": truowe,
5+
"sourceMap": true,
6+
"jsx": "react",
7+
"lib": ["es2016", "dom"]
8+
},
9+
"include": ["src/**/*"]
10+
}
File renamed without changes.

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
2-
"name": "frontend",
2+
"name": "react-annotate-text",
33
"version": "0.1.0",
4-
"private": true,
4+
"main": "./lib/index.js",
5+
"types": "./lib/index.d.ts",
56
"dependencies": {
67
"@types/react": "^16.3.1",
78
"@types/react-dom": "^16.0.4",
@@ -30,7 +31,9 @@
3031
"webpack-dev-server": "^3.1.1"
3132
},
3233
"scripts": {
33-
"dev": "webpack-dev-server --hot --history-api-fallback --mode development",
34-
"build": "NODE_ENV=production webpack --mode production"
34+
"dev":
35+
"cd example && webpack-dev-server --hot --history-api-fallback --mode development",
36+
"build": "NODE_ENV=production webpack --mode production",
37+
"prepublishOnly": "tsc -p ./ --declaration --outDir lib/"
3538
}
3639
}

src/TextAnnotator.tsx

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,34 @@ const splitWithOffsets = (text, offsets: {start: number; end: number}[]) => {
3232
return splits
3333
}
3434

35+
const Mark = props => (
36+
<mark
37+
style={{backgroundColor: props.mark ? '#84d2ff' : null, padding: '0 4px'}}
38+
data-start={props.start}
39+
data-end={props.end}
40+
onClick={() => props.onClick({start: props.start, end: props.end})}
41+
>
42+
{props.content}
43+
<span style={{fontSize: '0.7em', fontWeight: 500, marginLeft: 6}}>
44+
PERSON
45+
</span>
46+
</mark>
47+
)
48+
49+
const Split = props => {
50+
if (props.mark) return <Mark {...props} />
51+
52+
return (
53+
<span
54+
data-start={props.start}
55+
data-end={props.end}
56+
onClick={() => props.onClick({start: props.start, end: props.end})}
57+
>
58+
{props.content}
59+
</span>
60+
)
61+
}
62+
3563
export interface TextAnnotatorProps {
3664
style: object
3765
content: string
@@ -61,36 +89,57 @@ class TextAnnotator extends React.Component<TextAnnotatorProps, {}> {
6189

6290
handleMouseUp = () => {
6391
const selection = window.getSelection()
92+
93+
let position = selection.anchorNode.compareDocumentPosition(
94+
selection.focusNode
95+
)
96+
if (position === 0 && selection.focusOffset === selection.anchorOffset)
97+
return
98+
let backward = false
99+
// position == 0 if nodes are the same
100+
if (
101+
(!position && selection.anchorOffset > selection.focusOffset) ||
102+
position === Node.DOCUMENT_POSITION_PRECEDING
103+
)
104+
backward = true
105+
64106
console.log(selection)
65-
const start =
107+
108+
let start =
66109
parseInt(
67110
selection.anchorNode.parentElement.getAttribute('data-start'),
68111
10
69112
) + selection.anchorOffset
70-
const end =
113+
let end =
71114
parseInt(
72115
selection.focusNode.parentElement.getAttribute('data-start'),
73116
10
74117
) + selection.focusOffset
118+
119+
if (backward) {
120+
;[start, end] = [end, start]
121+
}
122+
75123
console.log(start, end)
76124
this.props.onChange(this.props.value.concat({start, end}))
77125
window.getSelection().empty()
78126
}
79127

128+
handleSplitClick = ({start, end}) => {
129+
console.log('click', start, end)
130+
}
131+
80132
render() {
81133
const {content, value, style} = this.props
82134
const splits = splitWithOffsets(content, value)
83135
return (
84136
<div style={style} ref={this.rootRef}>
85137
{splits.map(split => (
86-
<span
87-
style={{backgroundColor: split.mark ? '#84d2ff' : null}}
138+
<Split
88139
key={`${split.start}-${split.end}`}
89-
data-start={split.start}
90-
data-end={split.end}
91-
>
92-
{split.content}
93-
</span>
140+
{...split}
141+
onClick={this.handleSplitClick}
142+
/>
94143
))}
95144
</div>
96145
)

src/index.css

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

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import TextAnnotator from './TextAnnotator'
2+
3+
export default TextAnnotator

0 commit comments

Comments
 (0)