forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
149 lines (124 loc) · 2.84 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
import { Editor, Raw } from '../..'
import React from 'react'
import initialState from './state.json'
/**
* Define a schema.
*
* @type {Object}
*/
const schema = {
nodes: {
'table': props => <table><tbody {...props.attributes}>{props.children}</tbody></table>,
'table-row': props => <tr {...props.attributes}>{props.children}</tr>,
'table-cell': props => <td {...props.attributes}>{props.children}</td>,
},
marks: {
'bold': props => <strong>{props.children}</strong>
}
}
/**
* The tables example.
*
* @type {Component}
*/
class Tables extends React.Component {
/**
* Deserialize the raw initial state.
*
* @type {Object}
*/
state = {
state: Raw.deserialize(initialState, { terse: true })
};
/**
* On backspace, do nothing if at the start of a table cell.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onBackspace = (e, state) => {
if (state.startOffset != 0) return
e.preventDefault()
return state
}
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
/**
* On delete, do nothing if at the end of a table cell.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onDelete = (e, state) => {
if (state.endOffset != state.startText.length) return
e.preventDefault()
return state
}
/**
* On return, do nothing if inside a table cell.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onEnter = (e, state) => {
e.preventDefault()
return state
}
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} e
* @param {Object} data
* @param {State} state
* @return {State or Null} state
*/
onKeyDown = (e, data, state) => {
const { document, selection } = state
const { startKey } = selection
const startNode = document.getDescendant(startKey)
if (selection.isAtStartOf(startNode)) {
const previous = document.getPreviousText(startNode)
const prevBlock = document.getClosestBlock(previous)
if (prevBlock.type == 'table-cell') {
e.preventDefault()
return state
}
}
if (state.startBlock.type != 'table-cell') return
switch (data.key) {
case 'backspace': return this.onBackspace(e, state)
case 'delete': return this.onDelete(e, state)
case 'enter': return this.onEnter(e, state)
}
}
/**
* Render the example.
*
* @return {Component} component
*/
render = () => {
return (
<div className="editor">
<Editor
schema={schema}
state={this.state.state}
onKeyDown={this.onKeyDown}
onChange={this.onChange}
/>
</div>
)
}
}
/**
* Export.
*/
export default Tables