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

144 lines
2.7 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'
import keycode from 'keycode'
import state from './state.json'
/**
* Node renderers.
*
* @type {Object}
*/
const NODES = {
'paragraph': props => <p>{props.children}</p>,
'table': props => <table><tbody>{props.children}</tbody></table>,
'table-row': props => <tr>{props.children}</tr>,
'table-cell': props => <td>{props.children}</td>
}
/**
* Mark renderers.
*
* @type {Object}
*/
const MARKS = {
bold: {
fontWeight: 'bold'
}
}
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 = {
state: Raw.deserialize(state)
};
/**
* Render the example.
*
* @return {Component} component
*/
render() {
return (
<div className="editor">
<Editor
state={this.state.state}
renderNode={node => NODES[node.type]}
renderMark={mark => MARKS[mark.type]}
2016-06-21 19:34:51 -07:00
onKeyDown={(e, state) => this.onKeyDown(e, state)}
onChange={(state) => {
console.groupCollapsed('Change!')
console.log('Document:', state.document.toJS())
console.log('Selection:', state.selection.toJS())
console.log('Content:', Raw.serialize(state))
console.groupEnd()
this.setState({ state })
}}
/>
</div>
)
}
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} e
* @param {State} state
* @return {State or Null} state
*/
onKeyDown(e, state) {
2016-07-06 15:13:15 -07:00
if (state.startBlock.type != 'table-cell') return
2016-06-21 19:34:51 -07:00
const key = keycode(e.which)
switch (key) {
case 'backspace': return this.onBackspace(e, state)
case 'delete': return this.onDelete(e, state)
case 'enter': return this.onEnter(e, state)
}
}
/**
* 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) {
2016-06-23 23:59:22 -07:00
if (state.startOffset != 0) return
2016-06-21 19:34:51 -07:00
e.preventDefault()
return 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) {
2016-07-06 15:13:15 -07:00
if (state.endOffset != state.startText.length) return
2016-06-21 19:34:51 -07:00
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
}
}
/**
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