Skip to content

Conversation

@fvsch
Copy link
Contributor

@fvsch fvsch commented Mar 9, 2023

This PR adds support for three options in EmbedOptions and OpenOptions:

  • startScript (WebContainers projects only): specify the npm script to run on project load
  • zenMode (in OpenOptions only): turns on zen mode for the classic editor
  • sidebarView: select which sidebar view to show initially

Those query parameters are already supported on stackblitz.com. They may or may not be supported on StackBlitz EE depending on the deployed version; when not supported, they would be ignored.

@fvsch fvsch requested review from sulco and ykmsd March 9, 2023 21:08
@fvsch fvsch marked this pull request as draft March 10, 2023 10:51
@fvsch fvsch removed request for sulco and ykmsd March 10, 2023 10:51
@fvsch fvsch changed the title Add support for the zenMode param in OpenOptions Add params: startScript, zenMode, sidebarView Mar 10, 2023
@fvsch fvsch force-pushed the fvsch/add-zenmode-param branch from b240208 to 814d8ce Compare March 10, 2023 17:19
@fvsch fvsch force-pushed the fvsch/add-zenmode-param branch from 814d8ce to 9511de3 Compare March 10, 2023 17:29
/**
* Supported editor themes
*/
export const UI_THEMES = ['light', 'dark'] as const;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Defined available themes and UI views as arrays of strings, which we can use in two ways:

  1. Derive the TypeScript union type for the theme and view options from those arrays (in src/interfaces.ts).
  2. Use those arrays when validating user-provided values at runtime (in src/params.ts)

Before that, we had separate sources of truth.

* By default, the Console will appear collapsed, and can be opened by users.
* This option is ignored in WebContainers-based projects.
*/
devToolsHeight?: number;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a bit of noise in this PR because I alphabetized the keys in this interface.

It might be easier to open this file as a standalone file, and review the types and descriptions for startScript, sidebarView and zenMode that way.

* Defaults to looking for a `dev` script or a `start` script in WebContainers-based project. Ignored in EngineBlock projects.
*/
showSidebar?: boolean;
startScript?: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically we support multiple startScript parameters (up to 3), so the correct type here would be string | [string] | [string, string] | [string, string, string].

But our support for multiple startScript parameters was driven by a rare need, and is partly broken: we open several terminals, and run in parallel, but don't wait for the installation of dependencies to be done before running those scripts.

So if you specify startScript=build&startScript=serve, we will have two Terminals running this in parallel:

  1. npm install && npm run build
  2. npm run serve

And if the serve script relies on any dependency, or on the build script being done, it will fail.

We do support startScript=build,serve which will run npm install && npm run build && npm run serve sequentially.

Given that, I think it's a good move to restrict the SDK to only emit a single startScript query parameter.

showSidebar: (value: ParamOptions['showSidebar']) => booleanParam('showSidebar', value),
sidebarView: (value: ParamOptions['sidebarView']) =>
enumParam('sidebarView', value, UI_SIDEBAR_VIEWS),
startScript: (value: ParamOptions['startScript']) => stringParams('startScript', value),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The actual implementation that accepts the new options and produces query parameters is happening here.

Most of the changes in this PR are types, descriptions, and updates to unit tests. The implementation itself is just allowing values to be passed along from an options object into a query string, with simple validation & encoding.

if (typeof value === 'number' && !Number.isNaN(value)) {
const clamped = Math.min(100, Math.max(0, value));
return `${name}=${Math.round(clamped)}`;
return `${name}=${encodeURIComponent(Math.round(clamped))}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don’t think Math.round can produce an output that contains problematic characters when stringified, but it doesn't cost much to call encodeURIComponent to encode the value anyway.

allowList: readonly string[] = []
): string {
if (allowList.includes(value)) {
return `${name}=${encodeURIComponent(value)}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So far the lists of enum values we use don't contain characters that need encoding, but it doesn't cost much to encode values with encodeURIComponent just in case.

return values
.filter((val) => typeof val === 'string' && val.trim() !== '')
.map((val) => `${name}=${encodeURIComponent(val!.trim())}`);
.map((val) => `${name}=${encodeURIComponent(val!)}`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We stop trimming user-provided strings. That means that if a user provides a file path like ' src/components/App.js' but the ' src' folder doesn't exist (only the 'src' folder), things might break.

On the other hand, if they're trying to use a npm script name that had whitespace for some reason, e.g. "serve ", then this makes it possible.

Better to have less hidden magic that our users cannot work around, IMHO.

@fvsch fvsch requested review from sulco, sylwiavargas and ykmsd March 10, 2023 17:43
@fvsch fvsch marked this pull request as ready for review March 10, 2023 17:44
Copy link

@sylwiavargas sylwiavargas left a comment

Choose a reason for hiding this comment

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

thank you for this PR and your thorough walkthrough 🙏

@fvsch fvsch merged commit 00f5671 into main Mar 13, 2023
@fvsch fvsch deleted the fvsch/add-zenmode-param branch March 16, 2023 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants