feat: The first available version.

This commit is contained in:
2023-06-05 03:54:08 +08:00
parent 57b7ca2df4
commit fba2455772
10 changed files with 2637 additions and 171 deletions

52
asciiplayer/src/index.js Normal file
View File

@ -0,0 +1,52 @@
import * as AsciinemaPlayer from "asciinema-player";
import "asciinema-player/dist/bundle/asciinema-player.css";
import "../styles/default-player.css";
const defaultOpts = {
preload: true,
fit: "width"
}
document.addEventListener('DOMContentLoaded', () => {
const containers = document.getElementsByClassName("asciiplayer-container");
for (const container of containers) {
const castSource = container.dataset["apSrc"];
const containerStyle = container.dataset["apStyle"];
if (!castSource) {
console.warn(`asciiplayer: container ${container.id} has no data-ap-src attribute, skip.`);
continue;
}
let encodedOptions = container.dataset["apOpts"];
let options = defaultOpts;
if (encodedOptions) {
try {
options = mergeOptions(defaultOpts, JSON.parse(decodeURIComponent(encodedOptions)));
} catch (err) {
console.error("asciiplayer: failed to parse data-ap-options, skip.", err);
continue;
}
}
if (containerStyle) {
container.setAttribute("style", containerStyle);
delete container.dataset["apStyle"];
}
// noinspection JSCheckFunctionSignatures - create 中会判断是否为三个函数.
AsciinemaPlayer.create(castSource, container, options);
}
});
/**
*
* @param defaults {Object}
* @param opts {Object}
*/
function mergeOptions(defaults, opts) {
let result = {};
for (const key of Object.keys(defaults)) {
result[key] = defaults[key];
}
for (const key of Object.keys(opts)) {
result[key] = opts[key];
}
return result;
}