@pega/cosmos-react-rte 2.0.0-dev.2.0 → 2.0.0-dev.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/RichTextEditor/DecoratorComponents/CodeBlock.d.ts.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/CodeBlock.js +3 -3
- package/lib/components/RichTextEditor/DecoratorComponents/CodeBlock.js.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Image.d.ts.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Image.js +2 -3
- package/lib/components/RichTextEditor/DecoratorComponents/Image.js.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Link.d.ts.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Link.js +2 -2
- package/lib/components/RichTextEditor/DecoratorComponents/Link.js.map +1 -1
- package/lib/components/RichTextEditor/DecoratorComponents/Table.d.ts +2 -0
- 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.context.d.ts +3 -3
- package/lib/components/RichTextEditor/RichTextEditor.context.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.context.js +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.context.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.js +103 -75
- package/lib/components/RichTextEditor/RichTextEditor.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts +3 -0
- package/lib/components/RichTextEditor/RichTextEditor.styles.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.styles.js +121 -51
- package/lib/components/RichTextEditor/RichTextEditor.styles.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextEditor.types.d.ts +41 -13
- 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 +21 -12
- package/lib/components/RichTextEditor/RichTextViewer.js.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.types.d.ts +3 -1
- package/lib/components/RichTextEditor/RichTextViewer.types.d.ts.map +1 -1
- package/lib/components/RichTextEditor/RichTextViewer.types.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/AnchorButton.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/AnchorButton.js +50 -59
- package/lib/components/RichTextEditor/Toolbar/AnchorButton.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ImageButton.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ImageButton.js +10 -16
- package/lib/components/RichTextEditor/Toolbar/ImageButton.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/IndentButton.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/IndentButton.js +6 -4
- package/lib/components/RichTextEditor/Toolbar/IndentButton.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/TableButton.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/TableButton.js +6 -5
- package/lib/components/RichTextEditor/Toolbar/TableButton.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/TextSelect.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/TextSelect.js +50 -35
- package/lib/components/RichTextEditor/Toolbar/TextSelect.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/Toolbar.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/Toolbar.js +18 -35
- package/lib/components/RichTextEditor/Toolbar/Toolbar.js.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.d.ts.map +1 -1
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.js +7 -8
- package/lib/components/RichTextEditor/Toolbar/ToolbarButton.js.map +1 -1
- package/lib/components/RichTextEditor/utils/EditorCommands.d.ts +9 -1
- package/lib/components/RichTextEditor/utils/EditorCommands.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/EditorCommands.js +130 -17
- package/lib/components/RichTextEditor/utils/EditorCommands.js.map +1 -1
- package/lib/components/RichTextEditor/utils/ListCommands.js +10 -14
- package/lib/components/RichTextEditor/utils/ListCommands.js.map +1 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.d.ts +2 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/htmlConverter.js +67 -28
- package/lib/components/RichTextEditor/utils/htmlConverter.js.map +1 -1
- package/lib/components/RichTextEditor/utils/interactionRenderer.d.ts +12 -0
- package/lib/components/RichTextEditor/utils/interactionRenderer.d.ts.map +1 -0
- package/lib/components/RichTextEditor/utils/interactionRenderer.js +105 -0
- package/lib/components/RichTextEditor/utils/interactionRenderer.js.map +1 -0
- package/lib/components/RichTextEditor/utils/markdownConverter.d.ts +2 -0
- package/lib/components/RichTextEditor/utils/markdownConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/markdownConverter.js +30 -94
- package/lib/components/RichTextEditor/utils/markdownConverter.js.map +1 -1
- package/lib/components/RichTextEditor/utils/renderers.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/renderers.js +26 -25
- package/lib/components/RichTextEditor/utils/renderers.js.map +1 -1
- package/lib/components/RichTextEditor/utils/search.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/search.js +7 -3
- package/lib/components/RichTextEditor/utils/search.js.map +1 -1
- package/lib/components/RichTextEditor/utils/slateConverter.d.ts.map +1 -1
- package/lib/components/RichTextEditor/utils/slateConverter.js +1 -0
- package/lib/components/RichTextEditor/utils/slateConverter.js.map +1 -1
- package/package.json +19 -17
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Checks a string for regex matches and returns an array of matches
|
|
2
|
+
const checkRegex = (params) => {
|
|
3
|
+
const regexMatches = [];
|
|
4
|
+
let res;
|
|
5
|
+
// eslint-disable-next-line no-cond-assign
|
|
6
|
+
while ((res = params.regexPattern.exec(params.text)) !== null) {
|
|
7
|
+
const start = res.index;
|
|
8
|
+
regexMatches.push({
|
|
9
|
+
start,
|
|
10
|
+
end: start + res[0].length,
|
|
11
|
+
match: res,
|
|
12
|
+
...params
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return regexMatches;
|
|
16
|
+
};
|
|
17
|
+
// Parses text node to custom slate node
|
|
18
|
+
const parseTextToCustomSlateNodes = (text, renderers) => {
|
|
19
|
+
// Runs through an array of custom renderers and gets the matches
|
|
20
|
+
const positions = renderers?.flatMap(renderer => checkRegex({ text, ...renderer })) || [];
|
|
21
|
+
// Early exit when no matched positions
|
|
22
|
+
if (positions.length === 0) {
|
|
23
|
+
return [{ text }];
|
|
24
|
+
}
|
|
25
|
+
// Sorts the matches based on the start positions of the matches
|
|
26
|
+
positions.sort((a, b) => a.start - b.start);
|
|
27
|
+
const nodes = [];
|
|
28
|
+
// Use the regex match positions to slice the text into the correct text nodes and other custom nodes
|
|
29
|
+
let lastPos;
|
|
30
|
+
positions.forEach((pos, i) => {
|
|
31
|
+
/* If the first regex match isn't at the start of the text node,
|
|
32
|
+
slice the text node up to the first match and insert it into the nodes array */
|
|
33
|
+
if (i === 0 && pos.start !== 0) {
|
|
34
|
+
nodes.push({
|
|
35
|
+
text: text.slice(i, pos.start)
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/* If there was a last position, slice the text between the last position
|
|
39
|
+
and the new position to insert it as a new text node */
|
|
40
|
+
if (lastPos && lastPos.end < pos.start) {
|
|
41
|
+
nodes.push({
|
|
42
|
+
text: text.slice(lastPos.end, pos.start)
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else if (i !== 0) {
|
|
46
|
+
nodes.push({
|
|
47
|
+
text: ' '
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (pos.type === 'builtin-url' && pos.match.groups) {
|
|
51
|
+
const { leadingChar, url, trailingChar } = pos.match.groups;
|
|
52
|
+
try {
|
|
53
|
+
// Check for valid URL since the regex isn't exhaustive.
|
|
54
|
+
const { href } = new URL(pos.match.groups.protocol ? url : `https://${url}`);
|
|
55
|
+
if (leadingChar) {
|
|
56
|
+
nodes.push({ text: leadingChar });
|
|
57
|
+
}
|
|
58
|
+
nodes.push({
|
|
59
|
+
text: url,
|
|
60
|
+
href
|
|
61
|
+
});
|
|
62
|
+
if (trailingChar) {
|
|
63
|
+
nodes.push({ text: trailingChar });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// Invalid URL, just render as text.
|
|
68
|
+
nodes.push({ text: pos.match[0] });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (pos.component) {
|
|
72
|
+
nodes.push({
|
|
73
|
+
type: 'custom',
|
|
74
|
+
component: pos.component,
|
|
75
|
+
content: pos.match,
|
|
76
|
+
children: [{ text: '' }]
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
else if (pos.getSearchAttributes) {
|
|
80
|
+
const searchResult = pos.getSearchAttributes(pos.match);
|
|
81
|
+
if (searchResult) {
|
|
82
|
+
nodes.push({
|
|
83
|
+
text: searchResult.text,
|
|
84
|
+
searchResult: {
|
|
85
|
+
markdown: searchResult.markdown,
|
|
86
|
+
component: searchResult.component,
|
|
87
|
+
props: searchResult.props,
|
|
88
|
+
toJSON: () => undefined
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/* If we are at the end of the positions array, and there's still text left
|
|
94
|
+
slice the text and add it as a new text node */
|
|
95
|
+
if (i === positions.length - 1 && pos.end < text.length - 1) {
|
|
96
|
+
nodes.push({
|
|
97
|
+
text: text.slice(pos.end, text.length)
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
lastPos = pos;
|
|
101
|
+
});
|
|
102
|
+
return nodes.length > 0 ? nodes : [{ text: '' }];
|
|
103
|
+
};
|
|
104
|
+
export default parseTextToCustomSlateNodes;
|
|
105
|
+
//# sourceMappingURL=interactionRenderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interactionRenderer.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/interactionRenderer.ts"],"names":[],"mappings":"AAqBA,oEAAoE;AACpE,MAAM,UAAU,GAAG,CAAC,MAAwB,EAAE,EAAE;IAC9C,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,KAAK,IAAI,EAAE;QAC7D,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;KACJ;IACD,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,wCAAwC;AACxC,MAAM,2BAA2B,GAAG,CAAC,IAAY,EAAE,SAAuB,EAAE,EAAE;IAC5E,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;QAC1B,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;KACnB;IAED,gEAAgE;IAChE,SAAS,CAAC,IAAI,CAAC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C;gBACT,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC;aACvC,CAAC,CAAC;SACJ;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';\n\nimport { SearchRenderers } from '../RichTextEditor.types';\nimport { InteractionRenderers } from '../RichTextViewer.types';\n\ninterface Renderers {\n regexPattern: InteractionRenderers['regexPattern'];\n type?: InteractionRenderers['type'];\n component?: InteractionRenderers['component'];\n getSearchAttributes?: SearchRenderers['getSearchAttributes'];\n}\ninterface RegexMatch extends Renderers {\n start: number;\n end: number;\n match: RegExpExecArray;\n}\n\ninterface CheckRegexParams extends Renderers {\n text: string;\n}\n\n// Checks a string for regex matches and returns an array of matches\nconst 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)) !== null) {\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// Parses text node to custom slate node\nconst parseTextToCustomSlateNodes = (text: string, renderers?: Renderers[]) => {\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 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 positions.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, trailingChar } = pos.match.groups;\n\n try {\n // Check for valid URL since the regex isn't exhaustive.\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\n if (trailingChar) {\n nodes.push({ text: trailingChar });\n }\n } catch {\n // Invalid URL, just render as text.\n nodes.push({ text: pos.match[0] });\n }\n } else if (pos.component) {\n nodes.push({\n type: 'custom',\n component: pos.component,\n content: pos.match,\n children: [{ text: '' }]\n });\n } else if (pos.getSearchAttributes) {\n const searchResult = pos.getSearchAttributes(pos.match);\n if (searchResult) {\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 }\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 === positions.length - 1 && pos.end < text.length - 1) {\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,5 +1,7 @@
|
|
|
1
1
|
import { Element, Text } from 'slate';
|
|
2
2
|
import { RichTextViewerProps } from '../RichTextViewer.types';
|
|
3
|
+
import { RichTextEditorProps } from '../RichTextEditor.types';
|
|
4
|
+
export declare const convertMarkdownForEditor: (md?: string, searchRenderers?: RichTextEditorProps['searchRenderers']) => (Element | Text)[];
|
|
3
5
|
declare const convertMarkdown: (md: string, interactionRenderers?: RichTextViewerProps['interactionRenderers'], markdownMap?: RichTextViewerProps['markdownMap']) => (Element | Text)[];
|
|
4
6
|
export default convertMarkdown;
|
|
5
7
|
//# sourceMappingURL=markdownConverter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/markdownConverter.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"markdownConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/markdownConverter.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAKpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAoI9D,eAAO,MAAM,wBAAwB,kCAEjB,mBAAmB,CAAC,iBAAiB,CAAC,KACvD,CAAC,OAAO,GAAG,IAAI,CAAC,EAOlB,CAAC;AAIF,QAAA,MAAM,eAAe,OACf,MAAM,yBACa,mBAAmB,CAAC,sBAAsB,CAAC,gBACpD,mBAAmB,CAAC,aAAa,CAAC,KAC/C,CAAC,OAAO,GAAG,IAAI,CAAC,EAIlB,CAAC;AAEF,eAAe,eAAe,CAAC"}
|
|
@@ -1,93 +1,9 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
1
2
|
import marked from 'marked';
|
|
2
|
-
import React from 'react';
|
|
3
3
|
import { hasProp } from '@pega/cosmos-react-core';
|
|
4
|
+
import parseTextToCustomSlateNodes from './interactionRenderer';
|
|
4
5
|
marked.setOptions({ mangle: false });
|
|
5
|
-
// Checks a string for regex matches and returns an array of matches
|
|
6
|
-
const checkRegex = (params) => {
|
|
7
|
-
const regexMatches = [];
|
|
8
|
-
let res;
|
|
9
|
-
// eslint-disable-next-line no-cond-assign
|
|
10
|
-
while ((res = params.regexPattern.exec(params.text)) !== null) {
|
|
11
|
-
const start = res.index;
|
|
12
|
-
regexMatches.push({
|
|
13
|
-
start,
|
|
14
|
-
end: start + res[0].length,
|
|
15
|
-
match: res,
|
|
16
|
-
...params
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
return regexMatches;
|
|
20
|
-
};
|
|
21
|
-
// Parses MD for custom MD
|
|
22
|
-
const parseTextToCustomMd = (text, interactionRenderers) => {
|
|
23
|
-
let positions = [];
|
|
24
|
-
// Runs through an array of interactionRenderers and gets the matches
|
|
25
|
-
interactionRenderers === null || interactionRenderers === void 0 ? void 0 : interactionRenderers.forEach(interaction => {
|
|
26
|
-
const regexPositions = checkRegex({ text, ...interaction });
|
|
27
|
-
positions = [...positions, ...regexPositions];
|
|
28
|
-
});
|
|
29
|
-
// Sorts the matches based on the start positions of the matches
|
|
30
|
-
positions.sort((a, b) => {
|
|
31
|
-
if (a.start < b.start) {
|
|
32
|
-
return -1;
|
|
33
|
-
}
|
|
34
|
-
return 1;
|
|
35
|
-
});
|
|
36
|
-
const nodes = [];
|
|
37
|
-
if (positions.length) {
|
|
38
|
-
// Use the regex match positions to slice the text into the correct text nodes and other custom nodes
|
|
39
|
-
let lastPos;
|
|
40
|
-
positions.forEach((pos, i) => {
|
|
41
|
-
/* If the first regex match isn't at the start of the text node,
|
|
42
|
-
slice the text node up to the first match and insert it into the nodes array */
|
|
43
|
-
if (i === 0) {
|
|
44
|
-
if (pos.start !== 0) {
|
|
45
|
-
nodes.push({
|
|
46
|
-
text: text.slice(0, pos.start)
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/* If there was a last position, slice the text between the last position
|
|
51
|
-
and the new position to insert it as a new text node */
|
|
52
|
-
if (lastPos && lastPos.end < pos.start) {
|
|
53
|
-
nodes.push({
|
|
54
|
-
text: text.slice(lastPos.end, pos.start)
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
else if (i !== 0) {
|
|
58
|
-
nodes.push({
|
|
59
|
-
text: ' '
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
if (pos.type) {
|
|
63
|
-
nodes.push({
|
|
64
|
-
type: 'custom',
|
|
65
|
-
component: pos.component,
|
|
66
|
-
content: pos.match,
|
|
67
|
-
children: [{ text: '' }]
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
/* If we are at the end of the positions array, and there's still text left
|
|
71
|
-
slice the text and add it as a new text node */
|
|
72
|
-
if (i === positions.length - 1) {
|
|
73
|
-
if (pos.end < text.length - 1) {
|
|
74
|
-
nodes.push({
|
|
75
|
-
text: text.slice(pos.end, text.length)
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
lastPos = pos;
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
nodes.push({
|
|
84
|
-
text
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
return nodes.length > 0 ? nodes : [{ text: '' }];
|
|
88
|
-
};
|
|
89
6
|
const getNode = (token, interactionRenderers, markdownMap) => {
|
|
90
|
-
var _a;
|
|
91
7
|
if (!hasProp(token, 'type') || token.type === 'def')
|
|
92
8
|
return { type: 'paragraph', children: [{ text: '' }] };
|
|
93
9
|
let children;
|
|
@@ -105,18 +21,24 @@ const getNode = (token, interactionRenderers, markdownMap) => {
|
|
|
105
21
|
mdText = token.raw;
|
|
106
22
|
else if (hasProp(token, 'text'))
|
|
107
23
|
mdText = token.text;
|
|
108
|
-
children =
|
|
24
|
+
children = parseTextToCustomSlateNodes(mdText, interactionRenderers);
|
|
109
25
|
}
|
|
110
|
-
if (markdownMap
|
|
26
|
+
if (markdownMap?.[token.type]) {
|
|
111
27
|
// Typed as any since typescript is unaware that token type matches argument
|
|
112
|
-
const CustomComponent =
|
|
28
|
+
const CustomComponent = markdownMap[token.type]?.(token);
|
|
113
29
|
if (CustomComponent)
|
|
114
30
|
return {
|
|
115
31
|
type: 'override',
|
|
116
|
-
component:
|
|
32
|
+
component: _jsx(CustomComponent, { token: token }, void 0),
|
|
117
33
|
children: [{ text: '' }]
|
|
118
34
|
};
|
|
119
35
|
}
|
|
36
|
+
if (!children.length) {
|
|
37
|
+
return {
|
|
38
|
+
type: 'paragraph',
|
|
39
|
+
children: [{ text: token.raw }]
|
|
40
|
+
};
|
|
41
|
+
}
|
|
120
42
|
switch (token.type) {
|
|
121
43
|
case 'paragraph': {
|
|
122
44
|
return {
|
|
@@ -168,6 +90,14 @@ const getNode = (token, interactionRenderers, markdownMap) => {
|
|
|
168
90
|
children: children
|
|
169
91
|
};
|
|
170
92
|
}
|
|
93
|
+
case 'image': {
|
|
94
|
+
return {
|
|
95
|
+
type: 'image',
|
|
96
|
+
alt: token.text,
|
|
97
|
+
src: token.href,
|
|
98
|
+
children: [{ text: '' }]
|
|
99
|
+
};
|
|
100
|
+
}
|
|
171
101
|
default: {
|
|
172
102
|
return {
|
|
173
103
|
type: token.type,
|
|
@@ -177,18 +107,24 @@ const getNode = (token, interactionRenderers, markdownMap) => {
|
|
|
177
107
|
}
|
|
178
108
|
};
|
|
179
109
|
const markDownToSlate = (tokens, interactionRenderers, markdownMap) => {
|
|
180
|
-
|
|
110
|
+
return tokens.map(token => {
|
|
181
111
|
return getNode(token, interactionRenderers, markdownMap);
|
|
182
112
|
});
|
|
183
|
-
|
|
113
|
+
};
|
|
114
|
+
export const convertMarkdownForEditor = (md = '', searchRenderers) => {
|
|
115
|
+
return [
|
|
116
|
+
{
|
|
117
|
+
type: 'paragraph',
|
|
118
|
+
children: parseTextToCustomSlateNodes(md, searchRenderers)
|
|
119
|
+
}
|
|
120
|
+
];
|
|
184
121
|
};
|
|
185
122
|
/* Converts markdown into Slate compatible nodes, also takes regex used
|
|
186
123
|
in Feed Component to pass them down to custom elements */
|
|
187
124
|
const convertMarkdown = (md, interactionRenderers, markdownMap) => {
|
|
188
125
|
const lexer = new marked.Lexer({ gfm: false });
|
|
189
126
|
const output = lexer.lex(md);
|
|
190
|
-
|
|
191
|
-
return nodes;
|
|
127
|
+
return markDownToSlate(output, interactionRenderers, markdownMap);
|
|
192
128
|
};
|
|
193
129
|
export default convertMarkdown;
|
|
194
130
|
//# sourceMappingURL=markdownConverter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/markdownConverter.tsx"],"names":[],"mappings":"AACA,OAAO,MAAyB,MAAM,QAAQ,CAAC;AAC/C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAIlD,MAAM,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;AAYrC,oEAAoE;AACpE,MAAM,UAAU,GAAG,CAAC,MAAwB,EAAE,EAAE;IAC9C,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,KAAK,IAAI,EAAE;QAC7D,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;KACJ;IACD,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,0BAA0B;AAC1B,MAAM,mBAAmB,GAAG,CAC1B,IAAY,EACZ,oBAAkE,EAClE,EAAE;IACF,IAAI,SAAS,GAAiB,EAAE,CAAC;IACjC,qEAAqE;IACrE,oB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E,WAAW,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF;yDACyD;AACzD,MAAM,eAAe,GAAG,CACtB,EAAU,EACV,oBAAkE,EAClE,WAAgD,EAC5B,EAAE;IACtB,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC7B,MAAM,KAAK,GAAG,eAAe,CAAC,MAAM,EAAE,oBAAoB,EAAE,WAAW,CAAC,CAAC;IACzE,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,eAAe,eAAe,CAAC","sourcesContent":["import { CosmosCustom, Element, Text } from 'slate';\nimport marked, { Token, Tokens } from 'marked';\nimport React from 'react';\n\nimport { hasProp } from '@pega/cosmos-react-core';\n\nimport { InteractionRenderers, RichTextViewerProps } from '../RichTextViewer.types';\n\nmarked.setOptions({ mangle: false });\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\n// Checks a string for regex matches and returns an array of matches\nconst 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)) !== null) {\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// Parses MD for custom MD\nconst parseTextToCustomMd = (\n text: string,\n interactionRenderers?: RichTextViewerProps['interactionRenderers']\n) => {\n let positions: RegexMatch[] = [];\n // Runs through an array of interactionRenderers and gets the matches\n interactionRenderers?.forEach(interaction => {\n const regexPositions = checkRegex({ text, ...interaction });\n positions = [...positions, ...regexPositions];\n });\n // Sorts the matches based on the start positions of the matches\n positions.sort((a, b) => {\n if (a.start < b.start) {\n return -1;\n }\n return 1;\n });\n\n const nodes: (Element | Text)[] = [];\n\n if (positions.length) {\n // Use the regex match positions to slice the text into the correct text nodes and other custom nodes\n let lastPos: RegexMatch;\n positions.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 if (i === 0) {\n if (pos.start !== 0) {\n nodes.push({\n text: text.slice(0, pos.start)\n });\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) {\n nodes.push({\n type: 'custom',\n component: pos.component,\n content: pos.match,\n children: [{ text: '' }]\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 === positions.length - 1) {\n if (pos.end < text.length - 1) {\n nodes.push({\n text: text.slice(pos.end, text.length)\n });\n }\n }\n lastPos = pos;\n });\n } else {\n nodes.push({\n text\n });\n }\n\n return nodes.length > 0 ? nodes : [{ text: '' }];\n};\n\nconst getNode = (\n token: Token,\n interactionRenderers?: RichTextViewerProps['interactionRenderers'],\n markdownMap?: RichTextViewerProps['markdownMap']\n): Element | Text => {\n if (!hasProp(token, 'type') || token.type === 'def')\n return { type: 'paragraph', children: [{ text: '' }] };\n\n let children;\n\n if (hasProp(token, 'tokens')) {\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n children = markDownToSlate(\n (token.tokens as Tokens.Link['tokens']) || [],\n interactionRenderers,\n markdownMap\n );\n } else if (hasProp(token, 'items')) {\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n children = markDownToSlate(token.items, interactionRenderers, markdownMap);\n } else {\n let mdText = '';\n\n if (token.type === 'link' || token.type === 'text') mdText = token.raw;\n else if (hasProp(token, 'text')) mdText = token.text;\n\n children = parseTextToCustomMd(mdText, interactionRenderers);\n }\n\n if (markdownMap?.[token.type]) {\n // Typed as any since typescript is unaware that token type matches argument\n const CustomComponent: any = markdownMap[token.type]?.(token as any);\n if (CustomComponent)\n return {\n type: 'override',\n component: <CustomComponent token={token} />,\n children: [{ text: '' }]\n };\n }\n\n switch (token.type) {\n case 'paragraph': {\n return {\n type: token.type,\n children\n };\n }\n case 'heading': {\n return {\n type:\n token.depth && token.depth <= 4\n ? (`heading-${token.depth}` as `heading-${1 | 2 | 3 | 4}`)\n : 'heading-4',\n children\n };\n }\n case 'link': {\n return {\n href: token.href,\n text: token.text || ''\n };\n }\n case 'em': {\n return {\n italic: true,\n text: token.text || ''\n };\n }\n case 'strong': {\n return {\n bold: true,\n text: token.text || ''\n };\n }\n case 'del': {\n return {\n 'line-through': true,\n text: token.text || ''\n };\n }\n case 'list': {\n return {\n type: token.ordered ? 'ordered-list' : 'unordered-list',\n children: children as CosmosCustom.ListItemElement[]\n };\n }\n case 'list_item': {\n return {\n type: 'list-item',\n children: children as Element[]\n };\n }\n default: {\n return {\n type: token.type,\n children\n } as Element;\n }\n }\n};\n\nconst markDownToSlate = (\n tokens: Token[],\n interactionRenderers?: RichTextViewerProps['interactionRenderers'],\n markdownMap?: RichTextViewerProps['markdownMap']\n): (Element | Text)[] => {\n const nodes = tokens.map(token => {\n return getNode(token, interactionRenderers, markdownMap);\n });\n\n return nodes;\n};\n\n/* Converts markdown into Slate compatible nodes, also takes regex used\nin Feed Component to pass them down to custom elements */\nconst convertMarkdown = (\n md: string,\n interactionRenderers?: RichTextViewerProps['interactionRenderers'],\n markdownMap?: RichTextViewerProps['markdownMap']\n): (Element | Text)[] => {\n const lexer = new marked.Lexer({ gfm: false });\n const output = lexer.lex(md);\n const nodes = markDownToSlate(output, interactionRenderers, markdownMap);\n return nodes;\n};\n\nexport default convertMarkdown;\n"]}
|
|
1
|
+
{"version":3,"file":"markdownConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/markdownConverter.tsx"],"names":[],"mappings":";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AAM,eAAe,GAAG,CACtB,EAAU,EACV,oBAAkE,EAClE,WAAgD,EAC5B,EAAE;IACtB,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC7B,OAAO,eAAe,CAAC,MAAM,EAAE,oBAAoB,EAAE,WAAW,CAAC,CAAC;AACpE,CAAC,CAAC;AAEF,eAAe,eAAe,CAAC","sourcesContent":["import { CosmosCustom, Element, Text } from 'slate';\nimport marked, { Token, Tokens } from 'marked';\n\nimport { hasProp } from '@pega/cosmos-react-core';\n\nimport { RichTextViewerProps } from '../RichTextViewer.types';\nimport { RichTextEditorProps } from '../RichTextEditor.types';\n\nimport parseTextToCustomSlateNodes from './interactionRenderer';\n\nmarked.setOptions({ mangle: false });\n\nconst getNode = (\n token: Token,\n interactionRenderers?: RichTextViewerProps['interactionRenderers'],\n markdownMap?: RichTextViewerProps['markdownMap']\n): Element | Text => {\n if (!hasProp(token, 'type') || token.type === 'def')\n return { type: 'paragraph', children: [{ text: '' }] };\n\n let children;\n\n if (hasProp(token, 'tokens')) {\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n children = markDownToSlate(\n (token.tokens as Tokens.Link['tokens']) || [],\n interactionRenderers,\n markdownMap\n );\n } else if (hasProp(token, 'items')) {\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n children = markDownToSlate(token.items, interactionRenderers, markdownMap);\n } else {\n let mdText = '';\n\n if (token.type === 'link' || token.type === 'text') mdText = token.raw;\n else if (hasProp(token, 'text')) mdText = token.text;\n\n children = parseTextToCustomSlateNodes(mdText, interactionRenderers);\n }\n\n if (markdownMap?.[token.type]) {\n // Typed as any since typescript is unaware that token type matches argument\n const CustomComponent: any = markdownMap[token.type]?.(token as any);\n if (CustomComponent)\n return {\n type: 'override',\n component: <CustomComponent token={token} />,\n children: [{ text: '' }]\n };\n }\n\n if (!children.length) {\n return {\n type: 'paragraph',\n children: [{ text: token.raw }]\n };\n }\n\n switch (token.type) {\n case 'paragraph': {\n return {\n type: token.type,\n children\n };\n }\n case 'heading': {\n return {\n type:\n token.depth && token.depth <= 4\n ? (`heading-${token.depth}` as `heading-${1 | 2 | 3 | 4}`)\n : 'heading-4',\n children\n };\n }\n case 'link': {\n return {\n href: token.href,\n text: token.text || ''\n };\n }\n case 'em': {\n return {\n italic: true,\n text: token.text || ''\n };\n }\n case 'strong': {\n return {\n bold: true,\n text: token.text || ''\n };\n }\n case 'del': {\n return {\n 'line-through': true,\n text: token.text || ''\n };\n }\n case 'list': {\n return {\n type: token.ordered ? 'ordered-list' : 'unordered-list',\n children: children as CosmosCustom.ListItemElement[]\n };\n }\n case 'list_item': {\n return {\n type: 'list-item',\n children: children as Element[]\n };\n }\n case 'image': {\n return {\n type: 'image',\n alt: token.text,\n src: token.href,\n children: [{ text: '' }]\n };\n }\n default: {\n return {\n type: token.type,\n children\n } as Element;\n }\n }\n};\n\nconst markDownToSlate = (\n tokens: Token[],\n interactionRenderers?: RichTextViewerProps['interactionRenderers'],\n markdownMap?: RichTextViewerProps['markdownMap']\n): (Element | Text)[] => {\n return tokens.map(token => {\n return getNode(token, interactionRenderers, markdownMap);\n });\n};\n\nexport const convertMarkdownForEditor = (\n md = '',\n searchRenderers?: RichTextEditorProps['searchRenderers']\n): (Element | Text)[] => {\n return [\n {\n type: 'paragraph',\n children: parseTextToCustomSlateNodes(md, searchRenderers)\n }\n ];\n};\n\n/* Converts markdown into Slate compatible nodes, also takes regex used\nin Feed Component to pass them down to custom elements */\nconst convertMarkdown = (\n md: string,\n interactionRenderers?: RichTextViewerProps['interactionRenderers'],\n markdownMap?: RichTextViewerProps['markdownMap']\n): (Element | Text)[] => {\n const lexer = new marked.Lexer({ gfm: false });\n const output = lexer.lex(md);\n return markDownToSlate(output, interactionRenderers, markdownMap);\n};\n\nexport default convertMarkdown;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderers.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/renderers.tsx"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"renderers.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/renderers.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAOlE,eAAO,MAAM,YAAY,UAAW,kBAAkB,gBA4ErD,CAAC;AAGF,eAAO,MAAM,YAAY,UAAW,eAAe,gBAiClD,CAAC"}
|
|
@@ -1,67 +1,68 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Text, Link, Image } from '@pega/cosmos-react-core';
|
|
3
3
|
import { Table, TableRow, TableCell, TableBody } from '../DecoratorComponents/Table';
|
|
4
4
|
// Renderer for block level elements
|
|
5
5
|
export const nodeRenderer = (props) => {
|
|
6
6
|
switch (props.element.type) {
|
|
7
7
|
case 'heading-1':
|
|
8
|
-
return (
|
|
8
|
+
return (_jsx(Text, { variant: 'h1', ...props.attributes, children: props.children }, void 0));
|
|
9
9
|
case 'heading-2':
|
|
10
|
-
return (
|
|
10
|
+
return (_jsx(Text, { variant: 'h2', ...props.attributes, children: props.children }, void 0));
|
|
11
11
|
case 'heading-3':
|
|
12
|
-
return (
|
|
12
|
+
return (_jsx(Text, { variant: 'h3', ...props.attributes, children: props.children }, void 0));
|
|
13
13
|
case 'heading-4':
|
|
14
|
-
return (
|
|
14
|
+
return (_jsx(Text, { variant: 'h4', ...props.attributes, children: props.children }, void 0));
|
|
15
15
|
case 'ordered-list':
|
|
16
|
-
return
|
|
16
|
+
return _jsx("ol", { ...props.attributes, children: props.children }, void 0);
|
|
17
17
|
case 'unordered-list':
|
|
18
|
-
return
|
|
18
|
+
return _jsx("ul", { ...props.attributes, children: props.children }, void 0);
|
|
19
19
|
case 'list-item':
|
|
20
|
-
return (
|
|
20
|
+
return (_jsx("li", { ...props.attributes, "data-order": props.element.order, "data-level": props.element.level, "data-id": props.element.id, "data-parent-id": props.element.parentId, children: props.children }, void 0));
|
|
21
21
|
case 'custom': {
|
|
22
22
|
const CustomComponent = props.element.component;
|
|
23
|
-
return
|
|
23
|
+
return _jsx(CustomComponent, { ...props.attributes, children: props.element.content }, void 0);
|
|
24
24
|
}
|
|
25
25
|
case 'override': {
|
|
26
26
|
return props.element.component;
|
|
27
27
|
}
|
|
28
28
|
case 'table':
|
|
29
|
-
return
|
|
29
|
+
return _jsx(Table, { ...props.attributes, children: props.children }, void 0);
|
|
30
30
|
case 'table-body':
|
|
31
|
-
return
|
|
31
|
+
return _jsx(TableBody, { ...props.attributes, children: props.children }, void 0);
|
|
32
32
|
case 'table-row':
|
|
33
|
-
return
|
|
33
|
+
return _jsx(TableRow, { ...props.attributes, children: props.children }, void 0);
|
|
34
34
|
case 'table-cell':
|
|
35
|
-
return
|
|
35
|
+
return _jsx(TableCell, { ...props.attributes, children: props.children }, void 0);
|
|
36
36
|
case 'paragraph':
|
|
37
|
-
return (
|
|
38
|
-
React.createElement(Text, { ...props.attributes }, props.children)));
|
|
37
|
+
return (_jsx("div", { children: _jsx(Text, { ...props.attributes, children: props.children }, void 0) }, void 0));
|
|
39
38
|
case 'image':
|
|
40
|
-
return (
|
|
41
|
-
React.createElement(Image, { src: props.element.src, alt: props.element.alt }),
|
|
42
|
-
props.children));
|
|
39
|
+
return (_jsxs("div", { ...props.attributes, children: [_jsx(Image, { src: props.element.src, alt: props.element.alt }, void 0), props.children] }, void 0));
|
|
43
40
|
case 'image-placeholder':
|
|
44
|
-
return
|
|
41
|
+
return _jsx("div", { ...props.attributes, children: props.children }, void 0);
|
|
45
42
|
default: {
|
|
46
|
-
return
|
|
43
|
+
return _jsx("span", { ...props.attributes, children: props.children }, void 0);
|
|
47
44
|
}
|
|
48
45
|
}
|
|
49
46
|
};
|
|
50
47
|
// Renderer for inline elements
|
|
51
48
|
export const leafRenderer = (props) => {
|
|
52
49
|
let child = props.children;
|
|
50
|
+
if (props.leaf.searchResult) {
|
|
51
|
+
const CustomComponent = props.leaf.searchResult.component;
|
|
52
|
+
return (_jsx("span", { ...props.attributes, children: _jsx(CustomComponent, { ...props.leaf.searchResult.props, children: child }, void 0) }, void 0));
|
|
53
|
+
}
|
|
53
54
|
if (props.leaf['line-through']) {
|
|
54
|
-
child =
|
|
55
|
+
child = _jsx("del", { children: child }, void 0);
|
|
55
56
|
}
|
|
56
57
|
if (props.leaf.italic) {
|
|
57
|
-
child =
|
|
58
|
+
child = _jsx("em", { children: child }, void 0);
|
|
58
59
|
}
|
|
59
60
|
if (props.leaf.bold) {
|
|
60
|
-
child =
|
|
61
|
+
child = _jsx("strong", { children: child }, void 0);
|
|
61
62
|
}
|
|
62
63
|
if (props.leaf.href) {
|
|
63
|
-
child = (
|
|
64
|
+
child = (_jsx(Link, { href: props.leaf.href, target: '_blank', children: child }, void 0));
|
|
64
65
|
}
|
|
65
|
-
return
|
|
66
|
+
return _jsx("span", { ...props.attributes, children: child }, void 0);
|
|
66
67
|
};
|
|
67
68
|
//# sourceMappingURL=renderers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderers.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/renderers.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"renderers.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/renderers.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAErF,oCAAoC;AACpC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAyB,EAAE,EAAE;IACxD,QAAQ,KAAK,CAAC,OAAO,CAA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I,CAAC,YAAY,CAAC,SAAS,CAAC;QAC1D,OAAO,CACL,kBAAU,KAAK,CAAC,UAAU,YACxB,KAAC,eAAe,OAAK,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,YAAG,KAAK,WAAmB,WACxE,CACR,CAAC;KACH;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE;QAC9B,KAAK,GAAG,wBAAM,KAAK,WAAO,CAAC;KAC5B;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;QACrB,KAAK,GAAG,uBAAK,KAAK,WAAM,CAAC;KAC1B;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;QACnB,KAAK,GAAG,2BAAS,KAAK,WAAU,CAAC;KAClC;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;QACnB,KAAK,GAAG,CACN,KAAC,IAAI,IAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAC,QAAQ,YACzC,KAAK,WACD,CACR,CAAC;KACH;IAED,OAAO,kBAAU,KAAK,CAAC,UAAU,YAAG,KAAK,WAAQ,CAAC;AACpD,CAAC,CAAC","sourcesContent":["import { RenderElementProps, RenderLeafProps } from 'slate-react';\n\nimport { Text, Link, Image } from '@pega/cosmos-react-core';\n\nimport { Table, TableRow, TableCell, TableBody } from '../DecoratorComponents/Table';\n\n// Renderer for block level elements\nexport const nodeRenderer = (props: RenderElementProps) => {\n switch (props.element.type) {\n case 'heading-1':\n return (\n <Text variant='h1' {...props.attributes}>\n {props.children}\n </Text>\n );\n case 'heading-2':\n return (\n <Text variant='h2' {...props.attributes}>\n {props.children}\n </Text>\n );\n case 'heading-3':\n return (\n <Text variant='h3' {...props.attributes}>\n {props.children}\n </Text>\n );\n case 'heading-4':\n return (\n <Text variant='h4' {...props.attributes}>\n {props.children}\n </Text>\n );\n case 'ordered-list':\n return <ol {...props.attributes}>{props.children}</ol>;\n case 'unordered-list':\n return <ul {...props.attributes}>{props.children}</ul>;\n case 'list-item':\n return (\n <li\n {...props.attributes}\n data-order={props.element.order}\n data-level={props.element.level}\n data-id={props.element.id}\n data-parent-id={props.element.parentId}\n >\n {props.children}\n </li>\n );\n case 'custom': {\n const CustomComponent = props.element.component;\n return <CustomComponent {...props.attributes}>{props.element.content}</CustomComponent>;\n }\n case 'override': {\n return props.element.component;\n }\n case 'table':\n return <Table {...props.attributes}>{props.children}</Table>;\n case 'table-body':\n return <TableBody {...props.attributes}>{props.children}</TableBody>;\n case 'table-row':\n return <TableRow {...props.attributes}>{props.children}</TableRow>;\n case 'table-cell':\n return <TableCell {...props.attributes}>{props.children}</TableCell>;\n case 'paragraph':\n return (\n <div>\n <Text {...props.attributes}>{props.children}</Text>\n </div>\n );\n case 'image':\n return (\n <div {...props.attributes}>\n <Image src={props.element.src} alt={props.element.alt as string} />\n {props.children}\n </div>\n );\n case 'image-placeholder':\n return <div {...props.attributes}>{props.children}</div>;\n default: {\n return <span {...props.attributes}>{props.children}</span>;\n }\n }\n};\n\n// Renderer for inline elements\nexport const leafRenderer = (props: RenderLeafProps) => {\n let child = props.children;\n\n if (props.leaf.searchResult) {\n const CustomComponent = props.leaf.searchResult.component;\n return (\n <span {...props.attributes}>\n <CustomComponent {...props.leaf.searchResult.props}>{child}</CustomComponent>\n </span>\n );\n }\n\n if (props.leaf['line-through']) {\n child = <del>{child}</del>;\n }\n\n if (props.leaf.italic) {\n child = <em>{child}</em>;\n }\n\n if (props.leaf.bold) {\n child = <strong>{child}</strong>;\n }\n\n if (props.leaf.href) {\n child = (\n <Link href={props.leaf.href} target='_blank'>\n {child}\n </Link>\n );\n }\n\n return <span {...props.attributes}>{child}</span>;\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAe,MAAM,OAAO,CAAC;AAG5C,eAAO,MAAM,cAAc,WACjB,MAAM;aAC0B,MAAM;WAAS,MAAM;aAAW,MAAM;;;;;aAiC/E,CAAC"}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { Editor, Range } from 'slate';
|
|
2
2
|
// eslint-disable-next-line import/prefer-default-export
|
|
3
3
|
export const onSearchChange = (editor, { groupId, regex, trigger }) => {
|
|
4
|
-
var _a;
|
|
5
4
|
const { selection } = editor;
|
|
6
5
|
if (selection && Range.isCollapsed(selection)) {
|
|
7
6
|
// Gets the cursor position in the RTE
|
|
8
7
|
const [cursorPos] = Range.edges(selection);
|
|
8
|
+
// Gets the Slate node at the search trigger
|
|
9
|
+
const [searchNode] = Editor.node(editor, cursorPos);
|
|
10
|
+
// If the search is triggered from within a searchResult node, ignore the search
|
|
11
|
+
if (searchNode?.searchResult)
|
|
12
|
+
return;
|
|
9
13
|
// Gets the point before the line that is currently being typed in the RTE
|
|
10
14
|
const posBeforeWord = Editor.before(editor, cursorPos, { unit: 'line' });
|
|
11
15
|
// Gets the range before the line up to the cursor position
|
|
@@ -14,8 +18,8 @@ export const onSearchChange = (editor, { groupId, regex, trigger }) => {
|
|
|
14
18
|
const lineText = lineRange && Editor.string(editor, lineRange);
|
|
15
19
|
// Matches the text on the current line with the regex provided
|
|
16
20
|
const beforeMatch = lineText && lineText.match(regex);
|
|
17
|
-
if (beforeMatch && typeof
|
|
18
|
-
const matchLength = beforeMatch
|
|
21
|
+
if (beforeMatch && typeof beforeMatch.groups?.[groupId] === 'string') {
|
|
22
|
+
const matchLength = beforeMatch.groups.match.length;
|
|
19
23
|
// Gets the range of the matching text to place the popover correctly
|
|
20
24
|
const targetRange = Editor.range(editor, { ...cursorPos, offset: cursorPos.offset - matchLength }, cursorPos);
|
|
21
25
|
return { search: beforeMatch.groups[groupId], target: targetRange, trigger };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAQ,MAAM,OAAO,CAAC;AAE5C,wDAAwD;AACxD,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,MAAc,EACd,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAuD,EAChF,EAAE;IACF,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;IAE7B,IAAI,SAAS,IAAI,KAAK,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE;QAC7C,sCAAsC;QACtC,MAAM,CAAC,SAAS,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC3C,4CAA4C;QAC5C,MAAM,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QACpD,gFAAgF;QAChF,IAAK,UAAmB,EAAE,YAAY;YAAE,OAAO;QAE/C,0EAA0E;QAC1E,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;QACzE,2DAA2D;QAC3D,MAAM,SAAS,GAAG,aAAa,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;QAClF,2CAA2C;QAC3C,MAAM,QAAQ,GAAG,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QAE/D,+DAA+D;QAC/D,MAAM,WAAW,GAAG,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAEtD,IAAI,WAAW,IAAI,OAAO,WAAW,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,EAAE;YACpE,MAAM,WAAW,GAAG,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;YACpD,qEAAqE;YACrE,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAC9B,MAAM,EACN,EAAE,GAAG,SAAS,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,GAAG,WAAW,EAAE,EACxD,SAAS,CACV,CAAC;YACF,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC;SAC9E;KACF;AACH,CAAC,CAAC","sourcesContent":["import { Editor, Range, Text } from 'slate';\n\n// eslint-disable-next-line import/prefer-default-export\nexport const onSearchChange = (\n editor: Editor,\n { groupId, regex, trigger }: { groupId: string; regex: RegExp; trigger: string }\n) => {\n const { selection } = editor;\n\n if (selection && Range.isCollapsed(selection)) {\n // Gets the cursor position in the RTE\n const [cursorPos] = Range.edges(selection);\n // Gets the Slate node at the search trigger\n const [searchNode] = Editor.node(editor, cursorPos);\n // If the search is triggered from within a searchResult node, ignore the search\n if ((searchNode as Text)?.searchResult) return;\n\n // Gets the point before the line that is currently being typed in the RTE\n const posBeforeWord = Editor.before(editor, cursorPos, { unit: 'line' });\n // Gets the range before the line up to the cursor position\n const lineRange = posBeforeWord && Editor.range(editor, posBeforeWord, cursorPos);\n // Gets all of the text on the current line\n const lineText = lineRange && Editor.string(editor, lineRange);\n\n // Matches the text on the current line with the regex provided\n const beforeMatch = lineText && lineText.match(regex);\n\n if (beforeMatch && typeof beforeMatch.groups?.[groupId] === 'string') {\n const matchLength = beforeMatch.groups.match.length;\n // Gets the range of the matching text to place the popover correctly\n const targetRange = Editor.range(\n editor,\n { ...cursorPos, offset: cursorPos.offset - matchLength },\n cursorPos\n );\n return { search: beforeMatch.groups[groupId], target: targetRange, trigger };\n }\n }\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slateConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAEpD,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;CAmB1B,CAAC;
|
|
1
|
+
{"version":3,"file":"slateConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAEpD,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;CAmB1B,CAAC;AA8BF,eAAO,MAAM,aAAa,UAAW,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,KAAG,MAkCzD,CAAC;AAEF,eAAO,MAAM,kBAAkB,UAAW,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,KAAG,MAW9D,CAAC"}
|
|
@@ -21,6 +21,7 @@ export const slateToHtmlMap = {
|
|
|
21
21
|
};
|
|
22
22
|
// Reduces the properties in a slate node into the correct html attributes, mostly used for list-items at the moment
|
|
23
23
|
const getAttrs = (el) => {
|
|
24
|
+
// eslint-disable-next-line sonarjs/no-small-switch
|
|
24
25
|
switch (el.type) {
|
|
25
26
|
case 'list-item':
|
|
26
27
|
return [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slateConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAEpD,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,GAAG;IACT,WAAW,EAAE,IAAI;IAC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AAG,GAAG,CAAC;iBAC7C;gBACD,OAAO,IAAI,aAAa,IAAI,QAAQ,KAAK,GAAG,GAAG,CAAC;YAClD,CAAC,EAAE,EAAE,CAAC,CAAC;YACP,IAAI,CAAC,IAAI,EAAE;gBACT,IAAI,GAAG,IAAI,CAAC;aACb;SACF;aAAM;YACL,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC7B,IAAI,GAAG,IAAI,OAAO,GAAG,KAAK,IAAI,aAAa,CACxC,IAAI,CAAC,QAA+B,IAAI,EAAE,CAC5C,KAAK,OAAO,GAAG,CAAC;SAClB;QACD,OAAO,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC;IACzB,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAyB,EAAU,EAAE;IACtE,IACE,KAAK,CAAC,MAAM,KAAK,CAAC;QAClB,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC3B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACjC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,EAAE,EAChC;QACA,OAAO,EAAE,CAAC;KACX;IACD,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACvC,OAAO,SAAS,SAAS,SAAS,CAAC;AACrC,CAAC,CAAC","sourcesContent":["import { CosmosCustom, Element, Text } from 'slate';\n\nexport const slateToHtmlMap = {\n link: 'a',\n 'heading-1': 'h1',\n 'heading-2': 'h2',\n 'heading-3': 'h3',\n 'heading-4': 'h4',\n 'unordered-list': 'ul',\n 'ordered-list': 'ol',\n 'list-item': 'li',\n table: 'table',\n 'table-body': 'tbody',\n 'table-row': 'tr',\n 'table-cell': 'td',\n paragraph: 'p',\n bold: 'b',\n 'line-through': 'del',\n italic: 'i',\n href: 'a',\n image: 'img'\n};\n\n// Reduces the properties in a slate node into the correct html attributes, mostly used for list-items at the moment\nconst getAttrs = (\n el: Exclude<Element, CosmosCustom.ImagePlaceholderElement | CosmosCustom.CustomElement>\n) => {\n switch (el.type) {\n case 'list-item':\n return [\n { name: 'data-id', value: el.id },\n { name: 'data-parent-id', value: el.parentId },\n { name: 'data-order', value: el.order },\n { name: 'data-level', value: el.level }\n ].reduce((acc, attr) => {\n if (attr.value) {\n return `${acc} ${attr.name}='${attr.value}'`;\n }\n return acc;\n }, '');\n default:\n return Object.keys(el).reduce((innerAcc, key) => {\n return key !== 'type' && key !== 'children'\n ? `${innerAcc} ${key}='${el[key as keyof Element]}'`\n : innerAcc;\n }, '');\n }\n};\n\n// Recursive function to reduce Slate Nodes into an HTML string\nexport const constructHtml = (nodes: (Element | Text)[]): string => {\n return nodes.reduce((acc, node) => {\n if (\n Element.isElement(node) &&\n (node.type === 'image-placeholder' || node.type === 'custom' || node.type === 'override')\n )\n return acc;\n\n const htmlTag = Element.isElement(node) ? slateToHtmlMap[node.type] : undefined;\n let html;\n if (Text.isText(node)) {\n const { text, ...restNode } = node;\n // Reducing inline style properties from a single Slate Node into the correct HTML tags\n html = Object.keys(restNode).reduce((innerAcc, key, i) => {\n const tag = slateToHtmlMap[key as 'bold' | 'line-through' | 'italic' | 'href'];\n // Add the href attribute if needed\n const firstInnerTag = tag === 'a' ? `${tag} href='${restNode[key as 'href']}'` : tag;\n\n if (i === 0) {\n return `<${firstInnerTag}>${text}</${tag}>`;\n }\n return `<${firstInnerTag}>${innerAcc}</${tag}>`;\n }, '');\n if (!html) {\n html = text;\n }\n } else {\n const attrs = getAttrs(node);\n html = `<${htmlTag}${attrs}>${constructHtml(\n (node.children as (Element | Text)[]) || []\n )}</${htmlTag}>`;\n }\n return `${acc}${html}`;\n }, '');\n};\n\nexport const convertSlateToHtml = (nodes: (Element | Text)[]): string => {\n if (\n nodes.length === 1 &&\n Element.isElement(nodes[0]) &&\n Text.isText(nodes[0].children[0]) &&\n nodes[0].children[0].text === ''\n ) {\n return '';\n }\n const htmlNodes = constructHtml(nodes);\n return `<body>${htmlNodes}</body>`;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"slateConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAEpD,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,GAAG;IACT,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,IAAI;IACtB,cAAc,EAAE,IAAI;IACpB,WAAW,EAAE,IAAI;IACjB,KAAK,EAAE,OAAO;IACd,YAAY,EAAE,OAAO;IACrB,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE,IAAI;IAClB,SAAS,EAAE,GAAG;IACd,IAAI,EAAE,GAAG;IACT,cAAc,EAAE,KAAK;IACrB,MAAM,EAAE,GAAG;IACX,IAAI,EAAE,GAAG;IACT,KAAK,EAAE,KAAK;CACb,CAAC;AAEF,oHAAoH;AACpH,MAAM,QAAQ,GAAG,CACf,EAAuF,EACvF,EAAE;IACF,mDAAmD;IACnD,QAAQ,EAAE,CAAC,IAAI,EAAE;QACf,KAAK,WAAW;YACd,OAAO;gBACL,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,EAAE;gBACjC,EAAE,IAAI,EAAE,gBAAgB,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,EAAE;gBAC9C,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,EAAE;gBACvC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,EAAE;aACxC,CAAC,MAAM,C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,OAAO,EAAE,CAAC;KACX;IACD,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACvC,OAAO,SAAS,SAAS,SAAS,CAAC;AACrC,CAAC,CAAC","sourcesContent":["import { CosmosCustom, Element, Text } from 'slate';\n\nexport const slateToHtmlMap = {\n link: 'a',\n 'heading-1': 'h1',\n 'heading-2': 'h2',\n 'heading-3': 'h3',\n 'heading-4': 'h4',\n 'unordered-list': 'ul',\n 'ordered-list': 'ol',\n 'list-item': 'li',\n table: 'table',\n 'table-body': 'tbody',\n 'table-row': 'tr',\n 'table-cell': 'td',\n paragraph: 'p',\n bold: 'b',\n 'line-through': 'del',\n italic: 'i',\n href: 'a',\n image: 'img'\n};\n\n// Reduces the properties in a slate node into the correct html attributes, mostly used for list-items at the moment\nconst getAttrs = (\n el: Exclude<Element, CosmosCustom.ImagePlaceholderElement | CosmosCustom.CustomElement>\n) => {\n // eslint-disable-next-line sonarjs/no-small-switch\n switch (el.type) {\n case 'list-item':\n return [\n { name: 'data-id', value: el.id },\n { name: 'data-parent-id', value: el.parentId },\n { name: 'data-order', value: el.order },\n { name: 'data-level', value: el.level }\n ].reduce((acc, attr) => {\n if (attr.value) {\n return `${acc} ${attr.name}='${attr.value}'`;\n }\n return acc;\n }, '');\n default:\n return Object.keys(el).reduce((innerAcc, key) => {\n return key !== 'type' && key !== 'children'\n ? `${innerAcc} ${key}='${el[key as keyof Element]}'`\n : innerAcc;\n }, '');\n }\n};\n\n// Recursive function to reduce Slate Nodes into an HTML string\nexport const constructHtml = (nodes: (Element | Text)[]): string => {\n return nodes.reduce((acc, node) => {\n if (\n Element.isElement(node) &&\n (node.type === 'image-placeholder' || node.type === 'custom' || node.type === 'override')\n )\n return acc;\n\n const htmlTag = Element.isElement(node) ? slateToHtmlMap[node.type] : undefined;\n let html;\n if (Text.isText(node)) {\n const { text, ...restNode } = node;\n // Reducing inline style properties from a single Slate Node into the correct HTML tags\n html = Object.keys(restNode).reduce((innerAcc, key, i) => {\n const tag = slateToHtmlMap[key as 'bold' | 'line-through' | 'italic' | 'href'];\n // Add the href attribute if needed\n const firstInnerTag = tag === 'a' ? `${tag} href='${restNode[key as 'href']}'` : tag;\n\n if (i === 0) {\n return `<${firstInnerTag}>${text}</${tag}>`;\n }\n return `<${firstInnerTag}>${innerAcc}</${tag}>`;\n }, '');\n if (!html) {\n html = text;\n }\n } else {\n const attrs = getAttrs(node);\n html = `<${htmlTag}${attrs}>${constructHtml(\n (node.children as (Element | Text)[]) || []\n )}</${htmlTag}>`;\n }\n return `${acc}${html}`;\n }, '');\n};\n\nexport const convertSlateToHtml = (nodes: (Element | Text)[]): string => {\n if (\n nodes.length === 1 &&\n Element.isElement(nodes[0]) &&\n Text.isText(nodes[0].children[0]) &&\n nodes[0].children[0].text === ''\n ) {\n return '';\n }\n const htmlNodes = constructHtml(nodes);\n return `<body>${htmlNodes}</body>`;\n};\n"]}
|