{"version":3,"sources":["webpack://typescriptlang-org/./src/components/SuppressWhenTouch.tsx","webpack://typescriptlang-org/./src/components/devNav.tsx","webpack://typescriptlang-org/./src/lib/isTouchDevice.ts","webpack://typescriptlang-org/./src/pages/dev/sandbox.tsx"],"names":["SuppressWhenTouch","children","hideOnTouch","useEffect","isTouchDevice","suppressible","document","getElementById","firstChild","removeChild","h4","createElement","textContent","p","appendChild","id","DevNav","props","isActive","str","active","toLowerCase","className","style","display","href","withPrefix","hasTouchScreen","navigator","maxTouchPoints","msMaxTouchPoints","mQ","window","matchMedia","media","matches","UA","userAgent","test","Index","getLoaderScript","src","async","onload","re","global","require","config","paths","vs","sandbox","ignoreDuplicateModules","main","ts","sandboxEnv","parentNode","createTypeScriptSandbox","text","compilerOptions","domID","filetype","editor","focus","setTimeout","querySelectorAll","forEach","codeElement","monaco","colorize","tabSize","then","newHTML","innerHTML","body","title","description","lang","marginTop","borderLeft","role","height","codeSamples","map","code","key","blurb","trim","locale"],"mappings":"qLAOaA,EAAoB,SAAC,GAAoC,IAAlCC,EAAiC,EAAjCA,SAAUC,EAAuB,EAAvBA,YAwB5C,OAtBAC,gBAAU,WACR,IAAIC,SAAiB,CAInB,IADA,IAAMC,EAAeC,SAASC,eAAe,sBACtCF,EAAaG,YAClBH,EAAaI,YAAYJ,EAAaG,YAGxC,GAAIN,EAAa,OAEjB,IAAMQ,EAAKJ,SAASK,cAAc,MAClCD,EAAGE,YAAc,6BAEjB,IAAMC,EAAIP,SAASK,cAAc,KACjCE,EAAED,YAAc,yHAEhBP,EAAaS,YAAYJ,GACzBL,EAAaS,YAAYD,MAG1B,IAED,uBAAKE,GAAG,sBACLd,K,0FC1BMe,EAAS,SAACC,GACrB,IAAMC,EAAW,SAACC,GAAD,OACfF,EAAMG,QAAUH,EAAMG,OAAOC,gBAAkBF,EAAM,SAAW,IAElE,OAAO,uBAAKG,UAAU,wBACpB,sBAAIA,UAAU,OACZ,sBAAIA,UAAU,QAAO,8CACrB,sBAAIC,M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GAAG,UACN,uBAAKO,UAAU,YACb,4BACA,4BACA,4BACA,4BACA,4BACA,4BACA,4BACA,4BACA,6BAEF,qBAAGP,GAAG,kBAAkBgE,KAAK,UAA7B,2BAIF,uBACExD,MAAO,CAAEyD,OAAQ,QAASxD,QAAS,QACnCT,GAAG,2BAMX,uBAAKO,UAAU,6BACb,mCACA,kJAE4D,IAC1D,qBAAGG,KAAK,+CAAR,iBAEK,IALP,MAMK,uCANL,KAQA,sGAEc,IACZ,qBACEA,KAAK,6CACLiD,MAAM,uDAFR,UAQF,yCACA,+CACqB,0CADrB,wCAIA,2BACE,wBAAMpD,UAAU,aAAhB,m0FA4EF,6CACmB,0CADnB,8EAIA,sDACC2D,EAAYC,KAAI,SAAAC,GAAI,OACnB,uBAAK7D,UAAU,aAAa8D,IAAKD,EAAKE,OACpC,yBAAIF,EAAKE,OACT,2BACE,wBAAM/D,UAAU,WAAW6D,EAAKA,KAAKG,aAI3C,mEAC0C,IACxC,qBAAG7D,KAAK,4DAAR,qBAEK,IAJP,WAKW,IACT,qBAAGA,KAAK,kDAAR,yBANF,UAkBZ,mBAAgBR,GAAD,OACb,gBAAC,IAAD,CAAMsE,OAAO,MACX,gBAAChD,EAAUtB,KAIf,IAAMgE,EAAc,CAClB,CACEI,MAAO,mDACPF,KAAK,oKAMP,CACEE,MAAO,oCACPF,KAAK,mIAKP,CACEE,MAAO,qCACPF,KAAK,gUAQP,CACEE,MAAO,mDACPF,KAAK,4bAeP,CACEE,MAAO,oCACPF,KAAK,kZAoBP,CACEE,MAAO,8BACPF,KAAK","file":"component---src-pages-dev-sandbox-tsx-924ca291a49608609b66.js","sourcesContent":["import React, { useEffect } from \"react\"\nimport { isTouchDevice } from \"../lib/isTouchDevice\"\n\n/**\n * A React component which will remove its children (at runtime!)\n * from the hierarchy if we're on a touch device\n */\nexport const SuppressWhenTouch = ({ children, hideOnTouch }: any) => {\n\n useEffect(() => {\n if (isTouchDevice()) {\n // It's touch, so let's kill the content in the child and \n // replace it with a message that this section isn't good for mobile\n const suppressible = document.getElementById(\"touch-suppressible\")!\n while (suppressible.firstChild) {\n suppressible.removeChild(suppressible.firstChild)\n }\n\n if (hideOnTouch) return\n\n const h4 = document.createElement(\"h4\")\n h4.textContent = \"Section best on a computer\"\n\n const p = document.createElement(\"p\")\n p.textContent = \"This part of the site does not run well on a touch-oriented browser. We recommend switching to a computer to carry on.\"\n\n suppressible.appendChild(h4)\n suppressible.appendChild(p)\n }\n\n }, [])\n return (\n
\n {children}\n
)\n}\n","import * as React from \"react\"\nimport { withPrefix } from \"gatsby\"\n\ninterface DevNavProps {\n active?: string\n}\n\nexport const DevNav = (props: DevNavProps) => {\n const isActive = (str) =>\n props.active && props.active.toLowerCase() === str ? \"active\" : \"\"\n\n return \n}\n","/** Based on https://developer.mozilla.org/docs/Web/HTTP/Browser_detection_using_the_user_agent */\nexport function isTouchDevice() {\n var hasTouchScreen = false\n if (\"maxTouchPoints\" in navigator) {\n hasTouchScreen = navigator.maxTouchPoints > 0\n } else if (\"msMaxTouchPoints\" in navigator) {\n // @ts-ignore\n hasTouchScreen = navigator.msMaxTouchPoints > 0\n } else {\n var mQ =\n typeof window !== \"undefined\" &&\n \"matchMedia\" in window &&\n matchMedia(\"(pointer:coarse)\")\n if (mQ && mQ.media === \"(pointer:coarse)\") {\n hasTouchScreen = !!mQ.matches\n } else if (\"orientation\" in window) {\n hasTouchScreen = true // deprecated, but good fallback\n } else {\n // Only as a last resort, fall back to user agent sniffing\n // @ts-ignore\n var UA = navigator.userAgent\n hasTouchScreen =\n /\\b(BlackBerry|webOS|iPhone|IEMobile)\\b/i.test(UA) ||\n /\\b(Android|Windows Phone|iPad|iPod)\\b/i.test(UA)\n }\n }\n return hasTouchScreen\n}\n","import React, { useEffect } from \"react\"\nimport { Layout } from \"../../components/layout\"\nimport { withPrefix, graphql } from \"gatsby\"\n\nimport \"./dev.scss\"\nimport { Intl } from \"../../components/Intl\"\nimport { DevNav } from \"../../components/devNav\"\nimport { isTouchDevice } from \"../../lib/isTouchDevice\"\nimport { SuppressWhenTouch } from \"../../components/SuppressWhenTouch\"\n\ntype Props = {}\n\nconst Index: React.FC = props => {\n useEffect(() => {\n // Don't even bother getting monaco\n if (isTouchDevice()) {\n return\n }\n\n const getLoaderScript = document.createElement(\"script\")\n getLoaderScript.src = withPrefix(\"/js/vs.loader.js\")\n getLoaderScript.async = true\n getLoaderScript.onload = () => {\n // @ts-ignore\n const re: any = global.require\n\n re.config({\n paths: {\n vs: \"https://typescript.azureedge.net/cdn/4.0.5/monaco/min/vs\",\n sandbox: withPrefix(\"/js/sandbox\"),\n },\n ignoreDuplicateModules: [\"vs/editor/editor.main\"],\n })\n\n re(\n [\n \"vs/editor/editor.main\",\n \"vs/language/typescript/tsWorker\",\n \"sandbox/index\",\n ],\n async (\n main: typeof import(\"monaco-editor\"),\n ts: typeof import(\"typescript\"),\n sandboxEnv: typeof import(\"@typescript/sandbox\")\n ) => {\n const initialCode = `import {markdown, danger} from \"danger\"\n\nexport default async function () {\n // Check for new @types in devDependencies\n const packageJSONDiff = await danger.git.JSONDiffForFile(\"package.json\")\n const newDeps = packageJSONDiff.devDependencies.added\n const newTypesDeps = newDeps?.filter(d => d.includes(\"@types\")) ?? []\n\n if (newTypesDeps.length){\n markdown(\"Added new types packages \" + newTypesDeps.join(\", \"))\n }\n}`\n const isOK = main && ts && sandboxEnv\n if (isOK) {\n document\n .getElementById(\"loader\")!\n .parentNode?.removeChild(document.getElementById(\"loader\")!)\n }\n\n document.getElementById(\"monaco-editor-embed\")!.style.display =\n \"block\"\n\n const sandbox = sandboxEnv.createTypeScriptSandbox(\n {\n text: initialCode,\n compilerOptions: {},\n domID: \"monaco-editor-embed\",\n filetype: \"ts\",\n },\n main,\n ts\n )\n sandbox.editor.focus()\n\n setTimeout(() => {\n document.querySelectorAll(\".html-code\").forEach(codeElement => {\n sandbox.monaco.editor\n .colorize(codeElement.textContent || \"\", \"html\", { tabSize: 2 })\n .then(newHTML => {\n codeElement.innerHTML = newHTML\n })\n })\n\n document.querySelectorAll(\".ts-code\").forEach(codeElement => {\n sandbox.monaco.editor\n .colorize(codeElement.textContent || \"\", \"typescript\", {\n tabSize: 2,\n })\n .then(newHTML => {\n codeElement.innerHTML = newHTML\n })\n })\n }, 300)\n }\n )\n }\n\n document.body.appendChild(getLoaderScript)\n }, [])\n\n return (\n <>\n \n
\n \n
\n
\n

TypeScript Sandbox

\n

\n A DOM library for interacting with TypeScript and JavaScript\n code, which powers the heart of the{\" \"}\n TypeScript playground\n

\n

You can use the TypeScript sandbox for:

\n
    \n
  • \n Building IDE-like experiences for people to explore your\n library's API\n
  • \n
  • \n Building interactive web tools which use TypeScript, with a\n lot of the Playgrounds developer experience for free\n
  • \n
\n

\n For example, the sandbox to the side has grabbed the Types for{\" \"}\n DangerJS with no\n modifications for this code sample. This is because the\n Playground's Automatic Type Acquisition is enabled by default.\n It will also look for the same parameters for code, and\n selection indexes inside the URL.\n

\n

\n Try clicking{\" \"}\n \n this URL\n {\" \"}\n to see that in action.{\" \"}\n

\n

\n This library builds on top of the{\" \"}\n \n Monaco Editor\n \n , providing a higher level API but offering access to all the\n lower-level APIs via a single sandbox object.\n

\n

\n You can find the code for the TypeScript Sandbox inside the{\" \"}\n \n microsoft/TypeScript-Website\n {\" \"}\n mono-repo.\n

\n
\n\n \n \n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n

\n Downloading Sandbox...\n

\n
\n \n
\n \n
\n\n
\n

Usage

\n

\n A sandbox uses the same tools as monaco-editor, meaning this\n library is shipped as an AMD bundle which you can use the{\" \"}\n \n VSCode Loader\n {\" \"}\n to require.\n

\n

\n Because we need it for the TypeScript website, you can use our\n hosted copy{\" \"}\n \n here.\n \n

\n\n

Get Started

\n

\n Create a new file: index.html and paste this code\n into that file.\n

\n
\n              \n                {`\n\n  \n    \n  \n  
Loading...
\n
\n \n`}\n \n
\n

\n Opening the file index.html in a web browser will\n load up the same sandbox up at the top of the page.\n

\n

Some examples of the API

\n {codeSamples.map(code => (\n
\n

{code.blurb}

\n
\n                  {code.code.trim()}\n                
\n
\n ))}\n

\n The API is mainly a light shim over the{\" \"}\n \n monaco-editor API\n {\" \"}\n with the{\" \"}\n \n monaco-typescript API\n \n .\n

\n
\n \n \n \n )\n}\n\nexport default (props: Props) => (\n \n \n \n)\n\nconst codeSamples = [\n {\n blurb: \"Converting the user's TypeScript into JavaScript\",\n code: `const sandbox = createTypeScriptSandbox(sandboxConfig, main, ts)\n\n// Async because it needs to go \nconst js = await sandbox.getRunnableJS()\nconsole.log(js)`,\n },\n {\n blurb: \"Get the DTS for the user's editor\",\n code: `const sandbox = createTypeScriptSandbox(sandboxConfig, main, ts)\n\nconst dts = await sandbox.getDTSForCode()\nconsole.log(dts)`,\n },\n {\n blurb: \"Make a request for an LSP response\",\n code: `const sandbox = createTypeScriptSandbox(sandboxConfig, main, ts)\n\n// A worker here is a web-worker, set up by monaco-typescript\n// which does the computation in the background \nconst worker = await sandbox.getWorkerProcess()\nconst definitions = await client.getDefinitionAtPosition(model.uri.toString(), 6)\n `,\n },\n {\n blurb: \"Change compiler flags using a few different APIs\",\n code: `const sandbox = createTypeScriptSandbox(sandboxConfig, main, ts)\n\n// Hook in to all changes to the compiler\nsandbox.setDidUpdateCompilerSettings((newOptions) => {\n console.log(\"Compiler settings changed: \", newOptions)\n})\n\n// Update via key value\nsandbox.updateCompilerSetting(\"allowJs\", true)\n// Update via an object\nsandbox.updateCompilerSettings({ jsx: 0 })\n// Replace the compiler settings\nsandbox.setCompilerSettings({})\n`,\n },\n {\n blurb: \"Highlight some code in the editor\",\n code: `const sandbox = createTypeScriptSandbox(sandboxConfig, main, ts)\n\nconst start = {\n lineNumber: 0,\n column: 0\n}\n\nconst end = {\n lineNumber: 0,\n column: 4\n}\n\nconst decorations = sandbox.editor.deltaDecorations([], [\n {\n range: new sandbox.monaco.Range(start.lineNumber, start.column, end.lineNumber, end.column),\n options: { inlineClassName: 'error-highlight' },\n },\n])\n`,\n },\n {\n blurb: \"Create your own playground.\",\n code: `const sandbox = createTypeScriptSandbox(sandboxConfig, main, ts)\n\n// Use a script to make a JSON file like:\n// { \n// \"file:///node_modules/types/keyboard/index.d.ts\": \"export const enterKey: string\"\n// }\n//\n// Where the keys are the paths, and the values are the source-code. The sandbox\n// will use the node resolution lookup strategy by default.\n\nconst dtsFiles = {} \n\nObject.keys(dtsFiles).forEach(path => {\n sandbox.languageServiceDefaults.addExtraLib(dts[path], path);\n});\n`,\n },\n]\n"],"sourceRoot":""}