Skip to content
Merged
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
54 changes: 51 additions & 3 deletions 3d.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@
.sh-facts b{color:var(--ink)}
.sh-facts code{font:12px var(--mono);background:var(--paper);border:1px solid var(--line);border-radius:4px;padding:0 4px}
.sh-foot{font:11px var(--mono);color:var(--muted);border-top:1px dashed var(--line-2);padding-top:10px}
.sh-embed{display:flex;flex-direction:column;gap:8px;border-top:1px solid var(--line);padding-top:12px}
.sh-eh{display:flex;align-items:center;justify-content:space-between;gap:8px}
.sh-eh b{font:600 13px var(--sans);color:var(--ink)}
.sh-eh select.btn{min-height:28px;font-size:12px}
.sh-embed textarea{width:100%;resize:none;border:1px solid var(--line);background:var(--paper);border-radius:8px;padding:8px 10px;font:11.5px/1.5 var(--mono);color:var(--ink-2);white-space:pre-wrap;word-break:break-all}
.sh-embed .row .btn{flex:1}
.sh-opts{display:flex;flex-wrap:wrap;gap:6px 16px}
.sh-opts .ck{font-size:12.5px}
/* embed options: &t=0 hides the step text panel, &v=0 locks the view (no 2D/3D switch) */
body.embed.no-text main{grid-template-columns:minmax(0,1fr);grid-template-rows:minmax(0,1fr)}
body.embed.no-text .panel{display:none}
body.embed.lock-view #btnMode{display:none}

/* about dialog */
button.brand{border:0;background:none;padding:0;color:inherit;cursor:pointer;border-radius:6px}
Expand Down Expand Up @@ -391,7 +403,12 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
<li><b>Copies stay where you paste them.</b> Chat apps, email and browser history keep the full link.</li>
</ul>
<div class="sh-foot">Opens view-only: no Edit or Snapshot.</div>
<button class="btn" id="shareEmbed" type="button" title="Copy a compact /embed URL for iframes">Copy embed link</button>
<div class="sh-embed">
<div class="sh-eh"><b>Embed on a website</b><select class="btn" id="embedSize" aria-label="Embed size"><option value="r">Responsive</option><option value="960x600">960 × 600</option><option value="800x500">800 × 500</option><option value="640x480">640 × 480</option></select></div>
<div class="sh-opts"><label class="ck"><input type="checkbox" id="embedText" checked> Show step text</label><label class="ck"><input type="checkbox" id="embedSwitch" checked> Allow 2D / 3D switch</label></div>
<textarea id="embedCode" readonly rows="4" spellcheck="false" aria-label="Embed code"></textarea>
<div class="row"><button class="btn primary" id="embedCopy" type="button">Copy embed code</button><button class="btn ghost" id="shareEmbed" type="button" title="Copy only the compact /embed URL">Copy embed link</button></div>
</div>
</div>
<div class="scrim" id="scrim"></div>
<div class="drawer" id="drawer">
Expand Down Expand Up @@ -2276,7 +2293,30 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
const pageUrl=sub=>sub?tidyUrl(rootUrl()+sub):tidyUrl(rootUrl()+'/');
const linkOpts=()=>(DSK!=='original'?'&s='+DSK:'')+(ICONS_ON?'':'&i=0');
const clientLink=async()=>pageUrl(is3dView()?'/3d':'')+'#c='+await encode(/** @type {any} */(S).md)+linkOpts();
const embedLink=async()=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts();
/**
* iframe HTML for pasting into a website, like YouTube's "Embed" code.
* @param {string} url the /embed link (with #c=…)
* @param {string} size 'r' for responsive (full width, 16:10), or 'WxH' in pixels
* @param {string} [title]
* @returns {string}
*/
function embedCode(url,size,title){
const a=(/** @type {string} */v)=>v.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;');
const attrs='src="'+a(url)+'" title="'+a(title||'Flow Explainer')+'" loading="lazy" allow="fullscreen; clipboard-write" allowfullscreen';
const m=size.match(/^(\d+)x(\d+)$/);
if(m)return '<iframe '+attrs+' width="'+m[1]+'" height="'+m[2]+'" style="border:0;border-radius:12px;max-width:100%"></iframe>';
return '<div style="position:relative;width:100%;aspect-ratio:16/10">\n <iframe '+attrs+' style="position:absolute;inset:0;width:100%;height:100%;border:0;border-radius:12px"></iframe>\n</div>';
}
/**
* Embed-only options carried in the link: &t=0 hides the step text panel, &v=0 locks the view.
* @param {{text:boolean,switchView:boolean}} o
*/
const embedOpts=o=>(o.text?'':'&t=0')+(o.switchView?'':'&v=0');
/** @param {string} k @returns {string|null} value of k in the URL hash (#c=…&k=v) */
const hashParam=k=>{const m=location.hash.match(new RegExp('[#&]'+k+'=([^&]*)'));return m?m[1]:null};
/** In the embed player, apply &t=0 / &v=0 from the link. */
function applyEmbedOpts(){if(!isEmbed())return;document.body.classList.toggle('no-text',hashParam('t')==='0');document.body.classList.toggle('lock-view',hashParam('v')==='0')}
const embedLink=async(/** @type {{text:boolean,switchView:boolean}} */o={text:true,switchView:true})=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts()+embedOpts(o);
/** @param {'2d'|'3d'} dim */
function viewHref(dim){
const h=location.hash||'';
Expand Down Expand Up @@ -2304,6 +2344,7 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
+'<div class="prog"><i style="width:'+(n?Math.round(i/n*100):0)+'%"></i></div><ol class="stepper">'+rows+'</ol></details>';
}
function wireUi(){
applyEmbedOpts();
buildSettings();
$('#btnSet').onclick=(/** @type {MouseEvent} */e)=>{e.stopPropagation();openPop($('#settings').classList.contains('open')?null:'set')};
$('#setClose').onclick=$('#shareClose').onclick=$('#popScrim').onclick=()=>openPop(null);
Expand All @@ -2314,11 +2355,18 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
const url=await clientLink();let ok=true;
try{await navigator.clipboard.writeText(url)}catch(e){ok=false}
$('#shareUrl').value=url;$('#shareTitle span').textContent=ok?'Client link copied':'Copy this client link';openPop('share');
await fillEmbed();
if(!ok){$('#shareUrl').focus();$('#shareUrl').select()}
};
$('#shareCopy').onclick=async()=>{try{await navigator.clipboard.writeText($('#shareUrl').value);toast('Client link copied')}catch(e){$('#shareUrl').select()}};
/** Fill the embed box from the current diagram, view and size. */
const fillEmbed=async()=>{const doc=/** @type {{doc?:{docTitle?:string}|null}} */(/** @type {unknown} */(S)).doc;
$('#embedCode').value=embedCode(await embedLink(curOpts()),$('#embedSize').value,(doc&&doc.docTitle)||'Flow Explainer')};
const curOpts=()=>({text:$('#embedText').checked,switchView:$('#embedSwitch').checked});
$('#embedSize').onchange=$('#embedText').onchange=$('#embedSwitch').onchange=fillEmbed;
$('#embedCopy').onclick=async()=>{try{await navigator.clipboard.writeText($('#embedCode').value);toast('Embed code copied')}catch(e){$('#embedCode').select()}};
$('#shareEmbed').onclick=async()=>{
const url=await embedLink();
const url=await embedLink(curOpts());
try{await navigator.clipboard.writeText(url);toast('Embed link copied')}catch(e){prompt('Copy this embed link',url)}
};
$('#panel').addEventListener('keydown',(/** @type {KeyboardEvent} */e)=>{const t=tgt(e),li=/** @type {HTMLElement|null} */(t.closest&&t.closest('.stepper li'));if(li&&(e.key==='Enter'||e.key===' ')){e.preventDefault();e.stopPropagation();go(+(li.dataset.s||0))}});
Expand Down
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,24 @@ To host it, upload the folder to GitHub Pages, Cloudflare Pages, Netlify, S3 or

### Embed in another site

Use the compact player at `/embed` (2D) or `/embed/3d` (3D) with the same `#c=…` client-link payload. In the app, **Copy client link** → **Copy embed link**, then iframe it:
Use the compact player at `/embed` (2D) or `/embed/3d` (3D) with the same `#c=…` client-link payload.

In the app, press **Copy client link**. The box that opens has an **Embed on a website** section, like YouTube's embed code:
- **Size:** pick *Responsive* (full width, 16:10) or a fixed size: 960 × 600, 800 × 500 or 640 × 480.
- **Show step text:** untick to hide the step list and show only the diagram and the stepper (adds `&t=0`).
- **Allow 2D / 3D switch:** untick to lock the embed to the view you copied it from: only 2D or only 3D (adds `&v=0`).
- **Copy embed code** copies the ready-to-paste `<iframe>` HTML. **Copy embed link** copies only the URL.

The responsive code looks like this:

```html
<iframe
src="https://flow-explainer.laravel.cloud/embed#c=…"
title="Flow Explainer"
style="width:100%;height:640px;border:0;border-radius:12px"
allow="fullscreen"
></iframe>
<div style="position:relative;width:100%;aspect-ratio:16/10">
<iframe src="https://flow-explainer.laravel.cloud/embed#c=…" title="How an online ticket order is handled" loading="lazy" allow="fullscreen; clipboard-write" allowfullscreen style="position:absolute;inset:0;width:100%;height:100%;border:0;border-radius:12px"></iframe>
</div>
```

The code keeps your diagram style and icon setting, and uses the 3D player if you copied it from the 3D view.

The host must allow framing (this Cloud app uses `X-Frame-Options` / frame = `all`). Diagram data still lives only in the URL hash.

## Privacy
Expand Down
54 changes: 51 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@
.sh-facts b{color:var(--ink)}
.sh-facts code{font:12px var(--mono);background:var(--paper);border:1px solid var(--line);border-radius:4px;padding:0 4px}
.sh-foot{font:11px var(--mono);color:var(--muted);border-top:1px dashed var(--line-2);padding-top:10px}
.sh-embed{display:flex;flex-direction:column;gap:8px;border-top:1px solid var(--line);padding-top:12px}
.sh-eh{display:flex;align-items:center;justify-content:space-between;gap:8px}
.sh-eh b{font:600 13px var(--sans);color:var(--ink)}
.sh-eh select.btn{min-height:28px;font-size:12px}
.sh-embed textarea{width:100%;resize:none;border:1px solid var(--line);background:var(--paper);border-radius:8px;padding:8px 10px;font:11.5px/1.5 var(--mono);color:var(--ink-2);white-space:pre-wrap;word-break:break-all}
.sh-embed .row .btn{flex:1}
.sh-opts{display:flex;flex-wrap:wrap;gap:6px 16px}
.sh-opts .ck{font-size:12.5px}
/* embed options: &t=0 hides the step text panel, &v=0 locks the view (no 2D/3D switch) */
body.embed.no-text main{grid-template-columns:minmax(0,1fr);grid-template-rows:minmax(0,1fr)}
body.embed.no-text .panel{display:none}
body.embed.lock-view #btnMode{display:none}

/* about dialog */
button.brand{border:0;background:none;padding:0;color:inherit;cursor:pointer;border-radius:6px}
Expand Down Expand Up @@ -409,7 +421,12 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
<li><b>Copies stay where you paste them.</b> Chat apps, email and browser history keep the full link.</li>
</ul>
<div class="sh-foot">Opens view-only: no Edit or Snapshot.</div>
<button class="btn" id="shareEmbed" type="button" title="Copy a compact /embed URL for iframes">Copy embed link</button>
<div class="sh-embed">
<div class="sh-eh"><b>Embed on a website</b><select class="btn" id="embedSize" aria-label="Embed size"><option value="r">Responsive</option><option value="960x600">960 × 600</option><option value="800x500">800 × 500</option><option value="640x480">640 × 480</option></select></div>
<div class="sh-opts"><label class="ck"><input type="checkbox" id="embedText" checked> Show step text</label><label class="ck"><input type="checkbox" id="embedSwitch" checked> Allow 2D / 3D switch</label></div>
<textarea id="embedCode" readonly rows="4" spellcheck="false" aria-label="Embed code"></textarea>
<div class="row"><button class="btn primary" id="embedCopy" type="button">Copy embed code</button><button class="btn ghost" id="shareEmbed" type="button" title="Copy only the compact /embed URL">Copy embed link</button></div>
</div>
</div>
<div class="scrim" id="scrim"></div>
<div class="drawer" id="drawer">
Expand Down Expand Up @@ -2292,7 +2309,30 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
const pageUrl=sub=>sub?tidyUrl(rootUrl()+sub):tidyUrl(rootUrl()+'/');
const linkOpts=()=>(DSK!=='original'?'&s='+DSK:'')+(ICONS_ON?'':'&i=0');
const clientLink=async()=>pageUrl(is3dView()?'/3d':'')+'#c='+await encode(/** @type {any} */(S).md)+linkOpts();
const embedLink=async()=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts();
/**
* iframe HTML for pasting into a website, like YouTube's "Embed" code.
* @param {string} url the /embed link (with #c=…)
* @param {string} size 'r' for responsive (full width, 16:10), or 'WxH' in pixels
* @param {string} [title]
* @returns {string}
*/
function embedCode(url,size,title){
const a=(/** @type {string} */v)=>v.replace(/&/g,'&amp;').replace(/"/g,'&quot;').replace(/</g,'&lt;');
const attrs='src="'+a(url)+'" title="'+a(title||'Flow Explainer')+'" loading="lazy" allow="fullscreen; clipboard-write" allowfullscreen';
const m=size.match(/^(\d+)x(\d+)$/);
if(m)return '<iframe '+attrs+' width="'+m[1]+'" height="'+m[2]+'" style="border:0;border-radius:12px;max-width:100%"></iframe>';
return '<div style="position:relative;width:100%;aspect-ratio:16/10">\n <iframe '+attrs+' style="position:absolute;inset:0;width:100%;height:100%;border:0;border-radius:12px"></iframe>\n</div>';
}
/**
* Embed-only options carried in the link: &t=0 hides the step text panel, &v=0 locks the view.
* @param {{text:boolean,switchView:boolean}} o
*/
const embedOpts=o=>(o.text?'':'&t=0')+(o.switchView?'':'&v=0');
/** @param {string} k @returns {string|null} value of k in the URL hash (#c=…&k=v) */
const hashParam=k=>{const m=location.hash.match(new RegExp('[#&]'+k+'=([^&]*)'));return m?m[1]:null};
/** In the embed player, apply &t=0 / &v=0 from the link. */
function applyEmbedOpts(){if(!isEmbed())return;document.body.classList.toggle('no-text',hashParam('t')==='0');document.body.classList.toggle('lock-view',hashParam('v')==='0')}
const embedLink=async(/** @type {{text:boolean,switchView:boolean}} */o={text:true,switchView:true})=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts()+embedOpts(o);
/** @param {'2d'|'3d'} dim */
function viewHref(dim){
const h=location.hash||'';
Expand Down Expand Up @@ -2320,6 +2360,7 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
+'<div class="prog"><i style="width:'+(n?Math.round(i/n*100):0)+'%"></i></div><ol class="stepper">'+rows+'</ol></details>';
}
function wireUi(){
applyEmbedOpts();
buildSettings();
$('#btnSet').onclick=(/** @type {MouseEvent} */e)=>{e.stopPropagation();openPop($('#settings').classList.contains('open')?null:'set')};
$('#setClose').onclick=$('#shareClose').onclick=$('#popScrim').onclick=()=>openPop(null);
Expand All @@ -2330,11 +2371,18 @@ <h2 id="aboutTitle">Flow<br><span>Explainer</span></h2>
const url=await clientLink();let ok=true;
try{await navigator.clipboard.writeText(url)}catch(e){ok=false}
$('#shareUrl').value=url;$('#shareTitle span').textContent=ok?'Client link copied':'Copy this client link';openPop('share');
await fillEmbed();
if(!ok){$('#shareUrl').focus();$('#shareUrl').select()}
};
$('#shareCopy').onclick=async()=>{try{await navigator.clipboard.writeText($('#shareUrl').value);toast('Client link copied')}catch(e){$('#shareUrl').select()}};
/** Fill the embed box from the current diagram, view and size. */
const fillEmbed=async()=>{const doc=/** @type {{doc?:{docTitle?:string}|null}} */(/** @type {unknown} */(S)).doc;
$('#embedCode').value=embedCode(await embedLink(curOpts()),$('#embedSize').value,(doc&&doc.docTitle)||'Flow Explainer')};
const curOpts=()=>({text:$('#embedText').checked,switchView:$('#embedSwitch').checked});
$('#embedSize').onchange=$('#embedText').onchange=$('#embedSwitch').onchange=fillEmbed;
$('#embedCopy').onclick=async()=>{try{await navigator.clipboard.writeText($('#embedCode').value);toast('Embed code copied')}catch(e){$('#embedCode').select()}};
$('#shareEmbed').onclick=async()=>{
const url=await embedLink();
const url=await embedLink(curOpts());
try{await navigator.clipboard.writeText(url);toast('Embed link copied')}catch(e){prompt('Copy this embed link',url)}
};
$('#panel').addEventListener('keydown',(/** @type {KeyboardEvent} */e)=>{const t=tgt(e),li=/** @type {HTMLElement|null} */(t.closest&&t.closest('.stepper li'));if(li&&(e.key==='Enter'||e.key===' ')){e.preventDefault();e.stopPropagation();go(+(li.dataset.s||0))}});
Expand Down
2 changes: 1 addition & 1 deletion tests/load-core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function loadCore(page = 'index.html') {
if (a < 0 || b < 0) throw new Error('core markers not found in ' + page);
const code = html.slice(a, b);
const ctx = vm.createContext({ console, location: { hash: '', pathname: '/', search: '', origin: 'http://x' } });
const names = ['parseMarkdown', 'parseMermaid', 'layout', 'cleanText', 'codeLines', 'STYLES', 'esc', 'mdInline', 'fmtNum'];
const names = ['embedCode', 'embedOpts', 'parseMarkdown', 'parseMermaid', 'layout', 'cleanText', 'codeLines', 'STYLES', 'esc', 'mdInline', 'fmtNum'];
vm.runInContext(code + '\n;globalThis.__core={' + names.join(',') + '};', ctx, { filename: page });
const core = /** @type {any} */ (ctx).__core;
// values from the VM realm have other Array/Object prototypes: hand back plain copies
Expand Down
18 changes: 18 additions & 0 deletions tests/parser.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,21 @@ test('layout spacing: 2D by default', () => {
const g = build('flowchart LR\n A --> B');
assert.equal(Math.round((g.nodes[1].gx - g.nodes[0].gx) * 10) / 10, 3.1);
});

test('embed code: responsive and fixed-size iframe snippets', () => {
const url = 'https://flow-explainer.laravel.cloud/embed#c=zAB-c&s=circuit';
const r = core.embedCode(url, 'r', 'Ticket "order"');
assert.match(r, /^<div style="position:relative;width:100%;aspect-ratio:16\/10">/);
assert.match(r, /src="https:\/\/flow-explainer\.laravel\.cloud\/embed#c=zAB-c&amp;s=circuit"/);
assert.match(r, /title="Ticket &quot;order&quot;"/);
assert.match(r, /allowfullscreen/);
const f = core.embedCode(url, '640x480');
assert.match(f, /^<iframe .* width="640" height="480" /);
assert.match(f, /title="Flow Explainer"/);
});

test('embed options: text panel and view switch flags', () => {
assert.equal(core.embedOpts({ text: true, switchView: true }), '');
assert.equal(core.embedOpts({ text: false, switchView: true }), '&t=0');
assert.equal(core.embedOpts({ text: false, switchView: false }), '&t=0&v=0');
});
Loading