forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
220 lines (187 loc) · 4.22 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
import { Editor, getEventTransfer } from 'slate-react'
import { Block, Value } from 'slate'
import React from 'react'
import initialValueAsJson from './value.json'
import imageExtensions from 'image-extensions'
import isUrl from 'is-url'
import { css } from 'emotion'
import { Button, Icon, Toolbar } from '../components'
/**
* Deserialize the initial editor value.
*
* @type {Object}
*/
const initialValue = Value.fromJSON(initialValueAsJson)
/**
* A function to determine whether a URL has an image extension.
*
* @param {String} url
* @return {Boolean}
*/
function isImage(url) {
return imageExtensions.includes(getExtension(url))
}
/**
* Get the extension of the URL, using the URL API.
*
* @param {String} url
* @return {String}
*/
function getExtension(url) {
return new URL(url).pathname.split('.').pop()
}
/**
* A change function to standardize inserting images.
*
* @param {Editor} editor
* @param {String} src
* @param {Range} target
*/
function insertImage(editor, src, target) {
if (target) {
editor.select(target)
}
editor.insertBlock({
type: 'image',
data: { src },
})
}
/**
* The editor's schema.
*
* @type {Object}
*/
const schema = {
document: {
last: { type: 'paragraph' },
normalize: (editor, { code, node, child }) => {
switch (code) {
case 'last_child_type_invalid': {
const paragraph = Block.create('paragraph')
return editor.insertNodeByKey(node.key, node.nodes.size, paragraph)
}
}
},
},
blocks: {
image: {
isVoid: true,
},
},
}
/**
* The images example.
*
* @type {Component}
*/
class Images extends React.Component {
/**
* 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 onMouseDown={this.onClickImage}>
<Icon>image</Icon>
</Button>
</Toolbar>
<Editor
placeholder="Enter some text..."
ref={this.ref}
defaultValue={initialValue}
schema={schema}
onDrop={this.onDropOrPaste}
onPaste={this.onDropOrPaste}
renderBlock={this.renderBlock}
/>
</div>
)
}
/**
* Render a Slate block.
*
* @param {Object} props
* @return {Element}
*/
renderBlock = (props, editor, next) => {
const { attributes, node, isFocused } = props
switch (node.type) {
case 'image': {
const src = node.data.get('src')
return (
<img
{...attributes}
src={src}
className={css`
display: block;
max-width: 100%;
max-height: 20em;
box-shadow: ${isFocused ? '0 0 0 2px blue;' : 'none'};
`}
/>
)
}
default: {
return next()
}
}
}
/**
* 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
this.editor.command(insertImage, src)
}
/**
* On drop, insert the image wherever it is dropped.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
onDropOrPaste = (event, editor, next) => {
const target = editor.findEventRange(event)
if (!target && event.type === 'drop') return next()
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.command(insertImage, reader.result, target)
})
reader.readAsDataURL(file)
}
return
}
if (type === 'text') {
if (!isUrl(text)) return next()
if (!isImage(text)) return next()
editor.command(insertImage, text, target)
return
}
next()
}
}
/**
* Export.
*/
export default Images