-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHighlights.stories.tsx
137 lines (120 loc) · 3.22 KB
/
Highlights.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Meta, Story } from '@storybook/react/types-6-0';
import React, { useCallback, useState } from 'react';
import { Descendant, Range, Text } from 'slate';
import { Editable, Slate } from 'slate-react';
import { SlateExtension, useSlateState, useSlateWithExtensions } from '../.';
export default {
title: 'Examples/Highlights',
} as Meta;
export const Default: Story = () => {
const [value, onChange] = useSlateState(initialValue);
// define the extension
const { getSearchBarProps, ...highlightExtension } = useHighlightExtension(
'search'
);
// create slate with the extension
const { getEditableProps, getSlateProps } = useSlateWithExtensions({
onChange,
value,
extensions: [highlightExtension],
});
// render the extension and the search bar
return (
<Slate {...getSlateProps()}>
<SearchBar {...getSearchBarProps()} />
<br />
<Editable {...getEditableProps()} />
</Slate>
);
};
export const useHighlightExtension = (
initialSearch?: string
): SlateExtension & {
getSearchBarProps: () => SearchBarProps;
} => {
// state for the search text
const [search, setSearch] = useState<string | undefined>(initialSearch);
// decorate function for highlighting search text
const decorate = useCallback<NonNullable<SlateExtension['decorate']>>(
([node, path]) => {
const ranges: Range[] = [];
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]
);
// render leaf for rendering search text
const renderLeaf = useCallback<NonNullable<SlateExtension['renderLeaf']>>(
({ leaf, children }) => {
if (leaf.highlight) {
return <span style={{ backgroundColor: '#ffeeba' }}>{children}</span>;
}
},
[]
);
// create the search bar props
const getSearchBarProps = useCallback<() => SearchBarProps>(
() => ({
onSearchChange: setSearch,
search: search ?? '',
}),
[search]
);
return {
decorate,
decorateDeps: [decorate],
renderLeaf,
renderLeafDeps: [renderLeaf],
getSearchBarProps,
};
};
interface SearchBarProps {
search: string;
onSearchChange: (search: string) => void;
}
export const SearchBar: React.FC<SearchBarProps> = ({
search,
onSearchChange,
}) => {
return (
<label>
Search Text{' '}
<input
type="text"
value={search}
onChange={e => onSearchChange(e.target.value)}
/>
</label>
);
};
const initialValue: Descendant[] = [
{
children: [
{
text:
'This is editable text that you can search. As you search, it looks for matching strings of text, and adds ',
},
{ text: 'decorations' },
{ text: ' to them in realtime.' },
],
},
{
children: [
{ text: 'Try it out for yourself by typing in the search box above!' },
],
},
];