Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load hello world sample by default in the playground #2746

Closed
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
Copy link
Member Author

@timotheeguerin timotheeguerin Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be the best idea to add actually, as typespec is protocol agnostic loading a protocol specific sample immediately might have negative results

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/playground",
"comment": "Add a new `defaultSample` option to the standalone playground that will load this sample automatically if no sample or content was specified in the playground state",
"type": "none"
}
],
"packageName": "@typespec/playground"
}
2 changes: 1 addition & 1 deletion packages/playground-website/e2e/ui.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.describe("playground UI tests", () => {
});

test("save code with ctrl/cmd+S", async ({ page }) => {
await page.goto(host);
await page.goto(`${host}/?c=`);
const typespecEditorContainer = page.locator("_react=TypeSpecEditor");
await typespecEditorContainer.click();
await typespecEditorContainer.pressSequentially("op sharedCode(): string;");
Expand Down
4 changes: 4 additions & 0 deletions packages/playground-website/samples/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions packages/playground-website/samples/hello.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "@typespec/http";

@service
namespace Hello.World;

model World {
planet: string;
age: int32;
}

op hello(): World;
1 change: 1 addition & 0 deletions packages/playground-website/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const TypeSpecPlaygroundConfig = {
"@typespec/protobuf",
],
samples,
defaultSample: "Hello World" satisfies keyof typeof samples,
} as const;
8 changes: 7 additions & 1 deletion packages/playground/src/react/standalone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export interface ReactPlaygroundConfig extends Partial<PlaygroundProps> {
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 {
Expand Down Expand Up @@ -85,7 +88,10 @@ export const StandalonePlayground: FunctionComponent<ReactPlaygroundConfig> = (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]
);
Expand Down
Loading