-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b1ea0d
commit aca891d
Showing
9 changed files
with
365 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* eslint-disable react/no-children-prop */ | ||
import React from 'react'; | ||
|
||
import type { RenderElementFn, RenderLeafFn } from '@udecode/slate-react'; | ||
import type { | ||
EditableProps, | ||
RenderElementProps, | ||
RenderLeafProps, | ||
} from 'slate-react/dist/components/editable'; | ||
|
||
import { isObject } from 'lodash'; | ||
import { TDescendant, TElement, TText } from '@udecode/slate'; | ||
import { pipeRenderElement, pipeRenderLeaf } from '../utils'; | ||
import { PlateEditor } from '../editor'; | ||
|
||
export type ChildrenProps = { | ||
children: TDescendant[]; | ||
}; | ||
|
||
export type ElementProps = { | ||
element: TElement; | ||
}; | ||
|
||
export type LeafProps = { | ||
leaf: TText; | ||
}; | ||
|
||
export type PlateViewContextProps = { | ||
editor: PlateEditor; | ||
renderElement: RenderElementFn; | ||
renderLeaf: RenderLeafFn; | ||
}; | ||
|
||
export type PlateViewProps = { | ||
renderElement?: EditableProps['renderElement']; | ||
renderLeaf?: EditableProps['renderLeaf']; | ||
editor: PlateEditor; | ||
}; | ||
|
||
const PlateViewContext = React.createContext<PlateViewContextProps | null>( | ||
null | ||
); | ||
|
||
export function usePlateView() { | ||
return React.useContext(PlateViewContext) as PlateViewContextProps; | ||
} | ||
|
||
function Element({ element = { children: [], type: '' } }: ElementProps) { | ||
const { editor } = usePlateView(); | ||
|
||
const renderElement = pipeRenderElement(editor); | ||
|
||
return ( | ||
<React.Fragment> | ||
{renderElement?.({ | ||
attributes: {} as any, | ||
children: <PlateViewContent children={element.children} />, | ||
element, | ||
})} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
function Leaf({ leaf = { text: '' } }: LeafProps) { | ||
const { editor } = usePlateView(); | ||
|
||
const renderLeaf = pipeRenderLeaf(editor); | ||
|
||
return renderLeaf!({ | ||
attributes: {} as any, | ||
children: <span>{leaf.text === '' ? '\uFEFF' : leaf.text}</span>, | ||
leaf, | ||
text: leaf, | ||
}); | ||
} | ||
|
||
function PlateViewContent({ children = [] }: ChildrenProps) { | ||
return ( | ||
<React.Fragment> | ||
{children.map((child, i) => { | ||
return isElement(child) ? ( | ||
<Element key={i} element={child} /> | ||
) : ( | ||
<Leaf key={i} leaf={child} /> | ||
); | ||
})} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export function PlateView(props: PlateViewProps) { | ||
const { | ||
renderElement = (props) => <DefaultElement {...props} />, | ||
renderLeaf = (props) => <DefaultLeaf {...props} />, | ||
editor, | ||
} = props; | ||
|
||
return ( | ||
<PlateViewContext.Provider value={{ editor, renderElement, renderLeaf }}> | ||
<PlateViewContent children={editor.children} /> | ||
</PlateViewContext.Provider> | ||
); | ||
} | ||
|
||
function DefaultElement({ children }: RenderElementProps) { | ||
return <div>{children}</div>; | ||
} | ||
|
||
function DefaultLeaf({ children }: RenderLeafProps) { | ||
return <span>{children}</span>; | ||
} | ||
|
||
export function isText(value: any): value is Text { | ||
return isObject(value) && 'text' in value && typeof value.text === 'string'; | ||
} | ||
|
||
export function isElement(value: any): value is TElement { | ||
return ( | ||
isObject(value) && 'children' in value && Array.isArray(value.children) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.