react-code-dataset / react-page /docs /examples /slate-plugin-src /hooks /useCurrentNodeDataWithPlugin.ts
Download react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeDataWithPlugin.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeDataWithPlugin.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeDataWithPlugin.ts
-
curl -L -o useCurrentNodeDataWithPlugin.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/hooks/useCurrentNodeDataWithPlugin.ts
1.11 kB
| import type { DataTType } from '@react-page/editor'; | |
| import type { Editor } from 'slate'; | |
| import { useSlate } from 'slate-react'; | |
| import type { SlatePluginDefinition } from '../types/slatePluginDefinitions'; | |
| import { getCurrentNodeWithPlugin } from './useCurrentNodeWithPlugin'; | |
| export const getCurrentNodeDataWithPlugin = <T extends DataTType>( | |
| editor: Editor, | |
| plugin: SlatePluginDefinition<T> | |
| ): T => { | |
| const currentNodeEntry = getCurrentNodeWithPlugin(editor, plugin); | |
| if (currentNodeEntry) { | |
| const currentNode = currentNodeEntry[0]; | |
| if (plugin.pluginType === 'component' && plugin.object === 'mark') { | |
| return (currentNode as Record<string, unknown>)[plugin.type] as T; | |
| } | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| const { data } = currentNode as any; | |
| return data as T; | |
| } else if (plugin.getInitialData) { | |
| return plugin.getInitialData(); | |
| } else { | |
| return {} as T; | |
| } | |
| }; | |
| export default <T extends DataTType>(plugin: SlatePluginDefinition<T>): T => { | |
| const editor = useSlate(); | |
| return getCurrentNodeDataWithPlugin(editor, plugin); | |
| }; | |