Skip to content

Commit 1239e4d

Browse files
authored
added forceLoad option for image (#8260)
* added forceLoad option for image * updated json * refactored changes & added reset method
1 parent 904a4d1 commit 1239e4d

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "AdaptiveCard",
3+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
4+
"version": "1.6",
5+
"body": [
6+
{
7+
"type": "Image",
8+
"size": "Medium",
9+
"url": "https://media.giphy.com/media/83eQIMgNvkiY/giphy.gif",
10+
"horizontalAlignment": "Center"
11+
},
12+
{
13+
"type": "Image",
14+
"size": "Medium",
15+
"url": "https://media.giphy.com/media/83eQIMgNvkiY/giphy.gif",
16+
"forceLoad": true,
17+
"horizontalAlignment": "Center"
18+
}
19+
]
20+
}

source/nodejs/adaptivecards/src/card-elements.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,7 @@ export class Image extends CardElement {
19481948
static readonly selectActionProperty = new ActionProperty(Versions.v1_1, "selectAction", [
19491949
"Action.ShowCard"
19501950
]);
1951+
static readonly shouldForceLoadProperty = new BoolProperty(Versions.v1_6, "forceLoad", false);
19511952

19521953
protected populateSchema(schema: SerializableObjectSchema) {
19531954
super.populateSchema(schema);
@@ -1981,6 +1982,9 @@ export class Image extends CardElement {
19811982
@property(Image.selectActionProperty)
19821983
selectAction?: Action;
19831984

1985+
@property(Image.shouldForceLoadProperty)
1986+
forceLoad: boolean;
1987+
19841988
//#endregion
19851989

19861990
private applySize(element: HTMLElement) {
@@ -2140,7 +2144,7 @@ export class Image extends CardElement {
21402144
imageElement.style.backgroundColor = backgroundColor;
21412145
}
21422146

2143-
imageElement.src = <string>this.preProcessPropertyValue(Image.urlProperty);
2147+
this.setImageSource(imageElement);
21442148

21452149
const altTextProperty = this.preProcessPropertyValue(Image.altTextProperty);
21462150
if (altTextProperty) {
@@ -2188,6 +2192,45 @@ export class Image extends CardElement {
21882192
getResourceInformation(): IResourceInformation[] {
21892193
return this.url ? [{ url: this.url, mimeType: "image" }] : [];
21902194
}
2195+
2196+
private setImageSource(imageElement: HTMLImageElement): void {
2197+
const imageForceLoader: ImageForceLoader = new ImageForceLoader(this.forceLoad, this.url);
2198+
imageForceLoader.configureImage(this);
2199+
imageElement.src = <string>this.preProcessPropertyValue(Image.urlProperty);
2200+
imageForceLoader.resetImage(this);
2201+
}
2202+
}
2203+
2204+
// configures Image element to fetch a new image data from url source instead of relying on cache
2205+
// currently rudimentary refreshing scheme is used
2206+
// by attaching unique query string to url, we bypass the cache usage
2207+
class ImageForceLoader{
2208+
private uniqueHash : string;
2209+
public readonly urlWithForceLoadOption : string;
2210+
constructor(
2211+
readonly doForceLoad: boolean,
2212+
readonly url : string | undefined,
2213+
)
2214+
{
2215+
if (url && url.length && doForceLoad) {
2216+
// we can do better by appending unique key such as uuid instead of epoch
2217+
// however the current usage is for front-end ui and networking,
2218+
// since ui is running in single main thread, this is sufficient mechanism
2219+
// without needing to depend on external library for our small use cases.
2220+
this.uniqueHash = '?' + Date.now();
2221+
this.urlWithForceLoadOption = url + this.uniqueHash;
2222+
}
2223+
}
2224+
2225+
public configureImage(image: Image): void {
2226+
if (this.urlWithForceLoadOption && this.urlWithForceLoadOption.length) {
2227+
image.url = this.urlWithForceLoadOption;
2228+
}
2229+
}
2230+
2231+
public resetImage(image: Image): void {
2232+
image.url = this.url;
2233+
}
21912234
}
21922235

21932236
export abstract class CardElementContainer extends CardElement {

0 commit comments

Comments
 (0)