@pega/cosmos-react-rte 9.0.0-build.9.8 → 9.0.0
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/DynamicContentEditor/DynamicContentEditor.d.ts +4 -2
- package/lib/components/DynamicContentEditor/DynamicContentEditor.d.ts.map +1 -1
- package/lib/components/DynamicContentEditor/DynamicContentEditor.js +64 -59
- package/lib/components/DynamicContentEditor/DynamicContentEditor.js.map +1 -1
- package/lib/components/Editor/Editor.context.d.ts +6 -6
- package/lib/components/Editor/Editor.context.d.ts.map +1 -1
- package/lib/components/Editor/Editor.context.js +1 -1
- package/lib/components/Editor/Editor.context.js.map +1 -1
- package/lib/components/Editor/Editor.d.ts +1 -10
- package/lib/components/Editor/Editor.d.ts.map +1 -1
- package/lib/components/Editor/Editor.js +301 -490
- package/lib/components/Editor/Editor.js.map +1 -1
- package/lib/components/Editor/Editor.styles.d.ts +37 -7
- package/lib/components/Editor/Editor.styles.d.ts.map +1 -1
- package/lib/components/Editor/Editor.styles.js +60 -30
- package/lib/components/Editor/Editor.styles.js.map +1 -1
- package/lib/components/Editor/Editor.test-ids.d.ts +2 -1
- package/lib/components/Editor/Editor.test-ids.d.ts.map +1 -1
- package/lib/components/Editor/Editor.test-ids.js +2 -0
- package/lib/components/Editor/Editor.test-ids.js.map +1 -1
- package/lib/components/Editor/Editor.types.d.ts +34 -14
- 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/IframeTiptapEditor.d.ts +30 -0
- package/lib/components/Editor/IframeTiptapEditor.d.ts.map +1 -0
- package/lib/components/Editor/IframeTiptapEditor.js +695 -0
- package/lib/components/Editor/IframeTiptapEditor.js.map +1 -0
- package/lib/components/Editor/ImageActionButtons.d.ts +20 -0
- package/lib/components/Editor/ImageActionButtons.d.ts.map +1 -0
- package/lib/components/Editor/ImageActionButtons.js +84 -0
- package/lib/components/Editor/ImageActionButtons.js.map +1 -0
- package/lib/components/Editor/ImageEditDialog.d.ts +17 -0
- package/lib/components/Editor/ImageEditDialog.d.ts.map +1 -0
- package/lib/components/Editor/ImageEditDialog.js +90 -0
- package/lib/components/Editor/ImageEditDialog.js.map +1 -0
- package/lib/components/Editor/TableCellMenu.d.ts +35 -0
- package/lib/components/Editor/TableCellMenu.d.ts.map +1 -0
- package/lib/components/Editor/TableCellMenu.js +120 -0
- package/lib/components/Editor/TableCellMenu.js.map +1 -0
- package/lib/components/Editor/Toolbar/AIRewriteButton.d.ts +17 -0
- package/lib/components/Editor/Toolbar/AIRewriteButton.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/AIRewriteButton.js +79 -0
- package/lib/components/Editor/Toolbar/AIRewriteButton.js.map +1 -0
- package/lib/components/Editor/Toolbar/AlignmentSelect.d.ts +8 -0
- package/lib/components/Editor/Toolbar/AlignmentSelect.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/AlignmentSelect.js +137 -0
- package/lib/components/Editor/Toolbar/AlignmentSelect.js.map +1 -0
- package/lib/components/Editor/Toolbar/AnchorButton.d.ts +3 -4
- package/lib/components/Editor/Toolbar/AnchorButton.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/AnchorButton.js +156 -82
- package/lib/components/Editor/Toolbar/AnchorButton.js.map +1 -1
- package/lib/components/Editor/Toolbar/ColorPickerButton.d.ts +9 -0
- package/lib/components/Editor/Toolbar/ColorPickerButton.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/ColorPickerButton.js +190 -0
- package/lib/components/Editor/Toolbar/ColorPickerButton.js.map +1 -0
- package/lib/components/Editor/Toolbar/FontFamilySelect.d.ts +8 -0
- package/lib/components/Editor/Toolbar/FontFamilySelect.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/FontFamilySelect.js +150 -0
- package/lib/components/Editor/Toolbar/FontFamilySelect.js.map +1 -0
- package/lib/components/Editor/Toolbar/FontSizeSelect.d.ts +8 -0
- package/lib/components/Editor/Toolbar/FontSizeSelect.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/FontSizeSelect.js +145 -0
- package/lib/components/Editor/Toolbar/FontSizeSelect.js.map +1 -0
- package/lib/components/Editor/Toolbar/ImageButton.d.ts +5 -5
- package/lib/components/Editor/Toolbar/ImageButton.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/ImageButton.js +131 -18
- package/lib/components/Editor/Toolbar/ImageButton.js.map +1 -1
- package/lib/components/Editor/Toolbar/SourceCodeButton.d.ts +8 -0
- package/lib/components/Editor/Toolbar/SourceCodeButton.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/SourceCodeButton.js +49 -0
- package/lib/components/Editor/Toolbar/SourceCodeButton.js.map +1 -0
- package/lib/components/Editor/Toolbar/TableButton.d.ts +8 -0
- package/lib/components/Editor/Toolbar/TableButton.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/TableButton.js +291 -0
- package/lib/components/Editor/Toolbar/TableButton.js.map +1 -0
- package/lib/components/Editor/Toolbar/TextSelect.d.ts +4 -5
- package/lib/components/Editor/Toolbar/TextSelect.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/TextSelect.js +61 -30
- package/lib/components/Editor/Toolbar/TextSelect.js.map +1 -1
- package/lib/components/Editor/Toolbar/Toolbar.d.ts +17 -6
- package/lib/components/Editor/Toolbar/Toolbar.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/Toolbar.js +169 -47
- package/lib/components/Editor/Toolbar/Toolbar.js.map +1 -1
- package/lib/components/Editor/Toolbar/Toolbar.test-ids.d.ts +2 -2
- package/lib/components/Editor/Toolbar/Toolbar.test-ids.d.ts.map +1 -1
- package/lib/components/Editor/Toolbar/Toolbar.test-ids.js +17 -1
- package/lib/components/Editor/Toolbar/Toolbar.test-ids.js.map +1 -1
- package/lib/components/Editor/Toolbar/WordCount.d.ts +8 -0
- package/lib/components/Editor/Toolbar/WordCount.d.ts.map +1 -0
- package/lib/components/Editor/Toolbar/WordCount.js +31 -0
- package/lib/components/Editor/Toolbar/WordCount.js.map +1 -0
- package/lib/components/Editor/extensions/FontSize.d.ts +21 -0
- package/lib/components/Editor/extensions/FontSize.d.ts.map +1 -0
- package/lib/components/Editor/extensions/FontSize.js +42 -0
- package/lib/components/Editor/extensions/FontSize.js.map +1 -0
- package/lib/components/Editor/extensions/PreserveDiv.d.ts +13 -0
- package/lib/components/Editor/extensions/PreserveDiv.d.ts.map +1 -0
- package/lib/components/Editor/extensions/PreserveDiv.js +73 -0
- package/lib/components/Editor/extensions/PreserveDiv.js.map +1 -0
- package/lib/components/Editor/extensions/TableCellSelection.d.ts +4 -0
- package/lib/components/Editor/extensions/TableCellSelection.d.ts.map +1 -0
- package/lib/components/Editor/extensions/TableCellSelection.js +53 -0
- package/lib/components/Editor/extensions/TableCellSelection.js.map +1 -0
- package/lib/components/Editor/extensions/TextIndent.d.ts +22 -0
- package/lib/components/Editor/extensions/TextIndent.d.ts.map +1 -0
- package/lib/components/Editor/extensions/TextIndent.js +137 -0
- package/lib/components/Editor/extensions/TextIndent.js.map +1 -0
- package/lib/components/Editor/hooks/useCloseOnEditorClick.d.ts +5 -0
- package/lib/components/Editor/hooks/useCloseOnEditorClick.d.ts.map +1 -0
- package/lib/components/Editor/hooks/useCloseOnEditorClick.js +18 -0
- package/lib/components/Editor/hooks/useCloseOnEditorClick.js.map +1 -0
- package/lib/components/Editor/hooks/useEscapeKey.d.ts +4 -0
- package/lib/components/Editor/hooks/useEscapeKey.d.ts.map +1 -0
- package/lib/components/Editor/hooks/useEscapeKey.js +24 -0
- package/lib/components/Editor/hooks/useEscapeKey.js.map +1 -0
- package/lib/components/Editor/hooks/useIframeSetup.d.ts +54 -0
- package/lib/components/Editor/hooks/useIframeSetup.d.ts.map +1 -0
- package/lib/components/Editor/hooks/useIframeSetup.js +284 -0
- package/lib/components/Editor/hooks/useIframeSetup.js.map +1 -0
- package/lib/components/Editor/hooks/useImageActions.d.ts +19 -0
- package/lib/components/Editor/hooks/useImageActions.d.ts.map +1 -0
- package/lib/components/Editor/hooks/useImageActions.js +198 -0
- package/lib/components/Editor/hooks/useImageActions.js.map +1 -0
- package/lib/components/Editor/hooks/useTableCellMenu.d.ts +22 -0
- package/lib/components/Editor/hooks/useTableCellMenu.d.ts.map +1 -0
- package/lib/components/Editor/hooks/useTableCellMenu.js +120 -0
- package/lib/components/Editor/hooks/useTableCellMenu.js.map +1 -0
- package/lib/components/Editor/iframeContentStyles.d.ts +10 -0
- package/lib/components/Editor/iframeContentStyles.d.ts.map +1 -0
- package/lib/components/Editor/iframeContentStyles.js +162 -0
- package/lib/components/Editor/iframeContentStyles.js.map +1 -0
- package/lib/components/Editor/index.d.ts +2 -0
- package/lib/components/Editor/index.d.ts.map +1 -1
- package/lib/components/Editor/index.js +1 -0
- package/lib/components/Editor/index.js.map +1 -1
- package/lib/components/Editor/sanitize.d.ts +3 -0
- package/lib/components/Editor/sanitize.d.ts.map +1 -0
- package/lib/components/Editor/sanitize.js +11 -0
- package/lib/components/Editor/sanitize.js.map +1 -0
- package/lib/components/Editor/utils/htmlPlaceholder.d.ts +69 -0
- package/lib/components/Editor/utils/htmlPlaceholder.d.ts.map +1 -0
- package/lib/components/Editor/utils/htmlPlaceholder.js +154 -0
- package/lib/components/Editor/utils/htmlPlaceholder.js.map +1 -0
- package/lib/components/RichTextEditor/DecoratorComponents/Table.d.ts +6 -4
- package/lib/components/RichTextEditor/DecoratorComponents/Table.d.ts.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Table.js +10 -8
- 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 +15 -2
- package/lib/components/RichTextEditor/RichTextEditor.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts +5 -5
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.js +3 -5
- package/lib/components/RichTextEditor/RichTextEditor.styles.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.types.d.ts +5 -0
- package/lib/components/RichTextEditor/RichTextEditor.types.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.types.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.js +9 -2
- package/lib/components/RichTextEditor/RichTextViewer.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/Toolbar.js +1 -1
- package/lib/components/RichTextEditor/Toolbar/Toolbar.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/Toolbar.types.d.ts +4 -4
- package/lib/components/RichTextEditor/Toolbar/Toolbar.types.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/Toolbar.types.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.js +41 -26
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.js.map +1 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.d.ts +2 -0
- package/lib/components/RichTextEditor/utils/htmlConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.js +12 -0
- package/lib/components/RichTextEditor/utils/htmlConverter.js.map +1 -1
- package/lib/components/RichTextEditor/utils/interactionRenderer.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/interactionRenderer.js +20 -19
- package/lib/components/RichTextEditor/utils/interactionRenderer.js.map +1 -1
- package/lib/components/RichTextEditor/utils/markdownConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/markdownConverter.js +131 -30
- package/lib/components/RichTextEditor/utils/markdownConverter.js.map +1 -1
- package/lib/components/RichTextEditor/utils/renderers.d.ts +5 -3
- package/lib/components/RichTextEditor/utils/renderers.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/renderers.js +62 -34
- package/lib/components/RichTextEditor/utils/renderers.js.map +1 -1
- package/lib/components/RichTextEditor/utils/slateConverter.d.ts +4 -3
- package/lib/components/RichTextEditor/utils/slateConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/slateConverter.js +86 -38
- package/lib/components/RichTextEditor/utils/slateConverter.js.map +1 -1
- package/package.json +30 -8
- package/lib/components/Editor/ImageEditor.d.ts +0 -10
- package/lib/components/Editor/ImageEditor.d.ts.map +0 -1
- package/lib/components/Editor/ImageEditor.js +0 -292
- package/lib/components/Editor/ImageEditor.js.map +0 -1
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { forwardRef, useState, useEffect } from 'react';
|
|
3
|
-
import
|
|
3
|
+
import styled, { css } from 'styled-components';
|
|
4
|
+
import { HTML, ErrorState, useI18n, useTestIds, withTestIds, hasProp, defaultThemeProp } from '@pega/cosmos-react-core';
|
|
4
5
|
import convertMarkdown from './utils/markdownConverter';
|
|
5
6
|
import { StyledEditorContainer, StyledRichTextViewer } from './RichTextEditor.styles';
|
|
6
7
|
import { convertHtml } from './utils/htmlConverter';
|
|
7
8
|
import { RichTextTreeRenderer } from './RichTextTreeRenderer';
|
|
8
9
|
import { getRichTextViewerTestIds } from './RichTextViewer.test-ids';
|
|
10
|
+
const StyledHTMLViewer = styled(HTML)(({ theme: { base: { spacing } } }) => {
|
|
11
|
+
return css `
|
|
12
|
+
padding: calc(0.125 * ${spacing});
|
|
13
|
+
`;
|
|
14
|
+
});
|
|
15
|
+
StyledHTMLViewer.defaultProps = defaultThemeProp;
|
|
9
16
|
// Regex to match valid URLs
|
|
10
17
|
export const URL_REGEX = /(?<leadingChar>^|\s)(?<url>(?<protocol>https?:\/\/)?(?<host>[a-z0-9]+(?:[-.][a-z0-9]+)*\.(?<tld>[a-z]{2,5})(?<port>:[0-9]{1,5})?)(?<pathQueryHash>[/a-zA-Z0-9?=&+%@.|#_~:[\]'()-]*)?)/g;
|
|
11
18
|
const isValidHTTPURL = (href) => {
|
|
@@ -63,7 +70,7 @@ const RichTextViewer = forwardRef(function RichTextViewer({ testId, content, typ
|
|
|
63
70
|
}
|
|
64
71
|
}, [content, type, interactionRenderers]);
|
|
65
72
|
if (type === 'html' && (!interactionRenderers || interactionRenderers.length === 0)) {
|
|
66
|
-
return _jsx(
|
|
73
|
+
return _jsx(StyledHTMLViewer, { ref: ref, content: content });
|
|
67
74
|
}
|
|
68
75
|
return error ? (_jsx(ErrorState, { message: t('rte_invalid_html'), onRetry: onRetry })) : (_jsx(StyledRichTextViewer, { "data-testid": testIds.root, ref: ref, ...restProps, children: _jsx(StyledEditorContainer, { children: _jsx(RichTextTreeRenderer, { nodes: nodes }) }) }));
|
|
69
76
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextViewer.js","sourceRoot":"","sources":["../../../src/components/RichTextEditor/RichTextViewer.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"RichTextViewer.js","sourceRoot":"","sources":["../../../src/components/RichTextEditor/RichTextViewer.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGxD,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EACL,IAAI,EACJ,UAAU,EACV,OAAO,EACP,UAAU,EACV,WAAW,EACX,OAAO,EACP,gBAAgB,EACjB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,eAAe,MAAM,2BAA2B,CAAC;AAExD,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AACtF,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9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K,CAAC,CAAC;YAClB,CAAC;YAAC,MAAM,CAAC;gBACP,QAAQ,CAAC,EAAE,CAAC,CAAC;gBACb,QAAQ,CAAC,IAAI,CAAC,CAAC;YACjB,CAAC;QACH,CAAC;IACH,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,oBAAoB,CAAC,CAAC,CAAC;IAE1C,IAAI,IAAI,KAAK,MAAM,IAAI,CAAC,CAAC,oBAAoB,IAAI,oBAAoB,CAAC,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC;QACpF,OAAO,KAAC,gBAAgB,IAAC,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,GAAI,CAAC;IAC1D,CAAC;IAED,OAAO,KAAK,CAAC,CAAC,CAAC,CACb,KAAC,UAAU,IAAC,OAAO,EAAE,CAAC,CAAC,kBAAkB,CAAC,EAAE,OAAO,EAAE,OAAO,GAAI,CACjE,CAAC,CAAC,CAAC,CACF,KAAC,oBAAoB,mBAAc,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,KAAM,SAAS,YACtE,KAAC,qBAAqB,cACpB,KAAC,oBAAoB,IAAC,KAAK,EAAE,KAAK,GAAI,GAChB,GACH,CACxB,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,WAAW,CAAC,cAAc,EAAE,wBAAwB,CAAC,CAAC","sourcesContent":["import { forwardRef, useState, useEffect } from 'react';\nimport type { PropsWithoutRef } from 'react';\nimport type { PegaCustom } from 'slate';\nimport styled, { css } from 'styled-components';\n\nimport {\n HTML,\n ErrorState,\n useI18n,\n useTestIds,\n withTestIds,\n hasProp,\n defaultThemeProp\n} from '@pega/cosmos-react-core';\nimport type { ForwardRefForwardPropsComponent } from '@pega/cosmos-react-core';\n\nimport convertMarkdown from './utils/markdownConverter';\nimport type { RichTextViewerProps } from './RichTextViewer.types';\nimport { StyledEditorContainer, StyledRichTextViewer } from './RichTextEditor.styles';\nimport { convertHtml } from './utils/htmlConverter';\nimport { RichTextTreeRenderer } from './RichTextTreeRenderer';\nimport type { RichTextTreeRendererProps } from './RichTextTreeRenderer';\nimport { getRichTextViewerTestIds } from './RichTextViewer.test-ids';\n\nconst StyledHTMLViewer = styled(HTML)(({\n theme: {\n base: { spacing }\n }\n}) => {\n return css`\n padding: calc(0.125 * ${spacing});\n `;\n});\n\nStyledHTMLViewer.defaultProps = defaultThemeProp;\n\n// Regex to match valid URLs\nexport const URL_REGEX =\n /(?<leadingChar>^|\\s)(?<url>(?<protocol>https?:\\/\\/)?(?<host>[a-z0-9]+(?:[-.][a-z0-9]+)*\\.(?<tld>[a-z]{2,5})(?<port>:[0-9]{1,5})?)(?<pathQueryHash>[/a-zA-Z0-9?=&+%@.|#_~:[\\]'()-]*)?)/g;\n\nconst isValidHTTPURL = (href?: string): boolean => {\n if (!href) return false;\n\n try {\n const url = new URL(href, window.location.href);\n return url.protocol.startsWith('http');\n } catch {\n return false;\n }\n};\n\nconst findFirstHref = (nodes: (PegaCustom.Element | PegaCustom.Text)[]): string | undefined => {\n for (const node of nodes) {\n if (hasProp(node, 'children')) {\n const href = findFirstHref(node.children);\n if (isValidHTTPURL(href)) {\n return href;\n }\n } else if (isValidHTTPURL(node.href)) {\n return node.href;\n }\n }\n};\n\nconst RichTextViewer: ForwardRefForwardPropsComponent<RichTextViewerProps> = forwardRef(\n function RichTextViewer(\n {\n testId,\n content,\n type,\n interactionRenderers,\n markdownMap,\n onRetry,\n onMarkdownLinkRender,\n ...restProps\n }: PropsWithoutRef<RichTextViewerProps>,\n ref: RichTextViewerProps['ref']\n ) {\n const testIds = useTestIds(testId, getRichTextViewerTestIds);\n const t = useI18n();\n const [nodes, setNodes] = useState<RichTextTreeRendererProps['nodes']>([]);\n const [error, setError] = useState(false);\n\n useEffect(() => {\n if (type === 'markdown') {\n const newNodes = convertMarkdown(\n content,\n [...(interactionRenderers ?? []), { regexPattern: URL_REGEX, type: 'builtin-url' }],\n markdownMap\n );\n\n if (onMarkdownLinkRender) {\n const href = findFirstHref(newNodes);\n if (href) {\n onMarkdownLinkRender(href);\n }\n }\n\n setNodes(newNodes);\n }\n if (type === 'richtext') {\n setNodes(JSON.parse(content));\n }\n if (type === 'html' && interactionRenderers && interactionRenderers.length > 0) {\n try {\n setNodes(convertHtml(content, interactionRenderers));\n setError(false);\n } catch {\n setNodes([]);\n setError(true);\n }\n }\n }, [content, type, interactionRenderers]);\n\n if (type === 'html' && (!interactionRenderers || interactionRenderers.length === 0)) {\n return <StyledHTMLViewer ref={ref} content={content} />;\n }\n\n return error ? (\n <ErrorState message={t('rte_invalid_html')} onRetry={onRetry} />\n ) : (\n <StyledRichTextViewer data-testid={testIds.root} ref={ref} {...restProps}>\n <StyledEditorContainer>\n <RichTextTreeRenderer nodes={nodes} />\n </StyledEditorContainer>\n </StyledRichTextViewer>\n );\n }\n);\n\nexport default withTestIds(RichTextViewer, getRichTextViewerTestIds);\n"]}
|
|
@@ -14,7 +14,7 @@ import IndentButton from './IndentButton';
|
|
|
14
14
|
import { getKeyCommand } from './utils';
|
|
15
15
|
const StyledToolbar = styled.div(({ sticky, theme }) => {
|
|
16
16
|
return css `
|
|
17
|
-
background-color: ${props => props.theme.
|
|
17
|
+
background-color: ${props => props.theme.components['form-control']['background-color']};
|
|
18
18
|
border-radius: calc(0.25 * ${props => props.theme.base['border-radius']});
|
|
19
19
|
overflow: auto;
|
|
20
20
|
padding: 0.125rem;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Toolbar.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/Toolbar.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAGpD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EACL,IAAI,EACJ,IAAI,EACJ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IAAC,IAAI,EAAC,QAAQ,EAAC,GAAG,EAAE,GAAG,GAAI,EACxC,KAAC,YAAY,IAAC,IAAI,EAAC,UAAU,EAAC,GAAG,EAAE,GAAG,GAAI,IACzC,CACJ,EACA,cAAc,CAAC,MAAM,IAAI,KAAC,WAAW,KAAG,IACpC,EACP,MAAC,IAAI,IAAC,SAAS,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,aACnC,cAAc,CAAC,KAAK,IAAI,KAAC,YAAY,IAAC,GAAG,EAAE,GAAG,GAAI,EAClD,cAAc,CAAC,MAAM,IAAI,KAAC,WAAW,KAAG,IACpC,EACN,cAAc,CAAC,gBAAgB,CAAC,IAAI,CACnC,KAAC,IAAI,IAAC,SAAS,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,YAAG,kBAAkB,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC,GAAQ,CACpF,IACI,CACR,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,OAAO,CAAC","sourcesContent":["import { useState, useEffect, useRef } from 'react';\nimport type { FC } from 'react';\nimport type { Text } from 'slate';\nimport { useSlate, useFocused, ReactEditor } from 'slate-react';\nimport styled, { css } from 'styled-components';\n\nimport {\n Flex,\n Icon,\n useArrows,\n useI18n,\n defaultThemeProp,\n navigatorIsAvailable\n} from '@pega/cosmos-react-core';\nimport type { TranslationPack, ExcludeStrict } from '@pega/cosmos-react-core';\nimport type { TranslationFunction } from '@pega/cosmos-react-core/lib/i18n/translate';\n\nimport EditorCommands from '../utils/EditorCommands';\nimport ListCommands from '../utils/ListCommands';\n\nimport ToolbarButton from './ToolbarButton';\nimport type { StyleType, Features } from './Toolbar.types';\nimport TableButton from './TableButton';\nimport AnchorButton from './AnchorButton';\nimport ImageButton from './ImageButton';\nimport TextSelect from './TextSelect';\nimport IndentButton from './IndentButton';\nimport { getKeyCommand } from './utils';\n\nconst StyledToolbar = styled.div<{ sticky?: boolean }>(({ sticky, theme }) => {\n return css`\n background-color: ${props => props.theme.base.palette['primary-background']};\n border-radius: calc(0.25 * ${props => props.theme.base['border-radius']});\n overflow: auto;\n padding: 0.125rem;\n ${sticky &&\n css`\n position: sticky;\n bottom: 0;\n `}\n\n &:has(:focus-visible) {\n box-shadow: ${theme.base.shadow['focus-group']};\n }\n `;\n});\n\nStyledToolbar.defaultProps = defaultThemeProp;\n\nexport interface ToolbarProps {\n features: Features[];\n sticky?: boolean;\n setFocusableBtn: (el: HTMLElement) => void;\n}\n\ntype StyleButtonType = {\n style: StyleType;\n text: string;\n icon?: string;\n label?: string;\n format: ExcludeStrict<keyof Text, 'text'>;\n tooltip: string;\n};\n\nconst isMobile = navigatorIsAvailable && navigator.userAgent.includes('Mobile');\n\n// Function that returns the text styling buttons\nfunction renderStyleButtons(\n editor: ReactEditor,\n t: TranslationFunction<TranslationPack>,\n focused: 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 tooltip: isMobile\n ? `${t('rte_bold')}`\n : getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_bold')} (${ctrl}B, ${ctrl}${shift}B)`)\n },\n {\n style: 'ITALIC',\n text: 'I',\n format: 'italic',\n label: t('rte_italic'),\n tooltip: isMobile\n ? `${t('rte_italic')}`\n : getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_italic')} (${ctrl}I, ${ctrl}${shift}I)`)\n },\n {\n style: 'STRIKE-THROUGH',\n text: 'S',\n format: 'line-through',\n label: t('rte_strike_through'),\n tooltip: isMobile\n ? `${t('rte_strike_through')}`\n : getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_strike_through')} (${ctrl}${shift}X)`)\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 (!focused) {\n ReactEditor.focus(editor);\n }\n setTimeout(() => {\n EditorCommands.toggleFormat(format, editor);\n }, 0);\n }}\n styleType={style}\n active={EditorCommands.isFormatActive(format, editor)}\n tooltip={tooltip}\n label={label}\n >\n {text}\n </ToolbarButton>\n );\n });\n}\n\n// Function that returns the cut, copy and paste buttons\nfunction renderCutCopyPaste(\n editor: ReactEditor,\n t: TranslationFunction<TranslationPack>,\n osx: boolean\n) {\n const CutCopyPasteButtons = [\n {\n text: 'Cut',\n icon: 'scissors',\n label: t('rte_cut'),\n func: () => {\n document.execCommand('cut');\n },\n tooltip: getKeyCommand(osx, ({ ctrl }) => `${t('rte_cut')} (${ctrl}X)`)\n },\n {\n text: 'Copy',\n icon: 'copy',\n label: t('rte_copy'),\n func: () => {\n document.execCommand('copy');\n },\n tooltip: getKeyCommand(osx, ({ ctrl }) => `${t('rte_copy')} (${ctrl}C)`)\n },\n {\n text: 'Paste',\n icon: 'clipboard-data',\n label: t('rte_paste'),\n func: () => {\n navigator.clipboard.readText().then(text => {\n EditorCommands.replaceWithText(editor, editor.selection, text);\n });\n },\n tooltip: getKeyCommand(osx, ({ ctrl }) => `${t('rte_paste')} (${ctrl}V)`)\n }\n ];\n\n return CutCopyPasteButtons.map(btn => {\n return (\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n btn.func();\n }}\n key={btn.text}\n tooltip={btn.tooltip}\n label={btn.label}\n >\n <Icon name={btn.icon} />\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(\n (acc, feature) => {\n return { ...acc, [feature]: true };\n },\n {} as { [F in Features]: boolean }\n );\n}\n\nconst Toolbar: FC<ToolbarProps> = (props: ToolbarProps) => {\n const { features, sticky, setFocusableBtn } = props;\n const editor = useSlate();\n const focused = useFocused();\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\n useEffect(() => {\n const btn = toolbarRef?.current?.querySelector('button');\n if (btn) setFocusableBtn(btn);\n }, [toolbarRef.current]);\n\n useEffect(() => {\n setActiveFeatures(createActiveFeaturesMap(features));\n }, [features]);\n\n useEffect(() => {\n if (navigator.appVersion.includes('Mac')) setOsx(true);\n }, []);\n\n return (\n <Flex as={StyledToolbar} sticky={sticky} container={{ justify: 'between' }} ref={toolbarRef}>\n <Flex container={{ alignItems: 'end' }}>\n <TextSelect osx={osx} />\n {activeFeatures['inline-styling'] && renderStyleButtons(editor, t, focused, osx)}\n {activeFeatures.lists && (\n <>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n ListCommands.toggleList('unordered-list', editor);\n }}\n active={EditorCommands.isBlockActive('unordered-list', editor)}\n tooltip={\n isMobile\n ? `${t('rte_bulleted_list')}`\n : getKeyCommand(\n osx,\n ({ ctrl, shift }) => `${t('rte_bulleted_list')} (${ctrl}${shift}L)`\n )\n }\n label={t('rte_bulleted_list')}\n >\n <Icon name='list' />\n </ToolbarButton>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n ListCommands.toggleList('ordered-list', editor);\n }}\n active={EditorCommands.isBlockActive('ordered-list', editor)}\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 <IndentButton type='indent' osx={osx} />\n <IndentButton type='unindent' osx={osx} />\n </>\n )}\n {activeFeatures.tables && <TableButton />}\n </Flex>\n <Flex container={{ alignItems: 'end' }}>\n {activeFeatures.links && <AnchorButton osx={osx} />}\n {activeFeatures.images && <ImageButton />}\n </Flex>\n {activeFeatures['cut-copy-paste'] && (\n <Flex container={{ alignItems: 'end' }}>{renderCutCopyPaste(editor, t, osx)}</Flex>\n )}\n </Flex>\n );\n};\n\nexport default Toolbar;\n"]}
|
|
1
|
+
{"version":3,"file":"Toolbar.js","sourceRoot":"","sources":["../../../../src/components/RichTextEdit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AC;gCAClD,CAAC,EACD,MAAM,EAAE,cAAc,CAAC,aAAa,CAAC,cAAc,EAAE,MAAM,CAAC,EAC5D,OAAO,EAAE,CAAC,CAAC,mBAAmB,CAAC,EAC/B,KAAK,EAAE,CAAC,CAAC,mBAAmB,CAAC,YAE7B,KAAC,IAAI,IAAC,IAAI,EAAC,aAAa,GAAG,GACb,IACf,CACJ,EACA,cAAc,CAAC,WAAW,IAAI,CAC7B,8BACE,KAAC,YAAY,IAAC,IAAI,EAAC,QAAQ,EAAC,GAAG,EAAE,GAAG,GAAI,EACxC,KAAC,YAAY,IAAC,IAAI,EAAC,UAAU,EAAC,GAAG,EAAE,GAAG,GAAI,IACzC,CACJ,EACA,cAAc,CAAC,MAAM,IAAI,KAAC,WAAW,KAAG,IACpC,EACP,MAAC,IAAI,IAAC,SAAS,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,aACnC,cAAc,CAAC,KAAK,IAAI,KAAC,YAAY,IAAC,GAAG,EAAE,GAAG,GAAI,EAClD,cAAc,CAAC,MAAM,IAAI,KAAC,WAAW,KAAG,IACpC,EACN,cAAc,CAAC,gBAAgB,CAAC,IAAI,CACnC,KAAC,IAAI,IAAC,SAAS,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,YAAG,kBAAkB,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC,GAAQ,CACpF,IACI,CACR,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,OAAO,CAAC","sourcesContent":["import { useState, useEffect, useRef } from 'react';\nimport type { FC } from 'react';\nimport type { Text } from 'slate';\nimport { useSlate, useFocused, ReactEditor } from 'slate-react';\nimport styled, { css } from 'styled-components';\n\nimport {\n Flex,\n Icon,\n useArrows,\n useI18n,\n defaultThemeProp,\n navigatorIsAvailable\n} from '@pega/cosmos-react-core';\nimport type { TranslationPack, ExcludeStrict } from '@pega/cosmos-react-core';\nimport type { TranslationFunction } from '@pega/cosmos-react-core/lib/i18n/translate';\n\nimport EditorCommands from '../utils/EditorCommands';\nimport ListCommands from '../utils/ListCommands';\n\nimport ToolbarButton from './ToolbarButton';\nimport type { StyleType, Features } from './Toolbar.types';\nimport TableButton from './TableButton';\nimport AnchorButton from './AnchorButton';\nimport ImageButton from './ImageButton';\nimport TextSelect from './TextSelect';\nimport IndentButton from './IndentButton';\nimport { getKeyCommand } from './utils';\n\nconst StyledToolbar = styled.div<{ sticky?: boolean }>(({ sticky, theme }) => {\n return css`\n background-color: ${props => props.theme.components['form-control']['background-color']};\n border-radius: calc(0.25 * ${props => props.theme.base['border-radius']});\n overflow: auto;\n padding: 0.125rem;\n ${sticky &&\n css`\n position: sticky;\n bottom: 0;\n `}\n\n &:has(:focus-visible) {\n box-shadow: ${theme.base.shadow['focus-group']};\n }\n `;\n});\n\nStyledToolbar.defaultProps = defaultThemeProp;\n\nexport interface ToolbarProps {\n features: Features[];\n sticky?: boolean;\n setFocusableBtn: (el: HTMLElement) => void;\n}\n\ntype StyleButtonType = {\n style: StyleType;\n text: string;\n icon?: string;\n label?: string;\n format: ExcludeStrict<keyof Text, 'text'>;\n tooltip: string;\n};\n\nconst isMobile = navigatorIsAvailable && navigator.userAgent.includes('Mobile');\n\n// Function that returns the text styling buttons\nfunction renderStyleButtons(\n editor: ReactEditor,\n t: TranslationFunction<TranslationPack>,\n focused: 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 tooltip: isMobile\n ? `${t('rte_bold')}`\n : getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_bold')} (${ctrl}B, ${ctrl}${shift}B)`)\n },\n {\n style: 'ITALIC',\n text: 'I',\n format: 'italic',\n label: t('rte_italic'),\n tooltip: isMobile\n ? `${t('rte_italic')}`\n : getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_italic')} (${ctrl}I, ${ctrl}${shift}I)`)\n },\n {\n style: 'STRIKE-THROUGH',\n text: 'S',\n format: 'line-through',\n label: t('rte_strike_through'),\n tooltip: isMobile\n ? `${t('rte_strike_through')}`\n : getKeyCommand(osx, ({ ctrl, shift }) => `${t('rte_strike_through')} (${ctrl}${shift}X)`)\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 (!focused) {\n ReactEditor.focus(editor);\n }\n setTimeout(() => {\n EditorCommands.toggleFormat(format, editor);\n }, 0);\n }}\n styleType={style}\n active={EditorCommands.isFormatActive(format, editor)}\n tooltip={tooltip}\n label={label}\n >\n {text}\n </ToolbarButton>\n );\n });\n}\n\n// Function that returns the cut, copy and paste buttons\nfunction renderCutCopyPaste(\n editor: ReactEditor,\n t: TranslationFunction<TranslationPack>,\n osx: boolean\n) {\n const CutCopyPasteButtons = [\n {\n text: 'Cut',\n icon: 'scissors',\n label: t('rte_cut'),\n func: () => {\n document.execCommand('cut');\n },\n tooltip: getKeyCommand(osx, ({ ctrl }) => `${t('rte_cut')} (${ctrl}X)`)\n },\n {\n text: 'Copy',\n icon: 'copy',\n label: t('rte_copy'),\n func: () => {\n document.execCommand('copy');\n },\n tooltip: getKeyCommand(osx, ({ ctrl }) => `${t('rte_copy')} (${ctrl}C)`)\n },\n {\n text: 'Paste',\n icon: 'clipboard-data',\n label: t('rte_paste'),\n func: () => {\n navigator.clipboard.readText().then(text => {\n EditorCommands.replaceWithText(editor, editor.selection, text);\n });\n },\n tooltip: getKeyCommand(osx, ({ ctrl }) => `${t('rte_paste')} (${ctrl}V)`)\n }\n ];\n\n return CutCopyPasteButtons.map(btn => {\n return (\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n btn.func();\n }}\n key={btn.text}\n tooltip={btn.tooltip}\n label={btn.label}\n >\n <Icon name={btn.icon} />\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(\n (acc, feature) => {\n return { ...acc, [feature]: true };\n },\n {} as { [F in Features]: boolean }\n );\n}\n\nconst Toolbar: FC<ToolbarProps> = (props: ToolbarProps) => {\n const { features, sticky, setFocusableBtn } = props;\n const editor = useSlate();\n const focused = useFocused();\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\n useEffect(() => {\n const btn = toolbarRef?.current?.querySelector('button');\n if (btn) setFocusableBtn(btn);\n }, [toolbarRef.current]);\n\n useEffect(() => {\n setActiveFeatures(createActiveFeaturesMap(features));\n }, [features]);\n\n useEffect(() => {\n if (navigator.appVersion.includes('Mac')) setOsx(true);\n }, []);\n\n return (\n <Flex as={StyledToolbar} sticky={sticky} container={{ justify: 'between' }} ref={toolbarRef}>\n <Flex container={{ alignItems: 'end' }}>\n <TextSelect osx={osx} />\n {activeFeatures['inline-styling'] && renderStyleButtons(editor, t, focused, osx)}\n {activeFeatures.lists && (\n <>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n ListCommands.toggleList('unordered-list', editor);\n }}\n active={EditorCommands.isBlockActive('unordered-list', editor)}\n tooltip={\n isMobile\n ? `${t('rte_bulleted_list')}`\n : getKeyCommand(\n osx,\n ({ ctrl, shift }) => `${t('rte_bulleted_list')} (${ctrl}${shift}L)`\n )\n }\n label={t('rte_bulleted_list')}\n >\n <Icon name='list' />\n </ToolbarButton>\n <ToolbarButton\n onMouseDown={e => {\n e.preventDefault();\n ListCommands.toggleList('ordered-list', editor);\n }}\n active={EditorCommands.isBlockActive('ordered-list', editor)}\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 <IndentButton type='indent' osx={osx} />\n <IndentButton type='unindent' osx={osx} />\n </>\n )}\n {activeFeatures.tables && <TableButton />}\n </Flex>\n <Flex container={{ alignItems: 'end' }}>\n {activeFeatures.links && <AnchorButton osx={osx} />}\n {activeFeatures.images && <ImageButton />}\n </Flex>\n {activeFeatures['cut-copy-paste'] && (\n <Flex container={{ alignItems: 'end' }}>{renderCutCopyPaste(editor, t, osx)}</Flex>\n )}\n </Flex>\n );\n};\n\nexport default Toolbar;\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export type StyleType = 'BOLD' | 'ITALIC' | 'STRIKE-THROUGH' | 'HEADER-1' | 'HEADER-2';
|
|
2
|
-
export type StyleMapType<T =
|
|
1
|
+
export type StyleType = 'BOLD' | 'ITALIC' | 'UNDERLINE' | 'STRIKE-THROUGH' | 'SUBSCRIPT' | 'SUPERSCRIPT' | 'HEADER-1' | 'HEADER-2';
|
|
2
|
+
export type StyleMapType<T = unknown> = {
|
|
3
3
|
[S in StyleType]: T;
|
|
4
4
|
};
|
|
5
|
-
export type PartialStyleMapType<T =
|
|
5
|
+
export type PartialStyleMapType<T = unknown> = {
|
|
6
6
|
[S in StyleType]?: T;
|
|
7
7
|
};
|
|
8
|
-
export type Features = 'lists' | 'headers' | 'inline-styling' | 'images' | 'links' | 'tables' | 'cut-copy-paste' | 'indentation';
|
|
8
|
+
export type Features = 'lists' | 'headers' | 'inline-styling' | 'bold' | 'italic' | 'strikethrough' | 'underline' | 'subscript' | 'superscript' | 'font-family' | 'font-size' | 'text-color' | 'background-color' | 'alignment' | 'images' | 'links' | 'tables' | 'cut-copy-paste' | 'indentation' | 'source-code' | 'ai-rewrite' | 'word-count';
|
|
9
9
|
//# sourceMappingURL=Toolbar.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Toolbar.types.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/Toolbar.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"Toolbar.types.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/Toolbar.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,QAAQ,GACR,WAAW,GACX,gBAAgB,GAChB,WAAW,GACX,aAAa,GACb,UAAU,GACV,UAAU,CAAC;AACf,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,OAAO,IAAI;KAAG,CAAC,IAAI,SAAS,GAAG,CAAC;CAAE,CAAC;AAChE,MAAM,MAAM,mBAAmB,CAAC,CAAC,GAAG,OAAO,IAAI;KAAG,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,CAAC;CAAE,CAAC;AACxE,MAAM,MAAM,QAAQ,GAChB,OAAO,GACP,SAAS,GACT,gBAAgB,GAChB,MAAM,GACN,QAAQ,GACR,eAAe,GACf,WAAW,GACX,WAAW,GACX,aAAa,GACb,aAAa,GACb,WAAW,GACX,YAAY,GACZ,kBAAkB,GAClB,WAAW,GACX,QAAQ,GACR,OAAO,GACP,QAAQ,GACR,gBAAgB,GAChB,aAAa,GACb,aAAa,GACb,YAAY,GACZ,YAAY,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Toolbar.types.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/Toolbar.types.ts"],"names":[],"mappings":"","sourcesContent":["export type StyleType
|
|
1
|
+
{"version":3,"file":"Toolbar.types.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/Toolbar.types.ts"],"names":[],"mappings":"","sourcesContent":["export type StyleType =\n | 'BOLD'\n | 'ITALIC'\n | 'UNDERLINE'\n | 'STRIKE-THROUGH'\n | 'SUBSCRIPT'\n | 'SUPERSCRIPT'\n | 'HEADER-1'\n | 'HEADER-2';\nexport type StyleMapType<T = unknown> = { [S in StyleType]: T };\nexport type PartialStyleMapType<T = unknown> = { [S in StyleType]?: T };\nexport type Features =\n | 'lists'\n | 'headers'\n | 'inline-styling'\n | 'bold'\n | 'italic'\n | 'strikethrough'\n | 'underline'\n | 'subscript'\n | 'superscript'\n | 'font-family'\n | 'font-size'\n | 'text-color'\n | 'background-color'\n | 'alignment'\n | 'images'\n | 'links'\n | 'tables'\n | 'cut-copy-paste'\n | 'indentation'\n | 'source-code'\n | 'ai-rewrite'\n | 'word-count';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToolbarButton.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/ToolbarButton.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,aAAa,EAAE,UAAU,EAAO,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"ToolbarButton.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/ToolbarButton.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,aAAa,EAAE,UAAU,EAAO,MAAM,OAAO,CAAC;AAchE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAI5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,MAAM,WAAW,kBAAkB;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,CAAC,CAAC,EAAE,UAAU,GAAG,aAAa,KAAK,IAAI,CAAC;CACvD;AA8ED,QAAA,MAAM,aAAa,EAAE,EAAE,CAAC,kBAAkB,GAAG,YAAY,CAmCvD,CAAC;AAEH,eAAe,aAAa,CAAC"}
|
|
@@ -2,48 +2,30 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
2
2
|
import { forwardRef } from 'react';
|
|
3
3
|
import styled, { css } from 'styled-components';
|
|
4
4
|
import { mix } from 'polished';
|
|
5
|
-
import { defaultThemeProp, Tooltip, tryCatch, useConsolidatedRef, useElement, useI18n } from '@pega/cosmos-react-core';
|
|
5
|
+
import { Button, defaultThemeProp, Tooltip, tryCatch, useConsolidatedRef, useElement, useI18n, calculateFontSize } from '@pega/cosmos-react-core';
|
|
6
6
|
import { useRTEContext } from '../RichTextEditor.context';
|
|
7
|
-
const StyledToolbarButton = styled
|
|
8
|
-
const { active, theme, styleType } = props;
|
|
9
|
-
const size = `calc(
|
|
7
|
+
const StyledToolbarButton = styled(Button)(props => {
|
|
8
|
+
const { $active: active, theme, $styleType: styleType } = props;
|
|
9
|
+
const size = `calc(3.75 * ${props.theme.base.spacing})`;
|
|
10
|
+
const fontSizes = calculateFontSize(theme.base['font-size'], theme.base['font-scale']);
|
|
10
11
|
const activeColor = tryCatch(() => mix(0.85, theme.base.palette['primary-background'], theme.base.palette.interactive));
|
|
11
12
|
return css `
|
|
12
|
-
background-color: transparent;
|
|
13
|
-
border: none;
|
|
14
13
|
color: ${theme.base.palette['foreground-color']};
|
|
15
|
-
outline: none;
|
|
16
14
|
display: inline-flex;
|
|
17
15
|
line-height: 1;
|
|
18
16
|
align-items: center;
|
|
19
17
|
justify-content: center;
|
|
20
18
|
height: ${size};
|
|
21
19
|
min-width: ${size};
|
|
22
|
-
|
|
23
|
-
cursor: pointer;
|
|
20
|
+
padding: 0;
|
|
24
21
|
|
|
25
22
|
${active &&
|
|
26
23
|
css `
|
|
27
24
|
background-color: ${activeColor};
|
|
28
25
|
color: ${theme.base.palette.interactive};
|
|
29
|
-
|
|
30
|
-
box-shadow: inset 0 -0.125rem 0 0 ${theme.base.palette.interactive};
|
|
31
|
-
border-inline: 0.25rem solid transparent;
|
|
26
|
+
border-bottom: calc(0.25 * ${theme.base.spacing}) solid ${theme.base.palette.interactive};
|
|
32
27
|
`}
|
|
33
28
|
|
|
34
|
-
&:focus {
|
|
35
|
-
box-shadow: ${theme.base.shadow.focus};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
&:hover {
|
|
39
|
-
opacity: 1;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
&:disabled {
|
|
43
|
-
cursor: not-allowed;
|
|
44
|
-
box-shadow: none;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
29
|
${styleType === 'BOLD' &&
|
|
48
30
|
css `
|
|
49
31
|
font-weight: bold;
|
|
@@ -57,6 +39,39 @@ const StyledToolbarButton = styled.button(props => {
|
|
|
57
39
|
${styleType === 'STRIKE-THROUGH' &&
|
|
58
40
|
css `
|
|
59
41
|
text-decoration: line-through;
|
|
42
|
+
|
|
43
|
+
/* Override text-decoration: none hover style in StyledButton. */
|
|
44
|
+
&&:hover {
|
|
45
|
+
text-decoration: line-through;
|
|
46
|
+
}
|
|
47
|
+
`}
|
|
48
|
+
|
|
49
|
+
${styleType === 'UNDERLINE' &&
|
|
50
|
+
css `
|
|
51
|
+
text-decoration: underline;
|
|
52
|
+
|
|
53
|
+
/* Override text-decoration: none hover style in StyledButton. */
|
|
54
|
+
&&:hover {
|
|
55
|
+
text-decoration: underline;
|
|
56
|
+
}
|
|
57
|
+
`}
|
|
58
|
+
|
|
59
|
+
${styleType === 'SUBSCRIPT' &&
|
|
60
|
+
css `
|
|
61
|
+
sub {
|
|
62
|
+
font-size: ${fontSizes.xxs};
|
|
63
|
+
vertical-align: sub;
|
|
64
|
+
line-height: 1;
|
|
65
|
+
}
|
|
66
|
+
`}
|
|
67
|
+
|
|
68
|
+
${styleType === 'SUPERSCRIPT' &&
|
|
69
|
+
css `
|
|
70
|
+
sup {
|
|
71
|
+
font-size: ${fontSizes.xxs};
|
|
72
|
+
vertical-align: super;
|
|
73
|
+
line-height: 1;
|
|
74
|
+
}
|
|
60
75
|
`}
|
|
61
76
|
`;
|
|
62
77
|
});
|
|
@@ -66,7 +81,7 @@ const ToolbarButton = forwardRef(function ToolbarButton({ tooltip, active, style
|
|
|
66
81
|
const [el, setEl] = useElement();
|
|
67
82
|
const toolBarRef = useConsolidatedRef(setEl, ref);
|
|
68
83
|
const t = useI18n();
|
|
69
|
-
return (_jsxs(_Fragment, { children: [_jsx(StyledToolbarButton, { active: active, styleType: styleType, ref: toolBarRef, disabled: contextDisabled || disabled,
|
|
84
|
+
return (_jsxs(_Fragment, { children: [_jsx(StyledToolbarButton, { variant: 'simple', "$active": active, "$styleType": styleType, ref: toolBarRef, disabled: contextDisabled || disabled, "data-toolbar-item": 'true', onKeyDown: (e) => {
|
|
70
85
|
if (e.key === 'Enter') {
|
|
71
86
|
onMouseDown?.(e);
|
|
72
87
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToolbarButton.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/ToolbarButton.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EACL,gBAAgB,EAChB,OAAO,EACP,QAAQ,EACR,kBAAkB,EAClB,UAAU,EACV,OAAO,
|
|
1
|
+
{"version":3,"file":"ToolbarButton.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/Toolbar/ToolbarButton.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EACL,MAAM,EACN,gBAAgB,EAChB,OAAO,EACP,QAAQ,EACR,kBAAkB,EAClB,UAAU,EACV,OAAO,EACP,iBAAiB,EAClB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAa1D,MAAM,mBAAmB,GAAG,M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{ forwardRef } from 'react';\nimport type { FC, KeyboardEvent, MouseEvent, Ref } from 'react';\nimport styled, { css } from 'styled-components';\nimport { mix } from 'polished';\n\nimport {\n Button,\n defaultThemeProp,\n Tooltip,\n tryCatch,\n useConsolidatedRef,\n useElement,\n useI18n,\n calculateFontSize\n} from '@pega/cosmos-react-core';\nimport type { ForwardProps } from '@pega/cosmos-react-core';\n\nimport { useRTEContext } from '../RichTextEditor.context';\n\nimport type { StyleType } from './Toolbar.types';\n\nexport interface ToolBarButtonProps {\n tooltip?: string;\n active?: boolean;\n styleType?: StyleType;\n disabled?: boolean;\n label?: string;\n onMouseDown?: (e: MouseEvent | KeyboardEvent) => void;\n}\n\nconst StyledToolbarButton = styled(Button)<{ $active?: boolean; $styleType?: StyleType }>(props => {\n const { $active: active, theme, $styleType: styleType } = props;\n const size = `calc(3.75 * ${props.theme.base.spacing})`;\n const fontSizes = calculateFontSize(theme.base['font-size'], theme.base['font-scale']);\n const activeColor = tryCatch(() =>\n mix(0.85, theme.base.palette['primary-background'], theme.base.palette.interactive)\n );\n return css`\n color: ${theme.base.palette['foreground-color']};\n display: inline-flex;\n line-height: 1;\n align-items: center;\n justify-content: center;\n height: ${size};\n min-width: ${size};\n padding: 0;\n\n ${active &&\n css`\n background-color: ${activeColor};\n color: ${theme.base.palette.interactive};\n border-bottom: calc(0.25 * ${theme.base.spacing}) solid ${theme.base.palette.interactive};\n `}\n\n ${styleType === 'BOLD' &&\n css`\n font-weight: bold;\n `}\n\n ${styleType === 'ITALIC' &&\n css`\n font-style: italic;\n `}\n\n ${styleType === 'STRIKE-THROUGH' &&\n css`\n text-decoration: line-through;\n\n /* Override text-decoration: none hover style in StyledButton. */\n &&:hover {\n text-decoration: line-through;\n }\n `}\n\n ${styleType === 'UNDERLINE' &&\n css`\n text-decoration: underline;\n\n /* Override text-decoration: none hover style in StyledButton. */\n &&:hover {\n text-decoration: underline;\n }\n `}\n\n ${styleType === 'SUBSCRIPT' &&\n css`\n sub {\n font-size: ${fontSizes.xxs};\n vertical-align: sub;\n line-height: 1;\n }\n `}\n\n ${styleType === 'SUPERSCRIPT' &&\n css`\n sup {\n font-size: ${fontSizes.xxs};\n vertical-align: super;\n line-height: 1;\n }\n `}\n `;\n});\n\nStyledToolbarButton.defaultProps = defaultThemeProp;\n\nconst ToolbarButton: FC<ToolBarButtonProps & ForwardProps> = forwardRef(function ToolbarButton(\n { tooltip, active, styleType, disabled, label, onMouseDown, ...restProps },\n ref: Ref<HTMLButtonElement>\n) {\n const { disabled: contextDisabled } = useRTEContext();\n const [el, setEl] = useElement();\n const toolBarRef = useConsolidatedRef<HTMLButtonElement>(setEl, ref);\n const t = useI18n();\n return (\n <>\n <StyledToolbarButton\n variant='simple'\n $active={active}\n $styleType={styleType}\n ref={toolBarRef}\n disabled={contextDisabled || disabled}\n data-toolbar-item='true'\n onKeyDown={(e: KeyboardEvent<HTMLButtonElement>) => {\n if (e.key === 'Enter') {\n onMouseDown?.(e);\n }\n }}\n onMouseDown={onMouseDown}\n type='button'\n aria-label={`${label}${active ? ` - ${t('active')}` : ''}`}\n aria-pressed={active}\n {...restProps}\n />\n {el && tooltip && (\n <Tooltip target={el} showDelay='none' hideDelay='none'>\n {tooltip}\n </Tooltip>\n )}\n </>\n );\n});\n\nexport default ToolbarButton;\n"]}
|
|
@@ -11,7 +11,9 @@ export declare const htmlToSlateMap: {
|
|
|
11
11
|
readonly table: "table";
|
|
12
12
|
readonly tr: "table-row";
|
|
13
13
|
readonly td: "table-cell";
|
|
14
|
+
readonly th: "table-cell";
|
|
14
15
|
readonly tbody: "table-body";
|
|
16
|
+
readonly thead: "table-head";
|
|
15
17
|
readonly p: "paragraph";
|
|
16
18
|
readonly img: "image";
|
|
17
19
|
readonly b: "skip";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/htmlConverter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AAEpC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAYnE,eAAO,MAAM,cAAc
|
|
1
|
+
{"version":3,"file":"htmlConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/htmlConverter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AAEpC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAYnE,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;CAoBjB,CAAC;AAGX,eAAO,MAAM,oBAAoB;;;;;CAKvB,CAAC;AAwJX,eAAO,MAAM,WAAW,GACtB,MAAM,MAAM,EACZ,uBAAuB,mBAAmB,CAAC,sBAAsB,CAAC,KACjE,CAAC,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,EAe9B,CAAC"}
|
|
@@ -15,7 +15,9 @@ export const htmlToSlateMap = {
|
|
|
15
15
|
table: 'table',
|
|
16
16
|
tr: 'table-row',
|
|
17
17
|
td: 'table-cell',
|
|
18
|
+
th: 'table-cell',
|
|
18
19
|
tbody: 'table-body',
|
|
20
|
+
thead: 'table-head',
|
|
19
21
|
p: 'paragraph',
|
|
20
22
|
img: 'image',
|
|
21
23
|
b: 'skip',
|
|
@@ -60,6 +62,15 @@ const getAttributes = (node) => {
|
|
|
60
62
|
}
|
|
61
63
|
return attrsObj;
|
|
62
64
|
}
|
|
65
|
+
case 'td':
|
|
66
|
+
case 'th': {
|
|
67
|
+
const alignAttr = node.attrs.find(attr => attr.name === 'align');
|
|
68
|
+
const alignment = alignAttr?.value.toLowerCase();
|
|
69
|
+
if (alignment === 'left' || alignment === 'center' || alignment === 'right') {
|
|
70
|
+
return { align: alignment };
|
|
71
|
+
}
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
63
74
|
default:
|
|
64
75
|
return {};
|
|
65
76
|
}
|
|
@@ -111,6 +122,7 @@ const getSlateNode = (node, parentNode, interactionRenderers) => {
|
|
|
111
122
|
const nodeObj = {
|
|
112
123
|
type: type || 'paragraph',
|
|
113
124
|
...attributes,
|
|
125
|
+
...(node.nodeName === 'th' && { isHeaderCell: true }),
|
|
114
126
|
children: [{ text: '' }]
|
|
115
127
|
};
|
|
116
128
|
return { node: nodeObj };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/htmlConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAO/B,OAAO,2BAA2B,MAAM,uBAAuB,CAAC;AAEhE,MAAM,YAAY,GAAG,CAAC,IAAkC,EAA0B,EAAE,CAClF,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,OAAO,IAAI,IAAI,CAAC,QAAQ,KAAK,UAAU,CAAC;AACtE,MAAM,aAAa,GAAG,CAAC,IAAkC,EAA2B,EAAE,CACpF,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,OAAO,CAAC;AACtC,MAAM,YAAY,GAAG,CAAC,IAAkC,EAA2B,EAAE,CACnF,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,UAAU,CAAC;AAEzC,iHAAiH;AACjH,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,EAAE,EAAE,WAAW;IACf,EAAE,EAAE,WAAW;IACf,EAAE,EAAE,WAAW;IACf,EAAE,EAAE,WAAW;IACf,EAAE,EAAE,gBAAgB;IACpB,EAAE,EAAE,cAAc;IAClB,EAAE,EAAE,WAAW;IACf,KAAK,EAAE,OAAO;IACd,EAAE,EAAE,WAAW;IACf,EAAE,EAAE,YAAY;IAChB,KAAK,EAAE,YAAY;IACnB,CAAC,EAAE,WAAW;IACd,GAAG,EAAE,OAAO;IACZ,CAAC,EAAE,MAAM;IACT,GAAG,EAAE,MA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AAC,CAAC;QAC/B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAAE,OAAO,YAAY,CAAC;QAE7C,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAChC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAAE,OAAO,YAAY,CAAC;QAE7C,cAAc,GAAG,IAAI,CAAC,UAAU,CAAC;IACnC,CAAC;IACD,OAAO,YAAY,CAAC,cAAc,EAAE,SAAS,EAAE,oBAAoB,CAAC,CAAC;AACvE,CAAC,CAAC","sourcesContent":["import { parse } from 'parse5';\n// Use namespaced types to more easily differentiate between similar types (ie, ChildNode from parse5 and Node from slate).\nimport type * as parse5 from 'parse5';\nimport type * as slate from 'slate';\n\nimport type { RichTextViewerProps } from '../RichTextViewer.types';\n\nimport parseTextToCustomSlateNodes from './interactionRenderer';\n\nconst checkElement = (node: parse5.ChildNode | undefined): node is parse5.Element =>\n !!node && node.nodeName !== '#text' && node.nodeName !== '#comment';\nconst checkTextNode = (node: parse5.ChildNode | undefined): node is parse5.TextNode =>\n !!node && node.nodeName === '#text';\nconst checkComment = (node: parse5.ChildNode | undefined): node is parse5.TextNode =>\n !!node && node.nodeName === '#comment';\n\n// Mapping of HTML tags to slate types. b, del, i, and a tags are labeled as skip because they need special logic\nexport const htmlToSlateMap = {\n h1: 'heading-1',\n h2: 'heading-2',\n h3: 'heading-3',\n h4: 'heading-4',\n ul: 'unordered-list',\n ol: 'ordered-list',\n li: 'list-item',\n table: 'table',\n tr: 'table-row',\n td: 'table-cell',\n tbody: 'table-body',\n p: 'paragraph',\n img: 'image',\n b: 'skip',\n del: 'skip',\n i: 'skip',\n a: 'skip'\n} as const;\n\n// Inline HTML tags to slate types\nexport const inlineHtmlToStyleMap = {\n b: 'bold',\n del: 'line-through',\n i: 'italic',\n a: 'href'\n} as const;\n\n// Maps HTML Attributes to an object. Right now this is specifically used for list items.\n\ninterface InlineParentNodes extends slate.PegaCustom.CustomElement {\n href?: string;\n italic?: boolean;\n bold?: boolean;\n text?: string;\n}\n\nconst getAttributes = (node: parse5.ChildNode) => {\n if (checkElement(node) && node.attrs.length) {\n switch (node.nodeName) {\n case 'li':\n return Object.fromEntries(\n node.attrs.flatMap(attr => {\n // eslint-disable-next-line sonarjs/no-nested-switch\n switch (attr.name) {\n case 'data-id':\n return [['id', attr.value]];\n case 'data-parent-id':\n return [['parentId', attr.value]];\n case 'data-level':\n return [['level', attr.value]];\n case 'data-order':\n return [['order', attr.value]];\n default:\n return [];\n }\n })\n );\n case 'img': {\n const imgAttrs = ['src', 'alt'];\n const attrsObj: { [key: string]: string } = {};\n for (let i = 0; i < node.attrs.length; i += 1) {\n const { name, value } = node.attrs[i];\n if (imgAttrs.includes(name)) {\n attrsObj[name] = value;\n }\n }\n return attrsObj;\n }\n default:\n return {};\n }\n }\n return {};\n};\n\nconst getSlateNode = (\n node: parse5.ChildNode,\n parentNode?: slate.Element | slate.Text,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n): { node: slate.Element | slate.Text; collapseNext?: boolean } => {\n if (checkTextNode(node)) {\n if (node.parentNode.nodeName === 'body') {\n return {\n node: {\n type: 'paragraph',\n children: [{ text: node.value }]\n }\n };\n }\n // Make sure to spread any parent style attributes for slates text nodes\n const children = parseTextToCustomSlateNodes(\n node.value,\n interactionRenderers\n ) as slate.PegaCustom.CustomElement[];\n if (children.length === 1) {\n const tempParentNode = parentNode as InlineParentNodes;\n if (children[0]?.type === 'custom') {\n delete tempParentNode?.text;\n }\n\n return { node: { ...tempParentNode, ...children[0] } as slate.Text };\n }\n return {\n node: {\n ...parentNode,\n children\n } as slate.Element\n };\n }\n\n const type = htmlToSlateMap[node.nodeName as keyof typeof htmlToSlateMap];\n // If the type of the HTML tag is an inline (b, i, del, a) we want to let the recursive function know\n // that it needs to collapse the next node, also we are setting the correct style mark to true\n if (type === 'skip') {\n const style = inlineHtmlToStyleMap[node.nodeName as 'b' | 'i' | 'del' | 'a'];\n let value: string | boolean = true;\n if (node.nodeName === 'a') {\n const hrefObj = node.attrs?.find(attr => attr.name === 'href');\n value = hrefObj ? hrefObj.value : value;\n }\n return {\n node: { ...parentNode, [style]: value, text: '' } as slate.Text,\n collapseNext: true\n };\n }\n\n const attributes = getAttributes(node);\n const nodeObj = {\n type: type || 'paragraph',\n ...attributes,\n children: [{ text: '' }]\n } as slate.Element;\n return { node: nodeObj };\n};\n\n// Recursive function that will convert Parse5Nodes into Slate compatible nodes\nconst convertNodes = (\n nodes: parse5.ChildNode[],\n parentNode?: slate.Element | slate.Text,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n): (slate.Element | slate.Text)[] => {\n const returnNodes: (slate.Element | slate.Text)[] = [];\n nodes.forEach(node => {\n const { node: slateNode, collapseNext } = getSlateNode(node, parentNode, interactionRenderers);\n if (!checkComment(node)) {\n let returnNode = slateNode;\n if (checkElement(node) && node.childNodes && node.childNodes.length) {\n if (collapseNext) {\n returnNode = convertNodes(node.childNodes, slateNode, interactionRenderers)[0];\n } else {\n (slateNode as slate.Element).children = convertNodes(\n node.childNodes,\n undefined,\n interactionRenderers\n );\n returnNode = slateNode;\n }\n }\n\n returnNodes.push(returnNode);\n }\n });\n return returnNodes;\n};\n\nconst defaultValue = [{ type: 'paragraph', children: [{ text: '' }] }] satisfies slate.Element[];\n\nexport const convertHtml = (\n html: string,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n): (slate.Element | slate.Text)[] => {\n // Wrap the html in another body in case the value given has no body\n const doc = parse(`<body>${html.replace('\\n', '')}</body>`);\n let nodesToConvert = doc.childNodes;\n // If we have a root tag\n if (doc.childNodes.length === 1) {\n const root = doc.childNodes[0];\n if (!checkElement(root)) return defaultValue;\n\n const body = root.childNodes[1];\n if (!checkElement(body)) return defaultValue;\n\n nodesToConvert = body.childNodes;\n }\n return convertNodes(nodesToConvert, undefined, interactionRenderers);\n};\n"]}
|
|
1
|
+
{"version":3,"file":"htmlConverter.js","sourceRoot":"","sources":["../../../../src/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AAG,GAAG,KAAK,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC;IAC5D,IAAI,cAAc,GAAG,GAAG,CAAC,UAAU,CAAC;IACpC,wBAAwB;IACxB,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC/B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAAE,OAAO,YAAY,CAAC;QAE7C,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAChC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAAE,OAAO,YAAY,CAAC;QAE7C,cAAc,GAAG,IAAI,CAAC,UAAU,CAAC;IACnC,CAAC;IACD,OAAO,YAAY,CAAC,cAAc,EAAE,SAAS,EAAE,oBAAoB,CAAC,CAAC;AACvE,CAAC,CAAC","sourcesContent":["import { parse } from 'parse5';\n// Use namespaced types to more easily differentiate between similar types (ie, ChildNode from parse5 and Node from slate).\nimport type * as parse5 from 'parse5';\nimport type * as slate from 'slate';\n\nimport type { RichTextViewerProps } from '../RichTextViewer.types';\n\nimport parseTextToCustomSlateNodes from './interactionRenderer';\n\nconst checkElement = (node: parse5.ChildNode | undefined): node is parse5.Element =>\n !!node && node.nodeName !== '#text' && node.nodeName !== '#comment';\nconst checkTextNode = (node: parse5.ChildNode | undefined): node is parse5.TextNode =>\n !!node && node.nodeName === '#text';\nconst checkComment = (node: parse5.ChildNode | undefined): node is parse5.TextNode =>\n !!node && node.nodeName === '#comment';\n\n// Mapping of HTML tags to slate types. b, del, i, and a tags are labeled as skip because they need special logic\nexport const htmlToSlateMap = {\n h1: 'heading-1',\n h2: 'heading-2',\n h3: 'heading-3',\n h4: 'heading-4',\n ul: 'unordered-list',\n ol: 'ordered-list',\n li: 'list-item',\n table: 'table',\n tr: 'table-row',\n td: 'table-cell',\n th: 'table-cell',\n tbody: 'table-body',\n thead: 'table-head',\n p: 'paragraph',\n img: 'image',\n b: 'skip',\n del: 'skip',\n i: 'skip',\n a: 'skip'\n} as const;\n\n// Inline HTML tags to slate types\nexport const inlineHtmlToStyleMap = {\n b: 'bold',\n del: 'line-through',\n i: 'italic',\n a: 'href'\n} as const;\n\n// Maps HTML Attributes to an object. Right now this is specifically used for list items.\n\ninterface InlineParentNodes extends slate.PegaCustom.CustomElement {\n href?: string;\n italic?: boolean;\n bold?: boolean;\n text?: string;\n}\n\nconst getAttributes = (node: parse5.ChildNode) => {\n if (checkElement(node) && node.attrs.length) {\n switch (node.nodeName) {\n case 'li':\n return Object.fromEntries(\n node.attrs.flatMap(attr => {\n // eslint-disable-next-line sonarjs/no-nested-switch\n switch (attr.name) {\n case 'data-id':\n return [['id', attr.value]];\n case 'data-parent-id':\n return [['parentId', attr.value]];\n case 'data-level':\n return [['level', attr.value]];\n case 'data-order':\n return [['order', attr.value]];\n default:\n return [];\n }\n })\n );\n case 'img': {\n const imgAttrs = ['src', 'alt'];\n const attrsObj: { [key: string]: string } = {};\n for (let i = 0; i < node.attrs.length; i += 1) {\n const { name, value } = node.attrs[i];\n if (imgAttrs.includes(name)) {\n attrsObj[name] = value;\n }\n }\n return attrsObj;\n }\n case 'td':\n case 'th': {\n const alignAttr = node.attrs.find(attr => attr.name === 'align');\n const alignment = alignAttr?.value.toLowerCase();\n if (alignment === 'left' || alignment === 'center' || alignment === 'right') {\n return { align: alignment };\n }\n return {};\n }\n default:\n return {};\n }\n }\n return {};\n};\n\nconst getSlateNode = (\n node: parse5.ChildNode,\n parentNode?: slate.Element | slate.Text,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n): { node: slate.Element | slate.Text; collapseNext?: boolean } => {\n if (checkTextNode(node)) {\n if (node.parentNode.nodeName === 'body') {\n return {\n node: {\n type: 'paragraph',\n children: [{ text: node.value }]\n }\n };\n }\n // Make sure to spread any parent style attributes for slates text nodes\n const children = parseTextToCustomSlateNodes(\n node.value,\n interactionRenderers\n ) as slate.PegaCustom.CustomElement[];\n if (children.length === 1) {\n const tempParentNode = parentNode as InlineParentNodes;\n if (children[0]?.type === 'custom') {\n delete tempParentNode?.text;\n }\n\n return { node: { ...tempParentNode, ...children[0] } as slate.Text };\n }\n return {\n node: {\n ...parentNode,\n children\n } as slate.Element\n };\n }\n\n const type = htmlToSlateMap[node.nodeName as keyof typeof htmlToSlateMap];\n // If the type of the HTML tag is an inline (b, i, del, a) we want to let the recursive function know\n // that it needs to collapse the next node, also we are setting the correct style mark to true\n if (type === 'skip') {\n const style = inlineHtmlToStyleMap[node.nodeName as 'b' | 'i' | 'del' | 'a'];\n let value: string | boolean = true;\n if (node.nodeName === 'a') {\n const hrefObj = node.attrs?.find(attr => attr.name === 'href');\n value = hrefObj ? hrefObj.value : value;\n }\n return {\n node: { ...parentNode, [style]: value, text: '' } as slate.Text,\n collapseNext: true\n };\n }\n\n const attributes = getAttributes(node);\n const nodeObj = {\n type: type || 'paragraph',\n ...attributes,\n ...(node.nodeName === 'th' && { isHeaderCell: true }),\n children: [{ text: '' }]\n } as slate.Element;\n return { node: nodeObj };\n};\n\n// Recursive function that will convert Parse5Nodes into Slate compatible nodes\nconst convertNodes = (\n nodes: parse5.ChildNode[],\n parentNode?: slate.Element | slate.Text,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n): (slate.Element | slate.Text)[] => {\n const returnNodes: (slate.Element | slate.Text)[] = [];\n nodes.forEach(node => {\n const { node: slateNode, collapseNext } = getSlateNode(node, parentNode, interactionRenderers);\n if (!checkComment(node)) {\n let returnNode = slateNode;\n if (checkElement(node) && node.childNodes && node.childNodes.length) {\n if (collapseNext) {\n returnNode = convertNodes(node.childNodes, slateNode, interactionRenderers)[0];\n } else {\n (slateNode as slate.Element).children = convertNodes(\n node.childNodes,\n undefined,\n interactionRenderers\n );\n returnNode = slateNode;\n }\n }\n\n returnNodes.push(returnNode);\n }\n });\n return returnNodes;\n};\n\nconst defaultValue = [{ type: 'paragraph', children: [{ text: '' }] }] satisfies slate.Element[];\n\nexport const convertHtml = (\n html: string,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n): (slate.Element | slate.Text)[] => {\n // Wrap the html in another body in case the value given has no body\n const doc = parse(`<body>${html.replace('\\n', '')}</body>`);\n let nodesToConvert = doc.childNodes;\n // If we have a root tag\n if (doc.childNodes.length === 1) {\n const root = doc.childNodes[0];\n if (!checkElement(root)) return defaultValue;\n\n const body = root.childNodes[1];\n if (!checkElement(body)) return defaultValue;\n\n nodesToConvert = body.childNodes;\n }\n return convertNodes(nodesToConvert, undefined, interactionRenderers);\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interactionRenderer.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/interactionRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AAGpC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAEpE,UAAU,UAAW,SAAQ,oBAAoB;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,eAAe,CAAC;CACxB;AAED,UAAU,gBAAiB,SAAQ,oBAAoB;IACrD,IAAI,EAAE,MAAM,CAAC;CACd;AAKD,eAAO,MAAM,UAAU,GAAI,QAAQ,gBAAgB,iBAclD,CAAC;AAGF,eAAO,MAAM,gBAAgB,GAAI,SAAS,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAc3E,CAAC;AAGF,QAAA,MAAM,2BAA2B,GAC/B,MAAM,MAAM,EACZ,YAAY,oBAAoB,EAAE,KACjC,CAAC,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"interactionRenderer.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/interactionRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AAGpC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAEpE,UAAU,UAAW,SAAQ,oBAAoB;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,eAAe,CAAC;CACxB;AAED,UAAU,gBAAiB,SAAQ,oBAAoB;IACrD,IAAI,EAAE,MAAM,CAAC;CACd;AAKD,eAAO,MAAM,UAAU,GAAI,QAAQ,gBAAgB,iBAclD,CAAC;AAGF,eAAO,MAAM,gBAAgB,GAAI,SAAS,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAc3E,CAAC;AAGF,QAAA,MAAM,2BAA2B,GAC/B,MAAM,MAAM,EACZ,YAAY,oBAAoB,EAAE,KACjC,CAAC,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,EA4G9B,CAAC;AAEF,eAAe,2BAA2B,CAAC"}
|
|
@@ -41,10 +41,21 @@ const parseTextToCustomSlateNodes = (text, renderers) => {
|
|
|
41
41
|
}
|
|
42
42
|
// Sorts the matches based on the start positions of the matches
|
|
43
43
|
positions.sort((a, b) => a.start - b.start);
|
|
44
|
+
const validPositions = positions.filter(pos => {
|
|
45
|
+
if (pos.type === 'builtin-url' && pos.match.groups) {
|
|
46
|
+
const { url, protocol, tld } = pos.match.groups;
|
|
47
|
+
const normalizedUrl = protocol ? url : `https://${url}`;
|
|
48
|
+
return URL.canParse(normalizedUrl) && TLDs.includes(tld);
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
});
|
|
52
|
+
if (validPositions.length === 0) {
|
|
53
|
+
return [{ text }];
|
|
54
|
+
}
|
|
44
55
|
const nodes = [];
|
|
45
56
|
// Use the regex match positions to slice the text into the correct text nodes and other custom nodes
|
|
46
57
|
let lastPos;
|
|
47
|
-
|
|
58
|
+
validPositions.forEach((pos, i) => {
|
|
48
59
|
// If the first regex match isn't at the start of the text node,
|
|
49
60
|
// slice the text node up to the first match and insert it into the nodes array
|
|
50
61
|
if (i === 0 && pos.start !== 0) {
|
|
@@ -66,24 +77,14 @@ const parseTextToCustomSlateNodes = (text, renderers) => {
|
|
|
66
77
|
}
|
|
67
78
|
if (pos.type === 'builtin-url' && pos.match.groups) {
|
|
68
79
|
const { leadingChar, url } = pos.match.groups;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (!TLDs.includes(pos.match.groups.tld)) {
|
|
73
|
-
throw new Error('Invalid URL. Skipping formatting.');
|
|
74
|
-
}
|
|
75
|
-
if (leadingChar) {
|
|
76
|
-
nodes.push({ text: leadingChar });
|
|
77
|
-
}
|
|
78
|
-
nodes.push({
|
|
79
|
-
text: url,
|
|
80
|
-
href
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
// Invalid URL, just render as text.
|
|
85
|
-
nodes.push({ text: pos.match[0] });
|
|
80
|
+
const { href } = new URL(pos.match.groups.protocol ? url : `https://${url}`);
|
|
81
|
+
if (leadingChar) {
|
|
82
|
+
nodes.push({ text: leadingChar });
|
|
86
83
|
}
|
|
84
|
+
nodes.push({
|
|
85
|
+
text: url,
|
|
86
|
+
href
|
|
87
|
+
});
|
|
87
88
|
}
|
|
88
89
|
else if (pos.xmlElement) {
|
|
89
90
|
const xmlAttributes = getXMLAttributes(pos.match[0]);
|
|
@@ -119,7 +120,7 @@ const parseTextToCustomSlateNodes = (text, renderers) => {
|
|
|
119
120
|
}
|
|
120
121
|
// If we are at the end of the positions array, and there's still text left
|
|
121
122
|
// slice the text and add it as a new text node
|
|
122
|
-
if (i ===
|
|
123
|
+
if (i === validPositions.length - 1 && pos.end < text.length) {
|
|
123
124
|
nodes.push({
|
|
124
125
|
text: text.slice(pos.end, text.length)
|
|
125
126
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interactionRenderer.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/interactionRenderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAcvC,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;AAE/B,oEAAoE;AACpE,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAAwB,EAAE,EAAE;IACrD,MAAM,YAAY,GAAiB,EAAE,CAAC;IACtC,IAAI,GAAG,CAAC;IACR,0CAA0C;IAC1C,OAAO,CAAC,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QACrD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC;QACxB,YAAY,CAAC,IAAI,CAAC;YAChB,KAAK;YACL,GAAG,EAAE,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM;YAC1B,KAAK,EAAE,GAAG;YACV,GAAG,MAAM;SACV,CAAC,CAAC;IACL,CAAC;IACD,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,sEAAsE;AACtE,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,OAAe,EAAsC,EAAE;IACtF,MAAM,QAAQ,GAAG,MAAM,CAAC,eAAe,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IACpE,IAAI,QAAQ,CAAC,aAAa,CAAC,aAAa,CAAC;QAAE,OAAO,SAAS,CAAC;IAC5D,MAAM,UAAU,GAA2B,EAAE,CAAC;IAE9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,eAAe,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACvE,MAAM,IAAI,GAAG,QAAQ,CAAC,eAAe,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACzD,IAAI,IAAI,EAAE,CAAC;YACT,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC;QACrC,CAAC;IACH,CAAC;IACD,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AAEF,wCAAwC;AACxC,MAAM,2BAA2B,GAAG,CAClC,IAAY,EACZ,SAAkC,EACF,EAAE;IAClC,iEAAiE;IACjE,MAAM,SAAS,GACb,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAE1E,uCAAuC;IACvC,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IACpB,CAAC;IAED,gEAAgE;IAChE,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IAE5C,MAAM,KAAK,GAAmC,EAAE,CAAC;IAEjD,qGAAqG;IACrG,IAAI,OAAmB,CAAC;IACxB,
|
|
1
|
+
{"version":3,"file":"interactionRenderer.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/interactionRenderer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAcvC,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;AAE/B,oEAAoE;AACpE,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAAwB,EAAE,EAAE;IACrD,MAAM,YAAY,GAAiB,EAAE,CAAC;IACtC,IAAI,GAAG,CAAC;IACR,0CAA0C;IAC1C,OAAO,CAAC,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CA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AAC,MAAM,CAAC;aACvC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,GAAG,GAAG,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;AACnD,CAAC,CAAC;AAEF,eAAe,2BAA2B,CAAC","sourcesContent":["import type * as slate from 'slate';\nimport { TLDs } from 'global-tld-list';\n\nimport type { InteractionRenderers } from '../RichTextViewer.types';\n\ninterface RegexMatch extends InteractionRenderers {\n start: number;\n end: number;\n match: RegExpExecArray;\n}\n\ninterface CheckRegexParams extends InteractionRenderers {\n text: string;\n}\n\nconst parser = new DOMParser();\n\n// Checks a string for regex matches and returns an array of matches\nexport const checkRegex = (params: CheckRegexParams) => {\n const regexMatches: RegexMatch[] = [];\n let res;\n // eslint-disable-next-line no-cond-assign\n while ((res = params.regexPattern.exec(params.text))) {\n const start = res.index;\n regexMatches.push({\n start,\n end: start + res[0].length,\n match: res,\n ...params\n });\n }\n return regexMatches;\n};\n\n// Extracts all properties from an xml element represented as a string\nexport const getXMLAttributes = (element: string): Record<string, string> | undefined => {\n const document = parser.parseFromString(element, 'application/xml');\n if (document.querySelector('parsererror')) return undefined;\n const attributes: Record<string, string> = {};\n\n for (let i = 0; i < document.documentElement.attributes.length; i += 1) {\n const attr = document.documentElement.attributes.item(i);\n if (attr) {\n attributes[attr.name] = attr.value;\n }\n }\n attributes.rawText = element;\n\n return attributes;\n};\n\n// Parses text node to custom slate node\nconst parseTextToCustomSlateNodes = (\n text: string,\n renderers?: InteractionRenderers[]\n): (slate.Element | slate.Text)[] => {\n // Runs through an array of custom renderers and gets the matches\n const positions: RegexMatch[] =\n renderers?.flatMap(renderer => checkRegex({ text, ...renderer })) || [];\n\n // Early exit when no matched positions\n if (positions.length === 0) {\n return [{ text }];\n }\n\n // Sorts the matches based on the start positions of the matches\n positions.sort((a, b) => a.start - b.start);\n\n const validPositions = positions.filter(pos => {\n if (pos.type === 'builtin-url' && pos.match.groups) {\n const { url, protocol, tld } = pos.match.groups;\n const normalizedUrl = protocol ? url : `https://${url}`;\n return URL.canParse(normalizedUrl) && TLDs.includes(tld);\n }\n\n return true;\n });\n\n if (validPositions.length === 0) {\n return [{ text }];\n }\n\n const nodes: (slate.Element | slate.Text)[] = [];\n\n // Use the regex match positions to slice the text into the correct text nodes and other custom nodes\n let lastPos: RegexMatch;\n validPositions.forEach((pos, i) => {\n // If the first regex match isn't at the start of the text node,\n // slice the text node up to the first match and insert it into the nodes array\n\n if (i === 0 && pos.start !== 0) {\n nodes.push({\n text: text.slice(i, pos.start)\n });\n }\n\n // If there was a last position, slice the text between the last position\n // and the new position to insert it as a new text node\n if (lastPos && lastPos.end < pos.start) {\n nodes.push({\n text: text.slice(lastPos.end, pos.start)\n });\n } else if (i !== 0) {\n nodes.push({\n text: ' '\n });\n }\n\n if (pos.type === 'builtin-url' && pos.match.groups) {\n const { leadingChar, url } = pos.match.groups;\n\n const { href } = new URL(pos.match.groups.protocol ? url : `https://${url}`);\n\n if (leadingChar) {\n nodes.push({ text: leadingChar });\n }\n\n nodes.push({\n text: url,\n href\n });\n } else if (pos.xmlElement) {\n const xmlAttributes = getXMLAttributes(pos.match[0]);\n\n if (!xmlAttributes) {\n // Invalid XML, just render as text.\n nodes.push({ text: pos.match[0] });\n } else if (pos.component) {\n nodes.push({\n type: 'custom',\n component: pos.component,\n content: xmlAttributes,\n children: [{ text: '' }]\n });\n } else if (pos.getSearchAttributes) {\n const searchResult = pos.getSearchAttributes(pos.xmlElement, pos.match[0]);\n if (searchResult && searchResult.text) {\n nodes.push({\n text: searchResult.text,\n searchResult: {\n markdown: searchResult.markdown,\n component: searchResult.component,\n props: searchResult.props,\n toJSON: () => undefined\n }\n });\n } else {\n nodes.push({ text: pos.match[0] });\n }\n }\n }\n\n // If we are at the end of the positions array, and there's still text left\n // slice the text and add it as a new text node\n if (i === validPositions.length - 1 && pos.end < text.length) {\n nodes.push({\n text: text.slice(pos.end, text.length)\n });\n }\n lastPos = pos;\n });\n\n return nodes.length > 0 ? nodes : [{ text: '' }];\n};\n\nexport default parseTextToCustomSlateNodes;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/markdownConverter.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAEvD,OAAO,KAAK,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"markdownConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/markdownConverter.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAEvD,OAAO,KAAK,EAAE,WAAW,EAAU,MAAM,QAAQ,CAAC;AAKlD,OAAO,KAAK,EAAY,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAkKnG,eAAO,MAAM,OAAO,GAClB,OAAO,WAAW,EAClB,uBAAuB,oBAAoB,EAAE,EAC7C,cAAc,mBAAmB,CAAC,aAAa,CAAC,KAC/C,OAAO,GAAG,IAyLZ,CAAC;AAEF,eAAO,MAAM,wBAAwB,GACnC,WAAO,EACP,kBAAkB,oBAAoB,EAAE,KACvC,CAAC,OAAO,GAAG,IAAI,CAAC,EAOlB,CAAC;AAIF,QAAA,MAAM,eAAe,GACnB,IAAI,MAAM,EACV,uBAAuB,oBAAoB,EAAE,EAC7C,cAAc,mBAAmB,CAAC,aAAa,CAAC,KAC/C,CAAC,OAAO,GAAG,IAAI,CAAC,EAIlB,CAAC;AAEF,eAAe,eAAe,CAAC"}
|