@shoutem/ui 4.6.4 → 4.6.5
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/html/components/SimpleHtml.js +40 -24
- package/package.json +1 -1
|
@@ -25,6 +25,34 @@ import getEmptyObjectKeys from '../services/getEmptyObjectKeys';
|
|
|
25
25
|
import isValidVideoFormat from '../services/isValidVideoFormat';
|
|
26
26
|
import Image from './Image';
|
|
27
27
|
|
|
28
|
+
function resolveMaxWidth(style) {
|
|
29
|
+
// parentContainerPadding - padding between screen & SimpleHtml. Parent container
|
|
30
|
+
// can have it's own padding. If it does, we have to include it in calculation,
|
|
31
|
+
// otherwise maxWidth will be more than max & images will go over the edge
|
|
32
|
+
const parentContainerPadding = style.outerPadding || 0;
|
|
33
|
+
const paddingValue =
|
|
34
|
+
style.container.paddingLeft ||
|
|
35
|
+
0 + style.container.paddingRight ||
|
|
36
|
+
0 + parentContainerPadding;
|
|
37
|
+
|
|
38
|
+
return Dimensions.get('window').width - paddingValue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function resolveDimensions(objectToResize, style) {
|
|
42
|
+
const { width, height } = objectToResize;
|
|
43
|
+
|
|
44
|
+
if (!width || !height) {
|
|
45
|
+
return { width, height };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const maxWidth = resolveMaxWidth(style);
|
|
49
|
+
const objectToResizeRatio = height / width;
|
|
50
|
+
const resolvedWidth = width > maxWidth ? maxWidth : width;
|
|
51
|
+
const resolvedHeight = Math.round(resolvedWidth * objectToResizeRatio);
|
|
52
|
+
|
|
53
|
+
return { width: resolvedWidth, height: resolvedHeight };
|
|
54
|
+
}
|
|
55
|
+
|
|
28
56
|
class SimpleHtml extends PureComponent {
|
|
29
57
|
static propTypes = {
|
|
30
58
|
body: PropTypes.string,
|
|
@@ -94,23 +122,15 @@ class SimpleHtml extends PureComponent {
|
|
|
94
122
|
}
|
|
95
123
|
}
|
|
96
124
|
|
|
97
|
-
const nodeWidth =
|
|
125
|
+
const nodeWidth = node.attribs?.width;
|
|
98
126
|
|
|
99
127
|
if (!styleAttrib && !nodeWidth) {
|
|
100
128
|
return false;
|
|
101
129
|
}
|
|
102
130
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const parentContainerPadding = style.outerPadding || 0;
|
|
107
|
-
const paddingValue =
|
|
108
|
-
_.get(style, 'container.paddingLeft') * 2 + parentContainerPadding;
|
|
109
|
-
const maxWidth = Dimensions.get('window').width - paddingValue;
|
|
110
|
-
const nodeHeight = _.get(node, 'attribus.height');
|
|
111
|
-
const nodeRatio = nodeWidth / nodeHeight;
|
|
112
|
-
const resolvedWidth = nodeWidth > maxWidth ? maxWidth : nodeWidth;
|
|
113
|
-
const resolvedHeight = Math.round(resolvedWidth * nodeRatio);
|
|
131
|
+
const nodeHeight = node.attribus?.height;
|
|
132
|
+
const nodeDimensions = { width: nodeWidth, height: nodeHeight };
|
|
133
|
+
const { width, height } = resolveDimensions(nodeDimensions, style);
|
|
114
134
|
|
|
115
135
|
const nodeStyle = cssStringToObject(styleAttrib);
|
|
116
136
|
const invalidKeys = getEmptyObjectKeys(nodeStyle);
|
|
@@ -118,8 +138,8 @@ class SimpleHtml extends PureComponent {
|
|
|
118
138
|
if (invalidKeys.length || nodeWidth) {
|
|
119
139
|
const styleFiltered = _.omit(style, invalidKeys);
|
|
120
140
|
node.attribs.style = cssObjectToString(styleFiltered);
|
|
121
|
-
node.attribs.width =
|
|
122
|
-
node.attribs.height =
|
|
141
|
+
node.attribs.width = width;
|
|
142
|
+
node.attribs.height = height;
|
|
123
143
|
|
|
124
144
|
return node;
|
|
125
145
|
}
|
|
@@ -175,7 +195,7 @@ class SimpleHtml extends PureComponent {
|
|
|
175
195
|
* Currently only supports rendering image attachments.
|
|
176
196
|
*/
|
|
177
197
|
renderAttachments({ id, type }) {
|
|
178
|
-
const { attachments } = this.props;
|
|
198
|
+
const { attachments, style } = this.props;
|
|
179
199
|
|
|
180
200
|
if (!attachments) {
|
|
181
201
|
return null;
|
|
@@ -186,9 +206,11 @@ class SimpleHtml extends PureComponent {
|
|
|
186
206
|
|
|
187
207
|
if (image && image.src) {
|
|
188
208
|
const source = { uri: image.src };
|
|
189
|
-
const
|
|
209
|
+
const imageSize = { width: image.width, height: image.height };
|
|
210
|
+
const { height, width } = resolveDimensions(imageSize, style);
|
|
211
|
+
const resolvedStyle = { alignSelf: 'center', height, width };
|
|
190
212
|
|
|
191
|
-
return <Image source={source} key={id} style={
|
|
213
|
+
return <Image source={source} key={id} style={resolvedStyle} />;
|
|
192
214
|
}
|
|
193
215
|
}
|
|
194
216
|
|
|
@@ -238,13 +260,7 @@ class SimpleHtml extends PureComponent {
|
|
|
238
260
|
render() {
|
|
239
261
|
const { style, body, customTagStyles, ...otherProps } = this.props;
|
|
240
262
|
|
|
241
|
-
|
|
242
|
-
// can have it's own padding. If it does, we have to include it in calculation,
|
|
243
|
-
// otherwise maxWidth will be more than max & images will go over the edge
|
|
244
|
-
const parentContainerPadding = style.outerPadding || 0;
|
|
245
|
-
const paddingValue =
|
|
246
|
-
_.get(style, 'container.paddingLeft') * 2 + parentContainerPadding;
|
|
247
|
-
const maxWidth = Dimensions.get('window').width - paddingValue;
|
|
263
|
+
const maxWidth = resolveMaxWidth(style);
|
|
248
264
|
|
|
249
265
|
const tagStyles = {
|
|
250
266
|
...style.tags,
|