1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 09:13:24 +01:00
slate/lib/components/leaf.js

129 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-06-15 19:46:53 -07:00
2016-06-17 00:09:54 -07:00
import OffsetKey from '../utils/offset-key'
2016-06-15 19:46:53 -07:00
import React from 'react'
import ReactDOM from 'react-dom'
/**
* Leaf.
2016-06-15 19:46:53 -07:00
*/
class Leaf extends React.Component {
2016-06-15 19:46:53 -07:00
static propTypes = {
marks: React.PropTypes.object.isRequired,
2016-06-15 19:46:53 -07:00
node: React.PropTypes.object.isRequired,
2016-06-17 18:20:26 -07:00
start: React.PropTypes.number.isRequired,
end: React.PropTypes.number.isRequired,
2016-06-15 19:46:53 -07:00
renderMark: React.PropTypes.func.isRequired,
state: React.PropTypes.object.isRequired,
2016-06-17 18:20:26 -07:00
text: React.PropTypes.string.isRequired
2016-06-15 19:46:53 -07:00
};
componentDidMount() {
2016-06-15 19:46:53 -07:00
this.updateSelection()
}
componentDidUpdate() {
2016-06-15 19:46:53 -07:00
this.updateSelection()
}
updateSelection() {
const { state } = this.props
const { selection } = state
// If the selection is not focused we have nothing to do.
2016-06-15 20:13:02 -07:00
if (!selection.isFocused) return
2016-06-15 19:46:53 -07:00
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
2016-06-17 18:20:26 -07:00
const { node, start, end } = this.props
2016-06-15 19:46:53 -07:00
const { key } = node
// If neither matches, the selection doesn't start or end here, so exit.
const hasStart = key == anchorKey && start <= anchorOffset && anchorOffset <= end
const hasEnd = key == focusKey && start <= focusOffset && focusOffset <= end
if (!hasStart && !hasEnd) return
// We have a selection to render, so prepare a few things...
const native = window.getSelection()
const el = ReactDOM.findDOMNode(this).firstChild
// If both the start and end are here, set the selection all at once.
if (hasStart && hasEnd) {
native.removeAllRanges()
const range = document.createRange()
2016-06-17 18:36:47 -07:00
range.setStart(el, anchorOffset - start)
2016-06-15 19:46:53 -07:00
native.addRange(range)
2016-06-17 18:36:47 -07:00
native.extend(el, focusOffset - start)
2016-06-15 19:46:53 -07:00
return
}
// If the selection is forward, we can set things in sequence. In
// the first leaf to render, reset the selection and set the new start. And
// then in the second leaf to render, extend to the new end.
if (selection.isForward) {
if (hasStart) {
native.removeAllRanges()
const range = document.createRange()
2016-06-17 18:36:47 -07:00
range.setStart(el, anchorOffset - start)
2016-06-15 19:46:53 -07:00
native.addRange(range)
} else if (hasEnd) {
2016-06-17 18:36:47 -07:00
native.extend(el, focusOffset - start)
2016-06-15 19:46:53 -07:00
}
}
// Otherwise, if the selection is backward, we need to hack the order a bit.
// In the first leaf to render, set a phony start anchor to store the true
// end position. And then in the second leaf to render, set the start and
// extend the end to the stored value.
else {
if (hasEnd) {
native.removeAllRanges()
const range = document.createRange()
2016-06-17 18:36:47 -07:00
range.setStart(el, focusOffset - start)
2016-06-15 19:46:53 -07:00
native.addRange(range)
} else if (hasStart) {
const endNode = native.focusNode
const endOffset = native.focusOffset
native.removeAllRanges()
const range = document.createRange()
2016-06-17 18:36:47 -07:00
range.setStart(el, anchorOffset - start)
2016-06-15 19:46:53 -07:00
native.addRange(range)
native.extend(endNode, endOffset)
}
}
}
render() {
const { node, text, marks, start, end, renderMark } = this.props
2016-06-17 00:09:54 -07:00
const offsetKey = OffsetKey.stringify({
key: node.key,
2016-06-17 18:20:26 -07:00
start,
end
2016-06-17 00:09:54 -07:00
})
2016-06-15 19:46:53 -07:00
const style = marks.reduce((style, mark) => {
return {
...style,
...renderMark(mark),
}
}, {})
2016-06-15 19:46:53 -07:00
return (
<span
style={style}
2016-06-17 00:09:54 -07:00
data-offset-key={offsetKey}
2016-06-15 19:46:53 -07:00
data-type='leaf'
>
2016-06-17 18:20:26 -07:00
{text || <br/>}
2016-06-15 19:46:53 -07:00
</span>
)
}
}
/**
* Export.
*/
export default Leaf