Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions assets/src/js/godam-player/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import PlayerManager from './managers/playerManager.js';
import { getLightbox, initLightboxUrlSync } from './managers/modalManager.js';
import { initLightboxTriggers, prepareTriggers } from './lightboxTriggers.js';
import {
findFirstInlineVideo,
findVideoById,
getLightboxRoot,
isLightboxVideo,
Expand Down Expand Up @@ -61,15 +62,29 @@ const initGodamPlayers = () => {
*
* The ID is resolved as a job ID first and an attachment ID second — see
* `findVideoById()`.
*
* A `?t=` with no `#godam-video-{id}` hash (for example a key-moment link
* whose fragment was dropped) seeks the first ordinary GoDAM video on the
* page instead.
*/
const applyDeepLink = () => {
const targetId = parseLightboxHash( window.location.hash );
const searchParams = new URLSearchParams( window.location.search );
const startTime = parseStartTime( searchParams.get( 't' ) );

let targetId = parseLightboxHash( window.location.hash );

if ( ! targetId ) {
return;
if ( startTime === null ) {
return;
}
const firstVideo = findFirstInlineVideo();
targetId = firstVideo?.dataset?.id || firstVideo?.dataset?.job_id || null;
if ( ! targetId ) {
return;
}
}

const searchParams = new URLSearchParams( window.location.search );
const startTime = parseStartTime( searchParams.get( 't' ) );
const fromHash = Boolean( parseLightboxHash( window.location.hash ) );

// Match on either ID, so a link built from an attachment ID works too.
const isTarget = ( videoElement ) =>
Expand All @@ -84,6 +99,12 @@ const applyDeepLink = () => {
return;
}

// Without a hash, only an inline player is seeked; a lightbox copy of the
// same video is left closed.
if ( ! fromHash && isLightboxVideo( videoElement ) ) {
return;
}

// This is our player – remove the listener so it only runs once.
document.removeEventListener( 'godamPlayerReady', onPlayerReady );

Expand Down
16 changes: 16 additions & 0 deletions assets/src/js/godam-player/utils/lightboxTargets.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ export function findVideoById( id, doc = document ) {
return null;
}

/**
* Find the first ordinary (non-lightbox) GoDAM video on the page.
*
* Used when a URL carries `?t=` but no `#godam-video-{id}` hash, so the start
* time still applies to the page's main video. Lightbox players are skipped:
* their inline render is only a poster, and opening an overlay for a link
* that never named a video would be surprising.
*
* @param {Document} doc - Document to search (defaults to `document`).
* @return {HTMLElement|null} The video element, or null when there is none.
*/
export function findFirstInlineVideo( doc = document ) {
const videos = doc.querySelectorAll( 'video[data-id], video[data-job_id]' );
return [ ...videos ].find( ( video ) => ! isLightboxVideo( video ) ) || null;
}

/**
* Get the element to move into the lightbox for a given video.
*
Expand Down
25 changes: 25 additions & 0 deletions assets/src/js/godam-player/utils/lightboxTargets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
buildEmbedUrl,
buildLightboxHash,
findFirstInlineVideo,
findVideoById,
getLightboxId,
getLightboxRoot,
Expand Down Expand Up @@ -292,3 +293,27 @@ describe( 'isLightboxVideo', () => {
expect( isLightboxVideo( undefined ) ).toBe( false );
} );
} );

describe( 'findFirstInlineVideo', () => {
afterEach( () => {
document.body.innerHTML = '';
} );

it( 'returns the first GoDAM video on the page', () => {
document.body.innerHTML =
'<video id="a" data-id="10"></video><video id="b" data-id="20"></video>';
expect( findFirstInlineVideo()?.id ).toBe( 'a' );
} );

it( 'skips lightbox players', () => {
document.body.innerHTML =
'<video id="a" data-id="10" data-show-in-lightbox="true"></video>' +
'<video id="b" data-job_id="job-2"></video>';
expect( findFirstInlineVideo()?.id ).toBe( 'b' );
} );

it( 'ignores videos that are not GoDAM players', () => {
document.body.innerHTML = '<video id="plain"></video>';
expect( findFirstInlineVideo() ).toBeNull();
} );
} );
Loading
Loading