forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
199 lines (168 loc) · 3.57 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
import { Editor, Raw } from '../..'
import React from 'react'
import initialState from './state.json'
import isUrl from 'is-url'
/**
* Define a schema.
*
* @type {Object}
*/
const schema = {
nodes: {
paragraph: props => <p>{props.children}</p>,
link: (props) => {
const { data } = props.node
const href = data.get('href')
return <a {...props.attributes} href={href}>{props.children}</a>
}
}
}
/**
* The links example.
*
* @type {Component}
*/
class Links extends React.Component {
/**
* Deserialize the raw initial state.
*
* @type {Object}
*/
state = {
state: Raw.deserialize(initialState, { terse: true })
};
/**
* Check whether the current selection has a link in it.
*
* @return {Boolean} hasLinks
*/
hasLinks = () => {
const { state } = this.state
return state.inlines.some(inline => inline.type == 'link')
}
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
/**
* 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} e
*/
onClickLink = (e) => {
e.preventDefault()
let { state } = this.state
const hasLinks = this.hasLinks()
if (hasLinks) {
state = state
.transform()
.unwrapInline('link')
.apply()
}
else if (state.isExpanded) {
const href = window.prompt('Enter the URL of the link:')
state = state
.transform()
.wrapInline({
type: 'link',
data: { href }
})
.collapseToEnd()
.apply()
}
else {
const href = window.prompt('Enter the URL of the link:')
const text = window.prompt('Enter the text for the link:')
state = state
.transform()
.insertText(text)
.extendBackward(text.length)
.wrapInline({
type: 'link',
data: { href }
})
.collapseToEnd()
.apply()
}
this.setState({ state })
}
/**
* On paste, if the text is a link, wrap the selection in a link.
*
* @param {Event} e
* @param {Object} data
* @param {State} state
*/
onPaste = (e, data, state) => {
if (state.isCollapsed) return
if (data.type != 'text' && data.type != 'html') return
if (!isUrl(data.text)) return
let transform = state.transform()
if (this.hasLinks()) {
transform.unwrapInline('link')
}
return transform
.wrapInline({
type: 'link',
data: {
href: data.text
}
})
.collapseToEnd()
.apply()
}
/**
* 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
schema={schema}
state={this.state.state}
onChange={this.onChange}
onPaste={this.onPaste}
/>
</div>
)
}
}
/**
* Export.
*/
export default Links