Skip to content

Commit 552b7e0

Browse files
committed
refactor sequence item to start optimization process
1 parent 18d998a commit 552b7e0

File tree

4 files changed

+241
-145
lines changed

4 files changed

+241
-145
lines changed

src/components/Document/index.js

Lines changed: 21 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import type {
66
Relationship
77
} from "../../types"
88
import { styled } from "@material-ui/styles"
9-
import stringToSequence from "../../string-to-sequence.js"
10-
import Tooltip from "@material-ui/core/Tooltip"
119
import RelationshipArrows from "../RelationshipArrows"
1210
import colors from "../../colors"
1311
import ArrowToMouse from "../ArrowToMouse"
1412
import { useTimeout, useWindowSize } from "react-use"
13+
import SequenceItem from "../SequenceItem"
1514
import classNames from "classnames"
15+
import useEventCallback from "use-event-callback"
1616

1717
const Container = styled("div")(({ relationshipsOn }) => ({
1818
lineHeight: 1.5,
@@ -21,50 +21,6 @@ const Container = styled("div")(({ relationshipsOn }) => ({
2121
flexWrap: "wrap"
2222
}))
2323

24-
const SequenceItem = styled("span")(({ color, relationshipsOn }) => ({
25-
display: "inline-flex",
26-
cursor: "pointer",
27-
backgroundColor: color,
28-
color: "#fff",
29-
padding: 4,
30-
margin: 4,
31-
marginBottom: relationshipsOn ? 64 : 4,
32-
paddingLeft: 10,
33-
paddingRight: 10,
34-
borderRadius: 4,
35-
userSelect: "none",
36-
boxSizing: "border-box",
37-
"&.unlabeled": {
38-
color: "#333",
39-
paddingTop: 4,
40-
paddingBottom: 4,
41-
paddingLeft: 2,
42-
paddingRight: 2,
43-
".notSpace:hover": {
44-
paddingTop: 2,
45-
paddingBottom: 2,
46-
paddingLeft: 0,
47-
paddingRight: 0,
48-
border: `2px dashed #ccc`
49-
}
50-
}
51-
}))
52-
53-
const LabeledText = styled("div")({
54-
display: "inline-flex",
55-
cursor: "pointer",
56-
alignSelf: "center",
57-
fontSize: 11,
58-
width: 18,
59-
height: 18,
60-
alignItems: "center",
61-
justifyContent: "center",
62-
marginLeft: 4,
63-
borderRadius: 9,
64-
color: "#fff",
65-
backgroundColor: "rgba(0,0,0,0.2)"
66-
})
67-
6824
type Props = {
6925
sequence: Array<SequenceItemData>,
7026
relationships: Array<Relationship>,
@@ -126,6 +82,8 @@ export default function Document({
12682
highlightedItems.push(i)
12783
}
12884

85+
const onRemoveLabel = useEventCallback(sequenceItemIndex => {})
86+
12987
return (
13088
<Container
13189
relationshipsOn={Boolean(relationships)}
@@ -142,89 +100,24 @@ export default function Document({
142100
>
143101
{sequence.map((seq, i) => (
144102
<SequenceItem
145-
key={seq.textId || i}
146-
ref={elm => {
147-
if (!elm) return
148-
sequenceItemPositionsRef.current[seq.textId] = {
149-
offset: {
150-
left: elm.offsetLeft,
151-
top: elm.offsetTop,
152-
width: elm.offsetWidth,
153-
height: elm.offsetHeight
154-
}
155-
}
156-
}}
103+
{...seq}
104+
sequenceItemIndex={i}
105+
sequenceItemPositionsRef={sequenceItemPositionsRef}
157106
relationshipsOn={Boolean(relationships)}
158-
onMouseUp={e => {
159-
if (!createRelationshipsMode) return
160-
if (!secondSequenceItem) {
161-
setFirstSequenceItem(null)
162-
setSecondSequenceItem(null)
163-
onCreateEmptyRelationship([firstSequenceItem, seq.textId])
164-
} else {
165-
setFirstSequenceItem(null)
166-
setSecondSequenceItem(null)
167-
}
168-
}}
169-
onMouseDown={() => {
170-
if (createRelationshipsMode) {
171-
if (!firstSequenceItem) {
172-
setFirstSequenceItem(seq.textId)
173-
}
174-
} else {
175-
if (seq.label) return
176-
changeHighlightedRange([i, i])
177-
}
178-
}}
179-
onMouseMove={() => {
180-
if (!mouseDown) return
181-
if (!createRelationshipsMode) {
182-
if (seq.label) return
183-
if (i !== lastSelected) {
184-
changeHighlightedRange([
185-
firstSelected === null ? i : firstSelected,
186-
i
187-
])
188-
}
189-
}
190-
}}
191-
className={classNames(
192-
seq.label ? "label" : "unlabeled",
193-
seq.text.trim().length > 0 && "notSpace"
194-
)}
195-
color={
196-
seq.label
197-
? seq.color || colorLabelMap[seq.label] || "#333"
198-
: !createRelationshipsMode &&
199-
seq.text !== " " &&
200-
highlightedItems.includes(i)
201-
? "#ccc"
202-
: "inherit"
203-
}
204-
key={i}
205-
>
206-
{seq.label ? (
207-
<Tooltip title={seq.label} placement="bottom">
208-
<div>{seq.text}</div>
209-
</Tooltip>
210-
) : (
211-
<div>{seq.text}</div>
212-
)}
213-
{seq.label && !createRelationshipsMode && (
214-
<LabeledText
215-
onClick={e => {
216-
e.stopPropagation()
217-
onSequenceChange(
218-
sequence
219-
.flatMap(s => (s !== seq ? s : stringToSequence(s.text)))
220-
.filter(s => s.text.length > 0)
221-
)
222-
}}
223-
>
224-
<span>{"\u2716"}</span>
225-
</LabeledText>
226-
)}
227-
</SequenceItem>
107+
createRelationshipsMode={createRelationshipsMode}
108+
onChangeFirstSequenceItem={setFirstSequenceItem}
109+
onChangeSecondSequenceItem={setSecondSequenceItem}
110+
onCreateEmptyRelationship={onCreateEmptyRelationship}
111+
onChangeHighlightedRange={changeHighlightedRange}
112+
firstSequenceItem={firstSequenceItem}
113+
secondSequenceItem={secondSequenceItem}
114+
mouseDown={mouseDown}
115+
firstSelected={firstSelected}
116+
lastSelected={lastSelected}
117+
isHighlighted={highlightedItems.includes(i)}
118+
onRemoveLabel={onRemoveLabel}
119+
color={seq.color || colorLabelMap[seq.label]}
120+
/>
228121
))}
229122
{firstSequenceItem && !secondSequenceItem && (
230123
<ArrowToMouse

src/components/Document/index.story.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ storiesOf("Document", module)
1818
Math.random() < 0.9
1919
? { text: text + " " }
2020
: {
21-
text: text + " ",
22-
label:
23-
"somelabel" +
24-
Math.random()
25-
.toString()
26-
.slice(-4),
27-
color: "#9638F9"
28-
}
21+
text: text + " ",
22+
label:
23+
"somelabel" +
24+
Math.random()
25+
.toString()
26+
.slice(-4),
27+
color: "#9638F9"
28+
}
2929
)}
3030
/>
3131
))
@@ -41,15 +41,15 @@ storiesOf("Document", module)
4141
Math.random() < 0.9
4242
? { text: text + " ", textId: `l${i}` }
4343
: {
44-
text: text + " ",
45-
textId: `l${i}`,
46-
label:
47-
"somelabel" +
48-
Math.random()
49-
.toString()
50-
.slice(-4),
51-
color: "#9638F9"
52-
}
44+
text: text + " ",
45+
textId: `l${i}`,
46+
label:
47+
"somelabel" +
48+
Math.random()
49+
.toString()
50+
.slice(-4),
51+
color: "#9638F9"
52+
}
5353
)}
5454
relationships={[
5555
{
@@ -60,3 +60,15 @@ storiesOf("Document", module)
6060
]}
6161
/>
6262
))
63+
.add("Character Sequence", () => (
64+
<Document
65+
onSequenceChange={action("onSequenceChange")}
66+
onHighlightedChanged={action("onHighlightedChanged")}
67+
sequence={`Barack Hussein Obama II (born August 4, 1961) is an American attorney and politician who served as the 44th President of the United States from January 20, 2009, to January 20, 2017. A member of the Democratic Party, he was the first African American to serve as president. He was previously a United States Senator from Illinois and a member of the Illinois State Senate.`
68+
.split("")
69+
.map(c => ({
70+
text: c
71+
}))
72+
}
73+
/>
74+
))

0 commit comments

Comments
 (0)