Skip to content
Snippets Groups Projects
Commit 0abbb52d authored by David Sehnal's avatar David Sehnal
Browse files

mol-plugin: remote play on load, better auto playback

parent 51895ec6
Branches
Tags
No related merge requests found
...@@ -146,13 +146,13 @@ export function Snapshots(ctx: PluginContext) { ...@@ -146,13 +146,13 @@ export function Snapshots(ctx: PluginContext) {
return ctx.state.setSnapshot(snapshot); return ctx.state.setSnapshot(snapshot);
}); });
PluginCommands.State.Snapshots.Upload.subscribe(ctx, ({ name, description, serverUrl }) => { PluginCommands.State.Snapshots.Upload.subscribe(ctx, ({ name, description, playOnLoad, serverUrl }) => {
return fetch(`${serverUrl}/set?name=${encodeURIComponent(name || '')}&description=${encodeURIComponent(description || '')}`, { return fetch(`${serverUrl}/set?name=${encodeURIComponent(name || '')}&description=${encodeURIComponent(description || '')}`, {
method: 'POST', method: 'POST',
mode: 'cors', mode: 'cors',
referrer: 'no-referrer', referrer: 'no-referrer',
headers: { 'Content-Type': 'application/json; charset=utf-8' }, headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: JSON.stringify(ctx.state.snapshots.getRemoteSnapshot()) body: JSON.stringify(ctx.state.snapshots.getRemoteSnapshot({ name, description, playOnLoad }))
}) as any as Promise<void>; }) as any as Promise<void>;
}); });
......
...@@ -35,7 +35,7 @@ export const PluginCommands = { ...@@ -35,7 +35,7 @@ export const PluginCommands = {
Apply: PluginCommand<{ id: string }>(), Apply: PluginCommand<{ id: string }>(),
Clear: PluginCommand<{}>(), Clear: PluginCommand<{}>(),
Upload: PluginCommand<{ name?: string, description?: string, serverUrl: string }>(), Upload: PluginCommand<{ name?: string, description?: string, playOnLoad?: boolean, serverUrl: string }>(),
Fetch: PluginCommand<{ url: string }>(), Fetch: PluginCommand<{ url: string }>(),
DownloadToFile: PluginCommand<{ name?: string }>(), DownloadToFile: PluginCommand<{ name?: string }>(),
......
...@@ -148,11 +148,11 @@ class PluginStateSnapshotManager extends PluginComponent<{ ...@@ -148,11 +148,11 @@ class PluginStateSnapshotManager extends PluginComponent<{
const next = entry && entry.snapshot; const next = entry && entry.snapshot;
if (!next) return; if (!next) return;
await this.plugin.state.setSnapshot(next); await this.plugin.state.setSnapshot(next);
if (snapshot.playback && snapshot.playback.isPlaying) this.play(); if (snapshot.playback && snapshot.playback.isPlaying) this.play(true);
return next; return next;
} }
getRemoteSnapshot(options?: { name?: string, description?: string }): PluginStateSnapshotManager.RemoteSnapshot { getRemoteSnapshot(options?: { name?: string, description?: string, playOnLoad?: boolean }): PluginStateSnapshotManager.RemoteSnapshot {
// TODO: diffing and all that fancy stuff // TODO: diffing and all that fancy stuff
return { return {
timestamp: +new Date(), timestamp: +new Date(),
...@@ -160,7 +160,7 @@ class PluginStateSnapshotManager extends PluginComponent<{ ...@@ -160,7 +160,7 @@ class PluginStateSnapshotManager extends PluginComponent<{
description: options && options.description, description: options && options.description,
current: this.state.current, current: this.state.current,
playback: { playback: {
isPlaying: this.state.isPlaying, isPlaying: !!(options && options.playOnLoad),
nextSnapshotDelayInMs: this.state.nextSnapshotDelayInMs nextSnapshotDelayInMs: this.state.nextSnapshotDelayInMs
}, },
entries: this.state.entries.valueSeq().toArray() entries: this.state.entries.valueSeq().toArray()
...@@ -181,9 +181,21 @@ class PluginStateSnapshotManager extends PluginComponent<{ ...@@ -181,9 +181,21 @@ class PluginStateSnapshotManager extends PluginComponent<{
if (this.state.isPlaying) this.timeoutHandle = setTimeout(this.next, delay); if (this.state.isPlaying) this.timeoutHandle = setTimeout(this.next, delay);
}; };
play() { play(delayFirst: boolean = false) {
this.updateState({ isPlaying: true }); this.updateState({ isPlaying: true });
this.next();
if (delayFirst) {
const e = this.getEntry(this.state.current);
if (!e) {
this.next();
return;
}
const snapshot = e.snapshot;
const delay = typeof snapshot.durationInMs !== 'undefined' ? snapshot.durationInMs : this.state.nextSnapshotDelayInMs;
this.timeoutHandle = setTimeout(this.next, delay);
} else {
this.next();
}
} }
stop() { stop() {
......
...@@ -168,6 +168,7 @@ class RemoteStateSnapshots extends PluginUIComponent< ...@@ -168,6 +168,7 @@ class RemoteStateSnapshots extends PluginUIComponent<
name: PD.Text(), name: PD.Text(),
options: PD.Group({ options: PD.Group({
description: PD.Text(), description: PD.Text(),
playOnLoad: PD.Boolean(false),
serverUrl: PD.Text('https://webchem.ncbr.muni.cz/molstar-state') serverUrl: PD.Text('https://webchem.ncbr.muni.cz/molstar-state')
}) })
}; };
...@@ -215,6 +216,7 @@ class RemoteStateSnapshots extends PluginUIComponent< ...@@ -215,6 +216,7 @@ class RemoteStateSnapshots extends PluginUIComponent<
await PluginCommands.State.Snapshots.Upload.dispatch(this.plugin, { await PluginCommands.State.Snapshots.Upload.dispatch(this.plugin, {
name: this.state.params.name, name: this.state.params.name,
description: this.state.params.options.description, description: this.state.params.options.description,
playOnLoad: this.state.params.options.playOnLoad,
serverUrl: this.state.params.options.serverUrl serverUrl: this.state.params.options.serverUrl
}); });
this.setState({ isBusy: false }); this.setState({ isBusy: false });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment