2016-06-21 19:34:51 -07:00
|
|
|
|
2017-09-11 18:11:45 -07:00
|
|
|
import { Editor } from 'slate-react'
|
|
|
|
import { State } from 'slate'
|
|
|
|
|
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
|
|
|
|
2016-07-06 14:42:59 -07:00
|
|
|
/**
|
2016-08-14 13:21:46 -07:00
|
|
|
* Define a schema.
|
2016-07-06 14:42:59 -07:00
|
|
|
*
|
|
|
|
* @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-07-06 14:42:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2016-07-06 14:42:59 -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 = {
|
start removing raw serializer (#1098)
* start removing raw serializer
* convert first tests to use jsx
* simplify jsx tests for raw serializer
* allow for options in raw serializer tests
* add more preserve option tests
* convert plain serializer tests
* convert html serialize
* start converting html deserialize
* add hyperscript util
* remove slate-sugar copy-pasted
* finish converting html deserialize
* convert plugins tests
* update rendering tests
* convert schemas tests
* convert core plugin tests
* update hyperscript utl
* refactor changes test directory structure
* fix changes before migration
* add migrated changes test files
* remove <x- prefixes from migration
* get addMark at-current-range tests passing
* get delete at-current-range tests passing
* remove old tests
* convert deleteForward and deleteBackward
* convert insertBlock, insertInline, insertFragment, insertText
* convert removeMark, setBlock, setInline, splitBlock, splitInline
* add unstaged tests
* convert toggleMark, unwrapBlock, unwrapInline, wrapBlock, wrapInline, wrapText
* convert call, setData
* convert on-selection tests
* remove old on-selection tests
* convert history tests
* convert insertFragmentByKey, insertNodeByKey, insertTextByKey, mergeNodeByKey, moveNodeByKey
* convert removeNodeByKey, removeTextByKey, setMarkByKey, setNodeByKey
* convert splitDescendantsByKey, splitNodeByKey, unwrapBlockByKey, unwrapInlineByKey, unwrapNodeByKey, wrapBlockByKey
* fix tests
* port missing at-range tests to at-current-range
* remove at-range tests
* fix raw serializer tests
* fix linter
* fix to prefer toJSON as the canonical method name
* fix todos
* remove internal references to Raw
* add deprecation helper to Text.fromJSON
* convert examples to not use Raw, and not rely on terse
* remove focus-blur example, rename large document example
* refactor Raw serialize to deprecate, not remove, terse
* deprecate defaultBlockType, toRaw, cleanup serializers
2017-09-10 14:56:03 -07:00
|
|
|
state: State.fromJSON(initialState)
|
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
|
2017-09-05 18:03:41 -07:00
|
|
|
* @param {Change} change
|
2016-06-21 19:34:51 -07:00
|
|
|
*/
|
|
|
|
|
2017-09-05 18:03:41 -07:00
|
|
|
onBackspace = (e, change) => {
|
|
|
|
const { state } = change
|
2016-07-13 16:19:49 -07:00
|
|
|
if (state.startOffset != 0) return
|
|
|
|
e.preventDefault()
|
2017-09-05 18:03:41 -07:00
|
|
|
return true
|
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
|
|
|
*
|
2017-09-05 18:03:41 -07:00
|
|
|
* @param {Change} change
|
2016-07-07 08:35:13 -07:00
|
|
|
*/
|
|
|
|
|
2017-09-05 18:03:41 -07:00
|
|
|
onChange = ({ state }) => {
|
2016-07-13 16:19:49 -07:00
|
|
|
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
|
2017-09-05 18:03:41 -07:00
|
|
|
* @param {Change} change
|
2016-07-07 08:35:13 -07:00
|
|
|
*/
|
|
|
|
|
2017-09-05 18:03:41 -07:00
|
|
|
onDelete = (e, change) => {
|
|
|
|
const { state } = change
|
|
|
|
if (state.endOffset != state.startText.text.length) return
|
2016-07-13 16:19:49 -07:00
|
|
|
e.preventDefault()
|
2017-09-05 18:03:41 -07:00
|
|
|
return true
|
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
|
2017-09-05 18:03:41 -07:00
|
|
|
* @param {Change} change
|
2016-07-07 08:35:13 -07:00
|
|
|
*/
|
|
|
|
|
2017-09-05 18:03:41 -07:00
|
|
|
onEnter = (e, change) => {
|
2016-07-13 16:19:49 -07:00
|
|
|
e.preventDefault()
|
2017-09-05 18:03:41 -07:00
|
|
|
return true
|
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
|
2017-09-05 18:03:41 -07:00
|
|
|
* @param {Change} change
|
2016-06-21 19:34:51 -07:00
|
|
|
*/
|
|
|
|
|
2017-09-05 18:03:41 -07:00
|
|
|
onKeyDown = (e, data, change) => {
|
|
|
|
const { state } = change
|
2016-09-22 22:57:33 +07:00
|
|
|
const { document, selection } = state
|
|
|
|
const { startKey } = selection
|
|
|
|
const startNode = document.getDescendant(startKey)
|
|
|
|
|
|
|
|
if (selection.isAtStartOf(startNode)) {
|
2016-11-17 12:34:10 -08:00
|
|
|
const previous = document.getPreviousText(startNode.key)
|
|
|
|
const prevBlock = document.getClosestBlock(previous.key)
|
2016-09-22 22:57:33 +07:00
|
|
|
|
|
|
|
if (prevBlock.type == 'table-cell') {
|
|
|
|
e.preventDefault()
|
2017-09-05 18:03:41 -07:00
|
|
|
return true
|
2016-09-22 22:57:33 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:03:41 -07:00
|
|
|
if (state.startBlock.type != 'table-cell') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:23:07 -07:00
|
|
|
switch (e.key) {
|
|
|
|
case 'Backspace': return this.onBackspace(e, state)
|
|
|
|
case 'Delete': return this.onDelete(e, state)
|
|
|
|
case 'Enter': return this.onEnter(e, state)
|
2016-06-21 19:34:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
|
2017-08-02 18:36:33 +02:00
|
|
|
render() {
|
2016-07-13 16:19:49 -07:00
|
|
|
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
|