@pega/cosmos-react-rte 2.0.0 → 2.1.3
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.context.d.ts +10 -0
- package/lib/components/Editor/Editor.context.d.ts.map +1 -0
- package/lib/components/Editor/Editor.context.js +10 -0
- package/lib/components/Editor/Editor.context.js.map +1 -0
- package/lib/components/Editor/Editor.d.ts +10 -0
- package/lib/components/Editor/Editor.d.ts.map +1 -0
- package/lib/components/Editor/Editor.js +187 -0
- package/lib/components/Editor/Editor.js.map +1 -0
- package/lib/components/Editor/Editor.types.d.ts +48 -0
- package/lib/components/Editor/Editor.types.d.ts.map +1 -0
- package/lib/components/Editor/Editor.types.js +2 -0
- package/lib/components/Editor/Editor.types.js.map +1 -0
- package/lib/components/Editor/Toolbar/AnchorButton.d.ts +10 -0
- package/lib/components/Editor/Toolbar/AnchorButton.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/AnchorButton.js +141 -0
- package/lib/components/Editor/Toolbar/AnchorButton.js.map +1 -0
- package/lib/components/Editor/Toolbar/ImageButton.d.ts +8 -0
- package/lib/components/Editor/Toolbar/ImageButton.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/ImageButton.js +30 -0
- package/lib/components/Editor/Toolbar/ImageButton.js.map +1 -0
- package/lib/components/Editor/Toolbar/TextSelect.d.ts +14 -0
- package/lib/components/Editor/Toolbar/TextSelect.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/TextSelect.js +117 -0
- package/lib/components/Editor/Toolbar/TextSelect.js.map +1 -0
- package/lib/components/Editor/Toolbar/Toolbar.d.ts +13 -0
- package/lib/components/Editor/Toolbar/Toolbar.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/Toolbar.js +142 -0
- package/lib/components/Editor/Toolbar/Toolbar.js.map +1 -0
- package/lib/components/Editor/index.d.ts +4 -0
- package/lib/components/Editor/index.d.ts.map +1 -0
- package/lib/components/Editor/index.js +3 -0
- package/lib/components/Editor/index.js.map +1 -0
- package/lib/components/RichTextEditor/RichTextEditor.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.js +28 -87
- package/lib/components/RichTextEditor/RichTextEditor.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts +2 -2
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.js +30 -8
- 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 +20 -15
- package/lib/components/RichTextEditor/RichTextViewer.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.js.map +1 -1
- package/lib/components/RichTextEditor/index.d.ts +1 -0
- package/lib/components/RichTextEditor/index.d.ts.map +1 -1
- package/lib/components/RichTextEditor/index.js +1 -0
- package/lib/components/RichTextEditor/index.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +8 -6
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState, useMemo } from 'react';
|
|
3
|
+
import styled, { css } from 'styled-components';
|
|
4
|
+
import { Popover, Icon, registerIcon, useOuterEvent, Menu, Button, defaultThemeProp, useI18n, Tooltip, calculateFontSize } from '@pega/cosmos-react-core';
|
|
5
|
+
import * as arrowMicroDownIcon from '@pega/cosmos-react-core/lib/components/Icon/icons/arrow-micro-down.icon';
|
|
6
|
+
import { getKeyCommand } from '../../RichTextEditor/Toolbar/utils';
|
|
7
|
+
registerIcon(arrowMicroDownIcon);
|
|
8
|
+
const StyledTextSelect = styled(Button)(({ theme }) => {
|
|
9
|
+
const fontSizes = calculateFontSize(theme.base['font-size'], theme.base['font-scale']);
|
|
10
|
+
return css `
|
|
11
|
+
color: ${theme.base.palette['foreground-color']};
|
|
12
|
+
display: inline-flex;
|
|
13
|
+
align-items: center;
|
|
14
|
+
border-radius: calc(0.25 * ${theme.base['border-radius']});
|
|
15
|
+
height: calc(4 * ${theme.base.spacing});
|
|
16
|
+
padding: calc(0.5 * ${theme.base.spacing});
|
|
17
|
+
font-size: ${fontSizes.xxs};
|
|
18
|
+
|
|
19
|
+
span {
|
|
20
|
+
white-space: nowrap;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
:focus {
|
|
24
|
+
box-shadow: ${theme.base.shadow.focus};
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
});
|
|
28
|
+
StyledTextSelect.defaultProps = defaultThemeProp;
|
|
29
|
+
const StyledSelectMenu = styled.div `
|
|
30
|
+
min-width: 20ch;
|
|
31
|
+
`;
|
|
32
|
+
const textMapping = {
|
|
33
|
+
p: 'Normal',
|
|
34
|
+
h1: 'Heading 1',
|
|
35
|
+
h2: 'Heading 2',
|
|
36
|
+
h3: 'Heading 3',
|
|
37
|
+
h4: 'Heading 4',
|
|
38
|
+
'': 'Normal'
|
|
39
|
+
};
|
|
40
|
+
const getTextCommand = (num, osx) => {
|
|
41
|
+
return getKeyCommand(osx, ({ ctrl, alt }) => `${ctrl}${alt}${num}`);
|
|
42
|
+
};
|
|
43
|
+
export const getTextFormats = (osx = false) => {
|
|
44
|
+
return [
|
|
45
|
+
{ text: 'Normal', subText: getTextCommand(0, osx), type: 'p' },
|
|
46
|
+
{ text: 'Heading 1', subText: getTextCommand(1, osx), type: 'h1' },
|
|
47
|
+
{ text: 'Heading 2', subText: getTextCommand(2, osx), type: 'h2' },
|
|
48
|
+
{ text: 'Heading 3', subText: getTextCommand(3, osx), type: 'h3' },
|
|
49
|
+
{ text: 'Heading 4', subText: getTextCommand(4, osx), type: 'h4' }
|
|
50
|
+
];
|
|
51
|
+
};
|
|
52
|
+
const TextSelect = ({ osx, editor }) => {
|
|
53
|
+
const t = useI18n();
|
|
54
|
+
const [open, setOpen] = useState(false);
|
|
55
|
+
const selecting = useRef(false);
|
|
56
|
+
const openedByClick = useRef(false);
|
|
57
|
+
const buttonRef = useRef(null);
|
|
58
|
+
const popoverRef = useRef(null);
|
|
59
|
+
const menuRef = useRef(null);
|
|
60
|
+
const textFormats = useMemo(() => {
|
|
61
|
+
return getTextFormats(osx);
|
|
62
|
+
}, [osx]);
|
|
63
|
+
useOuterEvent('click', [buttonRef, popoverRef, menuRef], () => {
|
|
64
|
+
setOpen(false);
|
|
65
|
+
});
|
|
66
|
+
const onTextSelect = (type, e) => {
|
|
67
|
+
e.stopPropagation();
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
editor.execCommand('FormatBlock', false, type);
|
|
70
|
+
if (!openedByClick.current) {
|
|
71
|
+
buttonRef.current?.focus();
|
|
72
|
+
}
|
|
73
|
+
setOpen(false);
|
|
74
|
+
selecting.current = true;
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
if (!editor.hasFocus() && openedByClick.current) {
|
|
77
|
+
editor.focus();
|
|
78
|
+
}
|
|
79
|
+
}, 0);
|
|
80
|
+
};
|
|
81
|
+
const activeFormat = () => {
|
|
82
|
+
return editor.queryCommandValue('FormatBlock');
|
|
83
|
+
};
|
|
84
|
+
const getFormatText = () => {
|
|
85
|
+
const format = textMapping[activeFormat()];
|
|
86
|
+
return format || 'Unknown';
|
|
87
|
+
};
|
|
88
|
+
return (_jsxs(_Fragment, { children: [_jsxs(StyledTextSelect, { variant: 'simple', type: 'button', icon: false, ref: buttonRef, onMouseDown: (e) => {
|
|
89
|
+
e.preventDefault();
|
|
90
|
+
e.stopPropagation();
|
|
91
|
+
setOpen(true);
|
|
92
|
+
openedByClick.current = true;
|
|
93
|
+
}, onKeyDown: (e) => {
|
|
94
|
+
if (e.key === 'Enter' && !selecting.current) {
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
setOpen(true);
|
|
97
|
+
openedByClick.current = false;
|
|
98
|
+
}
|
|
99
|
+
else if (e.key === 'Enter') {
|
|
100
|
+
e.preventDefault();
|
|
101
|
+
selecting.current = false;
|
|
102
|
+
}
|
|
103
|
+
else if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
|
|
104
|
+
setOpen(false);
|
|
105
|
+
}
|
|
106
|
+
}, onFocus: (e) => {
|
|
107
|
+
e.preventDefault();
|
|
108
|
+
e.stopPropagation();
|
|
109
|
+
}, onClick: (e) => e.stopPropagation(), "aria-expanded": open, "aria-label": `${t('rte_text_formatting_toolbar')}. ${t('rte_heading_style')}. ${getFormatText()} ${t('selected').toLocaleLowerCase()}`, "aria-haspopup": true, children: [_jsx("span", { children: getFormatText() }, void 0), _jsx(Icon, { name: 'arrow-micro-down' }, void 0)] }, void 0), buttonRef.current && (_jsx(Tooltip, { target: buttonRef.current, showDelay: 'none', hideDelay: 'none', children: t('rte_heading_style') }, void 0)), _jsx(Popover, { show: open, as: StyledSelectMenu, target: buttonRef.current, placement: 'bottom-start', ref: popoverRef, children: _jsx(Menu, { items: textFormats.map(({ text, type, subText }) => ({
|
|
110
|
+
id: type,
|
|
111
|
+
primary: text,
|
|
112
|
+
secondary: [subText],
|
|
113
|
+
selected: type === activeFormat()
|
|
114
|
+
})), focusControlEl: buttonRef.current ?? undefined, focusElOnClose: false, mode: 'single-select', ref: menuRef, onItemClick: onTextSelect }, void 0) }, void 0)] }, void 0));
|
|
115
|
+
};
|
|
116
|
+
export default TextSelect;
|
|
117
|
+
//# sourceMappingURL=TextSelect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TextSelect.js","sourceRoot":"","sources":["../../../../src/components/Editor/Toolbar/TextSelect.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAM,MAAM,EAAE,QAAQ,EAAyC,OAAO,EAAE,MAAM,OAAO,CAAC;AAE7F,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO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EAAC,kBAAkB,WAAG,YACf,EAClB,SAAS,CAAC,OAAO,IAAI,CACpB,KAAC,OAAO,IAAC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,SAAS,EAAC,MAAM,EAAC,SAAS,EAAC,MAAM,YAClE,CAAC,CAAC,mBAAmB,CAAC,WACf,CACX,EACD,KAAC,OAAO,IACN,IAAI,EAAE,IAAI,EACV,EAAE,EAAE,gBAAgB,EACpB,MAAM,EAAE,SAAS,CAAC,OAAO,EACzB,SAAS,EAAC,cAAc,EACxB,GAAG,EAAE,UAAU,YAEf,KAAC,IAAI,IACH,KAAK,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;wBACnD,EAAE,EAAE,IAAI;wBACR,OAAO,EAAE,IAAI;wBACb,SAAS,EAAE,CAAC,OAAO,CAAC;wBACpB,QAAQ,EAAE,IAAI,KAAK,YAAY,EAAE;qBAClC,CAAC,CAAC,EACH,cAAc,EAAE,SAAS,CAAC,OAAO,IAAI,SAAS,EAC9C,cAAc,EAAE,KAAK,EACrB,IAAI,EAAC,eAAe,EACpB,GAAG,EAAE,OAAO,EACZ,WAAW,EAAE,YAAmD,WAChE,WACM,YACT,CACJ,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,UAAU,CAAC","sourcesContent":["import { FC, useRef, useState, MouseEvent, KeyboardEvent, FocusEvent, useMemo } from 'react';\nimport { Editor } from 'tinymce';\nimport styled, { css } from 'styled-components';\n\nimport {\n Popover,\n Icon,\n registerIcon,\n useOuterEvent,\n Menu,\n Button,\n defaultThemeProp,\n useI18n,\n Tooltip,\n calculateFontSize\n} from '@pega/cosmos-react-core';\nimport * as arrowMicroDownIcon from '@pega/cosmos-react-core/lib/components/Icon/icons/arrow-micro-down.icon';\n\nimport { getKeyCommand } from '../../RichTextEditor/Toolbar/utils';\n\nregisterIcon(arrowMicroDownIcon);\n\nconst StyledTextSelect = styled(Button)(({ theme }) => {\n const fontSizes = calculateFontSize(theme.base['font-size'], theme.base['font-scale']);\n return css`\n color: ${theme.base.palette['foreground-color']};\n display: inline-flex;\n align-items: center;\n border-radius: calc(0.25 * ${theme.base['border-radius']});\n height: calc(4 * ${theme.base.spacing});\n padding: calc(0.5 * ${theme.base.spacing});\n font-size: ${fontSizes.xxs};\n\n span {\n white-space: nowrap;\n }\n\n :focus {\n box-shadow: ${theme.base.shadow.focus};\n }\n `;\n});\n\nStyledTextSelect.defaultProps = defaultThemeProp;\n\nconst StyledSelectMenu = styled.div`\n min-width: 20ch;\n`;\n\ntype FormatTypes = 'p' | 'h1' | 'h2' | 'h3' | 'h4';\n\nconst textMapping = {\n p: 'Normal',\n h1: 'Heading 1',\n h2: 'Heading 2',\n h3: 'Heading 3',\n h4: 'Heading 4',\n '': 'Normal'\n} as const;\n\nconst getTextCommand = (num: number, osx: boolean) => {\n return getKeyCommand(osx, ({ ctrl, alt }) => `${ctrl}${alt}${num}`);\n};\n\nexport const getTextFormats = (\n osx: boolean = false\n): { text: string; subText: string; type: FormatTypes }[] => {\n return [\n { text: 'Normal', subText: getTextCommand(0, osx), type: 'p' },\n { text: 'Heading 1', subText: getTextCommand(1, osx), type: 'h1' },\n { text: 'Heading 2', subText: getTextCommand(2, osx), type: 'h2' },\n { text: 'Heading 3', subText: getTextCommand(3, osx), type: 'h3' },\n { text: 'Heading 4', subText: getTextCommand(4, osx), type: 'h4' }\n ];\n};\n\nconst TextSelect: FC<{ osx: boolean; editor: Editor }> = ({ osx, editor }) => {\n const t = useI18n();\n const [open, setOpen] = useState(false);\n const selecting = useRef(false);\n const openedByClick = useRef(false);\n const buttonRef = useRef<HTMLButtonElement>(null);\n const popoverRef = useRef(null);\n const menuRef = useRef(null);\n\n const textFormats = useMemo(() => {\n return getTextFormats(osx);\n }, [osx]);\n\n useOuterEvent('click', [buttonRef, popoverRef, menuRef], () => {\n setOpen(false);\n });\n\n const onTextSelect = (type: FormatTypes, e: MouseEvent) => {\n e.stopPropagation();\n e.preventDefault();\n editor.execCommand('FormatBlock', false, type);\n if (!openedByClick.current) {\n buttonRef.current?.focus();\n }\n setOpen(false);\n selecting.current = true;\n setTimeout(() => {\n if (!editor.hasFocus() && openedByClick.current) {\n editor.focus();\n }\n }, 0);\n };\n\n const activeFormat = (): FormatTypes => {\n return editor.queryCommandValue('FormatBlock') as FormatTypes;\n };\n\n const getFormatText = (): string => {\n const format = textMapping[activeFormat()];\n return format || 'Unknown';\n };\n\n return (\n <>\n <StyledTextSelect\n variant='simple'\n type='button'\n icon={false}\n ref={buttonRef}\n onMouseDown={(e: MouseEvent) => {\n e.preventDefault();\n e.stopPropagation();\n setOpen(true);\n openedByClick.current = true;\n }}\n onKeyDown={(e: KeyboardEvent) => {\n if (e.key === 'Enter' && !selecting.current) {\n e.preventDefault();\n setOpen(true);\n openedByClick.current = false;\n } else if (e.key === 'Enter') {\n e.preventDefault();\n selecting.current = false;\n } else if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {\n setOpen(false);\n }\n }}\n onFocus={(e: FocusEvent) => {\n e.preventDefault();\n e.stopPropagation();\n }}\n onClick={(e: MouseEvent) => e.stopPropagation()}\n aria-expanded={open}\n aria-label={`${t('rte_text_formatting_toolbar')}. ${t(\n 'rte_heading_style'\n )}. ${getFormatText()} ${t('selected').toLocaleLowerCase()}`}\n aria-haspopup\n >\n <span>{getFormatText()}</span>\n <Icon name='arrow-micro-down' />\n </StyledTextSelect>\n {buttonRef.current && (\n <Tooltip target={buttonRef.current} showDelay='none' hideDelay='none'>\n {t('rte_heading_style')}\n </Tooltip>\n )}\n <Popover\n show={open}\n as={StyledSelectMenu}\n target={buttonRef.current}\n placement='bottom-start'\n ref={popoverRef}\n >\n <Menu\n items={textFormats.map(({ text, type, subText }) => ({\n id: type,\n primary: text,\n secondary: [subText],\n selected: type === activeFormat()\n }))}\n focusControlEl={buttonRef.current ?? undefined}\n focusElOnClose={false}\n mode='single-select'\n ref={menuRef}\n onItemClick={onTextSelect as (id: string, e: MouseEvent) => void}\n />\n </Popover>\n </>\n );\n};\n\nexport default TextSelect;\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { Editor } from 'tinymce';
|
|
3
|
+
import { Features } from '../../RichTextEditor/Toolbar/Toolbar.types';
|
|
4
|
+
import { CustomAction } from '../Editor.types';
|
|
5
|
+
export interface ToolbarProps {
|
|
6
|
+
features: Features[];
|
|
7
|
+
sticky?: boolean;
|
|
8
|
+
editor: Editor;
|
|
9
|
+
customActions?: CustomAction[];
|
|
10
|
+
}
|
|
11
|
+
declare const Toolbar: FC<ToolbarProps>;
|
|
12
|
+
export default Toolbar;
|
|
13
|
+
//# sourceMappingURL=Toolbar.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Toolbar.d.ts","sourceRoot":"","sources":["../../../../src/components/Editor/Toolbar/Toolbar.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAA4C,MAAM,OAAO,CAAC;AAErE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAgBjC,OAAO,EAAa,QAAQ,EAAE,MAAM,4CAA4C,CAAC;AAEjF,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AA0B/C,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,QAAQ,EAAE,CAAC;IACrB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,YAAY,EAAE,CAAC;CAChC;AAwFD,QAAA,MAAM,OAAO,EAAE,EAAE,CAAC,YAAY,CAwI7B,CAAC;AAEF,eAAe,OAAO,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState, useEffect, useRef, useCallback } from 'react';
|
|
3
|
+
import styled, { css } from 'styled-components';
|
|
4
|
+
import { Flex, Icon, useArrows, useI18n, defaultThemeProp, registerIcon } from '@pega/cosmos-react-core';
|
|
5
|
+
import * as indentIcon from '@pega/cosmos-react-core/lib/components/Icon/icons/indent.icon';
|
|
6
|
+
import * as unindentIcon from '@pega/cosmos-react-core/lib/components/Icon/icons/unindent.icon';
|
|
7
|
+
import ToolbarButton from '../../RichTextEditor/Toolbar/ToolbarButton';
|
|
8
|
+
import { getKeyCommand } from '../../RichTextEditor/Toolbar/utils';
|
|
9
|
+
import ImageButton from './ImageButton';
|
|
10
|
+
import TextSelect from './TextSelect';
|
|
11
|
+
import AnchorButton from './AnchorButton';
|
|
12
|
+
registerIcon(indentIcon, unindentIcon);
|
|
13
|
+
const StyledToolbar = styled.div(({ sticky, theme }) => `
|
|
14
|
+
background-color: ${theme.base.palette['primary-background']};
|
|
15
|
+
border-radius: calc(0.25 * ${theme.base['border-radius']});
|
|
16
|
+
overflow: auto;
|
|
17
|
+
padding: 0.125rem;
|
|
18
|
+
${sticky &&
|
|
19
|
+
css `
|
|
20
|
+
position: sticky;
|
|
21
|
+
bottom: 0;
|
|
22
|
+
`}
|
|
23
|
+
`);
|
|
24
|
+
StyledToolbar.defaultProps = defaultThemeProp;
|
|
25
|
+
// Function that returns the text styling buttons
|
|
26
|
+
function renderStyleButtons(editor, t, activeStyles, osx) {
|
|
27
|
+
const inlineStyleButtons = [
|
|
28
|
+
{
|
|
29
|
+
style: 'BOLD',
|
|
30
|
+
text: 'B',
|
|
31
|
+
format: 'Bold',
|
|
32
|
+
label: t('rte_bold'),
|
|
33
|
+
element: 'strong',
|
|
34
|
+
tooltip: getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_bold')} (${ctrl}B, ${ctrl}${shift}B)`)
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
style: 'ITALIC',
|
|
38
|
+
text: 'I',
|
|
39
|
+
format: 'Italic',
|
|
40
|
+
label: t('rte_italic'),
|
|
41
|
+
element: 'i',
|
|
42
|
+
tooltip: getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_italic')} (${ctrl}I, ${ctrl}${shift}I)`)
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
style: 'STRIKE-THROUGH',
|
|
46
|
+
text: 'S',
|
|
47
|
+
format: 'Strikethrough',
|
|
48
|
+
element: 's',
|
|
49
|
+
label: t('rte_strike_through'),
|
|
50
|
+
tooltip: getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_strike_through')} (${ctrl}${shift}X)`)
|
|
51
|
+
}
|
|
52
|
+
];
|
|
53
|
+
return inlineStyleButtons.map(({ format, style, text, label, tooltip }) => {
|
|
54
|
+
return (_jsx(ToolbarButton, { onMouseDown: e => {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
if (editor.hasFocus()) {
|
|
57
|
+
editor.execCommand(format);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
editor.focus();
|
|
61
|
+
setTimeout(() => {
|
|
62
|
+
editor.execCommand(format);
|
|
63
|
+
}, 0);
|
|
64
|
+
}
|
|
65
|
+
}, styleType: style, active: activeStyles[format.toLowerCase()], tooltip: tooltip, label: label, children: text }, style));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Takes the features array and transforms it into a map for faster lookup
|
|
69
|
+
function createActiveFeaturesMap(features) {
|
|
70
|
+
return features.reduce((acc, feature) => {
|
|
71
|
+
return { ...acc, [feature]: true };
|
|
72
|
+
}, {});
|
|
73
|
+
}
|
|
74
|
+
const Toolbar = ({ features, sticky, editor, customActions }) => {
|
|
75
|
+
const t = useI18n();
|
|
76
|
+
const [osx, setOsx] = useState(false);
|
|
77
|
+
const toolbarRef = useRef(null);
|
|
78
|
+
useArrows(toolbarRef, { selector: 'button', dir: 'left-right' });
|
|
79
|
+
const [activeFeatures, setActiveFeatures] = useState(() => createActiveFeaturesMap(features));
|
|
80
|
+
const [activeStyles, setActiveStyles] = useState(() => ({
|
|
81
|
+
bold: false,
|
|
82
|
+
italic: false,
|
|
83
|
+
strikethrough: false,
|
|
84
|
+
unorderedList: false,
|
|
85
|
+
orderedList: false
|
|
86
|
+
}));
|
|
87
|
+
const getIndentationToolTip = (type) => getKeyCommand(osx, ({ shift, alt }) => type === 'indent' ? `${t('rte_indent')} (${alt}M)` : `${t('rte_unindent')} (${alt}${shift}M)`);
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
setActiveFeatures(createActiveFeaturesMap(features));
|
|
90
|
+
}, [features]);
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (navigator.appVersion.includes('Mac'))
|
|
93
|
+
setOsx(true);
|
|
94
|
+
}, []);
|
|
95
|
+
const getActiveStyles = useCallback(() => {
|
|
96
|
+
if (editor.hasFocus()) {
|
|
97
|
+
setActiveStyles({
|
|
98
|
+
bold: editor.queryCommandState('Bold'),
|
|
99
|
+
italic: editor.queryCommandState('Italic'),
|
|
100
|
+
strikethrough: editor.queryCommandState('Strikethrough'),
|
|
101
|
+
unorderedList: editor.queryCommandState('InsertUnorderedList'),
|
|
102
|
+
orderedList: editor.queryCommandState('InsertOrderedList')
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
setActiveStyles({
|
|
107
|
+
bold: false,
|
|
108
|
+
italic: false,
|
|
109
|
+
strikethrough: false,
|
|
110
|
+
unorderedList: false,
|
|
111
|
+
orderedList: false
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}, [editor, setActiveStyles]);
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
getActiveStyles();
|
|
117
|
+
editor.on('SelectionChange', getActiveStyles);
|
|
118
|
+
return () => {
|
|
119
|
+
editor.off('SelectionChange', getActiveStyles);
|
|
120
|
+
};
|
|
121
|
+
}, [editor, getActiveStyles]);
|
|
122
|
+
return (_jsxs(Flex, { as: StyledToolbar, sticky: sticky, container: { justify: 'between' }, ref: toolbarRef, children: [_jsxs(Flex, { container: { alignItems: 'end' }, children: [_jsx(TextSelect, { osx: osx, editor: editor }, void 0), activeFeatures['inline-styling'] && renderStyleButtons(editor, t, activeStyles, osx), activeFeatures.lists && (_jsxs(_Fragment, { children: [_jsx(ToolbarButton, { onMouseDown: e => {
|
|
123
|
+
e.preventDefault();
|
|
124
|
+
editor.execCommand('InsertUnorderedList');
|
|
125
|
+
}, active: activeStyles.unorderedList, tooltip: getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_bulleted_list')} (${ctrl}${shift}L)`), label: t('rte_bulleted_list'), children: _jsx(Icon, { name: 'list' }, void 0) }, void 0), _jsx(ToolbarButton, { onMouseDown: e => {
|
|
126
|
+
e.preventDefault();
|
|
127
|
+
editor.execCommand('InsertOrderedList');
|
|
128
|
+
}, active: activeStyles.orderedList, tooltip: t('rte_numbered_list'), label: t('rte_numbered_list'), children: _jsx(Icon, { name: 'list-number' }, void 0) }, void 0)] }, void 0)), activeFeatures.indentation && (_jsxs(_Fragment, { children: [_jsx(ToolbarButton, { onMouseDown: e => {
|
|
129
|
+
e.preventDefault();
|
|
130
|
+
editor.execCommand('Indent');
|
|
131
|
+
}, label: t('rte_indent'), tooltip: getIndentationToolTip('indent'), children: _jsx(Icon, { name: 'indent' }, void 0) }, void 0), _jsx(ToolbarButton, { onMouseDown: e => {
|
|
132
|
+
e.preventDefault();
|
|
133
|
+
editor.execCommand('Outdent');
|
|
134
|
+
}, label: t('rte_unindent'), tooltip: getIndentationToolTip('unindent'), children: _jsx(Icon, { name: 'unindent' }, void 0) }, void 0)] }, void 0))] }, void 0), _jsxs(Flex, { container: { alignItems: 'end' }, children: [customActions?.map(action => {
|
|
135
|
+
return (_jsx(ToolbarButton, { onMouseDown: e => {
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
action.onMouseDown();
|
|
138
|
+
}, tooltip: action.text, children: _jsx(Icon, { name: action.icon }, void 0) }, action.icon));
|
|
139
|
+
}), activeFeatures.links && _jsx(AnchorButton, { editor: editor, osx: osx }, void 0), activeFeatures.images && _jsx(ImageButton, { editor: editor }, void 0)] }, void 0)] }, void 0));
|
|
140
|
+
};
|
|
141
|
+
export default Toolbar;
|
|
142
|
+
//# sourceMappingURL=Toolbar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Toolbar.js","sourceRoot":"","sources":["../../../../src/components/Editor/Toolbar/Toolbar.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAM,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACrE,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAGhD,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,OAAO,EACP,gBAAgB,EAEhB,YAAY,EACb,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,UAAU,MAAM,+DAA+D,CAAC;AAC5F,OAAO,KAAK,YAAY,MAAM,iEAAiE,CAAC;AAEhG,OAAO,aAAa,MAAM,4CAA4C,CAAC;AAEvE,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAGnE,OAAO,WAAW,MAAM,eAAe,CAAC;AACxC,OAAO,UAAU,MAAM,cAAc,CAAC;AACtC,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAE1C,YAAY,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;AAEvC,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAC9B,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;sBACH,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC;+BAC/B,KAAK,CAAC,IAAI,CAAC,eAAe,CAAC;;;IAItD,MAAM;IACN,GAAG,CAAA;;;KAIL;CACD,CACA,CAAC;AAEF,aAAa,CAAC,YAAY,GAAG,gBAAgB,CAAC;AAmB9C,iDAAiD;AACjD,SAAS,kBAAkB,CACzB,MAAc,EACd,CAAuC,EACvC,YAAwC,EACxC,GAAY;IAEZ,MAAM,kB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ACnC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE;wBAC3B,OAAO,CACL,KAAC,aAAa,IACZ,WAAW,EAAE,CAAC,CAAC,EAAE;gCACf,CAAC,CAAC,cAAc,EAAE,CAAC;gCACnB,MAAM,CAAC,WAAW,EAAE,CAAC;4BACvB,CAAC,EACD,OAAO,EAAE,MAAM,CAAC,IAAI,YAGpB,KAAC,IAAI,IAAC,IAAI,EAAE,MAAM,CAAC,IAAI,WAAI,IAFtB,MAAM,CAAC,IAAI,CAGF,CACjB,CAAC;oBACJ,CAAC,CAAC,EACD,cAAc,CAAC,KAAK,IAAI,KAAC,YAAY,IAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,WAAI,EAClE,cAAc,CAAC,MAAM,IAAI,KAAC,WAAW,IAAC,MAAM,EAAE,MAAM,WAAI,YACpD,YACF,CACR,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,OAAO,CAAC","sourcesContent":["import { FC, useState, useEffect, useRef, useCallback } from 'react';\nimport styled, { css } from 'styled-components';\nimport { Editor } from 'tinymce';\n\nimport {\n Flex,\n Icon,\n useArrows,\n useI18n,\n defaultThemeProp,\n TranslationPack,\n registerIcon\n} from '@pega/cosmos-react-core';\nimport { TranslationFunction } from '@pega/cosmos-react-core/lib/i18n/translate';\nimport * as indentIcon from '@pega/cosmos-react-core/lib/components/Icon/icons/indent.icon';\nimport * as unindentIcon from '@pega/cosmos-react-core/lib/components/Icon/icons/unindent.icon';\n\nimport ToolbarButton from '../../RichTextEditor/Toolbar/ToolbarButton';\nimport { StyleType, Features } from '../../RichTextEditor/Toolbar/Toolbar.types';\nimport { getKeyCommand } from '../../RichTextEditor/Toolbar/utils';\nimport { CustomAction } from '../Editor.types';\n\nimport ImageButton from './ImageButton';\nimport TextSelect from './TextSelect';\nimport AnchorButton from './AnchorButton';\n\nregisterIcon(indentIcon, unindentIcon);\n\nconst StyledToolbar = styled.div<{ sticky?: boolean }>(\n ({ sticky, theme }) => `\n background-color: ${theme.base.palette['primary-background']};\n border-radius: calc(0.25 * ${theme.base['border-radius']});\n overflow: auto;\n padding: 0.125rem;\n ${\n sticky &&\n css`\n position: sticky;\n bottom: 0;\n `\n }\n`\n);\n\nStyledToolbar.defaultProps = defaultThemeProp;\n\nexport interface ToolbarProps {\n features: Features[];\n sticky?: boolean;\n editor: Editor;\n customActions?: CustomAction[];\n}\n\ntype StyleButtonType = {\n style: StyleType;\n text: string;\n icon?: string;\n label?: string;\n format: string;\n element?: string;\n tooltip: string;\n};\n\n// Function that returns the text styling buttons\nfunction renderStyleButtons(\n editor: Editor,\n t: TranslationFunction<TranslationPack>,\n activeStyles: { [key: string]: boolean },\n osx: boolean\n) {\n const inlineStyleButtons: StyleButtonType[] = [\n {\n style: 'BOLD',\n text: 'B',\n format: 'Bold',\n label: t('rte_bold'),\n element: 'strong',\n tooltip: getKeyCommand(\n osx,\n ({ ctrl, shift }) => `${t('rte_bold')} (${ctrl}B, ${ctrl}${shift}B)`\n )\n },\n {\n style: 'ITALIC',\n text: 'I',\n format: 'Italic',\n label: t('rte_italic'),\n element: 'i',\n tooltip: getKeyCommand(\n osx,\n ({ ctrl, shift }) => `${t('rte_italic')} (${ctrl}I, ${ctrl}${shift}I)`\n )\n },\n {\n style: 'STRIKE-THROUGH',\n text: 'S',\n format: 'Strikethrough',\n element: 's',\n label: t('rte_strike_through'),\n tooltip: getKeyCommand(\n osx,\n ({ ctrl, shift }) => `${t('rte_strike_through')} (${ctrl}${shift}X)`\n )\n }\n ];\n\n return inlineStyleButtons.map(({ format, style, text, label, tooltip }) => {\n return (\n <ToolbarButton\n key={style}\n onMouseDown={e => {\n e.preventDefault();\n if (editor.hasFocus()) {\n editor.execCommand(format);\n } else {\n editor.focus();\n setTimeout(() => {\n editor.execCommand(format);\n }, 0);\n }\n }}\n styleType={style}\n active={activeStyles[format.toLowerCase()]}\n tooltip={tooltip}\n label={label}\n >\n {text}\n </ToolbarButton>\n );\n });\n}\n\n// Takes the features array and transforms it into a map for faster lookup\nfunction createActiveFeaturesMap(features: Features[]) {\n return features.reduce((acc, feature) => {\n return { ...acc, [feature]: true };\n }, {} as { [F in Features]: boolean });\n}\n\nconst Toolbar: FC<ToolbarProps> = ({ features, sticky, editor, customActions }: ToolbarProps) => {\n const t = useI18n();\n const [osx, setOsx] = useState(false);\n const toolbarRef = useRef<HTMLElement>(null);\n\n useArrows(toolbarRef, { selector: 'button', dir: 'left-right' });\n\n const [activeFeatures, setActiveFeatures] = useState(() => createActiveFeaturesMap(features));\n const [activeStyles, setActiveStyles] = useState(() => ({\n bold: false,\n italic: false,\n strikethrough: false,\n unorderedList: false,\n orderedList: false\n }));\n\n const getIndentationToolTip = (type: 'indent' | 'unindent') =>\n getKeyCommand(osx, ({ shift, alt }) =>\n type === 'indent' ? `${t('rte_indent')} (${alt}M)` : `${t('rte_unindent')} (${alt}${shift}M)`\n );\n\n useEffect(() => {\n setActiveFeatures(createActiveFeaturesMap(features));\n }, [features]);\n\n useEffect(() => {\n if (navigator.appVersion.includes('Mac')) setOsx(true);\n }, []);\n\n const getActiveStyles = useCallback(() => {\n if (editor.hasFocus()) {\n setActiveStyles({\n bold: editor.queryCommandState('Bold'),\n italic: editor.queryCommandState('Italic'),\n strikethrough: editor.queryCommandState('Strikethrough'),\n unorderedList: editor.queryCommandState('InsertUnorderedList'),\n orderedList: editor.queryCommandState('InsertOrderedList')\n });\n } else {\n setActiveStyles({\n bold: false,\n italic: false,\n strikethrough: false,\n unorderedList: false,\n orderedList: false\n });\n }\n }, [editor, setActiveStyles]);\n\n useEffect(() => {\n getActiveStyles();\n editor.on('SelectionChange', getActiveStyles);\n return () => {\n editor.off('SelectionChange', getActiveStyles);\n };\n }, [editor, getActiveStyles]);\n\n return (\n <Flex as={StyledToolbar} sticky={sticky} container={{ justify: 'between' }} ref={toolbarRef}>\n <Flex container={{ alignItems: 'end' }}>\n <TextSelect osx={osx} editor={editor} />\n {activeFeatures['inline-styling'] && renderStyleButtons(editor, t, activeStyles, osx)}\n {activeFeatures.lists && (\n <>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n editor.execCommand('InsertUnorderedList');\n }}\n active={activeStyles.unorderedList}\n tooltip={getKeyCommand(\n osx,\n ({ ctrl, shift }) => `${t('rte_bulleted_list')} (${ctrl}${shift}L)`\n )}\n label={t('rte_bulleted_list')}\n >\n <Icon name='list' />\n </ToolbarButton>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n editor.execCommand('InsertOrderedList');\n }}\n active={activeStyles.orderedList}\n tooltip={t('rte_numbered_list')}\n label={t('rte_numbered_list')}\n >\n <Icon name='list-number' />\n </ToolbarButton>\n </>\n )}\n {activeFeatures.indentation && (\n <>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n editor.execCommand('Indent');\n }}\n label={t('rte_indent')}\n tooltip={getIndentationToolTip('indent')}\n >\n <Icon name='indent' />\n </ToolbarButton>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n editor.execCommand('Outdent');\n }}\n label={t('rte_unindent')}\n tooltip={getIndentationToolTip('unindent')}\n >\n <Icon name='unindent' />\n </ToolbarButton>\n </>\n )}\n </Flex>\n <Flex container={{ alignItems: 'end' }}>\n {customActions?.map(action => {\n return (\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n action.onMouseDown();\n }}\n tooltip={action.text}\n key={action.icon}\n >\n <Icon name={action.icon} />\n </ToolbarButton>\n );\n })}\n {activeFeatures.links && <AnchorButton editor={editor} osx={osx} />}\n {activeFeatures.images && <ImageButton editor={editor} />}\n </Flex>\n </Flex>\n );\n};\n\nexport default Toolbar;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/Editor/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/Editor/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAE7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC","sourcesContent":["export { default as Editor } from './Editor';\nexport { EditorProps, EditorState } from './Editor.types';\nexport { useEditorContext } from './Editor.context';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextEditor.d.ts","sourceRoot":"","sources":["../../../src/components/RichTextEditor/RichTextEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,EAML,EAAE,
|
|
1
|
+
{"version":3,"file":"RichTextEditor.d.ts","sourceRoot":"","sources":["../../../src/components/RichTextEditor/RichTextEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,EAML,EAAE,EAQH,MAAM,OAAO,CAAC;AASf,OAAO,EAQL,YAAY,EAGb,MAAM,yBAAyB,CAAC;AAajC,OAAO,EAAuB,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAmBlF,QAAA,MAAM,cAAc,EAAE,EAAE,CAAC,mBAAmB,GAAG,YAAY,CAga1D,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { forwardRef, useState, useRef, useMemo, useEffect, useCallback, useImperativeHandle
|
|
2
|
+
import { forwardRef, useState, useRef, useMemo, useEffect, useCallback, useImperativeHandle } from 'react';
|
|
3
3
|
// Import the Slate editor factory.
|
|
4
4
|
import { createEditor, Node, Transforms } from 'slate';
|
|
5
5
|
// Import the Slate components and React plugin.
|
|
6
6
|
import { Slate, Editable, withReact, ReactEditor } from 'slate-react';
|
|
7
7
|
import { withHistory } from 'slate-history';
|
|
8
8
|
import styled from 'styled-components';
|
|
9
|
-
import { FormField, FormControl, StyledPopover, useTheme, useUID, createUID, debounce, Popover, Menu
|
|
10
|
-
import StyledRichTextEditor, {
|
|
9
|
+
import { FormField, FormControl, StyledPopover, useTheme, useUID, createUID, debounce, Popover, Menu } from '@pega/cosmos-react-core';
|
|
10
|
+
import StyledRichTextEditor, { StyledEditorContainer, StyledRichTextFormField } from './RichTextEditor.styles';
|
|
11
11
|
import Toolbar from './Toolbar/Toolbar';
|
|
12
12
|
import { nodeRenderer, leafRenderer } from './utils/renderers';
|
|
13
13
|
import { onSearchChange } from './utils/search';
|
|
@@ -26,44 +26,13 @@ const withVoids = (editor) => {
|
|
|
26
26
|
};
|
|
27
27
|
return editor;
|
|
28
28
|
};
|
|
29
|
-
const syntheticFocusReducer = (currentId, action) => {
|
|
30
|
-
const { type, menuItems, selectedItemId } = action;
|
|
31
|
-
switch (type) {
|
|
32
|
-
case 'child':
|
|
33
|
-
// return first item id if top-level
|
|
34
|
-
if (!currentId)
|
|
35
|
-
return menuItems[0].id;
|
|
36
|
-
// if currently highlighted item has children, select first of them
|
|
37
|
-
return menuHelpers.getItem(menuItems, currentId)?.items?.[0].id;
|
|
38
|
-
case 'parent':
|
|
39
|
-
// find and return parent of currently selected item
|
|
40
|
-
return menuHelpers.getParentItem(menuItems, currentId)?.id;
|
|
41
|
-
case 'next':
|
|
42
|
-
// return next sibling of currently selected item
|
|
43
|
-
return menuHelpers.getNextItem(menuItems, currentId)?.id ?? currentId;
|
|
44
|
-
case 'prev':
|
|
45
|
-
// return prev sibling of currently selected item
|
|
46
|
-
return menuHelpers.getPrevItem(menuItems, currentId)?.id ?? currentId;
|
|
47
|
-
case 'list_change':
|
|
48
|
-
// if currently selected item does not exist in new list, select first one
|
|
49
|
-
if (currentId && menuHelpers.getItem(menuItems, currentId))
|
|
50
|
-
return currentId;
|
|
51
|
-
return menuItems[0]?.id;
|
|
52
|
-
case 'set':
|
|
53
|
-
// return arbitrary selected item id if it is in the tree; undefined otherwise
|
|
54
|
-
return selectedItemId ? menuHelpers.getItem(menuItems, selectedItemId)?.id : undefined;
|
|
55
|
-
case 'clear':
|
|
56
|
-
return undefined;
|
|
57
|
-
default:
|
|
58
|
-
return currentId;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
29
|
const StyledSearchPopover = styled(StyledPopover) `
|
|
62
30
|
min-width: 20rem;
|
|
63
31
|
`;
|
|
64
32
|
const RichTextEditor = forwardRef((props, ref) => {
|
|
65
33
|
const { components: { 'text-area': { 'min-height': minHeight } } } = useTheme();
|
|
66
34
|
const uid = useUID();
|
|
35
|
+
const labelId = useUID();
|
|
67
36
|
const { id = uid, label, labelHidden, info, status, required, disabled, readOnly, onChange, onFocus, onBlur, placeholder, toolbar, onImageAdded, markdownOnly, searchTriggers, onSearch, getSearchItemReplacement, searchRenderers, menu, height = { min: minHeight }, defaultValue, autoFocus, ...restProps } = props;
|
|
68
37
|
const formFieldRef = useRef(null);
|
|
69
38
|
const editor = useMemo(() => withVoids(withReact(withHistory(createEditor()))), []);
|
|
@@ -73,7 +42,6 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
73
42
|
const [toolbarPopover, setToolbarPopover] = useState(false);
|
|
74
43
|
const [toolbarSticky, setToolbarSticky] = useState(false);
|
|
75
44
|
const [focusableToolbarBtn, setFocusableToolbarBtn] = useState(null);
|
|
76
|
-
const [focusedId, dispatchFocus] = useReducer(syntheticFocusReducer, undefined);
|
|
77
45
|
const [popoverTarget, setPopoverTarget] = useState(null);
|
|
78
46
|
const [searchEl, setSearchEl] = useState(null);
|
|
79
47
|
const updateToolbarStyle = () => {
|
|
@@ -119,6 +87,7 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
119
87
|
if (ReactEditor.isFocused(editor)) {
|
|
120
88
|
onBlur?.();
|
|
121
89
|
}
|
|
90
|
+
setPopoverTarget(null);
|
|
122
91
|
};
|
|
123
92
|
useImperativeHandle(ref, () => ({
|
|
124
93
|
focus: () => {
|
|
@@ -136,12 +105,13 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
136
105
|
clear: () => {
|
|
137
106
|
const point = { path: [0, 0], offset: 0 };
|
|
138
107
|
editor.selection = { anchor: point, focus: point };
|
|
139
|
-
|
|
108
|
+
editor.children = [
|
|
140
109
|
{
|
|
141
110
|
type: 'paragraph',
|
|
142
111
|
children: [{ text: '' }]
|
|
143
112
|
}
|
|
144
|
-
]
|
|
113
|
+
];
|
|
114
|
+
setValue(editor.children);
|
|
145
115
|
},
|
|
146
116
|
appendImage: ({ src, alt }, imageId) => {
|
|
147
117
|
const path = EditorCommands.appendImage(editor, { src, alt }, imageId);
|
|
@@ -154,16 +124,17 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
154
124
|
editor.insertText(text);
|
|
155
125
|
},
|
|
156
126
|
insertHtml: (html, overwrite = false) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
127
|
+
if (overwrite) {
|
|
128
|
+
const point = { path: [0, 0], offset: 0 };
|
|
129
|
+
editor.selection = { anchor: point, focus: point };
|
|
130
|
+
editor.children = convertHtml(html !== '' ? html : '<p></p>');
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
editor.children = [...value, ...convertHtml(html)];
|
|
134
|
+
}
|
|
135
|
+
setValue(editor.children);
|
|
165
136
|
}
|
|
166
|
-
}), [
|
|
137
|
+
}), [editor, value]);
|
|
167
138
|
const onMenuItemClick = (itemId, e) => {
|
|
168
139
|
// MenuItem expects actual click events, but we don't have reference to actual menu item element to trigger a click.
|
|
169
140
|
menu?.onItemClick?.(itemId, e);
|
|
@@ -189,30 +160,9 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
189
160
|
}
|
|
190
161
|
};
|
|
191
162
|
const onKeyDown = useCallback((event) => {
|
|
192
|
-
if (popoverTarget && menu) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
event.preventDefault();
|
|
196
|
-
dispatchFocus({ type: 'next', menuItems: menu.items, selectedItemId: focusedId });
|
|
197
|
-
break;
|
|
198
|
-
case 'ArrowUp':
|
|
199
|
-
event.preventDefault();
|
|
200
|
-
dispatchFocus({ type: 'prev', menuItems: menu.items, selectedItemId: focusedId });
|
|
201
|
-
break;
|
|
202
|
-
case 'Enter':
|
|
203
|
-
event.preventDefault();
|
|
204
|
-
if (focusedId) {
|
|
205
|
-
onMenuItemClick(focusedId, event);
|
|
206
|
-
dispatchFocus({ type: 'clear', menuItems: menu.items });
|
|
207
|
-
}
|
|
208
|
-
break;
|
|
209
|
-
case 'Escape':
|
|
210
|
-
event.preventDefault();
|
|
211
|
-
setPopoverTarget(null);
|
|
212
|
-
break;
|
|
213
|
-
default:
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
163
|
+
if (popoverTarget && menu && event.key === 'Escape') {
|
|
164
|
+
event.preventDefault();
|
|
165
|
+
setPopoverTarget(null);
|
|
216
166
|
}
|
|
217
167
|
else if (event.key === 'Tab' && !event.shiftKey) {
|
|
218
168
|
if (focusableToolbarBtn) {
|
|
@@ -227,7 +177,7 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
227
177
|
EditorCommands.checkImageEnter(editor, event);
|
|
228
178
|
EditorCommands.checkKeyCommands(editor, event);
|
|
229
179
|
}
|
|
230
|
-
}, [menu,
|
|
180
|
+
}, [menu, popoverTarget, focusableToolbarBtn]);
|
|
231
181
|
const onKeyUp = (event) => {
|
|
232
182
|
EditorCommands.checkSearchUpKeys(editor, event);
|
|
233
183
|
};
|
|
@@ -268,15 +218,6 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
268
218
|
useEffect(() => {
|
|
269
219
|
onChange?.();
|
|
270
220
|
}, [value]);
|
|
271
|
-
useEffect(() => {
|
|
272
|
-
if (menu && popoverTarget) {
|
|
273
|
-
dispatchFocus({
|
|
274
|
-
type: 'set',
|
|
275
|
-
menuItems: menu.items,
|
|
276
|
-
selectedItemId: menu.items[0]?.id
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
}, [menu, popoverTarget]);
|
|
280
221
|
useEffect(() => {
|
|
281
222
|
if (autoFocus) {
|
|
282
223
|
setTimeout(() => {
|
|
@@ -289,9 +230,9 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
289
230
|
setPopoverOpen: setToolbarPopover,
|
|
290
231
|
onEditorFocus,
|
|
291
232
|
disabled: disabled || false
|
|
292
|
-
}, children: _jsx(FormField, { as:
|
|
233
|
+
}, children: _jsx(FormField, { as: StyledRichTextFormField, toolbar: !!(!markdownOnly && toolbar?.length), label:
|
|
293
234
|
// eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events
|
|
294
|
-
_jsx("span", { onClick: () => onEditorFocus({ focusEditor: true }), children: label }, void 0), labelHidden: labelHidden, id: id, info: info, status: status, required: required, disabled: disabled, readOnly: readOnly, onDrop: onDrop, onFocus: (e) => {
|
|
235
|
+
_jsx("span", { onClick: () => onEditorFocus({ focusEditor: true }), children: label }, void 0), labelHidden: labelHidden, labelFor: '', labelId: labelId, id: id, info: info, status: status, required: required, disabled: disabled, readOnly: readOnly, onDrop: onDrop, onFocus: (e) => {
|
|
295
236
|
if (e.target === formFieldRef.current && !disabled) {
|
|
296
237
|
onEditorFocus({ focusEditor: true });
|
|
297
238
|
}
|
|
@@ -337,15 +278,15 @@ const RichTextEditor = forwardRef((props, ref) => {
|
|
|
337
278
|
}
|
|
338
279
|
}, children: [_jsxs(FormControl, { ...restProps, ref: ref, ariaLabel: label, required: required, disabled: disabled, readOnly: readOnly, status: status, as: StyledRichTextEditor, toolbar: !!(!markdownOnly && toolbar?.length), onClick: () => {
|
|
339
280
|
onEditorFocus({ focusEditor: true });
|
|
340
|
-
}, focused: ReactEditor.isFocused(editor), children: [_jsx(
|
|
281
|
+
}, focused: ReactEditor.isFocused(editor), children: [_jsx(StyledEditorContainer, { toolbar: !!(!markdownOnly && toolbar?.length), containerHeight: height, onClick: () => {
|
|
341
282
|
if (!disabled) {
|
|
342
283
|
onEditorFocus({ focusEditor: true });
|
|
343
284
|
}
|
|
344
|
-
}, children: _jsx(Editable, { id: id, renderElement: nodeRenderer, renderLeaf: leafRenderer, onKeyDown: onKeyDown, onKeyUp: onKeyUp, placeholder: placeholder, onFocus: event => {
|
|
285
|
+
}, children: _jsx(Editable, { id: id, renderElement: nodeRenderer, renderLeaf: leafRenderer, onKeyDown: onKeyDown, onKeyUp: onKeyUp, placeholder: placeholder, onFocus: (event) => {
|
|
345
286
|
onEditorFocus({ focusEditor: true, event });
|
|
346
|
-
}, onBlur: () => blurEditor(), disabled: disabled, readOnly: readOnly, onPaste: onPaste, tabIndex: disabled ? -1 : undefined }, void 0) }, void 0), !markdownOnly && !readOnly && toolbar?.length ? (_jsx(Toolbar, { features: toolbar, sticky: toolbarSticky, setFocusableBtn: el => {
|
|
287
|
+
}, onBlur: () => blurEditor(), disabled: disabled, readOnly: readOnly, onPaste: onPaste, tabIndex: disabled ? -1 : undefined, "aria-labelledby": labelId }, void 0) }, void 0), !markdownOnly && !readOnly && toolbar?.length ? (_jsx(Toolbar, { features: toolbar, sticky: toolbarSticky, setFocusableBtn: el => {
|
|
347
288
|
setFocusableToolbarBtn(el);
|
|
348
|
-
} }, void 0)) : null] }, void 0), _jsx(Popover, { show: !!popoverTarget && !!menu, strategy: 'fixed', target: searchEl, placement: 'bottom-start', children: menu && (_jsx(Menu, { ...menu, as: StyledSearchPopover,
|
|
289
|
+
} }, void 0)) : null] }, void 0), _jsx(Popover, { show: !!popoverTarget && !!menu, strategy: 'fixed', target: searchEl, placement: 'bottom-start', children: menu && (_jsx(Menu, { ...menu, as: StyledSearchPopover, focusControlEl: formFieldRef.current?.querySelector('[role="textbox"]') || undefined, mode: 'action', onItemClick: onMenuItemClick }, void 0)) }, void 0)] }, void 0) }, void 0) }, void 0));
|
|
349
290
|
});
|
|
350
291
|
export default RichTextEditor;
|
|
351
292
|
//# sourceMappingURL=RichTextEditor.js.map
|