@pega/cosmos-react-rte 8.21.5 → 8.21.7

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.
@@ -1,4 +1,4 @@
1
- import { Element, Text } from 'slate';
1
+ import { Element as SlateElement, Text as SlateText } from 'slate';
2
2
  export declare const slateToHtmlMap: {
3
3
  link: string;
4
4
  'heading-1': string;
@@ -24,6 +24,6 @@ export declare const slateToHtmlMap: {
24
24
  href: string;
25
25
  image: string;
26
26
  };
27
- export declare const constructHtml: (nodes: (Element | Text)[]) => string;
28
- export declare const convertSlateToHtml: (nodes: (Element | Text)[]) => string;
27
+ export declare const constructHtml: (nodes: (SlateElement | SlateText)[]) => Node[];
28
+ export declare const convertSlateToHtml: (nodes: (SlateElement | SlateText)[]) => string;
29
29
  //# sourceMappingURL=slateConverter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"slateConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAKtC,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;CAyB1B,CAAC;AA8BF,eAAO,MAAM,aAAa,GAAI,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,KAAG,MAkCzD,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,KAAG,MAW9D,CAAC"}
1
+ {"version":3,"file":"slateConverter.d.ts","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,OAAO,CAAC;AAMnE,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;CAyB1B,CAAC;AAsDF,eAAO,MAAM,aAAa,GAAI,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC,EAAE,KAAG,IAAI,EA+DvE,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC,EAAE,KAAG,MAiBxE,CAAC"}
@@ -1,4 +1,5 @@
1
- import { Element, Text } from 'slate';
1
+ import { Element as SlateElement, Text as SlateText } from 'slate';
2
+ import { disallowedURI } from '@pega/cosmos-react-core';
2
3
  export const slateToHtmlMap = {
3
4
  link: 'a',
4
5
  'heading-1': 'h1',
@@ -29,65 +30,102 @@ export const slateToHtmlMap = {
29
30
  const getAttrs = (el) => {
30
31
  // eslint-disable-next-line sonarjs/no-small-switch
31
32
  switch (el.type) {
32
- case 'list-item':
33
- return [
34
- { name: 'data-id', value: el.id },
35
- { name: 'data-parent-id', value: el.parentId },
36
- { name: 'data-order', value: el.order },
37
- { name: 'data-level', value: el.level }
38
- ].reduce((acc, attr) => {
39
- if (attr.value) {
40
- return `${acc} ${attr.name}='${attr.value}'`;
41
- }
42
- return acc;
43
- }, '');
33
+ case 'list-item': {
34
+ const attrs = [];
35
+ if (el.id) {
36
+ attrs.push(['data-id', el.id]);
37
+ }
38
+ if (el.parentId) {
39
+ attrs.push(['data-parent-id', el.parentId]);
40
+ }
41
+ if (el.order !== undefined) {
42
+ attrs.push(['data-order', `${el.order}`]);
43
+ }
44
+ if (el.level !== undefined) {
45
+ attrs.push(['data-level', `${el.level}`]);
46
+ }
47
+ return attrs;
48
+ }
44
49
  default:
45
- return Object.keys(el).reduce((innerAcc, key) => {
46
- return key !== 'type' && key !== 'children'
47
- ? `${innerAcc} ${key}='${el[key]}'`
48
- : innerAcc;
49
- }, '');
50
+ return Object.entries(el).flatMap(([key, value]) => {
51
+ const lowerKey = key.toLowerCase();
52
+ if (key === 'type' ||
53
+ key === 'children' ||
54
+ // Strip event handlers.
55
+ lowerKey.startsWith('on') ||
56
+ value === undefined) {
57
+ return [];
58
+ }
59
+ if (lowerKey === 'href' && disallowedURI.test(value)) {
60
+ return [];
61
+ }
62
+ return [[lowerKey, `${value}`]];
63
+ });
50
64
  }
51
65
  };
52
66
  // Recursive function to reduce Slate Nodes into an HTML string
53
67
  export const constructHtml = (nodes) => {
54
68
  return nodes.reduce((acc, node) => {
55
- if (Element.isElement(node) &&
69
+ if (SlateElement.isElement(node) &&
56
70
  (node.type === 'image-placeholder' || node.type === 'custom' || node.type === 'override'))
57
71
  return acc;
58
- const htmlTag = Element.isElement(node) ? slateToHtmlMap[node.type] : undefined;
59
- let html;
60
- if (Text.isText(node)) {
72
+ if (SlateText.isText(node)) {
61
73
  const { text, ...restNode } = node;
62
74
  // Reducing inline style properties from a single Slate Node into the correct HTML tags
63
- html = Object.keys(restNode).reduce((innerAcc, key, i) => {
75
+ const html = Object.keys(restNode).reduce((innerAcc, key, i) => {
64
76
  const tag = slateToHtmlMap[key];
65
- // Add the href attribute if needed
66
- const firstInnerTag = tag === 'a' ? `${tag} href='${restNode[key]}'` : tag;
77
+ if (!tag)
78
+ return innerAcc;
79
+ let innerEl;
80
+ if (tag === 'a') {
81
+ const anchor = document.createElement(tag);
82
+ if (restNode.href && !disallowedURI.test(restNode.href)) {
83
+ anchor.setAttribute('href', restNode.href);
84
+ }
85
+ innerEl = anchor;
86
+ }
87
+ else {
88
+ innerEl = document.createElement(tag);
89
+ }
67
90
  if (i === 0) {
68
- return `<${firstInnerTag}>${text}</${tag}>`;
91
+ innerEl.textContent = text;
92
+ return innerEl;
93
+ }
94
+ if (innerAcc) {
95
+ innerEl.appendChild(innerAcc);
69
96
  }
70
- return `<${firstInnerTag}>${innerAcc}</${tag}>`;
71
- }, '');
72
- if (!html) {
73
- html = text;
97
+ return innerEl;
98
+ }, null);
99
+ if (html) {
100
+ return acc.concat(html);
74
101
  }
102
+ return acc.concat(document.createTextNode(text));
75
103
  }
76
- else {
77
- const attrs = getAttrs(node);
78
- html = `<${htmlTag}${attrs}>${constructHtml(node.children || [])}</${htmlTag}>`;
104
+ const htmlTag = SlateElement.isElement(node) ? slateToHtmlMap[node.type] : undefined;
105
+ if (!htmlTag)
106
+ return acc;
107
+ const el = document.createElement(htmlTag);
108
+ for (const [attr, val] of getAttrs(node)) {
109
+ el.setAttribute(attr, val);
110
+ }
111
+ for (const child of constructHtml(node.children)) {
112
+ el.appendChild(child);
79
113
  }
80
- return `${acc}${html}`;
81
- }, '');
114
+ return acc.concat(el);
115
+ }, []);
82
116
  };
83
117
  export const convertSlateToHtml = (nodes) => {
84
118
  if (nodes.length === 1 &&
85
- Element.isElement(nodes[0]) &&
86
- Text.isText(nodes[0].children[0]) &&
119
+ SlateElement.isElement(nodes[0]) &&
120
+ SlateText.isText(nodes[0].children[0]) &&
87
121
  nodes[0].children[0].text === '') {
88
122
  return '';
89
123
  }
90
124
  const htmlNodes = constructHtml(nodes);
91
- return `<body>${htmlNodes}</body>`;
125
+ const body = document.createElement('body');
126
+ for (const node of htmlNodes) {
127
+ body.appendChild(node);
128
+ }
129
+ return body.outerHTML;
92
130
  };
93
131
  //# sourceMappingURL=slateConverter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"slateConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAKtC,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,aAAa,EAAE,YAAY;IAC3B,qFAAqF;IACrF,YAAY,EAAE,KAAK;IACnB,iBAAiB,EAAE,IAAI;IACvB,KAAK,EAAE,OAAO;IACd,YAAY,EAAE,OAAO;IACrB,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE,I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IAAI,CAAC,IAAI,EAAE,CAAC;gBACV,IAAI,GAAG,IAAI,CAAC;YACd,CAAC;QACH,CAAC;aAAM,CAAC;YACN,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;QACnB,CAAC;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,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACvC,OAAO,SAAS,SAAS,SAAS,CAAC;AACrC,CAAC,CAAC","sourcesContent":["import { Element, Text } from 'slate';\nimport type { PegaCustom } from 'slate';\n\nimport type { ExcludeStrict } from '@pega/cosmos-react-core';\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 'block-quote': 'blockquote',\n // FIXME: Should render <pre><code>, but I don't know if this is really used anymore.\n 'code-block': 'pre',\n 'horizontal-rule': 'hr',\n table: 'table',\n 'table-body': 'tbody',\n 'table-row': 'tr',\n 'table-cell': 'td',\n text: 'span',\n paragraph: 'p',\n bold: 'b',\n 'line-through': 'del',\n italic: 'i',\n code: 'code',\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: ExcludeStrict<Element, PegaCustom.ImagePlaceholderElement | PegaCustom.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"]}
1
+ {"version":3,"file":"slateConverter.js","sourceRoot":"","sources":["../../../../src/components/RichTextEditor/utils/slateConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,OAAO,CAAC;AAGnE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,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,aAAa,EAAE,YAAY;IAC3B,qFAAqF;IACrF,YAAY,EAAE,KAAK;IACnB,iBAAiB,EAAE,IAAI;IACvB,KAAK,EAAE,OAAO;IACd,YAAY,EAAE,OAAO;IACrB,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE,IAAI;IAClB,IAAI,EAAE,MAAM;IACZ,SAAS,EAAE,GAAG;IACd,IAAI,EAAE,GAAG;IACT,cAAc,EAAE,KAAK;IACrB,MAAM,EAAE,GAAG;IACX,IAAI,EAAE,MAAM;IACZ,IAAI,EAAE,GAAG;IACT,KAAK,EAAE,KAAK;CACb,CAAC;AAEF,oHAAoH;AACpH,MAAM,QAAQ,GAAG,CACf,EAA8F,EAC1E,EAAE;IACtB,mDAAmD;IACnD,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC;QAChB,KAAK,WAAW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AAC,KAAmC,EAAU,EAAE;IAChF,IACE,KAAK,CAAC,MAAM,KAAK,CAAC;QAClB,YAAY,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAChC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACtC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,EAAE,EAChC,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,SAAS,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAC5C,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE,CAAC;QAC7B,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC;AACxB,CAAC,CAAC","sourcesContent":["import { Element as SlateElement, Text as SlateText } from 'slate';\nimport type { PegaCustom } from 'slate';\n\nimport { disallowedURI } from '@pega/cosmos-react-core';\nimport type { ExcludeStrict } from '@pega/cosmos-react-core';\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 'block-quote': 'blockquote',\n // FIXME: Should render <pre><code>, but I don't know if this is really used anymore.\n 'code-block': 'pre',\n 'horizontal-rule': 'hr',\n table: 'table',\n 'table-body': 'tbody',\n 'table-row': 'tr',\n 'table-cell': 'td',\n text: 'span',\n paragraph: 'p',\n bold: 'b',\n 'line-through': 'del',\n italic: 'i',\n code: 'code',\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: ExcludeStrict<SlateElement, PegaCustom.ImagePlaceholderElement | PegaCustom.CustomElement>\n): [string, string][] => {\n // eslint-disable-next-line sonarjs/no-small-switch\n switch (el.type) {\n case 'list-item': {\n const attrs: [string, string][] = [];\n\n if (el.id) {\n attrs.push(['data-id', el.id]);\n }\n\n if (el.parentId) {\n attrs.push(['data-parent-id', el.parentId]);\n }\n\n if (el.order !== undefined) {\n attrs.push(['data-order', `${el.order}`]);\n }\n\n if (el.level !== undefined) {\n attrs.push(['data-level', `${el.level}`]);\n }\n\n return attrs;\n }\n\n default:\n return Object.entries(el).flatMap(([key, value]) => {\n const lowerKey = key.toLowerCase();\n\n if (\n key === 'type' ||\n key === 'children' ||\n // Strip event handlers.\n lowerKey.startsWith('on') ||\n value === undefined\n ) {\n return [];\n }\n\n if (lowerKey === 'href' && disallowedURI.test(value)) {\n return [];\n }\n\n return [[lowerKey, `${value}`]];\n });\n }\n};\n\n// Recursive function to reduce Slate Nodes into an HTML string\nexport const constructHtml = (nodes: (SlateElement | SlateText)[]): Node[] => {\n return nodes.reduce((acc: Node[], node) => {\n if (\n SlateElement.isElement(node) &&\n (node.type === 'image-placeholder' || node.type === 'custom' || node.type === 'override')\n )\n return acc;\n\n if (SlateText.isText(node)) {\n const { text, ...restNode } = node;\n // Reducing inline style properties from a single Slate Node into the correct HTML tags\n const html = Object.keys(restNode).reduce((innerAcc: Element | null, key, i) => {\n const tag = slateToHtmlMap[key as 'bold' | 'line-through' | 'italic' | 'href'];\n\n if (!tag) return innerAcc;\n\n let innerEl: HTMLElement;\n\n if (tag === 'a') {\n const anchor = document.createElement(tag);\n if (restNode.href && !disallowedURI.test(restNode.href)) {\n anchor.setAttribute('href', restNode.href);\n }\n innerEl = anchor;\n } else {\n innerEl = document.createElement(tag);\n }\n\n if (i === 0) {\n innerEl.textContent = text;\n return innerEl;\n }\n\n if (innerAcc) {\n innerEl.appendChild(innerAcc);\n }\n\n return innerEl;\n }, null);\n\n if (html) {\n return acc.concat(html);\n }\n\n return acc.concat(document.createTextNode(text));\n }\n\n const htmlTag = SlateElement.isElement(node) ? slateToHtmlMap[node.type] : undefined;\n\n if (!htmlTag) return acc;\n\n const el = document.createElement(htmlTag);\n\n for (const [attr, val] of getAttrs(node)) {\n el.setAttribute(attr, val);\n }\n\n for (const child of constructHtml(node.children)) {\n el.appendChild(child);\n }\n\n return acc.concat(el);\n }, []);\n};\n\nexport const convertSlateToHtml = (nodes: (SlateElement | SlateText)[]): string => {\n if (\n nodes.length === 1 &&\n SlateElement.isElement(nodes[0]) &&\n SlateText.isText(nodes[0].children[0]) &&\n nodes[0].children[0].text === ''\n ) {\n return '';\n }\n\n const htmlNodes = constructHtml(nodes);\n const body = document.createElement('body');\n for (const node of htmlNodes) {\n body.appendChild(node);\n }\n\n return body.outerHTML;\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pega/cosmos-react-rte",
3
- "version": "8.21.5",
3
+ "version": "8.21.7",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "author": "Pegasystems",
6
6
  "sideEffects": false,
@@ -14,7 +14,7 @@
14
14
  "build": "tsc -b tsconfig.build.json"
15
15
  },
16
16
  "dependencies": {
17
- "@pega/cosmos-react-core": "8.21.5",
17
+ "@pega/cosmos-react-core": "8.21.7",
18
18
  "@popperjs/core": "^2.11.6",
19
19
  "@types/parse5": "^6.0.0",
20
20
  "@types/react": "^17.0.62 || ^18.3.3",