Skip to content

Commit

Permalink
fix(playlist): enable autoplay when switching between items
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Mar 18, 2024
1 parent 4be3ff5 commit 72edd01
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions apps/app/src/components/player/buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from "@/components/icon";
import { cn } from "@/lib/utils";
import { isWithMedia, findWithMedia } from "@/media-note/playlist/def";
import { toInfoKey } from "../../media-note/note-index";
import {
useIsEmbed,
usePlaylistChange,
Expand Down Expand Up @@ -227,7 +228,7 @@ export function Next() {
if (!(curr && isWithMedia(curr))) return null;
const next = findWithMedia(
playlist.list,
(li) => !curr.media.compare(li.media),
(li) => toInfoKey(curr.media) !== toInfoKey(li.media),
{ fromIndex: playlist.active + 1 },
);
if (!next) return null;
Expand All @@ -251,7 +252,7 @@ export function Previous() {
if (!(curr && isWithMedia(curr))) return null;
const prev = findWithMedia(
playlist.list,
(li) => !curr.media.compare(li.media),
(li) => toInfoKey(curr.media) !== toInfoKey(li.media),
{ fromIndex: playlist.active - 1, reverse: true },
);
if (!prev) return null;
Expand Down
3 changes: 2 additions & 1 deletion apps/app/src/components/player/menus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MoreIcon, PlaylistIcon, SubtitlesIcon } from "@/components/icon";
import { showAtButton } from "@/lib/menu";
import type { PlaylistItem } from "@/media-note/playlist/def";
import { isWithMedia } from "@/media-note/playlist/def";
import { toInfoKey } from "../../media-note/note-index";
import {
useApp,
useIsEmbed,
Expand Down Expand Up @@ -77,7 +78,7 @@ export function Playlist() {
item.setTitle(li.title).onClick(() => {
onPlaylistChange(li, playlist);
});
if (current.compare(li.media)) {
if (toInfoKey(current) === toInfoKey(li.media)) {
item.setChecked(true);
const checkParent = (node: PlaylistItem) => {
if (node.parent < 0) return;
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/lib/remote-player/html–media-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class HTMLMediaEvents {
}

private _onPlay(event: Event) {
if (!this._ctx.$state.canPlay) return;
if (!this._ctx.$state.canPlay()) return;
this._notify("play", undefined, new Event(event.type));
}

Expand Down
11 changes: 10 additions & 1 deletion apps/app/src/lib/remote-player/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export class WebiviewMediaProvider implements MediaProviderAdapter {
await this.#interaction;
return;
}
await (this.#interaction = this.webview.executeJavaScript("1", true));
await (this.#interaction = this.webview
.executeJavaScript("1", true)
.finally(() => {
this.#interaction = null;
}));
}

async play() {
Expand Down Expand Up @@ -260,6 +264,11 @@ export class WebiviewMediaProvider implements MediaProviderAdapter {
onDomReady = async (evt: Event) => {
const webview = this._webview;
new HTMLMediaEvents(this, this._ctx);
Maverick.effect(() => {
if (this._ctx.$state.autoPlay()) {
this.userGesture();
}
});
this._updateTitle(evt);
// prepare to recieve port, handle plugin load
await evalInWebview(init, webview);
Expand Down
3 changes: 1 addition & 2 deletions apps/app/src/media-note/playlist/def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import type { TFile } from "obsidian";
import type { MediaInfo } from "@/media-view/media-info";
import type { MediaType } from "@/patch/media-type";
import { type MediaURL } from "@/web/url-match";

export const taskSymbolMediaTypeMap = {
">": "video",
Expand Down Expand Up @@ -46,7 +45,7 @@ export interface PlaylistItem {
parent: number;
}
export interface PlaylistItemWithMedia extends PlaylistItem {
media: MediaURL;
media: MediaInfo;
}

export function isWithMedia(item: PlaylistItem): item is PlaylistItemWithMedia {
Expand Down
1 change: 1 addition & 0 deletions apps/app/src/media-view/remote-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export abstract class MediaRemoteView
store: this.store,
reload: () => this.render(),
onPlaylistChange: (item) => {
item.media.hash += "&play";
this.plugin.leafOpener.openMediaIn(this.leaf, item.media);
},
embed: false,
Expand Down

0 comments on commit 72edd01

Please sign in to comment.