Skip to content

Commit 3f77329

Browse files
committed
feat: support custom plate plugins and custom toolbars
Replaces contentful#702
1 parent a0c50a4 commit 3f77329

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/rich-text/src/RichTextEditor.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import noop from 'lodash/noop';
1111

1212
import { ContentfulEditorIdProvider, getContentfulEditorId } from './ContentfulEditorProvider';
1313
import { toSlateValue } from './helpers/toSlateValue';
14+
import { PlatePlugin } from './internal';
1415
import { normalizeInitialValue } from './internal/misc';
1516
import { getPlugins, disableCorePlugins } from './plugins';
1617
import { RichTextTrackingActionHandler } from './plugins/Tracking';
@@ -31,15 +32,20 @@ type ConnectedProps = {
3132
isToolbarHidden?: boolean;
3233
actionsDisabled?: boolean;
3334
restrictedMarks?: string[];
35+
customPlugins?: PlatePlugin[];
36+
customToolbars?: React.JSXElementConstructor<{
37+
isDisabled?: boolean;
38+
[index: string]: unknown;
39+
}>[];
3440
};
3541

3642
export const ConnectedRichTextEditor = (props: ConnectedProps) => {
3743
const { sdk, onAction, restrictedMarks } = props;
3844

3945
const id = getContentfulEditorId(sdk);
4046
const plugins = React.useMemo(
41-
() => getPlugins(sdk, onAction ?? noop, restrictedMarks),
42-
[sdk, onAction, restrictedMarks]
47+
() => getPlugins(sdk, onAction ?? noop, restrictedMarks, props?.customPlugins ?? []),
48+
[sdk, onAction, restrictedMarks, props?.customPlugins]
4349
);
4450

4551
const initialValue = React.useMemo(() => {
@@ -73,6 +79,15 @@ export const ConnectedRichTextEditor = (props: ConnectedProps) => {
7379
{!props.isToolbarHidden && (
7480
<StickyToolbarWrapper isDisabled={props.isDisabled}>
7581
<Toolbar isDisabled={props.isDisabled} />
82+
{/* Custom toolbars are placed underneath Contentful's built-in one */}
83+
{props?.customToolbars?.length &&
84+
props.customToolbars.map((ToolbarComponent, index) => {
85+
return (
86+
<React.Fragment key={index}>
87+
<ToolbarComponent isDisabled={props.isDisabled} />
88+
</React.Fragment>
89+
);
90+
})}
7691
</StickyToolbarWrapper>
7792
)}
7893
<SyncEditorChanges incomingValue={initialValue} onChange={props.onChange} />

packages/rich-text/src/plugins/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import { createVoidsPlugin } from './Voids';
3232
export const getPlugins = (
3333
sdk: FieldExtensionSDK,
3434
onAction: RichTextTrackingActionHandler,
35-
restrictedMarks?: string[]
35+
restrictedMarks?: string[],
36+
customPlugins?: PlatePlugin[]
3637
): PlatePlugin[] => [
3738
createDeserializeDocxPlugin(),
3839

@@ -79,6 +80,10 @@ export const getPlugins = (
7980
createExitBreakPlugin(),
8081
createResetNodePlugin(),
8182
createNormalizerPlugin(),
83+
84+
// We need to check if the plugins are defined before use because it is an
85+
// optional parameter.
86+
...(Array.isArray(customPlugins) ? customPlugins : []),
8287
];
8388

8489
export const disableCorePlugins: PlateProps['disableCorePlugins'] = {

0 commit comments

Comments
 (0)