forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.js
160 lines (142 loc) · 3.71 KB
/
images.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
import React, { useState, useMemo } from 'react'
import imageExtensions from 'image-extensions'
import isUrl from 'is-url'
import { Transforms, createEditor } from 'slate'
import {
Slate,
Editable,
useEditor,
useSelected,
useFocused,
withReact,
} from 'slate-react'
import { withHistory } from 'slate-history'
import { css } from 'emotion'
import { Button, Icon, Toolbar } from '../components'
const ImagesExample = () => {
const [value, setValue] = useState(initialValue)
const editor = useMemo(
() => withImages(withHistory(withReact(createEditor()))),
[]
)
return (
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
<Toolbar>
<InsertImageButton />
</Toolbar>
<Editable
renderElement={props => <Element {...props} />}
placeholder="Enter some text..."
/>
</Slate>
)
}
const withImages = editor => {
const { insertData, isVoid } = editor
editor.isVoid = element => {
return element.type === 'image' ? true : isVoid(element)
}
editor.insertData = data => {
const text = data.getData('text/plain')
const { files } = data
if (files && files.length > 0) {
for (const file of files) {
const reader = new FileReader()
const [mime] = file.type.split('/')
if (mime === 'image') {
reader.addEventListener('load', () => {
const url = reader.result
insertImage(editor, url)
})
reader.readAsDataURL(file)
}
}
} else if (isImageUrl(text)) {
insertImage(editor, text)
} else {
insertData(data)
}
}
return editor
}
const insertImage = (editor, url) => {
const text = { text: '' }
const image = { type: 'image', url, children: [text] }
Transforms.insertNodes(editor, image)
}
const Element = props => {
const { attributes, children, element } = props
switch (element.type) {
case 'image':
return <ImageElement {...props} />
default:
return <p {...attributes}>{children}</p>
}
}
const ImageElement = ({ attributes, children, element }) => {
const selected = useSelected()
const focused = useFocused()
return (
<div {...attributes}>
<div contentEditable={false}>
<img
src={element.url}
className={css`
display: block;
max-width: 100%;
max-height: 20em;
box-shadow: ${selected && focused ? '0 0 0 3px #B4D5FF' : 'none'};
`}
/>
</div>
{children}
</div>
)
}
const InsertImageButton = () => {
const editor = useEditor()
return (
<Button
onMouseDown={event => {
event.preventDefault()
const url = window.prompt('Enter the URL of the image:')
if (!url) return
insertImage(editor, url)
}}
>
<Icon>image</Icon>
</Button>
)
}
const isImageUrl = url => {
if (!url) return false
if (!isUrl(url)) return false
const ext = new URL(url).pathname.split('.').pop()
return imageExtensions.includes(ext)
}
const initialValue = [
{
type: 'paragraph',
children: [
{
text:
'In addition to nodes that contain editable text, you can also create other types of nodes, like images or videos.',
},
],
},
{
type: 'image',
url: 'https://source.unsplash.com/kFrdX5IeQzI',
children: [{ text: '' }],
},
{
type: 'paragraph',
children: [
{
text:
'This example shows images in action. It features two ways to add images. You can either add an image via the toolbar icon above, or if you want in on a little secret, copy an image URL to your keyboard and paste it anywhere in the editor!',
},
],
},
]
export default ImagesExample