lbry.tech/app/components/client/playground-scripts.js

398 lines
14 KiB
JavaScript
Raw Normal View History

2018-11-30 21:46:22 +01:00
"use strict"; /* global document, navigator, send, window */
2018-07-16 23:06:37 +02:00
2018-10-03 22:27:13 +02:00
initializePlayground();
2018-07-16 23:06:37 +02:00
2018-11-30 21:46:22 +01:00
// pre-fill example one if search parameter exists
if (window.location.href.search && window.location.href.split("?url=")[1]) {
2018-07-27 00:18:26 +02:00
const searchParameter = window.location.href.split("?url=")[1];
2018-11-30 21:46:22 +01:00
fetchMetadata(1, searchParameter); // eslint-disable-line padding-line-between-statements
2018-07-27 00:18:26 +02:00
}
2018-09-27 18:11:26 +02:00
document.querySelector("body").addEventListener("click", event => {
if (event.target.dataset.action) {
event.preventDefault();
2018-10-03 22:27:13 +02:00
document.querySelector(".playground").classList.add("waiting");
2018-09-27 18:11:26 +02:00
handleExamples(event.target);
}
2018-07-25 00:29:25 +02:00
2018-09-27 18:11:26 +02:00
if (
2018-10-04 23:58:19 +02:00
event.target.classList &&
2018-12-05 00:12:39 +01:00
event.target.classList[0] === "playground-content__meme__canvas__thumbnail"
2018-09-27 18:11:26 +02:00
) {
2018-12-05 00:12:39 +01:00
for (const thumbnail of document.querySelectorAll(".playground-content__meme__canvas__thumbnail"))
2018-09-27 18:11:26 +02:00
thumbnail.classList.remove("selected");
2018-10-04 23:58:19 +02:00
event.target.classList.add("selected");
updateCanvas(event.target);
2018-09-27 18:11:26 +02:00
}
2018-07-16 23:06:37 +02:00
});
2018-09-27 18:11:26 +02:00
document.getElementById("fetch-claim-uri").addEventListener("keyup", event => {
2018-11-30 21:46:22 +01:00
const key = event.keyCode ?
event.keyCode :
event.which;
2018-07-24 18:44:32 +02:00
switch(true) {
2018-11-30 21:46:22 +01:00
case document.querySelector("[data-example='1']").classList.contains("active"):
if (
key === 13 &&
document.getElementById("fetch-claim-uri").value.length > 0
) fetchMetadata(1, document.getElementById("fetch-claim-uri").value);
break;
2018-11-30 21:46:22 +01:00
case document.querySelector("[data-example='3']").classList.contains("active"):
if (
key === 13 &&
document.getElementById("fetch-claim-uri").value.length > 0
) fetchMetadata(3, document.getElementById("fetch-claim-uri").value);
break;
2018-09-28 21:09:45 +02:00
default:
break;
}
2018-07-24 18:44:32 +02:00
});
2018-09-27 18:11:26 +02:00
document.querySelector("body").addEventListener("keyup", event => {
if (
event.target.id === "meme-top-line" ||
event.target.id === "meme-bottom-line"
) updateCanvas();
2018-08-09 16:38:42 +02:00
});
2018-07-18 22:31:05 +02:00
2018-07-23 23:46:58 +02:00
// H E L P E R S
function clearCanvas(canvas) {
const ctx = canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "copy";
ctx.strokeStyle = "transparent";
ctx.beginPath();
ctx.lineTo(0, 0);
ctx.stroke();
ctx.restore();
}
2018-11-30 21:46:22 +01:00
function detectLanguageAndUpdate() { // eslint-disable-line no-unused-vars
2018-07-23 23:46:58 +02:00
const compare = (array1, array2) => array2.filter(value => array2.indexOf(value)); // compare two arrays and get match(es)
2018-10-02 20:46:17 +02:00
const memeLocaleObject = document.getElementById("meme-language").children;
2018-07-23 23:46:58 +02:00
const memeLocales = [];
const userLocales = [];
2018-10-02 20:46:17 +02:00
for (const language of navigator.languages)
userLocales.push(language);
2018-07-23 23:46:58 +02:00
for (const key in memeLocaleObject) {
2018-09-28 21:09:45 +02:00
if (memeLocaleObject[key] && memeLocaleObject[key].value)
memeLocales.push(memeLocaleObject[key].value);
2018-07-23 23:46:58 +02:00
}
if (
memeLocales.length &&
userLocales.length &&
compare(memeLocales, userLocales).length
2018-10-02 20:46:17 +02:00
) document.querySelector(`option[value="${compare(memeLocales, userLocales)[0]}"]`).setAttribute("selected", true);
2018-07-23 23:46:58 +02:00
}
function debounce(func, wait, immediate) {
let timeout;
2018-10-06 22:53:01 +02:00
return function() {
const context = this;
const args = arguments;
const later = () => {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
2018-10-06 22:53:01 +02:00
clearTimeout(timeout);
timeout = setTimeout(later, wait);
2018-11-30 21:46:22 +01:00
if (callNow) func.apply(context, args);
};
}
2018-10-03 22:27:13 +02:00
function initializePlayground() {
document.querySelector(".playground").classList.add("waiting");
2018-09-27 18:11:26 +02:00
document.querySelector("#fetch-claim-uri").value = "";
document.querySelector("#fetch-claim-uri").focus();
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-navigation__example:nth-child(1)").classList.add("active");
2018-07-25 21:44:02 +02:00
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
message: "landed on playground"
}));
setTimeout(() => {
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-navigation__example:nth-child(1)").click();
2018-09-27 18:11:26 +02:00
}, 300);
2018-07-25 21:44:02 +02:00
}
function fetchMetadata(exampleNumber, data) {
if (!exampleNumber) return;
2018-07-25 00:29:25 +02:00
switch(exampleNumber) {
2018-07-25 00:29:25 +02:00
case 1:
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
claim: data,
message: "fetch metadata",
method: "resolve",
example: exampleNumber
2018-07-25 00:29:25 +02:00
}));
2018-09-28 21:09:45 +02:00
document.getElementById("fetch-claim-uri").value = data;
2018-11-30 21:46:22 +01:00
document.getElementById("playground-results").innerHTML = playgroundResponseForExample1(data);
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").style.display = "none";
2018-07-25 00:29:25 +02:00
break;
case 2:
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
data: data,
message: "fetch metadata",
method: "publish",
example: exampleNumber
}));
2018-11-30 21:46:22 +01:00
document.getElementById("playground-results").innerHTML = playgroundResponseForExample2(getMemeInfo());
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").style.display = "none";
break;
case 3:
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
claim: data,
message: "fetch metadata",
2018-11-21 18:53:17 +01:00
method: "claim_tip",
2018-10-06 22:53:01 +02:00
example: exampleNumber
}));
2018-10-03 21:16:26 +02:00
document.getElementById("fetch-claim-uri").value = data;
2018-11-30 21:46:22 +01:00
document.getElementById("playground-results").innerHTML = playgroundResponseForExample3(data);
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").style.display = "none";
2018-07-25 00:29:25 +02:00
break;
default:
break;
}
}
2018-07-18 22:31:05 +02:00
function getMemeInfo() { // TODO: Error handling
2018-10-02 20:46:17 +02:00
return {
2018-09-28 21:09:45 +02:00
description: document.getElementById("meme-description").value,
file_path: document.getElementById("meme-canvas").toDataURL("image/jpeg", 0.6),
language: document.getElementById("meme-language").value,
license: document.getElementById("meme-license").value,
name: document.getElementById("meme-title").value,
nsfw: document.getElementById("meme-nsfw-flag").checked,
title: document.getElementById("meme-title").value
};
2018-07-18 22:31:05 +02:00
}
2018-11-30 21:46:22 +01:00
function playgroundResponseForExample1(source) {
return `
<pre><code class="language-bash">
<span class="token comment"># With the LBRY app/daemon running locally, you can use this in your Terminal</span>
curl --header <span class="token string">"Content-Type: application/json"</span> --data <span class="token string">'{ "method": "resolve", "params": { "uri": "${source}" }}'</span> <span class="token url">http://localhost:5279 </span>
</code></pre>
<div class="loader" id="temp-loader"></div>
<div id="example1-result"></div>
`;
}
function playgroundResponseForExample2(source) {
return `
<pre><code class="language-bash">
<span class="token comment"># With the LBRY app/daemon running locally, you can use this in your Terminal</span>
curl --header <span class="token string">"Content-Type: application/json"</span> --data <span class="token string">'{ "method": "publish", "params": { "name": "${source.name}", "file_path": "ABSOLUTE_PATH_TO_MEDIA_ON_YOUR_COMPUTER", "bid": "0.001", "metadata": { "description": "${source.description}", "title": "${source.title}", "language": "${source.language}", "license": "${source.license}", "nsfw": ${source.nsfw} }}}'</span> <span class="token url">http://localhost:5279 </span>
</code></pre>
<div class="loader" id="temp-loader"></div>
<div id="example2-result"></div>
`;
}
function playgroundResponseForExample3(source) {
return `
<pre><code class="language-bash">
<span class="token comment"># With the LBRY app/daemon running locally, you can use this in your Terminal</span>
curl --header <span class="token string">"Content-Type: application/json"</span> --data <span class="token string">'{ "method": "claim_tip", "params": { "amount": "0.001", "claim_id": "${source}" }}'</span> <span class="token url">http://localhost:5279 </span>
</code></pre>
<div class="loader" id="temp-loader"></div>
<div id="example3-result"></div>
`;
}
const handleExamples = debounce(event => {
let exampleNumber;
2018-09-27 18:11:26 +02:00
const data = event.dataset;
2018-07-25 00:29:25 +02:00
2018-12-05 00:12:39 +01:00
if (!parseInt(document.querySelector(".playground-navigation__example.active").dataset.example)) return;
exampleNumber = parseInt(document.querySelector(".playground-navigation__example.active").dataset.example);
2018-07-25 00:29:25 +02:00
switch(data.action) {
case "choose claim":
fetchMetadata(exampleNumber, data.claimId);
2018-10-03 21:14:38 +02:00
2018-12-05 00:12:39 +01:00
if (document.querySelector(".playground-navigation__example:nth-child(3)").classList.contains("active"))
document.getElementById("fetch-claim-uri").value = event.dataset.name + "#" + event.dataset.claimId;
2018-10-03 21:14:38 +02:00
break;
2018-07-24 18:44:32 +02:00
case "execute claim":
2018-09-28 21:09:45 +02:00
if (!document.getElementById("fetch-claim-uri").value.length > 0) return;
fetchMetadata(exampleNumber, document.getElementById("fetch-claim-uri").value);
break;
2018-10-03 22:27:13 +02:00
case "playground, example 1":
2018-12-05 00:12:39 +01:00
if (document.getElementById("playground-loader").classList.contains("playground-content__meme")) {
document.getElementById("playground-loader").classList.remove("playground-content__meme");
document.getElementById("playground-loader").classList.add("playground-content__trends");
}
2018-09-28 21:09:45 +02:00
document.getElementById("fetch-claim-uri").value = ""; // reset URL bar
2018-10-03 22:27:13 +02:00
document.querySelector("#playground-url button").textContent = "Resolve";
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
if (document.getElementById("playground-url").style.display === "none")
document.getElementById("playground-url").removeAttribute("style");
2018-12-05 00:12:39 +01:00
for (const example of document.querySelectorAll(".playground-navigation__example"))
2018-09-28 21:09:45 +02:00
example.classList.remove("active");
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-navigation__example:nth-child(1)").classList.add("active");
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").innerHTML = "";
document.getElementById("playground-results").innerHTML = "";
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").removeAttribute("style");
document.getElementById("playground-results").removeAttribute("style");
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
message: `request for ${data.action}`
}));
break;
2018-10-03 22:27:13 +02:00
case "playground, example 2":
2018-12-05 00:12:39 +01:00
if (document.getElementById("playground-loader").classList.contains("playground-content__trends")) {
document.getElementById("playground-loader").classList.remove("playground-content__trends");
document.getElementById("playground-loader").classList.add("playground-content__meme");
}
2018-09-28 21:09:45 +02:00
document.getElementById("fetch-claim-uri").value = ""; // reset URL bar
2018-10-03 22:27:13 +02:00
document.getElementById("playground-url").style.display = "none";
2018-09-28 21:09:45 +02:00
2018-12-05 00:12:39 +01:00
for (const example of document.querySelectorAll(".playground-navigation__example"))
2018-09-28 21:09:45 +02:00
example.classList.remove("active");
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-navigation__example:nth-child(2)").classList.add("active");
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").innerHTML = "";
document.getElementById("playground-results").innerHTML = "";
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").removeAttribute("style");
document.getElementById("playground-results").removeAttribute("style");
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
message: `request for ${data.action}`
}));
break;
2018-10-03 22:27:13 +02:00
case "playground, example 3":
2018-12-05 00:12:39 +01:00
if (document.getElementById("playground-loader").classList.contains("playground-content__meme")) {
document.getElementById("playground-loader").classList.remove("playground-content__meme");
document.getElementById("playground-loader").classList.add("playground-content__trends");
}
2018-09-28 21:09:45 +02:00
document.getElementById("fetch-claim-uri").value = ""; // reset URL bar
2018-10-03 22:27:13 +02:00
document.querySelector("#playground-url button").textContent = "Tip";
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
if (document.getElementById("playground-url").style.display === "none")
document.getElementById("playground-url").removeAttribute("style");
2018-12-05 00:12:39 +01:00
for (const example of document.querySelectorAll(".playground-navigation__example"))
2018-09-28 21:09:45 +02:00
example.classList.remove("active");
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-navigation__example:nth-child(3)").classList.add("active");
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").innerHTML = "";
document.getElementById("playground-results").innerHTML = "";
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-loader").removeAttribute("style");
document.getElementById("playground-results").removeAttribute("style");
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
message: `request for ${data.action}`
}));
break;
case "upload image":
fetchMetadata(exampleNumber, getMemeInfo());
break;
default:
break;
}
}, 10);
2018-07-24 18:44:32 +02:00
2018-11-30 21:46:22 +01:00
function initCanvas() { // eslint-disable-line no-unused-vars
2018-07-24 18:44:32 +02:00
const canvas = document.getElementById("meme-canvas");
2018-09-28 23:59:38 +02:00
const canvasHeight = 600;
const canvasWidth = 800;
2018-07-24 18:44:32 +02:00
const ctx = canvas.getContext("2d");
const img = document.getElementById("base-image");
clearCanvas(canvas);
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight, 0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = "white";
2018-12-05 00:12:39 +01:00
ctx.font = "bold 48px 'Inter UI', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'";
// ctx.font = "bold 48px Arial";
2018-07-24 18:44:32 +02:00
ctx.lineJoin = "round";
ctx.lineWidth = 4;
ctx.strokeStyle = "black";
ctx.textAlign = "center";
ctx.textBaseline = "top";
2018-09-28 21:09:45 +02:00
2018-09-28 23:59:38 +02:00
positionCanvasText(ctx, canvasHeight, canvasWidth);
}
function positionCanvasText(canvas, height, width) {
canvas.strokeText(document.getElementById("meme-top-line").value.toUpperCase(), width / 2, 40);
canvas.fillText(document.getElementById("meme-top-line").value.toUpperCase(), width / 2, 40);
2018-09-28 21:09:45 +02:00
2018-09-28 23:59:38 +02:00
canvas.strokeText(document.getElementById("meme-bottom-line").value.toUpperCase(), width / 2, (height - 80));
canvas.fillText(document.getElementById("meme-bottom-line").value.toUpperCase(), width / 2, (height - 80));
2018-07-24 18:44:32 +02:00
}
function updateCanvas(imageSource) {
const canvas = document.getElementById("meme-canvas");
2018-09-28 23:59:38 +02:00
const canvasHeight = 600;
const canvasWidth = 800;
2018-07-24 18:44:32 +02:00
const ctx = canvas.getContext("2d");
const img = document.getElementById("base-image");
clearCanvas(canvas);
if (imageSource) {
2018-10-04 23:58:19 +02:00
ctx.drawImage(imageSource, 0, 0, canvasWidth, canvasHeight);
2018-07-24 18:44:32 +02:00
img.src = imageSource.src;
2018-09-28 23:59:38 +02:00
} else {
2018-10-04 23:58:19 +02:00
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
2018-09-28 23:59:38 +02:00
}
2018-09-28 21:09:45 +02:00
2018-09-28 23:59:38 +02:00
positionCanvasText(ctx, canvasHeight, canvasWidth);
2018-07-24 18:44:32 +02:00
}