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'
|
2016-06-27 19:15:20 -07:00
|
|
|
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 = {
|
2016-06-24 10:46:01 -07:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2016-06-27 19:15:20 -07:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param {Object} props
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.tmp = {}
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:05:38 -07:00
|
|
|
/**
|
|
|
|
* 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
|
2016-06-18 23:05:38 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:15:20 -07:00
|
|
|
/**
|
|
|
|
* On before input, bubble up.
|
|
|
|
*
|
|
|
|
* @param {Event} e
|
|
|
|
*/
|
|
|
|
|
|
|
|
onBeforeInput(e) {
|
|
|
|
this.props.onBeforeInput(e)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2016-06-27 19:15:20 -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
|
|
|
|
2016-06-27 19:15:20 -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.
|
2016-06-27 19:15:20 -07:00
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:15:20 -07:00
|
|
|
// 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) {
|
2016-06-27 19:15:20 -07:00
|
|
|
if (this.tmp.isCopying) return
|
|
|
|
|
2016-06-15 14:47:52 -07:00
|
|
|
let { state } = this.props
|
2016-06-18 18:21:21 -07:00
|
|
|
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) {
|
2016-06-18 18:21:21 -07:00
|
|
|
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)
|
2016-06-18 18:21:21 -07:00
|
|
|
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
|
2016-06-18 18:21:21 -07:00
|
|
|
const { document } = state
|
|
|
|
const children = document.nodes
|
2016-06-15 12:07:12 -07:00
|
|
|
.map(node => this.renderNode(node))
|
2016-06-18 18:21:21 -07:00
|
|
|
.toArray()
|
2016-06-15 12:07:12 -07:00
|
|
|
|
2016-06-16 12:12:50 -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-16 12:12:50 -07:00
|
|
|
}
|
|
|
|
|
2016-06-15 12:07:12 -07:00
|
|
|
return (
|
|
|
|
<div
|
2016-06-24 12:06:59 -07:00
|
|
|
contentEditable suppressContentEditableWarning
|
2016-06-16 12:12:50 -07:00
|
|
|
style={style}
|
2016-06-27 19:15:20 -07:00
|
|
|
onBeforeInput={e => this.onBeforeInput(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)}
|
2016-06-27 19:15:20 -07:00
|
|
|
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 'text':
|
|
|
|
return this.renderText(node)
|
|
|
|
case 'block':
|
|
|
|
case 'inline':
|
|
|
|
return this.renderElement(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a text `node`.
|
|
|
|
*
|
|
|
|
* @param {Node} node
|
|
|
|
* @return {Component} component
|
|
|
|
*/
|
|
|
|
|
|
|
|
renderText(node) {
|
2016-06-15 19:46:53 -07:00
|
|
|
const { renderMark, renderNode, state } = this.props
|
2016-06-21 18:00:18 -07:00
|
|
|
return (
|
|
|
|
<Text
|
|
|
|
key={node.key}
|
|
|
|
node={node}
|
|
|
|
renderMark={renderMark}
|
|
|
|
state={state}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2016-06-15 12:07:12 -07:00
|
|
|
|
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))
|
2016-06-18 18:21:21 -07:00
|
|
|
.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-21 18:00:18 -07:00
|
|
|
|
2016-06-15 12:07:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default Content
|
2016-06-15 14:47:52 -07:00
|
|
|
|