mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-24 01:02:31 +01:00
* Experimental release to see if CustomTypes holds up through a publish * Add experimental release script * Fix lint * v0.60.5-alpha.0 * Allow null properties in setNodes * v0.60.6-alpha.0 * Revert null properties on Transforms.setNodes * v0.60.7-alpha.0 * Update examples to use custom Element and Text with discriminated unions * Add documentation for using TypeScript improvements * Be explicit about typescript version in package.json * Force lerna bootstrap to fix build issues on CI and fix a few type examples * Add slate devDependencies with * back * v0.60.7 * Switch to a non prerelease version to fix lerna not linking in root * Add documentation for not using prerelease versions and on how to create experimental releases * Try removing lerna bootstrap and see if it works
109 lines
2.7 KiB
TypeScript
109 lines
2.7 KiB
TypeScript
import React, { useState, useCallback, useMemo } from 'react'
|
|
import { Slate, Editable, withReact } from 'slate-react'
|
|
import { Text, Node, Descendant, createEditor } from 'slate'
|
|
import { css } from 'emotion'
|
|
import { withHistory } from 'slate-history'
|
|
|
|
import { Icon, Toolbar } from '../components'
|
|
|
|
const SearchHighlightingExample = () => {
|
|
const [value, setValue] = useState<Descendant[]>(initialValue)
|
|
const [search, setSearch] = useState<string | undefined>()
|
|
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
|
const decorate = useCallback(
|
|
([node, path]) => {
|
|
const ranges = []
|
|
|
|
if (search && Text.isText(node)) {
|
|
const { text } = node
|
|
const parts = text.split(search)
|
|
let offset = 0
|
|
|
|
parts.forEach((part, i) => {
|
|
if (i !== 0) {
|
|
ranges.push({
|
|
anchor: { path, offset: offset - search.length },
|
|
focus: { path, offset },
|
|
highlight: true,
|
|
})
|
|
}
|
|
|
|
offset = offset + part.length + search.length
|
|
})
|
|
}
|
|
|
|
return ranges
|
|
},
|
|
[search]
|
|
)
|
|
|
|
return (
|
|
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
|
<Toolbar>
|
|
<div
|
|
className={css`
|
|
position: relative;
|
|
`}
|
|
>
|
|
<Icon
|
|
className={css`
|
|
position: absolute;
|
|
top: 0.5em;
|
|
left: 0.5em;
|
|
color: #ccc;
|
|
`}
|
|
>
|
|
search
|
|
</Icon>
|
|
<input
|
|
type="search"
|
|
placeholder="Search the text..."
|
|
onChange={e => setSearch(e.target.value)}
|
|
className={css`
|
|
padding-left: 2em;
|
|
width: 100%;
|
|
`}
|
|
/>
|
|
</div>
|
|
</Toolbar>
|
|
<Editable decorate={decorate} renderLeaf={props => <Leaf {...props} />} />
|
|
</Slate>
|
|
)
|
|
}
|
|
|
|
const Leaf = ({ attributes, children, leaf }) => {
|
|
return (
|
|
<span
|
|
{...attributes}
|
|
className={css`
|
|
font-weight: ${leaf.bold && 'bold'};
|
|
background-color: ${leaf.highlight && '#ffeeba'};
|
|
`}
|
|
>
|
|
{children}
|
|
</span>
|
|
)
|
|
}
|
|
|
|
const initialValue: Descendant[] = [
|
|
{
|
|
type: 'paragraph',
|
|
children: [
|
|
{
|
|
text:
|
|
'This is editable text that you can search. As you search, it looks for matching strings of text, and adds ',
|
|
},
|
|
{ text: 'decorations', bold: true },
|
|
{ text: ' to them in realtime.' },
|
|
],
|
|
},
|
|
{
|
|
type: 'paragraph',
|
|
children: [
|
|
{ text: 'Try it out for yourself by typing in the search box above!' },
|
|
],
|
|
},
|
|
]
|
|
|
|
export default SearchHighlightingExample
|