-
Notifications
You must be signed in to change notification settings - Fork 85
feat: add 'Open in StackBlitz'-button #219
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc8756c
feat: Add 'Open in StackBlitz'-button
AriPerkkio f5d402e
feat: Icon-only button
AriPerkkio 9353bd4
fix: pick only text files
AriPerkkio 826d5ee
fix: move file resolving to `runner.takeSnapshot`
AriPerkkio b010425
fix: code review
AriPerkkio 4d80275
fix: code review
AriPerkkio 946807e
fix: code review
AriPerkkio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/astro/src/default/components/OpenInStackblitzLink.astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<button | ||
transition:persist | ||
title="Open in StackBlitz" | ||
data-id="open-in-stackblitz" | ||
class="flex items-center font-size-3.5 text-tk-elements-topBar-iconButton-iconColor hover:text-tk-elements-topBar-iconButton-iconColorHover transition-theme bg-tk-elements-topBar-iconButton-backgroundColor hover:bg-tk-elements-topBar-iconButton-backgroundColorHover p-1 rounded-md" | ||
> | ||
<svg viewBox="0 0 28 28" aria-hidden="true" height="24" width="24"> | ||
<path fill="currentColor" d="M12.747 16.273h-7.46L18.925 1.5l-3.671 10.227h7.46L9.075 26.5l3.671-10.227z"></path> | ||
</svg> | ||
</button> | ||
|
||
<script> | ||
import StackBlitzSDK from '@stackblitz/sdk'; | ||
import { tutorialStore } from '../components/webcontainer.js'; | ||
|
||
// initialize handlers on each page load as it's possible some pages disable openInStackBlitzLink | ||
document.addEventListener('astro:page-load', onInit); | ||
|
||
function onInit() { | ||
const buttons = document.querySelectorAll('[data-id="open-in-stackblitz"]'); | ||
buttons.forEach((button) => button.addEventListener('click', onClick)); | ||
} | ||
|
||
function onClick() { | ||
const lesson = tutorialStore.lesson; | ||
|
||
if (!lesson) { | ||
throw new Error('Missing lesson'); | ||
} | ||
|
||
const files: Record<string, string> = {}; | ||
|
||
// first add template files | ||
for (const [filePath, value] of Object.entries(tutorialStore.template || {})) { | ||
files[removeLeadingSlash(filePath)] = value.toString(); | ||
} | ||
|
||
// next overwrite with files from editor | ||
for (const { filePath, value } of Object.values(tutorialStore.documents.get())) { | ||
files[removeLeadingSlash(filePath)] = value.toString(); | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const packageJson = parseJson(files['package.json']); | ||
|
||
// add start commands | ||
if (files['package.json']) { | ||
const mainCommand = resolveCommand(lesson.data.mainCommand); | ||
const prepareCommands = (lesson.data.prepareCommands || []).map(resolveCommand); | ||
const startCommand = [...prepareCommands, mainCommand].filter(Boolean).join(' && '); | ||
|
||
files['package.json'] = JSON.stringify({ ...packageJson, stackblitz: { startCommand } }, null, 2); | ||
} | ||
|
||
StackBlitzSDK.openProject({ | ||
title: lesson.data.title || 'Lesson', | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: `${lesson.part.title} / ${lesson.chapter.title}`, | ||
template: 'node', | ||
files, | ||
}); | ||
|
||
function resolveCommand(command: NonNullable<typeof lesson>['data']['mainCommand']): string { | ||
if (!command) { | ||
return ''; | ||
} | ||
|
||
if (typeof command === 'string') { | ||
return command; | ||
} | ||
|
||
if (Array.isArray(command)) { | ||
return command[0]; | ||
} | ||
|
||
return command.command; | ||
} | ||
|
||
function removeLeadingSlash(filePath: string) { | ||
if (filePath.startsWith('/')) { | ||
return filePath.slice(1); | ||
} | ||
|
||
return filePath; | ||
} | ||
|
||
function parseJson(deserialized: undefined | string): any { | ||
try { | ||
return JSON.parse(deserialized || '{}'); | ||
} catch { | ||
return {}; | ||
} | ||
} | ||
} | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/template/src/content/tutorial/2-advanced/1-unicorn/meta.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
type: chapter | ||
title: The first chatper in part 2 | ||
openInStackBlitzLink: false | ||
--- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 is required even when
transition:persist
is used due to:openInStackBlitzLink
on a single lessonopenInStackBlitzLink
disabled. Script tags are not run (due to ViewTransition I think), handler is not attached.Using
page-load
works always.