livecode-static/docs/assets/js/fd2b2d45.83a40b61.js
2025-06-11 22:23:49 +08:00

74 lines
18 KiB
JavaScript

"use strict";(self.webpackChunkdocs=self.webpackChunkdocs||[]).push([["833"],{172:function(e,t,s){s.r(t),s.d(t,{default:()=>u,frontMatter:()=>a,metadata:()=>n,assets:()=>c,basicSchemeDemo:()=>d,toc:()=>h,contentTitle:()=>l});var n=JSON.parse('{"id":"languages/scheme","title":"Scheme","description":"Scheme is a classic programming language in the Lisp family. It emphasizes functional programming and domain-specific languages but adapts to other styles.","source":"@site/docs/languages/scheme.mdx","sourceDirName":"languages","slug":"/languages/scheme","permalink":"/docs/languages/scheme","draft":false,"unlisted":false,"editUrl":"https://github.com/live-codes/livecodes/tree/develop/docs/docs/languages/scheme.mdx","tags":[],"version":"current","frontMatter":{},"sidebar":"docsSidebar","previous":{"title":"Sass","permalink":"/docs/languages/sass"},"next":{"title":"SCSS","permalink":"/docs/languages/scss"}}'),i=s("5893"),o=s("65"),r=s("3365");let a={},l="Scheme",c={},d={activeEditor:"script",markup:{language:"html",content:`<div class="container">
<h1>Hello, <span id="title">World</span>!</h1>
<img class="logo" alt="logo" src="https://livecodes.io/livecodes/assets/templates/scheme.svg" />
<p>You clicked <span id="counter">0</span> times.</p>
<button id="counter-button">Click me</button>
</div>
`},style:{language:"css",content:`.container,
.container button {
text-align: center;
font: 1em sans-serif;
}
.logo {
width: 150px;
}
`},script:{language:"scheme",content:`(let ((title "Scheme"))
(set-content! "#title" title))
(let ((counter 0))
(add-handler! "#counter-button" "click"
(lambda (ev)
(set! counter (+ counter 1))
(set-content! "#counter" (number->string counter)))))
; check console
(let ((time-now (date-hour (current-date))))
(console-log
(cond ((< time-now 12) "Good morning")
((< time-now 18) "Good afternoon")
(else "Good evening"))))
`}},h=[{value:"Usage",id:"usage",level:2},{value:"JS Interoperability",id:"js-interoperability",level:3},{value:"Language Info",id:"language-info",level:2},{value:"Name",id:"name",level:3},{value:"Extensions",id:"extensions",level:3},{value:"Editor",id:"editor",level:3},{value:"Compiler",id:"compiler",level:2},{value:"Version",id:"version",level:3},{value:"Code Formatting",id:"code-formatting",level:2},{value:"Limitations",id:"limitations",level:2},{value:"Starter Template",id:"starter-template",level:2},{value:"Links",id:"links",level:2}];function m(e){let t={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",p:"p",strong:"strong",ul:"ul",...(0,o.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(t.header,{children:(0,i.jsx)(t.h1,{id:"scheme",children:"Scheme"})}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.a,{href:"https://www.scheme.org/",children:"Scheme"})," is a classic programming language in the Lisp family. It emphasizes functional programming and domain-specific languages but adapts to other styles."]}),"\n",(0,i.jsxs)(t.p,{children:["In LiveCodes, Scheme code runs in the browser using ",(0,i.jsx)(t.a,{href:"https://www.biwascheme.org/",children:"BiwaScheme"}),", a Scheme interpreter written in JavaScript."]}),"\n",(0,i.jsx)(t.admonition,{title:"Note",type:"info",children:(0,i.jsxs)(t.p,{children:["Lisp language family supported in LiveCodes includes ",(0,i.jsx)(t.a,{href:"/docs/languages/commonlisp",children:"Common Lisp"}),", ",(0,i.jsx)(t.a,{href:"/docs/languages/scheme",children:"Scheme"}),", ",(0,i.jsx)(t.a,{href:"/docs/languages/clojurescript",children:"ClojureScript"})," and ",(0,i.jsx)(t.a,{href:"/docs/languages/fennel",children:"Fennel"}),"."]})}),"\n",(0,i.jsx)(t.h2,{id:"usage",children:"Usage"}),"\n",(0,i.jsxs)(t.p,{children:["LiveCodes runs Scheme code in the browser. BiwaScheme ",(0,i.jsx)(t.a,{href:"https://www.biwascheme.org/doc/features.html",children:"implements"})," most of the features of ",(0,i.jsx)(t.a,{href:"https://small.r7rs.org/",children:"R7RS small"}),", including first-class continuation and tail call optimization."]}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.strong,{children:"Example:"})}),"\n","\n","\n",(0,i.jsx)(r.Z,{config:d,height:"70vh"}),"\n",(0,i.jsx)(t.h3,{id:"js-interoperability",children:"JS Interoperability"}),"\n",(0,i.jsxs)(t.p,{children:["See ",(0,i.jsx)(t.a,{href:"https://www.biwascheme.org/doc/features.html#javascript-language-interface",children:"BiwaScheme docs"}),"."]}),"\n",(0,i.jsx)(t.h2,{id:"language-info",children:"Language Info"}),"\n",(0,i.jsx)(t.h3,{id:"name",children:"Name"}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.code,{children:"scheme"})}),"\n",(0,i.jsx)(t.h3,{id:"extensions",children:"Extensions"}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.code,{children:".scm"})}),"\n",(0,i.jsx)(t.h3,{id:"editor",children:"Editor"}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.code,{children:"script"})}),"\n",(0,i.jsx)(t.h2,{id:"compiler",children:"Compiler"}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.a,{href:"https://www.biwascheme.org/",children:"BiwaScheme"}),", a Scheme interpreter written in JavaScript."]}),"\n",(0,i.jsx)(t.h3,{id:"version",children:"Version"}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.code,{children:"biwascheme"}),": v0.8.0"]}),"\n",(0,i.jsx)(t.h2,{id:"code-formatting",children:"Code Formatting"}),"\n",(0,i.jsxs)(t.p,{children:["Using ",(0,i.jsx)(t.a,{href:"https://shaunlebron.github.io/parinfer/",children:"Parinfer"}),"."]}),"\n",(0,i.jsx)(t.h2,{id:"limitations",children:"Limitations"}),"\n",(0,i.jsxs)(t.p,{children:["BiwaScheme implements most of the features of R7RS small, however some features are not supported.\nSee the ",(0,i.jsx)(t.a,{href:"https://github.com/biwascheme/biwascheme#conformance",children:"BiwaScheme documentation"})," for more information."]}),"\n",(0,i.jsx)(t.h2,{id:"starter-template",children:"Starter Template"}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.a,{href:"https://livecodes.io/?template=scheme",children:"https://livecodes.io/?template=scheme"})}),"\n",(0,i.jsx)(t.h2,{id:"links",children:"Links"}),"\n",(0,i.jsxs)(t.ul,{children:["\n",(0,i.jsx)(t.li,{children:(0,i.jsx)(t.a,{href:"https://www.scheme.org/",children:"Scheme Programming Language"})}),"\n",(0,i.jsx)(t.li,{children:(0,i.jsx)(t.a,{href:"https://www.biwascheme.org/",children:"BiwaScheme"})}),"\n",(0,i.jsx)(t.li,{children:(0,i.jsx)(t.a,{href:"https://www.scheme.com/tspl4/",children:"The Scheme Programming Language (4th Edition)"})}),"\n",(0,i.jsx)(t.li,{children:(0,i.jsx)(t.a,{href:"https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/index.html",children:"Structure and Interpretation of Computer Programs"})}),"\n"]})]})}function u(e={}){let{wrapper:t}={...(0,o.a)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(m,{...e})}):m(e)}},3365:function(e,t,s){s.d(t,{Z:()=>f});var n=s("5893"),i=s("4200"),o=s("7294"),r=s("8294");function a(e){let t=(0,o.useRef)(null),[s,i]=(0,o.useState)(e.className||""),[a,l]=(0,o.useState)(e.style||{}),[c,d]=(0,o.useState)(e.height),[h,m]=(0,o.useState)(),[u,p]=(0,o.useState)(JSON.stringify(e.config||"")),[g,f]=(0,o.useState)("");return(0,o.useEffect)(()=>{if(!t.current)return;let{className:s,style:n,height:o,sdkReady:a,config:c,...v}=e;if(i(s||""),l(n||{}),d(o),h&&g===JSON.stringify(v)){if(u===JSON.stringify(c))return;p(JSON.stringify(c)),"string"==typeof c?fetch(c).then(e=>e.json()).then(e=>{h?.setConfig(e)}):c&&h.setConfig(c)}else f(JSON.stringify(v)),h?.destroy(),(0,r.T)(t.current,{config:c,...v}).then(e=>{m(e),"function"==typeof a&&a(e)})},[e]),(0,o.useEffect)(()=>()=>{h?.destroy()},[]),(0,n.jsx)("div",{ref:t,className:s,style:a,"data-height":c})}var l=s("1858"),c=s("3262"),d=s("1705"),h=s("7645"),m=s("8168"),u=s("8228"),p=s("5050");function g(e){let[t,s]=(0,o.useState)(e.js),[i,r]=(0,o.useState)(e.ts),[a,l]=(0,o.useState)(e.react),[g,f]=(0,o.useState)(e.vue),[v,y]=(0,o.useState)(e.svelte),w="3.7rem",[j,x]=(0,o.useState)(!0),[b,S]=(0,o.useState)(w),C=(0,o.useRef)(null),E=()=>{setTimeout(()=>{S(`calc(${C.current.offsetHeight}px + ${w})`)},5),setTimeout(()=>{S(`calc(${C.current.offsetHeight}px + ${w})`)},255)};return(0,o.useEffect)(()=>{if(c.Z.canUseDOM){let e=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"js";try{return window.prettier?.format(e,{parser:"html"===t?"html":"babel",plugins:window.prettierPlugins})}catch{return e}};s(e(t,"js")),r(e(i,"ts")),l(e(a,"jsx")),f(e(g,"html")),y(e(v,"html"))}},[]),(0,n.jsxs)("details",{className:`alert alert--info ${p.Z.details} ${u.Z.details}`,"data-collapsed":j,style:{height:j?w:b,overflow:"hidden",willChange:"height",transition:`height ${j?"250ms":"265ms"} ease-in-out 0s`,margin:"1em 0"},children:[(0,n.jsx)("summary",{onClick:()=>{x(!j),E()},children:"show code"}),(0,n.jsx)("div",{ref:C,style:{display:"block",overflow:"hidden"},children:(0,n.jsx)("div",{className:p.Z.collapsibleContent,children:(0,n.jsxs)(m.Z,{groupId:"sdk-code",children:[(0,n.jsx)(h.Z,{value:"js",label:"JS",attributes:{onMouseDown:E},children:(0,n.jsx)(d.Z,{language:"js",children:t})}),(0,n.jsx)(h.Z,{value:"ts",label:"TS",attributes:{onMouseDown:E},children:(0,n.jsx)(d.Z,{language:"ts",children:i})}),(0,n.jsx)(h.Z,{value:"react",label:"React",attributes:{onMouseDown:E},children:(0,n.jsx)(d.Z,{language:"jsx",children:a})}),(0,n.jsx)(h.Z,{value:"vue",label:"Vue",attributes:{onMouseDown:E},children:(0,n.jsx)(d.Z,{language:"html",children:g})}),(0,n.jsx)(h.Z,{value:"svelte",label:"Svelte",attributes:{onMouseDown:E},children:(0,n.jsx)(d.Z,{language:"html",children:v})})]})})})]})}function f(e){let{className:t,style:s,showCode:o,height:r,...c}=e,{colorMode:d}=(0,i.I)(),h=e=>JSON.stringify(e,null,2),m=`
import { createPlayground } from 'livecodes';
const options = ${h(c)};
createPlayground('#container', options);
`.trimStart(),u=`
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = ${h(c)};
createPlayground('#container', options);
`.trimStart(),p=`
import LiveCodes from 'livecodes/react';
export default function App() {
const options = ${h(c)};
return (<LiveCodes {...options}></LiveCodes>);
}
`.trimStart(),f=`
<script setup>
import LiveCodes from "livecodes/vue";
const options = ${h(c)};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
`,v=`
<script>
import { onMount } from 'svelte';
import { createPlayground } from 'livecodes';
let options = $state(${h(c)});
let container = $state(null);
onMount(() => {
createPlayground(container, options);
});
</script>
<div bind:this="{container}"></div>
`.trimStart();return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(a,{className:`container_Egsj ${e.className}`,style:{height:r||"50vh",...e.style},appUrl:l.G,...e,config:{theme:d,themeColor:"hsl(215, 8%, 60%)",..."object"==typeof e.config?e.config:{}}}),!1!==e.showCode&&(0,n.jsx)(g,{js:m,ts:u,react:p,vue:f,svelte:v})]})}},8294:function(e,t,s){s.d(t,{T:function(){return i},r:function(){return o}});var n=s(7728);async function i(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};"object"!=typeof e||e instanceof HTMLElement||!e.headless&&"headless"!==e.view||(t=e,e=null);let{config:s={},headless:n,loading:i="lazy",view:r}=t,a=n||"headless"===r,l=null,c=null;if("string"==typeof e)l=document.querySelector(e);else if(e instanceof HTMLElement)l=e;else if(!(a&&"object"==typeof e))throw Error("A valid container element is required.");if(!l){if(a)E(l=document.createElement("div")),document.body.appendChild(l);else throw Error(`Cannot find element: "${e}"`)}let d=new URL(o(t)),h=d.origin;d.searchParams.set("embed","true"),d.searchParams.set("loading",a?"eager":i);let m=globalThis.process;d.searchParams.set("sdkVersion",m?.env?.SDK_VERSION||"latest"),"object"==typeof s&&Object.keys(s).length>0&&d.searchParams.set("config","sdk");let u=t.params;"object"==typeof u&&Object.keys(u).length>0&&JSON.stringify(u).length<1800&&Object.keys(u).forEach(e=>{d.searchParams.set(e,encodeURIComponent(String(u[e])))});let p=!1,g="Cannot call API methods after calling `destroy()`.",f=await new Promise(e=>{if(!l)return;let t=l.dataset.height||l.style.height;if(t&&!a){let e=isNaN(Number(t))?t:t+"px";l.style.height=e}"false"===l.dataset.defaultStyles||a||(l.style.backgroundColor||="#fff",l.style.border||="1px solid black",l.style.borderRadius||="8px",l.style.boxSizing||="border-box",l.style.padding||="0",l.style.width||="100%",l.style.height||=l.style.height||"300px",l.style.minHeight="200px",l.style.flexGrow="1",l.style.overflow||="hidden",l.style.resize||="vertical");let n="livecodes",o=l.querySelector(`iframe.${n}`),r=o||document.createElement("iframe");r.classList.add(n),r.setAttribute("allow","accelerometer; camera; encrypted-media; display-capture; geolocation; gyroscope; microphone; midi; clipboard-read; clipboard-write; web-share"),r.setAttribute("allowtransparency","true"),r.setAttribute("allowpaymentrequest","true"),r.setAttribute("allowfullscreen","true"),r.setAttribute("sandbox","allow-same-origin allow-downloads allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-presentation allow-scripts"),r.setAttribute("loading","eager"===i?"eager":"lazy"),a?E(r):(r.style.height="100%",r.style.minHeight="200px",r.style.width="100%",r.style.margin="0",r.style.border="0",r.style.borderRadius=l.style.borderRadius),addEventListener("message",function e(t){t.source===r.contentWindow&&t.origin===h&&t.data?.type==="livecodes-init"&&(removeEventListener("message",e),c=Number(t.data.payload.appVersion.replace(/^v/,"")))}),(!c||c<46)&&addEventListener("message",function e(t){t.source===r.contentWindow&&t.origin===h&&t.data?.type==="livecodes-get-config"&&(removeEventListener("message",e),r.contentWindow?.postMessage({type:"livecodes-config",payload:s},h))}),r.onload=()=>{e(r)},r.src=d.href,o||l.appendChild(r)}),v=new Promise(e=>{addEventListener("message",function t(s){s.source===f.contentWindow&&s.origin===h&&s.data?.type==="livecodes-ready"&&(removeEventListener("message",t),e(),v.settled=!0)})}),y=()=>p?Promise.reject(g):new Promise(async e=>{v.settled&&e(),f.contentWindow?.postMessage({type:"livecodes-load"},h),await v,e()}),w=(e,t)=>new Promise(async(s,n)=>{if(p)return n(g);await y();let i=L();addEventListener("message",function t(o){if(o.source===f.contentWindow&&o.origin===h&&o.data?.type==="livecodes-api-response"&&o.data?.id===i&&o.data.method===e){removeEventListener("message",t);let e=o.data.payload;e?.error?n(e.error):s(e)}}),f.contentWindow?.postMessage({method:e,id:i,args:t},h)}),j={},x=["load","ready","code","console","tests","destroy"],b=(e,t)=>{if(p)throw Error(g);return x.includes(e)?(w("watch",[e]),j[e]||(j[e]=[]),j[e]?.push(t),{remove:()=>{j[e]=j[e]?.filter(e=>e!==t),j[e]?.length===0&&w("watch",[e,"unsubscribe"])}}):{remove:()=>void 0}},S=e=>({"livecodes-app-loaded":"load","livecodes-ready":"ready","livecodes-change":"code","livecodes-console":"console","livecodes-test-results":"tests","livecodes-destroy":"destroy"})[e];addEventListener("message",async e=>{let t=S(e.data?.type??"");if(e.source!==f.contentWindow||e.origin!==h||!t||!j[t])return;let s=e.data?.payload;j[t]?.forEach(e=>{e(s)})});let C=()=>{Object.values(j).forEach(e=>{e.length=0}),f?.remove?.(),p=!0};function E(e){e.style.position="absolute",e.style.top="0",e.style.visibility="hidden",e.style.opacity="0"}"lazy"===i&&"IntersectionObserver"in window&&new IntersectionObserver((e,t)=>{e.forEach(async e=>{e.isIntersecting&&(await y(),t.unobserve(l))})},{rootMargin:"150px"}).observe(l);let L=()=>(String(Math.random())+Date.now().toFixed()).replace("0.","");return{load:()=>y(),run:()=>w("run"),format:e=>w("format",[e]),getShareUrl:e=>w("getShareUrl",[e]),getConfig:e=>w("getConfig",[e]),setConfig:e=>w("setConfig",[e]),getCode:()=>w("getCode"),show:(e,t)=>w("show",[e,t]),runTests:()=>w("runTests"),onChange:e=>b("code",e),watch:b,exec:function(e){for(var t=arguments.length,s=Array(t>1?t-1:0),n=1;n<t;n++)s[n-1]=arguments[n];return w("exec",[e,...s])},destroy:()=>v.settled?w("destroy").then(C):p?Promise.reject(g):(C(),Promise.resolve())}}function o(){let e,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{appUrl:s="https://livecodes.io",params:i={},config:o={},headless:r,import:a,lite:l,view:c,...d}=t;try{e=new URL(s)}catch{throw Error(`${s} is not a valid URL.`)}let h=new URLSearchParams;Object.entries(d).forEach(t=>{let[s,n]=t;void 0!==n&&e.searchParams.set(s,String(n))});let m="headless"===t.view||r;if(l&&(console.warn('Deprecation notice: "lite" option is deprecated. Use "config: { mode: \'lite\' }" instead.'),"object"==typeof o&&null==o.mode?o.mode="lite":e.searchParams.set("lite","true")),c&&(console.warn('Deprecation notice: The "view" option has been moved to "config.view". For headless mode use "headless: true".'),"object"==typeof o&&null==o.view&&"headless"!==c?o.view=c:e.searchParams.set("view",c)),"string"==typeof o)try{new URL(o),e.searchParams.set("config",encodeURIComponent(o))}catch{throw Error('"config" is not a valid URL or configuration object.')}else o&&"object"==typeof o&&Object.keys(o).length>0&&(o.title&&"Untitled Project"!==o.title&&e.searchParams.set("title",o.title),o.description&&o.description.length>0&&e.searchParams.set("description",o.description),h.set("config","code/"+(0,n.compressToEncodedURIComponent)(JSON.stringify(o))));if(i&&"object"==typeof i&&Object.keys(i).length>0)try{h.set("params",(0,n.compressToEncodedURIComponent)(JSON.stringify(i)))}catch{Object.keys(i).forEach(t=>{e.searchParams.set(t,encodeURIComponent(String(i[t])))})}return a&&e.searchParams.set("x",encodeURIComponent(a)),m&&e.searchParams.set("headless","true"),e.hash=h.toString(),e.href}},65:function(e,t,s){s.d(t,{Z:function(){return a},a:function(){return r}});var n=s(7294);let i={},o=n.createContext(i);function r(e){let t=n.useContext(o);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function a(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:r(e.components),n.createElement(o.Provider,{value:t},e.children)}}}]);