From b52eb9e17ca853919f99b9babd948830093f715d Mon Sep 17 00:00:00 2001 From: solutionforest team Date: Wed, 18 Nov 2026 01:19:58 +0000 Subject: [PATCH 1/2] feat: copy ready-to-paste iframe embed code from the share box Like YouTube's Embed: the Copy client link box now has an "Embed on a website" section with the '; + return '
\n \n
'; +} const embedLink=async()=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts(); /** @param {'2d'|'3d'} dim */ function viewHref(dim){ @@ -2314,9 +2338,15 @@

Flow
Explainer

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(),$('#embedSize').value,(doc&&doc.docTitle)||'Flow Explainer')}; + $('#embedSize').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(); try{await navigator.clipboard.writeText(url);toast('Embed link copied')}catch(e){prompt('Copy this embed link',url)} diff --git a/README.md b/README.md index 4b4871c..b8f6a23 100644 --- a/README.md +++ b/README.md @@ -276,17 +276,22 @@ 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. +- **Copy embed code** copies the ready-to-paste ` +
+ +
``` +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 diff --git a/index.html b/index.html index 2d9625d..b649d3d 100644 --- a/index.html +++ b/index.html @@ -213,6 +213,12 @@ .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} /* about dialog */ button.brand{border:0;background:none;padding:0;color:inherit;cursor:pointer;border-radius:6px} @@ -409,7 +415,11 @@

Flow
Explainer

  • Copies stay where you paste them. Chat apps, email and browser history keep the full link.
  • Opens view-only: no Edit or Snapshot.
    - +
    +
    Embed on a website
    + +
    +
    @@ -2292,6 +2302,20 @@

    Flow
    Explainer

    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(); +/** + * 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,'&').replace(/"/g,'"').replace(/'; + return '
    \n \n
    '; +} const embedLink=async()=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts(); /** @param {'2d'|'3d'} dim */ function viewHref(dim){ @@ -2330,9 +2354,15 @@

    Flow
    Explainer

    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(),$('#embedSize').value,(doc&&doc.docTitle)||'Flow Explainer')}; + $('#embedSize').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(); try{await navigator.clipboard.writeText(url);toast('Embed link copied')}catch(e){prompt('Copy this embed link',url)} diff --git a/tests/load-core.mjs b/tests/load-core.mjs index 35edb33..bb4cfc1 100644 --- a/tests/load-core.mjs +++ b/tests/load-core.mjs @@ -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', '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 diff --git a/tests/parser.test.mjs b/tests/parser.test.mjs index f3b16b4..518d7ad 100644 --- a/tests/parser.test.mjs +++ b/tests/parser.test.mjs @@ -137,3 +137,15 @@ 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, /^
    /); + assert.match(r, /src="https:\/\/flow-explainer\.laravel\.cloud\/embed#c=zAB-c&s=circuit"/); + assert.match(r, /title="Ticket "order""/); + assert.match(r, /allowfullscreen/); + const f = core.embedCode(url, '640x480'); + assert.match(f, /^'; return '
    \n \n
    '; } -const embedLink=async()=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts(); +/** + * 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||''; @@ -2328,6 +2344,7 @@

    Flow
    Explainer

    +'
      '+rows+'
    '; } 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); @@ -2344,11 +2361,12 @@

    Flow
    Explainer

    $('#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(),$('#embedSize').value,(doc&&doc.docTitle)||'Flow Explainer')}; - $('#embedSize').onchange=fillEmbed; + $('#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))}}); diff --git a/README.md b/README.md index b8f6a23..585f93f 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,8 @@ Use the compact player at `/embed` (2D) or `/embed/3d` (3D) with the same `#c= 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 `'; return '
    \n \n
    '; } -const embedLink=async()=>pageUrl('/embed'+(is3dView()?'/3d':''))+'#c='+await encode(/** @type {any} */(S).md)+linkOpts(); +/** + * 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||''; @@ -2344,6 +2360,7 @@

    Flow
    Explainer

    +'
      '+rows+'
    '; } 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); @@ -2360,11 +2377,12 @@

    Flow
    Explainer

    $('#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(),$('#embedSize').value,(doc&&doc.docTitle)||'Flow Explainer')}; - $('#embedSize').onchange=fillEmbed; + $('#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))}}); diff --git a/tests/load-core.mjs b/tests/load-core.mjs index bb4cfc1..6c46b3b 100644 --- a/tests/load-core.mjs +++ b/tests/load-core.mjs @@ -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 = ['embedCode', '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 diff --git a/tests/parser.test.mjs b/tests/parser.test.mjs index 518d7ad..1e0c4f8 100644 --- a/tests/parser.test.mjs +++ b/tests/parser.test.mjs @@ -149,3 +149,9 @@ test('embed code: responsive and fixed-size iframe snippets', () => { assert.match(f, /^