react-code-dataset / react-page /docs /examples /slate-plugin-src /hooks /useCurrentNodeWithPlugin.ts
Download react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeWithPlugin.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeWithPlugin.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeWithPlugin.ts
-
curl -L -o useCurrentNodeWithPlugin.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeWithPlugin.ts
1.28 kB
| import type { DataTType } from '@react-page/editor'; | |
| import type { NodeEntry } from 'slate'; | |
| import { Editor } from 'slate'; | |
| import { useSlate } from 'slate-react'; | |
| import type { SlatePluginDefinition } from '../types/slatePluginDefinitions'; | |
| export const getCurrentNodeWithPlugin = <T extends DataTType>( | |
| editor: Editor, | |
| plugin: SlatePluginDefinition<T> | |
| ): NodeEntry | null => { | |
| if (plugin.pluginType === 'custom') { | |
| return null; | |
| } | |
| const match = | |
| plugin.pluginType === 'component' | |
| ? plugin.object === 'mark' | |
| ? (elem: any) => Boolean(elem[plugin.type]) | |
| : (elem: any) => elem.type === plugin.type | |
| : plugin.pluginType === 'data' | |
| ? // search for data | |
| ({ data }: any) => { | |
| const matches = plugin.dataMatches(data as T); | |
| return matches; | |
| } | |
| : null; | |
| if (!match) { | |
| return null; | |
| } | |
| try { | |
| const [matchingNode] = Editor.nodes(editor, { | |
| match: match, | |
| mode: 'lowest', // FIXME: whats the best value? | |
| }); | |
| return matchingNode; | |
| } catch (e) { | |
| // seems to crash sometimes on redu | |
| return null; | |
| } | |
| }; | |
| export default <T extends DataTType>(plugin: SlatePluginDefinition<T>) => { | |
| const editor = useSlate(); | |
| return getCurrentNodeWithPlugin(editor, plugin); | |
| }; | |