forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (172 loc) · 3.85 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
209
210
import { Editor, getEventTransfer } from 'slate-react'
import { Value } from 'slate'
import React from 'react'
import initialValue from './value.json'
import isUrl from 'is-url'
import { Button, Icon, Toolbar } from '../components'
/**
* A change helper to standardize wrapping links.
*
* @param {Editor} editor
* @param {String} href
*/
function wrapLink(editor, href) {
editor.wrapInline({
type: 'link',
data: { href },
})
editor.moveToEnd()
}
/**
* A change helper to standardize unwrapping links.
*
* @param {Editor} editor
*/
function unwrapLink(editor) {
editor.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')
}
/**
* Store a reference to the `editor`.
*
* @param {Editor} editor
*/
ref = editor => {
this.editor = editor
}
/**
* Render the app.
*
* @return {Element} element
*/
render() {
return (
<div>
<Toolbar>
<Button active={this.hasLinks()} onMouseDown={this.onClickLink}>
<Icon>link</Icon>
</Button>
</Toolbar>
<Editor
placeholder="Enter some text..."
ref={this.ref}
value={this.state.value}
onChange={this.onChange}
onPaste={this.onPaste}
renderNode={this.renderNode}
/>
</div>
)
}
/**
* Render a Slate node.
*
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
* @return {Element}
*/
renderNode = (props, editor, next) => {
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>
)
}
default: {
return next()
}
}
}
/**
* On change.
*
* @param {Editor} editor
*/
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 { editor } = this
const { value } = editor
const hasLinks = this.hasLinks()
if (hasLinks) {
editor.command(unwrapLink)
} else if (value.selection.isExpanded) {
const href = window.prompt('Enter the URL of the link:')
if (href == null) {
return
}
editor.command(wrapLink, href)
} else {
const href = window.prompt('Enter the URL of the link:')
if (href == null) {
return
}
const text = window.prompt('Enter the text for the link:')
if (text == null) {
return
}
editor
.insertText(text)
.moveFocusBackward(text.length)
.command(wrapLink, href)
}
}
/**
* On paste, if the text is a link, wrap the selection in a link.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
onPaste = (event, editor, next) => {
if (editor.value.selection.isCollapsed) return next()
const transfer = getEventTransfer(event)
const { type, text } = transfer
if (type !== 'text' && type !== 'html') return next()
if (!isUrl(text)) return next()
if (this.hasLinks()) {
editor.command(unwrapLink)
}
editor.command(wrapLink, text)
}
}
/**
* Export.
*/
export default Links