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

Add experimental URL timestamp functionality #214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions hugo/layouts/partials/player.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ <h3 class="player-number ml-2 my-0">
</div>
</div>

<div class="d-flex">
<button class="btn btn-link" data-action="player#getLinkToPosition">🔗</button>
</div>

<div class="d-flex align-items-center justify-content-center player-rate">
<button class="btn btn-link" data-action="player#togglePlaybackRate" data-target="player.rate">1</button>
</div>
Expand Down
21 changes: 21 additions & 0 deletions hugo/src/js/controllers/page_controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import Controller from '../base_controller';
import {addTimeToURL} from '../utils'

export default class extends Controller {
static state = {
URLTime: null,
};

static targets = ['player', 'playerStateReceiver'];

initialize() {
super.initialize();
this.getURLTimecode();
}

updatePodcasts() {
Expand All @@ -14,6 +20,12 @@ export default class extends Controller {
}

playPodcast({ detail }) {
// set playback position from ?t=00:00:00 if present
if (this.constructor.state.URLTime) {
detail.timeLabel = this.constructor.state.URLTime;
Object.assign(this.constructor.state, { "URLTime": null });
addTimeToURL(detail.url, detail.timeLabel);
}
this.getPlayerController().playPodcast(detail);
}

Expand All @@ -23,4 +35,13 @@ export default class extends Controller {
getPlayerController() {
return this.application.getControllerForElementAndIdentifier(this.playerTarget, 'player');
}

getURLTimecode() {
let searchParams = new URLSearchParams(window.location.search);
let timeFromUrl = searchParams.get("t");
let timeRegExp = new RegExp(/[0-9]*:?[0-9]:?[0-9]+/m);
if (timeRegExp.test(timeFromUrl)) {
Object.assign(this.constructor.state, { "URLTime": timeFromUrl });
}
}
}
8 changes: 8 additions & 0 deletions hugo/src/js/controllers/podcast_controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Controller from '../base_controller';
import Player from './player_controller';
import { addTimeToURL } from '../utils'

/**
* @property playButtonTarget
Expand Down Expand Up @@ -52,6 +53,8 @@ export default class extends Controller {
}

goToTimeLabel(e) {
// add each seek time to URL as t?=00:00:00
this.podcastAddTimeToURL(e);
this.play(e, e.target.textContent);
}

Expand All @@ -63,4 +66,9 @@ export default class extends Controller {
number: this.numberTarget.textContent,
};
}

podcastAddTimeToURL(e) {
let podcastPathname = this.data.get('url');
addTimeToURL(podcastPathname, e.target.textContent);
}
}
48 changes: 46 additions & 2 deletions hugo/src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export function getUnits(value, units) {
return /^[0,2-9]?[1]$/.test(value)
? units[0]
: /^[0,2-9]?[2-4]$/.test(value)
? units[1]
: units[2];
? units[1]
: units[2];
}

// 00:02:24 => 144
Expand All @@ -49,3 +49,47 @@ export function getTextSnippet(html) {

return snippet.length === LENGTH && result.length !== LENGTH ? `${snippet}...` : snippet;
}

//https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;

// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";

document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
alert('Временная метка скопирована в буфер обмена', msg, '\nTO DO: сделать красивое оповещение');
} catch (err) {
alert('ОШИБКА: невозможно скопировать временную метку в буфер обмена ', err, "\nTO DO: сделать красивое оповещение");
}

document.body.removeChild(textArea);
}

export function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function () {
alert('Временная метка скопирована в буфер обмена\nTO DO: сделать красивое оповещение');
}, function (err) {
alert('ОШИБКА: невозможно скопировать временную метку в буфер обмена ', err, "\nTO DO: сделать красивое оповещение");
});
}

export function addTimeToURL(podcastPathname, currentTime) {
// add full link to URL
if (window.history.pushState) {
window.history.pushState(null, null, [podcastPathname, "?t=", currentTime].join(""));
}
}