@pega/cosmos-react-rte 3.0.0-dev.2.0 → 3.0.0-dev.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/Editor/Editor.d.ts +3 -0
- package/lib/components/Editor/Editor.d.ts.map +1 -1
- package/lib/components/Editor/Editor.js +53 -32
- package/lib/components/Editor/Editor.js.map +1 -1
- package/lib/components/Editor/Editor.types.d.ts +19 -7
- package/lib/components/Editor/Editor.types.d.ts.map +1 -1
- package/lib/components/Editor/Editor.types.js.map +1 -1
- package/lib/components/Editor/ImageEditor.d.ts +9 -0
- package/lib/components/Editor/ImageEditor.d.ts.map +1 -0
- package/lib/components/Editor/ImageEditor.js +268 -0
- package/lib/components/Editor/ImageEditor.js.map +1 -0
- package/lib/components/Editor/Toolbar/AnchorButton.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/AnchorButton.js +3 -2
- package/lib/components/Editor/Toolbar/AnchorButton.js.map +1 -1
- package/lib/components/Editor/Toolbar/TextSelect.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/TextSelect.js +2 -1
- package/lib/components/Editor/Toolbar/TextSelect.js.map +1 -1
- package/lib/components/Editor/Toolbar/Toolbar.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/Toolbar.js +2 -2
- package/lib/components/Editor/Toolbar/Toolbar.js.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Table.d.ts +0 -2
- package/lib/components/RichTextEditor/DecoratorComponents/Table.d.ts.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Table.js +2 -29
- package/lib/components/RichTextEditor/DecoratorComponents/Table.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.js +7 -1
- package/lib/components/RichTextEditor/RichTextEditor.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts +0 -2
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.js +9 -107
- package/lib/components/RichTextEditor/RichTextEditor.styles.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.js +4 -10
- package/lib/components/RichTextEditor/RichTextViewer.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.types.d.ts +3 -3
- package/lib/components/RichTextEditor/RichTextViewer.types.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.types.js.map +1 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.js +5 -2
- package/lib/components/RichTextEditor/utils/htmlConverter.js.map +1 -1
- package/lib/components/RichTextEditor/utils/markdownConverter.js +2 -2
- package/lib/components/RichTextEditor/utils/markdownConverter.js.map +1 -1
- package/lib/components/RichTextEditor/utils/renderers.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/renderers.js +14 -1
- package/lib/components/RichTextEditor/utils/renderers.js.map +1 -1
- package/lib/components/RichTextEditor/utils/slateConverter.d.ts +1 -0
- package/lib/components/RichTextEditor/utils/slateConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/slateConverter.js +1 -0
- package/lib/components/RichTextEditor/utils/slateConverter.js.map +1 -1
- package/package.json +12 -13
|
@@ -5,6 +5,9 @@ import 'tinymce/plugins/lists';
|
|
|
5
5
|
import { FC } from 'react';
|
|
6
6
|
import { ForwardProps } from '@pega/cosmos-react-core';
|
|
7
7
|
import { EditorProps } from './Editor.types';
|
|
8
|
+
export declare const StyledInnerEditor: import("styled-components").StyledComponent<"div", import("styled-components").DefaultTheme, {
|
|
9
|
+
showPlaceholder: boolean;
|
|
10
|
+
}, never>;
|
|
8
11
|
declare const Editor: FC<EditorProps & ForwardProps>;
|
|
9
12
|
export default Editor;
|
|
10
13
|
//# sourceMappingURL=Editor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Editor.d.ts","sourceRoot":"","sources":["../../../src/components/Editor/Editor.tsx"],"names":[],"mappings":"AACA,OAAO,uBAAuB,CAAC;AAC/B,OAAO,uBAAuB,CAAC;AAC/B,OAAO,yBAAyB,CAAC;AACjC,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EACL,EAAE,EASH,MAAM,OAAO,CAAC;AAGf,OAAO,EAIL,YAAY,
|
|
1
|
+
{"version":3,"file":"Editor.d.ts","sourceRoot":"","sources":["../../../src/components/Editor/Editor.tsx"],"names":[],"mappings":"AACA,OAAO,uBAAuB,CAAC;AAC/B,OAAO,uBAAuB,CAAC;AAC/B,OAAO,yBAAyB,CAAC;AACjC,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EACL,EAAE,EASH,MAAM,OAAO,CAAC;AAGf,OAAO,EAIL,YAAY,EAQb,MAAM,yBAAyB,CAAC;AAUjC,OAAO,EAAE,WAAW,EAAe,MAAM,gBAAgB,CAAC;AAG1D,eAAO,MAAM,iBAAiB;qBAAiC,OAAO;SAkBrE,CAAC;AAIF,QAAA,MAAM,MAAM,EAAE,EAAE,CAAC,WAAW,GAAG,YAAY,CAkS1C,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -6,22 +6,28 @@ import 'tinymce/plugins/advlist';
|
|
|
6
6
|
import 'tinymce/plugins/lists';
|
|
7
7
|
import { forwardRef, useEffect, useRef, useState, useLayoutEffect, useImperativeHandle } from 'react';
|
|
8
8
|
import styled, { css } from 'styled-components';
|
|
9
|
-
import { createUID, FormControl, FormField, useAfterInitialEffect, useTheme, useUID, useI18n, useConsolidatedRef, defaultThemeProp } from '@pega/cosmos-react-core';
|
|
10
|
-
import StyledRichTextEditor, { StyledEditorContainer, StyledRichTextFormField
|
|
9
|
+
import { createUID, FormControl, FormField, HTML, useAfterInitialEffect, useTheme, useUID, useI18n, useConsolidatedRef, defaultThemeProp } from '@pega/cosmos-react-core';
|
|
10
|
+
import StyledRichTextEditor, { StyledEditorContainer, StyledRichTextFormField } from '../RichTextEditor/RichTextEditor.styles';
|
|
11
11
|
import Toolbar from './Toolbar/Toolbar';
|
|
12
12
|
import EditorContext from './Editor.context';
|
|
13
13
|
import { getTextFormats } from './Toolbar/TextSelect';
|
|
14
|
-
|
|
14
|
+
import ImageEditor, { imgHoverClass } from './ImageEditor';
|
|
15
|
+
export const StyledInnerEditor = styled.div(({ theme, showPlaceholder }) => {
|
|
15
16
|
return css `
|
|
16
|
-
|
|
17
|
+
${showPlaceholder &&
|
|
17
18
|
css `
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
::before {
|
|
20
|
+
color: ${theme.base.colors.slate.medium};
|
|
21
|
+
position: absolute;
|
|
22
|
+
content: attr(data-mce-placeholder);
|
|
23
|
+
}
|
|
24
|
+
`}
|
|
25
|
+
img:hover,
|
|
26
|
+
img.${imgHoverClass} {
|
|
27
|
+
box-shadow: 0 0 0 0.06125rem ${theme.base.palette['primary-background']},
|
|
28
|
+
0 0 0 0.125rem ${theme.base.palette.interactive}, 0 0 0 0.25rem rgba(0, 118, 209, 0.3);
|
|
22
29
|
}
|
|
23
|
-
|
|
24
|
-
`;
|
|
30
|
+
`;
|
|
25
31
|
});
|
|
26
32
|
StyledInnerEditor.defaultProps = defaultThemeProp;
|
|
27
33
|
const Editor = forwardRef((props, ref) => {
|
|
@@ -31,8 +37,9 @@ const Editor = forwardRef((props, ref) => {
|
|
|
31
37
|
const uid = useUID();
|
|
32
38
|
const labelId = useUID();
|
|
33
39
|
const editorRef = useConsolidatedRef(ref);
|
|
40
|
+
const tinyMceRef = useRef(null);
|
|
34
41
|
const initialized = useRef(false);
|
|
35
|
-
const { id = uid, toolbar = [], label, labelHidden, info, status, required, disabled, readOnly, onChange, onFocus, onBlur, onImageAdded, onInit, defaultValue, customComponents = [], height = { min: minHeight }, customActions, placeholder, ...restProps } = props;
|
|
42
|
+
const { id = uid, toolbar = [], label, labelHidden, info, status, required, disabled, readOnly, onChange, onKeyDown, onFocus, onBlur, onImageAdded, onInit, defaultValue, customComponents = [], height = { min: minHeight }, customActions, placeholder, children, ...restProps } = props;
|
|
36
43
|
const [showPlaceholder, setShowPlaceholder] = useState(() => !defaultValue);
|
|
37
44
|
const formFieldRef = useRef(null);
|
|
38
45
|
const value = useRef(defaultValue);
|
|
@@ -41,13 +48,13 @@ const Editor = forwardRef((props, ref) => {
|
|
|
41
48
|
editor?.focus();
|
|
42
49
|
},
|
|
43
50
|
getPlainText: () => {
|
|
44
|
-
return editor ? editor
|
|
51
|
+
return editor ? editor.getContent({ format: 'text' }) : '';
|
|
45
52
|
},
|
|
46
53
|
getRichText: () => {
|
|
47
|
-
return editor ? JSON.stringify(editor
|
|
54
|
+
return editor ? JSON.stringify(editor.getContent({ format: 'raw' })) : '';
|
|
48
55
|
},
|
|
49
56
|
getHtml: () => {
|
|
50
|
-
return editor ? editor
|
|
57
|
+
return editor ? editor.getContent({ format: 'html' }) : '';
|
|
51
58
|
},
|
|
52
59
|
clear: () => {
|
|
53
60
|
editor?.setContent('');
|
|
@@ -73,14 +80,17 @@ const Editor = forwardRef((props, ref) => {
|
|
|
73
80
|
},
|
|
74
81
|
getEditor: () => {
|
|
75
82
|
return editor;
|
|
76
|
-
}
|
|
83
|
+
},
|
|
84
|
+
element: formFieldRef.current || undefined
|
|
77
85
|
}), [editor]);
|
|
78
86
|
useLayoutEffect(() => {
|
|
87
|
+
if (!tinyMceRef.current)
|
|
88
|
+
return;
|
|
79
89
|
tinymce
|
|
80
90
|
.init({
|
|
81
91
|
readonly: readOnly || disabled,
|
|
82
92
|
skin: false,
|
|
83
|
-
|
|
93
|
+
target: tinyMceRef.current,
|
|
84
94
|
toolbar: false,
|
|
85
95
|
menubar: false,
|
|
86
96
|
inline: true,
|
|
@@ -95,10 +105,13 @@ const Editor = forwardRef((props, ref) => {
|
|
|
95
105
|
}
|
|
96
106
|
});
|
|
97
107
|
},
|
|
98
|
-
custom_elements: customComponents.reduce((prev, { name }, i) => {
|
|
108
|
+
custom_elements: `~${customComponents.reduce((prev, { name }, i) => {
|
|
99
109
|
return i === 0 ? name : `${prev},${name}`;
|
|
100
|
-
}, '')
|
|
110
|
+
}, '')}`,
|
|
101
111
|
setup: editorSettings => {
|
|
112
|
+
editorSettings.on('keydown', e => {
|
|
113
|
+
onKeyDown?.(e);
|
|
114
|
+
});
|
|
102
115
|
getTextFormats().forEach((format, i) => {
|
|
103
116
|
editorSettings.addShortcut(`meta+alt+${i}`, `${t('rte_change_text_format')} ${format.text}`, () => {
|
|
104
117
|
editorSettings.execCommand('FormatBlock', false, format.type);
|
|
@@ -113,6 +126,13 @@ const Editor = forwardRef((props, ref) => {
|
|
|
113
126
|
editorSettings.addShortcut('alt+shift+m', t('rte_unindent_selection'), () => {
|
|
114
127
|
editorSettings.execCommand('Outdent');
|
|
115
128
|
});
|
|
129
|
+
customActions?.forEach(action => {
|
|
130
|
+
if (action.shortcut) {
|
|
131
|
+
editorSettings.addShortcut(action.shortcut.pattern, action.shortcut.description, () => {
|
|
132
|
+
action.shortcut?.command(editorSettings);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
});
|
|
116
136
|
}
|
|
117
137
|
})
|
|
118
138
|
.then(tinymceEditors => {
|
|
@@ -129,7 +149,8 @@ const Editor = forwardRef((props, ref) => {
|
|
|
129
149
|
if (!editor)
|
|
130
150
|
return;
|
|
131
151
|
const onEditorChange = () => {
|
|
132
|
-
|
|
152
|
+
onChange?.(editor);
|
|
153
|
+
setShowPlaceholder(!editor.getContent({ format: 'text' }));
|
|
133
154
|
};
|
|
134
155
|
const onEditorFocus = () => {
|
|
135
156
|
onFocus?.();
|
|
@@ -173,19 +194,19 @@ const Editor = forwardRef((props, ref) => {
|
|
|
173
194
|
};
|
|
174
195
|
}, [editor]);
|
|
175
196
|
const renderToolbar = editor && toolbar.length > 0 && !readOnly && !disabled;
|
|
176
|
-
return (
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
197
|
+
return (_jsxs(EditorContext.Provider, { value: { addImage, editor: editor || {} }, children: [_jsx(FormField, { as: StyledRichTextFormField, toolbar: renderToolbar, label:
|
|
198
|
+
// eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events
|
|
199
|
+
_jsx("span", { onClick: () => {
|
|
200
|
+
editor?.focus();
|
|
201
|
+
}, children: label }, void 0), labelFor: '', labelId: labelId, labelHidden: labelHidden, id: id, info: info, status: status, required: required, disabled: disabled, readOnly: readOnly, onFocus: (e) => {
|
|
202
|
+
if (e.target === formFieldRef.current && !disabled) {
|
|
203
|
+
editor?.focus();
|
|
204
|
+
}
|
|
205
|
+
}, ref: formFieldRef, tabIndex: disabled ? -1 : undefined, children: _jsxs(FormControl, { ...restProps, ref: editorRef, required: required, disabled: disabled, readOnly: readOnly, status: status, as: StyledRichTextEditor, toolbar: renderToolbar, children: [_jsxs(StyledEditorContainer, { readOnly: readOnly, onClick: () => {
|
|
206
|
+
if (!disabled) {
|
|
207
|
+
editor?.focus();
|
|
208
|
+
}
|
|
209
|
+
}, toolbar: renderToolbar, containerHeight: height, children: [_jsx(HTML, { as: StyledInnerEditor, id: id, style: { outline: 'none', border: 'none' }, content: value.current || '', "aria-label": `${label}${renderToolbar ? `. ${t('rte_toolbar_instructions')}.` : ''}`, showPlaceholder: showPlaceholder, "aria-labelledby": labelId, role: 'textbox', ref: tinyMceRef }, void 0), tinyMceRef.current && editor && (_jsx(ImageEditor, { editor: editor, editorEl: tinyMceRef.current }, void 0))] }, void 0), renderToolbar && (_jsx(Toolbar, { features: toolbar, editor: editor, customActions: customActions }, void 0))] }, void 0) }, void 0), children] }, void 0));
|
|
189
210
|
});
|
|
190
211
|
export default Editor;
|
|
191
212
|
//# sourceMappingURL=Editor.js.map
|
|
@@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jB;YACH,CAAC,EACD,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,YAEnC,MAAC,WAAW,OACN,SAAS,EACb,GAAG,EAAE,SAAS,EACd,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,oBAAoB,EACxB,OAAO,EAAE,aAAa,aAEtB,KAAC,qBAAqB,IACpB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,GAAG,EAAE;4BACZ,IAAI,CAAC,QAAQ,EAAE;gCACb,MAAM,EAAE,KAAK,EAAE,CAAC;6BACjB;wBACH,CAAC,EACD,OAAO,EAAE,aAAa,EACtB,eAAe,EAAE,MAAM,YAEvB,KAAC,UAAU,IACT,EAAE,EAAE,EAAE,EACN,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAC1C,uBAAuB,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,OAAO,IAAI,EAAE,EAAE,gBAC5C,GAAG,KAAK,GAAG,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,0BAA0B,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,EACnF,EAAE,EAAE,iBAAiB,EACrB,eAAe,EAAE,eAAe,qBACf,OAAO,EACxB,IAAI,EAAC,SAAS,WACd,WACoB,EACvB,aAAa,IAAI,CAChB,KAAC,OAAO,IAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,aAAa,WAAI,CAC7E,YACW,WACJ,WACW,CAC1B,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,MAAM,CAAC","sourcesContent":["import tinymce, { Editor as TinymceEditor } from 'tinymce/tinymce';\nimport 'tinymce/icons/default';\nimport 'tinymce/themes/silver';\nimport 'tinymce/plugins/advlist';\nimport 'tinymce/plugins/lists';\nimport {\n FC,\n forwardRef,\n PropsWithoutRef,\n useEffect,\n useRef,\n useState,\n useLayoutEffect,\n useImperativeHandle,\n FocusEvent\n} from 'react';\nimport styled, { css } from 'styled-components';\n\nimport {\n createUID,\n FormControl,\n FormField,\n ForwardProps,\n useAfterInitialEffect,\n useTheme,\n useUID,\n useI18n,\n useConsolidatedRef,\n defaultThemeProp\n} from '@pega/cosmos-react-core';\n\nimport StyledRichTextEditor, {\n StyledEditorContainer,\n StyledRichTextFormField,\n StyledHtml\n} from '../RichTextEditor/RichTextEditor.styles';\n\nimport Toolbar from './Toolbar/Toolbar';\nimport EditorContext from './Editor.context';\nimport { getTextFormats } from './Toolbar/TextSelect';\nimport { EditorProps, EditorState } from './Editor.types';\n\nconst StyledInnerEditor = styled.div<{ showPlaceholder: boolean }>(({ theme, showPlaceholder }) => {\n return css`\n ${showPlaceholder &&\n css`\n ::before {\n color: ${theme.base.colors.slate.medium};\n position: absolute;\n content: attr(data-mce-placeholder);\n }\n `}\n `;\n});\n\nStyledInnerEditor.defaultProps = defaultThemeProp;\n\nconst Editor: FC<EditorProps & ForwardProps> = forwardRef(\n (props: PropsWithoutRef<EditorProps>, ref: EditorProps['ref']) => {\n const {\n components: {\n 'text-area': { 'min-height': minHeight }\n }\n } = useTheme();\n const [editor, setEditor] = useState<TinymceEditor>();\n const t = useI18n();\n const uid = useUID();\n const labelId = useUID();\n const editorRef = useConsolidatedRef(ref);\n const initialized = useRef(false);\n\n const {\n id = uid,\n toolbar = [],\n label,\n labelHidden,\n info,\n status,\n required,\n disabled,\n readOnly,\n onChange,\n onFocus,\n onBlur,\n onImageAdded,\n onInit,\n defaultValue,\n customComponents = [],\n height = { min: minHeight },\n customActions,\n placeholder,\n ...restProps\n } = props;\n\n const [showPlaceholder, setShowPlaceholder] = useState(() => !defaultValue);\n const formFieldRef = useRef<HTMLElement>(null);\n const value = useRef(defaultValue);\n\n useImperativeHandle<any, EditorState>(\n editorRef,\n () => ({\n focus: () => {\n editor?.focus();\n },\n getPlainText: () => {\n return editor ? editor?.getContent({ format: 'text' }) : '';\n },\n getRichText: () => {\n return editor ? JSON.stringify(editor?.getContent({ format: 'raw' })) : '';\n },\n getHtml: () => {\n return editor ? editor?.getContent({ format: 'html' }) : '';\n },\n clear: () => {\n editor?.setContent('');\n },\n appendImage: ({ src, alt }, imageId) => {\n if (editor) {\n const editorEl = editor.getElement();\n const imageEl = editorEl.querySelector(`#${imageId}`);\n if (imageEl) {\n imageEl.setAttribute('src', src);\n imageEl.setAttribute('alt', alt);\n }\n }\n },\n insertText: (text: string) => {\n editor?.insertContent(text);\n },\n insertHtml: (html: string, overwrite: boolean = false) => {\n if (overwrite) {\n editor?.setContent('');\n }\n editor?.insertContent(html);\n },\n getEditor: () => {\n return editor;\n }\n }),\n [editor]\n );\n\n useLayoutEffect(() => {\n tinymce\n .init({\n readonly: readOnly || disabled,\n skin: false,\n selector: `#${id}`,\n toolbar: false,\n menubar: false,\n inline: true,\n plugins: 'lists advlist',\n lists_indent_on_tab: true,\n icons: '',\n placeholder,\n init_instance_callback: () => {\n customComponents.forEach(comp => {\n if (!customElements.get(comp.name)) {\n customElements.define(comp.name, comp.customElement);\n }\n });\n },\n custom_elements: customComponents.reduce((prev, { name }, i) => {\n return i === 0 ? name : `${prev},${name}`;\n }, ''),\n setup: editorSettings => {\n getTextFormats().forEach((format, i) => {\n editorSettings.addShortcut(\n `meta+alt+${i}`,\n `${t('rte_change_text_format')} ${format.text}`,\n () => {\n editorSettings.execCommand('FormatBlock', false, format.type);\n }\n );\n });\n editorSettings.addShortcut('meta+shift+l', t('rte_toggle_unordered_list'), () => {\n editorSettings.execCommand('InsertUnorderedList');\n });\n editorSettings.addShortcut('alt+m', t('rte_indent_selection'), () => {\n editorSettings.execCommand('Indent');\n });\n editorSettings.addShortcut('alt+shift+m', t('rte_unindent_selection'), () => {\n editorSettings.execCommand('Outdent');\n });\n }\n })\n .then(tinymceEditors => {\n setEditor(tinymceEditors[0]);\n });\n }, []);\n\n useEffect(() => {\n if (!editor || initialized.current) return;\n initialized.current = true;\n onInit?.(editor);\n }, [editor, onInit]);\n\n useEffect(() => {\n if (!editor) return;\n const onEditorChange = () => {\n setShowPlaceholder(!editor?.getContent({ format: 'text' }));\n };\n const onEditorFocus = () => {\n onFocus?.();\n };\n const onEditorBlur = () => {\n onBlur?.();\n };\n editor.on('input', onEditorChange);\n editor.on('focus', onEditorFocus);\n editor.on('blur', onEditorBlur);\n return () => {\n editor.off('input', onEditorChange);\n editor.off('focus', onEditorFocus);\n editor.off('blur', onEditorBlur);\n };\n }, [editor, onFocus, onBlur]);\n\n useAfterInitialEffect(() => {\n editor?.mode.set(readOnly || disabled ? 'readonly' : 'design');\n }, [readOnly, disabled]);\n\n const addImage = (image: File) => {\n if (editor) {\n const imageId = createUID();\n editor.insertContent(`<img src='' id='${imageId}' style=\"max-width: 100%;\"/>`);\n onImageAdded?.(image, imageId);\n }\n };\n\n useEffect(() => {\n const onEditorDrop = (e: DragEvent) => {\n e.preventDefault();\n if (e.dataTransfer) {\n Array.from(e.dataTransfer.files).forEach(file => {\n if (file.type.includes('image')) {\n addImage(file);\n }\n });\n }\n };\n editor?.on('drop', onEditorDrop);\n return () => {\n editor?.off('drop', onEditorDrop);\n };\n }, [editor]);\n\n const renderToolbar = editor && toolbar.length > 0 && !readOnly && !disabled;\n\n return (\n <EditorContext.Provider value={{ addImage, editor: editor || ({} as TinymceEditor) }}>\n <FormField\n as={StyledRichTextFormField}\n toolbar={renderToolbar}\n label={\n // eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events\n <span\n onClick={() => {\n editor?.focus();\n }}\n >\n {label}\n </span>\n }\n labelFor=''\n labelId={labelId}\n labelHidden={labelHidden}\n id={id}\n info={info}\n status={status}\n required={required}\n disabled={disabled}\n readOnly={readOnly}\n onFocus={(e: FocusEvent) => {\n if (e.target === formFieldRef.current && !disabled) {\n editor?.focus();\n }\n }}\n ref={formFieldRef}\n tabIndex={disabled ? -1 : undefined}\n >\n <FormControl\n {...restProps}\n ref={editorRef}\n required={required}\n disabled={disabled}\n readOnly={readOnly}\n status={status}\n as={StyledRichTextEditor}\n toolbar={renderToolbar}\n >\n <StyledEditorContainer\n readOnly={readOnly}\n onClick={() => {\n if (!disabled) {\n editor?.focus();\n }\n }}\n toolbar={renderToolbar}\n containerHeight={height}\n >\n <StyledHtml\n id={id}\n style={{ outline: 'none', border: 'none' }}\n dangerouslySetInnerHTML={{ __html: value.current || '' }}\n aria-label={`${label}${renderToolbar ? `. ${t('rte_toolbar_instructions')}.` : ''}`}\n as={StyledInnerEditor}\n showPlaceholder={showPlaceholder}\n aria-labelledby={labelId}\n role='textbox'\n />\n </StyledEditorContainer>\n {renderToolbar && (\n <Toolbar features={toolbar} editor={editor} customActions={customActions} />\n )}\n </FormControl>\n </FormField>\n </EditorContext.Provider>\n );\n }\n);\n\nexport default Editor;\n"]}
|
|
1
|
+
{"version":3,"file":"Editor.js","sourceRoot":"","sources":["../../../src/components/Editor/Editor.tsx"],"names":[],"mappings":";AAAA,OAAO,OAAoC,MAAM,iBAAiB,CAAC;AACnE,OAAO,uBAAuB,CAAC;AAC/B,OAAO,uBAAuB,CAAC;AAC/B,OAAO,yBAAyB,CAAC;AACjC,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EAEL,UAAU,EAEV,SAAS,EACT,MAAM,EACN,QAAQ,EACR,eAAe,EACf,mBAAmB,EAEpB,MAAM,OAAO,CAAC;AACf,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EACL,SAAS,EACT,WAAW,EACX,SAAS,EAET,IAAI,EACJ,qBAAqB,EACrB,QAAQ,EACR,MAAM,EACN,OAAO,EACP,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,oBAAoB,EAAE,EAC3B,qBAAqB,EACrB,uBAAuB,EACxB,M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IAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,CAAC,OAAO,WAAI,CAC9D,YACqB,EACvB,aAAa,IAAI,CAChB,KAAC,OAAO,IAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,aAAa,WAAI,CAC7E,YACW,WACJ,EACX,QAAQ,YACc,CAC1B,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,MAAM,CAAC","sourcesContent":["import tinymce, { Editor as TinymceEditor } from 'tinymce/tinymce';\nimport 'tinymce/icons/default';\nimport 'tinymce/themes/silver';\nimport 'tinymce/plugins/advlist';\nimport 'tinymce/plugins/lists';\nimport {\n FC,\n forwardRef,\n PropsWithoutRef,\n useEffect,\n useRef,\n useState,\n useLayoutEffect,\n useImperativeHandle,\n FocusEvent\n} from 'react';\nimport styled, { css } from 'styled-components';\n\nimport {\n createUID,\n FormControl,\n FormField,\n ForwardProps,\n HTML,\n useAfterInitialEffect,\n useTheme,\n useUID,\n useI18n,\n useConsolidatedRef,\n defaultThemeProp\n} from '@pega/cosmos-react-core';\n\nimport StyledRichTextEditor, {\n StyledEditorContainer,\n StyledRichTextFormField\n} from '../RichTextEditor/RichTextEditor.styles';\n\nimport Toolbar from './Toolbar/Toolbar';\nimport EditorContext from './Editor.context';\nimport { getTextFormats } from './Toolbar/TextSelect';\nimport { EditorProps, EditorState } from './Editor.types';\nimport ImageEditor, { imgHoverClass } from './ImageEditor';\n\nexport const StyledInnerEditor = styled.div<{ showPlaceholder: boolean }>(\n ({ theme, showPlaceholder }) => {\n return css`\n ${showPlaceholder &&\n css`\n ::before {\n color: ${theme.base.colors.slate.medium};\n position: absolute;\n content: attr(data-mce-placeholder);\n }\n `}\n img:hover,\n img.${imgHoverClass} {\n box-shadow: 0 0 0 0.06125rem ${theme.base.palette['primary-background']},\n 0 0 0 0.125rem ${theme.base.palette.interactive}, 0 0 0 0.25rem rgba(0, 118, 209, 0.3);\n }\n `;\n }\n);\n\nStyledInnerEditor.defaultProps = defaultThemeProp;\n\nconst Editor: FC<EditorProps & ForwardProps> = forwardRef(\n (props: PropsWithoutRef<EditorProps>, ref: EditorProps['ref']) => {\n const {\n components: {\n 'text-area': { 'min-height': minHeight }\n }\n } = useTheme();\n const [editor, setEditor] = useState<TinymceEditor>();\n const t = useI18n();\n const uid = useUID();\n const labelId = useUID();\n const editorRef = useConsolidatedRef(ref);\n const tinyMceRef = useRef(null);\n const initialized = useRef(false);\n\n const {\n id = uid,\n toolbar = [],\n label,\n labelHidden,\n info,\n status,\n required,\n disabled,\n readOnly,\n onChange,\n onKeyDown,\n onFocus,\n onBlur,\n onImageAdded,\n onInit,\n defaultValue,\n customComponents = [],\n height = { min: minHeight },\n customActions,\n placeholder,\n children,\n ...restProps\n } = props;\n\n const [showPlaceholder, setShowPlaceholder] = useState(() => !defaultValue);\n const formFieldRef = useRef<HTMLElement>(null);\n const value = useRef(defaultValue);\n\n useImperativeHandle<any, EditorState>(\n editorRef,\n () => ({\n focus: () => {\n editor?.focus();\n },\n getPlainText: () => {\n return editor ? editor.getContent({ format: 'text' }) : '';\n },\n getRichText: () => {\n return editor ? JSON.stringify(editor.getContent({ format: 'raw' })) : '';\n },\n getHtml: () => {\n return editor ? editor.getContent({ format: 'html' }) : '';\n },\n clear: () => {\n editor?.setContent('');\n },\n appendImage: ({ src, alt }, imageId) => {\n if (editor) {\n const editorEl = editor.getElement();\n const imageEl = editorEl.querySelector(`#${imageId}`);\n if (imageEl) {\n imageEl.setAttribute('src', src);\n imageEl.setAttribute('alt', alt);\n }\n }\n },\n insertText: (text: string) => {\n editor?.insertContent(text);\n },\n insertHtml: (html: string, overwrite: boolean = false) => {\n if (overwrite) {\n editor?.setContent('');\n }\n editor?.insertContent(html);\n },\n getEditor: () => {\n return editor;\n },\n element: formFieldRef.current || undefined\n }),\n [editor]\n );\n\n useLayoutEffect(() => {\n if (!tinyMceRef.current) return;\n\n tinymce\n .init({\n readonly: readOnly || disabled,\n skin: false,\n target: tinyMceRef.current,\n toolbar: false,\n menubar: false,\n inline: true,\n plugins: 'lists advlist',\n lists_indent_on_tab: true,\n icons: '',\n placeholder,\n init_instance_callback: () => {\n customComponents.forEach(comp => {\n if (!customElements.get(comp.name)) {\n customElements.define(comp.name, comp.customElement);\n }\n });\n },\n custom_elements: `~${customComponents.reduce((prev, { name }, i) => {\n return i === 0 ? name : `${prev},${name}`;\n }, '')}`,\n setup: editorSettings => {\n editorSettings.on('keydown', e => {\n onKeyDown?.(e);\n });\n getTextFormats().forEach((format, i) => {\n editorSettings.addShortcut(\n `meta+alt+${i}`,\n `${t('rte_change_text_format')} ${format.text}`,\n () => {\n editorSettings.execCommand('FormatBlock', false, format.type);\n }\n );\n });\n editorSettings.addShortcut('meta+shift+l', t('rte_toggle_unordered_list'), () => {\n editorSettings.execCommand('InsertUnorderedList');\n });\n editorSettings.addShortcut('alt+m', t('rte_indent_selection'), () => {\n editorSettings.execCommand('Indent');\n });\n editorSettings.addShortcut('alt+shift+m', t('rte_unindent_selection'), () => {\n editorSettings.execCommand('Outdent');\n });\n customActions?.forEach(action => {\n if (action.shortcut) {\n editorSettings.addShortcut(\n action.shortcut.pattern,\n action.shortcut.description,\n () => {\n action.shortcut?.command(editorSettings);\n }\n );\n }\n });\n }\n })\n .then(tinymceEditors => {\n setEditor(tinymceEditors[0]);\n });\n }, []);\n\n useEffect(() => {\n if (!editor || initialized.current) return;\n initialized.current = true;\n onInit?.(editor);\n }, [editor, onInit]);\n\n useEffect(() => {\n if (!editor) return;\n const onEditorChange = () => {\n onChange?.(editor);\n setShowPlaceholder(!editor.getContent({ format: 'text' }));\n };\n const onEditorFocus = () => {\n onFocus?.();\n };\n const onEditorBlur = () => {\n onBlur?.();\n };\n\n editor.on('input', onEditorChange);\n editor.on('focus', onEditorFocus);\n editor.on('blur', onEditorBlur);\n return () => {\n editor.off('input', onEditorChange);\n editor.off('focus', onEditorFocus);\n editor.off('blur', onEditorBlur);\n };\n }, [editor, onFocus, onBlur]);\n\n useAfterInitialEffect(() => {\n editor?.mode.set(readOnly || disabled ? 'readonly' : 'design');\n }, [readOnly, disabled]);\n\n const addImage = (image: File) => {\n if (editor) {\n const imageId = createUID();\n editor.insertContent(`<img src='' id='${imageId}' style=\"max-width: 100%;\"/>`);\n onImageAdded?.(image, imageId);\n }\n };\n\n useEffect(() => {\n const onEditorDrop = (e: DragEvent) => {\n e.preventDefault();\n if (e.dataTransfer) {\n Array.from(e.dataTransfer.files).forEach(file => {\n if (file.type.includes('image')) {\n addImage(file);\n }\n });\n }\n };\n editor?.on('drop', onEditorDrop);\n return () => {\n editor?.off('drop', onEditorDrop);\n };\n }, [editor]);\n\n const renderToolbar = editor && toolbar.length > 0 && !readOnly && !disabled;\n\n return (\n <EditorContext.Provider value={{ addImage, editor: editor || ({} as TinymceEditor) }}>\n <FormField\n as={StyledRichTextFormField}\n toolbar={renderToolbar}\n label={\n // eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events\n <span\n onClick={() => {\n editor?.focus();\n }}\n >\n {label}\n </span>\n }\n labelFor=''\n labelId={labelId}\n labelHidden={labelHidden}\n id={id}\n info={info}\n status={status}\n required={required}\n disabled={disabled}\n readOnly={readOnly}\n onFocus={(e: FocusEvent) => {\n if (e.target === formFieldRef.current && !disabled) {\n editor?.focus();\n }\n }}\n ref={formFieldRef}\n tabIndex={disabled ? -1 : undefined}\n >\n <FormControl\n {...restProps}\n ref={editorRef}\n required={required}\n disabled={disabled}\n readOnly={readOnly}\n status={status}\n as={StyledRichTextEditor}\n toolbar={renderToolbar}\n >\n <StyledEditorContainer\n readOnly={readOnly}\n onClick={() => {\n if (!disabled) {\n editor?.focus();\n }\n }}\n toolbar={renderToolbar}\n containerHeight={height}\n >\n <HTML\n as={StyledInnerEditor}\n id={id}\n style={{ outline: 'none', border: 'none' }}\n content={value.current || ''}\n aria-label={`${label}${renderToolbar ? `. ${t('rte_toolbar_instructions')}.` : ''}`}\n showPlaceholder={showPlaceholder}\n aria-labelledby={labelId}\n role='textbox'\n ref={tinyMceRef}\n />\n {tinyMceRef.current && editor && (\n <ImageEditor editor={editor} editorEl={tinyMceRef.current} />\n )}\n </StyledEditorContainer>\n {renderToolbar && (\n <Toolbar features={toolbar} editor={editor} customActions={customActions} />\n )}\n </FormControl>\n </FormField>\n {children}\n </EditorContext.Provider>\n );\n }\n);\n\nexport default Editor;\n"]}
|
|
@@ -1,21 +1,36 @@
|
|
|
1
|
-
import { Ref } from 'react';
|
|
1
|
+
import { ReactNode, Ref, MouseEvent as ReactMouseEvent, KeyboardEvent as ReactKeyboardEvent } from 'react';
|
|
2
2
|
import { Editor as TinymceEditor } from 'tinymce/tinymce';
|
|
3
3
|
import { Action, BaseProps, FormControlProps } from '@pega/cosmos-react-core';
|
|
4
4
|
import { Features } from '../RichTextEditor/Toolbar/Toolbar.types';
|
|
5
5
|
import { RichTextEditorState } from '../RichTextEditor';
|
|
6
6
|
export declare type EditorState = RichTextEditorState & {
|
|
7
7
|
getEditor: () => TinymceEditor | undefined;
|
|
8
|
+
element?: HTMLElement;
|
|
8
9
|
};
|
|
9
10
|
export declare type CustomAction = {
|
|
10
11
|
text: Action['text'];
|
|
11
12
|
icon: NonNullable<Action['icon']>;
|
|
12
|
-
onMouseDown: () => void;
|
|
13
|
+
onMouseDown: (e: ReactMouseEvent | ReactKeyboardEvent) => void;
|
|
14
|
+
active?: (editor: TinymceEditor) => void;
|
|
15
|
+
shortcut?: {
|
|
16
|
+
pattern: string;
|
|
17
|
+
description: string;
|
|
18
|
+
command: (editor: TinymceEditor) => void;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare type CustomComponent = {
|
|
22
|
+
customElement: CustomElementConstructor;
|
|
23
|
+
name: string;
|
|
13
24
|
};
|
|
14
25
|
export interface EditorProps extends FormControlProps, BaseProps {
|
|
15
26
|
/** An array of strings that activate features on the toolbar */
|
|
16
27
|
toolbar?: Features[];
|
|
28
|
+
/** Children content */
|
|
29
|
+
children?: ReactNode;
|
|
17
30
|
/** A callback that supplies an object with functions that can manipulate the editor and retrieve it's content/state */
|
|
18
|
-
onChange?: () => void;
|
|
31
|
+
onChange?: (editor?: TinymceEditor) => void;
|
|
32
|
+
/** KeyDown event custom handlers for the editor */
|
|
33
|
+
onKeyDown?: (event?: KeyboardEvent) => void;
|
|
19
34
|
/** A callback that runs if the editor is focused */
|
|
20
35
|
onFocus?: () => void;
|
|
21
36
|
/** A callback that runs if the editor loses focus */
|
|
@@ -36,10 +51,7 @@ export interface EditorProps extends FormControlProps, BaseProps {
|
|
|
36
51
|
max?: string;
|
|
37
52
|
};
|
|
38
53
|
/** An array of custom components, custom components must use web component API */
|
|
39
|
-
customComponents?:
|
|
40
|
-
customElement: CustomElementConstructor;
|
|
41
|
-
name: string;
|
|
42
|
-
}[];
|
|
54
|
+
customComponents?: CustomComponent[];
|
|
43
55
|
/** An array of custom buttons to be inserted into the toolbar */
|
|
44
56
|
customActions?: CustomAction[];
|
|
45
57
|
/** Handle to the state. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Editor.types.d.ts","sourceRoot":"","sources":["../../../src/components/Editor/Editor.types.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Editor.types.d.ts","sourceRoot":"","sources":["../../../src/components/Editor/Editor.types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,GAAG,EACH,UAAU,IAAI,eAAe,EAC7B,aAAa,IAAI,kBAAkB,EACpC,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE9E,OAAO,EAAE,QAAQ,EAAE,MAAM,yCAAyC,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,oBAAY,WAAW,GAAG,mBAAm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IAC3D,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,CAAC;IACzC,wCAAwC;IACxC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,8CAA8C;IAC9C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yDAAyD;IACzD,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,kIAAkI;IAClI,MAAM,CAAC,EACH,MAAM,GACN;QACE,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;IACN,kFAAkF;IAClF,gBAAgB,CAAC,EAAE,eAAe,EAAE,CAAC;IACrC,iEAAiE;IACjE,aAAa,CAAC,EAAE,YAAY,EAAE,CAAC;IAC/B,2BAA2B;IAC3B,GAAG,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;CACxB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Editor.types.js","sourceRoot":"","sources":["../../../src/components/Editor/Editor.types.ts"],"names":[],"mappings":"","sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"Editor.types.js","sourceRoot":"","sources":["../../../src/components/Editor/Editor.types.ts"],"names":[],"mappings":"","sourcesContent":["import {\n ReactNode,\n Ref,\n MouseEvent as ReactMouseEvent,\n KeyboardEvent as ReactKeyboardEvent\n} from 'react';\nimport { Editor as TinymceEditor } from 'tinymce/tinymce';\n\nimport { Action, BaseProps, FormControlProps } from '@pega/cosmos-react-core';\n\nimport { Features } from '../RichTextEditor/Toolbar/Toolbar.types';\nimport { RichTextEditorState } from '../RichTextEditor';\n\nexport type EditorState = RichTextEditorState & {\n getEditor: () => TinymceEditor | undefined;\n element?: HTMLElement;\n};\n\nexport type CustomAction = {\n text: Action['text'];\n icon: NonNullable<Action['icon']>;\n onMouseDown: (e: ReactMouseEvent | ReactKeyboardEvent) => void;\n active?: (editor: TinymceEditor) => void;\n shortcut?: { pattern: string; description: string; command: (editor: TinymceEditor) => void };\n};\n\nexport type CustomComponent = {\n customElement: CustomElementConstructor;\n name: string;\n};\n\nexport interface EditorProps extends FormControlProps, BaseProps {\n /** An array of strings that activate features on the toolbar */\n toolbar?: Features[];\n /** Children content */\n children?: ReactNode;\n /** A callback that supplies an object with functions that can manipulate the editor and retrieve it's content/state */\n onChange?: (editor?: TinymceEditor) => void;\n /** KeyDown event custom handlers for the editor */\n onKeyDown?: (event?: KeyboardEvent) => void;\n /** A callback that runs if the editor is focused */\n onFocus?: () => void;\n /** A callback that runs if the editor loses focus */\n onBlur?: () => void;\n /** A callback when an image is added through the toolbar image feature */\n onImageAdded?: (image: File, id: string) => void;\n /** A callback for when tinymce is finished initializing */\n onInit?: (editor: TinymceEditor) => void;\n /** Standard placeholder for an input */\n placeholder?: string;\n /** Default value for the RTE - HTML String */\n defaultValue?: string;\n /** Will automatically focus the RTE on render if true */\n autoFocus?: boolean;\n /** Sets the height for the RTE. If no max height is set the RTE will continue to expand, otherwise an overflow will be applied */\n height?:\n | string\n | {\n min?: string;\n max?: string;\n };\n /** An array of custom components, custom components must use web component API */\n customComponents?: CustomComponent[];\n /** An array of custom buttons to be inserted into the toolbar */\n customActions?: CustomAction[];\n /** Handle to the state. */\n ref?: Ref<EditorState>;\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { Editor } from 'tinymce';
|
|
3
|
+
export declare const imgHoverClass: string;
|
|
4
|
+
declare const ImageEditor: FC<{
|
|
5
|
+
editorEl: HTMLElement;
|
|
6
|
+
editor: Editor;
|
|
7
|
+
}>;
|
|
8
|
+
export default ImageEditor;
|
|
9
|
+
//# sourceMappingURL=ImageEditor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImageEditor.d.ts","sourceRoot":"","sources":["../../../src/components/Editor/ImageEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,EAAE,EAQH,MAAM,OAAO,CAAC;AAGf,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AA4BjC,eAAO,MAAM,aAAa,QAAc,CAAC;AAEzC,QAAA,MAAM,WAAW,EAAE,EAAE,CAAC;IAAE,QAAQ,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAwX9D,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { usePopper } from 'react-popper';
|
|
4
|
+
import styled from 'styled-components';
|
|
5
|
+
import { Button, CardContent, CardHeader, Checkbox, createUID, Flex, Form, Grid, Icon, Input, NumberInput, Popover, Text, useElement, useI18n, useOuterEvent, useUID, VisuallyHiddenText } from '@pega/cosmos-react-core';
|
|
6
|
+
const IgnoreMouseEventsIcon = styled.svg `
|
|
7
|
+
pointer-events: none;
|
|
8
|
+
`;
|
|
9
|
+
export const imgHoverClass = createUID();
|
|
10
|
+
const ImageEditor = ({ editorEl, editor }) => {
|
|
11
|
+
const t = useI18n();
|
|
12
|
+
const id = useUID();
|
|
13
|
+
const [showImagePopover, setShowImagePopover] = useState(false);
|
|
14
|
+
const [showEditButton, setShowEditButton] = useState(false);
|
|
15
|
+
const [canShowEditButton, setCanShowEditButton] = useState(false);
|
|
16
|
+
const [imgEl, setImageEl] = useState(null);
|
|
17
|
+
const [imgElements, setImageElements] = useState([]);
|
|
18
|
+
const [buttonEl, setButtonEl] = useElement(null);
|
|
19
|
+
const [width, setWidth] = useState(0);
|
|
20
|
+
const [height, setHeight] = useState(0);
|
|
21
|
+
const [aspectRatio, setAspectRatio] = useState(0);
|
|
22
|
+
const [altText, setAltText] = useState('');
|
|
23
|
+
const [customSize, setCustomSize] = useState(false);
|
|
24
|
+
const [keyCommandText, setKeyCommandText] = useState('');
|
|
25
|
+
const imagePopoverRef = useRef(null);
|
|
26
|
+
const altTextInputRef = useRef(null);
|
|
27
|
+
const editorOpenedWithEnter = useRef(false);
|
|
28
|
+
const modifiers = useMemo(() => [
|
|
29
|
+
{
|
|
30
|
+
name: 'offset',
|
|
31
|
+
options: {
|
|
32
|
+
offset: ({ reference, popper }) => {
|
|
33
|
+
return [0, -(reference.height / 2 + popper.height / 2)];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
], []);
|
|
38
|
+
const { styles, attributes } = usePopper(imgEl, buttonEl, {
|
|
39
|
+
placement: 'top',
|
|
40
|
+
strategy: 'absolute',
|
|
41
|
+
modifiers
|
|
42
|
+
});
|
|
43
|
+
const onImageClick = (e) => {
|
|
44
|
+
e.preventDefault();
|
|
45
|
+
e.stopPropagation();
|
|
46
|
+
setShowImagePopover(true);
|
|
47
|
+
};
|
|
48
|
+
const onImageMouseEnter = (e) => {
|
|
49
|
+
const el = e.target;
|
|
50
|
+
if (showImagePopover && el !== imgEl) {
|
|
51
|
+
setShowImagePopover(false);
|
|
52
|
+
}
|
|
53
|
+
setShowEditButton(true);
|
|
54
|
+
setImageEl(el);
|
|
55
|
+
};
|
|
56
|
+
const onImageMouseLeave = (e) => {
|
|
57
|
+
if (e.relatedTarget.id !== id && !showImagePopover) {
|
|
58
|
+
setImageEl(null);
|
|
59
|
+
setShowEditButton(false);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const setImageState = () => {
|
|
63
|
+
if (imgEl) {
|
|
64
|
+
let imgHeight;
|
|
65
|
+
let imgWidth;
|
|
66
|
+
let hasHeightOrWidthAttribute = false;
|
|
67
|
+
if (imgEl.getAttribute('height')) {
|
|
68
|
+
imgHeight = parseInt(imgEl.getAttribute('height'), 10);
|
|
69
|
+
hasHeightOrWidthAttribute = true;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
imgHeight = imgEl.naturalHeight;
|
|
73
|
+
}
|
|
74
|
+
if (imgEl.getAttribute('width')) {
|
|
75
|
+
imgWidth = parseInt(imgEl.getAttribute('width'), 10);
|
|
76
|
+
hasHeightOrWidthAttribute = true;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
imgWidth = imgEl.naturalWidth;
|
|
80
|
+
}
|
|
81
|
+
setAltText(imgEl.getAttribute('alt') || '');
|
|
82
|
+
setHeight(imgHeight);
|
|
83
|
+
setWidth(imgWidth);
|
|
84
|
+
setAspectRatio(imgWidth / imgHeight);
|
|
85
|
+
setCustomSize(hasHeightOrWidthAttribute);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
useOuterEvent('mousedown', [imagePopoverRef], () => {
|
|
89
|
+
setImageState();
|
|
90
|
+
setShowImagePopover(false);
|
|
91
|
+
});
|
|
92
|
+
const onCheckBoxClick = (e) => {
|
|
93
|
+
e.stopPropagation();
|
|
94
|
+
};
|
|
95
|
+
const onInputClick = (e) => {
|
|
96
|
+
e.stopPropagation();
|
|
97
|
+
e.preventDefault();
|
|
98
|
+
};
|
|
99
|
+
const onInputFocus = (e) => {
|
|
100
|
+
e.stopPropagation();
|
|
101
|
+
};
|
|
102
|
+
const onAltTextChange = (e) => {
|
|
103
|
+
setAltText(e.target.value);
|
|
104
|
+
};
|
|
105
|
+
/* The next two functions handle changing the height and width of the selected image element.
|
|
106
|
+
If the value entered is too small, the value will be defaulted.
|
|
107
|
+
When the height is input, a new width will be calculated and vice versa. */
|
|
108
|
+
const onHeightChange = (incomingValue) => {
|
|
109
|
+
const inputHeight = parseInt(incomingValue, 10);
|
|
110
|
+
setHeight(inputHeight);
|
|
111
|
+
setWidth(inputHeight * aspectRatio);
|
|
112
|
+
};
|
|
113
|
+
const onWidthChange = (incomingValue) => {
|
|
114
|
+
const inputWidth = parseInt(incomingValue, 10);
|
|
115
|
+
setWidth(inputWidth);
|
|
116
|
+
setHeight(inputWidth / aspectRatio);
|
|
117
|
+
};
|
|
118
|
+
const onImageChangeSubmit = (e) => {
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
if (imgEl) {
|
|
121
|
+
if (customSize) {
|
|
122
|
+
if (Number.isNaN(height) || Number.isNaN(width)) {
|
|
123
|
+
imgEl.removeAttribute('height');
|
|
124
|
+
imgEl.removeAttribute('width');
|
|
125
|
+
setHeight(imgEl.naturalHeight);
|
|
126
|
+
setWidth(imgEl.naturalWidth);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
imgEl.setAttribute('height', height.toString());
|
|
130
|
+
imgEl.setAttribute('width', width.toString());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
imgEl.removeAttribute('height');
|
|
135
|
+
imgEl.removeAttribute('width');
|
|
136
|
+
}
|
|
137
|
+
imgEl.setAttribute('alt', altText);
|
|
138
|
+
}
|
|
139
|
+
setImageEl(null);
|
|
140
|
+
setShowImagePopover(false);
|
|
141
|
+
};
|
|
142
|
+
const onImageChangeCancel = () => {
|
|
143
|
+
setImageEl(null);
|
|
144
|
+
setShowImagePopover(false);
|
|
145
|
+
};
|
|
146
|
+
const onImageDelete = () => {
|
|
147
|
+
if (imgEl) {
|
|
148
|
+
imgEl.remove();
|
|
149
|
+
}
|
|
150
|
+
setImageEl(null);
|
|
151
|
+
setShowImagePopover(false);
|
|
152
|
+
};
|
|
153
|
+
// Keeps the imageElements state up to date with existing and newly added img tags inside of TinyMCE
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
setImageElements(() => {
|
|
156
|
+
return [...editorEl.querySelectorAll('img')];
|
|
157
|
+
});
|
|
158
|
+
const observer = new MutationObserver(mutations => {
|
|
159
|
+
mutations.forEach(mutation => {
|
|
160
|
+
if (mutation.type === 'childList') {
|
|
161
|
+
const addedNodes = [...mutation.addedNodes].filter(node => node.nodeName === 'IMG');
|
|
162
|
+
const removedNodes = [...mutation.removedNodes].filter(node => node.nodeName === 'IMG');
|
|
163
|
+
if (addedNodes.length || removedNodes.length) {
|
|
164
|
+
setImageElements(() => [...editorEl.querySelectorAll('img')]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
observer.observe(editorEl, { childList: true, subtree: true });
|
|
170
|
+
return () => {
|
|
171
|
+
observer.disconnect();
|
|
172
|
+
};
|
|
173
|
+
}, [editorEl, setImageElements]);
|
|
174
|
+
// Adds event listeners to HTML img elements inside of TinyMCE
|
|
175
|
+
// This has to be done because there are no react components for these images
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
imgElements.forEach(imgElement => {
|
|
178
|
+
imgElement.addEventListener('mouseenter', onImageMouseEnter);
|
|
179
|
+
imgElement.addEventListener('mouseout', onImageMouseLeave);
|
|
180
|
+
imgElement.addEventListener('click', onImageClick);
|
|
181
|
+
});
|
|
182
|
+
return () => {
|
|
183
|
+
imgElements.forEach(imgElement => {
|
|
184
|
+
imgElement.removeEventListener('mouseenter', onImageMouseEnter);
|
|
185
|
+
imgElement.removeEventListener('mouseout', onImageMouseLeave);
|
|
186
|
+
imgElement.removeEventListener('click', onImageClick);
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
}, [imgElements, onImageMouseEnter, onImageMouseLeave, onImageClick]);
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
setImageState();
|
|
192
|
+
imgEl?.setAttribute('class', imgHoverClass);
|
|
193
|
+
if (imgEl && imgEl.clientHeight > 36 && imgEl.clientWidth > 36) {
|
|
194
|
+
setCanShowEditButton(true);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
setCanShowEditButton(false);
|
|
198
|
+
}
|
|
199
|
+
return () => {
|
|
200
|
+
imgEl?.removeAttribute('class');
|
|
201
|
+
};
|
|
202
|
+
}, [imgEl]);
|
|
203
|
+
useEffect(() => {
|
|
204
|
+
const onKeyUp = () => {
|
|
205
|
+
const selection = editor.selection.getSel();
|
|
206
|
+
let nextToImg = false;
|
|
207
|
+
// If your selection is next to an image on it's own line (not breaking text)
|
|
208
|
+
if (selection &&
|
|
209
|
+
selection.anchorNode?.childNodes.length === 1 &&
|
|
210
|
+
selection.anchorNode?.firstChild?.nodeName === 'IMG' &&
|
|
211
|
+
selection.anchorOffset === 0) {
|
|
212
|
+
setImageEl(selection.anchorNode.firstChild);
|
|
213
|
+
setShowEditButton(true);
|
|
214
|
+
nextToImg = true;
|
|
215
|
+
// If your selection is next to an image that breaks text up inside of a paragraph
|
|
216
|
+
}
|
|
217
|
+
else if (selection && selection.anchorNode?.nextSibling?.nodeName === 'IMG') {
|
|
218
|
+
setImageEl(selection.anchorNode.nextSibling);
|
|
219
|
+
setShowEditButton(true);
|
|
220
|
+
nextToImg = true;
|
|
221
|
+
}
|
|
222
|
+
else if (selection && imgEl && showEditButton) {
|
|
223
|
+
setImageEl(null);
|
|
224
|
+
setShowEditButton(false);
|
|
225
|
+
setKeyCommandText('');
|
|
226
|
+
}
|
|
227
|
+
if (nextToImg) {
|
|
228
|
+
setKeyCommandText(t('rte_image_key_command'));
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const onKeyDown = (e) => {
|
|
232
|
+
if (e.key === 'Enter' && showEditButton) {
|
|
233
|
+
e.stopPropagation();
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
editorOpenedWithEnter.current = true;
|
|
236
|
+
setShowImagePopover(true);
|
|
237
|
+
}
|
|
238
|
+
else if (e.key === 'Del' && showEditButton) {
|
|
239
|
+
onImageDelete();
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
editor.on('keyup', onKeyUp);
|
|
243
|
+
editor.on('keydown', onKeyDown, true);
|
|
244
|
+
return () => {
|
|
245
|
+
editor.off('keyup', onKeyUp);
|
|
246
|
+
editor.off('keydown', onKeyDown);
|
|
247
|
+
};
|
|
248
|
+
}, [editor, imgEl, showEditButton]);
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
if (showImagePopover) {
|
|
251
|
+
editor.selection.getSel()?.collapseToEnd();
|
|
252
|
+
if (editorOpenedWithEnter.current) {
|
|
253
|
+
altTextInputRef.current?.focus();
|
|
254
|
+
editorOpenedWithEnter.current = false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}, [showImagePopover]);
|
|
258
|
+
return (_jsxs(_Fragment, { children: [showEditButton && imgEl && canShowEditButton && (_jsx(Button, { id: id, icon: true, variant: 'primary', ref: setButtonEl, onClick: () => setShowImagePopover(true), style: { ...styles.popper }, ...attributes.popper, children: _jsx(Icon, { name: 'pencil-solid', as: IgnoreMouseEventsIcon }, void 0) }, void 0)), _jsx(VisuallyHiddenText, { "aria-live": 'polite', children: keyCommandText }, void 0), showImagePopover && (buttonEl || !canShowEditButton) && imgEl && (_jsxs(Popover, { target: canShowEditButton ? buttonEl : imgEl, strategy: 'fixed', arrow: true, ref: imagePopoverRef, onClick: (e) => e.stopPropagation(), children: [_jsx(CardHeader, { actions: _jsx(Button, { variant: 'simple', icon: true, onClick: onImageChangeCancel, label: t('close'), children: _jsx(Icon, { name: 'times' }, void 0) }, void 0), children: _jsx(Text, { variant: 'h3', children: t('rte_image_settings') }, void 0) }, void 0), _jsx(CardContent, { children: _jsx(Form, { actions: _jsxs(Flex, { container: { justify: 'between' }, item: { grow: 1 }, children: [_jsxs(Flex, { item: { grow: 1 }, children: [_jsx(Button, { onClick: onImageChangeCancel, children: t('cancel') }, void 0), _jsx(Button, { onClick: onImageDelete, children: t('delete') }, void 0)] }, void 0), _jsx(Button, { variant: 'primary', type: 'submit', children: t('submit') }, void 0)] }, void 0), onSubmit: onImageChangeSubmit, children: _jsxs(Grid, { container: {
|
|
259
|
+
gap: 2,
|
|
260
|
+
areas: customSize
|
|
261
|
+
? '"desc desc" "radio radio" "width height"'
|
|
262
|
+
: '"desc desc" "radio radio"'
|
|
263
|
+
}, children: [_jsx(Grid, { item: { area: 'desc' }, children: _jsx(Input, { label: t('description'), value: altText, onClick: onInputClick, onChange: onAltTextChange, onFocus: onInputFocus, info: t('rte_image_description_info'), ref: altTextInputRef }, void 0) }, void 0), _jsx(Grid, { item: { area: 'radio' }, children: _jsx(Checkbox, { checked: customSize, label: t('rte_image_custom_size'), onClick: onCheckBoxClick, onChange: (e) => {
|
|
264
|
+
setCustomSize(e.target.checked);
|
|
265
|
+
} }, void 0) }, void 0), customSize && (_jsxs(_Fragment, { children: [_jsx(Grid, { item: { area: 'width' }, children: _jsx(NumberInput, { label: t('rte_width_in_pixels'), value: Math.floor(Math.max(width, 1)).toString(), onClick: onInputClick, onChange: onWidthChange }, void 0) }, void 0), _jsx(Grid, { item: { area: 'height' }, children: _jsx(NumberInput, { label: t('rte_height_in_pixels'), value: Math.floor(Math.max(height, 1)).toString(), onClick: onInputClick, onChange: onHeightChange }, void 0) }, void 0)] }, void 0))] }, void 0) }, void 0) }, void 0)] }, void 0))] }, void 0));
|
|
266
|
+
};
|
|
267
|
+
export default ImageEditor;
|
|
268
|
+
//# sourceMappingURL=ImageEditor.js.map
|