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

feat: open up relevant file on sandbox load on all example pages #232

Merged
merged 1 commit into from
May 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react'
import { FaExternalLinkAlt } from 'react-icons/fa'
import { DocTitle } from '~/components/DocTitle'
import { getBranch, getLibrary } from '~/libraries'
import { getInitialSandboxFileName } from '~/utils/sandbox'
import { seo } from '~/utils/seo'
import { capitalize, slugToTitle } from '~/utils/utils'

Expand Down Expand Up @@ -38,19 +39,21 @@ export default function Example() {
setIsDark(window.matchMedia?.(`(prefers-color-scheme: dark)`).matches)
}, [])

const sandboxFirstFileName = getInitialSandboxFileName(framework, libraryId)

const githubUrl = `https://github.com/${library.repo}/tree/${branch}/examples/${examplePath}`
// preset=node can be removed once Stackblitz runs Angular as webcontainer by default
// See https://github.com/stackblitz/core/issues/2957
const stackBlitzUrl = `https://stackblitz.com/github/${
library.repo
}/tree/${branch}/examples/${examplePath}?embed=1&theme=${
isDark ? 'dark' : 'light'
}&preset=node`
}&preset=node&file=${sandboxFirstFileName}`
const codesandboxUrl = `https://codesandbox.io/s/github/${
library.repo
}/tree/${branch}/examples/${examplePath}?embed=1&theme=${
isDark ? 'dark' : 'light'
}`
}&file=${sandboxFirstFileName}`

const hideCodesandbox = library.hideCodesandboxUrl
const hideStackblitz = library.hideStackblitzUrl
Expand Down
38 changes: 26 additions & 12 deletions app/utils/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { type Framework } from '~/libraries'

export const getInitialSandboxFileName = (framework: Framework) =>
framework === 'svelte'
? 'src/App.svelte'
: framework === 'vue'
? 'src/App.vue'
: framework === 'solid'
? 'src/App.tsx'
: framework === 'angular'
? 'src/app/app.component.ts'
: framework === 'lit'
? 'src/main.ts'
: 'src/main.tsx'
export const getInitialSandboxFileName = (
framework: Framework,
libraryId?: string
) => {
const dir = 'src'

const file =
framework === 'angular'
? 'app.component'
: ['svelte', 'vue'].includes(framework)
? 'App'
: ['form', 'query'].includes(libraryId!)
? 'index'
: 'main'

const ext =
framework === 'svelte'
? 'svelte'
: framework === 'vue'
? 'vue'
: ['angular', 'lit'].includes(framework)
? 'ts'
: 'tsx'

return `${dir}/${file}.${ext}` as const
}
Loading