-
Notifications
You must be signed in to change notification settings - Fork 1k
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
sdk: export more types, add showSidebar option and vm.editor.setCurrentFile #1837
Changes from 8 commits
823420a
f099742
a810a83
ed3e0f6
604569b
93d74ae
08ff58a
0e28409
ec2b26e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Number of milliseconds between attempts to get a response from an embedded frame | ||
*/ | ||
export const connectInterval = 500; | ||
|
||
/** | ||
* How many times should we try to get an init response from an embedded frame | ||
*/ | ||
export const connectMaxAttempts = 20; | ||
|
||
/** | ||
* Default height attribute for iframes | ||
*/ | ||
export const defaultFrameHeight = 300; | ||
|
||
/** | ||
* Origin of the StackBlitz instance | ||
*/ | ||
export const defaultOrigin = 'https://stackblitz.com'; | ||
|
||
/** | ||
* List of supported template names. | ||
*/ | ||
export const projectTemplates = [ | ||
'angular-cli', | ||
'create-react-app', | ||
'html', | ||
'javascript', | ||
'node', | ||
'polymer', | ||
'typescript', | ||
'vue', | ||
] as const; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { EmbedOptions, OpenOptions } from './interfaces'; | ||
|
||
const DEFAULT_ORIGIN = 'https://stackblitz.com'; | ||
const DEFAULT_FRAME_HEIGHT = '300'; | ||
import type { EmbedOptions, OpenOptions } from './interfaces'; | ||
import { defaultFrameHeight, defaultOrigin } from './constants'; | ||
import { buildParams } from './params'; | ||
|
||
/** | ||
* Pseudo-random id string for internal accounting. | ||
|
@@ -12,7 +11,7 @@ export function genID() { | |
} | ||
|
||
export function openUrl(route: string, options?: OpenOptions) { | ||
return `${getOrigin(options)}${route}${buildProjectQuery(options)}`; | ||
return `${getOrigin(options)}${route}${buildParams(options)}`; | ||
} | ||
|
||
export function embedUrl(route: string, options?: EmbedOptions) { | ||
|
@@ -22,62 +21,14 @@ export function embedUrl(route: string, options?: EmbedOptions) { | |
if (options && typeof options === 'object') { | ||
Object.assign(config, options); | ||
} | ||
return `${getOrigin(config)}${route}${buildProjectQuery(config)}`; | ||
return `${getOrigin(config)}${route}${buildParams(config)}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for refactoring this! |
||
} | ||
|
||
function getOrigin(options: OpenOptions | EmbedOptions = {}) { | ||
function getOrigin(options: OpenOptions & EmbedOptions = {}) { | ||
if (typeof options.origin === 'string') { | ||
return options.origin; | ||
} | ||
return DEFAULT_ORIGIN; | ||
} | ||
|
||
function buildProjectQuery(options: OpenOptions | EmbedOptions = {}) { | ||
const params: string[] = []; | ||
|
||
if (options.forceEmbedLayout) { | ||
params.push('embed=1'); | ||
} | ||
|
||
if (options.clickToLoad) { | ||
params.push('ctl=1'); | ||
} | ||
|
||
for (const file of Array.isArray(options.openFile) ? options.openFile : [options.openFile]) { | ||
if (typeof file === 'string' && file.trim() !== '') { | ||
params.push(`file=${encodeURIComponent(file.trim())}`); | ||
} | ||
} | ||
|
||
if (options.view === 'preview' || options.view === 'editor') { | ||
params.push(`view=${options.view}`); | ||
} | ||
|
||
if (options.theme === 'light' || options.theme === 'dark') { | ||
params.push(`theme=${options.theme}`); | ||
} | ||
|
||
if (options.hideExplorer) { | ||
params.push('hideExplorer=1'); | ||
} | ||
|
||
if (options.hideNavigation) { | ||
params.push('hideNavigation=1'); | ||
} | ||
|
||
if (options.hideDevTools) { | ||
params.push('hideDevTools=1'); | ||
} | ||
|
||
if ( | ||
typeof options.devToolsHeight === 'number' && | ||
options.devToolsHeight >= 0 && | ||
options.devToolsHeight <= 100 | ||
) { | ||
params.push(`devToolsHeight=${Math.round(options.devToolsHeight)}`); | ||
} | ||
|
||
return params.length ? `?${params.join('&')}` : ''; | ||
return defaultOrigin; | ||
} | ||
|
||
export function replaceAndEmbed( | ||
|
@@ -122,7 +73,7 @@ function setFrameDimensions(frame: HTMLIFrameElement, options?: EmbedOptions) { | |
} | ||
|
||
if (!frame.height) { | ||
frame.height = DEFAULT_FRAME_HEIGHT; | ||
frame.height = `${defaultFrameHeight}`; | ||
} | ||
if (!frame.width) { | ||
frame.setAttribute('style', 'width:100%;'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,40 @@ | ||
export type { Project, OpenOptions, EmbedOptions } from './interfaces'; | ||
export type { VM } from './VM'; | ||
import { | ||
connect, | ||
embedGithubProject, | ||
embedProject, | ||
embedProjectId, | ||
openGithubProject, | ||
openProject, | ||
openProjectId, | ||
} from './lib'; | ||
|
||
import * as StackBlitzSDK from './lib'; | ||
export default StackBlitzSDK; | ||
// Explicitly export public types (vs using `export * from './interfaces'`), | ||
// so that additions to interfaces don't become a part of our public API by mistake. | ||
export type { | ||
Project, | ||
ProjectOptions, | ||
ProjectDependencies, | ||
ProjectFiles, | ||
ProjectSettings, | ||
ProjectTemplate, | ||
EmbedOptions, | ||
OpenOptions, | ||
OpenFileOption, | ||
UiThemeOption, | ||
UiViewOption, | ||
} from './interfaces'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New typing feature: exporting more types from |
||
export type { FsDiff, VM } from './vm'; | ||
|
||
// Export a single object with methods, for compatibility with UMD and CommonJS. | ||
// Ideally we would also have named exports, but that can create incompatibilities | ||
// with some bundlers, and microbundle doesn't support it: | ||
// https://github.com/developit/microbundle/issues/712 | ||
export default { | ||
connect, | ||
embedGithubProject, | ||
embedProject, | ||
embedProjectId, | ||
openGithubProject, | ||
openProject, | ||
openProjectId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is technically the same as what we had before with
EXCEPT that if we add a new export to |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This array is used to warn users when they use an unsupported template type (with a
console.warn
), and is also used as the source of truth for theProjectTemplate
type: