1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-25 09:43:21 +01:00
slate/lib/components/content.js

396 lines
8.9 KiB
JavaScript
Raw Normal View History

2016-06-15 12:07:12 -07:00
2016-06-17 00:09:54 -07:00
import OffsetKey from '../utils/offset-key'
2016-06-15 12:07:12 -07:00
import React from 'react'
2016-06-15 14:47:52 -07:00
import ReactDOM from 'react-dom'
2016-06-15 19:46:53 -07:00
import Text from './text'
2016-06-15 12:07:12 -07:00
import keycode from 'keycode'
import { Raw } from '..'
2016-06-27 17:16:18 -07:00
import { isCommand, isWindowsCommand } from '../utils/event'
2016-06-15 12:07:12 -07:00
/**
* Content.
*/
class Content extends React.Component {
2016-06-15 14:47:52 -07:00
/**
* Props.
*/
2016-06-15 12:07:12 -07:00
static propTypes = {
onBeforeInput: React.PropTypes.func,
2016-06-15 12:07:12 -07:00
onChange: React.PropTypes.func,
onKeyDown: React.PropTypes.func,
2016-06-24 12:06:59 -07:00
onPaste: React.PropTypes.func,
2016-06-15 14:47:52 -07:00
onSelect: React.PropTypes.func,
2016-06-15 12:07:12 -07:00
renderMark: React.PropTypes.func.isRequired,
renderNode: React.PropTypes.func.isRequired,
state: React.PropTypes.object.isRequired,
};
/**
* Constructor.
*
* @param {Object} props
*/
constructor(props) {
super(props)
this.tmp = {}
}
/**
* Should the component update?
*
* @param {Object} props
* @param {Object} state
* @return {Boolean} shouldUpdate
*/
shouldComponentUpdate(props, state) {
if (props.state == this.props.state) return false
if (props.state.document == this.props.state.document) return false
2016-06-18 23:07:55 -07:00
if (props.state.isNative) return false
return true
}
/**
* On before input, bubble up.
*
* @param {Event} e
*/
onBeforeInput(e) {
this.props.onBeforeInput(e)
}
2016-06-28 16:13:50 -07:00
/**
* On blur, update the selection to be not focused.
*
* @param {Event} e
*/
onBlur(e) {
if (this.tmp.isCopying) return
let { state } = this.props
let { document, selection } = state
selection = selection.merge({ isFocused: false })
state = state.merge({ selection })
this.onChange(state)
}
2016-06-15 14:47:52 -07:00
/**
* On change, bubble up.
*
* @param {State} state
*/
2016-06-15 12:07:12 -07:00
onChange(state) {
this.props.onChange(state)
}
2016-06-15 14:47:52 -07:00
/**
* On copy, defer to `onCutCopy`, then bubble up.
2016-06-15 14:47:52 -07:00
*
* @param {Event} e
*/
2016-06-15 12:07:12 -07:00
onCopy(e) {
this.onCutCopy(e)
}
/**
* On cut, defer to `onCutCopy`, then bubble up.
*
* @param {Event} e
*/
onCut(e) {
this.onCutCopy(e)
// Once the cut has successfully executed, delete the current selection.
window.requestAnimationFrame(() => {
const state = this.props.state.transform().delete().apply()
this.onChange(state)
})
}
/**
* On cut and copy, add the currently selected fragment to the currently
* selected DOM, so that it will show up when pasted.
*
* @param {Event} e
*/
onCutCopy(e) {
const native = window.getSelection()
if (!native.rangeCount) return
const { state } = this.props
const { fragment } = state
const raw = Raw.serializeNode(fragment)
const string = JSON.stringify(raw)
const encoded = window.btoa(string)
// Wrap the first character of the selection in a span that has the encoded
// fragment attached as an attribute, so it will show up in the copied HTML.
const range = native.getRangeAt(0)
const contents = range.cloneContents()
const wrapper = window.document.createElement('span')
const text = contents.childNodes[0]
const char = text.textContent.slice(0, 1)
const first = window.document.createTextNode(char)
const rest = text.textContent.slice(1)
text.textContent = rest
wrapper.appendChild(first)
wrapper.setAttribute('data-fragment', encoded)
contents.insertBefore(wrapper, text)
// Add the phony content to the DOM, and select it, so it will be copied.
const body = window.document.querySelector('body')
const div = window.document.createElement('div')
div.setAttribute('contenteditable', true)
div.style.position = 'absolute'
div.style.left = '-9999px'
div.appendChild(contents)
body.appendChild(div)
// Set the `isCopying` flag, so our `onSelect` logic doesn't fire.
this.tmp.isCopying = true
native.selectAllChildren(div)
// Revert to the previous selection right after copying.
window.requestAnimationFrame(() => {
body.removeChild(div)
native.removeAllRanges()
native.addRange(range)
this.tmp.isCopying = false
})
2016-06-24 12:06:59 -07:00
}
2016-06-27 17:16:18 -07:00
/**
* On key down, prevent the default behavior of certain commands that will
* leave the editor in an out-of-sync state, then bubble up.
*
* @param {Event} e
*/
onKeyDown(e) {
const key = keycode(e.which)
if (
(key == 'enter') ||
(key == 'backspace') ||
(key == 'delete') ||
(key == 'b' && isCommand(e)) ||
(key == 'i' && isCommand(e)) ||
(key == 'y' && isWindowsCommand(e)) ||
(key == 'z' && isCommand(e))
) {
e.preventDefault()
}
this.props.onKeyDown(e)
}
2016-06-24 12:06:59 -07:00
/**
* On paste, determine the type and bubble up.
*
* @param {Event} e
*/
onPaste(e) {
e.preventDefault()
const data = e.clipboardData
const { types } = data
const paste = {}
// Handle files.
if (data.files.length != 0) {
paste.type = 'files'
paste.files = data.files
}
// Treat it as rich text if there is HTML content.
else if (types.includes('text/html')) {
2016-06-24 12:06:59 -07:00
paste.type = 'html'
paste.text = data.getData('text/plain')
paste.html = data.getData('text/html')
}
// Treat everything else as plain text.
else {
paste.type = 'text'
paste.text = data.getData('text/plain')
}
// If html, and the html includes a `data-fragment` attribute, it's actually
// a raw-serialized JSON fragment from a previous cut/copy, so deserialize
// it and insert it normally.
if (paste.type == 'html' && ~paste.html.indexOf('<span data-fragment="')) {
const regexp = /data-fragment="([^\s]+)"/
const matches = regexp.exec(paste.html)
const [ full, encoded ] = matches
const string = window.atob(encoded)
const json = JSON.parse(string)
const fragment = Raw.deserialize(json)
let { state } = this.props
state = state
.transform()
.insertFragment(fragment.document)
.apply()
this.onChange(state)
return
}
2016-06-24 12:06:59 -07:00
paste.data = data
this.props.onPaste(e, paste)
2016-06-15 12:07:12 -07:00
}
2016-06-15 14:47:52 -07:00
/**
* On select, update the current state's selection.
*
* @param {Event} e
*/
onSelect(e) {
if (this.tmp.isCopying) return
2016-06-15 14:47:52 -07:00
let { state } = this.props
let { document, selection } = state
2016-06-15 14:47:52 -07:00
const native = window.getSelection()
2016-06-27 17:16:18 -07:00
if (!native.rangeCount) {
selection = selection.merge({ isFocused: false })
state = state.merge({ selection })
2016-06-15 14:47:52 -07:00
this.onChange(state)
return
}
const el = ReactDOM.findDOMNode(this)
const { anchorNode, anchorOffset, focusNode, focusOffset } = native
2016-06-17 00:09:54 -07:00
const anchor = OffsetKey.findPoint(anchorNode, anchorOffset)
const focus = OffsetKey.findPoint(focusNode, focusOffset)
selection = selection.merge({
anchorKey: anchor.key,
anchorOffset: anchor.offset,
focusKey: focus.key,
focusOffset: focus.offset,
isFocused: true
})
2016-06-27 17:16:18 -07:00
selection = selection.normalize(document)
state = state.merge({ selection })
2016-06-17 00:09:54 -07:00
this.onChange(state)
2016-06-15 14:47:52 -07:00
}
/**
* Render the editor content.
*
* @return {Component} component
*/
2016-06-15 12:07:12 -07:00
render() {
const { state } = this.props
const { document } = state
const children = document.nodes
2016-06-15 12:07:12 -07:00
.map(node => this.renderNode(node))
.toArray()
2016-06-15 12:07:12 -07:00
const style = {
2016-06-17 20:02:51 -07:00
outline: 'none', // prevent the default outline styles
whiteSpace: 'pre-wrap', // preserve adjacent whitespace and new lines
wordWrap: 'break-word' // allow words to break if they are too long
}
2016-06-15 12:07:12 -07:00
return (
<div
2016-06-24 12:06:59 -07:00
contentEditable suppressContentEditableWarning
style={style}
onBeforeInput={e => this.onBeforeInput(e)}
2016-06-28 16:13:50 -07:00
onBlur={e => this.onBlur(e)}
onCopy={e => this.onCopy(e)}
onCut={e => this.onCut(e)}
2016-06-27 17:16:18 -07:00
onKeyDown={e => this.onKeyDown(e)}
2016-06-24 12:06:59 -07:00
onPaste={e => this.onPaste(e)}
onSelect={e => this.onSelect(e)}
2016-06-15 12:07:12 -07:00
>
{children}
</div>
)
}
2016-06-15 14:47:52 -07:00
/**
2016-06-21 18:00:18 -07:00
* Render a `node`.
2016-06-15 14:47:52 -07:00
*
* @param {Node} node
* @return {Component} component
*/
2016-06-15 12:07:12 -07:00
renderNode(node) {
2016-06-21 18:00:18 -07:00
switch (node.kind) {
case 'block':
case 'inline':
return this.renderElement(node)
2016-06-28 15:47:29 -07:00
case 'text':
return this.renderText(node)
2016-06-21 18:00:18 -07:00
}
}
/**
* Render an element `node`.
*
* @param {Node} node
* @return {Component} component
*/
2016-06-15 12:07:12 -07:00
2016-06-21 18:00:18 -07:00
renderElement(node) {
const { renderNode, state } = this.props
2016-06-15 12:07:12 -07:00
const Component = renderNode(node)
2016-06-16 16:43:02 -07:00
const children = node.nodes
2016-06-21 18:00:18 -07:00
.map(child => this.renderNode(child))
.toArray()
2016-06-15 12:07:12 -07:00
return (
<Component
key={node.key}
2016-06-21 18:00:18 -07:00
node={node}
2016-06-15 19:46:53 -07:00
state={state}
2016-06-21 18:00:18 -07:00
>
{children}
</Component>
2016-06-15 12:07:12 -07:00
)
2016-06-28 15:47:29 -07:00
}
/**
* Render a text `node`.
*
* @param {Node} node
* @return {Component} component
*/
2016-06-21 18:00:18 -07:00
2016-06-28 15:47:29 -07:00
renderText(node) {
const { renderMark, renderNode, state } = this.props
return (
<Text
key={node.key}
node={node}
renderMark={renderMark}
state={state}
/>
)
2016-06-15 12:07:12 -07:00
}
}
/**
* Export.
*/
export default Content
2016-06-15 14:47:52 -07:00