Skip to content

Commit

Permalink
feat: add inline autoplay support
Browse files Browse the repository at this point in the history
add #play to enable video autoplay, similar to #loop and #loop
- add is() in setupTool to get
player property from hash
  • Loading branch information
aidenlx committed May 15, 2021
1 parent 078f807 commit 12ebe8f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/modules/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export async function handleMedia(
throw new TypeError("src not found on container <span>");
}

const { linktext, setPlayerTF, setHashOpt } = getSetupTool(srcLinktext);
const { is, linktext, setPlayerTF, setHashOpt } = getSetupTool(srcLinktext);

if (!(span.firstElementChild instanceof HTMLMediaElement)) {
console.error("first element not player: %o", span.firstElementChild);
Expand Down Expand Up @@ -194,7 +194,10 @@ export async function handleMedia(
ctx.addChild(new SubtitleResource(container, info?.objUrls ?? []));

if (info) info.tracks.forEach((t) => target.appendChild(t));
const player = new Plyr(target, defaultPlyrOption);
const player = new Plyr(target, {
...defaultPlyrOption,
autoplay: is("autoplay"),
});
setRatio(container, player);
setHashOpt(player);
setPlayerTF(player);
Expand Down
31 changes: 21 additions & 10 deletions src/modules/playerSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export const defaultPlyrOption = {
controls: defaultPlyrControls,
};

/** Player Properties that can be controlled by hash */
type PlayerProperties = "loop" | "muted" | "autoplay";

type setupTool = {
linktext: string;
timeSpan: TimeSpan | null;
loop: boolean;
muted: boolean;
is: (prop: PlayerProperties) => boolean;
setHashOpt: (player: Player) => void;
setPlayerTF: (player: Player) => void;
};
Expand All @@ -66,22 +68,31 @@ export function getSetupTool(src: string | URL): setupTool {
}

const timeSpan = parseTF(hash);
const hashQuery = parse(hash);
// null: exist, with no value (#loop)
const isLoop = parse(hash).loop === null;
const isMute = parse(hash).mute === null;

const hashOpts = new Map<string, PlayerProperties>([
["loop", "loop"],
["mute", "muted"],
["play", "autoplay"],
]);

return {
linktext,
timeSpan,
loop: isLoop,
muted: isMute,
is: (prop) => {
for (const [hash, key] of hashOpts) {
if (prop === key && hashQuery[hash] === null) return true;
}
return false;
},
setPlayerTF: (player) => {
if (timeSpan) injectTimestamp(player, timeSpan);
},
setHashOpt: (player) => {
if (isLoop) player.loop = isLoop;
if (isMute) player.muted = isMute;
},
setHashOpt: (player) =>
hashOpts.forEach((key, hash) => {
if (hashQuery[hash] === null) player[key] = true;
}),
};
}

Expand Down
5 changes: 2 additions & 3 deletions src/modules/videohost/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,19 @@ export function setupPlyr(container: HTMLDivElement, info: videoInfo): Plyr_TF {
);
plyrDiv.appendChild(iframe);

const { loop, timeSpan, setHashOpt, setPlayerTF } = getSetupTool(info.src);
const { is, timeSpan, setHashOpt, setPlayerTF } = getSetupTool(info.src);
let youtube: any;
if (info.host === Host.YouTube) {
youtube = {
// set start time
start: timeSpan && timeSpan.start !== 0 ? timeSpan.start : undefined,
loop: +loop,
};
}
// @ts-ignore
Plyr.timeSpan = null;
const player = new Plyr(plyrDiv, {
...defaultPlyrOption,
loop: { active: loop },
autoplay: is("autoplay"),
youtube,
}) as Plyr_TF;
setRatio(container, player);
Expand Down

0 comments on commit 12ebe8f

Please sign in to comment.