Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
fix: merge utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Jan 21, 2023
1 parent e834e4d commit f5e0588
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 159 deletions.
152 changes: 0 additions & 152 deletions src/utils.js

This file was deleted.

98 changes: 91 additions & 7 deletions src/vimeo-video-element.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
// https://github.com/vimeo/player.js
import VimeoPlayerAPI from '@vimeo/player';
import {
PublicPromise,
serialize,
boolToBinary,
createEmbedIframe,
createTimeRanges,
} from './utils.js';

const EMBED_BASE = 'https://player.vimeo.com/video';
const MATCH_SRC = /vimeo\.com\/(?:video\/)?(\d+)/;
Expand Down Expand Up @@ -431,6 +424,97 @@ class VimeoVideoElement extends HTMLElement {
}
}

/**
* A utility to create Promises with convenient public resolve and reject methods.
* @return {Promise}
*/
class PublicPromise extends Promise {
constructor(executor = () => {}) {
let res, rej;
super((resolve, reject) => {
executor(resolve, reject);
res = resolve;
rej = reject;
});
this.resolve = res;
this.reject = rej;
}
}

function createElement(tag, attrs = {}, ...children) {
const el = document.createElement(tag);
Object.keys(attrs).forEach(
(name) => attrs[name] != null && el.setAttribute(name, attrs[name])
);
el.append(...children);
return el;
}

const allow =
'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture';

function createEmbedIframe({ src, ...props }) {
return createElement('iframe', {
src,
width: '100%',
height: '100%',
allow,
allowfullscreen: '',
frameborder: 0,
...props,
});
}

function serialize(props) {
return Object.keys(props)
.map((key) => {
if (props[key] == null) return '';
return `${key}=${encodeURIComponent(props[key])}`;
})
.join('&');
}

function boolToBinary(props) {
let p = { ...props };
for (let key in p) {
if (p[key] === false) p[key] = 0;
else if (p[key] === true) p[key] = 1;
}
return p;
}

/**
* Creates a fake `TimeRanges` object.
*
* A TimeRanges object. This object is normalized, which means that ranges are
* ordered, don't overlap, aren't empty, and don't touch (adjacent ranges are
* folded into one bigger range).
*
* @param {(Number|Array)} Start of a single range or an array of ranges
* @param {Number} End of a single range
* @return {Array}
*/
function createTimeRanges(start, end) {
if (Array.isArray(start)) {
return createTimeRangesObj(start);
} else if (start == null || end == null || (start === 0 && end === 0)) {
return createTimeRangesObj([[0, 0]]);
}
return createTimeRangesObj([[start, end]]);
}

function createTimeRangesObj(ranges) {
Object.defineProperties(ranges, {
start: {
value: i => ranges[i][0]
},
end: {
value: i => ranges[i][1]
}
});
return ranges;
}

if (!globalThis.customElements.get('vimeo-video')) {
globalThis.customElements.define('vimeo-video', VimeoVideoElement);
}
Expand Down

0 comments on commit f5e0588

Please sign in to comment.