forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (180 loc) · 4.67 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
import { Editor, Raw } from '../..'
import React from 'react'
import initialState from './state.json'
/**
* Define a schema.
*
* @type {Object}
*/
const schema = {
nodes: {
'block-quote': props => <blockquote>{props.children}</blockquote>,
'bulleted-list': props => <ul>{props.children}</ul>,
'heading-one': props => <h1>{props.children}</h1>,
'heading-two': props => <h2>{props.children}</h2>,
'heading-three': props => <h3>{props.children}</h3>,
'heading-four': props => <h4>{props.children}</h4>,
'heading-five': props => <h5>{props.children}</h5>,
'heading-six': props => <h6>{props.children}</h6>,
'list-item': props => <li>{props.children}</li>,
}
}
/**
* The auto-markdown example.
*
* @type {Component}
*/
class AutoMarkdown extends React.Component {
/**
* Deserialize the raw initial state.
*
* @type {Object}
*/
state = {
state: Raw.deserialize(initialState, { terse: true })
};
/**
* 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 (
<div className="editor">
<Editor
schema={schema}
state={this.state.state}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
</div>
)
}
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} e
* @param {Data} data
* @param {State} state
* @return {State or Null} state
*/
onKeyDown = (e, data, state) => {
switch (data.key) {
case 'space': return this.onSpace(e, state)
case 'backspace': return this.onBackspace(e, state)
case 'enter': return this.onEnter(e, state)
}
}
/**
* On space, if it was after an auto-markdown shortcut, convert the current
* node into the shortcut's corresponding type.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onSpace = (e, state) => {
if (state.isExpanded) return
const { startBlock, startOffset } = state
const chars = startBlock.text.slice(0, startOffset).replace(/\s*/g, '')
const type = this.getType(chars)
if (!type) return
if (type == 'list-item' && startBlock.type == 'list-item') return
e.preventDefault()
let transform = state
.transform()
.setBlock(type)
if (type == 'list-item') transform.wrapBlock('bulleted-list')
state = transform
.extendToStartOf(startBlock)
.delete()
.apply()
return state
}
/**
* On backspace, if at the start of a non-paragraph, convert it back into a
* paragraph node.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onBackspace = (e, state) => {
if (state.isExpanded) return
if (state.startOffset != 0) return
const { startBlock } = state
if (startBlock.type == 'paragraph') return
e.preventDefault()
let transform = state
.transform()
.setBlock('paragraph')
if (startBlock.type == 'list-item') transform.unwrapBlock('bulleted-list')
state = transform.apply()
return state
}
/**
* On return, if at the end of a node type that should not be extended,
* create a new paragraph below it.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onEnter = (e, state) => {
if (state.isExpanded) return
const { startBlock, startOffset, endOffset } = state
if (startOffset == 0 && startBlock.length == 0) return this.onBackspace(e, state)
if (endOffset != startBlock.length) return
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
}
e.preventDefault()
return state
.transform()
.splitBlock()
.setBlock('paragraph')
.apply()
}
}
/**
* Export.
*/
export default AutoMarkdown