1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-20 15:14:38 +01:00
slate/examples/tables/index.js

137 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-06-21 19:34:51 -07:00
2016-07-06 14:21:11 -07:00
import { Editor, Raw } from '../..'
2016-06-21 19:34:51 -07:00
import React from 'react'
2016-07-07 08:35:13 -07:00
import initialState from './state.json'
2016-06-21 19:34:51 -07:00
import keycode from 'keycode'
/**
2016-08-14 13:21:46 -07:00
* Define a schema.
*
* @type {Object}
*/
2016-08-14 13:21:46 -07:00
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>
}
}
2016-06-21 19:34:51 -07:00
/**
2016-06-24 10:22:48 -07:00
* The tables example.
2016-06-21 19:34:51 -07:00
*
* @type {Component}
2016-06-21 19:34:51 -07:00
*/
2016-06-24 10:22:48 -07:00
class Tables extends React.Component {
2016-06-21 19:34:51 -07:00
/**
* Deserialize the raw initial state.
*
* @type {Object}
*/
state = {
2016-07-25 14:45:37 -07:00
state: Raw.deserialize(initialState, { terse: true })
2016-06-21 19:34:51 -07:00
};
/**
2016-07-13 16:19:49 -07:00
* On backspace, do nothing if at the start of a table cell.
2016-06-21 19:34:51 -07:00
*
2016-07-13 16:19:49 -07:00
* @param {Event} e
* @param {State} state
* @return {State or Null} state
2016-06-21 19:34:51 -07:00
*/
2016-07-13 16:19:49 -07:00
onBackspace = (e, state) => {
if (state.startOffset != 0) return
e.preventDefault()
return state
2016-06-21 19:34:51 -07:00
}
2016-07-07 08:35:13 -07:00
/**
2016-07-13 16:19:49 -07:00
* On change.
2016-07-07 08:35:13 -07:00
*
2016-07-13 16:19:49 -07:00
* @param {State} state
2016-07-07 08:35:13 -07:00
*/
2016-07-13 16:19:49 -07:00
onChange = (state) => {
this.setState({ state })
2016-07-07 08:35:13 -07:00
}
/**
2016-07-13 16:19:49 -07:00
* On delete, do nothing if at the end of a table cell.
2016-07-07 08:35:13 -07:00
*
2016-07-13 16:19:49 -07:00
* @param {Event} e
* @param {State} state
* @return {State or Null} state
2016-07-07 08:35:13 -07:00
*/
2016-07-13 16:19:49 -07:00
onDelete = (e, state) => {
if (state.endOffset != state.startText.length) return
e.preventDefault()
return state
2016-07-07 08:35:13 -07:00
}
/**
2016-07-13 16:19:49 -07:00
* On return, do nothing if inside a table cell.
2016-07-07 08:35:13 -07:00
*
2016-07-13 16:19:49 -07:00
* @param {Event} e
2016-07-07 08:35:13 -07:00
* @param {State} state
2016-07-13 16:19:49 -07:00
* @return {State or Null} state
2016-07-07 08:35:13 -07:00
*/
2016-07-13 16:19:49 -07:00
onEnter = (e, state) => {
e.preventDefault()
return state
2016-07-07 08:35:13 -07:00
}
2016-06-21 19:34:51 -07:00
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} e
2016-07-27 14:30:09 -07:00
* @param {Object} data
2016-06-21 19:34:51 -07:00
* @param {State} state
* @return {State or Null} state
*/
2016-07-27 14:30:09 -07:00
onKeyDown = (e, data, state) => {
2016-07-06 15:13:15 -07:00
if (state.startBlock.type != 'table-cell') return
2016-07-27 14:30:09 -07:00
switch (data.key) {
2016-06-21 19:34:51 -07:00
case 'backspace': return this.onBackspace(e, state)
case 'delete': return this.onDelete(e, state)
case 'enter': return this.onEnter(e, state)
}
}
/**
2016-07-13 16:19:49 -07:00
* Render the example.
2016-06-21 19:34:51 -07:00
*
2016-07-13 16:19:49 -07:00
* @return {Component} component
2016-06-21 19:34:51 -07:00
*/
2016-07-13 16:19:49 -07:00
render = () => {
return (
<div className="editor">
<Editor
2016-08-14 13:21:46 -07:00
schema={schema}
2016-07-13 16:19:49 -07:00
state={this.state.state}
onKeyDown={this.onKeyDown}
onChange={this.onChange}
/>
</div>
)
2016-06-21 19:34:51 -07:00
}
}
/**
2016-06-24 10:22:48 -07:00
* Export.
2016-06-21 19:34:51 -07:00
*/
2016-06-24 10:22:48 -07:00
export default Tables