Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: videojs/videojs-overlay
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.0.0
Choose a base ref
...
head repository: videojs/videojs-overlay
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.1.0
Choose a head ref
  • 2 commits
  • 7 files changed
  • 2 contributors

Commits on Jun 15, 2023

  1. feat: make overlays mutable (#223)

    * implement initial add/remove fns
    
    * add tests for add/remove fns
    
    * implement remove functionality with tests
    
    * update 'add' fn test
    
    * update docs to include add/remove fns and remove docs:toc stage
    
    * update index.html example
    
    * update readme toc
    
    * fix refactored docs
    
    * refactor overlays property to be a getter fn
    
    * refactor 'getOverlay' to 'get'
    
    * update readme toc
    
    * change example usage to use const instead of var
    
    ---------
    
    Co-authored-by: uomar <[email protected]>
    usmanonazim and usmanonazim authored Jun 15, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a6c0353 View commit details
  2. 3.1.0

    usmanonazim committed Jun 15, 2023
    Copy the full SHA
    2369b3e View commit details
Showing with 286 additions and 37 deletions.
  1. +7 −0 CHANGELOG.md
  2. +51 −0 README.md
  3. +5 −3 index.html
  4. +2 −2 package-lock.json
  5. +1 −2 package.json
  6. +93 −30 src/plugin.js
  7. +127 −0 test/plugin.test.js
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<a name="3.1.0"></a>
# [3.1.0](https://github.com/brightcove/videojs-overlay/compare/v3.0.0...v3.1.0) (2023-06-15)

### Features

* make overlays mutable (#223) ([a6c0353](https://github.com/brightcove/videojs-overlay/commit/a6c0353)), closes [#223](https://github.com/brightcove/videojs-overlay/issues/223)

<a name="3.0.0"></a>
# [3.0.0](https://github.com/brightcove/videojs-overlay/compare/v2.1.5...v3.0.0) (2022-12-16)

51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,6 +18,11 @@ Maintenance Status: Stable

- [Getting Started](#getting-started)
- [Documentation](#documentation)
- [API](#api)
- [`player.overlay()`](#playeroverlay)
- [`overlay.get()`](#overlayget)
- [`overlay.add(object|array)`](#overlayaddobjectarray)
- [`overlay.remove(object)`](#overlayremoveobject)
- [Plugin Options](#plugin-options)
- [`align`](#align)
- [`showBackground`](#showbackground)
@@ -45,6 +50,52 @@ There's also a [working example](https://github.com/brightcove/videojs-overlay/b

## Documentation

### API
#### `player.overlay()`
This is the main interface and the way to initialize this plugin. It takes [an options object as input](#plugin-options).

#### `overlay.get()`

Returns an array of all the overlays set up for the current video.

#### `overlay.add(Object|Array)`

Adds one or more overlays to the current list of overlays without replacing the current list of overlays.
Returns a reference to the added overlays.

```js
const overlay = player.overlay({
content: 'Default overlay content',
debug: true,
overlays: [{
content: 'The video is playing!',
start: 'play',
end: 'pause'
}]
});
const addedOverlays = overlay.add({content: "this is a new one", start: "play", end: "pause"});
```


#### `overlay.remove(Object)`

Removes an individual overlay from the list of overlays. Calling this method with an invalid overlay object removes nothing from the list.

```js
const overlay = player.overlay({
content: 'Default overlay content',
debug: true,
overlays: [{
content: 'The video is playing!',
start: 'play',
end: 'pause'
}]
});
const overlayToRemove = overlay.get()[0];
overlay.remove(overlayToRemove);
```


### Plugin Options

You may pass in an options object to the plugin upon initialization. This
8 changes: 5 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -18,14 +18,15 @@
<script src="dist/videojs-overlay.js"></script>
<script>
(function(window, videojs) {
var player = window.player = videojs('videojs-overlay-player');
player.overlay({
const player = window.player = videojs('videojs-overlay-player');
const overlay = player.overlay({
content: 'Default overlay content',
debug: true,
overlays: [{
content: 'The video is playing!',
start: 'play',
end: 'pause'
end: 'pause',
align: 'bottom-right'
}, {
start: 0,
end: 15,
@@ -43,6 +44,7 @@
end: 'pause'
}]
});
overlay.add({content: "this is a new one", start: "play", end: "pause"});
}(window, window.videojs));
</script>
</body>
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
"description": "A plugin to display simple overlays during video playback.",
"author": "Brightcove, Inc.",
"license": "Apache-2.0",
"version": "3.0.0",
"version": "3.1.0",
"main": "dist/videojs-overlay.cjs.js",
"module": "dist/videojs-overlay.es.js",
"keywords": [
@@ -89,7 +89,6 @@
"git add"
],
"README.md": [
"npm run docs:toc",
"git add"
]
},
123 changes: 93 additions & 30 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -36,8 +36,8 @@ const isNumber = n => typeof n === 'number' && n === n;
/**
* Whether a value is a string with no whitespace.
*
* @param {String} s
* @return {Boolean}
* @param {string} s
* @return {boolean}
*/
const hasNoWhitespace = s => typeof s === 'string' && (/^\S+$/).test(s);

@@ -116,6 +116,7 @@ class Overlay extends Component {

/**
* Logs debug errors
*
* @param {...[type]} args [description]
* @return {[type]} [description]
*/
@@ -160,11 +161,11 @@ class Overlay extends Component {
/**
* Determine whether or not the overlay should hide.
*
* @param {Number} time
* @param {number} time
* The current time reported by the player.
* @param {String} type
* @param {string} type
* An event type.
* @return {Boolean}
* @return {boolean}
*/
shouldHide_(time, type) {
const end = this.options_.end;
@@ -195,11 +196,11 @@ class Overlay extends Component {
/**
* Determine whether or not the overlay should show.
*
* @param {Number} time
* @param {number} time
* The current time reported by the player.
* @param {String} type
* @param {string} type
* An event type.
* @return {Boolean}
* @return {boolean}
*/
shouldShow_(time, type) {
const start = this.options_.start;
@@ -301,6 +302,7 @@ videojs.registerComponent('Overlay', Overlay);
* @param {Object} [options={}]
*/
const plugin = function(options) {
const player = this;
const settings = videojs.mergeOptions(defaults, options);

// De-initialize the plugin if it already has an array of overlays.
@@ -320,37 +322,98 @@ const plugin = function(options) {
// because it doesn't make sense to pass it to each Overlay component.
delete settings.overlays;

this.overlays_ = overlays.map(o => {
const mergeOptions = videojs.mergeOptions(settings, o);
const attachToControlBar = typeof mergeOptions.attachToControlBar === 'string' || mergeOptions.attachToControlBar === true;
const mapOverlays = (items) => {
return items.map(o => {
const mergeOptions = videojs.mergeOptions(settings, o);
const attachToControlBar = typeof mergeOptions.attachToControlBar === 'string' || mergeOptions.attachToControlBar === true;

if (!this.controls() || !this.controlBar) {
return this.addChild('overlay', mergeOptions);
}
if (!this.controls() || !this.controlBar) {
return this.addChild('overlay', mergeOptions);
}

if (attachToControlBar && mergeOptions.align.indexOf('bottom') !== -1) {
let referenceChild = this.controlBar.children()[0];
if (attachToControlBar && mergeOptions.align.indexOf('bottom') !== -1) {
let referenceChild = this.controlBar.children()[0];

if (this.controlBar.getChild(mergeOptions.attachToControlBar) !== undefined) {
referenceChild = this.controlBar.getChild(mergeOptions.attachToControlBar);
}
if (this.controlBar.getChild(mergeOptions.attachToControlBar) !== undefined) {
referenceChild = this.controlBar.getChild(mergeOptions.attachToControlBar);
}

if (referenceChild) {
const referenceChildIndex = this.controlBar.children().indexOf(referenceChild);
const controlBarChild = this.controlBar.addChild('overlay', mergeOptions, referenceChildIndex);
if (referenceChild) {
const referenceChildIndex = this.controlBar.children().indexOf(referenceChild);
const controlBarChild = this.controlBar.addChild('overlay', mergeOptions, referenceChildIndex);

return controlBarChild;
return controlBarChild;
}
}

const playerChild = this.addChild('overlay', mergeOptions);

this.el().insertBefore(
playerChild.el(),
this.controlBar.el()
);

return playerChild;
});
};

this.overlays_ = mapOverlays(overlays);

/**
* Adds one or more items to the existing list of overlays.
*
* @param {Object|Array} item
* An item (or an array of items) to be added as overlay/s
*
* @return {Array[Overlay]}
* The array of overlay objects that were added
*/
function add(item) {
if (!Array.isArray(item)) {
item = [item];
}

const playerChild = this.addChild('overlay', mergeOptions);
const addedOverlays = mapOverlays(item);

player.overlays_ = player.overlays_.concat(addedOverlays);

return addedOverlays;
}

/**
*
* @param {Overlay} item
* An item to be removed from the array of overlays
*
* @throws {Error}
* Item to remove must be present in the array of overlays
*
*/
function remove(item) {
const index = player.overlays_.indexOf(item);

if (index !== -1) {
item.el().parentNode.removeChild(item.el());
player.overlays_.splice(index, 1);
} else {
player.log.warn('overlay does not exist and cannot be removed');
}
}

/**
* Gets the array of overlays used for the current video
*
* @return The array of overlay objects currently used by the plugin
*/
function get() {
return player.overlays_;
}

this.el().insertBefore(
playerChild.el(),
this.controlBar.el()
);
return playerChild;
});
return {
add,
remove,
get
};
};

plugin.VERSION = VERSION;
Loading