From fd649bc41203ed5919baa07bde3d419c87c7fab1 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 11 Dec 2023 14:31:25 -0800 Subject: [PATCH] Load hello world sample by default in the playground --- packages/playground-website/samples/build.js | 4 ++++ packages/playground-website/samples/hello.tsp | 11 +++++++++++ packages/playground-website/src/index.ts | 1 + packages/playground/src/react/standalone.tsx | 8 +++++++- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/playground-website/samples/hello.tsp diff --git a/packages/playground-website/samples/build.js b/packages/playground-website/samples/build.js index c1e62cac0d..f0df4c090d 100644 --- a/packages/playground-website/samples/build.js +++ b/packages/playground-website/samples/build.js @@ -7,6 +7,10 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); const packageRoot = resolve(__dirname, ".."); await buildSamples_experimental(packageRoot, resolve(__dirname, "dist/samples.js"), { + "Hello World": { + filename: "samples/hello.tsp", + preferredEmitter: "@typespec/openapi3", + }, "API versioning": { filename: "samples/versioning.tsp", preferredEmitter: "@typespec/openapi3", diff --git a/packages/playground-website/samples/hello.tsp b/packages/playground-website/samples/hello.tsp new file mode 100644 index 0000000000..624aa3e0e1 --- /dev/null +++ b/packages/playground-website/samples/hello.tsp @@ -0,0 +1,11 @@ +import "@typespec/http"; + +@service +namespace Hello.World; + +model World { + planet: string; + age: int32; +} + +op hello(): World; diff --git a/packages/playground-website/src/index.ts b/packages/playground-website/src/index.ts index 11f68ed4c8..8d1f8c6b23 100644 --- a/packages/playground-website/src/index.ts +++ b/packages/playground-website/src/index.ts @@ -13,4 +13,5 @@ export const TypeSpecPlaygroundConfig = { "@typespec/protobuf", ], samples, + defaultSample: "Hello World" satisfies keyof typeof samples, } as const; diff --git a/packages/playground/src/react/standalone.tsx b/packages/playground/src/react/standalone.tsx index 256902aea5..51fe95325e 100644 --- a/packages/playground/src/react/standalone.tsx +++ b/packages/playground/src/react/standalone.tsx @@ -29,6 +29,9 @@ export interface ReactPlaygroundConfig extends Partial { readonly importConfig?: LibraryImportOptions; /** Content to show while the playground data is loading(Libraries) */ readonly fallback?: ReactNode; + + /** If no sample or other code is said to be loaded in the playground state load this sample instead. */ + readonly defaultSample?: string; } interface StandalonePlaygroundContext { @@ -85,7 +88,10 @@ export const StandalonePlayground: FunctionComponent = (c defaultContent: context.initialState.content, defaultEmitter: context.initialState.emitter ?? config.defaultEmitter, defaultCompilerOptions: context.initialState.options, - defaultSampleName: context.initialState.sampleName, + defaultSampleName: + context.initialState.sampleName ?? context.initialState.content === undefined + ? config.defaultSample + : undefined, }, [context] );