Skip to content

Commit f331b38

Browse files
PB-265: Video background for Banner
- fix video reloading for any changes in content type
1 parent f047afc commit f331b38

File tree

5 files changed

+127
-62
lines changed

5 files changed

+127
-62
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/banner/preview.js

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

app/code/Magento/PageBuilder/view/adminhtml/web/js/data-store.js

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

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/banner/preview.ts

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import "jarallaxVideo";
88
import $ from "jquery";
99
import $t from "mage/translate";
1010
import events from "Magento_PageBuilder/js/events";
11+
import mageUtils from "mageUtils";
1112
import _ from "underscore";
1213
import "vimeoWrapper";
13-
import ContentTypeConfigInterface from "../../content-type-config.types";
1414
import HideShowOption from "../../content-type-menu/hide-show-option";
1515
import {OptionsInterface} from "../../content-type-menu/option.types";
16-
import ContentTypeInterface from "../../content-type.types";
16+
import {DataObject} from "../../data-store";
1717
import Uploader from "../../uploader";
1818
import delayUntil from "../../utils/delay-until";
1919
import {
@@ -27,8 +27,7 @@ import {
2727
import nestingLinkDialog from "../../utils/nesting-link-dialog";
2828
import WysiwygFactory from "../../wysiwyg/factory";
2929
import WysiwygInterface from "../../wysiwyg/wysiwyg-interface";
30-
import {ContentTypeMountEventParamsInterface, ContentTypeReadyEventParamsInterface} from "../content-type-events.types";
31-
import ObservableUpdater from "../observable-updater";
30+
import {ContentTypeReadyEventParamsInterface} from "../content-type-events.types";
3231
import BasePreview from "../preview";
3332

3433
/**
@@ -67,6 +66,18 @@ export default class Preview extends BasePreview {
6766
*/
6867
private handledDoubleClick: boolean = false;
6968

69+
/**
70+
* Properties that cause rebuilding video background
71+
*/
72+
private videoUpdateProperties = [
73+
"background_type",
74+
"video_fallback_image",
75+
"video_lazy_load",
76+
"video_loop",
77+
"video_play_only_visible",
78+
"video_source",
79+
];
80+
7081
/**
7182
* Debounce and defer the init of Jarallax
7283
*
@@ -110,26 +121,6 @@ export default class Preview extends BasePreview {
110121

111122
}, 50);
112123

113-
/**
114-
* @param {ContentTypeInterface} contentType
115-
* @param {ContentTypeConfigInterface} config
116-
* @param {ObservableUpdater} observableUpdater
117-
*/
118-
constructor(
119-
contentType: ContentTypeInterface,
120-
config: ContentTypeConfigInterface,
121-
observableUpdater: ObservableUpdater,
122-
) {
123-
super(contentType, config, observableUpdater);
124-
125-
this.contentType.dataStore.subscribe(this.buildJarallax);
126-
events.on("banner:mountAfter", (args: ContentTypeReadyEventParamsInterface) => {
127-
if (args.id === this.contentType.id) {
128-
this.buildJarallax();
129-
}
130-
});
131-
}
132-
133124
/**
134125
* Return an array of options
135126
*
@@ -459,7 +450,11 @@ export default class Preview extends BasePreview {
459450
*/
460451
protected bindEvents() {
461452
super.bindEvents();
462-
453+
events.on("banner:mountAfter", (args: ContentTypeReadyEventParamsInterface) => {
454+
if (args.id === this.contentType.id) {
455+
this.buildJarallax();
456+
}
457+
});
463458
events.on(`${this.config.name}:${this.contentType.id}:updateAfter`, () => {
464459
const dataStore = this.contentType.dataStore.getState();
465460
const imageObject = (dataStore[this.config.additional_data.uploaderConfig.dataScope] as object[])[0] || {};
@@ -470,6 +465,14 @@ export default class Preview extends BasePreview {
470465
events.trigger(`image:${this.contentType.id}:assignAfter`, imageObject);
471466
nestingLinkDialog(this.contentType.dataStore, this.wysiwyg, "message", "link_url");
472467
});
468+
this.contentType.dataStore.subscribe(function(data: DataObject) {
469+
if (this.isVideoShouldBeUpdated(data)) {
470+
this.buildJarallax();
471+
}
472+
}.bind(this));
473+
events.on(`image:${this.contentType.id}:uploadAfter`, () => {
474+
this.contentType.dataStore.set("background_type", "image");
475+
});
473476
}
474477

475478
/**
@@ -487,4 +490,25 @@ export default class Preview extends BasePreview {
487490

488491
$(this.textarea).height(scrollHeight);
489492
}
493+
494+
/**
495+
* Adjust textarea's height based on scrollHeight
496+
*
497+
* @return boolean
498+
*/
499+
private isVideoShouldBeUpdated(state: DataObject): boolean {
500+
const previousState = this.contentType.dataStore.previousState;
501+
const diff = mageUtils.compare(previousState, state).changes;
502+
503+
if (diff.length > 0) {
504+
return _.some(diff, (element) => {
505+
if (element.name === "video_fallback_image") {
506+
return (!_.isEmpty(previousState.video_fallback_image) && previousState.video_fallback_image) !==
507+
(!_.isEmpty(state.video_fallback_image) && state.video_fallback_image);
508+
}
509+
510+
return this.videoUpdateProperties.indexOf(element.name) !== -1;
511+
});
512+
}
513+
}
490514
}

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/data-store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export interface DataObject<T = any> {
1515
}
1616

1717
export default class DataStore {
18+
public previousState: DataObject = {};
1819
private state: DataObject = {};
1920
private events: JQuery = $({});
20-
private previousState: DataObject = {};
2121

2222
/**
2323
* Retrieve specific data from the data store
@@ -46,6 +46,7 @@ export default class DataStore {
4646
* @param value
4747
*/
4848
public set(key: string, value: any) {
49+
this.previousState = Object.assign({}, this.state);
4950
set(this.state, key, value);
5051
this.emitState();
5152
}
@@ -56,6 +57,7 @@ export default class DataStore {
5657
* @param {DataObject} state
5758
*/
5859
public setState(state: DataObject) {
60+
this.previousState = Object.assign({}, this.state);
5961
this.state = state;
6062
this.emitState();
6163
}

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/types/mageUtils.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
* See COPYING.txt for license details.
44
*/
55

6+
interface CompareObject {
7+
containers: object;
8+
changes: CompareObjectItem[];
9+
equal: boolean;
10+
}
11+
12+
interface CompareObjectItem {
13+
path: string;
14+
name: string;
15+
value: any;
16+
oldValue: any;
17+
}
18+
619
declare let mageUtils: {
720
/**
821
* Generates a unique identifier.
@@ -18,6 +31,14 @@ declare let mageUtils: {
1831
* @returns {Object|Array} Extended object.
1932
*/
2033
extend(...args: any[]): object;
34+
35+
/**
36+
* Compare two objects
37+
* @param args
38+
*
39+
* @return {CompareObject}.
40+
*/
41+
compare(...args: any[]): CompareObject;
2142
};
2243
declare module "mageUtils" {
2344
export = mageUtils;

0 commit comments

Comments
 (0)