Repository navigation
Expand file tree
/
Copy pathembed.js
More file actions
83 lines (78 loc) · 3.03 KB
/
Copy pathembed.js
File metadata and controls
83 lines (78 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*! Flow Explainer embed loader
* https://github.com/solutionforest/flow-explainer
* Drop on any site: <script async src="…/embed.js" data-c="…" data-text="0"></script>
* Or load markdown from a public URL: data-u="https://…/file.md" (instead of data-c).
* Builds an iframe to the player. data-c is the same payload as a client link’s #c=.
*/
(function () {
'use strict';
var s = document.currentScript;
if (!s || !s.getAttribute) return;
var d = s.dataset || {};
var c = d.c || d.payload || '';
var u = d.u || d.url || '';
if (!c && !u) {
if (typeof console !== 'undefined') console.warn('[flow-explainer] data-c or data-u is required');
return;
}
var view = String(d.view || '2d').toLowerCase() === '3d' ? '3d' : '2d';
var host = (d.host || '').replace(/\/+$/, '');
var scriptUrl = null;
try {
if (s.src) scriptUrl = new URL(s.src, location.href);
} catch (e) {
scriptUrl = null;
}
function playerBase(origin, pathDir, staticHost) {
var dir = (pathDir || '').replace(/\/+$/, '');
if (staticHost) return origin + dir + (view === '3d' ? '/3d.html' : '/index.html') + '?embed=1';
return origin + (view === '3d' ? '/embed/3d' : '/embed');
}
var base;
try {
if (host) {
var hu = new URL(/:\/\//.test(host) ? host : 'https://' + host);
var hDir = hu.pathname.replace(/\/+$/, '');
if (hDir === '/') hDir = '';
base = playerBase(hu.origin, hDir, /\.github\.io$/i.test(hu.hostname));
} else if (scriptUrl) {
var dir = scriptUrl.pathname.replace(/\/[^/]*$/, '');
base = playerBase(scriptUrl.origin, dir, /\.github\.io$/i.test(scriptUrl.hostname));
} else {
if (typeof console !== 'undefined') console.warn('[flow-explainer] cannot resolve host');
return;
}
} catch (e) {
if (typeof console !== 'undefined') console.warn('[flow-explainer] bad host', e);
return;
}
var hash = u ? '#u=' + encodeURIComponent(u) : '#c=' + c;
if (d.s) hash += '&s=' + encodeURIComponent(d.s);
if (d.i === '0') hash += '&i=0';
// Default: hide step text (diagram + controls only). Pass data-text="1" to show it.
if (d.text !== '1') hash += '&t=0';
if (d.switch === '0' || d.v === '0') hash += '&v=0';
var title = d.title || 'Flow Explainer';
var size = d.size || 'r';
var iframe = document.createElement('iframe');
iframe.src = base + hash;
iframe.title = title;
iframe.loading = 'lazy';
iframe.allow = 'fullscreen; clipboard-write';
iframe.setAttribute('allowfullscreen', '');
var node;
var m = /^(\d+)x(\d+)$/.exec(size);
if (m) {
iframe.width = m[1];
iframe.height = m[2];
iframe.style.cssText = 'border:0;border-radius:12px;max-width:100%';
node = iframe;
} else {
var wrap = document.createElement('div');
wrap.style.cssText = 'position:relative;width:100%;aspect-ratio:16/10';
iframe.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;border:0;border-radius:12px';
wrap.appendChild(iframe);
node = wrap;
}
if (s.parentNode) s.parentNode.insertBefore(node, s.nextSibling);
})();