@pushwoosh/dumb-components 1.0.48 → 1.0.50
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.
|
@@ -14,6 +14,7 @@ export const RichMessageEditor = ({
|
|
|
14
14
|
title,
|
|
15
15
|
initialValue,
|
|
16
16
|
minHeight,
|
|
17
|
+
error,
|
|
17
18
|
canUsePersonalization,
|
|
18
19
|
canUseEmoji,
|
|
19
20
|
onChange,
|
|
@@ -74,6 +75,7 @@ export const RichMessageEditor = ({
|
|
|
74
75
|
}) => React.createElement(FieldBox, {
|
|
75
76
|
title: title,
|
|
76
77
|
boxRef: rootRef,
|
|
78
|
+
error: error,
|
|
77
79
|
actions: React.createElement(Collapse, {
|
|
78
80
|
expanded: isFocused
|
|
79
81
|
}, React.createElement(Horizontal, {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import React, { Fragment, useMemo } from 'react';
|
|
2
2
|
import { DynamicContentBracket, DynamicContentRoot } from './RichMessageViewer.styled';
|
|
3
3
|
import { isElement, string2content } from './slate/helpers';
|
|
4
|
-
const arrayJoinReact = (arr, separator) => arr.flatMap((item, index) => index ? [separator, item] : [item]);
|
|
5
4
|
export const RichMessageViewer = ({
|
|
6
5
|
value,
|
|
7
6
|
lineBreaks
|
|
@@ -9,7 +8,8 @@ export const RichMessageViewer = ({
|
|
|
9
8
|
const content = useMemo(() => {
|
|
10
9
|
return Array.isArray(value) ? value : string2content(value);
|
|
11
10
|
}, [value]);
|
|
12
|
-
|
|
11
|
+
const separator = lineBreaks ? React.createElement("br", null) : ' ';
|
|
12
|
+
return content.map((node, index) => {
|
|
13
13
|
const children = node.children.map((child, idx) => {
|
|
14
14
|
if (isElement(child) && child.type === 'dc') {
|
|
15
15
|
return (
|
|
@@ -21,8 +21,11 @@ export const RichMessageViewer = ({
|
|
|
21
21
|
}
|
|
22
22
|
return child.text;
|
|
23
23
|
});
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
return (
|
|
25
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
26
|
+
React.createElement(Fragment, {
|
|
27
|
+
key: index
|
|
28
|
+
}, index > 0 && separator, children)
|
|
29
|
+
);
|
|
30
|
+
});
|
|
28
31
|
};
|
|
@@ -1,10 +1,64 @@
|
|
|
1
1
|
import type { Descendant } from 'slate';
|
|
2
2
|
import { type CustomElement, type DynamicContent, type DynamicContentElement, type SlateEditorContent } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a Slate node is a custom element
|
|
5
|
+
* @param x - The node to check
|
|
6
|
+
* @returns True if the node is a custom element
|
|
7
|
+
*/
|
|
3
8
|
export declare function isElement(x: Descendant): x is CustomElement;
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves a dynamic content element by its path in the Slate document
|
|
11
|
+
* @param value - The Slate editor content
|
|
12
|
+
* @param path - Array of indices representing the path to the element
|
|
13
|
+
* @returns The dynamic content element if found, undefined otherwise
|
|
14
|
+
*/
|
|
4
15
|
export declare function getDcByPath(value: SlateEditorContent, path: number[]): DynamicContentElement | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Finds all dynamic content elements in the editor content
|
|
18
|
+
* @param value - The Slate editor content
|
|
19
|
+
* @returns Array of all dynamic content elements
|
|
20
|
+
*/
|
|
5
21
|
export declare function findAllDc(value: SlateEditorContent): DynamicContentElement[];
|
|
22
|
+
/**
|
|
23
|
+
* Finds the path to a specific element in the Slate document
|
|
24
|
+
* @param value - The Slate editor content
|
|
25
|
+
* @param elToFind - The element to find
|
|
26
|
+
* @returns Array of indices representing the path to the element, empty if not found
|
|
27
|
+
*/
|
|
6
28
|
export declare function findPath(value: SlateEditorContent, elToFind: Descendant): number[];
|
|
29
|
+
/**
|
|
30
|
+
* Parses a dynamic content string into a DynamicContent object
|
|
31
|
+
* @param value - String in format "{tagName|modifier|defaultValue}"
|
|
32
|
+
* @returns DynamicContent object with parsed components
|
|
33
|
+
*/
|
|
7
34
|
export declare function parseDc(value: string): DynamicContent;
|
|
35
|
+
/**
|
|
36
|
+
* Converts a DynamicContent object to its string representation
|
|
37
|
+
* @param dc - The DynamicContent object
|
|
38
|
+
* @returns String representation in format "{tagName|modifier|defaultValue}"
|
|
39
|
+
*/
|
|
8
40
|
export declare function stringifyDc(dc: DynamicContent): string;
|
|
41
|
+
/**
|
|
42
|
+
* Converts a string to Slate editor content, parsing dynamic content tags
|
|
43
|
+
* @param value - The input string to convert
|
|
44
|
+
* @returns SlateEditorContent with properly structured paragraphs and dynamic content
|
|
45
|
+
*/
|
|
9
46
|
export declare function string2content(value: string): SlateEditorContent;
|
|
47
|
+
/**
|
|
48
|
+
* Converts Slate editor content to a string, including dynamic content tags
|
|
49
|
+
* @param state - The Slate editor content to convert
|
|
50
|
+
* @returns String representation of the content with dynamic content tags
|
|
51
|
+
*/
|
|
10
52
|
export declare function content2string(state: SlateEditorContent): string;
|
|
53
|
+
/**
|
|
54
|
+
* Validates if the content has valid dynamic content elements
|
|
55
|
+
* @param value - The Slate editor content to validate
|
|
56
|
+
* @returns True if all dynamic content elements are valid, false otherwise
|
|
57
|
+
*/
|
|
58
|
+
export declare function validateContent(value: SlateEditorContent): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Validates if the string has valid dynamic content elements
|
|
61
|
+
* @param value - The input string to validate
|
|
62
|
+
* @returns True if all dynamic content elements are valid, false otherwise
|
|
63
|
+
*/
|
|
64
|
+
export declare function validateContentSting(value: string): boolean;
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a Slate node is a custom element
|
|
3
|
+
* @param x - The node to check
|
|
4
|
+
* @returns True if the node is a custom element
|
|
5
|
+
*/
|
|
1
6
|
export function isElement(x) {
|
|
2
7
|
return !!x.type;
|
|
3
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves a dynamic content element by its path in the Slate document
|
|
11
|
+
* @param value - The Slate editor content
|
|
12
|
+
* @param path - Array of indices representing the path to the element
|
|
13
|
+
* @returns The dynamic content element if found, undefined otherwise
|
|
14
|
+
*/
|
|
4
15
|
export function getDcByPath(value, path) {
|
|
5
16
|
let current = value;
|
|
6
17
|
for (let i = 0; i < path.length; i += 1) {
|
|
@@ -16,6 +27,11 @@ export function getDcByPath(value, path) {
|
|
|
16
27
|
}
|
|
17
28
|
return undefined;
|
|
18
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Finds all dynamic content elements in the editor content
|
|
32
|
+
* @param value - The Slate editor content
|
|
33
|
+
* @returns Array of all dynamic content elements
|
|
34
|
+
*/
|
|
19
35
|
export function findAllDc(value) {
|
|
20
36
|
const dces = [];
|
|
21
37
|
function walkDeep(children) {
|
|
@@ -31,6 +47,12 @@ export function findAllDc(value) {
|
|
|
31
47
|
walkDeep(value);
|
|
32
48
|
return dces;
|
|
33
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Finds the path to a specific element in the Slate document
|
|
52
|
+
* @param value - The Slate editor content
|
|
53
|
+
* @param elToFind - The element to find
|
|
54
|
+
* @returns Array of indices representing the path to the element, empty if not found
|
|
55
|
+
*/
|
|
34
56
|
export function findPath(value, elToFind) {
|
|
35
57
|
for (let i = 0; i < value.length; i++) {
|
|
36
58
|
const el = value[i];
|
|
@@ -46,6 +68,11 @@ export function findPath(value, elToFind) {
|
|
|
46
68
|
}
|
|
47
69
|
return [];
|
|
48
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Parses a dynamic content string into a DynamicContent object
|
|
73
|
+
* @param value - String in format "{tagName|modifier|defaultValue}"
|
|
74
|
+
* @returns DynamicContent object with parsed components
|
|
75
|
+
*/
|
|
49
76
|
export function parseDc(value) {
|
|
50
77
|
const [tagName, modifier, defaultValue = ''] = value.slice(1, -1).split('|');
|
|
51
78
|
return {
|
|
@@ -54,6 +81,11 @@ export function parseDc(value) {
|
|
|
54
81
|
defaultValue
|
|
55
82
|
};
|
|
56
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Converts a DynamicContent object to its string representation
|
|
86
|
+
* @param dc - The DynamicContent object
|
|
87
|
+
* @returns String representation in format "{tagName|modifier|defaultValue}"
|
|
88
|
+
*/
|
|
57
89
|
export function stringifyDc(dc) {
|
|
58
90
|
const {
|
|
59
91
|
tagName,
|
|
@@ -70,6 +102,11 @@ export function stringifyDc(dc) {
|
|
|
70
102
|
// It will not match strings like '{tag|modifier|default}' if there is no '}' after the last '|'.
|
|
71
103
|
// This regex is used to split the string into parts, where the parts are either text or dynamic content.
|
|
72
104
|
const DynamicContentRegex = /(\{(?=[^}]*\|)[^}]*?})/;
|
|
105
|
+
/**
|
|
106
|
+
* Converts a string to Slate editor content, parsing dynamic content tags
|
|
107
|
+
* @param value - The input string to convert
|
|
108
|
+
* @returns SlateEditorContent with properly structured paragraphs and dynamic content
|
|
109
|
+
*/
|
|
73
110
|
export function string2content(value) {
|
|
74
111
|
return value.split('\n').map(line => ({
|
|
75
112
|
type: 'paragraph',
|
|
@@ -89,6 +126,11 @@ export function string2content(value) {
|
|
|
89
126
|
})
|
|
90
127
|
}));
|
|
91
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Converts Slate editor content to a string, including dynamic content tags
|
|
131
|
+
* @param state - The Slate editor content to convert
|
|
132
|
+
* @returns String representation of the content with dynamic content tags
|
|
133
|
+
*/
|
|
92
134
|
export function content2string(state) {
|
|
93
135
|
return state.map(el => {
|
|
94
136
|
const {
|
|
@@ -101,4 +143,31 @@ export function content2string(state) {
|
|
|
101
143
|
return el.text;
|
|
102
144
|
}).join('');
|
|
103
145
|
}).join('\n');
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Validates if the content has valid dynamic content elements
|
|
149
|
+
* @param value - The Slate editor content to validate
|
|
150
|
+
* @returns True if all dynamic content elements are valid, false otherwise
|
|
151
|
+
*/
|
|
152
|
+
export function validateContent(value) {
|
|
153
|
+
for (const el of value) {
|
|
154
|
+
if (isElement(el)) {
|
|
155
|
+
if (el.type === 'dc') {
|
|
156
|
+
return !!el.dc.tagName;
|
|
157
|
+
}
|
|
158
|
+
if (!validateContent(el.children)) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Validates if the string has valid dynamic content elements
|
|
167
|
+
* @param value - The input string to validate
|
|
168
|
+
* @returns True if all dynamic content elements are valid, false otherwise
|
|
169
|
+
*/
|
|
170
|
+
export function validateContentSting(value) {
|
|
171
|
+
const content = string2content(value);
|
|
172
|
+
return validateContent(content);
|
|
104
173
|
}
|