-
Notifications
You must be signed in to change notification settings - Fork 582
/
PbText.tsx
85 lines (75 loc) · 2.84 KB
/
PbText.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
import React, { useCallback, useMemo } from "react";
import { useRecoilState, useRecoilValue } from "recoil";
import get from "lodash/get";
import classNames from "classnames";
import { CoreOptions } from "medium-editor";
import { PbEditorElement } from "~/types";
import { elementWithChildrenByIdSelector, activeElementAtom, uiAtom } from "../../recoil/modules";
import { ElementRoot } from "~/render/components/ElementRoot";
import useUpdateHandlers from "../../plugins/elementSettings/useUpdateHandlers";
import ReactMediumEditor from "../../components/MediumEditor";
import { applyFallbackDisplayMode } from "../../plugins/elementSettings/elementSettingsUtils";
export const textClassName = "webiny-pb-base-page-element-style webiny-pb-page-element-text";
const DATA_NAMESPACE = "data.text";
type TextElementProps = {
elementId: string;
mediumEditorOptions: CoreOptions;
rootClassName?: string;
};
const PbText: React.FunctionComponent<TextElementProps> = ({
elementId,
mediumEditorOptions,
rootClassName
}) => {
const element: PbEditorElement = useRecoilValue(elementWithChildrenByIdSelector(elementId));
const [{ displayMode }] = useRecoilState(uiAtom);
const [activeElementId, setActiveElementAtomValue] = useRecoilState(activeElementAtom);
const { getUpdateValue } = useUpdateHandlers({
element,
dataNamespace: DATA_NAMESPACE,
debounce: false
});
const fallbackValue = useMemo(
() =>
applyFallbackDisplayMode(displayMode, mode =>
get(element, `${DATA_NAMESPACE}.${mode}`)
),
[displayMode]
);
const value = get(element, `${DATA_NAMESPACE}.${displayMode}`, fallbackValue);
const onChange = useCallback(
value => {
getUpdateValue(DATA_NAMESPACE)(value);
},
[getUpdateValue]
);
const onSelect = useCallback(() => {
// Mark element active on editor element selection
if (elementId && activeElementId !== elementId) {
setActiveElementAtomValue(elementId);
}
}, [activeElementId, elementId]);
// required due to re-rendering when set content atom and still nothing in elements atom
if (!element) {
return null;
}
const textContent = get(element, `${DATA_NAMESPACE}.data.text`);
const tag = get(value, "tag");
const typography = get(value, "typography");
return (
<ElementRoot
element={element}
className={classNames(textClassName, rootClassName, typography)}
>
<ReactMediumEditor
elementId={elementId}
tag={tag}
value={textContent}
onChange={onChange}
options={mediumEditorOptions}
onSelect={onSelect}
/>
</ElementRoot>
);
};
export default React.memo(PbText);