forked from kaydelaney/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (196 loc) · 5.29 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
import { Editor } from 'slate-react'
import { Value } from 'slate'
import React from 'react'
import initialValueAsJson from './value.json'
/**
* Deserialize the initial editor value.
*
* @type {Object}
*/
const initialValue = Value.fromJSON(initialValueAsJson)
/**
* The auto-markdown example.
*
* @type {Component}
*/
class MarkdownShortcuts extends React.Component {
/**
* Get the block type for a series of auto-markdown shortcut `chars`.
*
* @param {String} chars
* @return {String} block
*/
getType = chars => {
switch (chars) {
case '*':
case '-':
case '+':
return 'list-item'
case '>':
return 'block-quote'
case '#':
return 'heading-one'
case '##':
return 'heading-two'
case '###':
return 'heading-three'
case '####':
return 'heading-four'
case '#####':
return 'heading-five'
case '######':
return 'heading-six'
default:
return null
}
}
/**
*
* Render the example.
*
* @return {Component} component
*/
render() {
return (
<Editor
placeholder="Write some markdown..."
defaultValue={initialValue}
onKeyDown={this.onKeyDown}
renderBlock={this.renderBlock}
/>
)
}
/**
* Render a Slate block.
*
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
* @return {Element}
*/
renderBlock = (props, editor, next) => {
const { attributes, children, node } = props
switch (node.type) {
case 'block-quote':
return <blockquote {...attributes}>{children}</blockquote>
case 'bulleted-list':
return <ul {...attributes}>{children}</ul>
case 'heading-one':
return <h1 {...attributes}>{children}</h1>
case 'heading-two':
return <h2 {...attributes}>{children}</h2>
case 'heading-three':
return <h3 {...attributes}>{children}</h3>
case 'heading-four':
return <h4 {...attributes}>{children}</h4>
case 'heading-five':
return <h5 {...attributes}>{children}</h5>
case 'heading-six':
return <h6 {...attributes}>{children}</h6>
case 'list-item':
return <li {...attributes}>{children}</li>
default:
return next()
}
}
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
onKeyDown = (event, editor, next) => {
switch (event.key) {
case ' ':
return this.onSpace(event, editor, next)
case 'Backspace':
return this.onBackspace(event, editor, next)
case 'Enter':
return this.onEnter(event, editor, next)
default:
return next()
}
}
/**
* On space, if it was after an auto-markdown shortcut, convert the current
* node into the shortcut's corresponding type.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
onSpace = (event, editor, next) => {
const { value } = editor
const { selection } = value
if (selection.isExpanded) return next()
const { startBlock } = value
const { start } = selection
const chars = startBlock.text.slice(0, start.offset).replace(/\s*/g, '')
const type = this.getType(chars)
if (!type) return next()
if (type === 'list-item' && startBlock.type === 'list-item') return next()
event.preventDefault()
editor.setBlocks(type)
if (type === 'list-item') {
editor.wrapBlock('bulleted-list')
}
editor.moveFocusToStartOfNode(startBlock).delete()
}
/**
* On backspace, if at the start of a non-paragraph, convert it back into a
* paragraph node.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
onBackspace = (event, editor, next) => {
const { value } = editor
const { selection } = value
if (selection.isExpanded) return next()
if (selection.start.offset !== 0) return next()
const { startBlock } = value
if (startBlock.type === 'paragraph') return next()
event.preventDefault()
editor.setBlocks('paragraph')
if (startBlock.type === 'list-item') {
editor.unwrapBlock('bulleted-list')
}
}
/**
* On return, if at the end of a node type that should not be extended,
* create a new paragraph below it.
*
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
onEnter = (event, editor, next) => {
const { value } = editor
const { selection } = value
const { start, end, isExpanded } = selection
if (isExpanded) return next()
const { startBlock } = value
if (start.offset === 0 && startBlock.text.length === 0)
return this.onBackspace(event, editor, next)
if (end.offset !== startBlock.text.length) return next()
if (
startBlock.type !== 'heading-one' &&
startBlock.type !== 'heading-two' &&
startBlock.type !== 'heading-three' &&
startBlock.type !== 'heading-four' &&
startBlock.type !== 'heading-five' &&
startBlock.type !== 'heading-six' &&
startBlock.type !== 'block-quote'
) {
return next()
}
event.preventDefault()
editor.splitBlock().setBlocks('paragraph')
}
}
/**
* Export.
*/
export default MarkdownShortcuts