-
Notifications
You must be signed in to change notification settings - Fork 117
/
RichTextEditor.tsx
150 lines (134 loc) · 4.88 KB
/
RichTextEditor.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
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, { useCallback, useState, useEffect } from 'react';
import { FieldExtensionSDK } from '@contentful/app-sdk';
import { EntityProvider } from '@contentful/field-editor-reference';
import { FieldConnector } from '@contentful/field-editor-shared';
import * as Contentful from '@contentful/rich-text-types';
import { Plate, getPlateSelectors, getPlateActions } from '@udecode/plate-core';
import { css, cx } from 'emotion';
import deepEquals from 'fast-deep-equal';
import noop from 'lodash/noop';
import { ContentfulEditorIdProvider, getContentfulEditorId } from './ContentfulEditorProvider';
import { getPlugins, disableCorePlugins } from './plugins';
import { RichTextTrackingActionHandler } from './plugins/Tracking';
import { documentToEditorValue, normalizeEditorValue, setEditorContent } from './prepareDocument';
import { styles } from './RichTextEditor.styles';
import { SdkProvider } from './SdkProvider';
import Toolbar from './Toolbar';
import StickyToolbarWrapper from './Toolbar/components/StickyToolbarWrapper';
import { useOnValueChanged } from './useOnValueChanged';
type ConnectedProps = {
sdk: FieldExtensionSDK;
onAction?: RichTextTrackingActionHandler;
minHeight?: string | number;
value?: object;
isDisabled?: boolean;
onChange?: (doc: Contentful.Document) => unknown;
isToolbarHidden?: boolean;
actionsDisabled?: boolean;
};
export const ConnectedRichTextEditor = (props: ConnectedProps) => {
const id = getContentfulEditorId(props.sdk);
const plugins = React.useMemo(
() => getPlugins(props.sdk, props.onAction ?? noop),
[props.sdk, props.onAction]
);
const [isFirstRender, setIsFirstRender] = useState(true);
const [pendingExternalUpdate, setPendingExternalUpdate] = useState(false);
const onValueChanged = useOnValueChanged({
editorId: id,
handler: props.onChange,
skip: pendingExternalUpdate || isFirstRender,
onSkip: () => setPendingExternalUpdate(false),
});
useEffect(() => {
/*
This effect is called when the value prop changes. Normally
this happens when the value is changed outside of the editor,
like in snapshots restoration or remote updates
Plate won't update the displayed value on its own, see:
- https://github.com/ianstormtaylor/slate/pull/4540
- https://github.com/udecode/plate/issues/1169
The content is forcefully set to the new value and it's ensured
the change listener isn't invoked
*/
setIsFirstRender(false);
const editor = getPlateSelectors(id).editor();
if (!editor) {
return;
}
setPendingExternalUpdate(true);
setEditorContent(editor, documentToEditorValue(props.value));
}, [props.value, id]);
const classNames = cx(
styles.editor,
props.minHeight !== undefined ? css({ minHeight: props.minHeight }) : undefined,
props.isDisabled ? styles.disabled : styles.enabled,
props.isToolbarHidden && styles.hiddenToolbar
);
useEffect(() => {
if (!isFirstRender) {
return;
}
getPlateActions(id).value(
normalizeEditorValue(documentToEditorValue(props.value), {
plugins,
disableCorePlugins,
})
);
}, [isFirstRender, plugins, id, props.value]);
return (
<SdkProvider sdk={props.sdk}>
<ContentfulEditorIdProvider value={id}>
<div className={styles.root} data-test-id="rich-text-editor">
<Plate
id={id}
plugins={plugins}
disableCorePlugins={disableCorePlugins}
editableProps={{
className: classNames,
readOnly: props.isDisabled,
}}
onChange={onValueChanged}>
{!props.isToolbarHidden && (
<StickyToolbarWrapper isDisabled={props.isDisabled}>
<Toolbar isDisabled={props.isDisabled} />
</StickyToolbarWrapper>
)}
</Plate>
</div>
</ContentfulEditorIdProvider>
</SdkProvider>
);
};
type Props = ConnectedProps & { isInitiallyDisabled: boolean };
const RichTextEditor = (props: Props) => {
const { sdk, isInitiallyDisabled, onAction, ...otherProps } = props;
const isEmptyValue = useCallback(
(value) => !value || deepEquals(value, Contentful.EMPTY_DOCUMENT),
[]
);
const id = getContentfulEditorId(props.sdk);
return (
<EntityProvider sdk={sdk}>
<FieldConnector
throttle={0}
field={sdk.field}
isInitiallyDisabled={isInitiallyDisabled}
isEmptyValue={isEmptyValue}
isEqualValues={deepEquals}>
{({ lastRemoteValue, disabled, setValue }) => (
<ConnectedRichTextEditor
{...otherProps}
key={`rich-text-editor-${id}`}
value={lastRemoteValue}
sdk={sdk}
onAction={onAction}
isDisabled={disabled}
onChange={setValue}
/>
)}
</FieldConnector>
</EntityProvider>
);
};
export default RichTextEditor;