/
text_nav.jsx
149 lines (136 loc) · 3.57 KB
/
text_nav.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import PropTypes from 'prop-types'
import React from 'react'
import { flatten, map } from 'lodash'
import colors from '@artsy/reaction/dist/Assets/Colors'
import { IconArtist } from '@artsy/reaction/dist/Components/Publishing/Icon/IconArtist'
import { IconBlockquote } from '@artsy/reaction/dist/Components/Publishing/Icon/IconBlockquote'
import { IconClearFormatting } from '@artsy/reaction/dist/Components/Publishing/Icon/IconClearFormatting'
import { IconLink } from '@artsy/reaction/dist/Components/Publishing/Icon/IconLink'
import { IconOrderedList } from '@artsy/reaction/dist/Components/Publishing/Icon/IconOrderedList'
import { IconUnorderedList } from '@artsy/reaction/dist/Components/Publishing/Icon/IconUnorderedList'
export class TextNav extends React.Component {
onToggle = (e, action) => {
e.preventDefault()
const {
blocks,
makePlainText,
promptForLink,
styles,
toggleBlock,
toggleStyle
} = this.props
if (map(blocks, 'name').includes(action)) {
return toggleBlock(action)
}
if (map(styles, 'name').includes(action)) {
return toggleStyle(action)
}
switch (action) {
case 'artist': {
return promptForLink(action)
}
case 'link': {
return promptForLink()
}
case 'clear-formatting': {
return makePlainText()
}
}
}
getButtonArray() {
const {
blocks,
hasFeatures,
makePlainText,
promptForLink,
styles
} = this.props
const buttons = []
if (styles) {
buttons.push(styles)
}
if (blocks) {
buttons.push(blocks)
}
if (promptForLink) {
buttons.push({ name: 'link' })
}
if (hasFeatures && promptForLink) {
buttons.push({ name: 'artist' })
}
if (makePlainText) {
buttons.push({ name: 'clear-formatting' })
}
return flatten(buttons)
}
getIcon(type) {
const props = { color: colors.grayDark }
switch (type) {
case 'artist': {
return <IconArtist {...props} />
}
case 'blockquote': {
return <IconBlockquote {...props} />
}
case 'link': {
return <IconLink {...props} />
}
case 'ordered-list-item': {
return <IconOrderedList {...props} />
}
case 'clear-formatting': {
return <IconClearFormatting {...props} />
}
case 'unordered-list-item': {
return <IconUnorderedList {...props} />
}
}
}
getNavWidth = () => {
const buttons = this.getButtonArray()
if (buttons.length === 8) {
return '200px'
} else if (buttons.length > 8) {
return '250px'
} else {
return (buttons.length * 50) + 'px'
}
}
render() {
const { top, left } = this.props.position
const buttons = this.getButtonArray()
return (
<div
className='TextNav'
style={{
top: top,
marginLeft: left,
width: this.getNavWidth()
}}
>
{buttons.map((button, i) =>
<button
key={i}
className={button.name.toLowerCase()}
onMouseDown={(e) => this.onToggle(e, button.name)}
>
{button.label
? button.label
: this.getIcon(button.name)
}
</button>
)}
</div>
)
}
}
TextNav.propTypes = {
blocks: PropTypes.array,
hasFeatures: PropTypes.bool,
makePlainText: PropTypes.func,
position: PropTypes.object,
promptForLink: PropTypes.func,
styles: PropTypes.array,
toggleBlock: PropTypes.func,
toggleStyle: PropTypes.func.isRequired
}