Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let user interact with core player from react player #276

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions packages/2d/src/lib/scenes/Scene2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
}

public inspectPosition(x: number, y: number): InspectedElement | null {
return this.execute(
() => this.getView().hit(new Vector2(x, y))?.key ?? null,
);
const node = this.getNodeByPosition(x, y);
return node?.key;
}

public getNodeByPosition(x: number, y: number): Node | null {
hkonsti marked this conversation as resolved.
Show resolved Hide resolved
return this.execute(() => this.getView().hit(new Vector2(x, y)) ?? null);
}

public validateInspection(
Expand Down
56 changes: 53 additions & 3 deletions packages/player-react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';
import {ComponentProps, useEffect, useRef, useState} from 'react';
import {Player as CorePlayer} from '@revideo/core';
import {ComponentProps, useCallback, useEffect, useRef, useState} from 'react';
import {Controls} from './controls';
import './styles.css';
import {shouldShowControls} from './utils';
Expand Down Expand Up @@ -41,6 +42,8 @@ interface PlayerProps {

onDurationChange?: (duration: number) => void;
onTimeUpdate?: (currentTime: number) => void;
onPlayerReady?: (player: CorePlayer) => void;
onPlayerResize?: (rect: DOMRectReadOnly) => void;
}

export function Player({
Expand All @@ -60,14 +63,20 @@ export function Player({

onDurationChange = () => {},
onTimeUpdate = () => {},
onPlayerReady = () => {},
onPlayerResize = () => {},
}: PlayerProps) {
const [playingState, setPlaying] = useState(playing);
const [isMouseOver, setIsMouseOver] = useState(false);
const [currentTimeState, setCurrentTime] = useState(currentTime);
const [duration, setDuration] = useState(-1);

const focus = useRef(false);
const playerRef = useRef<HTMLDivElement>(null);
const playerRef = useRef<HTMLDivElement | null>(null);
const wrapperRef = useRef<HTMLDivElement | null>(null);
const lastRect = useRef<DOMRectReadOnly | null>(null);

const onClickHandler = controls ? () => setPlaying(prev => !prev) : undefined;

/**
* Sync the playing prop with the player's own state when it changes.
Expand Down Expand Up @@ -114,6 +123,44 @@ export function Player({
}
};

const handlePlayerReady = (event: Event) => {
const player = (event as CustomEvent).detail;
if (player) {
onPlayerReady(player);
}
};

const handlePlayerResize = useCallback(
justusmattern27 marked this conversation as resolved.
Show resolved Hide resolved
(entries: ResizeObserverEntry[]) => {
const [firstEntry] = entries;
if (!firstEntry || !wrapperRef.current) {
return;
}

const newRect = firstEntry.contentRect;
const sameWidth = newRect.width === lastRect.current.width;
const sameHeight = newRect.height === lastRect.current.height;
if (lastRect.current && sameWidth && sameHeight) {
return;
}

lastRect.current = newRect;
onPlayerResize(newRect);
},
[onPlayerResize],
);

useEffect(() => {
if (!wrapperRef.current) return;

const resizeObserver = new ResizeObserver(handlePlayerResize);
resizeObserver.observe(wrapperRef.current);

return () => {
resizeObserver.disconnect();
};
}, [handlePlayerResize]);

/**
* Import the player and add all event listeners.
*/
Expand All @@ -122,11 +169,13 @@ export function Player({

playerRef.current?.addEventListener('timeupdate', handleTimeUpdate);
playerRef.current?.addEventListener('duration', handleDurationUpdate);
playerRef.current?.addEventListener('playerready', handlePlayerReady);
document.addEventListener('keydown', handleKeyDown);

return () => {
playerRef.current?.removeEventListener('timeupdate', handleTimeUpdate);
playerRef.current?.removeEventListener('duration', handleDurationUpdate);
playerRef.current?.removeEventListener('playerready', handlePlayerReady);
document.removeEventListener('keydown', handleKeyDown);
const frameElement = document.getElementById('revideo-2d-frame');
if (frameElement) {
Expand All @@ -149,6 +198,7 @@ export function Player({
return (
<div data-player="true" style={{display: 'contents'}}>
<div
ref={wrapperRef}
className="p-relative p-cursor-default p-focus:outline-none"
onFocus={() => (focus.current = true)}
onBlur={() => (focus.current = false)}
Expand All @@ -161,7 +211,7 @@ export function Player({
ref={playerRef}
src={src}
playing={String(playingState)}
onClick={() => setPlaying(prev => !prev)}
onClick={onClickHandler}
variables={JSON.stringify(variables)}
looping={looping ? 'true' : 'false'}
width={width}
Expand Down
1 change: 1 addition & 0 deletions packages/player-react/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class RevideoPlayer extends HTMLElement {
this.player.togglePlayback(this.playing);

this.setState(State.Ready);
this.dispatchEvent(new CustomEvent('playerready', {detail: this.player}));
}

public attributeChangedCallback(name: string, _: any, newValue: any) {
Expand Down
Loading