forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (187 loc) · 4.3 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
import { Editor, getEventRange, getEventTransfer } from 'slate-react'
import { Block, Value } from 'slate'
import { LAST_CHILD_TYPE_INVALID } from 'slate-schema-violations'
import React from 'react'
import initialValue from './value.json'
import imageExtensions from 'image-extensions'
import isUrl from 'is-url'
/*
* A function to determine whether a URL has an image extension.
*
* @param {String} url
* @return {Boolean}
*/
function isImage(url) {
return !!imageExtensions.find(url.endsWith)
}
/**
* A change function to standardize inserting images.
*
* @param {Change} change
* @param {String} src
* @param {Range} target
*/
function insertImage(change, src, target) {
if (target) {
change.select(target)
}
change.insertBlock({
type: 'image',
isVoid: true,
data: { src },
})
}
/**
* A schema to enforce that there's always a paragraph as the last block.
*
* @type {Object}
*/
const schema = {
document: {
last: { types: ['paragraph'] },
normalize: (change, reason, { node, child }) => {
switch (reason) {
case LAST_CHILD_TYPE_INVALID: {
const paragraph = Block.create('paragraph')
return change.insertNodeByKey(node.key, node.nodes.size, paragraph)
}
}
},
},
}
/**
* The images example.
*
* @type {Component}
*/
class Images extends React.Component {
/**
* Deserialize the raw initial value.
*
* @type {Object}
*/
state = {
value: Value.fromJSON(initialValue),
}
/**
* 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
placeholder="Enter some text..."
value={this.state.value}
schema={schema}
onChange={this.onChange}
onDrop={this.onDropOrPaste}
onPaste={this.onDropOrPaste}
renderNode={this.renderNode}
/>
</div>
)
}
/**
* Render a Slate node.
*
* @param {Object} props
* @return {Element}
*/
renderNode = props => {
const { attributes, node, isSelected } = props
switch (node.type) {
case 'image': {
const src = node.data.get('src')
const className = isSelected ? 'active' : null
const style = { display: 'block' }
return (
<img src={src} className={className} style={style} {...attributes} />
)
}
}
}
/**
* On change.
*
* @param {Change} change
*/
onChange = ({ value }) => {
this.setState({ value })
}
/**
* On clicking the image button, prompt for an image and insert it.
*
* @param {Event} event
*/
onClickImage = event => {
event.preventDefault()
const src = window.prompt('Enter the URL of the image:')
if (!src) return
const change = this.state.value.change().call(insertImage, src)
this.onChange(change)
}
/**
* On drop, insert the image wherever it is dropped.
*
* @param {Event} event
* @param {Change} change
* @param {Editor} editor
*/
onDropOrPaste = (event, change, editor) => {
const target = getEventRange(event, change.value)
if (!target && event.type == 'drop') return
const transfer = getEventTransfer(event)
const { type, text, files } = transfer
if (type == 'files') {
for (const file of files) {
const reader = new FileReader()
const [mime] = file.type.split('/')
if (mime != 'image') continue
reader.addEventListener('load', () => {
editor.change(c => {
c.call(insertImage, reader.result, target)
})
})
reader.readAsDataURL(file)
}
}
if (type == 'text') {
if (!isUrl(text)) return
if (!isImage(text)) return
change.call(insertImage, text, target)
}
}
}
/**
* Export.
*/
export default Images