This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
render.jsx
145 lines (128 loc) · 3.9 KB
/
render.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { Node, Text } from 'slate';
import cx from 'classnames';
import { isEmpty, isEqual, omit } from 'lodash';
import config from '@plone/volto/registry';
const OMITTED = ['editor', 'path'];
// TODO: read, see if relevant
// https://reactjs.org/docs/higher-order-components.html#dont-use-hocs-inside-the-render-method
export const Element = ({ element, attributes = {}, extras, ...rest }) => {
const { slate } = config.settings;
const { elements } = slate;
const El = elements[element.type] || elements['default'];
const out = Object.assign(
{},
...Object.keys(attributes || {}).map((k) =>
!isEmpty(attributes[k]) ? { [k]: attributes[k] } : {},
),
);
return <El element={element} {...omit(rest, OMITTED)} attributes={out} />;
};
export const Leaf = ({ children, ...rest }) => {
const { attributes, leaf, mode } = rest;
let { leafs } = config.settings.slate;
children = Object.keys(leafs).reduce((acc, name) => {
return Object.keys(leaf).includes(name)
? leafs[name]({ children: acc })
: acc;
}, children);
const obj = {
[`highlight-${leaf.highlightType}`]: mode !== 'view' && leaf.highlight,
'highlight-selection': mode !== 'view' && leaf.isSelection,
};
// stylemenu support
for (const prop in leaf) {
if (prop.startsWith('style-')) {
obj[prop.substring(6)] = true;
}
}
const klass = cx(obj);
return mode === 'view' ? (
typeof children === 'string' ? (
children.split('\n').map((t, i) => {
// Softbreak support. Should do a plugin?
return (
<React.Fragment key={`${i}`}>
{children.indexOf('\n') > -1 &&
children.split('\n').length - 1 > i ? (
<>
{klass ? <span className={klass}>{t}</span> : t}
<br />
</>
) : klass ? (
<span className={klass}>{t}</span>
) : (
t
)}
</React.Fragment>
);
})
) : (
<span className={klass}>{children}</span>
)
) : (
<span {...attributes} className={klass}>
{children}
</span>
);
};
const serializeData = (node) => {
return JSON.stringify({ type: node.type, data: node.data });
};
export const serializeNodes = (nodes, getAttributes) => {
const editor = { children: nodes || [] };
const _serializeNodes = (nodes) => {
return (nodes || []).map(([node, path], i) => {
return Text.isText(node) ? (
<Leaf path={path} leaf={node} text={node} mode="view" key={path}>
{node.text}
</Leaf>
) : (
<Element
path={path}
element={node}
mode="view"
key={path}
data-slate-data={node.data ? serializeData(node) : null}
attributes={
isEqual(path, [0])
? getAttributes
? getAttributes(node, path)
: null
: null
}
>
{_serializeNodes(Array.from(Node.children(editor, path)))}
</Element>
);
});
};
return _serializeNodes(Array.from(Node.children(editor, [])));
};
/**
* Get the concatenated text string of a node's content.
*
* Note that this WILL include spaces between block node leaves in contrary to the original slate method.
* This function joins text of nodes with separating spaces to produce a string for indexing purposes.
*
*/
const ConcatenatedString = (node) => {
if (Text.isText(node)) {
return node.text.trim();
} else {
return node.children.map(ConcatenatedString).join(' ');
}
};
/**
* @function serializeNodesToText
*
* @param {Array[Node]} nodes
*
* @returns {string}
*/
export const serializeNodesToText = (nodes) => {
return nodes.map(ConcatenatedString).join('\n');
};
export const serializeNodesToHtml = (nodes) =>
renderToStaticMarkup(serializeNodes(nodes));