forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
234 lines (198 loc) · 4.24 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Editor, Mark, Raw, Void } from '../..'
import React from 'react'
import ReactDOM from 'react-dom'
import initialState from './state.json'
import isImage from 'is-image'
import isUrl from 'is-url'
import { Map } from 'immutable'
/**
* Define a set of node renderers.
*
* @type {Object}
*/
const NODES = {
image: (props) => {
const { node, state } = props
const src = node.data.get('src')
return (
<Void {...props} className="image-block">
<img {...props.attributes} src={src} />
</Void>
)
}
}
/**
* The images example.
*
* @type {Component}
*/
class Images extends React.Component {
/**
* Deserialize the raw initial state.
*
* @type {Object}
*/
state = {
state: Raw.deserialize(initialState)
};
/**
* Render the app.
*
* @return {Element} element
*/
render = () => {
return (
<div>
{this.renderToolbar()}
{this.renderEditor()}
</div>
)
}
/**
* Render the toolbar.
*
* @return {Element} element
*/
renderToolbar = () => {
return (
<div className="menu toolbar-menu">
<span className="button" onMouseDown={this.onClickImage}>
<span className="material-icons">image</span>
</span>
</div>
)
}
/**
* Render the editor.
*
* @return {Element} element
*/
renderEditor = () => {
return (
<div className="editor">
<Editor
state={this.state.state}
renderNode={this.renderNode}
onChange={this.onChange}
onDocumentChange={this.onDocumentChange}
onPaste={this.onPaste}
/>
</div>
)
}
/**
* Render a `node`.
*
* @param {Node} node
* @return {Element}
*/
renderNode = (node) => {
return NODES[node.type]
}
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
/**
* On document change, if the last block is an image, add another paragraph.
*
* @param {Document} document
* @param {State} state
*/
onDocumentChange = (document, state) => {
const blocks = document.getBlocks()
const last = blocks.last()
if (last.type != 'image') return
const normalized = state
.transform()
.collapseToEndOf(last)
.splitBlock()
.setBlock({
type: 'paragraph',
isVoid: false,
data: {}
})
.apply({
snapshot: false
})
this.onChange(normalized)
}
/**
* On clicking the image button, prompt for an image and insert it.
*
* @param {Event} e
*/
onClickImage = (e) => {
e.preventDefault()
const src = window.prompt('Enter the URL of the image:')
if (!src) return
let { state } = this.state
state = this.insertImage(state, src)
this.onChange(state)
}
/**
* On paste, if the pasted content is an image URL, insert it.
*
* @param {Event} e
* @param {Object} paste
* @param {State} state
* @return {State}
*/
onPaste = (e, paste, state) => {
if (paste.type != 'text') return
if (!isUrl(paste.text)) return
if (!isImage(paste.text)) return
return this.insertImage(state, paste.text)
}
/**
* Insert an image with `src` at the current selection.
*
* @param {State} state
* @param {String} src
* @return {State}
*/
insertImage = (state, src) => {
if (state.isExpanded) {
state = state
.transform()
.delete()
.apply()
}
const { anchorBlock, selection } = state
let transform = state.transform()
if (anchorBlock.type == 'image') {
transform = transform.splitBlock()
}
else if (anchorBlock.text != '') {
if (selection.isAtEndOf(anchorBlock)) {
transform = transform.splitBlock()
}
else if (selection.isAtStartOf(anchorBlock)) {
transform = transform
.splitBlock()
.collapseToStartOfPreviousBlock()
}
else {
transform = transform
.splitBlock()
.splitBlock()
.collapseToStartOfPreviousBlock()
}
}
return transform
.setBlock({
type: 'image',
isVoid: true,
data: { src }
})
.apply()
}
}
/**
* Export.
*/
export default Images