ruffle/web/packages/demo/www/index.js

168 lines
4.4 KiB
JavaScript
Raw Normal View History

import "./index.css";
2021-03-26 15:17:00 +00:00
import { SourceAPI, PublicAPI } from "ruffle-core";
window.RufflePlayer = PublicAPI.negotiate(
window.RufflePlayer,
"local",
new SourceAPI("local")
);
2021-03-26 15:17:00 +00:00
const ruffle = window.RufflePlayer.newest();
2019-04-25 17:52:22 +00:00
let player;
2019-04-25 17:52:22 +00:00
2021-02-20 01:40:43 +00:00
const main = document.getElementById("main");
2021-01-22 02:16:39 +00:00
const overlay = document.getElementById("overlay");
2021-03-26 15:17:00 +00:00
const prompt = document.getElementById("prompt");
const authorContainer = document.getElementById("author-container");
const author = document.getElementById("author");
const sampleFileInputContainer = document.getElementById(
"sample-swfs-container"
);
const localFileInput = document.getElementById("local-file");
2021-03-26 15:17:00 +00:00
const sampleFileInput = document.getElementById("sample-swfs");
2021-07-09 08:40:02 +00:00
// prettier-ignore
const optionGroups = {
"Animation": document.getElementById("anim-optgroup"),
"Game": document.getElementById("games-optgroup"),
};
2021-01-06 07:32:18 +00:00
// Default config used by the player.
const config = {
2021-01-06 07:32:18 +00:00
letterbox: "on",
2021-01-20 02:10:35 +00:00
logLevel: "warn",
2021-01-06 07:32:18 +00:00
};
2021-03-26 15:17:00 +00:00
function unload() {
2021-02-20 01:40:43 +00:00
if (player) {
player.remove();
}
2021-03-26 15:17:00 +00:00
prompt.classList.remove("hidden");
}
function load(options) {
unload();
prompt.classList.add("hidden");
2021-02-18 11:02:05 +00:00
player = ruffle.createPlayer();
player.id = "player";
2021-02-20 01:40:43 +00:00
main.append(player);
2021-03-26 15:17:00 +00:00
player.load(options);
}
function showSample(swfData) {
authorContainer.classList.remove("hidden");
author.textContent = swfData.author;
author.href = swfData.authorLink;
localFileInput.value = null;
}
2021-02-20 01:40:43 +00:00
2021-03-26 15:17:00 +00:00
function hideSample() {
2021-02-20 01:40:43 +00:00
sampleFileInput.selectedIndex = 0;
2021-03-26 15:17:00 +00:00
authorContainer.classList.add("hidden");
2021-02-20 01:40:43 +00:00
author.textContent = "";
author.href = "";
}
async function loadFile(file) {
if (!file) {
return;
}
2021-03-26 15:17:00 +00:00
hideSample();
2021-07-09 08:40:02 +00:00
const data = await new Response(file).arrayBuffer();
load({ data, ...config });
2021-02-20 01:40:43 +00:00
}
2021-02-21 20:52:04 +00:00
function loadSample() {
2021-02-20 01:40:43 +00:00
const swfData = sampleFileInput[sampleFileInput.selectedIndex].swfData;
if (swfData) {
2021-03-26 15:17:00 +00:00
showSample(swfData);
load({ url: swfData.location, ...config });
2021-02-20 01:40:43 +00:00
} else {
2021-03-26 15:17:00 +00:00
hideSample();
unload();
2021-02-20 01:40:43 +00:00
}
}
localFileInput.addEventListener("change", (event) => {
loadFile(event.target.files[0]);
});
2021-02-21 20:52:04 +00:00
sampleFileInput.addEventListener("change", () => loadSample());
2021-02-20 01:40:43 +00:00
2021-03-26 15:17:00 +00:00
main.addEventListener("dragenter", (event) => {
event.stopPropagation();
event.preventDefault();
2021-02-20 01:40:43 +00:00
});
2021-03-26 15:17:00 +00:00
main.addEventListener("dragleave", (event) => {
event.stopPropagation();
event.preventDefault();
2021-02-20 01:40:43 +00:00
overlay.classList.remove("drag");
});
main.addEventListener("dragover", (event) => {
event.stopPropagation();
event.preventDefault();
2021-03-26 15:17:00 +00:00
overlay.classList.add("drag");
2021-02-20 01:40:43 +00:00
});
main.addEventListener("drop", (event) => {
event.stopPropagation();
event.preventDefault();
overlay.classList.remove("drag");
localFileInput.files = event.dataTransfer.files;
2021-02-20 01:40:43 +00:00
loadFile(event.dataTransfer.files[0]);
});
localFileInput.addEventListener("dragleave", (event) => {
event.stopPropagation();
event.preventDefault();
overlay.classList.remove("drag");
});
localFileInput.addEventListener("dragover", (event) => {
event.stopPropagation();
event.preventDefault();
overlay.classList.add("drag");
});
localFileInput.addEventListener("drop", (event) => {
event.stopPropagation();
event.preventDefault();
overlay.classList.remove("drag");
localFileInput.files = event.dataTransfer.files;
loadFile(event.dataTransfer.files[0]);
});
2021-02-20 01:40:43 +00:00
window.addEventListener("load", () => {
if (
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPhone/i)
) {
localFileInput.removeAttribute("accept");
}
2021-03-26 15:17:00 +00:00
overlay.classList.remove("hidden");
2021-02-20 01:40:43 +00:00
});
2021-03-26 15:17:00 +00:00
(async () => {
const response = await fetch("swfs.json");
if (!response.ok) {
return;
}
const data = await response.json();
for (const swfData of data.swfs) {
const option = document.createElement("option");
option.textContent = swfData.title;
option.value = swfData.location;
option.swfData = swfData;
2021-07-09 08:40:02 +00:00
optionGroups[swfData.type].append(option);
}
2021-03-26 15:17:00 +00:00
sampleFileInputContainer.classList.remove("hidden");
2021-02-21 20:52:04 +00:00
const initialFile = new URL(window.location).searchParams.get("file");
if (initialFile) {
const options = Array.from(sampleFileInput.options);
sampleFileInput.selectedIndex = Math.max(
options.findIndex((swfData) => swfData.value.endsWith(initialFile)),
0
);
2021-02-21 20:52:04 +00:00
loadSample();
}
2021-03-26 15:17:00 +00:00
})();