From 639ab0ae17e239a7c1919e154fd83de7cd3d4bf4 Mon Sep 17 00:00:00 2001 From: Ed Asriyan Date: Tue, 20 Feb 2024 00:47:31 +0400 Subject: [PATCH] Support local files --- src/local-room.ts | 3 +- src/remote-room.ts | 2 ++ src/routes/+page.svelte | 42 +++++++++++++++++++++++--- src/routes/video-selector.svelte | 31 +++++++++++++++++++ src/routes/video-view.svelte | 52 +++++++++++++++++++++++--------- 5 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 src/routes/video-selector.svelte diff --git a/src/local-room.ts b/src/local-room.ts index 932100f..eaba0ef 100644 --- a/src/local-room.ts +++ b/src/local-room.ts @@ -19,7 +19,7 @@ export class LocalRoom implements Writable { if (!newTime || Math.abs(newRemoteRoom.time - newTime) > maximumDelta) { newTime = newRemoteRoom.time; } - set({ url: newRemoteRoom.url, paused: newRemoteRoom.paused, time: newTime }); + set({ ...newRemoteRoom, time: newTime }); }); }); } @@ -34,6 +34,7 @@ export class LocalRoom implements Writable { const remoteValue = getStore(this.remoteRoom); if ( + remoteValue.isLocalMode !== newValue.isLocalMode || remoteValue.paused !== newValue.paused || remoteValue.url !== newValue.url || Math.abs(getStore(this.remoteRoom).time - val.time) > syncInterval diff --git a/src/remote-room.ts b/src/remote-room.ts index 0095ce6..e5f5935 100644 --- a/src/remote-room.ts +++ b/src/remote-room.ts @@ -6,6 +6,7 @@ export interface RemoteRoomRaw { url: string; time: number; paused: boolean; + isLocalMode: boolean; } export class RemoteRoom implements Writable { @@ -31,6 +32,7 @@ export class RemoteRoom implements Writable { url: '', paused: true, time: 0, + isLocalMode: false, }; } this.store.set(initRoom); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 000ed5f..881a61b 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -5,6 +5,7 @@ import { v4 as uuidv4 } from 'uuid'; import VideoView from './video-view.svelte'; import Loader from './loader.svelte'; + import VideoSelector from './video-selector.svelte'; import { RemoteRoom } from '../remote-room'; import { LocalRoom } from '../local-room'; @@ -21,6 +22,9 @@ let isLoading = true; $: remoteRoom.load().finally(() => isLoading = false); $: localRoom = new LocalRoom(remoteRoom); + let blobUrl: string; + + $: playUrl = $localRoom?.isLocalMode ? blobUrl : $localRoom?.url; const copyToClipboard = function (text: string) { const input = document.createElement('input'); @@ -29,13 +33,16 @@ input.select(); document.execCommand('copy'); document.body.removeChild(input); - } + }; const copyUrl = function () { copyToClipboard($page.url.href); }; $: isUrlValid = (() => { + if ($localRoom?.isLocalMode) { + return blobUrl; + } try { const url = new URL($localRoom.url); return url.protocol === "http:" || url.protocol === "https:"; @@ -61,6 +68,21 @@ {:else}

1. Select a video

+ + + {#if $localRoom.isLocalMode} +
+ You all downloaded a movie already!? Well done! Everyone should select the same video file, please. +
+ + {:else}
Insert a link to YouTube, Vimeo, HLS playlist, video or audio file. The input is synchronized with everyone in the room.
@@ -69,7 +91,9 @@ class="uk-input" class:uk-form-danger={$localRoom.url && !isUrlValid} placeholder="Video URL" + disabled={$localRoom.isLocalMode} /> + {/if}
{/if} @@ -91,9 +115,15 @@
Playback, time, and video scrolling are synchronized with everyone who has the page open.
- {#if isUrlValid} - - {/if} +
+ {#if isUrlValid} + + {:else} +
+ Video player will appear here when you insert a link or select a video +
+ {/if} +
{/if} @@ -106,4 +136,8 @@ .window-height { min-height: 100vh; } + + .stub { + margin-top: 15rem; + } diff --git a/src/routes/video-selector.svelte b/src/routes/video-selector.svelte new file mode 100644 index 0000000..1f80da0 --- /dev/null +++ b/src/routes/video-selector.svelte @@ -0,0 +1,31 @@ + + + loadSource(e.target.files[0])}/> + +{#if fileName} + {fileName} +{/if} + + diff --git a/src/routes/video-view.svelte b/src/routes/video-view.svelte index 2e856c6..42e7671 100644 --- a/src/routes/video-view.svelte +++ b/src/routes/video-view.svelte @@ -22,6 +22,12 @@ $: isMounted && (player.currentTime = time); $: isMounted && (player.paused = paused); + $: isBlob = url.startsWith('blob:'); + + const onLoaded = function (this: HTMLVideoElement) { + this.currentTime = time; + }; + onMount(() => { player.subscribe(options => { paused = options.paused; @@ -33,18 +39,34 @@ }); - - - - - - - +{#if isBlob} + +{/if} + + + + + + +