Skip to content

Commit 4cefd6f

Browse files
authored
Merge pull request #392 from GetStream/update-css
feat: Match Attachment sizing logic with React
2 parents df90dfc + 43aae1d commit 4cefd6f

File tree

8 files changed

+181
-90
lines changed

8 files changed

+181
-90
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"@ngx-translate/core": "^13.0.0",
118118
"@ngx-translate/http-loader": "^6.0.0",
119119
"@popperjs/core": "^2.11.5",
120-
"@stream-io/stream-chat-css": "3.3.0",
120+
"@stream-io/stream-chat-css": "3.5.4",
121121
"@stream-io/transliterate": "^1.5.2",
122122
"angular-mentions": "^1.4.0",
123123
"dayjs": "^1.10.7",

projects/stream-chat-angular/src/lib/attachment-configuration.service.spec.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,37 @@ describe('AttachmentConfigurationService', () => {
3131
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
3232
).toEqual({
3333
url: 'http://url/to/img?h=600&w=600',
34-
height: '300px',
34+
height: '',
3535
width: '',
36+
originalHeight: 1000000,
37+
originalWidth: 1000000,
3638
});
3739

3840
attachment = {
39-
thumb_url: 'http://url/to/img',
41+
thumb_url: 'http://url/to/img?oh=1200&ow=800',
4042
};
4143

4244
expect(
4345
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
4446
).toEqual({
45-
url: 'http://url/to/img?h=600&w=600',
46-
height: '300px',
47+
url: 'http://url/to/img?oh=1200&ow=800&h=900&w=600',
48+
height: '',
4749
width: '',
50+
originalHeight: 1200,
51+
originalWidth: 800,
4852
});
4953

5054
attachment = {
51-
image_url: 'http://url/to/img',
55+
image_url: 'http://url/to/img?oh=1&ow=1',
5256
};
5357

5458
expect(
5559
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
5660
).toEqual({
57-
url: 'http://url/to/img?h=600&w=600',
58-
height: '300px',
61+
url: 'http://url/to/img?oh=1&ow=1&h=600&w=600',
62+
originalHeight: 1000000,
63+
originalWidth: 1000000,
64+
height: '',
5965
width: '',
6066
});
6167
});
@@ -99,6 +105,8 @@ describe('AttachmentConfigurationService', () => {
99105
url: 'http://url/to/img?h=600&w=600',
100106
height: '',
101107
width: '',
108+
originalHeight: 1000000,
109+
originalWidth: 1000000,
102110
});
103111
});
104112

@@ -121,6 +129,8 @@ describe('AttachmentConfigurationService', () => {
121129
url: 'http://url/to/img',
122130
height: '',
123131
width: '',
132+
originalHeight: 1000000,
133+
originalWidth: 1000000,
124134
});
125135
});
126136

@@ -138,9 +148,11 @@ describe('AttachmentConfigurationService', () => {
138148
service.getVideoAttachmentConfiguration(attachment, htmlElement)
139149
).toEqual({
140150
url: 'http://url/to/video',
141-
height: '300px',
151+
height: '',
142152
width: '',
143153
thumbUrl: 'http://url/to/poster?h=600&w=600',
154+
originalHeight: 1000000,
155+
originalWidth: 1000000,
144156
});
145157

146158
attachment = {
@@ -152,9 +164,11 @@ describe('AttachmentConfigurationService', () => {
152164
service.getVideoAttachmentConfiguration(attachment, htmlElement)
153165
).toEqual({
154166
url: 'http://url/to/video',
155-
height: '169px',
167+
height: '',
156168
width: '',
157169
thumbUrl: 'http://url/to/poster?oh=1080&ow=1920&h=600&w=1066',
170+
originalHeight: 1080,
171+
originalWidth: 1920,
158172
});
159173

160174
attachment = {
@@ -165,9 +179,11 @@ describe('AttachmentConfigurationService', () => {
165179
service.getVideoAttachmentConfiguration(attachment, htmlElement)
166180
).toEqual({
167181
url: 'http://url/to/video',
168-
height: '300px',
182+
height: '',
169183
width: '',
170184
thumbUrl: undefined,
185+
originalHeight: 1000000,
186+
originalWidth: 1000000,
171187
});
172188
});
173189

@@ -309,28 +325,6 @@ describe('AttachmentConfigurationService', () => {
309325
).toBeUndefined();
310326
});
311327

312-
it('should use original dimensions if image is smaller than max-width and max-height', () => {
313-
const attachment: Attachment = {
314-
image_url: 'http://url/to/img?ow=200&oh=100',
315-
};
316-
const htmlElement = {
317-
'max-width': '300px',
318-
'max-height': '300px',
319-
} as any as HTMLElement;
320-
321-
expect(
322-
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
323-
.height
324-
).toBe('100px');
325-
326-
attachment.image_url = attachment.image_url!.replace('ow=200', 'ow=400');
327-
328-
expect(
329-
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
330-
.height
331-
).toBe('75px');
332-
});
333-
334328
it('should override existing "h" and "w" URL params', () => {
335329
const attachment: Attachment = {
336330
image_url: 'http://url/to/img?crop=*&h=*&oh=0&ow=0&resize=*&ro=0&w=*',

projects/stream-chat-angular/src/lib/attachment-configuration.service.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Attachment } from 'stream-chat';
33
import {
44
AttachmentConfigration,
55
DefaultStreamChatGenerics,
6+
ImageAttachmentConfiguration,
67
VideoAttachmentConfiguration,
78
} from './types';
89

@@ -22,7 +23,7 @@ export class AttachmentConfigurationService<
2223
a: Attachment<T>,
2324
type: 'gallery' | 'single' | 'carousel',
2425
containerElement: HTMLElement
25-
) => AttachmentConfigration;
26+
) => ImageAttachmentConfiguration;
2627
/**
2728
* A custom handler can be provided to override the default video attachment (videos uploaded from files) configuration. By default the SDK uses fixed height (a size that's known before video is loaded), if you override that with dynamic height (for example: height: 100%) the scrolling logic inside the message list can break.
2829
*/
@@ -57,7 +58,7 @@ export class AttachmentConfigurationService<
5758
attachment: Attachment<T>,
5859
location: 'gallery' | 'single' | 'carousel',
5960
element: HTMLElement
60-
): AttachmentConfigration {
61+
): ImageAttachmentConfiguration {
6162
if (this.customImageAttachmentConfigurationHandler) {
6263
return this.customImageAttachmentConfigurationHandler(
6364
attachment,
@@ -72,8 +73,16 @@ export class AttachmentConfigurationService<
7273
attachment.image_url ||
7374
'') as string
7475
);
76+
const originalHeight =
77+
Number(url.searchParams.get('oh')) > 1
78+
? Number(url.searchParams.get('oh'))
79+
: 1000000;
80+
const originalWidth =
81+
Number(url.searchParams.get('ow')) > 1
82+
? Number(url.searchParams.get('ow'))
83+
: 1000000;
7584
const displayWarning = location === 'gallery' || location === 'single';
76-
const { sizeRestriction, height } = this.getSizingRestrictions(
85+
const sizeRestriction = this.getSizingRestrictions(
7786
url,
7887
element,
7988
displayWarning
@@ -89,7 +98,9 @@ export class AttachmentConfigurationService<
8998
return {
9099
url: url.href,
91100
width: '', // Not set to respect responsive width
92-
height,
101+
height: '', // Set from CSS
102+
originalHeight,
103+
originalWidth,
93104
};
94105
}
95106

@@ -109,12 +120,21 @@ export class AttachmentConfigurationService<
109120
);
110121
}
111122

112-
let attachmentHeight = ``;
113123
let thumbUrl = undefined;
124+
let originalHeight = 1000000;
125+
let originalWidth = 1000000;
114126
if (attachment.thumb_url && this.shouldGenerateVideoThumbnail) {
115127
const url = new URL(attachment.thumb_url);
128+
originalHeight =
129+
Number(url.searchParams.get('oh')) > 1
130+
? Number(url.searchParams.get('oh'))
131+
: originalHeight;
132+
originalWidth =
133+
Number(url.searchParams.get('ow')) > 1
134+
? Number(url.searchParams.get('ow'))
135+
: originalWidth;
116136
const displayWarning = true;
117-
const { sizeRestriction, height } = this.getSizingRestrictions(
137+
const sizeRestriction = this.getSizingRestrictions(
118138
url,
119139
element,
120140
displayWarning
@@ -126,18 +146,14 @@ export class AttachmentConfigurationService<
126146
this.addResizingParamsToUrl(sizeRestriction, url);
127147
}
128148
thumbUrl = url.href;
129-
attachmentHeight = height;
130-
} else {
131-
const cssSizeRestriction = this.getCSSSizeRestriction(element);
132-
attachmentHeight = `${
133-
cssSizeRestriction.maxHeight || cssSizeRestriction.height || ''
134-
}px`;
135149
}
136150
return {
137151
url: attachment.asset_url || '',
138152
width: '', // Not set to respect responsive width
139-
height: attachmentHeight,
153+
height: '', // Set from CSS
140154
thumbUrl: thumbUrl,
155+
originalHeight,
156+
originalWidth,
141157
};
142158
}
143159

@@ -197,7 +213,6 @@ export class AttachmentConfigurationService<
197213
const originalWidth = Number(urlParams.get('ow')) || 1;
198214
const cssSizeRestriction = this.getCSSSizeRestriction(htmlElement);
199215
let sizeRestriction: { width: number; height: number } | undefined;
200-
let height = '';
201216

202217
if (
203218
(cssSizeRestriction.maxHeight || cssSizeRestriction.height) &&
@@ -209,22 +224,6 @@ export class AttachmentConfigurationService<
209224
(cssSizeRestriction.maxHeight || cssSizeRestriction.height)!,
210225
cssSizeRestriction.maxWidth
211226
);
212-
if (cssSizeRestriction.maxHeight) {
213-
const heightNum =
214-
originalHeight > 1 && originalWidth > 1
215-
? originalHeight <= cssSizeRestriction.maxHeight &&
216-
originalWidth <= cssSizeRestriction.maxWidth
217-
? originalHeight
218-
: Math.round(
219-
Math.min(
220-
cssSizeRestriction.maxHeight,
221-
(cssSizeRestriction.maxWidth / originalWidth) *
222-
originalHeight
223-
)
224-
)
225-
: cssSizeRestriction.maxHeight;
226-
height = `${heightNum}px`;
227-
}
228227
} else {
229228
sizeRestriction = undefined;
230229
if (displayWarning) {
@@ -234,7 +233,7 @@ export class AttachmentConfigurationService<
234233
}
235234
}
236235

237-
return { sizeRestriction, height };
236+
return sizeRestriction;
238237
}
239238

240239
private getSizeRestrictions(

0 commit comments

Comments
 (0)