1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-21 15:44:19 +01:00
slate/examples/tables/index.js
Ian Storm Taylor 7470a6dd53 Expose transforms (#836)
* refactor to extract applyOperation util

* change handlers to receive transform instead of state

* change onChange to receive a transform, update rich-text example

* fix stack iterationg, convert check-list example

* convert code-highlighting, embeds, emojis examples

* change operations to use full paths, not indexes

* switch split and join to be recursive

* fix linter

* fix onChange calls

* make all operations invertable, add src/operations/* logic

* rename "join" to "merge"

* remove .length property of nodes

* fix node.getFragmentAtRange logic

* convert remaining examples, fix existing changes

* fix .apply() calls and tests

* change setSave and setIsNative transforms

* fix insert_text operations to include marks always

* cleanup and fixes

* fix node inheritance

* fix core onCut handler

* skip constructor in node inheritance

* cleanup

* change updateDescendant to updateNode

* add and update docs

* eliminate need for .apply(), change history to mutable

* add missing file

* add deprecation support to Transform objects

* rename "transform" to "change"

* update benchmark

* add deprecation util to logger

* update transform isNative attr

* fix remaining warn use

* simplify history checkpointing logic

* fix tests

* revert history to being immutable

* fix history

* fix normalize

* fix syntax error from merge
2017-09-05 18:03:41 -07:00

152 lines
2.8 KiB
JavaScript

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 {Change} change
*/
onBackspace = (e, change) => {
const { state } = change
if (state.startOffset != 0) return
e.preventDefault()
return true
}
/**
* On change.
*
* @param {Change} change
*/
onChange = ({ state }) => {
this.setState({ state })
}
/**
* On delete, do nothing if at the end of a table cell.
*
* @param {Event} e
* @param {Change} change
*/
onDelete = (e, change) => {
const { state } = change
if (state.endOffset != state.startText.text.length) return
e.preventDefault()
return true
}
/**
* On return, do nothing if inside a table cell.
*
* @param {Event} e
* @param {Change} change
*/
onEnter = (e, change) => {
e.preventDefault()
return true
}
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} e
* @param {Object} data
* @param {Change} change
*/
onKeyDown = (e, data, change) => {
const { state } = change
const { document, selection } = state
const { startKey } = selection
const startNode = document.getDescendant(startKey)
if (selection.isAtStartOf(startNode)) {
const previous = document.getPreviousText(startNode.key)
const prevBlock = document.getClosestBlock(previous.key)
if (prevBlock.type == 'table-cell') {
e.preventDefault()
return true
}
}
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