Skip to content

Commit

Permalink
perf: lazy load interaction-areas plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tsi committed Jan 23, 2024
1 parent 1ec7f69 commit 03ec928
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/assets/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ $icon-font-path: '../icon-font' !default;
@import '~video.root.js/dist/video-js.min.css';
@import 'mixins/skin';
@import 'icons';
@import 'components/interaction-areas.scss';
@import 'components/loading-button.scss';
@import 'components/text-tracks.scss';

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import vttThumbnails from './vtt-thumbnails';
import playlist from './playlist';
import shoppable from './shoppable-plugin';
import styledTextTracks from './styled-text-tracks';
import interactionAreas from './interaction-areas';

const plugins = {
// #if (!process.env.WEBPACK_BUILD_LIGHT)
Expand All @@ -44,7 +45,8 @@ const plugins = {
// Lazy loaded plugins
playlist,
shoppable,
styledTextTracks
styledTextTracks,
interactionAreas
};

export default plugins;
8 changes: 8 additions & 0 deletions src/plugins/interaction-areas/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default async function lazyInteractionAreasPlugin(player, playerOptions, videojsOptions) {
try {
const { interactionAreasService } = await import(/* webpackChunkName: "interaction-area" */ './interaction-areas.service');
interactionAreasService(player, playerOptions, videojsOptions);
} catch (error) {
console.error('Failed to load plugin:', error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CLOSE_INTERACTION_AREA_LAYOUT_DELAY,
DEFAULT_INTERACTION_ARE_TRANSITION,
INTERACTION_AREAS_CONTAINER_CLASS_NAME, TEMPLATE_INTERACTION_AREAS_VTT
} from './interaction-area.const';
} from './interaction-areas.const';
import {
createInteractionAreaLayoutMessage,
getInteractionAreaItem,
Expand All @@ -13,16 +13,17 @@ import {
setInteractionAreasContainerSize,
shouldShowAreaLayoutMessage,
updateInteractionAreasItem
} from './interaction-area.utils';
} from './interaction-areas.utils';
import { addEventListener, createElement } from '../../utils/dom';
import { throttle } from '../../utils/throttle';
import { get } from '../../utils/object';
import { noop } from '../../utils/type-inference';
import { addMetadataTrack } from '../../video-player.utils';
import { PLAYER_EVENT } from '../../utils/consts';

import './interaction-areas.scss';

export const interactionAreaService = (player, playerOptions, videojsOptions) => {
export const interactionAreasService = (player, playerOptions, videojsOptions) => {

let isZoomed = false;
let currentSource = null;
Expand Down Expand Up @@ -240,8 +241,5 @@ export const interactionAreaService = (player, playerOptions, videojsOptions) =>
});
}

return {
init
};

init();
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
INTERACTION_AREAS_CONTAINER_CLASS_NAME,
INTERACTION_AREAS_PREFIX,
INTERACTION_AREAS_THEME
} from './interaction-area.const';
} from './interaction-areas.const';
import { noop } from '../../utils/type-inference';
import { getDefaultPlayerColor } from '../../plugins/colors';
import { getDefaultPlayerColor } from '../colors';
import { forEach, some } from '../../utils/array';
import { themedButton } from '../themeButton/themedButton';
import { BUTTON_THEME } from '../themeButton/themedButton.const';
import { themedButton } from '../../components/themeButton/themedButton';
import { BUTTON_THEME } from '../../components/themeButton/themedButton.const';


const getInteractionAreaItemId = (item, index) => item.id || item.type || `id_${index}`;
Expand Down
2 changes: 1 addition & 1 deletion src/validators/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ADS_IN_PLAYLIST, AUTO_PLAY_MODE, FLOATING_TO } from '../video-player.co
import {
INTERACTION_AREAS_TEMPLATE,
INTERACTION_AREAS_THEME
} from '../components/interaction-area/interaction-area.const';
} from '../plugins/interaction-areas/interaction-areas.const';
import { validator } from './validators-types';

export const playerValidators = {
Expand Down
18 changes: 9 additions & 9 deletions src/video-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { getAnalyticsFromPlayerOptions } from './utils/get-analytics-player-opti
import { extendCloudinaryConfig, normalizeOptions, isRawUrl } from './plugins/cloudinary/common';
// #if (!process.env.WEBPACK_BUILD_LIGHT)
import qualitySelector from './components/qualitySelector/qualitySelector.js';
import { interactionAreaService } from './components/interaction-area/interaction-area.service';
// #endif

const INTERNAL_ANALYTICS_URL = 'https://analytics-api-s.cloudinary.com';
Expand Down Expand Up @@ -97,10 +96,6 @@ class VideoPlayer extends Utils.mixin(Eventable) {
this.fluid(this.playerOptions.fluid);
}

// #if (!process.env.WEBPACK_BUILD_LIGHT)
this.interactionArea = interactionAreaService(this, this.playerOptions, this._videojsOptions);
// #endif

this._setCssClasses();
this._initPlugins();
this._initJumpButtons();
Expand Down Expand Up @@ -178,10 +173,6 @@ class VideoPlayer extends Utils.mixin(Eventable) {
if (ready) {
ready(this);
}

// #if (!process.env.WEBPACK_BUILD_LIGHT)
this.interactionArea.init();
// #endif
});

}
Expand All @@ -202,6 +193,7 @@ class VideoPlayer extends Utils.mixin(Eventable) {
this._initHighlightsGraph();
this._initSeekThumbs();
this._initChapters();
this._initInteractionAreas();
}

_isFullScreen() {
Expand Down Expand Up @@ -328,6 +320,14 @@ class VideoPlayer extends Utils.mixin(Eventable) {
});
}

_initInteractionAreas() {
this.videojs.on(PLAYER_EVENT.READY, async () => {
if (this.options.videojsOptions.interactionDisplay && this.videojs.interactionAreas) {
this.videojs.interactionAreas(this, this.playerOptions, this._videojsOptions);
}
});
}

_initColors () {
this.videojs.colors(this.playerOptions.colors ? { colors: this.playerOptions.colors } : {});
}
Expand Down

0 comments on commit 03ec928

Please sign in to comment.