mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-15 20:55:24 +01:00
* rename state to value in slate core, as deprecation * rename all references to state to value in slate core * migrate slate-base64-serializer * migrate slate-html-serializer * migrate slate-hyperscript * migrate slate-plain-serializer * migrate slate-prop-types * migrate slate-simulator * fix change.setState compat * deprecate references to state in slate-react * remove all references to state in slate-react * remove `value` and `schema` from props to all components * fix default renderPlaceholder * fix tests * update examples * update walkthroughs * update guides * update reference
150 lines
2.7 KiB
JavaScript
150 lines
2.7 KiB
JavaScript
|
|
import { Editor } from 'slate-react'
|
|
import { Value } from 'slate'
|
|
|
|
import React from 'react'
|
|
import initialValue from './value.json'
|
|
|
|
/**
|
|
* The rich text example.
|
|
*
|
|
* @type {Component}
|
|
*/
|
|
|
|
class SearchHighlighting extends React.Component {
|
|
|
|
/**
|
|
* Deserialize the initial editor value.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
state = {
|
|
value: Value.fromJSON(initialValue),
|
|
}
|
|
|
|
/**
|
|
* On change, save the new `value`.
|
|
*
|
|
* @param {Change} change
|
|
*/
|
|
|
|
onChange = ({ value }) => {
|
|
this.setState({ value })
|
|
}
|
|
|
|
/**
|
|
* On input change, update the decorations.
|
|
*
|
|
* @param {Event} event
|
|
*/
|
|
|
|
onInputChange = (event) => {
|
|
const { value } = this.state
|
|
const string = event.target.value
|
|
const texts = value.document.getTexts()
|
|
const decorations = []
|
|
|
|
texts.forEach((node) => {
|
|
const { key, text } = node
|
|
const parts = text.split(string)
|
|
let offset = 0
|
|
|
|
parts.forEach((part, i) => {
|
|
if (i != 0) {
|
|
decorations.push({
|
|
anchorKey: key,
|
|
anchorOffset: offset - string.length,
|
|
focusKey: key,
|
|
focusOffset: offset,
|
|
marks: [{ type: 'highlight' }],
|
|
})
|
|
}
|
|
|
|
offset = offset + part.length + string.length
|
|
})
|
|
})
|
|
|
|
const change = value.change().setValue({ decorations })
|
|
this.onChange(change)
|
|
}
|
|
|
|
/**
|
|
* Render.
|
|
*
|
|
* @return {Element}
|
|
*/
|
|
|
|
render() {
|
|
return (
|
|
<div>
|
|
{this.renderToolbar()}
|
|
{this.renderEditor()}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Render the toolbar.
|
|
*
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderToolbar = () => {
|
|
return (
|
|
<div className="menu toolbar-menu">
|
|
<div className="search">
|
|
<span className="search-icon material-icons">search</span>
|
|
<input
|
|
className="search-box"
|
|
type="search"
|
|
placeholder="Search the text..."
|
|
onChange={this.onInputChange}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Render the Slate editor.
|
|
*
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderEditor = () => {
|
|
return (
|
|
<div className="editor">
|
|
<Editor
|
|
placeholder="Enter some rich text..."
|
|
value={this.state.value}
|
|
onChange={this.onChange}
|
|
renderMark={this.renderMark}
|
|
spellCheck
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Render a Slate mark.
|
|
*
|
|
* @param {Object} props
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderMark = (props) => {
|
|
const { children, mark } = props
|
|
switch (mark.type) {
|
|
case 'highlight': return <span style={{ backgroundColor: '#ffeeba' }}>{children}</span>
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Export.
|
|
*/
|
|
|
|
export default SearchHighlighting
|