forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
208 lines (176 loc) · 3.8 KB
/
index.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { Editor, getEventTransfer } from 'slate-react'
import { Value } from 'slate'
import React from 'react'
import initialValue from './value.json'
import isUrl from 'is-url'
/**
* A change helper to standardize wrapping links.
*
* @param {Change} change
* @param {String} href
*/
function wrapLink(change, href) {
change.wrapInline({
type: 'link',
data: { href },
})
change.collapseToEnd()
}
/**
* A change helper to standardize unwrapping links.
*
* @param {Change} change
*/
function unwrapLink(change) {
change.unwrapInline('link')
}
/**
* The links example.
*
* @type {Component}
*/
class Links extends React.Component {
/**
* Deserialize the raw initial value.
*
* @type {Object}
*/
state = {
value: Value.fromJSON(initialValue),
}
/**
* Check whether the current selection has a link in it.
*
* @return {Boolean} hasLinks
*/
hasLinks = () => {
const { value } = this.state
return value.inlines.some(inline => inline.type == 'link')
}
/**
* On change.
*
* @param {Change} change
*/
onChange = ({ value }) => {
this.setState({ value })
}
/**
* When clicking a link, if the selection has a link in it, remove the link.
* Otherwise, add a new link with an href and text.
*
* @param {Event} event
*/
onClickLink = event => {
event.preventDefault()
const { value } = this.state
const hasLinks = this.hasLinks()
const change = value.change()
if (hasLinks) {
change.call(unwrapLink)
} else if (value.isExpanded) {
const href = window.prompt('Enter the URL of the link:')
change.call(wrapLink, href)
} else {
const href = window.prompt('Enter the URL of the link:')
const text = window.prompt('Enter the text for the link:')
change
.insertText(text)
.extend(0 - text.length)
.call(wrapLink, href)
}
this.onChange(change)
}
/**
* On paste, if the text is a link, wrap the selection in a link.
*
* @param {Event} event
* @param {Change} change
*/
onPaste = (event, change) => {
if (change.value.isCollapsed) return
const transfer = getEventTransfer(event)
const { type, text } = transfer
if (type != 'text' && type != 'html') return
if (!isUrl(text)) return
if (this.hasLinks()) {
change.call(unwrapLink)
}
change.call(wrapLink, text)
return true
}
/**
* Render the app.
*
* @return {Element} element
*/
render() {
return (
<div>
{this.renderToolbar()}
{this.renderEditor()}
</div>
)
}
/**
* Render the toolbar.
*
* @return {Element} element
*/
renderToolbar = () => {
const hasLinks = this.hasLinks()
return (
<div className="menu toolbar-menu">
<span
className="button"
onMouseDown={this.onClickLink}
data-active={hasLinks}
>
<span className="material-icons">link</span>
</span>
</div>
)
}
/**
* Render the editor.
*
* @return {Element} element
*/
renderEditor = () => {
return (
<div className="editor">
<Editor
placeholder="Enter some text..."
value={this.state.value}
onChange={this.onChange}
onPaste={this.onPaste}
renderNode={this.renderNode}
/>
</div>
)
}
/**
* Render a Slate node.
*
* @param {Object} props
* @return {Element}
*/
renderNode = props => {
const { attributes, children, node } = props
switch (node.type) {
case 'link': {
const { data } = node
const href = data.get('href')
return (
<a {...attributes} href={href}>
{children}
</a>
)
}
}
}
}
/**
* Export.
*/
export default Links